iPXE
cbc.h
Go to the documentation of this file.
1#ifndef _IPXE_CBC_H
2#define _IPXE_CBC_H
3
4/** @file
5 *
6 * Cipher-block chaining
7 *
8 */
9
10FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11FILE_SECBOOT ( PERMITTED );
12
13#include <ipxe/crypto.h>
14
15/**
16 * Set key
17 *
18 * @v ctx Context
19 * @v key Key
20 * @v keylen Key length
21 * @v raw_cipher Underlying cipher algorithm
22 * @v cbc_ctx CBC context
23 * @ret rc Return status code
24 */
25static inline int cbc_setkey ( void *ctx, const void *key, size_t keylen,
26 struct cipher_algorithm *raw_cipher,
27 void *cbc_ctx __unused ) {
28
29 return cipher_setkey ( raw_cipher, ctx, key, keylen );
30}
31
32/**
33 * Set initialisation vector
34 *
35 * @v ctx Context
36 * @v iv Initialisation vector
37 * @v ivlen Initialisation vector length
38 * @v raw_cipher Underlying cipher algorithm
39 * @v cbc_ctx CBC context
40 */
41static inline void cbc_setiv ( void *ctx __unused,
42 const void *iv, size_t ivlen,
43 struct cipher_algorithm *raw_cipher,
44 void *cbc_ctx ) {
45 assert ( ivlen == raw_cipher->blocksize );
46 memcpy ( cbc_ctx, iv, raw_cipher->blocksize );
47}
48
49extern void cbc_encrypt ( void *ctx, const void *src, void *dst,
50 size_t len, struct cipher_algorithm *raw_cipher,
51 void *cbc_ctx );
52extern void cbc_decrypt ( void *ctx, const void *src, void *dst,
53 size_t len, struct cipher_algorithm *raw_cipher,
54 void *cbc_ctx );
55
56/**
57 * Create a cipher-block chaining mode of behaviour of an existing cipher
58 *
59 * @v _cbc_name Name for the new CBC cipher
60 * @v _cbc_cipher New cipher algorithm
61 * @v _raw_cipher Underlying cipher algorithm
62 * @v _raw_context Context structure for the underlying cipher
63 * @v _blocksize Cipher block size
64 */
65#define CBC_CIPHER( _cbc_name, _cbc_cipher, _raw_cipher, _raw_context, \
66 _blocksize ) \
67struct _cbc_name ## _context { \
68 _raw_context raw_ctx; \
69 uint8_t cbc_ctx[_blocksize]; \
70}; \
71static int _cbc_name ## _setkey ( void *ctx, const void *key, \
72 size_t keylen ) { \
73 struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
74 return cbc_setkey ( &_cbc_name ## _ctx->raw_ctx, key, keylen, \
75 &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx );\
76} \
77static void _cbc_name ## _setiv ( void *ctx, const void *iv, \
78 size_t ivlen ) { \
79 struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
80 cbc_setiv ( &_cbc_name ## _ctx->raw_ctx, iv, ivlen, \
81 &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx ); \
82} \
83static void _cbc_name ## _encrypt ( void *ctx, const void *src, \
84 void *dst, size_t len ) { \
85 struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
86 cbc_encrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len, \
87 &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx ); \
88} \
89static void _cbc_name ## _decrypt ( void *ctx, const void *src, \
90 void *dst, size_t len ) { \
91 struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
92 cbc_decrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len, \
93 &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx ); \
94} \
95struct cipher_algorithm _cbc_cipher = { \
96 .name = #_cbc_name, \
97 .ctxsize = sizeof ( struct _cbc_name ## _context ), \
98 .blocksize = _blocksize, \
99 .alignsize = _blocksize, \
100 .authsize = 0, \
101 .setkey = _cbc_name ## _setkey, \
102 .setiv = _cbc_name ## _setiv, \
103 .encrypt = _cbc_name ## _encrypt, \
104 .decrypt = _cbc_name ## _decrypt, \
105 .auth = cipher_null_auth, \
106};
107
108#endif /* _IPXE_CBC_H */
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
struct golan_eq_context ctx
Definition CIB_PRM.h:0
static const void * src
Definition string.h:48
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
void cbc_encrypt(void *ctx, const void *src, void *dst, size_t len, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
Encrypt data.
Definition cbc.c:67
static int cbc_setkey(void *ctx, const void *key, size_t keylen, struct cipher_algorithm *raw_cipher, void *cbc_ctx __unused)
Set key.
Definition cbc.h:25
void cbc_decrypt(void *ctx, const void *src, void *dst, size_t len, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
Decrypt data.
Definition cbc.c:93
static void cbc_setiv(void *ctx __unused, const void *iv, size_t ivlen, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
Set initialisation vector.
Definition cbc.h:41
ring len
Length.
Definition dwmac.h:226
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
Cryptographic API.
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition crypto.h:235
void * memcpy(void *dest, const void *src, size_t len) __nonnull
A cipher algorithm.
Definition crypto.h:51
size_t blocksize
Block size.
Definition crypto.h:61
u8 iv[16]
Initialization vector.
Definition wpa.h:33