aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-04-26 14:33:38 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2021-04-26 14:33:38 +0200
commit074b33bf16b8dc047a94d615c24f40d2ba9ead46 (patch)
tree02cefa26509caeda392b8b33df646ad439d1436a
parent6b69ab68b47d0933f8b4a1d7ed8460274a736a5f (diff)
downloadbusybox-w32-074b33bf16b8dc047a94d615c24f40d2ba9ead46.tar.gz
busybox-w32-074b33bf16b8dc047a94d615c24f40d2ba9ead46.tar.bz2
busybox-w32-074b33bf16b8dc047a94d615c24f40d2ba9ead46.zip
tls: simplify sp_256_ecc_gen_k_10, cosmetic changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/tls.h6
-rw-r--r--networking/tls_sp_c32.c184
2 files changed, 52 insertions, 138 deletions
diff --git a/networking/tls.h b/networking/tls.h
index 154e9b2fb..215e92b02 100644
--- a/networking/tls.h
+++ b/networking/tls.h
@@ -111,9 +111,9 @@ void xorbuf_aligned_AES_BLOCK_SIZE(void* buf, const void* mask) FAST_FUNC;
111#define CURVE25519_KEYSIZE 32 111#define CURVE25519_KEYSIZE 32
112 112
113void curve_x25519_compute_pubkey_and_premaster( 113void curve_x25519_compute_pubkey_and_premaster(
114 uint8_t *pubkey, uint8_t *premaster, 114 uint8_t *pubkey32, uint8_t *premaster32,
115 const uint8_t *peerkey32) FAST_FUNC; 115 const uint8_t *peerkey32) FAST_FUNC;
116 116
117void curve_P256_compute_pubkey_and_premaster( 117void curve_P256_compute_pubkey_and_premaster(
118 uint8_t *pubkey, uint8_t *premaster, 118 uint8_t *pubkey2x32, uint8_t *premaster32,
119 const uint8_t *peerkey32) FAST_FUNC; 119 const uint8_t *peerkey2x32) FAST_FUNC;
diff --git a/networking/tls_sp_c32.c b/networking/tls_sp_c32.c
index e7667de73..b4e14deac 100644
--- a/networking/tls_sp_c32.c
+++ b/networking/tls_sp_c32.c
@@ -57,7 +57,6 @@ typedef int32_t sp_digit;
57 57
58/* Implementation by Sean Parkinson. */ 58/* Implementation by Sean Parkinson. */
59 59
60/* Point structure to use. */
61typedef struct sp_point { 60typedef struct sp_point {
62 sp_digit x[2 * 10]; 61 sp_digit x[2 * 10];
63 sp_digit y[2 * 10]; 62 sp_digit y[2 * 10];
@@ -165,8 +164,6 @@ static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32)
165 164
166/* Compare a with b in constant time. 165/* Compare a with b in constant time.
167 * 166 *
168 * a A single precision integer.
169 * b A single precision integer.
170 * return -ve, 0 or +ve if a is less than, equal to or greater than b 167 * return -ve, 0 or +ve if a is less than, equal to or greater than b
171 * respectively. 168 * respectively.
172 */ 169 */
@@ -181,8 +178,6 @@ static sp_digit sp_256_cmp_10(const sp_digit* a, const sp_digit* b)
181 178
182/* Compare two numbers to determine if they are equal. 179/* Compare two numbers to determine if they are equal.
183 * 180 *
184 * a First number to compare.
185 * b Second number to compare.
186 * return 1 when equal and 0 otherwise. 181 * return 1 when equal and 0 otherwise.
187 */ 182 */
188static int sp_256_cmp_equal_10(const sp_digit* a, const sp_digit* b) 183static int sp_256_cmp_equal_10(const sp_digit* a, const sp_digit* b)
@@ -198,10 +193,7 @@ static int sp_256_cmp_equal_10(const sp_digit* a, const sp_digit* b)
198#endif 193#endif
199} 194}
200 195
201/* Normalize the values in each word to 26. 196/* Normalize the values in each word to 26 bits. */
202 *
203 * a Array of sp_digit to normalize.
204 */
205static void sp_256_norm_10(sp_digit* a) 197static void sp_256_norm_10(sp_digit* a)
206{ 198{
207 int i; 199 int i;
@@ -211,12 +203,7 @@ static void sp_256_norm_10(sp_digit* a)
211 } 203 }
212} 204}
213 205
214/* Add b to a into r. (r = a + b) 206/* Add b to a into r. (r = a + b) */
215 *
216 * r A single precision integer.
217 * a A single precision integer.
218 * b A single precision integer.
219 */
220static void sp_256_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b) 207static void sp_256_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
221{ 208{
222 int i; 209 int i;
@@ -226,11 +213,6 @@ static void sp_256_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
226 213
227/* Conditionally add a and b using the mask m. 214/* Conditionally add a and b using the mask m.
228 * m is -1 to add and 0 when not. 215 * m is -1 to add and 0 when not.
229 *
230 * r A single precision number representing conditional add result.
231 * a A single precision number to add with.
232 * b A single precision number to add.
233 * m Mask value to apply.
234 */ 216 */
235static void sp_256_cond_add_10(sp_digit* r, const sp_digit* a, 217static void sp_256_cond_add_10(sp_digit* r, const sp_digit* a,
236 const sp_digit* b, const sp_digit m) 218 const sp_digit* b, const sp_digit m)
@@ -242,11 +224,6 @@ static void sp_256_cond_add_10(sp_digit* r, const sp_digit* a,
242 224
243/* Conditionally subtract b from a using the mask m. 225/* Conditionally subtract b from a using the mask m.
244 * m is -1 to subtract and 0 when not. 226 * m is -1 to subtract and 0 when not.
245 *
246 * r A single precision number representing condition subtract result.
247 * a A single precision number to subtract from.
248 * b A single precision number to subtract.
249 * m Mask value to apply.
250 */ 227 */
251static void sp_256_cond_sub_10(sp_digit* r, const sp_digit* a, 228static void sp_256_cond_sub_10(sp_digit* r, const sp_digit* a,
252 const sp_digit* b, const sp_digit m) 229 const sp_digit* b, const sp_digit m)
@@ -256,23 +233,7 @@ static void sp_256_cond_sub_10(sp_digit* r, const sp_digit* a,
256 r[i] = a[i] - (b[i] & m); 233 r[i] = a[i] - (b[i] & m);
257} 234}
258 235
259/* Add 1 to a. (a = a + 1) 236/* Shift number left one bit. Bottom bit is lost. */
260 *
261 * r A single precision integer.
262 * a A single precision integer.
263 */
264static void sp_256_add_one_10(sp_digit* a)
265{
266 a[0]++;
267 sp_256_norm_10(a);
268}
269
270/* Shift number left one bit.
271 * Bottom bit is lost.
272 *
273 * r Result of shift.
274 * a Number to shift.
275 */
276static void sp_256_rshift1_10(sp_digit* r, sp_digit* a) 237static void sp_256_rshift1_10(sp_digit* r, sp_digit* a)
277{ 238{
278 int i; 239 int i;
@@ -381,14 +342,8 @@ static void sp_256_mod_mul_norm_10(sp_digit* r, const sp_digit* a)
381 r[9] = (sp_digit)(t[7] >> 10); 342 r[9] = (sp_digit)(t[7] >> 10);
382} 343}
383 344
384/* Mul a by scalar b and add into r. (r += a * b) 345/* Mul a by scalar b and add into r. (r += a * b) */
385 * 346static void sp_256_mul_add_10(sp_digit* r, const sp_digit* a, sp_digit b)
386 * r A single precision integer.
387 * a A single precision integer.
388 * b A scalar.
389 */
390static void sp_256_mul_add_10(sp_digit* r, const sp_digit* a,
391 const sp_digit b)
392{ 347{
393 int64_t tb = b; 348 int64_t tb = b;
394 int64_t t = 0; 349 int64_t t = 0;
@@ -402,12 +357,7 @@ static void sp_256_mul_add_10(sp_digit* r, const sp_digit* a,
402 r[10] += t; 357 r[10] += t;
403} 358}
404 359
405/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) 360/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */
406 *
407 * r Result of division by 2.
408 * a Number to divide.
409 * m Modulus (prime).
410 */
411static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m) 361static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
412{ 362{
413 sp_256_cond_add_10(r, a, m, 0 - (a[0] & 1)); 363 sp_256_cond_add_10(r, a, m, 0 - (a[0] & 1));
@@ -415,11 +365,7 @@ static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
415 sp_256_rshift1_10(r, r); 365 sp_256_rshift1_10(r, r);
416} 366}
417 367
418/* Shift the result in the high 256 bits down to the bottom. 368/* Shift the result in the high 256 bits down to the bottom. */
419 *
420 * r A single precision number.
421 * a A single precision number.
422 */
423static void sp_256_mont_shift_10(sp_digit* r, const sp_digit* a) 369static void sp_256_mont_shift_10(sp_digit* r, const sp_digit* a)
424{ 370{
425 int i; 371 int i;
@@ -438,13 +384,7 @@ static void sp_256_mont_shift_10(sp_digit* r, const sp_digit* a)
438 memset(&r[10], 0, sizeof(*r) * 10); 384 memset(&r[10], 0, sizeof(*r) * 10);
439} 385}
440 386
441/* Add two Montgomery form numbers (r = a + b % m). 387/* Add two Montgomery form numbers (r = a + b % m) */
442 *
443 * r Result of addition.
444 * a First number to add in Montogmery form.
445 * b Second number to add in Montogmery form.
446 * m Modulus (prime).
447 */
448static void sp_256_mont_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b, 388static void sp_256_mont_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
449 const sp_digit* m) 389 const sp_digit* m)
450{ 390{
@@ -454,12 +394,7 @@ static void sp_256_mont_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b
454 sp_256_norm_10(r); 394 sp_256_norm_10(r);
455} 395}
456 396
457/* Double a Montgomery form number (r = a + a % m). 397/* Double a Montgomery form number (r = a + a % m) */
458 *
459 * r Result of doubling.
460 * a Number to double in Montogmery form.
461 * m Modulus (prime).
462 */
463static void sp_256_mont_dbl_10(sp_digit* r, const sp_digit* a, const sp_digit* m) 398static void sp_256_mont_dbl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
464{ 399{
465 sp_256_add_10(r, a, a); 400 sp_256_add_10(r, a, a);
@@ -468,12 +403,7 @@ static void sp_256_mont_dbl_10(sp_digit* r, const sp_digit* a, const sp_digit* m
468 sp_256_norm_10(r); 403 sp_256_norm_10(r);
469} 404}
470 405
471/* Triple a Montgomery form number (r = a + a + a % m). 406/* Triple a Montgomery form number (r = a + a + a % m) */
472 *
473 * r Result of Tripling.
474 * a Number to triple in Montogmery form.
475 * m Modulus (prime).
476 */
477static void sp_256_mont_tpl_10(sp_digit* r, const sp_digit* a, const sp_digit* m) 407static void sp_256_mont_tpl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
478{ 408{
479 sp_256_add_10(r, a, a); 409 sp_256_add_10(r, a, a);
@@ -486,27 +416,15 @@ static void sp_256_mont_tpl_10(sp_digit* r, const sp_digit* a, const sp_digit* m
486 sp_256_norm_10(r); 416 sp_256_norm_10(r);
487} 417}
488 418
489/* Sub b from a into r. (r = a - b) 419/* Sub b from a into r. (r = a - b) */
490 * 420static void sp_256_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
491 * r A single precision integer.
492 * a A single precision integer.
493 * b A single precision integer.
494 */
495static void sp_256_sub_10(sp_digit* r, const sp_digit* a,
496 const sp_digit* b)
497{ 421{
498 int i; 422 int i;
499 for (i = 0; i < 10; i++) 423 for (i = 0; i < 10; i++)
500 r[i] = a[i] - b[i]; 424 r[i] = a[i] - b[i];
501} 425}
502 426
503/* Subtract two Montgomery form numbers (r = a - b % m). 427/* Subtract two Montgomery form numbers (r = a - b % m) */
504 *
505 * r Result of subtration.
506 * a Number to subtract from in Montogmery form.
507 * b Number to subtract with in Montogmery form.
508 * m Modulus (prime).
509 */
510static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b, 428static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
511 const sp_digit* m) 429 const sp_digit* m)
512{ 430{
@@ -554,12 +472,7 @@ static void sp_256_mont_reduce_10(sp_digit* a, const sp_digit* m, sp_digit mp)
554 sp_256_norm_10(a); 472 sp_256_norm_10(a);
555} 473}
556 474
557/* Multiply a and b into r. (r = a * b) 475/* Multiply a and b into r. (r = a * b) */
558 *
559 * r A single precision integer.
560 * a A single precision integer.
561 * b A single precision integer.
562 */
563static void sp_256_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b) 476static void sp_256_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
564{ 477{
565 int i, j, k; 478 int i, j, k;
@@ -600,11 +513,7 @@ static void sp_256_mont_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b
600 sp_256_mont_reduce_10(r, m, mp); 513 sp_256_mont_reduce_10(r, m, mp);
601} 514}
602 515
603/* Square a and put result in r. (r = a * a) 516/* Square a and put result in r. (r = a * a) */
604 *
605 * r A single precision integer.
606 * a A single precision integer.
607 */
608static void sp_256_sqr_10(sp_digit* r, const sp_digit* a) 517static void sp_256_sqr_10(sp_digit* r, const sp_digit* a)
609{ 518{
610 int i, j, k; 519 int i, j, k;
@@ -937,8 +846,8 @@ static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit*
937 else 846 else
938 memcpy(r, t[0], sizeof(sp_point)); 847 memcpy(r, t[0], sizeof(sp_point));
939 848
940 memset(tmp, 0, sizeof(tmp)); 849 memset(tmp, 0, sizeof(tmp)); //paranoia
941 memset(td, 0, sizeof(td)); 850 memset(td, 0, sizeof(td)); //paranoia
942} 851}
943 852
944/* Multiply the base point of P256 by the scalar and return the result. 853/* Multiply the base point of P256 by the scalar and return the result.
@@ -956,20 +865,20 @@ static void sp_256_ecc_mulmod_base_10(sp_point* r, sp_digit* k /*, int map*/)
956 * The number is 0 padded to maximum size on output. 865 * The number is 0 padded to maximum size on output.
957 * 866 *
958 * priv Scalar to multiply the point by. 867 * priv Scalar to multiply the point by.
959 * peerkey2x32 Point to multiply. 868 * pub2x32 Point to multiply.
960 * out Buffer to hold X ordinate. 869 * out32 Buffer to hold X ordinate.
961 */ 870 */
962static void sp_ecc_secret_gen_256(sp_digit priv[10], const uint8_t *peerkey2x32, uint8_t* out32) 871static void sp_ecc_secret_gen_256(sp_digit priv[10], const uint8_t *pub2x32, uint8_t* out32)
963{ 872{
964 sp_point point[1]; 873 sp_point point[1];
965 874
966#if FIXED_PEER_PUBKEY 875#if FIXED_PEER_PUBKEY
967 memset((void*)peerkey32, 0x55, 64); 876 memset((void*)pub2x32, 0x55, 64);
968#endif 877#endif
969 dump_hex("peerkey32 %s\n", peerkey2x32, 32); 878 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
970 dump_hex(" %s\n", peerkey2x32 + 32, 32); 879 dump_hex(" %s\n", pub2x32 + 32, 32);
971 880
972 sp_256_point_from_bin2x32(point, peerkey2x32); 881 sp_256_point_from_bin2x32(point, pub2x32);
973 dump_hex("point->x %s\n", point->x, sizeof(point->x)); 882 dump_hex("point->x %s\n", point->x, sizeof(point->x));
974 dump_hex("point->y %s\n", point->y, sizeof(point->y)); 883 dump_hex("point->y %s\n", point->y, sizeof(point->y));
975 884
@@ -979,14 +888,18 @@ static void sp_ecc_secret_gen_256(sp_digit priv[10], const uint8_t *peerkey2x32,
979 dump_hex("out32: %s\n", out32, 32); 888 dump_hex("out32: %s\n", out32, 32);
980} 889}
981 890
982/* Generates a scalar that is in the range 1..order-1. 891/* Generates a scalar that is in the range 1..order-1. */
983 * 892#define SIMPLIFY 1
984 * rng Random number generator. 893/* Add 1 to a. (a = a + 1) */
985 * k Scalar value. 894#if !SIMPLIFY
986 */ 895static void sp_256_add_one_10(sp_digit* a)
896{
897 a[0]++;
898 sp_256_norm_10(a);
899}
900#endif
987static void sp_256_ecc_gen_k_10(sp_digit k[10]) 901static void sp_256_ecc_gen_k_10(sp_digit k[10])
988{ 902{
989#define SIMPLIFY 1
990#if !SIMPLIFY 903#if !SIMPLIFY
991 /* The order of the curve P256 minus 2. */ 904 /* The order of the curve P256 minus 2. */
992 static const sp_digit p256_order2[10] = { 905 static const sp_digit p256_order2[10] = {
@@ -1007,7 +920,7 @@ static void sp_256_ecc_gen_k_10(sp_digit k[10])
1007 break; 920 break;
1008#else 921#else
1009 /* non-loopy version (and not needing p256_order2[]): 922 /* non-loopy version (and not needing p256_order2[]):
1010 * if most-significant word seems that it can be larger 923 * if most-significant word seems that k can be larger
1011 * than p256_order2, fix it up: 924 * than p256_order2, fix it up:
1012 */ 925 */
1013 if (k[9] >= 0x03fffff) 926 if (k[9] >= 0x03fffff)
@@ -1015,21 +928,22 @@ static void sp_256_ecc_gen_k_10(sp_digit k[10])
1015 break; 928 break;
1016#endif 929#endif
1017 } 930 }
931#if !SIMPLIFY
1018 sp_256_add_one_10(k); 932 sp_256_add_one_10(k);
933#else
934 if (k[0] == 0)
935 k[0] = 1;
936#endif
1019#undef SIMPLIFY 937#undef SIMPLIFY
1020} 938}
1021 939
1022/* Makes a random EC key pair. 940/* Makes a random EC key pair. */
1023 * 941static void sp_ecc_make_key_256(sp_digit privkey[10], uint8_t *pubkey)
1024 * priv Generated private value.
1025 * pubkey Generated public point.
1026 */
1027static void sp_ecc_make_key_256(sp_digit k[10], uint8_t *pubkey)
1028{ 942{
1029 sp_point point[1]; 943 sp_point point[1];
1030 944
1031 sp_256_ecc_gen_k_10(k); 945 sp_256_ecc_gen_k_10(privkey);
1032 sp_256_ecc_mulmod_base_10(point, k); 946 sp_256_ecc_mulmod_base_10(point, privkey);
1033 sp_256_to_bin(point->x, pubkey); 947 sp_256_to_bin(point->x, pubkey);
1034 sp_256_to_bin(point->y, pubkey + 32); 948 sp_256_to_bin(point->y, pubkey + 32);
1035 949
@@ -1037,16 +951,16 @@ static void sp_ecc_make_key_256(sp_digit k[10], uint8_t *pubkey)
1037} 951}
1038 952
1039void FAST_FUNC curve_P256_compute_pubkey_and_premaster( 953void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
1040 uint8_t *pubkey, uint8_t *premaster32, 954 uint8_t *pubkey2x32, uint8_t *premaster32,
1041 const uint8_t *peerkey2x32) 955 const uint8_t *peerkey2x32)
1042{ 956{
1043 sp_digit privkey[10]; 957 sp_digit privkey[10];
1044 958
1045 sp_ecc_make_key_256(privkey, pubkey); 959 sp_ecc_make_key_256(privkey, pubkey2x32);
1046 dump_hex("pubkey: %s\n", pubkey, 32); 960 dump_hex("pubkey: %s\n", pubkey2x32, 32);
1047 dump_hex(" %s\n", pubkey + 32, 32); 961 dump_hex(" %s\n", pubkey2x32 + 32, 32);
1048 962
1049 /* Combine our privkey and peerkey32 to generate premaster */ 963 /* Combine our privkey and peer's public key to generate premaster */
1050 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32); 964 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
1051 dump_hex("premaster: %s\n", premaster32, 32); 965 dump_hex("premaster: %s\n", premaster32, 32);
1052} 966}