Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

axtls_aes.c 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "crypto/axtls/crypto.h"
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <gpxe/crypto.h>
  5. #include <gpxe/aes.h>
  6. struct aes_cbc_context {
  7. AES_CTX ctx;
  8. int decrypting;
  9. };
  10. static int aes_cbc_setkey ( void *ctx, const void *key, size_t keylen ) {
  11. struct aes_cbc_context *aesctx = ctx;
  12. AES_MODE mode;
  13. switch ( keylen ) {
  14. case ( 128 / 8 ):
  15. mode = AES_MODE_128;
  16. break;
  17. case ( 256 / 8 ):
  18. mode = AES_MODE_256;
  19. break;
  20. default:
  21. return -EINVAL;
  22. }
  23. AES_set_key ( &aesctx->ctx, key, aesctx->ctx.iv, mode );
  24. aesctx->decrypting = 0;
  25. return 0;
  26. }
  27. static void aes_cbc_setiv ( void *ctx, const void *iv ) {
  28. struct aes_cbc_context *aesctx = ctx;
  29. memcpy ( aesctx->ctx.iv, iv, sizeof ( aesctx->ctx.iv ) );
  30. }
  31. static void aes_cbc_encrypt ( void *ctx, const void *data, void *dst,
  32. size_t len ) {
  33. struct aes_cbc_context *aesctx = ctx;
  34. if ( aesctx->decrypting )
  35. assert ( 0 );
  36. AES_cbc_encrypt ( &aesctx->ctx, data, dst, len );
  37. }
  38. static void aes_cbc_decrypt ( void *ctx, const void *data, void *dst,
  39. size_t len ) {
  40. struct aes_cbc_context *aesctx = ctx;
  41. if ( ! aesctx->decrypting ) {
  42. AES_convert_key ( &aesctx->ctx );
  43. aesctx->decrypting = 1;
  44. }
  45. AES_cbc_decrypt ( &aesctx->ctx, data, dst, len );
  46. }
  47. struct crypto_algorithm aes_cbc_algorithm = {
  48. .name = "aes_cbc",
  49. .ctxsize = sizeof ( struct aes_cbc_context ),
  50. .blocksize = 16,
  51. .setkey = aes_cbc_setkey,
  52. .setiv = aes_cbc_setiv,
  53. .encode = aes_cbc_encrypt,
  54. .decode = aes_cbc_decrypt,
  55. };