00001 #ifndef _GPXE_CBC_H
00002 #define _GPXE_CBC_H
00003
00004
00005
00006
00007
00008
00009
00010 FILE_LICENCE ( GPL2_OR_LATER );
00011
00012 #include <gpxe/crypto.h>
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00033
00034
00035
00036
00037
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
00054
00055
00056
00057
00058
00059
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