1 /*
2 * Copyright (c) 2001-2022, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #ifdef CC_IOT
8 #include "mbedtls/build_info.h"
9 #endif
10
11 #if !defined(CC_IOT) || ( defined(CC_IOT) && defined(MBEDTLS_RSA_C))
12
13
14 /************* Include Files ****************/
15
16 #include "cc_rsa_error.h"
17 #include "rsa_public.h"
18 #include "cc_rsa_types.h"
19 #include "pka.h"
20 #include "pki.h"
21 #include "cc_pal_mutex.h"
22 #include "pka_error.h"
23
24 extern const int8_t regTemps[PKA_MAX_COUNT_OF_PHYS_MEM_REGS];
25
26 /*********** RsaInitPubKeyDb function **********************/
27 /**
28 * @brief Initializes the low level key database public structure.
29 * On the HW platform the Barrett tag is initialized
30 *
31 * @return CC_OK On success, otherwise indicates failure
32 */
RsaInitPubKeyDb(CCRsaPubKey_t * pPubKey)33 CCError_t RsaInitPubKeyDb(CCRsaPubKey_t *pPubKey) /*!< [in] Public key structure. */
34 {
35 CCError_t error = CC_OK;
36
37 if (pPubKey == NULL) {
38 return CC_RSA_INVALID_PUB_KEY_STRUCT_POINTER_ERROR;
39 }
40
41 /* calculate Barrett tag NP by initialization PKA for modular operations.
42 Default settings: N=PKA_REG_N, NP=PKA_REG_NP, T0=30, T1=31.
43 Our settings for temps: rT0=2, rT1=3, rT2=4 */
44 error = PkiCalcNp( ((RsaPubKeyDb_t*)(pPubKey->ccRSAIntBuff))->NP, /*out*/
45 pPubKey->n, /*in*/
46 pPubKey->nSizeInBits);
47
48 return error;
49
50
51 }
52
53 /*********** RsaExecPubKeyExp function **********************/
54 /**
55 * @brief Executes the RSA primitive public key exponent :
56 *
57 * pPubData->DataOut = pPubData->DataIn ** pPubKey->e mod pPubKey->n,
58 * where: ** - exponent symbol.
59 *
60 * Note: PKA registers used: r0-r4, r30,r31, size of registers - Nsize.
61 *
62 * @return CC_OK On success, otherwise indicates failure
63 */
64
RsaExecPubKeyExp(CCRsaPubKey_t * pPubKey,CCRsaPrimeData_t * pPubData)65 CCError_t RsaExecPubKeyExp( CCRsaPubKey_t *pPubKey, /*!< [in] Public key structure. */
66 CCRsaPrimeData_t *pPubData ) /*!< [in] Containing input data and output buffer. */
67 {
68 CCError_t error = CC_OK;
69 uint32_t nSizeInWords, eSizeInWords;
70 uint32_t pkaReqRegs = 7;
71
72 uint8_t rT2 = regTemps[2];
73 uint8_t rT3 = regTemps[3];
74 uint8_t rT4 = regTemps[4];
75
76 /* modulus size in bytes */
77 nSizeInWords = CALC_FULL_32BIT_WORDS(pPubKey->nSizeInBits);
78 eSizeInWords = CALC_FULL_32BIT_WORDS(pPubKey->eSizeInBits);
79 if (nSizeInWords > CALC_FULL_32BIT_WORDS(CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BITS)) {
80 return CC_RSA_INVALID_MODULUS_SIZE;
81 }
82
83 error = PkaInitAndMutexLock(pPubKey->nSizeInBits, &pkaReqRegs);
84 if (error != CC_OK) {
85 return error;
86 }
87
88 /* copy modulus N into r0 register */
89 PkaCopyDataIntoPkaReg(PKA_REG_N/*dstReg*/, LEN_ID_MAX_BITS/*LenID*/, pPubKey->n/*srcPtr*/,
90 nSizeInWords);
91
92 /* copy the NP into r1 register NP */
93 PkaCopyDataIntoPkaReg(PKA_REG_NP/*dstReg*/, LEN_ID_MAX_BITS/*LenID*/, ((RsaPubKeyDb_t*)(pPubKey->ccRSAIntBuff))->NP/*srcPtr*/,
94 CC_PKA_BARRETT_MOD_TAG_BUFF_SIZE_IN_WORDS);
95
96 /* copy input data into PKI register: DataIn=>r2 */
97 PkaCopyDataIntoPkaReg( rT2/*dstReg*/, LEN_ID_MAX_BITS/*LenID*/,
98 pPubData->DataIn, nSizeInWords);
99
100 /* copy exponent data PKI register: e=>r3 */
101 PkaCopyDataIntoPkaReg(rT3/*dstReg*/, LEN_ID_MAX_BITS/*LenID*/,
102 pPubKey->e, eSizeInWords);
103
104 /* .. calculate the exponent Res = OpA**OpB mod N; */
105 PKA_MOD_EXP(LEN_ID_N_BITS/*LenID*/, rT4/*Res*/, rT2/*OpA*/, rT3/*OpB*/);
106
107 /* copy result into output: r4 =>DataOut */
108 PkaCopyDataFromPkaReg(pPubData->DataOut, nSizeInWords, rT4/*srcReg*/);
109
110 PkaFinishAndMutexUnlock(pkaReqRegs);
111
112
113 return error;
114 }
115
116 #endif /* !defined(CC_IOT) || ( defined(CC_IOT) && defined(MBEDTLS_RSA_C)) */
117