1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 #include "cc_pal_mem.h"
7 #include "cc_rnd_common.h"
8 #include "cc_ecpki_types.h"
9 #include "cc_ecpki_error.h"
10 #include "cc_ecpki_local.h"
11 #include "pki.h"
12 #include "pka_ec_wrst.h"
13 
14 /***************  EcWrstGenKeyPairBase function  **************/
15 /**
16  * @brief Generates a pair of private and public keys
17  *        in little endian ordinary (non-Montgomery) form using a cofigurable base point.
18  *
19  *    The function performs the following:
20  *      1. Checks the validity of all of the function inputs. If one of the received
21  *         parameters is not valid, it returns an error.
22  *      2. Cleans buffers and generates random private key.
23  *      3. Calls the low level function PkaEcWrstScalarMult to generate EC public key.
24  *      4. Outputs the user public and private key structures in little endian form.
25  *      5. Cleans temporary buffers.
26  *      6. Exits.
27  *
28  * @return  CC_OK On success, otherwise indicates failure
29 */
EcWrstGenKeyPairBase(const CCEcpkiDomain_t * pDomain,const uint32_t ecX[CC_ECPKI_MODUL_MAX_LENGTH_IN_WORDS],const uint32_t ecY[CC_ECPKI_MODUL_MAX_LENGTH_IN_WORDS],CCEcpkiUserPrivKey_t * pUserPrivKey,CCEcpkiUserPublKey_t * pUserPublKey,CCEcpkiKgTempData_t * pTempBuff)30 CEXPORT_C CCError_t EcWrstGenKeyPairBase(const CCEcpkiDomain_t  *pDomain,
31                                          const uint32_t         ecX [CC_ECPKI_MODUL_MAX_LENGTH_IN_WORDS],
32                                          const uint32_t         ecY [CC_ECPKI_MODUL_MAX_LENGTH_IN_WORDS],
33                                          CCEcpkiUserPrivKey_t   *pUserPrivKey,
34                                          CCEcpkiUserPublKey_t   *pUserPublKey,
35                                          CCEcpkiKgTempData_t    *pTempBuff)
36 {
37     CCError_t err = CC_OK;
38     CCEcpkiPrivKey_t *pPrivKey;
39     CCEcpkiPublKey_t *pPublKey;
40     uint32_t  orderSizeInWords;
41 
42     if (pDomain == NULL)
43         return CC_ECPKI_DOMAIN_PTR_ERROR;
44 
45     if (pUserPrivKey == NULL)
46         return CC_ECPKI_GEN_KEY_INVALID_PRIVATE_KEY_PTR_ERROR;
47 
48     if (pUserPublKey == NULL)
49         return CC_ECPKI_GEN_KEY_INVALID_PUBLIC_KEY_PTR_ERROR;
50 
51     if (pTempBuff == NULL)
52         return CC_ECPKI_GEN_KEY_INVALID_TEMP_DATA_PTR_ERROR;
53 
54     if (NULL == ecX || NULL == ecY)
55         return CC_ECPKI_INVALID_BASE_POINT_PTR_ERROR;
56 
57     /* the pointer to the key database */
58     pPrivKey = (CCEcpkiPrivKey_t *)&pUserPrivKey->PrivKeyDbBuff;
59     pPublKey = (CCEcpkiPublKey_t *)&pUserPublKey->PublKeyDbBuff;
60 
61     orderSizeInWords = (pDomain->ordSizeInBits+CC_BITS_IN_32BIT_WORD-1)/CC_BITS_IN_32BIT_WORD;
62 
63     /* calculate public key point coordinates */
64     err = PkaEcWrstScalarMult(pDomain,
65                               pPrivKey->PrivKey/*scalar*/, orderSizeInWords, /*scalar size*/
66                               ecX, ecY, /*in point coordinates*/
67                               pPublKey->x, pPublKey->y,  /*out point coordinates*/
68                               (uint32_t*)pTempBuff);
69     if(err) {
70         err = CC_ECPKI_INTERNAL_ERROR;
71         goto End;
72     }
73 
74     if(CC_OK == err) {
75         /*     set the EC domain and  keys valid tags        */
76         CC_PalMemCopy((uint8_t*)&pPrivKey->domain, (uint8_t*)pDomain, sizeof(pPrivKey->domain));
77         pUserPrivKey->valid_tag  = CC_ECPKI_PRIV_KEY_VALIDATION_TAG;
78 
79         CC_PalMemCopy((uint8_t*)&pPublKey->domain, (uint8_t*)pDomain, sizeof(pPublKey->domain));
80         pUserPublKey->valid_tag  = CC_ECPKI_PUBL_KEY_VALIDATION_TAG;
81         return err;
82     }
83 
84 End:
85     pUserPrivKey->valid_tag  = 0;
86     pUserPublKey->valid_tag  = 0;
87     CC_PalMemSet(pPrivKey, 0, sizeof(pPrivKey->PrivKey));
88     CC_PalMemSet(pPublKey, 0, 2*sizeof(pPublKey->x));
89     return err;
90 
91 }
92 
93 /***************  EcWrstGenKeyPair function  **************/
94 /**
95  * @brief Generates a pair of private and public keys
96  *        in little endian ordinary (non-Montgomery) form.
97  *
98  *    The function performs the following:
99  *      1. Checks the validity of all of the function inputs. If one of the received
100  *         parameters is not valid, it returns an error.
101  *      2. Cleans buffers and generates random private key.
102  *      3. Calls the low level function PkaEcWrstScalarMult to generate EC public key.
103  *      4. Outputs the user public and private key structures in little endian form.
104  *      5. Cleans temporary buffers.
105  *      6. Exits.
106  *
107  * @return  CC_OK On success, otherwise indicates failure
108 */
EcWrstGenKeyPair(const CCEcpkiDomain_t * pDomain,CCEcpkiUserPrivKey_t * pUserPrivKey,CCEcpkiUserPublKey_t * pUserPublKey,CCEcpkiKgTempData_t * pTempBuff)109 CEXPORT_C CCError_t EcWrstGenKeyPair(const CCEcpkiDomain_t  *pDomain,      /*!< [in] Pointer to current EC domain.*/
110                     CCEcpkiUserPrivKey_t   *pUserPrivKey, /*!< [out] Pointer to the generated private key structure.*/
111                     CCEcpkiUserPublKey_t   *pUserPublKey, /*!< [out] Pointer to the generated public key structure.*/
112                     CCEcpkiKgTempData_t   *pTempBuff)    /*!< [in] Pointer to temporary buffer.*/
113 {
114 
115     if (pDomain == NULL)
116         return CC_ECPKI_DOMAIN_PTR_ERROR;
117 
118     return EcWrstGenKeyPairBase(pDomain, pDomain->ecGx, pDomain->ecGy, pUserPrivKey, pUserPublKey, pTempBuff);
119 
120 }
121 
122 
123 
124