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.1KB

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