1 #ifndef _IPXE_CBC_H
2 #define _IPXE_CBC_H
3 
4 /** @file
5  *
6  * Cipher-block chaining
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <ipxe/crypto.h>
13 
14 /**
15  * Set key
16  *
17  * @v ctx		Context
18  * @v key		Key
19  * @v keylen		Key length
20  * @v raw_cipher	Underlying cipher algorithm
21  * @v cbc_ctx		CBC context
22  * @ret rc		Return status code
23  */
cbc_setkey(void * ctx,const void * key,size_t keylen,struct cipher_algorithm * raw_cipher,void * cbc_ctx __unused)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 ) {
27 
28 	return cipher_setkey ( raw_cipher, ctx, key, keylen );
29 }
30 
31 /**
32  * Set initialisation vector
33  *
34  * @v ctx		Context
35  * @v iv		Initialisation vector
36  * @v raw_cipher	Underlying cipher algorithm
37  * @v cbc_ctx		CBC context
38  */
cbc_setiv(void * ctx __unused,const void * iv,struct cipher_algorithm * raw_cipher,void * cbc_ctx)39 static inline void cbc_setiv ( void *ctx __unused, const void *iv,
40 			       struct cipher_algorithm *raw_cipher,
41 			       void *cbc_ctx ) {
42 	memcpy ( cbc_ctx, iv, raw_cipher->blocksize );
43 }
44 
45 extern void cbc_encrypt ( void *ctx, const void *src, void *dst,
46 			  size_t len, struct cipher_algorithm *raw_cipher,
47 			  void *cbc_ctx );
48 extern void cbc_decrypt ( void *ctx, const void *src, void *dst,
49 			  size_t len, struct cipher_algorithm *raw_cipher,
50 			  void *cbc_ctx );
51 
52 /**
53  * Create a cipher-block chaining mode of behaviour of an existing cipher
54  *
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
60  */
61 #define CBC_CIPHER( _cbc_name, _cbc_cipher, _raw_cipher, _raw_context,	\
62 		    _blocksize )					\
63 struct _cbc_name ## _context {						\
64 	_raw_context raw_ctx;						\
65 	uint8_t cbc_ctx[_blocksize];					\
66 };									\
67 static int _cbc_name ## _setkey ( void *ctx, const void *key,		\
68 				  size_t keylen ) {			\
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 );\
72 }									\
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 );		\
77 }									\
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 );		\
83 }									\
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 );		\
89 }									\
90 struct cipher_algorithm _cbc_cipher = {					\
91 	.name		= #_cbc_name,					\
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,			\
98 };
99 
100 #endif /* _IPXE_CBC_H */
101