x963_kdf.c
Go to the documentation of this file.
1 /**
2  * @file x963_kdf.c
3  * @brief ANSI X9.63 key derivation function
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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  * The ANSI X9.63 KDF key derivation function is a simple construct based on a
30  * one-way hash function described in American National Standard X9.63
31  *
32  * @author Oryx Embedded SARL (www.oryx-embedded.com)
33  * @version 2.6.6
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/x963_kdf.h"
42 #include "hash/hash_algorithms.h"
43 
44 //Check crypto library configuration
45 #if (X963_KDF_SUPPORT == ENABLED)
46 
47 
48 /**
49  * @brief ANSI X9.63 key derivation function
50  * @param[in] hashAlgo Underlying hash function
51  * @param[in] z Shared secret Z
52  * @param[in] zLen Length of the shared secret Z, in bytes
53  * @param[in] sharedInfo Shared information (optional parameter)
54  * @param[in] sharedInfoLen Length of the shared information, in bytes
55  * @param[out] dk Derived keying material
56  * @param[in] dkLen Length of the keying material to be generated, in bytes
57  * @return Error code
58  **/
59 
60 error_t x963Kdf(const HashAlgo *hashAlgo, const uint8_t *z, size_t zLen,
61  const uint8_t *sharedInfo, size_t sharedInfoLen, 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(hashAlgo == NULL || z == NULL || dk == NULL)
76 
77  //The SharedInfo parameter is optional
78  if(sharedInfo == NULL && sharedInfoLen != 0)
80 
81 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
82  //Allocate a memory buffer to hold the hash context
83  hashContext = cryptoAllocMem(hashAlgo->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 Hash(Z || Counter || SharedInfo)
96  hashAlgo->init(hashContext);
97  hashAlgo->update(hashContext, z, zLen);
98  hashAlgo->update(hashContext, counter, sizeof(uint32_t));
99  hashAlgo->update(hashContext, sharedInfo, sharedInfoLen);
100  hashAlgo->final(hashContext, digest);
101 
102  //Number of octets in the current block
103  n = MIN(dkLen, hashAlgo->digestSize);
104  //Save the resulting block
105  osMemcpy(dk, digest, n);
106 
107  //Point to the next block
108  dk += n;
109  dkLen -= n;
110  }
111 
112 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
113  //Free previously allocated memory
114  cryptoFreeMem(hashContext);
115 #endif
116 
117  //Successful processing
118  return NO_ERROR;
119 }
120 
121 #endif
HashAlgoInit init
Definition: crypto.h:1253
Generic hash algorithm context.
size_t digestSize
Definition: crypto.h:1249
HashAlgoUpdate update
Definition: crypto.h:1254
error_t x963Kdf(const HashAlgo *hashAlgo, const uint8_t *z, size_t zLen, const uint8_t *sharedInfo, size_t sharedInfoLen, uint8_t *dk, size_t dkLen)
ANSI X9.63 key derivation function.
Definition: x963_kdf.c:60
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
size_t contextSize
Definition: crypto.h:1247
#define MAX_HASH_DIGEST_SIZE
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:147
error_t
Error codes.
Definition: error.h:43
ANSI X9.63 key derivation function.
General definitions for cryptographic algorithms.
#define MIN(a, b)
Definition: os_port.h:63
Collection of hash algorithms.
uint8_t z
Definition: dns_common.h:196
HashAlgoFinal final
Definition: crypto.h:1255
uint8_t n
#define cryptoFreeMem(p)
Definition: crypto.h:966
#define cryptoAllocMem(size)
Definition: crypto.h:961
Common interface for hash algorithms.
Definition: crypto.h:1243
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
@ NO_ERROR
Success.
Definition: error.h:44