aes.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 1
  28. # define AES_DEC_PREKEYED /* AES decryption with a precomputed key schedule */
  29. #endif
  30. #if 1
  31. # define AES_ENC_128_OTFK /* AES encryption with 'on the fly' 128 bit keying */
  32. #endif
  33. #if 1
  34. # define AES_DEC_128_OTFK /* AES decryption with 'on the fly' 128 bit keying */
  35. #endif
  36. #if 1
  37. # define AES_ENC_256_OTFK /* AES encryption with 'on the fly' 256 bit keying */
  38. #endif
  39. #if 1
  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 unsigned char uint_8t;
  47. typedef uint_8t return_type;
  48. /* Warning: The key length for 256 bit keys overflows a byte
  49. (see comment below)
  50. */
  51. typedef uint_8t length_type;
  52. typedef struct {
  53. uint_8t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
  54. uint_8t rnd;
  55. } aes_context;
  56. /* The following calls are for a precomputed key schedule
  57. NOTE: If the length_type used for the key length is an
  58. unsigned 8-bit character, a key length of 256 bits must
  59. be entered as a length in bytes (valid inputs are hence
  60. 128, 192, 16, 24 and 32).
  61. */
  62. #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED )
  63. return_type aes_set_key( const unsigned char key[],
  64. length_type keylen,
  65. aes_context ctx[1] );
  66. #endif
  67. #if defined( AES_ENC_PREKEYED )
  68. return_type bluedroid_aes_encrypt( const unsigned char in[N_BLOCK],
  69. unsigned char out[N_BLOCK],
  70. const aes_context ctx[1] );
  71. return_type aes_cbc_encrypt( const unsigned char *in,
  72. unsigned char *out,
  73. int n_block,
  74. unsigned char iv[N_BLOCK],
  75. const aes_context ctx[1] );
  76. #endif
  77. #if defined( AES_DEC_PREKEYED )
  78. return_type bluedroid_aes_decrypt( const unsigned char in[N_BLOCK],
  79. unsigned char out[N_BLOCK],
  80. const aes_context ctx[1] );
  81. return_type aes_cbc_decrypt( const unsigned char *in,
  82. unsigned char *out,
  83. int n_block,
  84. unsigned char iv[N_BLOCK],
  85. const aes_context ctx[1] );
  86. #endif
  87. /* The following calls are for 'on the fly' keying. In this case the
  88. encryption and decryption keys are different.
  89. The encryption subroutines take a key in an array of bytes in
  90. key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
  91. 192, and 256 bits respectively. They then encrypts the input
  92. data, in[] with this key and put the reult in the output array
  93. out[]. In addition, the second key array, o_key[L], is used
  94. to output the key that is needed by the decryption subroutine
  95. to reverse the encryption operation. The two key arrays can
  96. be the same array but in this case the original key will be
  97. overwritten.
  98. In the same way, the decryption subroutines output keys that
  99. can be used to reverse their effect when used for encryption.
  100. Only 128 and 256 bit keys are supported in these 'on the fly'
  101. modes.
  102. */
  103. #if defined( AES_ENC_128_OTFK )
  104. void bluedroid_aes_encrypt_128( const unsigned char in[N_BLOCK],
  105. unsigned char out[N_BLOCK],
  106. const unsigned char key[N_BLOCK],
  107. uint_8t o_key[N_BLOCK] );
  108. #endif
  109. #if defined( AES_DEC_128_OTFK )
  110. void bluedroid_aes_decrypt_128( const unsigned char in[N_BLOCK],
  111. unsigned char out[N_BLOCK],
  112. const unsigned char key[N_BLOCK],
  113. unsigned char o_key[N_BLOCK] );
  114. #endif
  115. #if defined( AES_ENC_256_OTFK )
  116. void bluedroid_aes_encrypt_256( const unsigned char in[N_BLOCK],
  117. unsigned char out[N_BLOCK],
  118. const unsigned char key[2 * N_BLOCK],
  119. unsigned char o_key[2 * N_BLOCK] );
  120. #endif
  121. #if defined( AES_DEC_256_OTFK )
  122. void bluedroid_aes_decrypt_256( const unsigned char in[N_BLOCK],
  123. unsigned char out[N_BLOCK],
  124. const unsigned char key[2 * N_BLOCK],
  125. unsigned char o_key[2 * N_BLOCK] );
  126. #endif
  127. #endif