aes.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 1998-2006, Brian Gladman, Worcester, UK. All rights reserved.
  4. LICENSE TERMS
  5. The free distribution and use of this software in both source and binary
  6. form is allowed (with or without changes) provided that:
  7. 1. distributions of this source code include the above copyright
  8. notice, this list of conditions and the following disclaimer;
  9. 2. distributions in binary form include the above copyright
  10. notice, this list of conditions and the following disclaimer
  11. in the documentation and/or other associated materials;
  12. 3. the copyright holder's name is not used to endorse products
  13. built using this software without specific written permission.
  14. ALTERNATIVELY, provided that this notice is retained in full, this product
  15. may be distributed under the terms of the GNU General Public License (GPL),
  16. in which case the provisions of the GPL apply INSTEAD OF those given above.
  17. DISCLAIMER
  18. This software is provided 'as is' with no explicit or implied warranties
  19. in respect of its properties, including, but not limited to, correctness
  20. and/or fitness for purpose.
  21. ---------------------------------------------------------------------------
  22. Issue 09/09/2006
  23. This file contains the definitions required to use AES in C. See aesopt.h
  24. for optimisation details.
  25. */
  26. #ifndef __aes_h__
  27. #define __aes_h__
  28. #include <stdlib.h>
  29. //#include "zrtp_types.h"
  30. /* This include is used to find 8 & 32 bit unsigned integer types */
  31. #include "brg_types.h"
  32. #if defined(__cplusplus)
  33. extern "C"
  34. {
  35. #endif
  36. #ifndef ZRTP_RESTRICT
  37. #define AES_128 /* define if AES with 128 bit keys is needed */
  38. #define AES_192 /* define if AES with 192 bit keys is needed */
  39. #define AES_256 /* define if AES with 256 bit keys is needed */
  40. #define AES_VAR /* define if a variable key size is needed */
  41. #define AES_MODES /* define if support is needed for modes */
  42. #else
  43. #define AES_128 /* define if AES with 128 bit keys is needed */
  44. #define AES_256 /* define if AES with 256 bit keys is needed */
  45. #define AES_MODES /* define if support is needed for modes */
  46. #endif //ZRTP_RESTRICT
  47. /* The following must also be set in assembler files if being used */
  48. #define AES_ENCRYPT /* if support for encryption is needed */
  49. #ifndef ZRTP_RESTRICT
  50. #define AES_DECRYPT /* if support for decryption is needed */
  51. #define AES_ERR_CHK /* for parameter checks & error return codes */
  52. #define AES_REV_DKS /* define to reverse decryption key schedule */
  53. #else
  54. #define AES_DECRYPT /* if support for decryption is needed */
  55. #define AES_ERR_CHK /* for parameter checks & error return codes */
  56. #endif //ZRTP_RESTRICT
  57. #define AES_BLOCK_SIZE 16 /* the AES block size in bytes */
  58. #define N_COLS 4 /* the number of columns in the state */
  59. /* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */
  60. /* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */
  61. /* or 44, 52 or 60 32-bit words. */
  62. #if defined( AES_VAR ) || defined( AES_256 )
  63. #define KS_LENGTH 60
  64. #elif defined( AES_192 )
  65. #define KS_LENGTH 52
  66. #else
  67. #define KS_LENGTH 44
  68. #endif
  69. #if defined( AES_ERR_CHK )
  70. #define AES_RETURN INT_RETURN
  71. #else
  72. #define AES_RETURN VOID_RETURN
  73. #endif
  74. /* the character array 'inf' in the following structures is used */
  75. /* to hold AES context information. This AES code uses cx->inf.b[0] */
  76. /* to hold the number of rounds multiplied by 16. The other three */
  77. /* elements can be used by code that implements additional modes */
  78. typedef union
  79. { uint_32t l;
  80. uint_8t b[4];
  81. } aes_inf;
  82. typedef struct
  83. { uint_32t ks[KS_LENGTH];
  84. aes_inf inf;
  85. } aes_encrypt_ctx;
  86. typedef struct
  87. { uint_32t ks[KS_LENGTH];
  88. aes_inf inf;
  89. } aes_decrypt_ctx;
  90. /* This routine must be called before first use if non-static */
  91. /* tables are being used */
  92. AES_RETURN zrtp_bg_gen_tabs(void);
  93. /* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */
  94. /* those in the range 128 <= key_len <= 256 are given in bits */
  95. #if defined( AES_ENCRYPT )
  96. #if defined(AES_128) || defined(AES_VAR)
  97. AES_RETURN zrtp_bg_aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
  98. #endif
  99. #if defined(AES_192) || defined(AES_VAR)
  100. AES_RETURN zrtp_bg_aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
  101. #endif
  102. #if defined(AES_256) || defined(AES_VAR)
  103. AES_RETURN zrtp_bg_aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
  104. #endif
  105. #if defined(AES_VAR)
  106. AES_RETURN zrtp_bg_aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]);
  107. #endif
  108. AES_RETURN zrtp_bg_aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
  109. #endif
  110. #if defined( AES_DECRYPT )
  111. #if defined(AES_128) || defined(AES_VAR)
  112. AES_RETURN zrtp_bg_aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
  113. #endif
  114. #if defined(AES_192) || defined(AES_VAR)
  115. AES_RETURN zrtp_bg_aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
  116. #endif
  117. #if defined(AES_256) || defined(AES_VAR)
  118. AES_RETURN zrtp_bg_aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
  119. #endif
  120. #if defined(AES_VAR)
  121. AES_RETURN zrtp_bg_aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]);
  122. #endif
  123. AES_RETURN zrtp_bg_aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
  124. #endif
  125. #if defined(AES_MODES)
  126. /* Multiple calls to the following subroutines for multiple block */
  127. /* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */
  128. /* long messages incremantally provided that the context AND the iv */
  129. /* are preserved between all such calls. For the ECB and CBC modes */
  130. /* each individual call within a series of incremental calls must */
  131. /* process only full blocks (i.e. len must be a multiple of 16) but */
  132. /* the CFB, OFB and CTR mode calls can handle multiple incremental */
  133. /* calls of any length. Each mode is reset when a new AES key is */
  134. /* set but ECB and CBC operations can be reset without setting a */
  135. /* new key by setting a new IV value. To reset CFB, OFB and CTR */
  136. /* without setting the key, zrtp_bg_aes_mode_reset() must be called and the */
  137. /* IV must be set. NOTE: All these calls update the IV on exit so */
  138. /* this has to be reset if a new operation with the same IV as the */
  139. /* previous one is required (or decryption follows encryption with */
  140. /* the same IV array). */
  141. AES_RETURN zrtp_bg_aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
  142. int len, const aes_encrypt_ctx cx[1]);
  143. AES_RETURN zrtp_bg_aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
  144. int len, const aes_decrypt_ctx cx[1]);
  145. #ifndef ZRTP_RESTRICT
  146. AES_RETURN zrtp_bg_aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
  147. int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);
  148. AES_RETURN zrtp_bg_aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
  149. int len, unsigned char *iv, const aes_decrypt_ctx cx[1]);
  150. #endif //ZRTP_RESTRICT
  151. AES_RETURN zrtp_bg_aes_mode_reset(aes_encrypt_ctx cx[1]);
  152. AES_RETURN zrtp_bg_aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
  153. int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
  154. AES_RETURN zrtp_bg_aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
  155. int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
  156. #ifndef ZRTP_RESTRICT
  157. #define zrtp_bg_aes_ofb_encrypt zrtp_bg_aes_ofb_crypt
  158. #define zrtp_bg_aes_ofb_decrypt zrtp_bg_aes_ofb_crypt
  159. AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
  160. int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
  161. #endif //ZRTP_RESTRICT
  162. typedef void cbuf_inc(unsigned char *cbuf);
  163. #define zrtp_bg_aes_ctr_encrypt zrtp_bg_aes_ctr_crypt
  164. #define zrtp_bg_aes_ctr_decrypt zrtp_bg_aes_ctr_crypt
  165. //[winfix]
  166. AES_RETURN zrtp_bg_aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
  167. int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]);
  168. #endif
  169. #if defined(__cplusplus)
  170. }
  171. #endif
  172. #endif