summaryrefslogtreecommitdiff
path: root/networking/tls_sp_c32.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-04-26 20:24:34 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2021-04-26 20:24:34 +0200
commit120401249a37a77cd2d4c71ad20a9a194bfea409 (patch)
tree45f84378f495d53f6c160f33f117d333c9e2ee58 /networking/tls_sp_c32.c
parent93b886f54bf85ca38f372ddd521f7c5bdbdc5d08 (diff)
downloadbusybox-120401249a37a77cd2d4c71ad20a9a194bfea409.tar.gz
tls: fix whitespace in P256 code
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/tls_sp_c32.c')
-rw-r--r--networking/tls_sp_c32.c1002
1 files changed, 501 insertions, 501 deletions
diff --git a/networking/tls_sp_c32.c b/networking/tls_sp_c32.c
index 97b2d3de9..8527e7864 100644
--- a/networking/tls_sp_c32.c
+++ b/networking/tls_sp_c32.c
@@ -92,30 +92,30 @@ static const sp_point p256_base = {
*/
static void sp_256_to_bin(sp_digit* r, uint8_t* a)
{
- int i, j, s = 0, b;
-
- for (i = 0; i < 9; i++) {
- r[i+1] += r[i] >> 26;
- r[i] &= 0x3ffffff;
- }
- j = 256 / 8 - 1;
- a[j] = 0;
- for (i=0; i<10 && j>=0; i++) {
- b = 0;
- a[j--] |= r[i] << s; b += 8 - s;
- if (j < 0)
- break;
- while (b < 26) {
- a[j--] = r[i] >> b; b += 8;
- if (j < 0)
- break;
- }
- s = 8 - (b - 26);
- if (j >= 0)
- a[j] = 0;
- if (s != 0)
- j++;
- }
+ int i, j, s = 0, b;
+
+ for (i = 0; i < 9; i++) {
+ r[i+1] += r[i] >> 26;
+ r[i] &= 0x3ffffff;
+ }
+ j = 256 / 8 - 1;
+ a[j] = 0;
+ for (i = 0; i < 10 && j >= 0; i++) {
+ b = 0;
+ a[j--] |= r[i] << s; b += 8 - s;
+ if (j < 0)
+ break;
+ while (b < 26) {
+ a[j--] = r[i] >> b; b += 8;
+ if (j < 0)
+ break;
+ }
+ s = 8 - (b - 26);
+ if (j >= 0)
+ a[j] = 0;
+ if (s != 0)
+ j++;
+ }
}
/* Read big endian unsigned byte aray into r.
@@ -126,37 +126,37 @@ static void sp_256_to_bin(sp_digit* r, uint8_t* a)
*/
static void sp_256_from_bin(sp_digit* r, int max, const uint8_t* a, int n)
{
- int i, j = 0, s = 0;
-
- r[0] = 0;
- for (i = n-1; i >= 0; i--) {
- r[j] |= ((sp_digit)a[i]) << s;
- if (s >= 18) {
- r[j] &= 0x3ffffff;
- s = 26 - s;
- if (j + 1 >= max)
- break;
- r[++j] = a[i] >> s;
- s = 8 - s;
- }
- else
- s += 8;
- }
+ int i, j = 0, s = 0;
+
+ r[0] = 0;
+ for (i = n-1; i >= 0; i--) {
+ r[j] |= ((sp_digit)a[i]) << s;
+ if (s >= 18) {
+ r[j] &= 0x3ffffff;
+ s = 26 - s;
+ if (j + 1 >= max)
+ break;
+ r[++j] = a[i] >> s;
+ s = 8 - s;
+ }
+ else
+ s += 8;
+ }
- for (j++; j < max; j++)
- r[j] = 0;
+ for (j++; j < max; j++)
+ r[j] = 0;
}
/* Convert a point of big-endian 32-byte x,y pair to type sp_point. */
static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32)
{
- memset(p, 0, sizeof(*p));
- /*p->infinity = 0;*/
- sp_256_from_bin(p->x, 2 * 10, bin2x32, 32);
- sp_256_from_bin(p->y, 2 * 10, bin2x32 + 32, 32);
- //static const uint8_t one[1] = { 1 };
- //sp_256_from_bin(p->z, 2 * 10, one, 1);
- p->z[0] = 1;
+ memset(p, 0, sizeof(*p));
+ /*p->infinity = 0;*/
+ sp_256_from_bin(p->x, 2 * 10, bin2x32, 32);
+ sp_256_from_bin(p->y, 2 * 10, bin2x32 + 32, 32);
+ //static const uint8_t one[1] = { 1 };
+ //sp_256_from_bin(p->z, 2 * 10, one, 1);
+ p->z[0] = 1;
}
/* Compare a with b.
@@ -166,14 +166,14 @@ static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32)
*/
static sp_digit sp_256_cmp_10(const sp_digit* a, const sp_digit* b)
{
- sp_digit r;
- int i;
- for (i = 9; i >= 0; i--) {
- r = a[i] - b[i];
- if (r != 0)
- break;
- }
- return r;
+ sp_digit r;
+ int i;
+ for (i = 9; i >= 0; i--) {
+ r = a[i] - b[i];
+ if (r != 0)
+ break;
+ }
+ return r;
}
/* Compare two numbers to determine if they are equal.
@@ -182,56 +182,56 @@ static sp_digit sp_256_cmp_10(const sp_digit* a, const sp_digit* b)
*/
static int sp_256_cmp_equal_10(const sp_digit* a, const sp_digit* b)
{
- return sp_256_cmp_10(a, b) == 0;
+ return sp_256_cmp_10(a, b) == 0;
}
/* Normalize the values in each word to 26 bits. */
static void sp_256_norm_10(sp_digit* a)
{
- int i;
- for (i = 0; i < 9; i++) {
- a[i+1] += a[i] >> 26;
- a[i] &= 0x3ffffff;
- }
+ int i;
+ for (i = 0; i < 9; i++) {
+ a[i+1] += a[i] >> 26;
+ a[i] &= 0x3ffffff;
+ }
}
/* Add b to a into r. (r = a + b) */
static void sp_256_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
{
- int i;
- for (i = 0; i < 10; i++)
- r[i] = a[i] + b[i];
+ int i;
+ for (i = 0; i < 10; i++)
+ r[i] = a[i] + b[i];
}
/* Conditionally add a and b using the mask m.
* m is -1 to add and 0 when not.
*/
static void sp_256_cond_add_10(sp_digit* r, const sp_digit* a,
- const sp_digit* b, const sp_digit m)
+ const sp_digit* b, const sp_digit m)
{
- int i;
- for (i = 0; i < 10; i++)
- r[i] = a[i] + (b[i] & m);
+ int i;
+ for (i = 0; i < 10; i++)
+ r[i] = a[i] + (b[i] & m);
}
/* Conditionally subtract b from a using the mask m.
* m is -1 to subtract and 0 when not.
*/
static void sp_256_cond_sub_10(sp_digit* r, const sp_digit* a,
- const sp_digit* b, const sp_digit m)
+ const sp_digit* b, const sp_digit m)
{
- int i;
- for (i = 0; i < 10; i++)
- r[i] = a[i] - (b[i] & m);
+ int i;
+ for (i = 0; i < 10; i++)
+ r[i] = a[i] - (b[i] & m);
}
/* Shift number left one bit. Bottom bit is lost. */
static void sp_256_rshift1_10(sp_digit* r, sp_digit* a)
{
- int i;
- for (i = 0; i < 9; i++)
- r[i] = ((a[i] >> 1) | (a[i + 1] << 25)) & 0x3ffffff;
- r[9] = a[9] >> 1;
+ int i;
+ for (i = 0; i < 9; i++)
+ r[i] = ((a[i] >> 1) | (a[i + 1] << 25)) & 0x3ffffff;
+ r[9] = a[9] >> 1;
}
/* Multiply a number by Montogmery normalizer mod modulus (prime).
@@ -241,188 +241,188 @@ static void sp_256_rshift1_10(sp_digit* r, sp_digit* a)
*/
static void sp_256_mod_mul_norm_10(sp_digit* r, const sp_digit* a)
{
- int64_t t[8];
- int64_t a32[8];
- int64_t o;
-
- a32[0] = a[0];
- a32[0] |= a[1] << 26;
- a32[0] &= 0xffffffff;
- a32[1] = (sp_digit)(a[1] >> 6);
- a32[1] |= a[2] << 20;
- a32[1] &= 0xffffffff;
- a32[2] = (sp_digit)(a[2] >> 12);
- a32[2] |= a[3] << 14;
- a32[2] &= 0xffffffff;
- a32[3] = (sp_digit)(a[3] >> 18);
- a32[3] |= a[4] << 8;
- a32[3] &= 0xffffffff;
- a32[4] = (sp_digit)(a[4] >> 24);
- a32[4] |= a[5] << 2;
- a32[4] |= a[6] << 28;
- a32[4] &= 0xffffffff;
- a32[5] = (sp_digit)(a[6] >> 4);
- a32[5] |= a[7] << 22;
- a32[5] &= 0xffffffff;
- a32[6] = (sp_digit)(a[7] >> 10);
- a32[6] |= a[8] << 16;
- a32[6] &= 0xffffffff;
- a32[7] = (sp_digit)(a[8] >> 16);
- a32[7] |= a[9] << 10;
- a32[7] &= 0xffffffff;
-
- /* 1 1 0 -1 -1 -1 -1 0 */
- t[0] = 0 + a32[0] + a32[1] - a32[3] - a32[4] - a32[5] - a32[6];
- /* 0 1 1 0 -1 -1 -1 -1 */
- t[1] = 0 + a32[1] + a32[2] - a32[4] - a32[5] - a32[6] - a32[7];
- /* 0 0 1 1 0 -1 -1 -1 */
- t[2] = 0 + a32[2] + a32[3] - a32[5] - a32[6] - a32[7];
- /* -1 -1 0 2 2 1 0 -1 */
- t[3] = 0 - a32[0] - a32[1] + 2 * a32[3] + 2 * a32[4] + a32[5] - a32[7];
- /* 0 -1 -1 0 2 2 1 0 */
- t[4] = 0 - a32[1] - a32[2] + 2 * a32[4] + 2 * a32[5] + a32[6];
- /* 0 0 -1 -1 0 2 2 1 */
- t[5] = 0 - a32[2] - a32[3] + 2 * a32[5] + 2 * a32[6] + a32[7];
- /* -1 -1 0 0 0 1 3 2 */
- t[6] = 0 - a32[0] - a32[1] + a32[5] + 3 * a32[6] + 2 * a32[7];
- /* 1 0 -1 -1 -1 -1 0 3 */
- t[7] = 0 + a32[0] - a32[2] - a32[3] - a32[4] - a32[5] + 3 * a32[7];
-
- t[1] += t[0] >> 32; t[0] &= 0xffffffff;
- t[2] += t[1] >> 32; t[1] &= 0xffffffff;
- t[3] += t[2] >> 32; t[2] &= 0xffffffff;
- t[4] += t[3] >> 32; t[3] &= 0xffffffff;
- t[5] += t[4] >> 32; t[4] &= 0xffffffff;
- t[6] += t[5] >> 32; t[5] &= 0xffffffff;
- t[7] += t[6] >> 32; t[6] &= 0xffffffff;
- o = t[7] >> 32; t[7] &= 0xffffffff;
- t[0] += o;
- t[3] -= o;
- t[6] -= o;
- t[7] += o;
- t[1] += t[0] >> 32; t[0] &= 0xffffffff;
- t[2] += t[1] >> 32; t[1] &= 0xffffffff;
- t[3] += t[2] >> 32; t[2] &= 0xffffffff;
- t[4] += t[3] >> 32; t[3] &= 0xffffffff;
- t[5] += t[4] >> 32; t[4] &= 0xffffffff;
- t[6] += t[5] >> 32; t[5] &= 0xffffffff;
- t[7] += t[6] >> 32; t[6] &= 0xffffffff;
-
- r[0] = (sp_digit)(t[0]) & 0x3ffffff;
- r[1] = (sp_digit)(t[0] >> 26);
- r[1] |= t[1] << 6;
- r[1] &= 0x3ffffff;
- r[2] = (sp_digit)(t[1] >> 20);
- r[2] |= t[2] << 12;
- r[2] &= 0x3ffffff;
- r[3] = (sp_digit)(t[2] >> 14);
- r[3] |= t[3] << 18;
- r[3] &= 0x3ffffff;
- r[4] = (sp_digit)(t[3] >> 8);
- r[4] |= t[4] << 24;
- r[4] &= 0x3ffffff;
- r[5] = (sp_digit)(t[4] >> 2) & 0x3ffffff;
- r[6] = (sp_digit)(t[4] >> 28);
- r[6] |= t[5] << 4;
- r[6] &= 0x3ffffff;
- r[7] = (sp_digit)(t[5] >> 22);
- r[7] |= t[6] << 10;
- r[7] &= 0x3ffffff;
- r[8] = (sp_digit)(t[6] >> 16);
- r[8] |= t[7] << 16;
- r[8] &= 0x3ffffff;
- r[9] = (sp_digit)(t[7] >> 10);
+ int64_t t[8];
+ int64_t a32[8];
+ int64_t o;
+
+ a32[0] = a[0];
+ a32[0] |= a[1] << 26;
+ a32[0] &= 0xffffffff;
+ a32[1] = (sp_digit)(a[1] >> 6);
+ a32[1] |= a[2] << 20;
+ a32[1] &= 0xffffffff;
+ a32[2] = (sp_digit)(a[2] >> 12);
+ a32[2] |= a[3] << 14;
+ a32[2] &= 0xffffffff;
+ a32[3] = (sp_digit)(a[3] >> 18);
+ a32[3] |= a[4] << 8;
+ a32[3] &= 0xffffffff;
+ a32[4] = (sp_digit)(a[4] >> 24);
+ a32[4] |= a[5] << 2;
+ a32[4] |= a[6] << 28;
+ a32[4] &= 0xffffffff;
+ a32[5] = (sp_digit)(a[6] >> 4);
+ a32[5] |= a[7] << 22;
+ a32[5] &= 0xffffffff;
+ a32[6] = (sp_digit)(a[7] >> 10);
+ a32[6] |= a[8] << 16;
+ a32[6] &= 0xffffffff;
+ a32[7] = (sp_digit)(a[8] >> 16);
+ a32[7] |= a[9] << 10;
+ a32[7] &= 0xffffffff;
+
+ /* 1 1 0 -1 -1 -1 -1 0 */
+ t[0] = 0 + a32[0] + a32[1] - a32[3] - a32[4] - a32[5] - a32[6];
+ /* 0 1 1 0 -1 -1 -1 -1 */
+ t[1] = 0 + a32[1] + a32[2] - a32[4] - a32[5] - a32[6] - a32[7];
+ /* 0 0 1 1 0 -1 -1 -1 */
+ t[2] = 0 + a32[2] + a32[3] - a32[5] - a32[6] - a32[7];
+ /* -1 -1 0 2 2 1 0 -1 */
+ t[3] = 0 - a32[0] - a32[1] + 2 * a32[3] + 2 * a32[4] + a32[5] - a32[7];
+ /* 0 -1 -1 0 2 2 1 0 */
+ t[4] = 0 - a32[1] - a32[2] + 2 * a32[4] + 2 * a32[5] + a32[6];
+ /* 0 0 -1 -1 0 2 2 1 */
+ t[5] = 0 - a32[2] - a32[3] + 2 * a32[5] + 2 * a32[6] + a32[7];
+ /* -1 -1 0 0 0 1 3 2 */
+ t[6] = 0 - a32[0] - a32[1] + a32[5] + 3 * a32[6] + 2 * a32[7];
+ /* 1 0 -1 -1 -1 -1 0 3 */
+ t[7] = 0 + a32[0] - a32[2] - a32[3] - a32[4] - a32[5] + 3 * a32[7];
+
+ t[1] += t[0] >> 32; t[0] &= 0xffffffff;
+ t[2] += t[1] >> 32; t[1] &= 0xffffffff;
+ t[3] += t[2] >> 32; t[2] &= 0xffffffff;
+ t[4] += t[3] >> 32; t[3] &= 0xffffffff;
+ t[5] += t[4] >> 32; t[4] &= 0xffffffff;
+ t[6] += t[5] >> 32; t[5] &= 0xffffffff;
+ t[7] += t[6] >> 32; t[6] &= 0xffffffff;
+ o = t[7] >> 32; t[7] &= 0xffffffff;
+ t[0] += o;
+ t[3] -= o;
+ t[6] -= o;
+ t[7] += o;
+ t[1] += t[0] >> 32; t[0] &= 0xffffffff;
+ t[2] += t[1] >> 32; t[1] &= 0xffffffff;
+ t[3] += t[2] >> 32; t[2] &= 0xffffffff;
+ t[4] += t[3] >> 32; t[3] &= 0xffffffff;
+ t[5] += t[4] >> 32; t[4] &= 0xffffffff;
+ t[6] += t[5] >> 32; t[5] &= 0xffffffff;
+ t[7] += t[6] >> 32; t[6] &= 0xffffffff;
+
+ r[0] = (sp_digit)(t[0]) & 0x3ffffff;
+ r[1] = (sp_digit)(t[0] >> 26);
+ r[1] |= t[1] << 6;
+ r[1] &= 0x3ffffff;
+ r[2] = (sp_digit)(t[1] >> 20);
+ r[2] |= t[2] << 12;
+ r[2] &= 0x3ffffff;
+ r[3] = (sp_digit)(t[2] >> 14);
+ r[3] |= t[3] << 18;
+ r[3] &= 0x3ffffff;
+ r[4] = (sp_digit)(t[3] >> 8);
+ r[4] |= t[4] << 24;
+ r[4] &= 0x3ffffff;
+ r[5] = (sp_digit)(t[4] >> 2) & 0x3ffffff;
+ r[6] = (sp_digit)(t[4] >> 28);
+ r[6] |= t[5] << 4;
+ r[6] &= 0x3ffffff;
+ r[7] = (sp_digit)(t[5] >> 22);
+ r[7] |= t[6] << 10;
+ r[7] &= 0x3ffffff;
+ r[8] = (sp_digit)(t[6] >> 16);
+ r[8] |= t[7] << 16;
+ r[8] &= 0x3ffffff;
+ r[9] = (sp_digit)(t[7] >> 10);
}
/* Mul a by scalar b and add into r. (r += a * b) */
static void sp_256_mul_add_10(sp_digit* r, const sp_digit* a, sp_digit b)
{
- int64_t tb = b;
- int64_t t = 0;
- int i;
+ int64_t tb = b;
+ int64_t t = 0;
+ int i;
- for (i = 0; i < 10; i++) {
- t += (tb * a[i]) + r[i];
- r[i] = t & 0x3ffffff;
- t >>= 26;
- }
- r[10] += t;
+ for (i = 0; i < 10; i++) {
+ t += (tb * a[i]) + r[i];
+ r[i] = t & 0x3ffffff;
+ t >>= 26;
+ }
+ r[10] += t;
}
/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */
static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
{
- sp_256_cond_add_10(r, a, m, 0 - (a[0] & 1));
- sp_256_norm_10(r);
- sp_256_rshift1_10(r, r);
+ sp_256_cond_add_10(r, a, m, 0 - (a[0] & 1));
+ sp_256_norm_10(r);
+ sp_256_rshift1_10(r, r);
}
/* Shift the result in the high 256 bits down to the bottom. */
static void sp_256_mont_shift_10(sp_digit* r, const sp_digit* a)
{
- int i;
- sp_digit n, s;
+ int i;
+ sp_digit n, s;
- s = a[10];
- n = a[9] >> 22;
- for (i = 0; i < 9; i++) {
- n += (s & 0x3ffffff) << 4;
- r[i] = n & 0x3ffffff;
- n >>= 26;
- s = a[11 + i] + (s >> 26);
- }
- n += s << 4;
- r[9] = n;
- memset(&r[10], 0, sizeof(*r) * 10);
+ s = a[10];
+ n = a[9] >> 22;
+ for (i = 0; i < 9; i++) {
+ n += (s & 0x3ffffff) << 4;
+ r[i] = n & 0x3ffffff;
+ n >>= 26;
+ s = a[11 + i] + (s >> 26);
+ }
+ n += s << 4;
+ r[9] = n;
+ memset(&r[10], 0, sizeof(*r) * 10);
}
/* Add two Montgomery form numbers (r = a + b % m) */
static void sp_256_mont_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
- const sp_digit* m)
+ const sp_digit* m)
{
- sp_256_add_10(r, a, b);
- sp_256_norm_10(r);
- sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
- sp_256_norm_10(r);
+ sp_256_add_10(r, a, b);
+ sp_256_norm_10(r);
+ sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
+ sp_256_norm_10(r);
}
/* Double a Montgomery form number (r = a + a % m) */
static void sp_256_mont_dbl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
{
- sp_256_add_10(r, a, a);
- sp_256_norm_10(r);
- sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
- sp_256_norm_10(r);
+ sp_256_add_10(r, a, a);
+ sp_256_norm_10(r);
+ sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
+ sp_256_norm_10(r);
}
/* Triple a Montgomery form number (r = a + a + a % m) */
static void sp_256_mont_tpl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
{
- sp_256_add_10(r, a, a);
- sp_256_norm_10(r);
- sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
- sp_256_norm_10(r);
- sp_256_add_10(r, r, a);
- sp_256_norm_10(r);
- sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
- sp_256_norm_10(r);
+ sp_256_add_10(r, a, a);
+ sp_256_norm_10(r);
+ sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
+ sp_256_norm_10(r);
+ sp_256_add_10(r, r, a);
+ sp_256_norm_10(r);
+ sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
+ sp_256_norm_10(r);
}
/* Sub b from a into r. (r = a - b) */
static void sp_256_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
{
- int i;
- for (i = 0; i < 10; i++)
- r[i] = a[i] - b[i];
+ int i;
+ for (i = 0; i < 10; i++)
+ r[i] = a[i] - b[i];
}
/* Subtract two Montgomery form numbers (r = a - b % m) */
static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
- const sp_digit* m)
+ const sp_digit* m)
{
- sp_256_sub_10(r, a, b);
- sp_256_cond_add_10(r, r, m, r[9] >> 22);
- sp_256_norm_10(r);
+ sp_256_sub_10(r, a, b);
+ sp_256_cond_add_10(r, r, m, r[9] >> 22);
+ sp_256_norm_10(r);
}
/* Reduce the number back to 256 bits using Montgomery reduction.
@@ -433,60 +433,60 @@ static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b
*/
static void sp_256_mont_reduce_10(sp_digit* a, const sp_digit* m, sp_digit mp)
{
- int i;
- sp_digit mu;
-
- if (mp != 1) {
- for (i = 0; i < 9; i++) {
- mu = (a[i] * mp) & 0x3ffffff;
- sp_256_mul_add_10(a+i, m, mu);
- a[i+1] += a[i] >> 26;
- }
- mu = (a[i] * mp) & 0x3fffffl;
- sp_256_mul_add_10(a+i, m, mu);
- a[i+1] += a[i] >> 26;
- a[i] &= 0x3ffffff;
- }
- else {
- for (i = 0; i < 9; i++) {
- mu = a[i] & 0x3ffffff;
- sp_256_mul_add_10(a+i, p256_mod, mu);
- a[i+1] += a[i] >> 26;
- }
- mu = a[i] & 0x3fffffl;
- sp_256_mul_add_10(a+i, p256_mod, mu);
- a[i+1] += a[i] >> 26;
- a[i] &= 0x3ffffff;
- }
-
- sp_256_mont_shift_10(a, a);
- sp_256_cond_sub_10(a, a, m, 0 - ((a[9] >> 22) > 0));
- sp_256_norm_10(a);
+ int i;
+ sp_digit mu;
+
+ if (mp != 1) {
+ for (i = 0; i < 9; i++) {
+ mu = (a[i] * mp) & 0x3ffffff;
+ sp_256_mul_add_10(a+i, m, mu);
+ a[i+1] += a[i] >> 26;
+ }
+ mu = (a[i] * mp) & 0x3fffffl;
+ sp_256_mul_add_10(a+i, m, mu);
+ a[i+1] += a[i] >> 26;
+ a[i] &= 0x3ffffff;
+ }
+ else {
+ for (i = 0; i < 9; i++) {
+ mu = a[i] & 0x3ffffff;
+ sp_256_mul_add_10(a+i, p256_mod, mu);
+ a[i+1] += a[i] >> 26;
+ }
+ mu = a[i] & 0x3fffffl;
+ sp_256_mul_add_10(a+i, p256_mod, mu);
+ a[i+1] += a[i] >> 26;
+ a[i] &= 0x3ffffff;
+ }
+
+ sp_256_mont_shift_10(a, a);
+ sp_256_cond_sub_10(a, a, m, 0 - ((a[9] >> 22) > 0));
+ sp_256_norm_10(a);
}
/* Multiply a and b into r. (r = a * b) */
static void sp_256_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
{
- int i, j, k;
- int64_t c;
-
- c = ((int64_t)a[9]) * b[9];
- r[19] = (sp_digit)(c >> 26);
- c = (c & 0x3ffffff) << 26;
- for (k = 17; k >= 0; k--) {
- for (i = 9; i >= 0; i--) {
- j = k - i;
- if (j >= 10)
- break;
- if (j < 0)
- continue;
- c += ((int64_t)a[i]) * b[j];
- }
- r[k + 2] += c >> 52;
- r[k + 1] = (c >> 26) & 0x3ffffff;
- c = (c & 0x3ffffff) << 26;
- }
- r[0] = (sp_digit)(c >> 26);
+ int i, j, k;
+ int64_t c;
+
+ c = ((int64_t)a[9]) * b[9];
+ r[19] = (sp_digit)(c >> 26);
+ c = (c & 0x3ffffff) << 26;
+ for (k = 17; k >= 0; k--) {
+ for (i = 9; i >= 0; i--) {
+ j = k - i;
+ if (j >= 10)
+ break;
+ if (j < 0)
+ continue;
+ c += ((int64_t)a[i]) * b[j];
+ }
+ r[k + 2] += c >> 52;
+ r[k + 1] = (c >> 26) & 0x3ffffff;
+ c = (c & 0x3ffffff) << 26;
+ }
+ r[0] = (sp_digit)(c >> 26);
}
/* Multiply two Montogmery form numbers mod the modulus (prime).
@@ -499,39 +499,39 @@ static void sp_256_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
* mp Montogmery mulitplier.
*/
static void sp_256_mont_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
- const sp_digit* m, sp_digit mp)
+ const sp_digit* m, sp_digit mp)
{
- sp_256_mul_10(r, a, b);
- sp_256_mont_reduce_10(r, m, mp);
+ sp_256_mul_10(r, a, b);
+ sp_256_mont_reduce_10(r, m, mp);
}
/* Square a and put result in r. (r = a * a) */
static void sp_256_sqr_10(sp_digit* r, const sp_digit* a)
{
- int i, j, k;
- int64_t c;
-
- c = ((int64_t)a[9]) * a[9];
- r[19] = (sp_digit)(c >> 26);
- c = (c & 0x3ffffff) << 26;
- for (k = 17; k >= 0; k--) {
- for (i = 9; i >= 0; i--) {
- j = k - i;
- if (j >= 10 || i <= j)
- break;
- if (j < 0)
- continue;
-
- c += ((int64_t)a[i]) * a[j] * 2;
- }
- if (i == j)
- c += ((int64_t)a[i]) * a[i];
-
- r[k + 2] += c >> 52;
- r[k + 1] = (c >> 26) & 0x3ffffff;
- c = (c & 0x3ffffff) << 26;
- }
- r[0] = (sp_digit)(c >> 26);
+ int i, j, k;
+ int64_t c;
+
+ c = ((int64_t)a[9]) * a[9];
+ r[19] = (sp_digit)(c >> 26);
+ c = (c & 0x3ffffff) << 26;
+ for (k = 17; k >= 0; k--) {
+ for (i = 9; i >= 0; i--) {
+ j = k - i;
+ if (j >= 10 || i <= j)
+ break;
+ if (j < 0)
+ continue;
+
+ c += ((int64_t)a[i]) * a[j] * 2;
+ }
+ if (i == j)
+ c += ((int64_t)a[i]) * a[i];
+
+ r[k + 2] += c >> 52;
+ r[k + 1] = (c >> 26) & 0x3ffffff;
+ c = (c & 0x3ffffff) << 26;
+ }
+ r[0] = (sp_digit)(c >> 26);
}
/* Square the Montgomery form number. (r = a * a mod m)
@@ -542,10 +542,10 @@ static void sp_256_sqr_10(sp_digit* r, const sp_digit* a)
* mp Montogmery mulitplier.
*/
static void sp_256_mont_sqr_10(sp_digit* r, const sp_digit* a, const sp_digit* m,
- sp_digit mp)
+ sp_digit mp)
{
- sp_256_sqr_10(r, a);
- sp_256_mont_reduce_10(r, m, mp);
+ sp_256_sqr_10(r, a);
+ sp_256_mont_reduce_10(r, m, mp);
}
/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
@@ -557,8 +557,8 @@ static void sp_256_mont_sqr_10(sp_digit* r, const sp_digit* a, const sp_digit* m
#if 0
/* Mod-2 for the P256 curve. */
static const uint32_t p256_mod_2[8] = {
- 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
- 0x00000000,0x00000000,0x00000001,0xffffffff,
+ 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
+ 0x00000000,0x00000000,0x00000001,0xffffffff,
};
//Bit pattern:
//2 2 2 2 2 2 2 1...1
@@ -568,17 +568,17 @@ static const uint32_t p256_mod_2[8] = {
#endif
static void sp_256_mont_inv_10(sp_digit* r, sp_digit* a)
{
- sp_digit t[2*10]; //can be just [10]?
- int i;
+ sp_digit t[2*10]; //can be just [10]?
+ int i;
- memcpy(t, a, sizeof(sp_digit) * 10);
- for (i = 254; i >= 0; i--) {
- sp_256_mont_sqr_10(t, t, p256_mod, p256_mp_mod);
- /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
- if (i >= 224 || i == 192 || (i <= 95 && i != 1))
- sp_256_mont_mul_10(t, t, a, p256_mod, p256_mp_mod);
- }
- memcpy(r, t, sizeof(sp_digit) * 10);
+ memcpy(t, a, sizeof(sp_digit) * 10);
+ for (i = 254; i >= 0; i--) {
+ sp_256_mont_sqr_10(t, t, p256_mod, p256_mp_mod);
+ /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
+ if (i >= 224 || i == 192 || (i <= 95 && i != 1))
+ sp_256_mont_mul_10(t, t, a, p256_mod, p256_mp_mod);
+ }
+ memcpy(r, t, sizeof(sp_digit) * 10);
}
/* Map the Montgomery form projective co-ordinate point to an affine point.
@@ -588,35 +588,35 @@ static void sp_256_mont_inv_10(sp_digit* r, sp_digit* a)
*/
static void sp_256_map_10(sp_point* r, sp_point* p)
{
- sp_digit t1[2*10];
- sp_digit t2[2*10];
- int32_t n;
+ sp_digit t1[2*10];
+ sp_digit t2[2*10];
+ int32_t n;
- sp_256_mont_inv_10(t1, p->z);
+ sp_256_mont_inv_10(t1, p->z);
- sp_256_mont_sqr_10(t2, t1, p256_mod, p256_mp_mod);
- sp_256_mont_mul_10(t1, t2, t1, p256_mod, p256_mp_mod);
+ sp_256_mont_sqr_10(t2, t1, p256_mod, p256_mp_mod);
+ sp_256_mont_mul_10(t1, t2, t1, p256_mod, p256_mp_mod);
- /* x /= z^2 */
- sp_256_mont_mul_10(r->x, p->x, t2, p256_mod, p256_mp_mod);
- memset(r->x + 10, 0, sizeof(r->x) / 2);
- sp_256_mont_reduce_10(r->x, p256_mod, p256_mp_mod);
- /* Reduce x to less than modulus */
- n = sp_256_cmp_10(r->x, p256_mod);
- sp_256_cond_sub_10(r->x, r->x, p256_mod, 0 - (n >= 0));
- sp_256_norm_10(r->x);
+ /* x /= z^2 */
+ sp_256_mont_mul_10(r->x, p->x, t2, p256_mod, p256_mp_mod);
+ memset(r->x + 10, 0, sizeof(r->x) / 2);
+ sp_256_mont_reduce_10(r->x, p256_mod, p256_mp_mod);
+ /* Reduce x to less than modulus */
+ n = sp_256_cmp_10(r->x, p256_mod);
+ sp_256_cond_sub_10(r->x, r->x, p256_mod, 0 - (n >= 0));
+ sp_256_norm_10(r->x);
- /* y /= z^3 */
- sp_256_mont_mul_10(r->y, p->y, t1, p256_mod, p256_mp_mod);
- memset(r->y + 10, 0, sizeof(r->y) / 2);
- sp_256_mont_reduce_10(r->y, p256_mod, p256_mp_mod);
- /* Reduce y to less than modulus */
- n = sp_256_cmp_10(r->y, p256_mod);
- sp_256_cond_sub_10(r->y, r->y, p256_mod, 0 - (n >= 0));
- sp_256_norm_10(r->y);
+ /* y /= z^3 */
+ sp_256_mont_mul_10(r->y, p->y, t1, p256_mod, p256_mp_mod);
+ memset(r->y + 10, 0, sizeof(r->y) / 2);
+ sp_256_mont_reduce_10(r->y, p256_mod, p256_mp_mod);
+ /* Reduce y to less than modulus */
+ n = sp_256_cmp_10(r->y, p256_mod);
+ sp_256_cond_sub_10(r->y, r->y, p256_mod, 0 - (n >= 0));
+ sp_256_norm_10(r->y);
- memset(r->z, 0, sizeof(r->z));
- r->z[0] = 1;
+ memset(r->z, 0, sizeof(r->z));
+ r->z[0] = 1;
}
/* Double the Montgomery form projective point p.
@@ -626,54 +626,54 @@ static void sp_256_map_10(sp_point* r, sp_point* p)
*/
static void sp_256_proj_point_dbl_10(sp_point* r, sp_point* p)
{
- sp_point tp;
- sp_digit t1[2*10];
- sp_digit t2[2*10];
-
- /* Put point to double into result */
- if (r != p)
- *r = *p; /* struct copy */
-
- if (r->infinity) {
- /* If infinity, don't double (work on dummy value) */
- r = &tp;
- }
- /* T1 = Z * Z */
- sp_256_mont_sqr_10(t1, r->z, p256_mod, p256_mp_mod);
- /* Z = Y * Z */
- sp_256_mont_mul_10(r->z, r->y, r->z, p256_mod, p256_mp_mod);
- /* Z = 2Z */
- sp_256_mont_dbl_10(r->z, r->z, p256_mod);
- /* T2 = X - T1 */
- sp_256_mont_sub_10(t2, r->x, t1, p256_mod);
- /* T1 = X + T1 */
- sp_256_mont_add_10(t1, r->x, t1, p256_mod);
- /* T2 = T1 * T2 */
- sp_256_mont_mul_10(t2, t1, t2, p256_mod, p256_mp_mod);
- /* T1 = 3T2 */
- sp_256_mont_tpl_10(t1, t2, p256_mod);
- /* Y = 2Y */
- sp_256_mont_dbl_10(r->y, r->y, p256_mod);
- /* Y = Y * Y */
- sp_256_mont_sqr_10(r->y, r->y, p256_mod, p256_mp_mod);
- /* T2 = Y * Y */
- sp_256_mont_sqr_10(t2, r->y, p256_mod, p256_mp_mod);
- /* T2 = T2/2 */
- sp_256_div2_10(t2, t2, p256_mod);
- /* Y = Y * X */
- sp_256_mont_mul_10(r->y, r->y, r->x, p256_mod, p256_mp_mod);
- /* X = T1 * T1 */
- sp_256_mont_mul_10(r->x, t1, t1, p256_mod, p256_mp_mod);
- /* X = X - Y */
- sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
- /* X = X - Y */
- sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
- /* Y = Y - X */
- sp_256_mont_sub_10(r->y, r->y, r->x, p256_mod);
- /* Y = Y * T1 */
- sp_256_mont_mul_10(r->y, r->y, t1, p256_mod, p256_mp_mod);
- /* Y = Y - T2 */
- sp_256_mont_sub_10(r->y, r->y, t2, p256_mod);
+ sp_point tp;
+ sp_digit t1[2*10];
+ sp_digit t2[2*10];
+
+ /* Put point to double into result */
+ if (r != p)
+ *r = *p; /* struct copy */
+
+ if (r->infinity) {
+ /* If infinity, don't double (work on dummy value) */
+ r = &tp;
+ }
+ /* T1 = Z * Z */
+ sp_256_mont_sqr_10(t1, r->z, p256_mod, p256_mp_mod);
+ /* Z = Y * Z */
+ sp_256_mont_mul_10(r->z, r->y, r->z, p256_mod, p256_mp_mod);
+ /* Z = 2Z */
+ sp_256_mont_dbl_10(r->z, r->z, p256_mod);
+ /* T2 = X - T1 */
+ sp_256_mont_sub_10(t2, r->x, t1, p256_mod);
+ /* T1 = X + T1 */
+ sp_256_mont_add_10(t1, r->x, t1, p256_mod);
+ /* T2 = T1 * T2 */
+ sp_256_mont_mul_10(t2, t1, t2, p256_mod, p256_mp_mod);
+ /* T1 = 3T2 */
+ sp_256_mont_tpl_10(t1, t2, p256_mod);
+ /* Y = 2Y */
+ sp_256_mont_dbl_10(r->y, r->y, p256_mod);
+ /* Y = Y * Y */
+ sp_256_mont_sqr_10(r->y, r->y, p256_mod, p256_mp_mod);
+ /* T2 = Y * Y */
+ sp_256_mont_sqr_10(t2, r->y, p256_mod, p256_mp_mod);
+ /* T2 = T2/2 */
+ sp_256_div2_10(t2, t2, p256_mod);
+ /* Y = Y * X */
+ sp_256_mont_mul_10(r->y, r->y, r->x, p256_mod, p256_mp_mod);
+ /* X = T1 * T1 */
+ sp_256_mont_mul_10(r->x, t1, t1, p256_mod, p256_mp_mod);
+ /* X = X - Y */
+ sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
+ /* X = X - Y */
+ sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
+ /* Y = Y - X */
+ sp_256_mont_sub_10(r->y, r->y, r->x, p256_mod);
+ /* Y = Y * T1 */
+ sp_256_mont_mul_10(r->y, r->y, t1, p256_mod, p256_mp_mod);
+ /* Y = Y - T2 */
+ sp_256_mont_sub_10(r->y, r->y, t2, p256_mod);
}
/* Add two Montgomery form projective points.
@@ -684,73 +684,73 @@ static void sp_256_proj_point_dbl_10(sp_point* r, sp_point* p)
*/
static void sp_256_proj_point_add_10(sp_point* r, sp_point* p, sp_point* q)
{
- sp_digit t1[2*10];
- sp_digit t2[2*10];
- sp_digit t3[2*10];
- sp_digit t4[2*10];
- sp_digit t5[2*10];
-
- /* Ensure only the first point is the same as the result. */
- if (q == r) {
- sp_point* a = p;
- p = q;
- q = a;
- }
-
- /* Check double */
- sp_256_sub_10(t1, p256_mod, q->y);
- sp_256_norm_10(t1);
- if (sp_256_cmp_equal_10(p->x, q->x)
- && sp_256_cmp_equal_10(p->z, q->z)
- && (sp_256_cmp_equal_10(p->y, q->y) || sp_256_cmp_equal_10(p->y, t1))
- ) {
- sp_256_proj_point_dbl_10(r, p);
- }
- else {
- sp_point tp;
- sp_point *v;
-
- v = r;
- if (p->infinity | q->infinity) {
- memset(&tp, 0, sizeof(tp));
- v = &tp;
- }
-
- *r = p->infinity ? *q : *p; /* struct copy */
-
- /* U1 = X1*Z2^2 */
- sp_256_mont_sqr_10(t1, q->z, p256_mod, p256_mp_mod);
- sp_256_mont_mul_10(t3, t1, q->z, p256_mod, p256_mp_mod);
- sp_256_mont_mul_10(t1, t1, v->x, p256_mod, p256_mp_mod);
- /* U2 = X2*Z1^2 */
- sp_256_mont_sqr_10(t2, v->z, p256_mod, p256_mp_mod);
- sp_256_mont_mul_10(t4, t2, v->z, p256_mod, p256_mp_mod);
- sp_256_mont_mul_10(t2, t2, q->x, p256_mod, p256_mp_mod);
- /* S1 = Y1*Z2^3 */
- sp_256_mont_mul_10(t3, t3, v->y, p256_mod, p256_mp_mod);
- /* S2 = Y2*Z1^3 */
- sp_256_mont_mul_10(t4, t4, q->y, p256_mod, p256_mp_mod);
- /* H = U2 - U1 */
- sp_256_mont_sub_10(t2, t2, t1, p256_mod);
- /* R = S2 - S1 */
- sp_256_mont_sub_10(t4, t4, t3, p256_mod);
- /* Z3 = H*Z1*Z2 */
- sp_256_mont_mul_10(v->z, v->z, q->z, p256_mod, p256_mp_mod);
- sp_256_mont_mul_10(v->z, v->z, t2, p256_mod, p256_mp_mod);
- /* X3 = R^2 - H^3 - 2*U1*H^2 */
- sp_256_mont_sqr_10(v->x, t4, p256_mod, p256_mp_mod);
- sp_256_mont_sqr_10(t5, t2, p256_mod, p256_mp_mod);
- sp_256_mont_mul_10(v->y, t1, t5, p256_mod, p256_mp_mod);
- sp_256_mont_mul_10(t5, t5, t2, p256_mod, p256_mp_mod);
- sp_256_mont_sub_10(v->x, v->x, t5, p256_mod);
- sp_256_mont_dbl_10(t1, v->y, p256_mod);
- sp_256_mont_sub_10(v->x, v->x, t1, p256_mod);
- /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
- sp_256_mont_sub_10(v->y, v->y, v->x, p256_mod);
- sp_256_mont_mul_10(v->y, v->y, t4, p256_mod, p256_mp_mod);
- sp_256_mont_mul_10(t5, t5, t3, p256_mod, p256_mp_mod);
- sp_256_mont_sub_10(v->y, v->y, t5, p256_mod);
- }
+ sp_digit t1[2*10];
+ sp_digit t2[2*10];
+ sp_digit t3[2*10];
+ sp_digit t4[2*10];
+ sp_digit t5[2*10];
+
+ /* Ensure only the first point is the same as the result. */
+ if (q == r) {
+ sp_point* a = p;
+ p = q;
+ q = a;
+ }
+
+ /* Check double */
+ sp_256_sub_10(t1, p256_mod, q->y);
+ sp_256_norm_10(t1);
+ if (sp_256_cmp_equal_10(p->x, q->x)
+ && sp_256_cmp_equal_10(p->z, q->z)
+ && (sp_256_cmp_equal_10(p->y, q->y) || sp_256_cmp_equal_10(p->y, t1))
+ ) {
+ sp_256_proj_point_dbl_10(r, p);
+ }
+ else {
+ sp_point tp;
+ sp_point *v;
+
+ v = r;
+ if (p->infinity | q->infinity) {
+ memset(&tp, 0, sizeof(tp));
+ v = &tp;
+ }
+
+ *r = p->infinity ? *q : *p; /* struct copy */
+
+ /* U1 = X1*Z2^2 */
+ sp_256_mont_sqr_10(t1, q->z, p256_mod, p256_mp_mod);
+ sp_256_mont_mul_10(t3, t1, q->z, p256_mod, p256_mp_mod);
+ sp_256_mont_mul_10(t1, t1, v->x, p256_mod, p256_mp_mod);
+ /* U2 = X2*Z1^2 */
+ sp_256_mont_sqr_10(t2, v->z, p256_mod, p256_mp_mod);
+ sp_256_mont_mul_10(t4, t2, v->z, p256_mod, p256_mp_mod);
+ sp_256_mont_mul_10(t2, t2, q->x, p256_mod, p256_mp_mod);
+ /* S1 = Y1*Z2^3 */
+ sp_256_mont_mul_10(t3, t3, v->y, p256_mod, p256_mp_mod);
+ /* S2 = Y2*Z1^3 */
+ sp_256_mont_mul_10(t4, t4, q->y, p256_mod, p256_mp_mod);
+ /* H = U2 - U1 */
+ sp_256_mont_sub_10(t2, t2, t1, p256_mod);
+ /* R = S2 - S1 */
+ sp_256_mont_sub_10(t4, t4, t3, p256_mod);
+ /* Z3 = H*Z1*Z2 */
+ sp_256_mont_mul_10(v->z, v->z, q->z, p256_mod, p256_mp_mod);
+ sp_256_mont_mul_10(v->z, v->z, t2, p256_mod, p256_mp_mod);
+ /* X3 = R^2 - H^3 - 2*U1*H^2 */
+ sp_256_mont_sqr_10(v->x, t4, p256_mod, p256_mp_mod);
+ sp_256_mont_sqr_10(t5, t2, p256_mod, p256_mp_mod);
+ sp_256_mont_mul_10(v->y, t1, t5, p256_mod, p256_mp_mod);
+ sp_256_mont_mul_10(t5, t5, t2, p256_mod, p256_mp_mod);
+ sp_256_mont_sub_10(v->x, v->x, t5, p256_mod);
+ sp_256_mont_dbl_10(t1, v->y, p256_mod);
+ sp_256_mont_sub_10(v->x, v->x, t1, p256_mod);
+ /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
+ sp_256_mont_sub_10(v->y, v->y, v->x, p256_mod);
+ sp_256_mont_mul_10(v->y, v->y, t4, p256_mod, p256_mp_mod);
+ sp_256_mont_mul_10(t5, t5, t3, p256_mod, p256_mp_mod);
+ sp_256_mont_sub_10(v->y, v->y, t5, p256_mod);
+ }
}
/* Multiply the point by the scalar and return the result.
@@ -763,48 +763,48 @@ static void sp_256_proj_point_add_10(sp_point* r, sp_point* p, sp_point* q)
*/
static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit* k /*, int map*/)
{
- enum { map = 1 }; /* we always convert result to affine coordinates */
- sp_point t[3];
- sp_digit n;
- int i;
- int c, y;
-
- memset(t, 0, sizeof(t));
-
- /* t[0] = {0, 0, 1} * norm */
- t[0].infinity = 1;
- /* t[1] = {g->x, g->y, g->z} * norm */
- sp_256_mod_mul_norm_10(t[1].x, g->x);
- sp_256_mod_mul_norm_10(t[1].y, g->y);
- sp_256_mod_mul_norm_10(t[1].z, g->z);
-
- i = 9;
- c = 22;
- n = k[i--] << (26 - c);
- for (; ; c--) {
- if (c == 0) {
- if (i == -1)
- break;
-
- n = k[i--];
- c = 26;
- }
-
- y = (n >> 25) & 1;
- n <<= 1;
-
- sp_256_proj_point_add_10(&t[y^1], &t[0], &t[1]);
- memcpy(&t[2], &t[y], sizeof(sp_point));
- sp_256_proj_point_dbl_10(&t[2], &t[2]);
- memcpy(&t[y], &t[2], sizeof(sp_point));
- }
+ enum { map = 1 }; /* we always convert result to affine coordinates */
+ sp_point t[3];
+ sp_digit n;
+ int i;
+ int c, y;
+
+ memset(t, 0, sizeof(t));
+
+ /* t[0] = {0, 0, 1} * norm */
+ t[0].infinity = 1;
+ /* t[1] = {g->x, g->y, g->z} * norm */
+ sp_256_mod_mul_norm_10(t[1].x, g->x);
+ sp_256_mod_mul_norm_10(t[1].y, g->y);
+ sp_256_mod_mul_norm_10(t[1].z, g->z);
+
+ i = 9;
+ c = 22;
+ n = k[i--] << (26 - c);
+ for (; ; c--) {
+ if (c == 0) {
+ if (i == -1)
+ break;
+
+ n = k[i--];
+ c = 26;
+ }
+
+ y = (n >> 25) & 1;
+ n <<= 1;
+
+ sp_256_proj_point_add_10(&t[y^1], &t[0], &t[1]);
+ memcpy(&t[2], &t[y], sizeof(sp_point));
+ sp_256_proj_point_dbl_10(&t[2], &t[2]);
+ memcpy(&t[y], &t[2], sizeof(sp_point));
+ }
- if (map)
- sp_256_map_10(r, &t[0]);
- else
- memcpy(r, &t[0], sizeof(sp_point));
+ if (map)
+ sp_256_map_10(r, &t[0]);
+ else
+ memcpy(r, &t[0], sizeof(sp_point));
- memset(t, 0, sizeof(t)); //paranoia
+ memset(t, 0, sizeof(t)); //paranoia
}
/* Multiply the base point of P256 by the scalar and return the result.
@@ -816,7 +816,7 @@ static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit*
*/
static void sp_256_ecc_mulmod_base_10(sp_point* r, sp_digit* k /*, int map*/)
{
- sp_256_ecc_mulmod_10(r, &p256_base, k /*, map*/);
+ sp_256_ecc_mulmod_10(r, &p256_base, k /*, map*/);
}
/* Multiply the point by the scalar and serialize the X ordinate.
@@ -828,22 +828,22 @@ static void sp_256_ecc_mulmod_base_10(sp_point* r, sp_digit* k /*, int map*/)
*/
static void sp_ecc_secret_gen_256(sp_digit priv[10], const uint8_t *pub2x32, uint8_t* out32)
{
- sp_point point[1];
+ sp_point point[1];
#if FIXED_PEER_PUBKEY
- memset((void*)pub2x32, 0x55, 64);
+ memset((void*)pub2x32, 0x55, 64);
#endif
- dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
- dump_hex(" %s\n", pub2x32 + 32, 32);
+ dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
+ dump_hex(" %s\n", pub2x32 + 32, 32);
- sp_256_point_from_bin2x32(point, pub2x32);
- dump_hex("point->x %s\n", point->x, sizeof(point->x));
- dump_hex("point->y %s\n", point->y, sizeof(point->y));
+ sp_256_point_from_bin2x32(point, pub2x32);
+ dump_hex("point->x %s\n", point->x, sizeof(point->x));
+ dump_hex("point->y %s\n", point->y, sizeof(point->y));
- sp_256_ecc_mulmod_10(point, point, priv);
+ sp_256_ecc_mulmod_10(point, point, priv);
- sp_256_to_bin(point->x, out32);
- dump_hex("out32: %s\n", out32, 32);
+ sp_256_to_bin(point->x, out32);
+ dump_hex("out32: %s\n", out32, 32);
}
/* Generates a scalar that is in the range 1..order-1. */
@@ -852,8 +852,8 @@ static void sp_ecc_secret_gen_256(sp_digit priv[10], const uint8_t *pub2x32, uin
#if !SIMPLIFY
static void sp_256_add_one_10(sp_digit* a)
{
- a[0]++;
- sp_256_norm_10(a);
+ a[0]++;
+ sp_256_norm_10(a);
}
#endif
static void sp_256_ecc_gen_k_10(sp_digit k[10])