1 /*
2  * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 /** \file cc3xx_internal_rsa_util.h
9  *
10  * This file contains the declarations of the internal utility functions used
11  * to manipulate RSA types and key formats
12  *
13  */
14 
15 #ifndef CC3XX_INTERNAL_RSA_UTIL_H
16 #define CC3XX_INTERNAL_RSA_UTIL_H
17 
18 #include "psa/crypto.h"
19 
20 #include "cc_common.h"
21 #include "cc_ecpki_error.h"
22 #include "cc_pal_abort.h"
23 #include "cc_pal_mem.h"
24 #include "cc_pal_types.h"
25 
26 #include "cc_ecpki_build.h"
27 #include "cc_ecpki_domain.h"
28 #include "cc_ecpki_ecdsa.h"
29 #include "cc_ecpki_kg.h"
30 #include "cc_ecpki_local.h"
31 #include "pka_ec_wrst.h"
32 
33 #include "cc_rsa_types.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /* Minimal and maximal size of RSA modulus in bits
40  * According to FIPS 186-4 size in bits should be in range [1024...3072]
41  */
42 #if defined(ARCH_IS_CC310)
43 #define CC3XX_RSA_MIN_VALID_KEY_SIZE_VALUE_IN_BITS 1024
44 #define CC3XX_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BITS 2048
45 #define CC3XX_RSA_MIN_VALID_KEYGEN_SIZE_VALUE_IN_BITS 1024
46 #define CC3XX_RSA_MAX_VALID_KEYGEN_SIZE_VALUE_IN_BITS 2048
47 #else
48 #define CC3XX_RSA_MIN_VALID_KEY_SIZE_VALUE_IN_BITS 1024
49 #define CC3XX_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BITS 4096
50 #define CC3XX_RSA_MIN_VALID_KEYGEN_SIZE_VALUE_IN_BITS 1024
51 #define CC3XX_RSA_MAX_VALID_KEYGEN_SIZE_VALUE_IN_BITS 3072
52 #endif
53 
54 /**
55  * \brief Converts a \ref CCError_t to the corresponding \ref psa_status_t
56  *
57  * \param[in] cc_error An error of type \ref CCError_t
58  *
59  * \retval The corresponding value of type \ref psa_status_t
60  */
61 psa_status_t cc3xx_rsa_cc_error_to_psa_error(CCError_t cc_error);
62 
63 /**
64  * \brief Translate an RSA private key in DER format
65  */
66 psa_status_t cc3xx_rsa_save_der_priv_key(uint8_t *key_buffer,
67                                          size_t key_buffer_size, uint32_t *n,
68                                          uint32_t *e, uint32_t *d, uint32_t *p,
69                                          uint32_t *q, uint32_t *dP, uint32_t *dQ,
70                                          uint32_t *qInv, size_t d_size_bytes,
71                                          size_t *key_buffer_length);
72 /**
73  * \brief Extract the public key from the private key in PSA format
74  */
75 psa_status_t cc3xx_rsa_psa_priv_to_psa_publ(uint8_t *priv_key_buffer,
76                                             size_t priv_key_buffer_size,
77                                             uint8_t *publ_key_buffer,
78                                             size_t publ_key_buffer_size,
79                                             size_t *publ_key_buffer_length);
80 /**
81  * \brief Convert a private key in PSA format to the same key in CC format, i.e.
82  *        the type specified by the low-level driver code, i.e.
83  *        \ref CCRsaUserPrivKey_t
84  */
85 psa_status_t cc3xx_rsa_psa_priv_to_cc_priv(const uint8_t *psa_priv_key_buffer,
86                                            size_t psa_priv_key_buffer_size,
87                                            CCRsaUserPrivKey_t *UserPrivKey_ptr);
88 /**
89  * \brief Extract the public key from a private key in PSA format, and in the
90  *        process translates it to CC format, i.e. the type specified by the
91  *        low-level driver, i.e. \ref CCRsaUserPubKey_t
92  */
93 psa_status_t cc3xx_rsa_psa_priv_to_cc_pub(const uint8_t *psa_priv_key_buffer,
94                                           size_t psa_priv_key_buffer_size,
95                                           CCRsaUserPubKey_t *UserPubKey_ptr);
96 /**
97  * \brief Convert a public key in PSA format to the same key in CC format, i.e.
98  *        the type specified by the low-level driver code, i.e.
99  *        \ref CCRsaUserPubKey_t
100  */
101 psa_status_t cc3xx_rsa_psa_pub_to_cc_pub(const uint8_t *psa_pub_key_buffer,
102                                          size_t psa_pub_key_buffer_size,
103                                          CCRsaUserPubKey_t *UserPubKey_ptr);
104 #ifdef __cplusplus
105 }
106 #endif
107 #endif /* CC3XX_INTERNAL_RSA_UTIL_H */
108