concat_kdf.c
Go to the documentation of this file.
1 /**
2  * @file concat_kdf.c
3  * @brief Concat KDF (Concatenation Key Derivation Function)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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  * @section Description
28  *
29  * Concat KDF is a key derivation function defined by NIST SP 800-56A
30  * revision 1, section 5.8.1
31  *
32  * @author Oryx Embedded SARL (www.oryx-embedded.com)
33  * @version 2.5.0
34  **/
35 
36 //Switch to the appropriate trace level
37 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
38 
39 //Dependencies
40 #include "core/crypto.h"
41 #include "kdf/pbkdf.h"
42 #include "mac/hmac.h"
43 
44 //Check crypto library configuration
45 #if (CONCAT_KDF_SUPPORT == ENABLED)
46 
47 
48 /**
49  * @brief Concat KDF key derivation function
50  * @param[in] hash Underlying hash function
51  * @param[in] z Shared secret Z
52  * @param[in] zLen Length in octets of the shared secret Z
53  * @param[in] otherInfo Context-specific information (optional parameter)
54  * @param[in] otherInfoLen Length in octets of the context-specific information
55  * @param[out] dk Derived keying material
56  * @param[in] dkLen Length in octets of the keying material to be generated
57  * @return Error code
58  **/
59 
60 error_t concatKdf(const HashAlgo *hash, const uint8_t *z, size_t zLen,
61  const uint8_t *otherInfo, size_t otherInfoLen, uint8_t *dk, size_t dkLen)
62 {
63  size_t n;
64  uint32_t i;
65  uint8_t counter[4];
66  uint8_t digest[MAX_HASH_DIGEST_SIZE];
67 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
68  HashContext *hashContext;
69 #else
70  HashContext hashContext[1];
71 #endif
72 
73  //Check parameters
74  if(hash == NULL || z == NULL || dk == NULL)
76 
77  //The OtherInfo parameter is optional
78  if(otherInfo == NULL && otherInfoLen != 0)
80 
81 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
82  //Allocate a memory buffer to hold the hash context
83  hashContext = cryptoAllocMem(hash->contextSize);
84  //Failed to allocate memory?
85  if(hashContext == NULL)
86  return ERROR_OUT_OF_MEMORY;
87 #endif
88 
89  //Derive the keying material
90  for(i = 1; dkLen > 0; i++)
91  {
92  //Encode the counter as a 32-bit big-endian string
93  STORE32BE(i, counter);
94 
95  //Compute H(counter || Z || OtherInfo)
96  hash->init(hashContext);
97  hash->update(hashContext, counter, sizeof(uint32_t));
98  hash->update(hashContext, z, zLen);
99 
100  //The OtherInfo parameter is optional
101  if(otherInfoLen > 0)
102  {
103  hash->update(hashContext, otherInfo, otherInfoLen);
104  }
105 
106  //Finalize hash calculation
107  hash->final(hashContext, digest);
108 
109  //Number of octets in the current block
110  n = MIN(dkLen, hash->digestSize);
111  //Save the resulting block
112  osMemcpy(dk, digest, n);
113 
114  //Point to the next block
115  dk += n;
116  dkLen -= n;
117  }
118 
119 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
120  //Free previously allocated memory
121  cryptoFreeMem(hashContext);
122 #endif
123 
124  //Successful processing
125  return NO_ERROR;
126 }
127 
128 #endif
HashAlgoInit init
Definition: crypto.h:1092
Generic hash algorithm context.
size_t digestSize
Definition: crypto.h:1088
HashAlgoUpdate update
Definition: crypto.h:1093
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
size_t contextSize
Definition: crypto.h:1086
#define MAX_HASH_DIGEST_SIZE
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
error_t
Error codes.
Definition: error.h:43
General definitions for cryptographic algorithms.
#define MIN(a, b)
Definition: os_port.h:63
uint8_t z
Definition: dns_common.h:191
HashAlgoFinal final
Definition: crypto.h:1094
PBKDF (Password-Based Key Derivation Function)
uint8_t n
#define cryptoFreeMem(p)
Definition: crypto.h:826
#define cryptoAllocMem(size)
Definition: crypto.h:821
error_t concatKdf(const HashAlgo *hash, const uint8_t *z, size_t zLen, const uint8_t *otherInfo, size_t otherInfoLen, uint8_t *dk, size_t dkLen)
Concat KDF key derivation function.
Definition: concat_kdf.c:60
Common interface for hash algorithms.
Definition: crypto.h:1082
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
@ NO_ERROR
Success.
Definition: error.h:44
HMAC (Keyed-Hashing for Message Authentication)