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-2024 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 2.4.4
29  **/
30 
31 #ifndef _HMAC_H
32 #define _HMAC_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "hash/hash_algorithms.h"
37 
38 //Application specific context
39 #ifndef HMAC_PRIVATE_CONTEXT
40  #define HMAC_PRIVATE_CONTEXT
41 #endif
42 
43 //Inner padding (ipad)
44 #define HMAC_IPAD 0x36
45 //Outer padding (opad)
46 #define HMAC_OPAD 0x5C
47 
48 //C++ guard
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 
54 /**
55  * @brief HMAC algorithm context
56  **/
57 
58 typedef struct
59 {
60  const HashAlgo *hash;
62  uint8_t key[MAX_HASH_BLOCK_SIZE];
63  uint8_t digest[MAX_HASH_DIGEST_SIZE];
65 } HmacContext;
66 
67 
68 //HMAC related constants
69 extern const uint8_t HMAC_WITH_MD5_OID[8];
70 extern const uint8_t HMAC_WITH_TIGER_OID[8];
71 extern const uint8_t HMAC_WITH_RIPEMD160_OID[8];
72 extern const uint8_t HMAC_WITH_SHA1_OID[8];
73 extern const uint8_t HMAC_WITH_SHA224_OID[8];
74 extern const uint8_t HMAC_WITH_SHA256_OID[8];
75 extern const uint8_t HMAC_WITH_SHA384_OID[8];
76 extern const uint8_t HMAC_WITH_SHA512_OID[8];
77 extern const uint8_t HMAC_WITH_SHA512_224_OID[8];
78 extern const uint8_t HMAC_WITH_SHA512_256_OID[8];
79 extern const uint8_t HMAC_WITH_SHA3_224_OID[9];
80 extern const uint8_t HMAC_WITH_SHA3_256_OID[9];
81 extern const uint8_t HMAC_WITH_SHA3_384_OID[9];
82 extern const uint8_t HMAC_WITH_SHA3_512_OID[9];
83 extern const uint8_t HMAC_WITH_SM3_OID[10];
84 
85 //HMAC related functions
86 error_t hmacCompute(const HashAlgo *hash, const void *key, size_t keyLen,
87  const void *data, size_t dataLen, uint8_t *digest);
88 
89 error_t hmacInit(HmacContext *context, const HashAlgo *hash,
90  const void *key, size_t keyLen);
91 
92 void hmacUpdate(HmacContext *context, const void *data, size_t length);
93 void hmacFinal(HmacContext *context, uint8_t *digest);
94 void hmacFinalRaw(HmacContext *context, uint8_t *digest);
95 void hmacDeinit(HmacContext *context);
96 
97 //C++ guard
98 #ifdef __cplusplus
99 }
100 #endif
101 
102 #endif
void hmacFinalRaw(HmacContext *context, uint8_t *digest)
Finish the HMAC calculation (no padding added)
Definition: hmac.c:273
const uint8_t HMAC_WITH_SHA512_224_OID[8]
Definition: hmac.c:65
const HashAlgo * hash
Definition: hmac.h:60
const uint8_t HMAC_WITH_RIPEMD160_OID[8]
Definition: hmac.c:53
Generic hash algorithm context.
HMAC algorithm context.
Definition: hmac.h:59
const uint8_t HMAC_WITH_SHA384_OID[8]
Definition: hmac.c:61
error_t hmacInit(HmacContext *context, const HashAlgo *hash, const void *key, size_t keyLen)
Initialize HMAC calculation.
Definition: hmac.c:140
const uint8_t HMAC_WITH_SHA512_256_OID[8]
Definition: hmac.c:67
uint8_t data[]
Definition: ethernet.h:222
const uint8_t HMAC_WITH_SHA224_OID[8]
Definition: hmac.c:57
#define HMAC_PRIVATE_CONTEXT
Definition: hmac.h:40
#define MAX_HASH_DIGEST_SIZE
const uint8_t HMAC_WITH_SHA3_224_OID[9]
Definition: hmac.c:69
error_t
Error codes.
Definition: error.h:43
#define MAX_HASH_BLOCK_SIZE
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:91
uint8_t length
Definition: tcp.h:368
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:201
uint32_t dataLen
Definition: sftp_common.h:229
const uint8_t HMAC_WITH_SHA1_OID[8]
Definition: hmac.c:55
Collection of hash algorithms.
const uint8_t HMAC_WITH_TIGER_OID[8]
Definition: hmac.c:51
void hmacDeinit(HmacContext *context)
Release HMAC context.
Definition: hmac.c:256
const uint8_t HMAC_WITH_SHA3_384_OID[9]
Definition: hmac.c:73
const uint8_t HMAC_WITH_SHA3_512_OID[9]
Definition: hmac.c:75
const uint8_t HMAC_WITH_SHA512_OID[8]
Definition: hmac.c:63
const uint8_t HMAC_WITH_SM3_OID[10]
Definition: hmac.c:77
void hmacFinal(HmacContext *context, uint8_t *digest)
Finish the HMAC calculation.
Definition: hmac.c:218
const uint8_t HMAC_WITH_MD5_OID[8]
Definition: hmac.c:49
HashContext hashContext
Definition: hmac.h:61
const uint8_t HMAC_WITH_SHA256_OID[8]
Definition: hmac.c:59
Common interface for hash algorithms.
Definition: crypto.h:1046
const uint8_t HMAC_WITH_SHA3_256_OID[9]
Definition: hmac.c:71