1 /*/******************************************************************************
2 * NTRU Cryptography Reference Source Code
3 * Copyright (c) 2009-2013, by Security Innovation, Inc. All rights reserved.
5 * ntru_crypto_hmac.h is a component of ntru-crypto.
7 * Copyright (C) 2009-2013 Security Innovation
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.
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.
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.
23 *****************************************************************************/
25 /******************************************************************************
27 * File: ntru_crypto_hmac.h
29 * Contents: Definitions and declarations for the HMAC implementation.
31 *****************************************************************************/
33 #ifndef NTRU_CRYPTO_HMAC_H
34 #define NTRU_CRYPTO_HMAC_H
37 #include "ntru_crypto_platform.h"
38 #include "ntru_crypto_hash.h"
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)
50 #define HMAC_RESULT(e) ((uint32_t)((e) ? HMAC_ERROR_BASE + (e) : (e)))
51 #define HMAC_RET(e) return HMAC_RESULT(e)
54 /*************************
55 * structure definitions *
56 *************************/
58 /* HMAC context structure */
60 struct _NTRU_CRYPTO_HMAC_CTX
; /* opaque forward reference */
61 typedef struct _NTRU_CRYPTO_HMAC_CTX NTRU_CRYPTO_HMAC_CTX
;
64 /*************************
65 * function declarations *
66 *************************/
68 /* ntru_crypto_hmac_create_ctx
70 * This routine creates an HMAC context, setting the hash algorithm and
73 * Returns NTRU_CRYPTO_HASH_OK if successful.
74 * Returns NTRU_CRYPTO_HMAC_BAD_PARAMETER if inappropriate NULL pointers are
76 * Returns NTRU_CRYPTO_HASH_OUT_OF_MEMORY if memory cannot be allocated.
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
88 /* ntru_crypto_hmac_destroy_ctx
90 * Destroys an HMAC context.
92 * Returns NTRU_CRYPTO_HASH_OK if successful.
93 * Returns NTRU_CRYPTO_HMAC_BAD_PARAMETER if inappropriate NULL pointers are
98 ntru_crypto_hmac_destroy_ctx(
99 NTRU_CRYPTO_HMAC_CTX
*c
); /* in/out - pointer to HMAC context */
102 /* ntru_crypto_hmac_get_md_len
104 * This routine gets the digest length of the HMAC.
106 * Returns NTRU_CRYPTO_HMAC_OK on success.
107 * Returns NTRU_CRYPTO_HMAC_BAD_PARAMETER if inappropriate NULL pointers are
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 */
117 /* ntru_crypto_hmac_set_key
119 * This routine sets a digest-length key into the HMAC context.
121 * Returns NTRU_CRYPTO_HMAC_OK on success.
122 * Returns NTRU_CRYPTO_HMAC_BAD_PARAMETER if inappropriate NULL pointers are
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 */
132 /* ntru_crypto_hmac_init
134 * This routine performs standard initialization of the HMAC state.
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
143 ntru_crypto_hmac_init(
144 NTRU_CRYPTO_HMAC_CTX
*c
); /* in/out - pointer to HMAC context */
147 /* ntru_crypto_hmac_update
149 * This routine processes input data and updates the HMAC hash calculation.
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
155 * Returns NTRU_CRYPTO_HMAC_OVERFLOW if more than bytes are hashed than the underlying
156 * hash algorithm can handle.
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 */
166 /* ntru_crypto_hmac_final
168 * This routine completes the HMAC hash calculation and returns the
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
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 */
183 #endif /* NTRU_CRYPTO_HMAC_H */