1 /******************************************************************************
2  *
3  *  Copyright 2006-2015 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /*******************************************************************************
20  *
21  *  This file contains simple pairing algorithms using Elliptic Curve
22  *  Cryptography for private public key
23  *
24  ******************************************************************************/
25 #include "p_256_ecc_pp.h"
26 
27 #include <cstdint>
28 #include <cstring>
29 
30 #include "p_256_multprecision.h"
31 
32 elliptic_curve_t curve;
33 elliptic_curve_t curve_p256;
34 
p_256_init_point(Point * q)35 static void p_256_init_point(Point* q) { memset(q, 0, sizeof(Point)); }
36 
p_256_copy_point(Point * q,Point * p)37 static void p_256_copy_point(Point* q, Point* p) {
38   memcpy(q, p, sizeof(Point));
39 }
40 
41 // q=2q
ECC_Double(Point * q,Point * p)42 static void ECC_Double(Point* q, Point* p) {
43   uint32_t t1[KEY_LENGTH_DWORDS_P256];
44   uint32_t t2[KEY_LENGTH_DWORDS_P256];
45   uint32_t t3[KEY_LENGTH_DWORDS_P256];
46   uint32_t* x1;
47   uint32_t* x3;
48   uint32_t* y1;
49   uint32_t* y3;
50   uint32_t* z1;
51   uint32_t* z3;
52 
53   if (multiprecision_iszero(p->z)) {
54     multiprecision_init(q->z);
55     return;  // return infinity
56   }
57 
58   x1 = p->x;
59   y1 = p->y;
60   z1 = p->z;
61   x3 = q->x;
62   y3 = q->y;
63   z3 = q->z;
64 
65   multiprecision_mersenns_squa_mod(t1, z1);      // t1=z1^2
66   multiprecision_sub_mod(t2, x1, t1);            // t2=x1-t1
67   multiprecision_add_mod(t1, x1, t1);            // t1=x1+t1
68   multiprecision_mersenns_mult_mod(t2, t1, t2);  // t2=t2*t1
69   multiprecision_lshift_mod(t3, t2);
70   multiprecision_add_mod(t2, t3, t2);  // t2=3t2
71 
72   multiprecision_mersenns_mult_mod(z3, y1, z1);  // z3=y1*z1
73   multiprecision_lshift_mod(z3, z3);
74 
75   multiprecision_mersenns_squa_mod(y3, y1);  // y3=y1^2
76   multiprecision_lshift_mod(y3, y3);
77   multiprecision_mersenns_mult_mod(t3, y3, x1);  // t3=y3*x1=x1*y1^2
78   multiprecision_lshift_mod(t3, t3);
79   multiprecision_mersenns_squa_mod(y3, y3);  // y3=y3^2=y1^4
80   multiprecision_lshift_mod(y3, y3);
81 
82   multiprecision_mersenns_squa_mod(x3, t2);      // x3=t2^2
83   multiprecision_lshift_mod(t1, t3);             // t1=2t3
84   multiprecision_sub_mod(x3, x3, t1);            // x3=x3-t1
85   multiprecision_sub_mod(t1, t3, x3);            // t1=t3-x3
86   multiprecision_mersenns_mult_mod(t1, t1, t2);  // t1=t1*t2
87   multiprecision_sub_mod(y3, t1, y3);            // y3=t1-y3
88 }
89 
90 // q=q+p,     zp must be 1
ECC_Add(Point * r,Point * p,Point * q)91 static void ECC_Add(Point* r, Point* p, Point* q) {
92   uint32_t t1[KEY_LENGTH_DWORDS_P256];
93   uint32_t t2[KEY_LENGTH_DWORDS_P256];
94   uint32_t* x1;
95   uint32_t* x2;
96   uint32_t* x3;
97   uint32_t* y1;
98   uint32_t* y2;
99   uint32_t* y3;
100   uint32_t* z1;
101   uint32_t* z2;
102   uint32_t* z3;
103 
104   x1 = p->x;
105   y1 = p->y;
106   z1 = p->z;
107   x2 = q->x;
108   y2 = q->y;
109   z2 = q->z;
110   x3 = r->x;
111   y3 = r->y;
112   z3 = r->z;
113 
114   // if Q=infinity, return p
115   if (multiprecision_iszero(z2)) {
116     p_256_copy_point(r, p);
117     return;
118   }
119 
120   // if P=infinity, return q
121   if (multiprecision_iszero(z1)) {
122     p_256_copy_point(r, q);
123     return;
124   }
125 
126   multiprecision_mersenns_squa_mod(t1, z1);      // t1=z1^2
127   multiprecision_mersenns_mult_mod(t2, z1, t1);  // t2=t1*z1
128   multiprecision_mersenns_mult_mod(t1, x2, t1);  // t1=t1*x2
129   multiprecision_mersenns_mult_mod(t2, y2, t2);  // t2=t2*y2
130 
131   multiprecision_sub_mod(t1, t1, x1);  // t1=t1-x1
132   multiprecision_sub_mod(t2, t2, y1);  // t2=t2-y1
133 
134   if (multiprecision_iszero(t1)) {
135     if (multiprecision_iszero(t2)) {
136       ECC_Double(r, q);
137       return;
138     } else {
139       multiprecision_init(z3);
140       return;  // return infinity
141     }
142   }
143 
144   multiprecision_mersenns_mult_mod(z3, z1, t1);  // z3=z1*t1
145   multiprecision_mersenns_squa_mod(y3, t1);      // t3=t1^2
146   multiprecision_mersenns_mult_mod(z1, y3, t1);  // t4=t3*t1
147   multiprecision_mersenns_mult_mod(y3, y3, x1);  // t3=t3*x1
148   multiprecision_lshift_mod(t1, y3);             // t1=2*t3
149   multiprecision_mersenns_squa_mod(x3, t2);      // x3=t2^2
150   multiprecision_sub_mod(x3, x3, t1);            // x3=x3-t1
151   multiprecision_sub_mod(x3, x3, z1);            // x3=x3-t4
152   multiprecision_sub_mod(y3, y3, x3);            // t3=t3-x3
153   multiprecision_mersenns_mult_mod(y3, y3, t2);  // t3=t3*t2
154   multiprecision_mersenns_mult_mod(z1, z1, y1);  // t4=t4*t1
155   multiprecision_sub_mod(y3, y3, z1);
156 }
157 
158 // Computing the Non-Adjacent Form of a positive integer
ECC_NAF(uint8_t * naf,uint32_t * NumNAF,uint32_t * k)159 static void ECC_NAF(uint8_t* naf, uint32_t* NumNAF, uint32_t* k) {
160   uint32_t sign;
161   int i = 0;
162   int j;
163   uint32_t var;
164 
165   while ((var = multiprecision_most_signbits(k)) >= 1) {
166     if (k[0] & 0x01)  // k is odd
167     {
168       sign = (k[0] & 0x03);  // 1 or 3
169 
170       // k = k-naf[i]
171       if (sign == 1)
172         k[0] = k[0] & 0xFFFFFFFE;
173       else {
174         k[0] = k[0] + 1;
175         if (k[0] == 0)  // overflow
176         {
177           j = 1;
178           do {
179             k[j]++;
180           } while (k[j++] == 0);  // overflow
181         }
182       }
183     } else
184       sign = 0;
185 
186     multiprecision_rshift(k, k);
187     naf[i / 4] |= (sign) << ((i % 4) * 2);
188     i++;
189   }
190 
191   *NumNAF = i;
192 }
193 
194 // Binary Non-Adjacent Form for point multiplication
ECC_PointMult_Bin_NAF(Point * q,Point * p,uint32_t * n)195 void ECC_PointMult_Bin_NAF(Point* q, Point* p, uint32_t* n) {
196   uint32_t sign;
197   uint8_t naf[256 / 4 + 1];
198   uint32_t NumNaf;
199   Point minus_p;
200   Point r;
201   uint32_t* modp;
202 
203   modp = curve_p256.p;
204 
205   p_256_init_point(&r);
206   multiprecision_init(p->z);
207   p->z[0] = 1;
208 
209   // initialization
210   p_256_init_point(q);
211 
212   // -p
213   multiprecision_copy(minus_p.x, p->x);
214   multiprecision_sub(minus_p.y, modp, p->y);
215 
216   multiprecision_init(minus_p.z);
217   minus_p.z[0] = 1;
218 
219   // NAF
220   memset(naf, 0, sizeof(naf));
221   ECC_NAF(naf, &NumNaf, n);
222 
223   for (int i = NumNaf - 1; i >= 0; i--) {
224     p_256_copy_point(&r, q);
225     ECC_Double(q, &r);
226     sign = (naf[i / 4] >> ((i % 4) * 2)) & 0x03;
227 
228     if (sign == 1) {
229       p_256_copy_point(&r, q);
230       ECC_Add(q, &r, p);
231     } else if (sign == 3) {
232       p_256_copy_point(&r, q);
233       ECC_Add(q, &r, &minus_p);
234     }
235   }
236 
237   multiprecision_inv_mod(minus_p.x, q->z);
238   multiprecision_mersenns_squa_mod(q->z, minus_p.x);
239   multiprecision_mersenns_mult_mod(q->x, q->x, q->z);
240   multiprecision_mersenns_mult_mod(q->z, q->z, minus_p.x);
241   multiprecision_mersenns_mult_mod(q->y, q->y, q->z);
242 }
243 
ECC_ValidatePoint(const Point & pt)244 bool ECC_ValidatePoint(const Point& pt) {
245   p_256_init_curve();
246 
247   // Ensure y^2 = x^3 + a*x + b (mod p); a = -3
248 
249   // y^2 mod p
250   uint32_t y2_mod[KEY_LENGTH_DWORDS_P256] = {0};
251   multiprecision_mersenns_squa_mod(y2_mod, (uint32_t*)pt.y);
252 
253   // Right hand side calculation
254   uint32_t rhs[KEY_LENGTH_DWORDS_P256] = {0};
255   multiprecision_mersenns_squa_mod(rhs, (uint32_t*)pt.x);
256   uint32_t three[KEY_LENGTH_DWORDS_P256] = {0};
257   three[0] = 3;
258   multiprecision_sub_mod(rhs, rhs, three);
259   multiprecision_mersenns_mult_mod(rhs, rhs, (uint32_t*)pt.x);
260   multiprecision_add_mod(rhs, rhs, curve_p256.b);
261 
262   return multiprecision_compare(rhs, y2_mod) == 0;
263 }
264