twofish.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Fast, portable, and easy-to-use Twofish implementation,
  3. * Version 0.3.
  4. * Copyright (c) 2002 by Niels Ferguson.
  5. *
  6. * See the twofish.c file for the details of the how and why of this code.
  7. *
  8. * The author hereby grants a perpetual license to everybody to
  9. * use this code for any purpose as long as the copyright message is included
  10. * in the source code of this or any derived work.
  11. */
  12. #include <stdint.h>
  13. /*
  14. * PLATFORM FIXES
  15. * ==============
  16. *
  17. * The following definitions have to be fixed for each particular platform
  18. * you work on. If you have a multi-platform program, you no doubt have
  19. * portable definitions that you can substitute here without changing
  20. * the rest of the code.
  21. *
  22. * The defaults provided here should work on most PC compilers.
  23. */
  24. /*
  25. * A BYTE must be an unsigned 8-bit integer.
  26. * It must also be the elementary data size of your C platform,
  27. * i.e. sizeof( BYTE ) == 1.
  28. */
  29. typedef uint8_t BYTE;
  30. /*
  31. * A DWORD must be an unsigned integer of at least 32 bits.
  32. *
  33. * This type is used only internally in the implementation, so ideally it
  34. * would not appear in the header file, but it is used inside the
  35. * twofish_key structure which means it has to be included here.
  36. */
  37. typedef uint32_t DWORD;
  38. typedef size_t SIZE;
  39. /*
  40. * END OF PLATFORM FIXES
  41. * =====================
  42. *
  43. * You should not have to touch the rest of this file, but the code
  44. * in twofish.c has a few things you need to fix too.
  45. */
  46. #define TWOFISH_BLOCKSIZE 16
  47. #define TWOFISH_KEYSIZE 32
  48. #define TWOFISH_IVSIZE 16
  49. /*
  50. * Structure that contains a prepared Twofish key.
  51. * A cipher key is used in two stages. In the first stage it is converted
  52. * form the original form to an internal representation.
  53. * This internal form is then used to encrypt and decrypt data.
  54. * This structure contains the internal form. It is rather large: 4256 bytes
  55. * on a platform with 32-bit unsigned values.
  56. *
  57. * Treat this as an opague structure, and don't try to manipulate the
  58. * elements in it. I wish I could hide the inside of the structure,
  59. * but C doesn't allow that.
  60. */
  61. typedef struct {
  62. DWORD s[4][256]; /* pre-computed S-boxes */
  63. DWORD K[40]; /* Round key words */
  64. } twofish_key;
  65. typedef enum {
  66. twofish_option_CBC,
  67. twofish_option_PaddingPKCS7,
  68. twofish_options_default = twofish_option_CBC | twofish_option_PaddingPKCS7
  69. } twofish_options;
  70. typedef struct {
  71. BYTE iv[TWOFISH_IVSIZE];
  72. twofish_key key;
  73. twofish_options options;
  74. } twofish_context;
  75. /*
  76. * Initialise and test the Twofish implementation.
  77. *
  78. * This function MUST be called before any other function in the
  79. * Twofish implementation is called.
  80. * It only needs to be called once.
  81. *
  82. * Apart from initialising the implementation it performs a self test.
  83. * If the twofish_fatal function is not called, the code passed the test.
  84. * (See the twofish.c file for details on the twofish_fatal function.)
  85. */
  86. extern void twofish_initialise(void);
  87. /*
  88. * Convert a cipher key to the internal form used for
  89. * encryption and decryption.
  90. *
  91. * The cipher key is an array of bytes; the BYTE type is
  92. * defined above to a type suitable on your platform.
  93. *
  94. * Any key must be converted to an internal form in the Twofisk_key structure
  95. * before it can be used.
  96. * The encryption and decryption functions only work with the internal form.
  97. * The conversion to internal form need only be done once for each key value.
  98. *
  99. * Be sure to wipe all key storage, including the twofish_key structure,
  100. * once you are done with the key data.
  101. * A simple memset( TwofishKey, 0, sizeof( TwofishKey ) ) will do just fine.
  102. *
  103. * Unlike most implementations, this one allows any key size from 0 bytes
  104. * to 32 bytes. According to the Twofish specifications,
  105. * irregular key sizes are handled by padding the key with zeroes at the end
  106. * until the key size is 16, 24, or 32 bytes, whichever
  107. * comes first. Note that each key of irregular size is equivalent to exactly
  108. * one key of 16, 24, or 32 bytes.
  109. *
  110. * WARNING: Short keys have low entropy, and result in low security.
  111. * Anything less than 8 bytes is utterly insecure. For good security
  112. * use at least 16 bytes. I prefer to use 32-byte keys to prevent
  113. * any collision attacks on the key.
  114. *
  115. * The key length argument key_len must be in the proper range.
  116. * If key_len is not in the range 0,...,32 this routine attempts to generate
  117. * a fatal error (depending on the code environment),
  118. * and at best (or worst) returns without having done anything.
  119. *
  120. * Arguments:
  121. * key Array of key bytes
  122. * key_len Number of key bytes, must be in the range 0,1,...,32.
  123. * xkey Pointer to an twofish_key structure that will be filled
  124. * with the internal form of the cipher key.
  125. */
  126. extern void twofish_prepare_key(const BYTE *key, int key_len, twofish_key * xkey);
  127. /*
  128. * Encrypt a single block of data.
  129. *
  130. * This function encrypts a single block of 16 bytes of data.
  131. * If you want to encrypt a larger or variable-length message,
  132. * you will have to use a cipher mode, such as CBC or CTR.
  133. * These are outside the scope of this implementation.
  134. *
  135. * The xkey structure is not modified by this routine, and can be
  136. * used for further encryption and decryption operations.
  137. *
  138. * Arguments:
  139. * xkey pointer to twofish_key, internal form of the key
  140. * produces by twofish_prepare_key()
  141. * p Plaintext to be encrypted
  142. * c Place to store the ciphertext
  143. */
  144. extern void twofish_encrypt_block(twofish_key *xkey, BYTE p[TWOFISH_BLOCKSIZE], BYTE c[TWOFISH_BLOCKSIZE]);
  145. /*
  146. * Encrypt arbitratry dta
  147. *
  148. * This functions uses CBC and PKS#7 padding to encrypt the intput data
  149. *
  150. * Arguments:
  151. * context pointer to the context for the opration
  152. * input pointer to the plaintext input data array
  153. * input_lenght length of the plaintext array
  154. * output pointer to the output buffer. This needs to be preallocated by the caller!
  155. * output_length available space for the output buffer. Use twofish_get_output_lenght to determine the size an allocate the buffer!
  156. */
  157. extern void twofish_encrypt(twofish_context *context, const BYTE *input, SIZE input_length, BYTE *output, SIZE output_length);
  158. /*
  159. * Determine the output buffer size for a given input lenght
  160. *
  161. * This function simply calculated the size of the output if a given input is used.
  162. */
  163. extern SIZE twofish_get_output_length(const twofish_context *context, SIZE input_lenght);
  164. /*
  165. * Decrypt a single block of data.
  166. *
  167. * This function decrypts a single block of 16 bytes of data.
  168. * If you want to decrypt a larger or variable-length message,
  169. * you will have to use a cipher mode, such as CBC or CTR.
  170. * These are outside the scope of this implementation.
  171. *
  172. * The xkey structure is not modified by this routine, and can be
  173. * used for further encryption and decryption operations.
  174. *
  175. * Arguments:
  176. * xkey pointer to twofish_key, internal form of the key
  177. * produces by twofish_prepare_key()
  178. * c Ciphertext to be decrypted
  179. * p Place to store the plaintext
  180. */
  181. extern void twofish_decrypt_block(const twofish_key * xkey, const BYTE c[TWOFISH_BLOCKSIZE], BYTE p[TWOFISH_BLOCKSIZE]);
  182. /* output length denotes the space available in output and needs to be at least as big as input_length
  183. * after exiting, output_lenght contains the actual ammount of data written to output. Use this to copy the plaintext
  184. */
  185. extern void twofish_decrypt(twofish_context *context, const BYTE *input, SIZE input_length, BYTE *output, SIZE *output_length);
  186. extern void twofish_setup(twofish_context *context, const BYTE key[TWOFISH_KEYSIZE], const BYTE iv[TWOFISH_IVSIZE], twofish_options options);