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.

axtls_aes.c 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. static int aes_setkey ( void *ctx, const void *key, size_t keylen ) {
  7. AES_CTX *aesctx = ctx;
  8. AES_MODE mode;
  9. switch ( keylen ) {
  10. case ( 128 / 8 ):
  11. mode = AES_MODE_128;
  12. break;
  13. case ( 256 / 8 ):
  14. mode = AES_MODE_256;
  15. break;
  16. default:
  17. return -EINVAL;
  18. }
  19. AES_set_key ( aesctx, key, aesctx->iv, mode );
  20. return 0;
  21. }
  22. static void aes_setiv ( void *ctx, const void *iv ) {
  23. AES_CTX *aesctx = ctx;
  24. memcpy ( aesctx->iv, iv, sizeof ( aesctx->iv ) );
  25. }
  26. static void aes_encrypt ( void *ctx, const void *data, void *dst,
  27. size_t len ) {
  28. AES_CTX *aesctx = ctx;
  29. AES_cbc_encrypt ( aesctx, data, dst, len );
  30. }
  31. static void aes_decrypt ( void *ctx, const void *data, void *dst,
  32. size_t len ) {
  33. AES_CTX *aesctx = ctx;
  34. AES_cbc_decrypt ( aesctx, data, dst, len );
  35. }
  36. struct crypto_algorithm aes_algorithm = {
  37. .name = "aes",
  38. .ctxsize = sizeof ( AES_CTX ),
  39. .blocksize = 16,
  40. .setkey = aes_setkey,
  41. .setiv = aes_setiv,
  42. .encode = aes_encrypt,
  43. .decode = aes_decrypt,
  44. };