pkcs5_common.c
Go to the documentation of this file.
1 /**
2  * @file pkcs5_common.c
3  * @brief PKCS #5 common definitions
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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "pkix/pkcs5_common.h"
37 #include "encoding/oid.h"
39 #include "mac/hmac.h"
40 #include "debug.h"
41 
42 //Check crypto library configuration
43 #if (PKCS5_SUPPORT == ENABLED)
44 
45 //PBE with MD2 and DES-CBC OID (1.2.840.113549.1.5.1)
46 const uint8_t PBE_WITH_MD2_AND_DES_CBC_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x01};
47 //PBE with MD5 and DES-CBC OID (1.2.840.113549.1.5.3)
48 const uint8_t PBE_WITH_MD5_AND_DES_CBC_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x03};
49 //PBE with MD2 and RC2-CBC OID (1.2.840.113549.1.5.4)
50 const uint8_t PBE_WITH_MD2_AND_RC2_CBC_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x04};
51 //PBE with MD5 and RC2-CBC OID (1.2.840.113549.1.5.6)
52 const uint8_t PBE_WITH_MD5_AND_RC2_CBC_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x06};
53 //PBE with SHA-1 and DES-CBC OID (1.2.840.113549.1.5.10)
54 const uint8_t PBE_WITH_SHA1_AND_DES_CBC_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0A};
55 //PBE with SHA-1 and RC2-CBC OID (1.2.840.113549.1.5.11)
56 const uint8_t PBE_WITH_SHA1_AND_RC2_CBC_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0B};
57 
58 //PBES2 OID (1.2.840.113549.1.5.13)
59 const uint8_t PBES2_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0D};
60 
61 
62 /**
63  * @brief Get the hash algorithm to be used for PBES1 operation
64  * @param[in] oid Encryption algorithm identifier
65  * @param[in] length Length of the encryption algorithm identifier, in bytes
66  * @return Hash algorithm
67  **/
68 
69 const HashAlgo *pkcs5GetPbes1HashAlgo(const uint8_t *oid, size_t length)
70 {
71  const HashAlgo *hashAlgo;
72 
73 #if (PKCS5_MD2_SUPPORT == ENABLED && MD2_SUPPORT == ENABLED)
74  //PBE with MD2 and RC2-CBC algorithm identifier?
76  {
77  hashAlgo = MD2_HASH_ALGO;
78  }
79  //PBE with MD2 and DES-CBC algorithm identifier?
81  {
82  hashAlgo = MD2_HASH_ALGO;
83  }
84  else
85 #endif
86 #if (PKCS5_MD5_SUPPORT == ENABLED && MD5_SUPPORT == ENABLED)
87  //PBE with MD5 and RC2-CBC algorithm identifier?
89  {
90  hashAlgo = MD5_HASH_ALGO;
91  }
92  //PBE with MD5 and DES-CBC algorithm identifier?
94  {
95  hashAlgo = MD5_HASH_ALGO;
96  }
97  else
98 #endif
99 #if (PKCS5_SHA1_SUPPORT == ENABLED && SHA1_SUPPORT == ENABLED)
100  //PBE with SHA-1 and RC2-CBC algorithm identifier?
102  {
103  hashAlgo = SHA1_HASH_ALGO;
104  }
105  //PBE with SHA-1 and DES-CBC algorithm identifier?
107  {
108  hashAlgo = SHA1_HASH_ALGO;
109  }
110  else
111 #endif
112  //Unknown algorithm identifier?
113  {
114  hashAlgo = NULL;
115  }
116 
117  //Return the hash algorithm that matches the specified OID
118  return hashAlgo;
119 }
120 
121 
122 /**
123  * @brief Get the hash algorithm to be used for PBES2 operation
124  * @param[in] oid KDF algorithm identifier
125  * @param[in] length Length of the KDF algorithm identifier, in bytes
126  * @return Hash algorithm
127  **/
128 
129 const HashAlgo *pkcs5GetPbes2HashAlgo(const uint8_t *oid, size_t length)
130 {
131  const HashAlgo *hashAlgo;
132 
133 #if (PKCS5_SHA1_SUPPORT == ENABLED && SHA1_SUPPORT == ENABLED)
134  //HMAC with SHA-1 algorithm identifier?
136  {
137  hashAlgo = SHA1_HASH_ALGO;
138  }
139  else
140 #endif
141 #if (PKCS5_SHA224_SUPPORT == ENABLED && SHA224_SUPPORT == ENABLED)
142  //HMAC with SHA-224 algorithm identifier?
144  {
145  hashAlgo = SHA224_HASH_ALGO;
146  }
147  else
148 #endif
149 #if (PKCS5_SHA256_SUPPORT == ENABLED && SHA256_SUPPORT == ENABLED)
150  //HMAC with SHA-256 algorithm identifier?
152  {
153  hashAlgo = SHA256_HASH_ALGO;
154  }
155  else
156 #endif
157 #if (PKCS5_SHA384_SUPPORT == ENABLED && SHA384_SUPPORT == ENABLED)
158  //HMAC with SHA-384 algorithm identifier?
160  {
161  hashAlgo = SHA384_HASH_ALGO;
162  }
163  else
164 #endif
165 #if (PKCS5_SHA512_SUPPORT == ENABLED && SHA512_SUPPORT == ENABLED)
166  //HMAC with SHA-512 algorithm identifier?
168  {
169  hashAlgo = SHA512_HASH_ALGO;
170  }
171  else
172 #endif
173 #if (PKCS5_SHA512_224_SUPPORT == ENABLED && SHA512_224_SUPPORT == ENABLED)
174  //HMAC with SHA-512/224 algorithm identifier?
176  {
177  hashAlgo = SHA512_224_HASH_ALGO;
178  }
179  else
180 #endif
181 #if (PKCS5_SHA512_256_SUPPORT == ENABLED && SHA512_256_SUPPORT == ENABLED)
182  //HMAC with SHA-512/256 algorithm identifier?
184  {
185  hashAlgo = SHA512_256_HASH_ALGO;
186  }
187  else
188 #endif
189 #if (PKCS5_SM3_SUPPORT == ENABLED && SM3_SUPPORT == ENABLED)
190  //HMAC with SM3 algorithm identifier?
192  {
193  hashAlgo = SM3_HASH_ALGO;
194  }
195  else
196 #endif
197  //Unknown algorithm identifier?
198  {
199  hashAlgo = NULL;
200  }
201 
202  //Return the hash algorithm that matches the specified OID
203  return hashAlgo;
204 }
205 
206 
207 /**
208  * @brief Get the cipher algorithm to be used for PBES1 operation
209  * @param[in] oid Encryption algorithm identifier
210  * @param[in] length Length of the encryption algorithm identifier, in bytes
211  * @return Cipher algorithm
212  **/
213 
214 const CipherAlgo *pkcs5GetPbes1CipherAlgo(const uint8_t *oid, size_t length)
215 {
216  const CipherAlgo *cipherAlgo;
217 
218 #if (PKCS5_RC2_SUPPORT == ENABLED && RC2_SUPPORT == ENABLED)
219  //PBE with MD2 and RC2-CBC algorithm identifier?
221  {
222  cipherAlgo = RC2_CIPHER_ALGO;
223  }
224  //PBE with MD5 and RC2-CBC algorithm identifier?
226  {
227  cipherAlgo = RC2_CIPHER_ALGO;
228  }
229  //PBE with SHA-1 and RC2-CBC algorithm identifier?
231  {
232  cipherAlgo = RC2_CIPHER_ALGO;
233  }
234  else
235 #endif
236 #if (PKCS5_DES_SUPPORT == ENABLED && DES_SUPPORT == ENABLED)
237  //PBE with MD2 and DES-CBC algorithm identifier?
239  {
240  cipherAlgo = DES_CIPHER_ALGO;
241  }
242  //PBE with MD5 and DES-CBC algorithm identifier?
244  {
245  cipherAlgo = DES_CIPHER_ALGO;
246  }
247  //PBE with SHA-1 and DES-CBC algorithm identifier?
249  {
250  cipherAlgo = DES_CIPHER_ALGO;
251  }
252  else
253 #endif
254  //Unknown algorithm identifier?
255  {
256  cipherAlgo = NULL;
257  }
258 
259  //Return the cipher algorithm that matches the specified OID
260  return cipherAlgo;
261 }
262 
263 
264 /**
265  * @brief Get the cipher algorithm to be used for PBES2 operation
266  * @param[in] oid Encryption algorithm identifier
267  * @param[in] length Length of the encryption algorithm identifier, in bytes
268  * @return Cipher algorithm
269  **/
270 
271 const CipherAlgo *pkcs5GetPbes2CipherAlgo(const uint8_t *oid, size_t length)
272 {
273  const CipherAlgo *cipherAlgo;
274 
275 #if (PKCS5_DES_SUPPORT == ENABLED && DES_SUPPORT == ENABLED)
276  //DES-CBC algorithm identifier?
277  if(OID_COMP(oid, length, DES_CBC_OID) == 0)
278  {
279  cipherAlgo = DES_CIPHER_ALGO;
280  }
281  else
282 #endif
283 #if (PKCS5_3DES_SUPPORT == ENABLED && DES3_SUPPORT == ENABLED)
284  //DES-EDE3-CBC algorithm identifier?
285  if(OID_COMP(oid, length, DES_EDE3_CBC_OID) == 0)
286  {
287  cipherAlgo = DES3_CIPHER_ALGO;
288  }
289  else
290 #endif
291 #if (PKCS5_AES_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
292  //AES128-CBC algorithm identifier?
293  if(OID_COMP(oid, length, AES128_CBC_OID) == 0)
294  {
295  cipherAlgo = AES_CIPHER_ALGO;
296  }
297  //AES192-CBC algorithm identifier?
298  else if(OID_COMP(oid, length, AES192_CBC_OID) == 0)
299  {
300  cipherAlgo = AES_CIPHER_ALGO;
301  }
302  //AES256-CBC algorithm identifier?
303  else if(OID_COMP(oid, length, AES256_CBC_OID) == 0)
304  {
305  cipherAlgo = AES_CIPHER_ALGO;
306  }
307  else
308 #endif
309 #if (PKCS5_CAMELLIA_SUPPORT == ENABLED && CAMELLIA_SUPPORT == ENABLED)
310  //Camellia128-CBC algorithm identifier?
312  {
313  cipherAlgo = CAMELLIA_CIPHER_ALGO;
314  }
315  //Camellia192-CBC algorithm identifier?
316  else if(OID_COMP(oid, length, CAMELLIA192_CBC_OID) == 0)
317  {
318  cipherAlgo = CAMELLIA_CIPHER_ALGO;
319  }
320  //Camellia256-CBC algorithm identifier?
321  else if(OID_COMP(oid, length, CAMELLIA256_CBC_OID) == 0)
322  {
323  cipherAlgo = CAMELLIA_CIPHER_ALGO;
324  }
325  else
326 #endif
327 #if (PKCS5_ARIA_SUPPORT == ENABLED && ARIA_SUPPORT == ENABLED)
328  //ARIA128-CBC algorithm identifier?
329  if(OID_COMP(oid, length, ARIA128_CBC_OID) == 0)
330  {
331  cipherAlgo = ARIA_CIPHER_ALGO;
332  }
333  //ARIA192-CBC algorithm identifier?
334  else if(OID_COMP(oid, length, ARIA192_CBC_OID) == 0)
335  {
336  cipherAlgo = ARIA_CIPHER_ALGO;
337  }
338  //ARIA256-CBC algorithm identifier?
339  else if(OID_COMP(oid, length, ARIA256_CBC_OID) == 0)
340  {
341  cipherAlgo = ARIA_CIPHER_ALGO;
342  }
343  else
344 #endif
345 #if (PKCS5_SM4_SUPPORT == ENABLED && SM4_SUPPORT == ENABLED)
346  //SM4-CBC algorithm identifier?
347  if(OID_COMP(oid, length, SM4_CBC_OID) == 0)
348  {
349  cipherAlgo = SM4_CIPHER_ALGO;
350  }
351  else
352 #endif
353  //Unknown algorithm identifier?
354  {
355  cipherAlgo = NULL;
356  }
357 
358  //Return the cipher algorithm that matches the specified OID
359  return cipherAlgo;
360 }
361 
362 
363 /**
364  * @brief Get the encryption key length to be used for PBES2 operation
365  * @param[in] oid Encryption algorithm identifier
366  * @param[in] length Length of the encryption algorithm identifier, in bytes
367  * @return Encryption key length
368  **/
369 
370 uint_t pkcs5GetPbes2KeyLength(const uint8_t *oid, size_t length)
371 {
372  uint_t keyLen;
373 
374 #if (PKCS5_DES_SUPPORT == ENABLED && DES_SUPPORT == ENABLED)
375  //DES-CBC algorithm identifier?
376  if(OID_COMP(oid, length, DES_CBC_OID) == 0)
377  {
378  keyLen = 8;
379  }
380  else
381 #endif
382 #if (PKCS5_3DES_SUPPORT == ENABLED && DES3_SUPPORT == ENABLED)
383  //DES-EDE3-CBC algorithm identifier?
384  if(OID_COMP(oid, length, DES_EDE3_CBC_OID) == 0)
385  {
386  keyLen = 24;
387  }
388  else
389 #endif
390 #if (PKCS5_AES_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
391  //AES128-CBC algorithm identifier?
392  if(OID_COMP(oid, length, AES128_CBC_OID) == 0)
393  {
394  keyLen = 16;
395  }
396  //AES192-CBC algorithm identifier?
397  else if(OID_COMP(oid, length, AES192_CBC_OID) == 0)
398  {
399  keyLen = 24;
400  }
401  //AES256-CBC algorithm identifier?
402  else if(OID_COMP(oid, length, AES256_CBC_OID) == 0)
403  {
404  keyLen = 32;
405  }
406  else
407 #endif
408 #if (PKCS5_CAMELLIA_SUPPORT == ENABLED && CAMELLIA_SUPPORT == ENABLED)
409  //Camellia128-CBC algorithm identifier?
411  {
412  keyLen = 16;
413  }
414  //Camellia192-CBC algorithm identifier?
415  else if(OID_COMP(oid, length, CAMELLIA192_CBC_OID) == 0)
416  {
417  keyLen = 24;
418  }
419  //Camellia256-CBC algorithm identifier?
420  else if(OID_COMP(oid, length, CAMELLIA256_CBC_OID) == 0)
421  {
422  keyLen = 32;
423  }
424  else
425 #endif
426 #if (PKCS5_ARIA_SUPPORT == ENABLED && ARIA_SUPPORT == ENABLED)
427  //ARIA128-CBC algorithm identifier?
428  if(OID_COMP(oid, length, ARIA128_CBC_OID) == 0)
429  {
430  keyLen = 16;
431  }
432  //ARIA192-CBC algorithm identifier?
433  else if(OID_COMP(oid, length, ARIA192_CBC_OID) == 0)
434  {
435  keyLen = 24;
436  }
437  //ARIA256-CBC algorithm identifier?
438  else if(OID_COMP(oid, length, ARIA256_CBC_OID) == 0)
439  {
440  keyLen = 32;
441  }
442  else
443 #endif
444 #if (PKCS5_SM4_SUPPORT == ENABLED && SM4_SUPPORT == ENABLED)
445  //SM4-CBC algorithm identifier?
446  if(OID_COMP(oid, length, SM4_CBC_OID) == 0)
447  {
448  keyLen = 16;
449  }
450  else
451 #endif
452  //Unknown algorithm identifier?
453  {
454  keyLen = 0;
455  }
456 
457  //Return the encryption key length that matches the specified OID
458  return keyLen;
459 }
460 
461 #endif
#define SHA512_224_HASH_ALGO
Definition: sha512_224.h:45
#define ARIA_CIPHER_ALGO
Definition: aria.h:40
#define SHA256_HASH_ALGO
Definition: sha256.h:49
const HashAlgo * pkcs5GetPbes1HashAlgo(const uint8_t *oid, size_t length)
Get the hash algorithm to be used for PBES1 operation.
Definition: pkcs5_common.c:69
#define SHA1_HASH_ALGO
Definition: sha1.h:49
#define SHA512_HASH_ALGO
Definition: sha512.h:49
const uint8_t HMAC_WITH_SM3_OID[10]
Definition: hmac.c:77
const uint8_t HMAC_WITH_SHA1_OID[8]
Definition: hmac.c:55
OID (Object Identifier)
const uint8_t HMAC_WITH_SHA256_OID[8]
Definition: hmac.c:59
Collection of AEAD algorithms.
const uint8_t ARIA256_CBC_OID[9]
Definition: aria.c:267
const uint8_t ARIA192_CBC_OID[9]
Definition: aria.c:256
const uint8_t ARIA128_CBC_OID[9]
Definition: aria.c:245
const uint8_t DES_CBC_OID[5]
Definition: des.c:270
#define SM3_HASH_ALGO
Definition: sm3.h:49
const CipherAlgo * pkcs5GetPbes2CipherAlgo(const uint8_t *oid, size_t length)
Get the cipher algorithm to be used for PBES2 operation.
Definition: pkcs5_common.c:271
uint_t pkcs5GetPbes2KeyLength(const uint8_t *oid, size_t length)
Get the encryption key length to be used for PBES2 operation.
Definition: pkcs5_common.c:370
uint8_t oid[]
Definition: lldp_tlv.h:300
PKCS #5 common definitions.
const uint8_t PBE_WITH_SHA1_AND_DES_CBC_OID[9]
Definition: pkcs5_common.c:54
const uint8_t PBES2_OID[9]
Definition: pkcs5_common.c:59
#define RC2_CIPHER_ALGO
Definition: rc2.h:40
const HashAlgo * pkcs5GetPbes2HashAlgo(const uint8_t *oid, size_t length)
Get the hash algorithm to be used for PBES2 operation.
Definition: pkcs5_common.c:129
const uint8_t HMAC_WITH_SHA384_OID[8]
Definition: hmac.c:61
#define MD5_HASH_ALGO
Definition: md5.h:49
General definitions for cryptographic algorithms.
const uint8_t DES_EDE3_CBC_OID[8]
Definition: des3.c:48
#define MD2_HASH_ALGO
Definition: md2.h:44
const uint8_t CAMELLIA256_CBC_OID[11]
Definition: camellia.c:287
#define CAMELLIA_CIPHER_ALGO
Definition: camellia.h:40
const uint8_t SM4_CBC_OID[8]
Definition: sm4.c:99
#define DES_CIPHER_ALGO
Definition: des.h:45
const uint8_t PBE_WITH_MD2_AND_DES_CBC_OID[9]
Definition: pkcs5_common.c:46
uint8_t length
Definition: tcp.h:375
const uint8_t PBE_WITH_MD5_AND_RC2_CBC_OID[9]
Definition: pkcs5_common.c:52
const uint8_t PBE_WITH_MD2_AND_RC2_CBC_OID[9]
Definition: pkcs5_common.c:50
const uint8_t AES256_CBC_OID[9]
Definition: aes.c:208
#define SM4_CIPHER_ALGO
Definition: sm4.h:45
const uint8_t HMAC_WITH_SHA224_OID[8]
Definition: hmac.c:57
#define SHA384_HASH_ALGO
Definition: sha384.h:45
const uint8_t AES128_CBC_OID[9]
Definition: aes.c:182
const uint8_t PBE_WITH_SHA1_AND_RC2_CBC_OID[9]
Definition: pkcs5_common.c:56
#define OID_COMP(oid1, oidLen1, oid2)
Definition: oid.h:42
const uint8_t HMAC_WITH_SHA512_OID[8]
Definition: hmac.c:63
const CipherAlgo * pkcs5GetPbes1CipherAlgo(const uint8_t *oid, size_t length)
Get the cipher algorithm to be used for PBES1 operation.
Definition: pkcs5_common.c:214
Common interface for encryption algorithms.
Definition: crypto.h:1104
#define AES_CIPHER_ALGO
Definition: aes.h:45
const uint8_t CAMELLIA128_CBC_OID[11]
Definition: camellia.c:283
const uint8_t HMAC_WITH_SHA512_256_OID[8]
Definition: hmac.c:67
#define SHA512_256_HASH_ALGO
Definition: sha512_256.h:45
#define SHA224_HASH_ALGO
Definition: sha224.h:45
Common interface for hash algorithms.
Definition: crypto.h:1082
const uint8_t HMAC_WITH_SHA512_224_OID[8]
Definition: hmac.c:65
#define DES3_CIPHER_ALGO
Definition: des3.h:46
unsigned int uint_t
Definition: compiler_port.h:57
const uint8_t PBE_WITH_MD5_AND_DES_CBC_OID[9]
Definition: pkcs5_common.c:48
const uint8_t CAMELLIA192_CBC_OID[11]
Definition: camellia.c:285
const uint8_t AES192_CBC_OID[9]
Definition: aes.c:195
HMAC (Keyed-Hashing for Message Authentication)
Debugging facilities.