threefish_api.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #ifndef THREEFISHAPI_H
  2. #define THREEFISHAPI_H
  3. /**
  4. * @file threefish_api.h
  5. * @brief A Threefish cipher API and its functions.
  6. * @{
  7. *
  8. * This API and the functions that implement this API simplify the usage
  9. * of the Threefish cipher. The design and the way to use the functions
  10. * follow the openSSL design but at the same time take care of some Threefish
  11. * specific behaviour and possibilities.
  12. *
  13. * These are the low level functions that deal with Threefish blocks only.
  14. * Implementations for cipher modes such as ECB, CFB, or CBC may use these
  15. * functions.
  16. *
  17. @code
  18. // Threefish cipher context data
  19. struct threefish_key key_ctx;
  20. // Initialize the context
  21. threefish_set_key(&key_ctx, THREEFISH_512, key, tweak);
  22. // Encrypt
  23. threefish_encrypt_block_bytes(&key_ctx, input, cipher);
  24. @endcode
  25. */
  26. #include <linux/types.h>
  27. #include "skein_base.h"
  28. #define KEY_SCHEDULE_CONST 0x1BD11BDAA9FC1A22L
  29. /**
  30. * Which Threefish size to use
  31. */
  32. enum threefish_size {
  33. THREEFISH_256 = 256, /*!< Skein with 256 bit state */
  34. THREEFISH_512 = 512, /*!< Skein with 512 bit state */
  35. THREEFISH_1024 = 1024 /*!< Skein with 1024 bit state */
  36. };
  37. /**
  38. * Context for Threefish key and tweak words.
  39. *
  40. * This structure was setup with some know-how of the internal
  41. * Skein structures, in particular ordering of header and size dependent
  42. * variables. If Skein implementation changes this, the adapt these
  43. * structures as well.
  44. */
  45. struct threefish_key {
  46. u64 state_size;
  47. u64 key[SKEIN_MAX_STATE_WORDS+1]; /* max number of key words*/
  48. u64 tweak[3];
  49. };
  50. /**
  51. * Set Threefish key and tweak data.
  52. *
  53. * This function sets the key and tweak data for the Threefish cipher of
  54. * the given size. The key data must have the same length (number of bits)
  55. * as the state size
  56. *
  57. * @param key_ctx
  58. * Pointer to a Threefish key structure.
  59. * @param size
  60. * Which Skein size to use.
  61. * @param key_data
  62. * Pointer to the key words (word has 64 bits).
  63. * @param tweak
  64. * Pointer to the two tweak words (word has 64 bits).
  65. */
  66. void threefish_set_key(struct threefish_key *key_ctx,
  67. enum threefish_size state_size,
  68. u64 *key_data, u64 *tweak);
  69. /**
  70. * Encrypt Threefish block (bytes).
  71. *
  72. * The buffer must have at least the same length (number of bits) as the
  73. * state size for this key. The function uses the first @c state_size bits
  74. * of the input buffer, encrypts them and stores the result in the output
  75. * buffer.
  76. *
  77. * @param key_ctx
  78. * Pointer to a Threefish key structure.
  79. * @param in
  80. * Poionter to plaintext data buffer.
  81. * @param out
  82. * Pointer to cipher buffer.
  83. */
  84. void threefish_encrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
  85. u8 *out);
  86. /**
  87. * Encrypt Threefish block (words).
  88. *
  89. * The buffer must have at least the same length (number of bits) as the
  90. * state size for this key. The function uses the first @c state_size bits
  91. * of the input buffer, encrypts them and stores the result in the output
  92. * buffer.
  93. *
  94. * The wordsize ist set to 64 bits.
  95. *
  96. * @param key_ctx
  97. * Pointer to a Threefish key structure.
  98. * @param in
  99. * Poionter to plaintext data buffer.
  100. * @param out
  101. * Pointer to cipher buffer.
  102. */
  103. void threefish_encrypt_block_words(struct threefish_key *key_ctx, u64 *in,
  104. u64 *out);
  105. /**
  106. * Decrypt Threefish block (bytes).
  107. *
  108. * The buffer must have at least the same length (number of bits) as the
  109. * state size for this key. The function uses the first @c state_size bits
  110. * of the input buffer, decrypts them and stores the result in the output
  111. * buffer
  112. *
  113. * @param key_ctx
  114. * Pointer to a Threefish key structure.
  115. * @param in
  116. * Poionter to cipher data buffer.
  117. * @param out
  118. * Pointer to plaintext buffer.
  119. */
  120. void threefish_decrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
  121. u8 *out);
  122. /**
  123. * Decrypt Threefish block (words).
  124. *
  125. * The buffer must have at least the same length (number of bits) as the
  126. * state size for this key. The function uses the first @c state_size bits
  127. * of the input buffer, encrypts them and stores the result in the output
  128. * buffer.
  129. *
  130. * The wordsize ist set to 64 bits.
  131. *
  132. * @param key_ctx
  133. * Pointer to a Threefish key structure.
  134. * @param in
  135. * Poionter to cipher data buffer.
  136. * @param out
  137. * Pointer to plaintext buffer.
  138. */
  139. void threefish_decrypt_block_words(struct threefish_key *key_ctx, u64 *in,
  140. u64 *out);
  141. void threefish_encrypt_256(struct threefish_key *key_ctx, u64 *input,
  142. u64 *output);
  143. void threefish_encrypt_512(struct threefish_key *key_ctx, u64 *input,
  144. u64 *output);
  145. void threefish_encrypt_1024(struct threefish_key *key_ctx, u64 *input,
  146. u64 *output);
  147. void threefish_decrypt_256(struct threefish_key *key_ctx, u64 *input,
  148. u64 *output);
  149. void threefish_decrypt_512(struct threefish_key *key_ctx, u64 *input,
  150. u64 *output);
  151. void threefish_decrypt_1024(struct threefish_key *key_ctx, u64 *input,
  152. u64 *output);
  153. /**
  154. * @}
  155. */
  156. #endif