You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cbc.h 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef _GPXE_CBC_H
  2. #define _GPXE_CBC_H
  3. /** @file
  4. *
  5. * Cipher-block chaining
  6. *
  7. */
  8. #include <gpxe/crypto.h>
  9. /**
  10. * Set key
  11. *
  12. * @v ctx Context
  13. * @v key Key
  14. * @v keylen Key length
  15. * @v raw_cipher Underlying cipher algorithm
  16. * @v cbc_ctx CBC context
  17. * @ret rc Return status code
  18. */
  19. static inline int cbc_setkey ( void *ctx, const void *key, size_t keylen,
  20. struct cipher_algorithm *raw_cipher,
  21. void *cbc_ctx __unused ) {
  22. return cipher_setkey ( raw_cipher, ctx, key, keylen );
  23. }
  24. /**
  25. * Set initialisation vector
  26. *
  27. * @v ctx Context
  28. * @v iv Initialisation vector
  29. * @v raw_cipher Underlying cipher algorithm
  30. * @v cbc_ctx CBC context
  31. */
  32. static inline void cbc_setiv ( void *ctx __unused, const void *iv,
  33. struct cipher_algorithm *raw_cipher,
  34. void *cbc_ctx ) {
  35. memcpy ( cbc_ctx, iv, raw_cipher->blocksize );
  36. }
  37. extern void cbc_encrypt ( void *ctx, const void *src, void *dst,
  38. size_t len, struct cipher_algorithm *raw_cipher,
  39. void *cbc_ctx );
  40. extern void cbc_decrypt ( void *ctx, const void *src, void *dst,
  41. size_t len, struct cipher_algorithm *raw_cipher,
  42. void *cbc_ctx );
  43. /**
  44. * Create a cipher-block chaining mode of behaviour of an existing cipher
  45. *
  46. * @v _cbc_name Name for the new CBC cipher
  47. * @v _cbc_cipher New cipher algorithm
  48. * @v _raw_cipher Underlying cipher algorithm
  49. * @v _raw_context Context structure for the underlying cipher
  50. * @v _blocksize Cipher block size
  51. */
  52. #define CBC_CIPHER( _cbc_name, _cbc_cipher, _raw_cipher, _raw_context, \
  53. _blocksize ) \
  54. struct _cbc_name ## _context { \
  55. _raw_context raw_ctx; \
  56. uint8_t cbc_ctx[_blocksize]; \
  57. }; \
  58. static int _cbc_name ## _setkey ( void *ctx, const void *key, \
  59. size_t keylen ) { \
  60. struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
  61. return cbc_setkey ( &_cbc_name ## _ctx->raw_ctx, key, keylen, \
  62. &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx );\
  63. } \
  64. static void _cbc_name ## _setiv ( void *ctx, const void *iv ) { \
  65. struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
  66. cbc_setiv ( &_cbc_name ## _ctx->raw_ctx, iv, \
  67. &_raw_cipher, &aes_cbc_ctx->cbc_ctx ); \
  68. } \
  69. static void _cbc_name ## _encrypt ( void *ctx, const void *src, \
  70. void *dst, size_t len ) { \
  71. struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
  72. cbc_encrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len, \
  73. &_raw_cipher, &aes_cbc_ctx->cbc_ctx ); \
  74. } \
  75. static void _cbc_name ## _decrypt ( void *ctx, const void *src, \
  76. void *dst, size_t len ) { \
  77. struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
  78. cbc_decrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len, \
  79. &_raw_cipher, &aes_cbc_ctx->cbc_ctx ); \
  80. } \
  81. struct cipher_algorithm _cbc_cipher = { \
  82. .name = #_cbc_name, \
  83. .ctxsize = sizeof ( struct _cbc_name ## _context ), \
  84. .blocksize = _blocksize, \
  85. .setkey = _cbc_name ## _setkey, \
  86. .setiv = _cbc_name ## _setiv, \
  87. .encrypt = _cbc_name ## _encrypt, \
  88. .decrypt = _cbc_name ## _decrypt, \
  89. };
  90. #endif /* _GPXE_CBC_H */