p_256_ecc_pp.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. * This file contains simple pairing algorithms using Elliptic Curve
  21. * Cryptography for private public key
  22. *
  23. ******************************************************************************/
  24. #include "p_256_ecc_pp.h"
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "p_256_multprecision.h"
  29. elliptic_curve_t curve;
  30. elliptic_curve_t curve_p256;
  31. static void p_256_init_point(Point* q) { memset(q, 0, sizeof(Point)); }
  32. static void p_256_copy_point(Point* q, Point* p) {
  33. memcpy(q, p, sizeof(Point));
  34. }
  35. // q=2q
  36. static void ECC_Double(Point* q, Point* p, uint32_t keyLength) {
  37. uint32_t t1[KEY_LENGTH_DWORDS_P256];
  38. uint32_t t2[KEY_LENGTH_DWORDS_P256];
  39. uint32_t t3[KEY_LENGTH_DWORDS_P256];
  40. uint32_t* x1;
  41. uint32_t* x3;
  42. uint32_t* y1;
  43. uint32_t* y3;
  44. uint32_t* z1;
  45. uint32_t* z3;
  46. if (multiprecision_iszero(p->z, keyLength)) {
  47. multiprecision_init(q->z, keyLength);
  48. return; // return infinity
  49. }
  50. x1 = p->x;
  51. y1 = p->y;
  52. z1 = p->z;
  53. x3 = q->x;
  54. y3 = q->y;
  55. z3 = q->z;
  56. multiprecision_mersenns_squa_mod(t1, z1, keyLength); // t1=z1^2
  57. multiprecision_sub_mod(t2, x1, t1, keyLength); // t2=x1-t1
  58. multiprecision_add_mod(t1, x1, t1, keyLength); // t1=x1+t1
  59. multiprecision_mersenns_mult_mod(t2, t1, t2, keyLength); // t2=t2*t1
  60. multiprecision_lshift_mod(t3, t2, keyLength);
  61. multiprecision_add_mod(t2, t3, t2, keyLength); // t2=3t2
  62. multiprecision_mersenns_mult_mod(z3, y1, z1, keyLength); // z3=y1*z1
  63. multiprecision_lshift_mod(z3, z3, keyLength);
  64. multiprecision_mersenns_squa_mod(y3, y1, keyLength); // y3=y1^2
  65. multiprecision_lshift_mod(y3, y3, keyLength);
  66. multiprecision_mersenns_mult_mod(t3, y3, x1, keyLength); // t3=y3*x1=x1*y1^2
  67. multiprecision_lshift_mod(t3, t3, keyLength);
  68. multiprecision_mersenns_squa_mod(y3, y3, keyLength); // y3=y3^2=y1^4
  69. multiprecision_lshift_mod(y3, y3, keyLength);
  70. multiprecision_mersenns_squa_mod(x3, t2, keyLength); // x3=t2^2
  71. multiprecision_lshift_mod(t1, t3, keyLength); // t1=2t3
  72. multiprecision_sub_mod(x3, x3, t1, keyLength); // x3=x3-t1
  73. multiprecision_sub_mod(t1, t3, x3, keyLength); // t1=t3-x3
  74. multiprecision_mersenns_mult_mod(t1, t1, t2, keyLength); // t1=t1*t2
  75. multiprecision_sub_mod(y3, t1, y3, keyLength); // y3=t1-y3
  76. }
  77. // q=q+p, zp must be 1
  78. static void ECC_Add(Point* r, Point* p, Point* q, uint32_t keyLength) {
  79. uint32_t t1[KEY_LENGTH_DWORDS_P256];
  80. uint32_t t2[KEY_LENGTH_DWORDS_P256];
  81. uint32_t* x1;
  82. uint32_t* x2;
  83. uint32_t* x3;
  84. uint32_t* y1;
  85. uint32_t* y2;
  86. uint32_t* y3;
  87. uint32_t* z1;
  88. uint32_t* z2;
  89. uint32_t* z3;
  90. x1 = p->x;
  91. y1 = p->y;
  92. z1 = p->z;
  93. x2 = q->x;
  94. y2 = q->y;
  95. z2 = q->z;
  96. x3 = r->x;
  97. y3 = r->y;
  98. z3 = r->z;
  99. // if Q=infinity, return p
  100. if (multiprecision_iszero(z2, keyLength)) {
  101. p_256_copy_point(r, p);
  102. return;
  103. }
  104. // if P=infinity, return q
  105. if (multiprecision_iszero(z1, keyLength)) {
  106. p_256_copy_point(r, q);
  107. return;
  108. }
  109. multiprecision_mersenns_squa_mod(t1, z1, keyLength); // t1=z1^2
  110. multiprecision_mersenns_mult_mod(t2, z1, t1, keyLength); // t2=t1*z1
  111. multiprecision_mersenns_mult_mod(t1, x2, t1, keyLength); // t1=t1*x2
  112. multiprecision_mersenns_mult_mod(t2, y2, t2, keyLength); // t2=t2*y2
  113. multiprecision_sub_mod(t1, t1, x1, keyLength); // t1=t1-x1
  114. multiprecision_sub_mod(t2, t2, y1, keyLength); // t2=t2-y1
  115. if (multiprecision_iszero(t1, keyLength)) {
  116. if (multiprecision_iszero(t2, keyLength)) {
  117. ECC_Double(r, q, keyLength);
  118. return;
  119. } else {
  120. multiprecision_init(z3, keyLength);
  121. return; // return infinity
  122. }
  123. }
  124. multiprecision_mersenns_mult_mod(z3, z1, t1, keyLength); // z3=z1*t1
  125. multiprecision_mersenns_squa_mod(y3, t1, keyLength); // t3=t1^2
  126. multiprecision_mersenns_mult_mod(z1, y3, t1, keyLength); // t4=t3*t1
  127. multiprecision_mersenns_mult_mod(y3, y3, x1, keyLength); // t3=t3*x1
  128. multiprecision_lshift_mod(t1, y3, keyLength); // t1=2*t3
  129. multiprecision_mersenns_squa_mod(x3, t2, keyLength); // x3=t2^2
  130. multiprecision_sub_mod(x3, x3, t1, keyLength); // x3=x3-t1
  131. multiprecision_sub_mod(x3, x3, z1, keyLength); // x3=x3-t4
  132. multiprecision_sub_mod(y3, y3, x3, keyLength); // t3=t3-x3
  133. multiprecision_mersenns_mult_mod(y3, y3, t2, keyLength); // t3=t3*t2
  134. multiprecision_mersenns_mult_mod(z1, z1, y1, keyLength); // t4=t4*t1
  135. multiprecision_sub_mod(y3, y3, z1, keyLength);
  136. }
  137. // Computing the Non-Adjacent Form of a positive integer
  138. static void ECC_NAF(uint8_t* naf, uint32_t* NumNAF, uint32_t* k,
  139. uint32_t keyLength) {
  140. uint32_t sign;
  141. int i = 0;
  142. int j;
  143. uint32_t var;
  144. while ((var = multiprecision_most_signbits(k, keyLength)) >= 1) {
  145. if (k[0] & 0x01) // k is odd
  146. {
  147. sign = (k[0] & 0x03); // 1 or 3
  148. // k = k-naf[i]
  149. if (sign == 1)
  150. k[0] = k[0] & 0xFFFFFFFE;
  151. else {
  152. k[0] = k[0] + 1;
  153. if (k[0] == 0) // overflow
  154. {
  155. j = 1;
  156. do {
  157. k[j]++;
  158. } while (k[j++] == 0); // overflow
  159. }
  160. }
  161. } else
  162. sign = 0;
  163. multiprecision_rshift(k, k, keyLength);
  164. naf[i / 4] |= (sign) << ((i % 4) * 2);
  165. i++;
  166. }
  167. *NumNAF = i;
  168. }
  169. // Binary Non-Adjacent Form for point multiplication
  170. void ECC_PointMult_Bin_NAF(Point* q, Point* p, uint32_t* n,
  171. uint32_t keyLength) {
  172. uint32_t sign;
  173. uint8_t naf[256 / 4 + 1];
  174. uint32_t NumNaf;
  175. Point minus_p;
  176. Point r;
  177. uint32_t* modp;
  178. if (keyLength == KEY_LENGTH_DWORDS_P256) {
  179. modp = curve_p256.p;
  180. } else {
  181. modp = curve.p;
  182. }
  183. p_256_init_point(&r);
  184. multiprecision_init(p->z, keyLength);
  185. p->z[0] = 1;
  186. // initialization
  187. p_256_init_point(q);
  188. // -p
  189. multiprecision_copy(minus_p.x, p->x, keyLength);
  190. multiprecision_sub(minus_p.y, modp, p->y, keyLength);
  191. multiprecision_init(minus_p.z, keyLength);
  192. minus_p.z[0] = 1;
  193. // NAF
  194. memset(naf, 0, sizeof(naf));
  195. ECC_NAF(naf, &NumNaf, n, keyLength);
  196. for (int i = NumNaf - 1; i >= 0; i--) {
  197. p_256_copy_point(&r, q);
  198. ECC_Double(q, &r, keyLength);
  199. sign = (naf[i / 4] >> ((i % 4) * 2)) & 0x03;
  200. if (sign == 1) {
  201. p_256_copy_point(&r, q);
  202. ECC_Add(q, &r, p, keyLength);
  203. } else if (sign == 3) {
  204. p_256_copy_point(&r, q);
  205. ECC_Add(q, &r, &minus_p, keyLength);
  206. }
  207. }
  208. multiprecision_inv_mod(minus_p.x, q->z, keyLength);
  209. multiprecision_mersenns_squa_mod(q->z, minus_p.x, keyLength);
  210. multiprecision_mersenns_mult_mod(q->x, q->x, q->z, keyLength);
  211. multiprecision_mersenns_mult_mod(q->z, q->z, minus_p.x, keyLength);
  212. multiprecision_mersenns_mult_mod(q->y, q->y, q->z, keyLength);
  213. }
  214. bool ECC_ValidatePoint(const Point& pt) {
  215. const size_t kl = KEY_LENGTH_DWORDS_P256;
  216. p_256_init_curve(kl);
  217. // Ensure y^2 = x^3 + a*x + b (mod p); a = -3
  218. // y^2 mod p
  219. uint32_t y2_mod[kl] = {0};
  220. multiprecision_mersenns_squa_mod(y2_mod, (uint32_t*)pt.y, kl);
  221. // Right hand side calculation
  222. uint32_t rhs[kl] = {0};
  223. multiprecision_mersenns_squa_mod(rhs, (uint32_t*)pt.x, kl);
  224. uint32_t three[kl] = {0};
  225. three[0] = 3;
  226. multiprecision_sub_mod(rhs, rhs, three, kl);
  227. multiprecision_mersenns_mult_mod(rhs, rhs, (uint32_t*)pt.x, kl);
  228. multiprecision_add_mod(rhs, rhs, curve_p256.b, kl);
  229. return multiprecision_compare(rhs, y2_mod, kl) == 0;
  230. }