6 * Cipher-block chaining
10 FILE_LICENCE ( GPL2_OR_LATER
);
12 #include <gpxe/crypto.h>
19 * @v keylen Key length
20 * @v raw_cipher Underlying cipher algorithm
21 * @v cbc_ctx CBC context
22 * @ret rc Return status code
24 static inline int cbc_setkey ( void *ctx
, const void *key
, size_t keylen
,
25 struct cipher_algorithm
*raw_cipher
,
26 void *cbc_ctx __unused
) {
28 return cipher_setkey ( raw_cipher
, ctx
, key
, keylen
);
32 * Set initialisation vector
35 * @v iv Initialisation vector
36 * @v raw_cipher Underlying cipher algorithm
37 * @v cbc_ctx CBC context
39 static inline void cbc_setiv ( void *ctx __unused
, const void *iv
,
40 struct cipher_algorithm
*raw_cipher
,
42 memcpy ( cbc_ctx
, iv
, raw_cipher
->blocksize
);
45 extern void cbc_encrypt ( void *ctx
, const void *src
, void *dst
,
46 size_t len
, struct cipher_algorithm
*raw_cipher
,
48 extern void cbc_decrypt ( void *ctx
, const void *src
, void *dst
,
49 size_t len
, struct cipher_algorithm
*raw_cipher
,
53 * Create a cipher-block chaining mode of behaviour of an existing cipher
55 * @v _cbc_name Name for the new CBC cipher
56 * @v _cbc_cipher New cipher algorithm
57 * @v _raw_cipher Underlying cipher algorithm
58 * @v _raw_context Context structure for the underlying cipher
59 * @v _blocksize Cipher block size
61 #define CBC_CIPHER( _cbc_name, _cbc_cipher, _raw_cipher, _raw_context, \
63 struct _cbc_name ## _context { \
64 _raw_context raw_ctx; \
65 uint8_t cbc_ctx[_blocksize]; \
67 static int _cbc_name ## _setkey ( void *ctx, const void *key, \
69 struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
70 return cbc_setkey ( &_cbc_name ## _ctx->raw_ctx, key, keylen, \
71 &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx );\
73 static void _cbc_name ## _setiv ( void *ctx, const void *iv ) { \
74 struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
75 cbc_setiv ( &_cbc_name ## _ctx->raw_ctx, iv, \
76 &_raw_cipher, &aes_cbc_ctx->cbc_ctx ); \
78 static void _cbc_name ## _encrypt ( void *ctx, const void *src, \
79 void *dst, size_t len ) { \
80 struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
81 cbc_encrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len, \
82 &_raw_cipher, &aes_cbc_ctx->cbc_ctx ); \
84 static void _cbc_name ## _decrypt ( void *ctx, const void *src, \
85 void *dst, size_t len ) { \
86 struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
87 cbc_decrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len, \
88 &_raw_cipher, &aes_cbc_ctx->cbc_ctx ); \
90 struct cipher_algorithm _cbc_cipher = { \
92 .ctxsize = sizeof ( struct _cbc_name ## _context ), \
93 .blocksize = _blocksize, \
94 .setkey = _cbc_name ## _setkey, \
95 .setiv = _cbc_name ## _setiv, \
96 .encrypt = _cbc_name ## _encrypt, \
97 .decrypt = _cbc_name ## _decrypt, \
100 #endif /* _GPXE_CBC_H */