1 /***************************************************************************//**
2 * \file cy_cryptolite_nist_p.h
3 * \version 2.30
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 Cryptolite_EC_NistP_PointMul(CRYPTOLITE_Type *base, cy_stc_cryptolite_context_ecdsa_t *cfContext, uint8_t *p_x, uint8_t *p_y,
45                                 uint8_t *p_d, uint8_t *p_order, int bitsize);
46 
47 void Cryptolite_EC_DivMod( CRYPTOLITE_Type *base,
48                             cy_stc_cryptolite_context_ecdsa_t *cfContext,
49                             uint8_t * z,
50                             uint8_t * a,
51                             uint8_t * b,
52                             int size );
53 
54 void Cryptolite_EC_SquareMod( CRYPTOLITE_Type *base,
55                         cy_stc_cryptolite_context_ecdsa_t *cfContext,
56                         uint8_t * z,
57                         uint8_t * a,
58                         int size );
59 void Cryptolite_EC_MulMod ( CRYPTOLITE_Type *base,
60                             cy_stc_cryptolite_context_ecdsa_t *cfContext,
61                             uint8_t * z,
62                             uint8_t * a,
63                             uint8_t * b,
64                             int size );
65 
66 //void Cy_Cryptolite_EC_NistP_SetRedAlg(cy_en_cryptolite_ecc_red_mul_algs_t alg);
67 // void Cy_Cryptolite_EC_NistP_SetMode(uint32_t bitsize);
68 
69 /// @brief Modular addition in GF(p).
70 ///
71 /// z = a + b % mod
72 ///
73 /// Leaf function.
74 ///
75 /// @param[in] z Register index for sum value.
76 /// @param[in] a Register index for augend a value.
77 /// @param[in] b Register index for addend b value.
78 /// @param[in] mod Register index for modulo value.
Cryptolite_EC_AddMod(CRYPTOLITE_Type * base,cy_stc_cryptolite_context_ecdsa_t * cfContext,uint8_t * z,uint8_t * a,uint8_t * b)79 __STATIC_INLINE void Cryptolite_EC_AddMod (CRYPTOLITE_Type *base,
80                                             cy_stc_cryptolite_context_ecdsa_t *cfContext,
81                                             uint8_t *z, uint8_t *a, uint8_t *b)
82 {
83     uint8_t *temp = cfContext->p_buf;//[VU_BITS_TO_BYTES(BIT_SIZE+1)];
84     //int sign;
85     uint8_t *my_P = cfContext->my_P;
86     uint16_t bitsize = (uint16_t)cfContext->bitsize;
87     cy_stc_cryptolite_descr_t *vu_struct0 = &cfContext->vu_desptr[0];
88     cy_stc_cryptolite_descr_t *vu_struct1 = &cfContext->vu_desptr[1];
89 
90     (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));
91     (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));
92 }
93 
94 /// @brief Modular subtraction in GF(p).
95 ///
96 /// z = a - b % mod
97 ///
98 /// Leaf function.
99 ///
100 /// @param[in] z Register index for difference value.
101 /// @param[in] a Register index for minuend a value.
102 /// @param[in] b Register index for subtrahend b value.
103 /// @param[in] mod Register index for modulo value.
104 
Cryptolite_EC_SubMod(CRYPTOLITE_Type * base,cy_stc_cryptolite_context_ecdsa_t * cfContext,uint8_t * z,uint8_t * a,uint8_t * b)105 __STATIC_INLINE void Cryptolite_EC_SubMod (CRYPTOLITE_Type *base,
106                                             cy_stc_cryptolite_context_ecdsa_t *cfContext,
107                                             uint8_t * z, uint8_t * a, uint8_t * b)
108 {
109     int sign;
110     //int flag = 0;
111     cy_stc_cryptolite_descr_t *vu_struct0 = &cfContext->vu_desptr[0];
112     cy_stc_cryptolite_descr_t *vu_struct1 = &cfContext->vu_desptr[1];
113     uint8_t *my_P = cfContext->my_P;
114     uint16_t bitsize = (uint16_t)cfContext->bitsize;
115     uint8_t *temp_z = cfContext->p_buf;//[VU_BITS_TO_BYTES(BIT_SIZE+1)];
116 
117     (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));
118     (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));
119 
120     Cy_Cryptolite_Vu_wait_hw(base);
121     sign = (int)Cy_Cryptolite_Vu_get_bit(temp_z, bitsize);
122 
123     if ((bool)sign)
124     {
125         (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));
126     }
127 }
128 
129 /// @brief Modular halving in GF(p).
130 ///
131 /// z = a / 2 % mod
132 ///
133 /// Leaf function.
134 ///
135 /// @param[in] z Register index for result value.
136 /// @param[in] a Register index for value to be halved.
137 /// @param[in] mod Register index for modulo value.
Cryptolite_EC_HalfMod(CRYPTOLITE_Type * base,cy_stc_cryptolite_context_ecdsa_t * cfContext,uint8_t * z,uint8_t * a)138 __STATIC_INLINE void Cryptolite_EC_HalfMod (CRYPTOLITE_Type *base,
139                                             cy_stc_cryptolite_context_ecdsa_t *cfContext,
140                                             uint8_t * z, uint8_t * a)
141 {
142     bool odd, sign;
143     uint8_t *temp_a = cfContext->p_buf;//[VU_BITS_TO_BYTES(BIT_SIZE+1)];
144     cy_stc_cryptolite_descr_t *vu_struct0 = &cfContext->vu_desptr[0];
145     cy_stc_cryptolite_descr_t *vu_struct1 = &cfContext->vu_desptr[1];
146     uint8_t *my_P = cfContext->my_P;
147     uint16_t bitsize = (uint16_t)cfContext->bitsize;
148 
149     Cy_Cryptolite_Vu_wait_hw(base);
150     odd =  !Cy_Cryptolite_Vu_test_even(a);
151     sign = 0;
152 
153     if (odd)
154     {
155         (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));
156         (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));
157         sign = (bool)Cy_Cryptolite_Vu_get_bit(temp_a, bitsize);
158     }
159     (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);
160 }
161 
162 
163 #endif /* #if (CY_CRYPTOLITE_CFG_ECP_C == 1) */
164 #endif /* #if (CPUSS_CRYPTOLITE_VU == 1) */
165 #if defined(__cplusplus)
166 }
167 #endif
168 
169 #endif /* CY_IP_MXCRYPTOLITE */
170 #endif /* #if !defined (CY_CRYPTOLITE_NIST_P_H) */
171 
172 /* [] END OF FILE */
173