blob: 7da8b386a28c4ee0b345f63040644559373b3114 [file] [log] [blame]
Jason Cooper449bb812014-03-24 01:48:58 +00001/*
2Copyright (c) 2010 Werner Dittmann
3
4Permission is hereby granted, free of charge, to any person
5obtaining a copy of this software and associated documentation
6files (the "Software"), to deal in the Software without
7restriction, including without limitation the rights to use,
8copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the
10Software is furnished to do so, subject to the following
11conditions:
12
13The above copyright notice and this permission notice shall be
14included in all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23OTHER DEALINGS IN THE SOFTWARE.
24
25*/
26
27#ifndef SKEINAPI_H
28#define SKEINAPI_H
29
30/**
Jake Edge85dfd522014-05-19 17:48:24 -060031 * @file skein_api.h
Jason Cooper449bb812014-03-24 01:48:58 +000032 * @brief A Skein API and its functions.
33 * @{
34 *
35 * This API and the functions that implement this API simplify the usage
36 * of Skein. The design and the way to use the functions follow the openSSL
37 * design but at the same time take care of some Skein specific behaviour
38 * and possibilities.
Jason Cooper06a620f2014-03-24 01:49:10 +000039 *
Jason Cooper449bb812014-03-24 01:48:58 +000040 * The functions enable applications to create a normal Skein hashes and
41 * message authentication codes (MAC).
Jason Cooper06a620f2014-03-24 01:49:10 +000042 *
Jason Cooper449bb812014-03-24 01:48:58 +000043 * Using these functions is simple and straight forward:
Jason Cooper06a620f2014-03-24 01:49:10 +000044 *
Jason Cooper449bb812014-03-24 01:48:58 +000045 * @code
Jason Cooper06a620f2014-03-24 01:49:10 +000046 *
Jake Edge85dfd522014-05-19 17:48:24 -060047 * #include "skein_api.h"
Jason Cooper06a620f2014-03-24 01:49:10 +000048 *
Jason Cooper449bb812014-03-24 01:48:58 +000049 * ...
Jason Cooper11d9ffb2014-03-24 01:49:04 +000050 * struct skein_ctx ctx; // a Skein hash or MAC context
Jason Cooper06a620f2014-03-24 01:49:10 +000051 *
Jason Cooper449bb812014-03-24 01:48:58 +000052 * // prepare context, here for a Skein with a state size of 512 bits.
Anton Saraev9435d3a2014-05-19 12:09:57 +040053 * skein_ctx_prepare(&ctx, SKEIN_512);
Jason Cooper06a620f2014-03-24 01:49:10 +000054 *
Jason Cooper449bb812014-03-24 01:48:58 +000055 * // Initialize the context to set the requested hash length in bits
56 * // here request a output hash size of 31 bits (Skein supports variable
57 * // output sizes even very strange sizes)
Anton Saraev68ace622014-05-19 12:09:54 +040058 * skein_init(&ctx, 31);
Jason Cooper06a620f2014-03-24 01:49:10 +000059 *
Jason Cooper449bb812014-03-24 01:48:58 +000060 * // Now update Skein with any number of message bits. A function that
61 * // takes a number of bytes is also available.
Anton Saraev95f1840a2014-05-19 12:09:55 +040062 * skein_update_bits(&ctx, message, msg_length);
Jason Cooper06a620f2014-03-24 01:49:10 +000063 *
Jason Cooper449bb812014-03-24 01:48:58 +000064 * // Now get the result of the Skein hash. The output buffer must be
65 * // large enough to hold the request number of output bits. The application
66 * // may now extract the bits.
Anton Saraev68ace622014-05-19 12:09:54 +040067 * skein_final(&ctx, result);
Jason Cooper449bb812014-03-24 01:48:58 +000068 * ...
69 * @endcode
Jason Cooper06a620f2014-03-24 01:49:10 +000070 *
Anton Saraev68ace622014-05-19 12:09:54 +040071 * An application may use @c skein_reset to reset a Skein context and use
Jason Cooper449bb812014-03-24 01:48:58 +000072 * it for creation of another hash with the same Skein state size and output
73 * bit length. In this case the API implementation restores some internal
74 * internal state data and saves a full Skein initialization round.
Jason Cooper06a620f2014-03-24 01:49:10 +000075 *
Anton Saraev68ace622014-05-19 12:09:54 +040076 * To create a MAC the application just uses @c skein_mac_init instead of
77 * @c skein_init. All other functions calls remain the same.
Jason Cooper06a620f2014-03-24 01:49:10 +000078 *
Jason Cooper449bb812014-03-24 01:48:58 +000079 */
80
Jason Cooperc2c74262014-03-24 02:32:49 +000081#include <linux/types.h>
Eric Rostc17cdeb2014-10-24 17:32:53 -050082#include "skein_base.h"
Jason Cooper449bb812014-03-24 01:48:58 +000083
Jason Cooper39bd42b2014-03-24 01:49:09 +000084/**
85 * Which Skein size to use
86 */
87enum skein_size {
Anton Saraev9435d3a2014-05-19 12:09:57 +040088 SKEIN_256 = 256, /*!< Skein with 256 bit state */
89 SKEIN_512 = 512, /*!< Skein with 512 bit state */
90 SKEIN_1024 = 1024 /*!< Skein with 1024 bit state */
Jason Cooper39bd42b2014-03-24 01:49:09 +000091};
Jason Cooper449bb812014-03-24 01:48:58 +000092
Jason Cooper39bd42b2014-03-24 01:49:09 +000093/**
94 * Context for Skein.
95 *
96 * This structure was setup with some know-how of the internal
97 * Skein structures, in particular ordering of header and size dependent
98 * variables. If Skein implementation changes this, then adapt these
99 * structures as well.
100 */
101struct skein_ctx {
Anton Saraev95f1840a2014-05-19 12:09:55 +0400102 u64 skein_size;
Jake Edge007dfe52014-05-20 08:02:28 -0600103 u64 x_save[SKEIN_MAX_STATE_WORDS]; /* save area for state variables */
Jason Cooper39bd42b2014-03-24 01:49:09 +0000104 union {
105 struct skein_ctx_hdr h;
106 struct skein_256_ctx s256;
107 struct skein_512_ctx s512;
Anton Saraev3201b7f2014-05-19 12:09:56 +0400108 struct skein_1024_ctx s1024;
Jason Cooper39bd42b2014-03-24 01:49:09 +0000109 } m;
110};
Jason Cooper449bb812014-03-24 01:48:58 +0000111
Jason Cooper39bd42b2014-03-24 01:49:09 +0000112/**
113 * Prepare a Skein context.
Jason Cooper06a620f2014-03-24 01:49:10 +0000114 *
Jason Cooper39bd42b2014-03-24 01:49:09 +0000115 * An application must call this function before it can use the Skein
116 * context. The functions clears memory and initializes size dependent
117 * variables.
118 *
119 * @param ctx
120 * Pointer to a Skein context.
121 * @param size
122 * Which Skein size to use.
123 * @return
Colin Cronin20ba4582015-05-13 18:05:42 -0700124 * SKEIN_SUCCESS of SKEIN_FAIL
Jason Cooper39bd42b2014-03-24 01:49:09 +0000125 */
Anton Saraev68ace622014-05-19 12:09:54 +0400126int skein_ctx_prepare(struct skein_ctx *ctx, enum skein_size size);
Jason Cooper449bb812014-03-24 01:48:58 +0000127
Jason Cooper39bd42b2014-03-24 01:49:09 +0000128/**
129 * Initialize a Skein context.
130 *
Jason Cooper06a620f2014-03-24 01:49:10 +0000131 * Initializes the context with this data and saves the resulting Skein
Jason Cooper39bd42b2014-03-24 01:49:09 +0000132 * state variables for further use.
133 *
134 * @param ctx
135 * Pointer to a Skein context.
Anton Saraev95f1840a2014-05-19 12:09:55 +0400136 * @param hash_bit_len
Jason Cooper39bd42b2014-03-24 01:49:09 +0000137 * Number of MAC hash bits to compute
138 * @return
Colin Cronin20ba4582015-05-13 18:05:42 -0700139 * SKEIN_SUCCESS of SKEIN_FAIL
Anton Saraev68ace622014-05-19 12:09:54 +0400140 * @see skein_reset
Jason Cooper39bd42b2014-03-24 01:49:09 +0000141 */
Anton Saraev95f1840a2014-05-19 12:09:55 +0400142int skein_init(struct skein_ctx *ctx, size_t hash_bit_len);
Jason Cooper449bb812014-03-24 01:48:58 +0000143
Jason Cooper39bd42b2014-03-24 01:49:09 +0000144/**
145 * Resets a Skein context for further use.
Jason Cooper06a620f2014-03-24 01:49:10 +0000146 *
147 * Restores the saved chaining variables to reset the Skein context.
148 * Thus applications can reuse the same setup to process several
Jason Cooper39bd42b2014-03-24 01:49:09 +0000149 * messages. This saves a complete Skein initialization cycle.
Jason Cooper06a620f2014-03-24 01:49:10 +0000150 *
Jason Cooper39bd42b2014-03-24 01:49:09 +0000151 * @param ctx
152 * Pointer to a pre-initialized Skein MAC context
153 */
Anton Saraev68ace622014-05-19 12:09:54 +0400154void skein_reset(struct skein_ctx *ctx);
Jason Cooper449bb812014-03-24 01:48:58 +0000155
Jason Cooper39bd42b2014-03-24 01:49:09 +0000156/**
157 * Initializes a Skein context for MAC usage.
Jason Cooper06a620f2014-03-24 01:49:10 +0000158 *
159 * Initializes the context with this data and saves the resulting Skein
Jason Cooper39bd42b2014-03-24 01:49:09 +0000160 * state variables for further use.
161 *
162 * Applications call the normal Skein functions to update the MAC and
163 * get the final result.
164 *
165 * @param ctx
166 * Pointer to an empty or preinitialized Skein MAC context
167 * @param key
168 * Pointer to key bytes or NULL
Anton Saraev95f1840a2014-05-19 12:09:55 +0400169 * @param key_len
Jason Cooper39bd42b2014-03-24 01:49:09 +0000170 * Length of the key in bytes or zero
Anton Saraev95f1840a2014-05-19 12:09:55 +0400171 * @param hash_bit_len
Jason Cooper39bd42b2014-03-24 01:49:09 +0000172 * Number of MAC hash bits to compute
173 * @return
Colin Cronin20ba4582015-05-13 18:05:42 -0700174 * SKEIN_SUCCESS of SKEIN_FAIL
Jason Cooper39bd42b2014-03-24 01:49:09 +0000175 */
Anton Saraev95f1840a2014-05-19 12:09:55 +0400176int skein_mac_init(struct skein_ctx *ctx, const u8 *key, size_t key_len,
177 size_t hash_bit_len);
Jason Cooper449bb812014-03-24 01:48:58 +0000178
Jason Cooper39bd42b2014-03-24 01:49:09 +0000179/**
180 * Update Skein with the next part of the message.
181 *
182 * @param ctx
183 * Pointer to initialized Skein context
184 * @param msg
185 * Pointer to the message.
Anton Saraev95f1840a2014-05-19 12:09:55 +0400186 * @param msg_byte_cnt
Jason Cooper39bd42b2014-03-24 01:49:09 +0000187 * Length of the message in @b bytes
188 * @return
189 * Success or error code.
190 */
Anton Saraev68ace622014-05-19 12:09:54 +0400191int skein_update(struct skein_ctx *ctx, const u8 *msg,
Anton Saraev95f1840a2014-05-19 12:09:55 +0400192 size_t msg_byte_cnt);
Jason Cooper449bb812014-03-24 01:48:58 +0000193
Jason Cooper39bd42b2014-03-24 01:49:09 +0000194/**
195 * Update the hash with a message bit string.
196 *
197 * Skein can handle data not only as bytes but also as bit strings of
198 * arbitrary length (up to its maximum design size).
199 *
200 * @param ctx
201 * Pointer to initialized Skein context
202 * @param msg
203 * Pointer to the message.
Anton Saraev95f1840a2014-05-19 12:09:55 +0400204 * @param msg_bit_cnt
Jason Cooper39bd42b2014-03-24 01:49:09 +0000205 * Length of the message in @b bits.
206 */
Anton Saraev68ace622014-05-19 12:09:54 +0400207int skein_update_bits(struct skein_ctx *ctx, const u8 *msg,
Anton Saraev95f1840a2014-05-19 12:09:55 +0400208 size_t msg_bit_cnt);
Jason Cooper39bd42b2014-03-24 01:49:09 +0000209
210/**
211 * Finalize Skein and return the hash.
Jason Cooper06a620f2014-03-24 01:49:10 +0000212 *
Jason Cooper39bd42b2014-03-24 01:49:09 +0000213 * Before an application can reuse a Skein setup the application must
214 * reset the Skein context.
215 *
216 * @param ctx
217 * Pointer to initialized Skein context
218 * @param hash
219 * Pointer to buffer that receives the hash. The buffer must be large
Anton Saraev95f1840a2014-05-19 12:09:55 +0400220 * enough to store @c hash_bit_len bits.
Jason Cooper39bd42b2014-03-24 01:49:09 +0000221 * @return
222 * Success or error code.
Anton Saraev68ace622014-05-19 12:09:54 +0400223 * @see skein_reset
Jason Cooper39bd42b2014-03-24 01:49:09 +0000224 */
Anton Saraev68ace622014-05-19 12:09:54 +0400225int skein_final(struct skein_ctx *ctx, u8 *hash);
Jason Cooper449bb812014-03-24 01:48:58 +0000226
Jason Cooper449bb812014-03-24 01:48:58 +0000227/**
228 * @}
229 */
230#endif