aes.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
  4. LICENSE TERMS
  5. The redistribution and use of this software (with or without changes)
  6. is allowed without the payment of fees or royalties provided that:
  7. 1. source code distributions include the above copyright notice, this
  8. list of conditions and the following disclaimer;
  9. 2. binary distributions include the above copyright notice, this list
  10. of conditions and the following disclaimer in their documentation;
  11. 3. the name of the copyright holder is not used to endorse products
  12. built using this software without specific written permission.
  13. DISCLAIMER
  14. This software is provided 'as is' with no explicit or implied warranties
  15. in respect of its properties, including, but not limited to, correctness
  16. and/or fitness for purpose.
  17. ---------------------------------------------------------------------------
  18. Issue 09/09/2006
  19. This is an AES implementation that uses only 8-bit byte operations on the
  20. cipher state.
  21. */
  22. #ifndef AES_H
  23. #define AES_H
  24. #if 1
  25. # define AES_ENC_PREKEYED /* AES encryption with a precomputed key schedule */
  26. #endif
  27. #if 0
  28. # define AES_DEC_PREKEYED /* AES decryption with a precomputed key schedule */
  29. #endif
  30. #if 0
  31. # define AES_ENC_128_OTFK /* AES encryption with 'on the fly' 128 bit keying */
  32. #endif
  33. #if 0
  34. # define AES_DEC_128_OTFK /* AES decryption with 'on the fly' 128 bit keying */
  35. #endif
  36. #if 0
  37. # define AES_ENC_256_OTFK /* AES encryption with 'on the fly' 256 bit keying */
  38. #endif
  39. #if 0
  40. # define AES_DEC_256_OTFK /* AES decryption with 'on the fly' 256 bit keying */
  41. #endif
  42. #define N_ROW 4
  43. #define N_COL 4
  44. #define N_BLOCK (N_ROW * N_COL)
  45. #define N_MAX_ROUNDS 14
  46. typedef uint8_t return_type;
  47. /* Warning: The key length for 256 bit keys overflows a byte
  48. (see comment below)
  49. */
  50. typedef uint8_t length_type;
  51. typedef struct
  52. { uint8_t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
  53. uint8_t rnd;
  54. } aes_context;
  55. /* The following calls are for a precomputed key schedule
  56. NOTE: If the length_type used for the key length is an
  57. unsigned 8-bit character, a key length of 256 bits must
  58. be entered as a length in bytes (valid inputs are hence
  59. 128, 192, 16, 24 and 32).
  60. */
  61. #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED )
  62. return_type aes_set_key( const uint8_t key[],
  63. length_type keylen,
  64. aes_context ctx[1] );
  65. #endif
  66. #if defined( AES_ENC_PREKEYED )
  67. return_type aes_encrypt( const uint8_t in[N_BLOCK],
  68. uint8_t out[N_BLOCK],
  69. const aes_context ctx[1] );
  70. return_type aes_cbc_encrypt( const uint8_t *in,
  71. uint8_t *out,
  72. int32_t n_block,
  73. uint8_t iv[N_BLOCK],
  74. const aes_context ctx[1] );
  75. #endif
  76. #if defined( AES_DEC_PREKEYED )
  77. return_type aes_decrypt( const uint8_t in[N_BLOCK],
  78. uint8_t out[N_BLOCK],
  79. const aes_context ctx[1] );
  80. return_type aes_cbc_decrypt( const uint8_t *in,
  81. uint8_t *out,
  82. int32_t n_block,
  83. uint8_t iv[N_BLOCK],
  84. const aes_context ctx[1] );
  85. #endif
  86. /* The following calls are for 'on the fly' keying. In this case the
  87. encryption and decryption keys are different.
  88. The encryption subroutines take a key in an array of bytes in
  89. key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
  90. 192, and 256 bits respectively. They then encrypts the input
  91. data, in[] with this key and put the reult in the output array
  92. out[]. In addition, the second key array, o_key[L], is used
  93. to output the key that is needed by the decryption subroutine
  94. to reverse the encryption operation. The two key arrays can
  95. be the same array but in this case the original key will be
  96. overwritten.
  97. In the same way, the decryption subroutines output keys that
  98. can be used to reverse their effect when used for encryption.
  99. Only 128 and 256 bit keys are supported in these 'on the fly'
  100. modes.
  101. */
  102. #if defined( AES_ENC_128_OTFK )
  103. void aes_encrypt_128( const uint8_t in[N_BLOCK],
  104. uint8_t out[N_BLOCK],
  105. const uint8_t key[N_BLOCK],
  106. uint8_t o_key[N_BLOCK] );
  107. #endif
  108. #if defined( AES_DEC_128_OTFK )
  109. void aes_decrypt_128( const uint8_t in[N_BLOCK],
  110. uint8_t out[N_BLOCK],
  111. const uint8_t key[N_BLOCK],
  112. uint8_t o_key[N_BLOCK] );
  113. #endif
  114. #if defined( AES_ENC_256_OTFK )
  115. void aes_encrypt_256( const uint8_t in[N_BLOCK],
  116. uint8_t out[N_BLOCK],
  117. const uint8_t key[2 * N_BLOCK],
  118. uint8_t o_key[2 * N_BLOCK] );
  119. #endif
  120. #if defined( AES_DEC_256_OTFK )
  121. void aes_decrypt_256( const uint8_t in[N_BLOCK],
  122. uint8_t out[N_BLOCK],
  123. const uint8_t key[2 * N_BLOCK],
  124. uint8_t o_key[2 * N_BLOCK] );
  125. #endif
  126. #endif