hmac.h
Go to the documentation of this file.
1 /**
2  * @file hmac.h
3  * @brief HMAC (Keyed-Hashing for Message Authentication)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCrypto Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.6
29  **/
30 
31 #ifndef _HMAC_H
32 #define _HMAC_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 
37 //MD2 hash support?
38 #if (MD2_SUPPORT == ENABLED)
39  #include "hash/md2.h"
40 #endif
41 
42 //MD4 hash support?
43 #if (MD4_SUPPORT == ENABLED)
44  #include "hash/md4.h"
45 #endif
46 
47 //MD5 hash support?
48 #if (MD5_SUPPORT == ENABLED)
49  #include "hash/md5.h"
50 #endif
51 
52 //RIPEMD-128 hash support?
53 #if (RIPEMD128_SUPPORT == ENABLED)
54  #include "hash/ripemd128.h"
55 #endif
56 
57 //RIPEMD-160 hash support?
58 #if (RIPEMD160_SUPPORT == ENABLED)
59  #include "hash/ripemd160.h"
60 #endif
61 
62 //SHA-1 hash support?
63 #if (SHA1_SUPPORT == ENABLED)
64  #include "hash/sha1.h"
65 #endif
66 
67 //SHA-224 hash support?
68 #if (SHA224_SUPPORT == ENABLED)
69  #include "hash/sha224.h"
70 #endif
71 
72 //SHA-256 hash support?
73 #if (SHA256_SUPPORT == ENABLED)
74  #include "hash/sha256.h"
75 #endif
76 
77 //SHA-384 hash support?
78 #if (SHA384_SUPPORT == ENABLED)
79  #include "hash/sha384.h"
80 #endif
81 
82 //SHA-512 hash support?
83 #if (SHA512_SUPPORT == ENABLED)
84  #include "hash/sha512.h"
85 #endif
86 
87 //SHA-512/224 hash support?
88 #if (SHA512_224_SUPPORT == ENABLED)
89  #include "hash/sha512_224.h"
90 #endif
91 
92 //SHA-512/256 hash support?
93 #if (SHA512_256_SUPPORT == ENABLED)
94  #include "hash/sha512_256.h"
95 #endif
96 
97 //SHA3-224 hash support?
98 #if (SHA3_224_SUPPORT == ENABLED)
99  #include "hash/sha3_224.h"
100 #endif
101 
102 //SHA3-256 hash support?
103 #if (SHA3_256_SUPPORT == ENABLED)
104  #include "hash/sha3_256.h"
105 #endif
106 
107 //SHA3-384 hash support?
108 #if (SHA3_384_SUPPORT == ENABLED)
109  #include "hash/sha3_384.h"
110 #endif
111 
112 //SHA3-512 hash support?
113 #if (SHA3_512_SUPPORT == ENABLED)
114  #include "hash/sha3_512.h"
115 #endif
116 
117 //BLAKE2b-160 hash support?
118 #if (BLAKE2B160_SUPPORT == ENABLED)
119  #include "hash/blake2b160.h"
120 #endif
121 
122 //BLAKE2b-256 hash support?
123 #if (BLAKE2B256_SUPPORT == ENABLED)
124  #include "hash/blake2b256.h"
125 #endif
126 
127 //BLAKE2b-384 hash support?
128 #if (BLAKE2B384_SUPPORT == ENABLED)
129  #include "hash/blake2b384.h"
130 #endif
131 
132 //BLAKE2b-512 hash support?
133 #if (BLAKE2B512_SUPPORT == ENABLED)
134  #include "hash/blake2b512.h"
135 #endif
136 
137 //BLAKE2s-128 hash support?
138 #if (BLAKE2S128_SUPPORT == ENABLED)
139  #include "hash/blake2s128.h"
140 #endif
141 
142 //BLAKE2s-160 hash support?
143 #if (BLAKE2S160_SUPPORT == ENABLED)
144  #include "hash/blake2s160.h"
145 #endif
146 
147 //BLAKE2s-224 hash support?
148 #if (BLAKE2S224_SUPPORT == ENABLED)
149  #include "hash/blake2s224.h"
150 #endif
151 
152 //BLAKE2s-256 hash support?
153 #if (BLAKE2S256_SUPPORT == ENABLED)
154  #include "hash/blake2s256.h"
155 #endif
156 
157 //Tiger hash support?
158 #if (TIGER_SUPPORT == ENABLED)
159  #include "hash/tiger.h"
160 #endif
161 
162 //Whirlpool hash support?
163 #if (WHIRLPOOL_SUPPORT == ENABLED)
164  #include "hash/whirlpool.h"
165 #endif
166 
167 //Inner padding (ipad)
168 #define HMAC_IPAD 0x36
169 //Outer padding (opad)
170 #define HMAC_OPAD 0x5C
171 
172 //C++ guard
173 #ifdef __cplusplus
174 extern "C" {
175 #endif
176 
177 
178 /**
179  * @brief HMAC algorithm context
180  **/
181 
182 typedef struct
183 {
184  const HashAlgo *hash;
185  uint8_t hashContext[MAX_HASH_CONTEXT_SIZE];
186  uint8_t key[MAX_HASH_BLOCK_SIZE];
187  uint8_t digest[MAX_HASH_DIGEST_SIZE];
188 } HmacContext;
189 
190 
191 //HMAC related constants
192 extern const uint8_t HMAC_WITH_MD5_OID[8];
193 extern const uint8_t HMAC_WITH_TIGER_OID[8];
194 extern const uint8_t HMAC_WITH_RIPEMD160_OID[8];
195 extern const uint8_t HMAC_WITH_SHA1_OID[8];
196 extern const uint8_t HMAC_WITH_SHA224_OID[8];
197 extern const uint8_t HMAC_WITH_SHA256_OID[8];
198 extern const uint8_t HMAC_WITH_SHA384_OID[8];
199 extern const uint8_t HMAC_WITH_SHA512_OID[8];
200 extern const uint8_t HMAC_WITH_SHA3_224_OID[9];
201 extern const uint8_t HMAC_WITH_SHA3_256_OID[9];
202 extern const uint8_t HMAC_WITH_SHA3_384_OID[9];
203 extern const uint8_t HMAC_WITH_SHA3_512_OID[9];
204 
205 //HMAC related functions
206 error_t hmacCompute(const HashAlgo *hash, const void *key, size_t keyLen,
207  const void *data, size_t dataLen, uint8_t *digest);
208 
209 void hmacInit(HmacContext *context, const HashAlgo *hash,
210  const void *key, size_t keyLen);
211 
212 void hmacUpdate(HmacContext *context, const void *data, size_t length);
213 void hmacFinal(HmacContext *context, uint8_t *digest);
214 void hmacFinalRaw(HmacContext *context, uint8_t *digest);
215 
216 //C++ guard
217 #ifdef __cplusplus
218 }
219 #endif
220 
221 #endif
void hmacFinalRaw(HmacContext *context, uint8_t *digest)
Finish the HMAC calculation (no padding is added)
Definition: hmac.c:222
SHA-512/256 (Secure Hash Algorithm)
const HashAlgo * hash
Definition: hmac.h:184
const uint8_t HMAC_WITH_RIPEMD160_OID[8]
Definition: hmac.c:53
uint8_t length
Definition: dtls_misc.h:149
SHA3-512 hash function (SHA-3 with 512-bit output)
HMAC algorithm context.
Definition: hmac.h:182
BLAKE2s-128 hash function.
#define MAX_HASH_CONTEXT_SIZE
Definition: crypto.h:701
RIPEMD-128 hash function.
SHA3-224 hash function (SHA-3 with 224-bit output)
const uint8_t HMAC_WITH_SHA384_OID[8]
Definition: hmac.c:61
SHA-1 (Secure Hash Algorithm 1)
SHA3-384 hash function (SHA-3 with 384-bit output)
BLAKE2s-160 hash function.
SHA-512 (Secure Hash Algorithm 512)
const uint8_t HMAC_WITH_SHA224_OID[8]
Definition: hmac.c:57
const uint8_t HMAC_WITH_SHA3_224_OID[9]
Definition: hmac.c:65
error_t
Error codes.
Definition: error.h:42
BLAKE2s-224 hash function.
Tiger hash function.
BLAKE2b-256 hash function.
General definitions for cryptographic algorithms.
error_t hmacCompute(const HashAlgo *hash, const void *key, size_t keyLen, const void *data, size_t dataLen, uint8_t *digest)
Compute HMAC using the specified hash function.
Definition: hmac.c:85
SHA-384 (Secure Hash Algorithm 384)
void hmacUpdate(HmacContext *context, const void *data, size_t length)
Update the HMAC context with a portion of the message being hashed.
Definition: hmac.c:168
const uint8_t HMAC_WITH_SHA1_OID[8]
Definition: hmac.c:55
uint8_t hash
Definition: tls.h:1369
RIPEMD-160 hash function.
SHA-512/224 (Secure Hash Algorithm)
const uint8_t HMAC_WITH_TIGER_OID[8]
Definition: hmac.c:51
SHA-256 (Secure Hash Algorithm 256)
BLAKE2s-256 hash function.
MD4 (Message-Digest Algorithm)
const uint8_t HMAC_WITH_SHA3_384_OID[9]
Definition: hmac.c:69
BLAKE2b-384 hash function.
MD5 (Message-Digest Algorithm)
const uint8_t HMAC_WITH_SHA3_512_OID[9]
Definition: hmac.c:71
#define MAX_HASH_BLOCK_SIZE
Definition: crypto.h:756
MD2 (Message-Digest Algorithm)
const uint8_t HMAC_WITH_SHA512_OID[8]
Definition: hmac.c:63
void hmacFinal(HmacContext *context, uint8_t *digest)
Finish the HMAC calculation.
Definition: hmac.c:185
const uint8_t HMAC_WITH_MD5_OID[8]
Definition: hmac.c:49
const uint8_t HMAC_WITH_SHA256_OID[8]
Definition: hmac.c:59
Common interface for hash algorithms.
Definition: crypto.h:1128
Whirlpool hash function.
#define MAX_HASH_DIGEST_SIZE
Definition: crypto.h:811
uint8_t data[]
Definition: dtls_misc.h:176
const uint8_t HMAC_WITH_SHA3_256_OID[9]
Definition: hmac.c:67
SHA3-256 hash function (SHA-3 with 256-bit output)
BLAKE2b-160 hash function.
BLAKE2b-512 hash function.
void hmacInit(HmacContext *context, const HashAlgo *hash, const void *key, size_t keyLen)
Initialize HMAC calculation.
Definition: hmac.c:118
SHA-224 (Secure Hash Algorithm 224)