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