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
9 /************* Include Files ****************/
10
11 #include "cc_pal_mem.h"
12 #include "cc_ecpki_error.h"
13 #include "cc_ecpki_local.h"
14 #include "ec_wrst.h"
15 #include "cc_fips_defs.h"
16
17 /************************ Defines *************************************/
18
19 /************************ Enums ***************************************/
20
21 /************************ Typedefs ************************************/
22
23 /************************ Global Data *********************************/
24
25 /************* Private function prototype *****************************/
26
27
28 /************************ Public Functions ****************************/
29
30
31 /***********************************************************************
32 * CC_EcdhSvdpDh function *
33 ***********************************************************************/
CC_EcdhSvdpDh(CCEcpkiUserPublKey_t * PartnerPublKey_ptr,CCEcpkiUserPrivKey_t * UserPrivKey_ptr,uint8_t * SharedSecretValue_ptr,size_t * SharedSecrValSize_ptr,CCEcdhTempData_t * TempBuff_ptr)34 CEXPORT_C CCError_t CC_EcdhSvdpDh(
35 CCEcpkiUserPublKey_t *PartnerPublKey_ptr, /*in*/
36 CCEcpkiUserPrivKey_t *UserPrivKey_ptr, /*in*/
37 uint8_t *SharedSecretValue_ptr, /*out*/
38 size_t *SharedSecrValSize_ptr, /*in/out*/
39 CCEcdhTempData_t *TempBuff_ptr /*in*/ )
40 {
41 /* LOCAL INITIALIZATIONS AND DECLERATIONS */
42
43 /* the error identifier */
44 CCError_t Error = CC_OK;
45
46 CCEcpkiPublKey_t *PublKey_ptr;
47 CCEcpkiPrivKey_t *PrivKey_ptr;
48
49 /* pointer to the current Domain structure */
50 CCEcpkiDomain_t *pDomain, *pPublDomain;
51 uint32_t modSizeInBytes;
52
53 CHECK_AND_RETURN_ERR_UPON_FIPS_ERROR();
54
55 /* ...... checking the validity of the user private key pointer .......... */
56 if (UserPrivKey_ptr == NULL)
57 return CC_ECDH_SVDP_DH_INVALID_USER_PRIV_KEY_PTR_ERROR;
58
59 /* ...... checking the valid tag of the user private key pointer ......... */
60 if (UserPrivKey_ptr->valid_tag != CC_ECPKI_PRIV_KEY_VALIDATION_TAG)
61 return CC_ECDH_SVDP_DH_USER_PRIV_KEY_VALID_TAG_ERROR;
62
63 /* .... checking the validity of the other partner public key pointer .... */
64 if (PartnerPublKey_ptr == NULL)
65 return CC_ECDH_SVDP_DH_INVALID_PARTNER_PUBL_KEY_PTR_ERROR;
66
67 /* ...... checking the valid tag of the user private key pointer ......... */
68 if (PartnerPublKey_ptr->valid_tag != CC_ECPKI_PUBL_KEY_VALIDATION_TAG)
69 return CC_ECDH_SVDP_DH_PARTNER_PUBL_KEY_VALID_TAG_ERROR;
70
71 /* ...... checking the validity of the SharedSecretValue pointer ..........*/
72 if (SharedSecretValue_ptr == NULL)
73 return CC_ECDH_SVDP_DH_INVALID_SHARED_SECRET_VALUE_PTR_ERROR;
74
75 /* ...... checking the validity of SharedSecrValSize_ptr pointer ......... */
76 if (SharedSecrValSize_ptr == NULL)
77 return CC_ECDH_SVDP_DH_INVALID_TEMP_DATA_PTR_ERROR;
78
79 /* ...... checking the validity of temp buffers .................. */
80 if (TempBuff_ptr == NULL)
81 return CC_ECDH_SVDP_DH_INVALID_SHARED_SECRET_VALUE_SIZE_PTR_ERROR;
82
83 /* .. initializtions and other checking .... */
84 /* --------------------------------------------- */
85
86 /* derive public and private keys pointers */
87 PublKey_ptr = (CCEcpkiPublKey_t*)&PartnerPublKey_ptr->PublKeyDbBuff;
88 PrivKey_ptr = (CCEcpkiPrivKey_t*)&UserPrivKey_ptr->PrivKeyDbBuff;
89
90 /* the pointers to private and public keys domains */
91 pDomain = &PrivKey_ptr->domain;
92 pPublDomain = &PublKey_ptr->domain;
93
94 /* if domains are not identical, return an error */
95 if(CC_PalMemCmp(pDomain, pPublDomain, sizeof(CCEcpkiDomain_t))) {
96 return CC_ECDH_SVDP_DH_NOT_CONCENT_PUBL_AND_PRIV_DOMAIN_ID_ERROR;
97 }
98
99 /* modulus size */
100 modSizeInBytes = CALC_FULL_BYTES(pDomain->modSizeInBits);
101
102 /* check the size of the buffer for Shared value */
103 if (*SharedSecrValSize_ptr < modSizeInBytes) {
104 *SharedSecrValSize_ptr = modSizeInBytes;
105 return CC_ECDH_SVDP_DH_INVALID_SHARED_SECRET_VALUE_SIZE_ERROR;
106 }
107
108 /* performing DH operations by calling EcWrstDhDeriveSharedSecret() function */
109 /*------------------------------------------------------------------*/
110 Error = EcWrstDhDeriveSharedSecret(
111 PublKey_ptr, PrivKey_ptr,
112 SharedSecretValue_ptr,
113 TempBuff_ptr);
114
115 if (Error != CC_OK)
116 goto End;
117
118 /* Set SharedSecrValSize = ModSizeInWords for user control */
119 *SharedSecrValSize_ptr = modSizeInBytes;
120
121 End:
122 if (Error != CC_OK) {
123 CC_PalMemSetZero(SharedSecretValue_ptr, *SharedSecrValSize_ptr);
124 *SharedSecrValSize_ptr = 0;
125 }
126 CC_PalMemSetZero(TempBuff_ptr, sizeof(CCEcdhTempData_t));
127
128 return Error;
129
130 }/* END OF CC_EcdhSvdpDh */
131
132