iPXE
cbc.h
Go to the documentation of this file.
00001 #ifndef _IPXE_CBC_H
00002 #define _IPXE_CBC_H
00003 
00004 /** @file
00005  *
00006  * Cipher-block chaining
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <ipxe/crypto.h>
00013 
00014 /**
00015  * Set key
00016  *
00017  * @v ctx               Context
00018  * @v key               Key
00019  * @v keylen            Key length
00020  * @v raw_cipher        Underlying cipher algorithm
00021  * @v cbc_ctx           CBC context
00022  * @ret rc              Return status code
00023  */
00024 static inline int cbc_setkey ( void *ctx, const void *key, size_t keylen,
00025                                struct cipher_algorithm *raw_cipher,
00026                                void *cbc_ctx __unused ) {
00027 
00028         return cipher_setkey ( raw_cipher, ctx, key, keylen );
00029 }
00030 
00031 /**
00032  * Set initialisation vector
00033  *
00034  * @v ctx               Context
00035  * @v iv                Initialisation vector
00036  * @v raw_cipher        Underlying cipher algorithm
00037  * @v cbc_ctx           CBC context
00038  */
00039 static inline void cbc_setiv ( void *ctx __unused, const void *iv,
00040                                struct cipher_algorithm *raw_cipher,
00041                                void *cbc_ctx ) {
00042         memcpy ( cbc_ctx, iv, raw_cipher->blocksize );
00043 }
00044 
00045 extern void cbc_encrypt ( void *ctx, const void *src, void *dst,
00046                           size_t len, struct cipher_algorithm *raw_cipher,
00047                           void *cbc_ctx );
00048 extern void cbc_decrypt ( void *ctx, const void *src, void *dst,
00049                           size_t len, struct cipher_algorithm *raw_cipher,
00050                           void *cbc_ctx );
00051 
00052 /**
00053  * Create a cipher-block chaining mode of behaviour of an existing cipher
00054  *
00055  * @v _cbc_name         Name for the new CBC cipher
00056  * @v _cbc_cipher       New cipher algorithm
00057  * @v _raw_cipher       Underlying cipher algorithm
00058  * @v _raw_context      Context structure for the underlying cipher
00059  * @v _blocksize        Cipher block size
00060  */
00061 #define CBC_CIPHER( _cbc_name, _cbc_cipher, _raw_cipher, _raw_context,  \
00062                     _blocksize )                                        \
00063 struct _cbc_name ## _context {                                          \
00064         _raw_context raw_ctx;                                           \
00065         uint8_t cbc_ctx[_blocksize];                                    \
00066 };                                                                      \
00067 static int _cbc_name ## _setkey ( void *ctx, const void *key,           \
00068                                   size_t keylen ) {                     \
00069         struct _cbc_name ## _context * _cbc_name ## _ctx = ctx;         \
00070         return cbc_setkey ( &_cbc_name ## _ctx->raw_ctx, key, keylen,   \
00071                             &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx );\
00072 }                                                                       \
00073 static void _cbc_name ## _setiv ( void *ctx, const void *iv ) {         \
00074         struct _cbc_name ## _context * _cbc_name ## _ctx = ctx;         \
00075         cbc_setiv ( &_cbc_name ## _ctx->raw_ctx, iv,                    \
00076                     &_raw_cipher, &aes_cbc_ctx->cbc_ctx );              \
00077 }                                                                       \
00078 static void _cbc_name ## _encrypt ( void *ctx, const void *src,         \
00079                                     void *dst, size_t len ) {           \
00080         struct _cbc_name ## _context * _cbc_name ## _ctx = ctx;         \
00081         cbc_encrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len,       \
00082                       &_raw_cipher, &aes_cbc_ctx->cbc_ctx );            \
00083 }                                                                       \
00084 static void _cbc_name ## _decrypt ( void *ctx, const void *src,         \
00085                                     void *dst, size_t len ) {           \
00086         struct _cbc_name ## _context * _cbc_name ## _ctx = ctx;         \
00087         cbc_decrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len,       \
00088                       &_raw_cipher, &aes_cbc_ctx->cbc_ctx );            \
00089 }                                                                       \
00090 struct cipher_algorithm _cbc_cipher = {                                 \
00091         .name           = #_cbc_name,                                   \
00092         .ctxsize        = sizeof ( struct _cbc_name ## _context ),      \
00093         .blocksize      = _blocksize,                                   \
00094         .setkey         = _cbc_name ## _setkey,                         \
00095         .setiv          = _cbc_name ## _setiv,                          \
00096         .encrypt        = _cbc_name ## _encrypt,                        \
00097         .decrypt        = _cbc_name ## _decrypt,                        \
00098 };
00099 
00100 #endif /* _IPXE_CBC_H */