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-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.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?
77  {
78  hashAlgo = MD2_HASH_ALGO;
79  }
80  //PBE with MD2 and DES-CBC algorithm identifier?
83  {
84  hashAlgo = MD2_HASH_ALGO;
85  }
86  else
87 #endif
88 #if (PKCS5_MD5_SUPPORT == ENABLED && MD5_SUPPORT == ENABLED)
89  //PBE with MD5 and RC2-CBC algorithm identifier?
92  {
93  hashAlgo = MD5_HASH_ALGO;
94  }
95  //PBE with MD5 and DES-CBC algorithm identifier?
98  {
99  hashAlgo = MD5_HASH_ALGO;
100  }
101  else
102 #endif
103 #if (PKCS5_SHA1_SUPPORT == ENABLED && SHA1_SUPPORT == ENABLED)
104  //PBE with SHA-1 and RC2-CBC algorithm identifier?
107  {
108  hashAlgo = SHA1_HASH_ALGO;
109  }
110  //PBE with SHA-1 and DES-CBC algorithm identifier?
113  {
114  hashAlgo = SHA1_HASH_ALGO;
115  }
116  else
117 #endif
118  //Unknown algorithm identifier?
119  {
120  hashAlgo = NULL;
121  }
122 
123  //Return the hash algorithm that matches the specified OID
124  return hashAlgo;
125 }
126 
127 
128 /**
129  * @brief Get the hash algorithm to be used for PBES2 operation
130  * @param[in] oid KDF algorithm identifier
131  * @param[in] length Length of the KDF algorithm identifier, in bytes
132  * @return Hash algorithm
133  **/
134 
135 const HashAlgo *pkcs5GetPbes2HashAlgo(const uint8_t *oid, size_t length)
136 {
137  const HashAlgo *hashAlgo;
138 
139 #if (PKCS5_SHA1_SUPPORT == ENABLED && SHA1_SUPPORT == ENABLED)
140  //HMAC with SHA-1 algorithm identifier?
142  {
143  hashAlgo = SHA1_HASH_ALGO;
144  }
145  else
146 #endif
147 #if (PKCS5_SHA224_SUPPORT == ENABLED && SHA224_SUPPORT == ENABLED)
148  //HMAC with SHA-224 algorithm identifier?
150  {
151  hashAlgo = SHA224_HASH_ALGO;
152  }
153  else
154 #endif
155 #if (PKCS5_SHA256_SUPPORT == ENABLED && SHA256_SUPPORT == ENABLED)
156  //HMAC with SHA-256 algorithm identifier?
158  {
159  hashAlgo = SHA256_HASH_ALGO;
160  }
161  else
162 #endif
163 #if (PKCS5_SHA384_SUPPORT == ENABLED && SHA384_SUPPORT == ENABLED)
164  //HMAC with SHA-384 algorithm identifier?
166  {
167  hashAlgo = SHA384_HASH_ALGO;
168  }
169  else
170 #endif
171 #if (PKCS5_SHA512_SUPPORT == ENABLED && SHA512_SUPPORT == ENABLED)
172  //HMAC with SHA-512 algorithm identifier?
174  {
175  hashAlgo = SHA512_HASH_ALGO;
176  }
177  else
178 #endif
179 #if (PKCS5_SHA512_224_SUPPORT == ENABLED && SHA512_224_SUPPORT == ENABLED)
180  //HMAC with SHA-512/224 algorithm identifier?
182  sizeof(HMAC_WITH_SHA512_224_OID)))
183  {
184  hashAlgo = SHA512_224_HASH_ALGO;
185  }
186  else
187 #endif
188 #if (PKCS5_SHA512_256_SUPPORT == ENABLED && SHA512_256_SUPPORT == ENABLED)
189  //HMAC with SHA-512/256 algorithm identifier?
191  sizeof(HMAC_WITH_SHA512_256_OID)))
192  {
193  hashAlgo = SHA512_256_HASH_ALGO;
194  }
195  else
196 #endif
197 #if (PKCS5_SM3_SUPPORT == ENABLED && SM3_SUPPORT == ENABLED)
198  //HMAC with SM3 algorithm identifier?
200  {
201  hashAlgo = SM3_HASH_ALGO;
202  }
203  else
204 #endif
205  //Unknown algorithm identifier?
206  {
207  hashAlgo = NULL;
208  }
209 
210  //Return the hash algorithm that matches the specified OID
211  return hashAlgo;
212 }
213 
214 
215 /**
216  * @brief Get the cipher algorithm to be used for PBES1 operation
217  * @param[in] oid Encryption algorithm identifier
218  * @param[in] length Length of the encryption algorithm identifier, in bytes
219  * @return Cipher algorithm
220  **/
221 
222 const CipherAlgo *pkcs5GetPbes1CipherAlgo(const uint8_t *oid, size_t length)
223 {
224  const CipherAlgo *cipherAlgo;
225 
226 #if (PKCS5_RC2_SUPPORT == ENABLED && RC2_SUPPORT == ENABLED)
227  //PBE with MD2 and RC2-CBC algorithm identifier?
230  {
231  cipherAlgo = RC2_CIPHER_ALGO;
232  }
233  //PBE with MD5 and RC2-CBC algorithm identifier?
236  {
237  cipherAlgo = RC2_CIPHER_ALGO;
238  }
239  //PBE with SHA-1 and RC2-CBC algorithm identifier?
242  {
243  cipherAlgo = RC2_CIPHER_ALGO;
244  }
245  else
246 #endif
247 #if (PKCS5_DES_SUPPORT == ENABLED && DES_SUPPORT == ENABLED)
248  //PBE with MD2 and DES-CBC algorithm identifier?
251  {
252  cipherAlgo = DES_CIPHER_ALGO;
253  }
254  //PBE with MD5 and DES-CBC algorithm identifier?
257  {
258  cipherAlgo = DES_CIPHER_ALGO;
259  }
260  //PBE with SHA-1 and DES-CBC algorithm identifier?
263  {
264  cipherAlgo = DES_CIPHER_ALGO;
265  }
266  else
267 #endif
268  //Unknown algorithm identifier?
269  {
270  cipherAlgo = NULL;
271  }
272 
273  //Return the cipher algorithm that matches the specified OID
274  return cipherAlgo;
275 }
276 
277 
278 /**
279  * @brief Get the cipher algorithm to be used for PBES2 operation
280  * @param[in] oid Encryption algorithm identifier
281  * @param[in] length Length of the encryption algorithm identifier, in bytes
282  * @return Cipher algorithm
283  **/
284 
285 const CipherAlgo *pkcs5GetPbes2CipherAlgo(const uint8_t *oid, size_t length)
286 {
287  const CipherAlgo *cipherAlgo;
288 
289 #if (PKCS5_DES_SUPPORT == ENABLED && DES_SUPPORT == ENABLED)
290  //DES-CBC algorithm identifier?
291  if(!oidComp(oid, length, DES_CBC_OID, sizeof(DES_CBC_OID)))
292  {
293  cipherAlgo = DES_CIPHER_ALGO;
294  }
295  else
296 #endif
297 #if (PKCS5_3DES_SUPPORT == ENABLED && DES3_SUPPORT == ENABLED)
298  //DES-EDE3-CBC algorithm identifier?
300  {
301  cipherAlgo = DES3_CIPHER_ALGO;
302  }
303  else
304 #endif
305 #if (PKCS5_AES_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
306  //AES128-CBC algorithm identifier?
308  {
309  cipherAlgo = AES_CIPHER_ALGO;
310  }
311  //AES192-CBC algorithm identifier?
312  else if(!oidComp(oid, length, AES192_CBC_OID, sizeof(AES192_CBC_OID)))
313  {
314  cipherAlgo = AES_CIPHER_ALGO;
315  }
316  //AES256-CBC algorithm identifier?
317  else if(!oidComp(oid, length, AES256_CBC_OID, sizeof(AES256_CBC_OID)))
318  {
319  cipherAlgo = AES_CIPHER_ALGO;
320  }
321  else
322 #endif
323 #if (PKCS5_CAMELLIA_SUPPORT == ENABLED && CAMELLIA_SUPPORT == ENABLED)
324  //Camellia128-CBC algorithm identifier?
326  {
327  cipherAlgo = CAMELLIA_CIPHER_ALGO;
328  }
329  //Camellia192-CBC algorithm identifier?
331  {
332  cipherAlgo = CAMELLIA_CIPHER_ALGO;
333  }
334  //Camellia256-CBC algorithm identifier?
336  {
337  cipherAlgo = CAMELLIA_CIPHER_ALGO;
338  }
339  else
340 #endif
341 #if (PKCS5_ARIA_SUPPORT == ENABLED && ARIA_SUPPORT == ENABLED)
342  //ARIA128-CBC algorithm identifier?
344  {
345  cipherAlgo = ARIA_CIPHER_ALGO;
346  }
347  //ARIA192-CBC algorithm identifier?
348  else if(!oidComp(oid, length, ARIA192_CBC_OID, sizeof(ARIA192_CBC_OID)))
349  {
350  cipherAlgo = ARIA_CIPHER_ALGO;
351  }
352  //ARIA256-CBC algorithm identifier?
353  else if(!oidComp(oid, length, ARIA256_CBC_OID, sizeof(ARIA256_CBC_OID)))
354  {
355  cipherAlgo = ARIA_CIPHER_ALGO;
356  }
357  else
358 #endif
359 #if (PKCS5_SM4_SUPPORT == ENABLED && SM4_SUPPORT == ENABLED)
360  //SM4-CBC algorithm identifier?
361  if(!oidComp(oid, length, SM4_CBC_OID, sizeof(SM4_CBC_OID)))
362  {
363  cipherAlgo = SM4_CIPHER_ALGO;
364  }
365  else
366 #endif
367  //Unknown algorithm identifier?
368  {
369  cipherAlgo = NULL;
370  }
371 
372  //Return the cipher algorithm that matches the specified OID
373  return cipherAlgo;
374 }
375 
376 
377 /**
378  * @brief Get the encryption key length to be used for PBES2 operation
379  * @param[in] oid Encryption algorithm identifier
380  * @param[in] length Length of the encryption algorithm identifier, in bytes
381  * @return Encryption key length
382  **/
383 
384 uint_t pkcs5GetPbes2KeyLength(const uint8_t *oid, size_t length)
385 {
386  uint_t keyLen;
387 
388 #if (PKCS5_DES_SUPPORT == ENABLED && DES_SUPPORT == ENABLED)
389  //DES-CBC algorithm identifier?
390  if(!oidComp(oid, length, DES_CBC_OID, sizeof(DES_CBC_OID)))
391  {
392  keyLen = 8;
393  }
394  else
395 #endif
396 #if (PKCS5_3DES_SUPPORT == ENABLED && DES3_SUPPORT == ENABLED)
397  //DES-EDE3-CBC algorithm identifier?
399  {
400  keyLen = 24;
401  }
402  else
403 #endif
404 #if (PKCS5_AES_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
405  //AES128-CBC algorithm identifier?
407  {
408  keyLen = 16;
409  }
410  //AES192-CBC algorithm identifier?
411  else if(!oidComp(oid, length, AES192_CBC_OID, sizeof(AES192_CBC_OID)))
412  {
413  keyLen = 24;
414  }
415  //AES256-CBC algorithm identifier?
416  else if(!oidComp(oid, length, AES256_CBC_OID, sizeof(AES256_CBC_OID)))
417  {
418  keyLen = 32;
419  }
420  else
421 #endif
422 #if (PKCS5_CAMELLIA_SUPPORT == ENABLED && CAMELLIA_SUPPORT == ENABLED)
423  //Camellia128-CBC algorithm identifier?
425  {
426  keyLen = 16;
427  }
428  //Camellia192-CBC algorithm identifier?
430  {
431  keyLen = 24;
432  }
433  //Camellia256-CBC algorithm identifier?
435  {
436  keyLen = 32;
437  }
438  else
439 #endif
440 #if (PKCS5_ARIA_SUPPORT == ENABLED && ARIA_SUPPORT == ENABLED)
441  //ARIA128-CBC algorithm identifier?
443  {
444  keyLen = 16;
445  }
446  //ARIA192-CBC algorithm identifier?
447  else if(!oidComp(oid, length, ARIA192_CBC_OID, sizeof(ARIA192_CBC_OID)))
448  {
449  keyLen = 24;
450  }
451  //ARIA256-CBC algorithm identifier?
452  else if(!oidComp(oid, length, ARIA256_CBC_OID, sizeof(ARIA256_CBC_OID)))
453  {
454  keyLen = 32;
455  }
456  else
457 #endif
458 #if (PKCS5_SM4_SUPPORT == ENABLED && SM4_SUPPORT == ENABLED)
459  //SM4-CBC algorithm identifier?
460  if(!oidComp(oid, length, SM4_CBC_OID, sizeof(SM4_CBC_OID)))
461  {
462  keyLen = 16;
463  }
464  else
465 #endif
466  //Unknown algorithm identifier?
467  {
468  keyLen = 0;
469  }
470 
471  //Return the encryption key length that matches the specified OID
472  return keyLen;
473 }
474 
475 #endif
const uint8_t AES192_CBC_OID[9]
Definition: aes.c:195
const uint8_t AES256_CBC_OID[9]
Definition: aes.c:208
const uint8_t AES128_CBC_OID[9]
Definition: aes.c:182
#define AES_CIPHER_ALGO
Definition: aes.h:45
const uint8_t ARIA128_CBC_OID[9]
Definition: aria.c:239
const uint8_t ARIA256_CBC_OID[9]
Definition: aria.c:261
const uint8_t ARIA192_CBC_OID[9]
Definition: aria.c:250
#define ARIA_CIPHER_ALGO
Definition: aria.h:40
const uint8_t CAMELLIA256_CBC_OID[11]
Definition: camellia.c:283
const uint8_t CAMELLIA128_CBC_OID[11]
Definition: camellia.c:279
const uint8_t CAMELLIA192_CBC_OID[11]
Definition: camellia.c:281
#define CAMELLIA_CIPHER_ALGO
Definition: camellia.h:40
Collection of AEAD algorithms.
unsigned int uint_t
Definition: compiler_port.h:50
General definitions for cryptographic algorithms.
Debugging facilities.
const uint8_t DES_EDE3_CBC_OID[8]
Definition: des3.c:48
#define DES3_CIPHER_ALGO
Definition: des3.h:46
const uint8_t DES_CBC_OID[5]
Definition: des.c:270
#define DES_CIPHER_ALGO
Definition: des.h:45
const uint8_t HMAC_WITH_SM3_OID[10]
Definition: hmac.c:77
const uint8_t HMAC_WITH_SHA256_OID[8]
Definition: hmac.c:59
const uint8_t HMAC_WITH_SHA224_OID[8]
Definition: hmac.c:57
const uint8_t HMAC_WITH_SHA512_256_OID[8]
Definition: hmac.c:67
const uint8_t HMAC_WITH_SHA512_OID[8]
Definition: hmac.c:63
const uint8_t HMAC_WITH_SHA1_OID[8]
Definition: hmac.c:55
const uint8_t HMAC_WITH_SHA384_OID[8]
Definition: hmac.c:61
const uint8_t HMAC_WITH_SHA512_224_OID[8]
Definition: hmac.c:65
HMAC (Keyed-Hashing for Message Authentication)
uint8_t oid[]
Definition: lldp_tlv.h:300
#define MD2_HASH_ALGO
Definition: md2.h:44
#define MD5_HASH_ALGO
Definition: md5.h:49
int_t oidComp(const uint8_t *oid1, size_t oidLen1, const uint8_t *oid2, size_t oidLen2)
Compare object identifiers.
Definition: oid.c:103
OID (Object Identifier)
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:384
const uint8_t PBE_WITH_MD2_AND_DES_CBC_OID[9]
Definition: pkcs5_common.c:46
const uint8_t PBE_WITH_MD5_AND_DES_CBC_OID[9]
Definition: pkcs5_common.c:48
const uint8_t PBE_WITH_SHA1_AND_RC2_CBC_OID[9]
Definition: pkcs5_common.c:56
const CipherAlgo * pkcs5GetPbes1CipherAlgo(const uint8_t *oid, size_t length)
Get the cipher algorithm to be used for PBES1 operation.
Definition: pkcs5_common.c:222
const CipherAlgo * pkcs5GetPbes2CipherAlgo(const uint8_t *oid, size_t length)
Get the cipher algorithm to be used for PBES2 operation.
Definition: pkcs5_common.c:285
const HashAlgo * pkcs5GetPbes2HashAlgo(const uint8_t *oid, size_t length)
Get the hash algorithm to be used for PBES2 operation.
Definition: pkcs5_common.c:135
const uint8_t PBE_WITH_MD2_AND_RC2_CBC_OID[9]
Definition: pkcs5_common.c:50
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
const uint8_t PBE_WITH_MD5_AND_RC2_CBC_OID[9]
Definition: pkcs5_common.c:52
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
PKCS #5 common definitions.
#define RC2_CIPHER_ALGO
Definition: rc2.h:40
#define SHA1_HASH_ALGO
Definition: sha1.h:49
#define SHA224_HASH_ALGO
Definition: sha224.h:45
#define SHA256_HASH_ALGO
Definition: sha256.h:49
#define SHA384_HASH_ALGO
Definition: sha384.h:45
#define SHA512_HASH_ALGO
Definition: sha512.h:49
#define SHA512_224_HASH_ALGO
Definition: sha512_224.h:45
#define SHA512_256_HASH_ALGO
Definition: sha512_256.h:45
#define SM3_HASH_ALGO
Definition: sm3.h:49
const uint8_t SM4_CBC_OID[8]
Definition: sm4.c:99
#define SM4_CIPHER_ALGO
Definition: sm4.h:45
Common interface for encryption algorithms.
Definition: crypto.h:1036
Common interface for hash algorithms.
Definition: crypto.h:1014
uint8_t length
Definition: tcp.h:368