1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #define CC_PAL_LOG_CUR_COMPONENT HOST_LOG_MASK_SECURE_BOOT
8 
9 /************* Include Files ****************/
10 #include "rsa_pki_pka.h"
11 #include "rsa_bsv.h"
12 
13 /************************ Defines ******************************/
14 
15 /************************ Enums ******************************/
16 
17 /************************ Typedefs ******************************/
18 
19 /************************ Global Data ******************************/
20 
21 #ifdef PKA_DEBUG
22   extern uint8_t tempRes[SB_CERT_RSA_KEY_SIZE_IN_WORDS+2*RSA_PKA_BIG_WORD_SIZE_IN_32_BIT_WORDS];
23   extern uint32_t g_SramPkaAddr;
24 #endif
25 
26 /* ********** Internal private functions ****************** */
27 
28 
29 /* ************************** public functions ********************************* */
30 
31 
32 /**
33  * @brief The RSA_CalcExponent calculates The following:
34  *
35  *                   Res = (Base ^ Exp) mod N. ( Exp = 0x10001 )
36  *
37  *        The calculation is done in a secured way using the PIC.
38  *
39  * @hwBaseAddress        - Cryptocell base address
40  * @Base_ptr[in]         - The pointer to the base buffer.
41  * @N_ptr[in]            - The pointer to the modulus buffer.
42  * @Np_ptr[in]           - The Np vector buffer..
43  *                       - Its size must be 160.
44  * @Res_ptr[out]         - The pointer to the buffer that will contain the result.
45  *
46  * @return CCError_t - On success CC_OK is returned, on failure a
47  *                        value MODULE_* as defined in ...
48  */
RSA_CalcExponent(unsigned long hwBaseAddress,uint32_t * Base_ptr,uint32_t * N_ptr,uint32_t * Np_ptr,uint32_t * Res_ptr)49 void RSA_CalcExponent( unsigned long hwBaseAddress,
50                        uint32_t *Base_ptr,
51                        uint32_t *N_ptr,
52                        uint32_t *Np_ptr,
53                        uint32_t *Res_ptr )
54 {
55 
56    /* Fix value for the Exponent */
57    uint32_t Exp = RSA_EXP_VAL;
58    uint32_t regsCount = 7; /*5 working + 2 temp registers*/
59 
60   /* FUNCTION LOGIC */
61 
62    /* .................... initialize local variables ...................... */
63    /* ---------------------------------------------------------------------- */
64 
65     /* initialize the PKA engine on default mode */
66     RSA_PKA_InitPka(SB_CERT_RSA_KEY_SIZE_IN_BITS, regsCount, hwBaseAddress);
67 
68     /* copy modulus N into r0 register */
69     RSA_HW_PKI_PKA_CopyDataIntoPkaReg( 0/*dstReg*/, 1/*LenID*/,
70                    N_ptr/*src_ptr*/, SB_CERT_RSA_KEY_SIZE_IN_WORDS, hwBaseAddress );
71 
72     /* copy the NP into r1 register NP */
73     RSA_HW_PKI_PKA_CopyDataIntoPkaReg( 1/*dstReg*/, 1/*LenID*/, Np_ptr/*src_ptr*/,
74                       RSA_HW_PKI_PKA_BARRETT_MOD_TAG_SIZE_IN_WORDS, hwBaseAddress );
75 
76     /* copy input data into PKI register: DataIn=>r2 */
77     RSA_HW_PKI_PKA_CopyDataIntoPkaReg( 2/*dstReg*/, 1/*LenID*/,
78              Base_ptr, SB_CERT_RSA_KEY_SIZE_IN_WORDS, hwBaseAddress );
79 
80     /* copy exponent data PKI register: e=>r3 */
81     RSA_HW_PKI_PKA_CopyDataIntoPkaReg( 3/*dstReg*/, 1/*LenID*/,
82              &Exp, RSA_EXP_SIZE_WORDS, hwBaseAddress );
83 
84 
85     /* .. calculate the exponent Res = OpA**OpB mod N;                  ... */
86     /* -------------------------------------------------------------------- */
87     RSA_HW_PKI_PKA_ModExp( 0/*LenID*/, 2/*OpA*/, 3/*OpB*/, 4/*Res*/, 0/*Tag*/, hwBaseAddress );
88 
89     /* copy result into output: r4 =>DataOut */
90     RSA_HW_PKI_PKA_CopyDataFromPkaReg( Res_ptr, SB_CERT_RSA_KEY_SIZE_IN_WORDS,
91                    4/*srcReg*/, hwBaseAddress );
92 
93     /* Finish PKA operations (waiting PKI done and close PKA clocks) */
94     RSA_HW_PKI_PKA_FinishPKA( hwBaseAddress );
95 
96 }/* END OF RSA_CalcExponent */
97