Skip to content

Commit 3392aef

Browse files
committed
Merge BoringSSL 227ff6e: Remove unions in EC_SCALAR and EC_FELEM.
2 parents d18d912 + 227ff6e commit 3392aef

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

crypto/fipsmodule/ec/p256.c

+24-24
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,17 @@ static void fiat_p256_select_point(const fiat_p256_limb_t idx, size_t size,
329329
}
330330

331331
// fiat_p256_get_bit returns the |i|th bit in |in|
332-
static crypto_word fiat_p256_get_bit(const uint8_t *in, int i) {
332+
static crypto_word fiat_p256_get_bit(const Limb scalar[P256_LIMBS], int i) {
333333
if (i < 0 || i >= 256) {
334334
return 0;
335335
}
336-
return (in[i >> 3] >> (i & 7)) & 1;
336+
#if defined(OPENSSL_64_BIT)
337+
OPENSSL_STATIC_ASSERT(sizeof(Limb) == 8, "BN_ULONG was not 64-bit");
338+
return (in->words[i >> 6] >> (i & 63)) & 1;
339+
#else
340+
OPENSSL_STATIC_ASSERT(sizeof(Limb) == 4, "BN_ULONG was not 32-bit");
341+
return (in->words[i >> 5] >> (i & 31)) & 1;
342+
#endif
337343
}
338344

339345
void p256_point_mul(P256_POINT *r, const Limb scalar[P256_LIMBS],
@@ -343,12 +349,8 @@ void p256_point_mul(P256_POINT *r, const Limb scalar[P256_LIMBS],
343349
debug_assert_nonsecret(p_x != NULL);
344350
debug_assert_nonsecret(p_y != NULL);
345351

346-
P256_SCALAR_BYTES scalar_bytes;
347-
p256_scalar_bytes_from_limbs(scalar_bytes, scalar);
348-
349352
fiat_p256_felem p_pre_comp[17][3];
350353
OPENSSL_memset(&p_pre_comp, 0, sizeof(p_pre_comp));
351-
352354
// Precompute multiples.
353355
limbs_copy(&p_pre_comp[1][0][0], p_x, P256_LIMBS);
354356
limbs_copy(&p_pre_comp[1][1][0], p_y, P256_LIMBS);
@@ -380,12 +382,12 @@ void p256_point_mul(P256_POINT *r, const Limb scalar[P256_LIMBS],
380382

381383
// do other additions every 5 doublings
382384
if (i % 5 == 0) {
383-
crypto_word bits = fiat_p256_get_bit(scalar_bytes, i + 4) << 5;
384-
bits |= fiat_p256_get_bit(scalar_bytes, i + 3) << 4;
385-
bits |= fiat_p256_get_bit(scalar_bytes, i + 2) << 3;
386-
bits |= fiat_p256_get_bit(scalar_bytes, i + 1) << 2;
387-
bits |= fiat_p256_get_bit(scalar_bytes, i) << 1;
388-
bits |= fiat_p256_get_bit(scalar_bytes, i - 1);
385+
crypto_word bits = fiat_p256_get_bit(scalar, i + 4) << 5;
386+
bits |= fiat_p256_get_bit(scalar, i + 3) << 4;
387+
bits |= fiat_p256_get_bit(scalar, i + 2) << 3;
388+
bits |= fiat_p256_get_bit(scalar, i + 1) << 2;
389+
bits |= fiat_p256_get_bit(scalar, i) << 1;
390+
bits |= fiat_p256_get_bit(scalar, i - 1);
389391
crypto_word sign, digit;
390392
recode_scalar_bits(&sign, &digit, bits);
391393

@@ -414,9 +416,6 @@ void p256_point_mul(P256_POINT *r, const Limb scalar[P256_LIMBS],
414416
}
415417

416418
void p256_point_mul_base(P256_POINT *r, const Limb scalar[P256_LIMBS]) {
417-
P256_SCALAR_BYTES scalar_bytes;
418-
p256_scalar_bytes_from_limbs(scalar_bytes, scalar);
419-
420419
// Set nq to the point at infinity.
421420
fiat_p256_felem nq[3] = {{0}, {0}, {0}}, tmp[3];
422421

@@ -427,10 +426,10 @@ void p256_point_mul_base(P256_POINT *r, const Limb scalar[P256_LIMBS]) {
427426
}
428427

429428
// First, look 32 bits upwards.
430-
crypto_word bits = fiat_p256_get_bit(scalar_bytes, i + 224) << 3;
431-
bits |= fiat_p256_get_bit(scalar_bytes, i + 160) << 2;
432-
bits |= fiat_p256_get_bit(scalar_bytes, i + 96) << 1;
433-
bits |= fiat_p256_get_bit(scalar_bytes, i + 32);
429+
crypto_word bits = fiat_p256_get_bit(scalar, i + 224) << 3;
430+
bits |= fiat_p256_get_bit(scalar, i + 160) << 2;
431+
bits |= fiat_p256_get_bit(scalar, i + 96) << 1;
432+
bits |= fiat_p256_get_bit(scalar, i + 32);
434433
// Select the point to add, in constant time.
435434
fiat_p256_select_point_affine((fiat_p256_limb_t)bits, 15,
436435
fiat_p256_g_pre_comp[1], tmp);
@@ -446,12 +445,13 @@ void p256_point_mul_base(P256_POINT *r, const Limb scalar[P256_LIMBS]) {
446445
}
447446

448447
// Second, look at the current position.
449-
bits = fiat_p256_get_bit(scalar_bytes, i + 192) << 3;
450-
bits |= fiat_p256_get_bit(scalar_bytes, i + 128) << 2;
451-
bits |= fiat_p256_get_bit(scalar_bytes, i + 64) << 1;
452-
bits |= fiat_p256_get_bit(scalar_bytes, i);
448+
bits = fiat_p256_get_bit(scalar, i + 192) << 3;
449+
bits |= fiat_p256_get_bit(scalar, i + 128) << 2;
450+
bits |= fiat_p256_get_bit(scalar, i + 64) << 1;
451+
bits |= fiat_p256_get_bit(scalar, i);
453452
// Select the point to add, in constant time.
454-
fiat_p256_select_point_affine((fiat_p256_limb_t)bits, 15, fiat_p256_g_pre_comp[0], tmp);
453+
fiat_p256_select_point_affine((fiat_p256_limb_t)bits, 15,
454+
fiat_p256_g_pre_comp[0], tmp);
455455
fiat_p256_point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1 /* mixed */,
456456
tmp[0], tmp[1], tmp[2]);
457457
}

0 commit comments

Comments
 (0)