sw_aes.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef HEADER_AES_H
  2. #define HEADER_AES_H
  3. #include <stdint.h>
  4. /**************************************************************************
  5. * AES declarations
  6. **************************************************************************/
  7. #define AES_MAXROUNDS 14
  8. #define AES_BLOCKSIZE 16
  9. #define AES_IV_SIZE 16
  10. #ifndef htonl
  11. #define htonl(a) \
  12. ((((a) >> 24) & 0x000000ff) | \
  13. (((a) >> 8) & 0x0000ff00) | \
  14. (((a) << 8) & 0x00ff0000) | \
  15. (((a) << 24) & 0xff000000))
  16. #endif
  17. #ifndef ntohl
  18. #define ntohl(a) htonl((a))
  19. #endif
  20. typedef struct aes_key_st
  21. {
  22. uint16_t rounds;
  23. uint16_t key_size;
  24. uint32_t ks[(AES_MAXROUNDS+1)*8];
  25. uint8_t iv[AES_IV_SIZE];
  26. } AES_CTX;
  27. typedef enum
  28. {
  29. AES_MODE_128,
  30. AES_MODE_256
  31. } AES_MODE;
  32. /**
  33. * Set up AES with the key/iv and cipher size.
  34. */
  35. void AES_set_key(AES_CTX *ctx, const uint8_t *key,
  36. const uint8_t *iv, AES_MODE mode);
  37. /**
  38. * Encrypt a byte sequence (with a block size 16) using the AES cipher.
  39. */
  40. void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg,
  41. uint8_t *out, int length);
  42. /**
  43. * Decrypt a byte sequence (with a block size 16) using the AES cipher.
  44. */
  45. void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length);
  46. /**
  47. * Change a key for decryption.
  48. */
  49. void AES_convert_key(AES_CTX *ctx);
  50. /**
  51. * Decrypt a single block (16 bytes) of data
  52. */
  53. void AES_decrypt(const AES_CTX *ctx, uint32_t *data);
  54. /**
  55. * Encrypt a single block (16 bytes) of data
  56. */
  57. void AES_encrypt(const AES_CTX *ctx, uint32_t *data);
  58. #endif /* !HEADER_AES_H */