Prototype implementation of IKE key exchange via NTRU encryption
[strongswan.git] / src / libstrongswan / plugins / ntru / ntru_crypto / ntru_crypto_hash.h
1 /******************************************************************************
2 * NTRU Cryptography Reference Source Code
3 * Copyright (c) 2009-2013, by Security Innovation, Inc. All rights reserved.
4 *
5 * ntru_crypto_hash.h is a component of ntru-crypto.
6 *
7 * Copyright (C) 2009-2013 Security Innovation
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 *
23 *****************************************************************************/
24
25 /******************************************************************************
26 *
27 * File: ntru_crypto_hash.h
28 *
29 * Contents: Definitions and declarations for the hash object abstraction.
30 *
31 *****************************************************************************/
32
33 #ifndef NTRU_CRYPTO_HASH_H
34 #define NTRU_CRYPTO_HASH_H
35
36 #include "ntru_crypto_platform.h"
37 #include "ntru_crypto_error.h"
38 #include "ntru_crypto_hash_basics.h"
39 #include "ntru_crypto_sha1.h"
40 #include "ntru_crypto_sha256.h"
41
42
43 /***************
44 * error macro *
45 ***************/
46
47 #define HASH_RESULT(r) ((uint32_t)((r) ? HASH_ERROR_BASE + (r) : (r)))
48 #define HASH_RET(r) return HASH_RESULT(r);
49
50
51 /*************************
52 * structure definitions *
53 *************************/
54
55 /* _NTRU_CRYPTO_HASH_ALG_PARAMS
56 *
57 * An opaque forward declaration for a private structure used
58 * internally by the hash object interface.
59 */
60
61 struct _NTRU_CRYPTO_HASH_ALG_PARAMS;
62
63
64 /* NTRU_CRYPTO_HASH_CTX
65 *
66 * Hash object context information.
67 */
68
69 typedef struct {
70 struct _NTRU_CRYPTO_HASH_ALG_PARAMS const *alg_params;
71 union {
72 NTRU_CRYPTO_SHA1_CTX sha1;
73 NTRU_CRYPTO_SHA2_CTX sha256;
74 } alg_ctx;
75 } NTRU_CRYPTO_HASH_CTX;
76
77
78 /*************************
79 * function declarations *
80 *************************/
81
82 /* ntru_crypto_hash_set_alg
83 *
84 * Sets the hash algorithm for the hash context. This must be called before
85 * any calls to crypto_hash_block_length(), crypto_hash_digest_length(), or
86 * crypto_hash_init() are made.
87 *
88 * Returns NTRU_CRYPTO_HASH_OK on success.
89 * Returns NTRU_CRYPTO_HASH_BAD_PARAMETER if inappropriate NULL pointers are
90 * passed.
91 * Returns NTRU_CRYPTO_HASH_BAD_ALG if the specified algorithm is not supported.
92 */
93
94 extern uint32_t
95 ntru_crypto_hash_set_alg(
96 NTRU_CRYPTO_HASH_ALGID algid, // in - hash algoirithm to be used
97 NTRU_CRYPTO_HASH_CTX *c); // in/out - pointer to the hash context
98
99
100 /* ntru_crypto_hash_block_length
101 *
102 * Gets the number of bytes in an input block for the hash algorithm
103 * specified in the hash context. The hash algorithm must have been set
104 * in the hash context with a call to crypto_hash_set_alg() prior to
105 * calling this function.
106 *
107 * Returns NTRU_CRYPTO_HASH_OK on success.
108 * Returns NTRU_CRYPTO_HASH_BAD_PARAMETER if inappropriate NULL pointers are
109 * passed.
110 * Returns NTRU_CRYPTO_HASH_BAD_ALG if the algorithm has not been set.
111 */
112
113 extern uint32_t
114 ntru_crypto_hash_block_length(
115 NTRU_CRYPTO_HASH_CTX *c, // in - pointer to the hash context
116 uint16_t *blk_len); // out - address for block length in bytes
117
118
119 /* ntru_crypto_hash_digest_length
120 *
121 * Gets the number of bytes needed to hold the message digest for the
122 * hash algorithm specified in the hash context. The algorithm must have
123 * been set in the hash context with a call to crypto_hash_set_alg() prior
124 * to calling this function.
125 *
126 * Returns NTRU_CRYPTO_HASH_OK on success.
127 * Returns NTRU_CRYPTO_HASH_BAD_PARAMETER if inappropriate NULL pointers are
128 * passed.
129 * Returns NTRU_CRYPTO_HASH_BAD_ALG if the algorithm has not been set.
130 */
131
132 extern uint32_t
133 ntru_crypto_hash_digest_length(
134 NTRU_CRYPTO_HASH_CTX const *c, // in - pointer to the hash context
135 uint16_t *md_len); // out - addrfor digest length in bytes
136
137
138 /* ntru_crypto_hash_init
139 *
140 * This routine initializes the hash state.
141 *
142 * Returns NTRU_CRYPTO_HASH_OK on success.
143 * Returns NTRU_CRYPTO_HASH_FAIL with corrupted context.
144 * Returns NTRU_CRYPTO_HASH_BAD_PARAMETER if inappropriate NULL pointers are
145 * passed.
146 * Returns NTRU_CRYPTO_HASH_BAD_ALG if the algorithm has not been set.
147 */
148
149 extern uint32_t
150 ntru_crypto_hash_init(
151 NTRU_CRYPTO_HASH_CTX *c); // in/out - pointer to hash context
152
153
154 /* ntru_crypto_hash_update
155 *
156 * This routine processes input data and updates the hash calculation.
157 *
158 * Returns NTRU_CRYPTO_HASH_OK on success.
159 * Returns NTRU_CRYPTO_HASH_FAIL with corrupted context.
160 * Returns NTRU_CRYPTO_HASH_BAD_PARAMETER if inappropriate NULL pointers are
161 * passed.
162 * Returns NTRU_CRYPTO_HASH_OVERFLOW if too much text has been fed to the
163 * hash algorithm. The size limit is dependent on the hash algorithm,
164 * and not all algorithms have this limit.
165 * Returns NTRU_CRYPTO_HASH_BAD_ALG if the algorithm has not been set.
166 */
167
168 extern uint32_t
169 ntru_crypto_hash_update(
170 NTRU_CRYPTO_HASH_CTX *c, // in/out - pointer to hash context
171 uint8_t const *data, // in - pointer to input data
172 uint32_t data_len); // in - number of bytes of input data
173
174
175 /* ntru_crypto_hash_final
176 *
177 * This routine completes the hash calculation and returns the message digest.
178 *
179 * Returns NTRU_CRYPTO_HASH_OK on success.
180 * Returns NTRU_CRYPTO_HASH_FAIL with corrupted context.
181 * Returns NTRU_CRYPTO_HASH_BAD_PARAMETER if inappropriate NULL pointers are
182 * passed.
183 * Returns NTRU_CRYPTO_HASH_BAD_ALG if the algorithm has not been set.
184 */
185
186 extern uint32_t
187 ntru_crypto_hash_final(
188 NTRU_CRYPTO_HASH_CTX *c, // in/out - pointer to hash context
189 uint8_t *md); // out - address for message digest
190
191
192 /* ntru_crypto_hash_final_zero_pad
193 *
194 * This routine completes the hash calculation using zero padding and
195 * returns the message digest.
196 *
197 * Returns NTRU_CRYPTO_HASH_OK on success.
198 * Returns NTRU_CRYPTO_HASH_FAIL with corrupted context.
199 * Returns NTRU_CRYPTO_HASH_BAD_PARAMETER if inappropriate NULL pointers are
200 * passed.
201 * Returns NTRU_CRYPTO_HASH_BAD_ALG if the algorithm has not been set.
202 */
203
204 extern uint32_t
205 ntru_crypto_hash_final_zero_pad(
206 NTRU_CRYPTO_HASH_CTX *c, // in/out - pointer to hash context
207 uint8_t *md); // out - address for message digest
208
209
210 /* ntru_crypto_hash_digest
211 *
212 * This routine computes a message digest. It is assumed that the
213 * output buffer md is large enough to hold the output (see
214 * crypto_hash_digest_length)
215 *
216 * Returns NTRU_CRYPTO_HASH_OK on success.
217 * Returns NTRU_CRYPTO_HASH_FAIL with corrupted context.
218 * Returns NTRU_CRYPTO_HASH_BAD_PARAMETER if inappropriate NULL pointers are passed.
219 * Returns NTRU_CRYPTO_HASH_OVERFLOW if too much text has been fed to the
220 * hash algorithm. The size limit is dependent on the hash algorithm,
221 * and not all algorithms have this limit.
222 * Returns NTRU_CRYPTO_HASH_BAD_ALG if the specified algorithm is not supported.
223 */
224
225 extern uint32_t
226 ntru_crypto_hash_digest(
227 NTRU_CRYPTO_HASH_ALGID algid, // in - the hash algorithm to use
228 uint8_t const *data, // in - pointer to input data
229 uint32_t data_len, // in - number of bytes of input data
230 uint8_t *md); // out - address for message digest
231
232
233 #endif /* NTRU_CRYPTO_HASH_H */