Prototype implementation of IKE key exchange via NTRU encryption
[strongswan.git] / src / libstrongswan / plugins / ntru / ntru_crypto / ntru_crypto_hmac.h
1 /*/******************************************************************************
2 * NTRU Cryptography Reference Source Code
3 * Copyright (c) 2009-2013, by Security Innovation, Inc. All rights reserved.
4 *
5 * ntru_crypto_hmac.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_hmac.h
28 *
29 * Contents: Definitions and declarations for the HMAC implementation.
30 *
31 *****************************************************************************/
32
33 #ifndef NTRU_CRYPTO_HMAC_H
34 #define NTRU_CRYPTO_HMAC_H
35
36
37 #include "ntru_crypto_platform.h"
38 #include "ntru_crypto_hash.h"
39
40
41 /***************
42 * error codes *
43 ***************/
44
45 #define NTRU_CRYPTO_HMAC_OK ((uint32_t)NTRU_CRYPTO_HASH_OK)
46 #define NTRU_CRYPTO_HMAC_BAD_PARAMETER ((uint32_t)NTRU_CRYPTO_HASH_BAD_PARAMETER)
47 #define NTRU_CRYPTO_HMAC_BAD_ALG ((uint32_t)NTRU_CRYPTO_HASH_BAD_ALG)
48 #define NTRU_CRYPTO_HMAC_OUT_OF_MEMORY ((uint32_t)NTRU_CRYPTO_HASH_OUT_OF_MEMORY)
49
50 #define HMAC_RESULT(e) ((uint32_t)((e) ? HMAC_ERROR_BASE + (e) : (e)))
51 #define HMAC_RET(e) return HMAC_RESULT(e)
52
53
54 /*************************
55 * structure definitions *
56 *************************/
57
58 /* HMAC context structure */
59
60 struct _NTRU_CRYPTO_HMAC_CTX; /* opaque forward reference */
61 typedef struct _NTRU_CRYPTO_HMAC_CTX NTRU_CRYPTO_HMAC_CTX;
62
63
64 /*************************
65 * function declarations *
66 *************************/
67
68 /* ntru_crypto_hmac_create_ctx
69 *
70 * This routine creates an HMAC context, setting the hash algorithm and
71 * the key to be used.
72 *
73 * Returns NTRU_CRYPTO_HASH_OK if successful.
74 * Returns NTRU_CRYPTO_HMAC_BAD_PARAMETER if inappropriate NULL pointers are
75 * passed.
76 * Returns NTRU_CRYPTO_HASH_OUT_OF_MEMORY if memory cannot be allocated.
77 */
78
79 extern uint32_t
80 ntru_crypto_hmac_create_ctx(
81 NTRU_CRYPTO_HASH_ALGID algid, /* in - the hash algorithm to be used */
82 uint8_t const *key, /* in - pointer to the HMAC key */
83 uint32_t key_len, /* in - number of bytes in HMAC key */
84 NTRU_CRYPTO_HMAC_CTX **c); /* out - address for pointer to HMAC
85 context */
86
87
88 /* ntru_crypto_hmac_destroy_ctx
89 *
90 * Destroys an HMAC context.
91 *
92 * Returns NTRU_CRYPTO_HASH_OK if successful.
93 * Returns NTRU_CRYPTO_HMAC_BAD_PARAMETER if inappropriate NULL pointers are
94 * passed.
95 */
96
97 extern uint32_t
98 ntru_crypto_hmac_destroy_ctx(
99 NTRU_CRYPTO_HMAC_CTX *c); /* in/out - pointer to HMAC context */
100
101
102 /* ntru_crypto_hmac_get_md_len
103 *
104 * This routine gets the digest length of the HMAC.
105 *
106 * Returns NTRU_CRYPTO_HMAC_OK on success.
107 * Returns NTRU_CRYPTO_HMAC_BAD_PARAMETER if inappropriate NULL pointers are
108 * passed.
109 */
110
111 extern uint32_t
112 ntru_crypto_hmac_get_md_len(
113 NTRU_CRYPTO_HMAC_CTX const *c, /* in - pointer to HMAC context */
114 uint16_t *md_len); /* out - address for digest length */
115
116
117 /* ntru_crypto_hmac_set_key
118 *
119 * This routine sets a digest-length key into the HMAC context.
120 *
121 * Returns NTRU_CRYPTO_HMAC_OK on success.
122 * Returns NTRU_CRYPTO_HMAC_BAD_PARAMETER if inappropriate NULL pointers are
123 * passed.
124 */
125
126 extern uint32_t
127 ntru_crypto_hmac_set_key(
128 NTRU_CRYPTO_HMAC_CTX *c, /* in - pointer to HMAC context */
129 uint8_t const *key); /* in - pointer to new HMAC key */
130
131
132 /* ntru_crypto_hmac_init
133 *
134 * This routine performs standard initialization of the HMAC state.
135 *
136 * Returns NTRU_CRYPTO_HMAC_OK on success.
137 * Returns NTRU_CRYPTO_HMAC_FAIL with corrupted context.
138 * Returns NTRU_CRYPTO_HMAC_BAD_PARAMETER if inappropriate NULL pointers are
139 * passed.
140 */
141
142 extern uint32_t
143 ntru_crypto_hmac_init(
144 NTRU_CRYPTO_HMAC_CTX *c); /* in/out - pointer to HMAC context */
145
146
147 /* ntru_crypto_hmac_update
148 *
149 * This routine processes input data and updates the HMAC hash calculation.
150 *
151 * Returns NTRU_CRYPTO_HMAC_OK on success.
152 * Returns NTRU_CRYPTO_HMAC_FAIL with corrupted context.
153 * Returns NTRU_CRYPTO_HMAC_BAD_PARAMETER if inappropriate NULL pointers are
154 * passed.
155 * Returns NTRU_CRYPTO_HMAC_OVERFLOW if more than bytes are hashed than the underlying
156 * hash algorithm can handle.
157 */
158
159 extern uint32_t
160 ntru_crypto_hmac_update(
161 NTRU_CRYPTO_HMAC_CTX *c, /* in/out - pointer to HMAC context */
162 uint8_t const *data, /* in - pointer to input data */
163 uint32_t data_len); /* in - no. of bytes of input data */
164
165
166 /* ntru_crypto_hmac_final
167 *
168 * This routine completes the HMAC hash calculation and returns the
169 * message digest.
170 *
171 * Returns NTRU_CRYPTO_HMAC_OK on success.
172 * Returns NTRU_CRYPTO_HMAC_FAIL with corrupted context.
173 * Returns NTRU_CRYPTO_HMAC_BAD_PARAMETER if inappropriate NULL pointers are
174 * passed.
175 */
176
177 extern uint32_t
178 ntru_crypto_hmac_final(
179 NTRU_CRYPTO_HMAC_CTX *c, /* in/out - pointer to HMAC context */
180 uint8_t *md); /* out - address for message digest */
181
182
183 #endif /* NTRU_CRYPTO_HMAC_H */