1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*
8  * All the includes that are needed for code using this module to
9  * compile correctly should be #included here.
10  */
11 #include "cc_pal_mem.h"
12 #include "cc_pal_types.h"
13 #include "cc_hal_plat.h"
14 #include "cc_common_math.h"
15 #include "cc_ecpki_error.h"
16 #include "cc_ec_mont_api.h"
17 
18 #include "pka_hw_defs.h"
19 #include "pka.h"
20 #include "pki.h"
21 #include "pka_error.h"
22 #include "ec_mont.h"
23 #include "pka_ec_mont_glob_regs_def.h"
24 #include "cc_int_general_defs.h"
25 
26 /* global data definitions */
27 extern CC_PalMutex CCAsymCryptoMutex;
28 
29 
30 /*********************************************************************/
31 /*!
32 @brief The function performs EC Montgomery (Curve25519) scalar multiplication:
33        resPoint = scalar * point.
34 
35  @return CCError_t
36 */
EcMontScalarmult(uint32_t * resPoint,uint32_t * scalar,uint32_t * inPoint,const CCEcMontDomain_t * pEcDomain)37 CCError_t EcMontScalarmult(
38                                 uint32_t *resPoint,       /* [out] pointer to result point (compressed,
39                                                                    the size = ec modulus size) */
40                                 uint32_t *scalar,         /* [in] pointer to the scalar, the size = ec order size) */
41                                 uint32_t *inPoint,        /* [in] pointer to the input point (compressed,
42                                                                        the size = ec modulus size)  */
43                                 const CCEcMontDomain_t *pEcDomain /* [in] pointer to EC domain (curve). */)
44 {
45         CCError_t err = CC_OK;
46         uint32_t pkaRegsUsed = EC_MONT_PKA_REGS_USED;
47         uint32_t scalarSizeBits;
48 
49         /* get the hardware semaphore  */
50         err = CC_PalMutexLock(&CCAsymCryptoMutex, CC_INFINITE);
51         if (err != CC_SUCCESS) {
52             CC_PalAbort("Fail to acquire mutex\n");
53         }
54 
55         /* verify that the device is not in fatal error state before activating the PKA engine */
56         CC_IS_FATAL_ERR_ON(err);
57         if (err == CC_TRUE) {
58                 /* release the hardware semaphore */
59                 if (CC_PalMutexUnlock(&CCAsymCryptoMutex) != CC_SUCCESS) {
60                         CC_PalAbort("Fail to release mutex\n");
61                 }
62                 return PKA_FATAL_ERR_STATE_ERROR;
63         }
64 
65         /* increase CC counter at the beginning of each operation */
66         err = CC_IS_WAKE;
67         if (err != CC_SUCCESS) {
68             CC_PalAbort("Fail to increase PM counter\n");
69         }
70 
71         /* init PKA, mapping and sizes tables */
72         err = PkaInitPka(pEcDomain->ecModSizeInBits, 0,
73                           &pkaRegsUsed/*regs.count*/);
74 
75         if (err != CC_SUCCESS) {
76                 goto End;
77         }
78 
79         scalarSizeBits = CC_CommonGetWordsCounterEffectiveSizeInBits(
80                                             scalar, pEcDomain->ecOrdSizeInWords);
81 
82         /* call EC scalar multiplication (with ladder) function */
83         err = EcMontPkaScalMultWithLadder(
84                         resPoint,
85                         scalar,
86                         scalarSizeBits,
87                         inPoint,
88                         pEcDomain);
89 
90 End:
91 
92         PkaFinishAndMutexUnlock(pkaRegsUsed);
93         return err;
94 
95 }
96 
97