1 /* 2 * Copyright (c) 2001-2022, Arm Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #ifndef _CC_RSA_BUILD_H 8 #define _CC_RSA_BUILD_H 9 10 #ifdef CC_IOT 11 #include "mbedtls/build_info.h" 12 #endif 13 14 #if !defined(CC_IOT) || ( defined(CC_IOT) && defined(MBEDTLS_RSA_C)) 15 16 #include "cc_error.h" 17 #include "cc_rsa_types.h" 18 19 #ifdef __cplusplus 20 extern "C" 21 { 22 #endif 23 24 /*! 25 @file 26 @brief This file defines some utility functions for working with RSA cryptography. 27 @defgroup cc_rsa_build CryptoCell RSA Utility APIs 28 @{ 29 @ingroup cc_rsa 30 */ 31 32 /******************************************************************************************/ 33 /*! 34 @brief Builds a ::CCRsaUserPubKey_t public key structure with the provided modulus and exponent. 35 36 @return CC_OK on success. 37 @return A non-zero value from cc_rsa_error.h on failure. 38 */ 39 CIMPORT_C CCError_t CC_RsaPubKeyBuild( 40 CCRsaUserPubKey_t *UserPubKey_ptr, /*!< [out] Pointer to the public key structure. */ 41 uint8_t *Exponent_ptr, /*!< [in] Pointer to the exponent stream of bytes (Big-Endian format). */ 42 size_t ExponentSize, /*!< [in] The size of the exponent (in bytes). */ 43 uint8_t *Modulus_ptr, /*!< [in] Pointer to the modulus stream of bytes (Big-Endian format). 44 The most significant bit (MSB) must be set to '1'. */ 45 size_t ModulusSize /*!< [in] The modulus size in bytes. Supported sizes are 256, 384 and 512 bytes. */ 46 ); 47 48 49 /******************************************************************************************/ 50 /*! 51 @brief Builds a ::CCRsaUserPrivKey_t private-key structure with the provided modulus and exponent, marking the key as a non-CRT key. 52 53 @return CC_OK on success. 54 @return A non-zero value from cc_rsa_error.h on failure. 55 */ 56 CIMPORT_C CCError_t CC_RsaPrivKeyBuild( 57 CCRsaUserPrivKey_t *UserPrivKey_ptr, /*!< [out] Pointer to the public key structure.*/ 58 uint8_t *PrivExponent_ptr, /*!< [in] Pointer to the private exponent stream of bytes (Big-Endian format). */ 59 size_t PrivExponentSize, /*!< [in] The size of the private exponent (in bytes). */ 60 uint8_t *PubExponent_ptr, /*!< [in] Pointer to the public exponent stream of bytes (Big-Endian format). */ 61 size_t PubExponentSize, /*!< [in] The size of the public exponent (in bytes). */ 62 uint8_t *Modulus_ptr, /*!< [in] Pointer to the modulus stream of bytes (Big-Endian format). 63 The most significant bit must be set to '1'. */ 64 size_t ModulusSize /*!< [in] The modulus size in bytes. Supported sizes are 256, 384 and 512. */ 65 ); 66 67 /******************************************************************************************/ 68 /*! 69 @brief Builds a ::CCRsaUserPrivKey_t private-key structure with the provided parameters, marking the key as a CRT key. 70 71 @return CC_OK on success. 72 @return A non-zero value from cc_rsa_error.h on failure. 73 */ 74 CIMPORT_C CCError_t CC_RsaPrivKeyCrtBuild( 75 CCRsaUserPrivKey_t *UserPrivKey_ptr, /*!< [out] Pointer to the public key structure. */ 76 uint8_t *P_ptr, /*!< [in] Pointer to the first factor stream of bytes (Big-Endian format). */ 77 size_t PSize, /*!< [in] The size of the first factor (in bytes). */ 78 uint8_t *Q_ptr, /*!< [in] Pointer to the second factor stream of bytes (Big-Endian format). */ 79 size_t QSize, /*!< [in] The size of the second factor (in bytes). */ 80 uint8_t *dP_ptr, /*!< [in] Pointer to the first factor's CRT exponent stream of bytes 81 (Big-Endian format). */ 82 size_t dPSize, /*!< [in] The size of the first factor's CRT exponent (in bytes). */ 83 uint8_t *dQ_ptr, /*!< [in] Pointer to the second factor's CRT exponent stream of bytes 84 (Big-Endian format). */ 85 size_t dQSize, /*!< [in] The size of the second factor's CRT exponent (in bytes). */ 86 uint8_t *qInv_ptr, /*!< [in] Pointer to the first CRT coefficient stream of bytes (Big-Endian format). */ 87 size_t qInvSize /*!< [in] The size of the first CRT coefficient (in bytes). */ 88 ); 89 90 91 /******************************************************************************************/ 92 /*! 93 @brief The function gets the e,n public key parameters from the input 94 CCRsaUserPubKey_t structure. The function can also be used to retrieve the 95 modulus and exponent sizes only (Exponent_ptr AND Modulus_ptr must be set to 96 NULL). 97 98 \note All members of input UserPubKey_ptr structure must be initialized. 99 100 @return CC_OK on success. 101 @return A non-zero value from cc_rsa_error.h on failure. 102 */ 103 CIMPORT_C CCError_t CC_RsaPubKeyGet( 104 CCRsaUserPubKey_t *UserPubKey_ptr, /*!< [in] A pointer to the public key structure. */ 105 uint8_t *Exponent_ptr, /*!< [out] A pointer to the exponent stream of bytes (Big-Endian format). */ 106 size_t *ExponentSize_ptr, /*!< [in/out] the size of the exponent buffer in bytes, 107 it is updated to the actual size of the exponent, in bytes. */ 108 uint8_t *Modulus_ptr, /*!< [out] A pointer to the modulus stream of bytes (Big-Endian format). 109 The MS (most significant) bit must be set to '1'. */ 110 size_t *ModulusSize_ptr /*!< [in/out] the size of the modulus buffer in bytes, it is updated to the actual 111 size of the modulus, in bytes. */ 112 ); 113 114 /******************************************************************************************/ 115 /*! 116 @brief The function gets the d,n and e - private key parameters (non CRT mode) from the input 117 CCRsaUserPrivKey_t structure. 118 119 \note All members of input UserPrivKey_ptr structure must be initialized. All output pointers must be allocated. 120 121 @return CC_OK on success. 122 @return A non-zero value from cc_rsa_error.h on failure. 123 */ 124 CEXPORT_C CCError_t CC_RsaGetPrivKey(CCRsaUserPrivKey_t *UserPrivKey_ptr /*!< [in] A pointer to the private key structure.*/, 125 uint8_t *PrivExponent_ptr /*!< [out] A pointer to the exponent stream of bytes (Big-Endian format).*/, 126 uint16_t *PrivExponentSize_ptr /*!< [in,out] The size of the private exponent buffer in bytes , it is updated to the 127 actual size of the private exponent, in bytes*/, 128 uint8_t *PubExponent_ptr /*!< [out] A pointer to the public exponent stream of bytes ( Big endian ).*/, 129 uint16_t *PubExponentSize_ptr, /*!< [in,out] The size of the exponent buffer in bytes , it is updated to the 130 actual size of the exponent, in bytes*/ 131 uint8_t *Modulus_ptr, /*!< [out] A pointer to the modulus stream of bytes (Big-Endian format). 132 The MS (most significant) bit must be set to '1'.*/ 133 uint16_t *ModulusSize_ptr /*!< [in,out] The size of the modulus buffer in bytes , it is updated to the 134 actual size of the modulus, in bytes*/ 135 ); 136 137 138 /******************************************************************************************/ 139 /*! 140 @brief The function gets the P, Q, dP, dQ and QInv - private key parameters (CRT mode) from the input 141 CCRsaUserPrivKey_t structure. 142 143 \note All members of input UserPrivKey_ptr structure must be initialized. All output pointers must be allocated. 144 145 @return CC_OK on success. 146 @return A non-zero value from cc_rsa_error.h on failure. 147 */ 148 CEXPORT_C CCError_t CC_RsaGetPrivKeyCRT(CCRsaUserPrivKey_t *UserPrivKey_ptr /*!< [in] A pointer to the private key structure.*/, 149 uint8_t *P_ptr /*!< [out] A pointer to the first factor stream of bytes ( Big endian ).*/, 150 uint16_t *PSize_ptr, /*!< [in,out] The size of the first factor buffer in bytes , updated to the actual size of the 151 first factor, in bytes.*/ 152 uint8_t *Q_ptr, /*!< [out] A pointer to the second factor stream of bytes ( Big endian ).*/ 153 uint16_t *QSize_ptr, /*!< [in,out] The size of the second factor buffer in bytes , updated to the 154 actual size of the second factor, in bytes.*/ 155 uint8_t *dP_ptr, /*!< [out] A pointer to the first factors CRT exponent stream of bytes ( Big endian ).*/ 156 uint16_t *dPSize_ptr, /*!< [in,out] The size of the first factor exponent buffer in bytes , updated to the 157 actual size of the first factor exponent, in bytes.*/ 158 uint8_t *dQ_ptr, /*!< [out] A pointer to the second factors CRT exponent stream of bytes ( Big endian ).*/ 159 uint16_t *dQSize_ptr, /*!< [in,out] The size of the second factors CRT exponent buffer in bytes , updated to the 160 actual size of the second factors CRT exponent, in bytes.*/ 161 uint8_t *qInv_ptr, /*!< [out] A pointer to the first CRT coefficient stream of bytes ( Big endian ).*/ 162 uint16_t *qInvSize_ptr /*!< [in,out] The size of the first CRT coefficient buffer in bytes , updated to the 163 actual size of the first CRT coefficient, in bytes.*/ 164 ); 165 166 #ifdef __cplusplus 167 } 168 #endif 169 /** 170 @} 171 */ 172 #endif /* !defined(CC_IOT) || ( defined(CC_IOT) && defined(MBEDTLS_RSA_C)) */ 173 #endif /* _CC_RSA_BUILD_H */ 174