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-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  * @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.4.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 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
67  HashContext *hashContext;
68 #else
69  HashContext hashContext[1];
70 #endif
71 
72  //Check parameters
73  if(hash == NULL || z == NULL || dk == NULL)
75 
76  //The OtherInfo parameter is optional
77  if(otherInfo == NULL && otherInfoLen != 0)
79 
80 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
81  //Allocate a memory buffer to hold the hash context
82  hashContext = cryptoAllocMem(hash->contextSize);
83  //Failed to allocate memory?
84  if(hashContext == NULL)
85  return ERROR_OUT_OF_MEMORY;
86 #endif
87 
88  //Derive the keying material
89  for(i = 1; dkLen > 0; i++)
90  {
91  //Encode the counter as a 32-bit big-endian string
92  STORE32BE(i, counter);
93 
94  //Compute H(counter || Z || OtherInfo)
95  hash->init(hashContext);
96  hash->update(hashContext, counter, sizeof(uint32_t));
97  hash->update(hashContext, z, zLen);
98 
99  //The OtherInfo parameter is optional
100  if(otherInfoLen > 0)
101  {
102  hash->update(hashContext, otherInfo, otherInfoLen);
103  }
104 
105  //Finalize hash calculation
106  hash->final(hashContext, NULL);
107 
108  //Number of octets in the current block
109  n = MIN(dkLen, hash->digestSize);
110  //Save the resulting block
111  osMemcpy(dk, hashContext->digest, n);
112 
113  //Point to the next block
114  dk += n;
115  dkLen -= n;
116  }
117 
118 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
119  //Free previously allocated memory
120  cryptoFreeMem(hashContext);
121 #endif
122 
123  //Successful processing
124  return NO_ERROR;
125 }
126 
127 #endif
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
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
General definitions for cryptographic algorithms.
#define cryptoAllocMem(size)
Definition: crypto.h:765
#define cryptoFreeMem(p)
Definition: crypto.h:770
uint8_t n
uint8_t z
Definition: dns_common.h:191
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
HMAC (Keyed-Hashing for Message Authentication)
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MIN(a, b)
Definition: os_port.h:63
PBKDF (Password-Based Key Derivation Function)
Common interface for hash algorithms.
Definition: crypto.h:1014
HashAlgoFinal final
Definition: crypto.h:1026
size_t contextSize
Definition: crypto.h:1018
HashAlgoUpdate update
Definition: crypto.h:1025
size_t digestSize
Definition: crypto.h:1020
HashAlgoInit init
Definition: crypto.h:1024
Generic hash algorithm context.
uint8_t digest[MAX_HASH_DIGEST_SIZE]