aes.h 5.5 KB

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