1 /***************************************************************************//**
2 * \file cy_cryptolite_nist_p.h
3 * \version 2.50
4 *
5 * \brief
6 *  This file provides constant and parameters
7 *  for the API of the ECC PDL in the Cryptolite driver.
8 *
9 ********************************************************************************
10 * Copyright 2020-2021 Cypress Semiconductor Corporation
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 *    http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *******************************************************************************/
25 
26 #if !defined (CY_CRYPTOLITE_NIST_P_H)
27 #define CY_CRYPTOLITE_NIST_P_H
28 
29 #include "cy_device.h"
30 
31 #if defined (CY_IP_MXCRYPTOLITE)
32 
33 #if defined(__cplusplus)
34 extern "C" {
35 #endif
36 
37 #include "cy_cryptolite_common.h"
38 #include "cy_cryptolite_vu.h"
39 #include "cy_cryptolite_ecdsa.h"
40 
41 #if (CRYPTOLITE_VU_PRESENT == 1)
42 #if defined(CY_CRYPTOLITE_CFG_ECP_C)
43 
44 void Cy_Cryptolite_EC_Bar_MulRed ( CRYPTOLITE_Type *base,
45                                 cy_stc_cryptolite_context_ecdsa_t *cfContext,
46                                 uint8_t* p_z,
47                                 uint8_t* p_x,
48                                 uint32_t bit_size );
49 
50 void Cy_Cryptolite_EC_NistP_PointMul(CRYPTOLITE_Type *base, cy_stc_cryptolite_context_ecdsa_t *cfContext, uint8_t *p_x, uint8_t *p_y,
51                                 uint8_t *p_d, uint8_t *p_order, int bitsize);
52 
53 void Cy_Cryptolite_EC_DivMod( CRYPTOLITE_Type *base,
54                             cy_stc_cryptolite_context_ecdsa_t *cfContext,
55                             uint8_t * z,
56                             uint8_t * a,
57                             uint8_t * b,
58                             int size );
59 
60 void Cy_Cryptolite_EC_SquareMod( CRYPTOLITE_Type *base,
61                         cy_stc_cryptolite_context_ecdsa_t *cfContext,
62                         uint8_t * z,
63                         uint8_t * a,
64                         int size );
65 void Cy_Cryptolite_EC_MulMod ( CRYPTOLITE_Type *base,
66                             cy_stc_cryptolite_context_ecdsa_t *cfContext,
67                             uint8_t * z,
68                             uint8_t * a,
69                             uint8_t * b,
70                             int size );
71 
72 //void Cy_Cryptolite_EC_NistP_SetRedAlg(cy_en_cryptolite_ecc_red_mul_algs_t alg);
73 // void Cy_Cryptolite_EC_NistP_SetMode(uint32_t bitsize);
74 
75 /// @brief Modular addition in GF(p).
76 ///
77 /// z = a + b % mod
78 ///
79 /// Leaf function.
80 ///
81 /// @param[in] z Register index for sum value.
82 /// @param[in] a Register index for augend a value.
83 /// @param[in] b Register index for addend b value.
84 /// @param[in] mod Register index for modulo value.
Cy_Cryptolite_EC_AddMod(CRYPTOLITE_Type * base,cy_stc_cryptolite_context_ecdsa_t * cfContext,uint8_t * z,uint8_t * a,uint8_t * b)85 __STATIC_INLINE void Cy_Cryptolite_EC_AddMod (CRYPTOLITE_Type *base,
86                                             cy_stc_cryptolite_context_ecdsa_t *cfContext,
87                                             uint8_t *z, uint8_t *a, uint8_t *b)
88 {
89     uint8_t *temp = cfContext->p_buf;//[VU_BITS_TO_BYTES(BIT_SIZE+1)];
90     //int sign;
91     uint8_t *my_P = cfContext->my_P;
92     uint16_t bitsize = (uint16_t)cfContext->bitsize;
93     cy_stc_cryptolite_descr_t *vu_struct0 = &cfContext->vu_desptr[0];
94     cy_stc_cryptolite_descr_t *vu_struct1 = &cfContext->vu_desptr[1];
95 
96     (void)Cy_Cryptolite_Vu_add_hw (base, vu_struct1, temp,VU_BITS_TO_WORDS((uint32_t)bitsize+1U), a,VU_BITS_TO_WORDS((uint32_t)bitsize), b,VU_BITS_TO_WORDS((uint32_t)bitsize));
97     (void)Cy_Cryptolite_Vu_cond_sub_hw (base, vu_struct0, z, VU_BITS_TO_WORDS((uint32_t)bitsize), temp, VU_BITS_TO_WORDS((uint32_t)bitsize+1U), my_P, VU_BITS_TO_WORDS((uint32_t)bitsize));
98 }
99 
100 /// @brief Modular subtraction in GF(p).
101 ///
102 /// z = a - b % mod
103 ///
104 /// Leaf function.
105 ///
106 /// @param[in] z Register index for difference value.
107 /// @param[in] a Register index for minuend a value.
108 /// @param[in] b Register index for subtrahend b value.
109 /// @param[in] mod Register index for modulo value.
110 
Cy_Cryptolite_EC_SubMod(CRYPTOLITE_Type * base,cy_stc_cryptolite_context_ecdsa_t * cfContext,uint8_t * z,uint8_t * a,uint8_t * b)111 __STATIC_INLINE void Cy_Cryptolite_EC_SubMod (CRYPTOLITE_Type *base,
112                                             cy_stc_cryptolite_context_ecdsa_t *cfContext,
113                                             uint8_t * z, uint8_t * a, uint8_t * b)
114 {
115     int sign;
116     //int flag = 0;
117     cy_stc_cryptolite_descr_t *vu_struct0 = &cfContext->vu_desptr[0];
118     cy_stc_cryptolite_descr_t *vu_struct1 = &cfContext->vu_desptr[1];
119     uint8_t *my_P = cfContext->my_P;
120     uint16_t bitsize = (uint16_t)cfContext->bitsize;
121     uint8_t *temp_z = cfContext->p_buf;//[VU_BITS_TO_BYTES(BIT_SIZE+1)];
122 
123     (void)Cy_Cryptolite_Vu_sub_hw (base, vu_struct0, temp_z, VU_BITS_TO_WORDS((uint32_t)bitsize+1U), a, VU_BITS_TO_WORDS((uint32_t)bitsize), b, VU_BITS_TO_WORDS((uint32_t)bitsize));
124     (void)Cy_Cryptolite_Vu_mov_hw (base, vu_struct1, z, VU_BITS_TO_WORDS((uint32_t)bitsize), temp_z,VU_BITS_TO_WORDS((uint32_t)bitsize));
125 
126     Cy_Cryptolite_Vu_wait_hw(base);
127     sign = (int)Cy_Cryptolite_Vu_get_bit(temp_z, bitsize);
128 
129     if ((bool)sign)
130     {
131         (void)Cy_Cryptolite_Vu_add_hw (base, vu_struct0, z, VU_BITS_TO_WORDS((uint32_t)bitsize), z,VU_BITS_TO_WORDS((uint32_t)bitsize), my_P,VU_BITS_TO_WORDS((uint32_t)bitsize));
132     }
133 }
134 
135 /// @brief Modular halving in GF(p).
136 ///
137 /// z = a / 2 % mod
138 ///
139 /// Leaf function.
140 ///
141 /// @param[in] z Register index for result value.
142 /// @param[in] a Register index for value to be halved.
143 /// @param[in] mod Register index for modulo value.
Cy_Cryptolite_EC_HalfMod(CRYPTOLITE_Type * base,cy_stc_cryptolite_context_ecdsa_t * cfContext,uint8_t * z,uint8_t * a)144 __STATIC_INLINE void Cy_Cryptolite_EC_HalfMod (CRYPTOLITE_Type *base,
145                                             cy_stc_cryptolite_context_ecdsa_t *cfContext,
146                                             uint8_t * z, uint8_t * a)
147 {
148     bool odd, sign;
149     uint8_t *temp_a = cfContext->p_buf;//[VU_BITS_TO_BYTES(BIT_SIZE+1)];
150     cy_stc_cryptolite_descr_t *vu_struct0 = &cfContext->vu_desptr[0];
151     cy_stc_cryptolite_descr_t *vu_struct1 = &cfContext->vu_desptr[1];
152     uint8_t *my_P = cfContext->my_P;
153     uint16_t bitsize = (uint16_t)cfContext->bitsize;
154 
155     Cy_Cryptolite_Vu_wait_hw(base);
156     odd =  !Cy_Cryptolite_Vu_test_even(a);
157     sign = 0;
158 
159     if (odd)
160     {
161         (void)Cy_Cryptolite_Vu_add_hw(base, vu_struct0, temp_a, VU_BITS_TO_WORDS((uint32_t)bitsize+1U), a, VU_BITS_TO_WORDS((uint32_t)bitsize), my_P, VU_BITS_TO_WORDS((uint32_t)bitsize));
162         (void)Cy_Cryptolite_Vu_add_hw(base, vu_struct1, a, VU_BITS_TO_WORDS((uint32_t)bitsize), a, VU_BITS_TO_WORDS((uint32_t)bitsize), my_P, VU_BITS_TO_WORDS((uint32_t)bitsize));
163         sign = (bool)Cy_Cryptolite_Vu_get_bit(temp_a, bitsize);
164     }
165     (void)Cy_Cryptolite_Vu_lsr1_carry_hw(base, vu_struct0, z, VU_BITS_TO_WORDS((uint32_t)bitsize), a, VU_BITS_TO_WORDS((uint32_t)bitsize), (uint32_t)sign, (uint32_t)bitsize);
166 }
167 
168 
169 #endif /* #if (CY_CRYPTOLITE_CFG_ECP_C == 1) */
170 #endif /* #if (CPUSS_CRYPTOLITE_VU == 1) */
171 #if defined(__cplusplus)
172 }
173 #endif
174 
175 #endif /* CY_IP_MXCRYPTOLITE */
176 #endif /* #if !defined (CY_CRYPTOLITE_NIST_P_H) */
177 
178 /* [] END OF FILE */
179