skein_api.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * Copyright (c) 2010 Werner Dittmann
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use,
  8. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following
  11. * conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef SKEINAPI_H
  26. #define SKEINAPI_H
  27. /**
  28. * @file skein_api.h
  29. * @brief A Skein API and its functions.
  30. * @{
  31. *
  32. * This API and the functions that implement this API simplify the usage
  33. * of Skein. The design and the way to use the functions follow the openSSL
  34. * design but at the same time take care of some Skein specific behaviour
  35. * and possibilities.
  36. *
  37. * The functions enable applications to create a normal Skein hashes and
  38. * message authentication codes (MAC).
  39. *
  40. * Using these functions is simple and straight forward:
  41. *
  42. * @code
  43. *
  44. * #include "skein_api.h"
  45. *
  46. * ...
  47. * struct skein_ctx ctx; // a Skein hash or MAC context
  48. *
  49. * // prepare context, here for a Skein with a state size of 512 bits.
  50. * skein_ctx_prepare(&ctx, SKEIN_512);
  51. *
  52. * // Initialize the context to set the requested hash length in bits
  53. * // here request a output hash size of 31 bits (Skein supports variable
  54. * // output sizes even very strange sizes)
  55. * skein_init(&ctx, 31);
  56. *
  57. * // Now update Skein with any number of message bits. A function that
  58. * // takes a number of bytes is also available.
  59. * skein_update_bits(&ctx, message, msg_length);
  60. *
  61. * // Now get the result of the Skein hash. The output buffer must be
  62. * // large enough to hold the request number of output bits. The application
  63. * // may now extract the bits.
  64. * skein_final(&ctx, result);
  65. * ...
  66. * @endcode
  67. *
  68. * An application may use @c skein_reset to reset a Skein context and use
  69. * it for creation of another hash with the same Skein state size and output
  70. * bit length. In this case the API implementation restores some internal
  71. * internal state data and saves a full Skein initialization round.
  72. *
  73. * To create a MAC the application just uses @c skein_mac_init instead of
  74. * @c skein_init. All other functions calls remain the same.
  75. *
  76. */
  77. #include <linux/types.h>
  78. #include "skein_base.h"
  79. /**
  80. * Which Skein size to use
  81. */
  82. enum skein_size {
  83. SKEIN_256 = 256, /*!< Skein with 256 bit state */
  84. SKEIN_512 = 512, /*!< Skein with 512 bit state */
  85. SKEIN_1024 = 1024 /*!< Skein with 1024 bit state */
  86. };
  87. /**
  88. * Context for Skein.
  89. *
  90. * This structure was setup with some know-how of the internal
  91. * Skein structures, in particular ordering of header and size dependent
  92. * variables. If Skein implementation changes this, then adapt these
  93. * structures as well.
  94. */
  95. struct skein_ctx {
  96. u64 skein_size;
  97. u64 x_save[SKEIN_MAX_STATE_WORDS]; /* save area for state variables */
  98. union {
  99. struct skein_ctx_hdr h;
  100. struct skein_256_ctx s256;
  101. struct skein_512_ctx s512;
  102. struct skein_1024_ctx s1024;
  103. } m;
  104. };
  105. /**
  106. * Prepare a Skein context.
  107. *
  108. * An application must call this function before it can use the Skein
  109. * context. The functions clears memory and initializes size dependent
  110. * variables.
  111. *
  112. * @param ctx
  113. * Pointer to a Skein context.
  114. * @param size
  115. * Which Skein size to use.
  116. * @return
  117. * SKEIN_SUCCESS of SKEIN_FAIL
  118. */
  119. int skein_ctx_prepare(struct skein_ctx *ctx, enum skein_size size);
  120. /**
  121. * Initialize a Skein context.
  122. *
  123. * Initializes the context with this data and saves the resulting Skein
  124. * state variables for further use.
  125. *
  126. * @param ctx
  127. * Pointer to a Skein context.
  128. * @param hash_bit_len
  129. * Number of MAC hash bits to compute
  130. * @return
  131. * SKEIN_SUCCESS of SKEIN_FAIL
  132. * @see skein_reset
  133. */
  134. int skein_init(struct skein_ctx *ctx, size_t hash_bit_len);
  135. /**
  136. * Resets a Skein context for further use.
  137. *
  138. * Restores the saved chaining variables to reset the Skein context.
  139. * Thus applications can reuse the same setup to process several
  140. * messages. This saves a complete Skein initialization cycle.
  141. *
  142. * @param ctx
  143. * Pointer to a pre-initialized Skein MAC context
  144. */
  145. void skein_reset(struct skein_ctx *ctx);
  146. /**
  147. * Initializes a Skein context for MAC usage.
  148. *
  149. * Initializes the context with this data and saves the resulting Skein
  150. * state variables for further use.
  151. *
  152. * Applications call the normal Skein functions to update the MAC and
  153. * get the final result.
  154. *
  155. * @param ctx
  156. * Pointer to an empty or preinitialized Skein MAC context
  157. * @param key
  158. * Pointer to key bytes or NULL
  159. * @param key_len
  160. * Length of the key in bytes or zero
  161. * @param hash_bit_len
  162. * Number of MAC hash bits to compute
  163. * @return
  164. * SKEIN_SUCCESS of SKEIN_FAIL
  165. */
  166. int skein_mac_init(struct skein_ctx *ctx, const u8 *key, size_t key_len,
  167. size_t hash_bit_len);
  168. /**
  169. * Update Skein with the next part of the message.
  170. *
  171. * @param ctx
  172. * Pointer to initialized Skein context
  173. * @param msg
  174. * Pointer to the message.
  175. * @param msg_byte_cnt
  176. * Length of the message in @b bytes
  177. * @return
  178. * Success or error code.
  179. */
  180. int skein_update(struct skein_ctx *ctx, const u8 *msg,
  181. size_t msg_byte_cnt);
  182. /**
  183. * Update the hash with a message bit string.
  184. *
  185. * Skein can handle data not only as bytes but also as bit strings of
  186. * arbitrary length (up to its maximum design size).
  187. *
  188. * @param ctx
  189. * Pointer to initialized Skein context
  190. * @param msg
  191. * Pointer to the message.
  192. * @param msg_bit_cnt
  193. * Length of the message in @b bits.
  194. */
  195. int skein_update_bits(struct skein_ctx *ctx, const u8 *msg,
  196. size_t msg_bit_cnt);
  197. /**
  198. * Finalize Skein and return the hash.
  199. *
  200. * Before an application can reuse a Skein setup the application must
  201. * reset the Skein context.
  202. *
  203. * @param ctx
  204. * Pointer to initialized Skein context
  205. * @param hash
  206. * Pointer to buffer that receives the hash. The buffer must be large
  207. * enough to store @c hash_bit_len bits.
  208. * @return
  209. * Success or error code.
  210. * @see skein_reset
  211. */
  212. int skein_final(struct skein_ctx *ctx, u8 *hash);
  213. /**
  214. * @}
  215. */
  216. #endif