rijndael.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // Name: rijndael.h
  4. // Purpose:
  5. // Author: Ulrich Telle
  6. // Modified by:
  7. // Created: 2006-12-06
  8. // Copyright: (c) Ulrich Telle (Copyright for original code see below)
  9. // Licence: wxWindows licence
  10. //
  11. // The original code is unchanged
  12. ///////////////////////////////////////////////////////////////////////////////
  13. /// \file rijndael.h Interface of the Rijndael cipher
  14. */
  15. #ifndef _RIJNDAEL_H_
  16. #define _RIJNDAEL_H_
  17. /*
  18. // File : rijndael.h
  19. // Creation date : Sun Nov 5 2000 03:21:05 CEST
  20. // Author : Szymon Stefanek (stefanek@tin.it)
  21. //
  22. // Another implementation of the Rijndael cipher.
  23. // This is intended to be an easily usable library file.
  24. // This code is public domain.
  25. // Based on the Vincent Rijmen and K.U.Leuven implementation 2.4.
  26. //
  27. // Original Copyright notice:
  28. //
  29. // rijndael-alg-fst.c v2.4 April '2000
  30. // rijndael-alg-fst.h
  31. // rijndael-api-fst.c
  32. // rijndael-api-fst.h
  33. //
  34. // Optimised ANSI C code
  35. //
  36. // authors: v1.0: Antoon Bosselaers
  37. // v2.0: Vincent Rijmen, K.U.Leuven
  38. // v2.3: Paulo Barreto
  39. // v2.4: Vincent Rijmen, K.U.Leuven
  40. //
  41. // This code is placed in the public domain.
  42. //
  43. //
  44. // This implementation works on 128 , 192 , 256 bit keys
  45. // and on 128 bit blocks
  46. //
  47. //
  48. // Example of usage:
  49. //
  50. // // Input data
  51. // unsigned char key[32]; // The key
  52. // initializeYour256BitKey(); // Obviously initialized with sth
  53. // const unsigned char * plainText = getYourPlainText(); // Your plain text
  54. // int plainTextLen = strlen(plainText); // Plain text length
  55. //
  56. // // Encrypting
  57. // Rijndael rin;
  58. // unsigned char output[plainTextLen + 16];
  59. //
  60. // rin.init(Rijndael::CBC,Rijndael::Encrypt,key,Rijndael::Key32Bytes);
  61. // // It is a good idea to check the error code
  62. // int len = rin.padEncrypt(plainText,len,output);
  63. // if(len >= 0)useYourEncryptedText();
  64. // else encryptError(len);
  65. //
  66. // // Decrypting: we can reuse the same object
  67. // unsigned char output2[len];
  68. // rin.init(Rijndael::CBC,Rijndael::Decrypt,key,Rijndael::Key32Bytes));
  69. // len = rin.padDecrypt(output,len,output2);
  70. // if(len >= 0)useYourDecryptedText();
  71. // else decryptError(len);
  72. //
  73. */
  74. #define _MAX_KEY_COLUMNS (256/32)
  75. #define _MAX_ROUNDS 14
  76. #define MAX_IV_SIZE 16
  77. /* We assume that unsigned int is 32 bits long.... */
  78. typedef unsigned char UINT8;
  79. typedef unsigned int UINT32;
  80. typedef unsigned short UINT16;
  81. /* Error codes */
  82. #define RIJNDAEL_SUCCESS 0
  83. #define RIJNDAEL_UNSUPPORTED_MODE -1
  84. #define RIJNDAEL_UNSUPPORTED_DIRECTION -2
  85. #define RIJNDAEL_UNSUPPORTED_KEY_LENGTH -3
  86. #define RIJNDAEL_BAD_KEY -4
  87. #define RIJNDAEL_NOT_INITIALIZED -5
  88. #define RIJNDAEL_BAD_DIRECTION -6
  89. #define RIJNDAEL_CORRUPTED_DATA -7
  90. #define RIJNDAEL_Direction_Encrypt 0
  91. #define RIJNDAEL_Direction_Decrypt 1
  92. #define RIJNDAEL_Direction_Mode_ECB 0
  93. #define RIJNDAEL_Direction_Mode_CBC 1
  94. #define RIJNDAEL_Direction_Mode_CFB1 2
  95. #define RIJNDAEL_Direction_KeyLength_Key16Bytes 0
  96. #define RIJNDAEL_Direction_KeyLength_Key24Bytes 1
  97. #define RIJNDAEL_Direction_KeyLength_Key32Bytes 2
  98. #define RIJNDAEL_State_Valid 0
  99. #define RIJNDAEL_State_Invalid 1
  100. /*
  101. /// Class implementing the Rijndael cipher. (For internal use only)
  102. */
  103. typedef struct _Rijndael
  104. {
  105. int m_state;
  106. int m_mode;
  107. int m_direction;
  108. UINT8 m_initVector[MAX_IV_SIZE];
  109. UINT32 m_uRounds;
  110. UINT8 m_expandedKey[_MAX_ROUNDS+1][4][4];
  111. } Rijndael;
  112. void RijndaelCreate(Rijndael* rijndael);
  113. /*
  114. //////////////////////////////////////////////////////////////////////////////////////////
  115. // API
  116. //////////////////////////////////////////////////////////////////////////////////////////
  117. // init(): Initializes the crypt session
  118. // Returns RIJNDAEL_SUCCESS or an error code
  119. // mode : Rijndael::ECB, Rijndael::CBC or Rijndael::CFB1
  120. // You have to use the same mode for encrypting and decrypting
  121. // dir : Rijndael::Encrypt or Rijndael::Decrypt
  122. // A cipher instance works only in one direction
  123. // (Well , it could be easily modified to work in both
  124. // directions with a single init() call, but it looks
  125. // useless to me...anyway , it is a matter of generating
  126. // two expanded keys)
  127. // key : array of unsigned octets , it can be 16 , 24 or 32 bytes long
  128. // this CAN be binary data (it is not expected to be null terminated)
  129. // keyLen : Rijndael::Key16Bytes , Rijndael::Key24Bytes or Rijndael::Key32Bytes
  130. // initVector: initialization vector, you will usually use 0 here
  131. */
  132. int RijndaelInit(Rijndael* rijndael, int mode, int dir, UINT8* key, int keyLen, UINT8* initVector);
  133. /*
  134. // Encrypts the input array (can be binary data)
  135. // The input array length must be a multiple of 16 bytes, the remaining part
  136. // is DISCARDED.
  137. // so it actually encrypts inputLen / 128 blocks of input and puts it in outBuffer
  138. // Input len is in BITS!
  139. // outBuffer must be at least inputLen / 8 bytes long.
  140. // Returns the encrypted buffer length in BITS or an error code < 0 in case of error
  141. */
  142. int RijndaelBlockEncrypt(Rijndael* rijndael, UINT8 *input, int inputLen, UINT8 *outBuffer);
  143. /*
  144. // Encrypts the input array (can be binary data)
  145. // The input array can be any length , it is automatically padded on a 16 byte boundary.
  146. // Input len is in BYTES!
  147. // outBuffer must be at least (inputLen + 16) bytes long
  148. // Returns the encrypted buffer length in BYTES or an error code < 0 in case of error
  149. */
  150. int RijndaelPadEncrypt(Rijndael* rijndael, UINT8 *input, int inputOctets, UINT8 *outBuffer);
  151. /*
  152. // Decrypts the input vector
  153. // Input len is in BITS!
  154. // outBuffer must be at least inputLen / 8 bytes long
  155. // Returns the decrypted buffer length in BITS and an error code < 0 in case of error
  156. */
  157. int RijndaelBlockDecrypt(Rijndael* rijndael, UINT8 *input, int inputLen, UINT8 *outBuffer);
  158. /*
  159. // Decrypts the input vector
  160. // Input len is in BYTES!
  161. // outBuffer must be at least inputLen bytes long
  162. // Returns the decrypted buffer length in BYTES and an error code < 0 in case of error
  163. */
  164. int RijndaelPadDecrypt(Rijndael* rijndael, UINT8 *input, int inputOctets, UINT8 *outBuffer);
  165. void RijndaelInvalidate(Rijndael* rijndael);
  166. void RijndaelKeySched(Rijndael* rijndael, UINT8 key[_MAX_KEY_COLUMNS][4]);
  167. void RijndaelKeyEncToDec(Rijndael* rijndael);
  168. void RijndaelEncrypt(Rijndael* rijndael, UINT8 a[16], UINT8 b[16]);
  169. void RijndaelDecrypt(Rijndael* rijndael, UINT8 a[16], UINT8 b[16]);
  170. #endif /* _RIJNDAEL_H_ */