| 1 |
#include "common.h" |
| 2 |
#include "endianess.h" |
| 3 |
|
| 4 |
FAKE_INIT(x25519) |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
+ x |
| 10 |
|
| 11 |
- 19. |
| 12 |
|
| 13 |
|
| 14 |
#include "mod25519.c" |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
₂ |
| 20 |
P₃ |
| 21 |
|
| 22 |
²⁶) |
| 23 |
²⁶) |
| 24 |
²⁶) |
| 25 |
²⁶) |
| 26 |
2²⁶) |
| 27 |
|
| 28 |
STATIC void ladder_step(uint32_t x2[10], uint32_t z2[10], uint32_t x3[10], uint32_t z3[10], const uint32_t xp[10]) |
| 29 |
{ |
| 30 |
uint32_t t0[10], t1[10]; |
| 31 |
static const uint32_t nr_121666[10] = { 121666 }; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
sub_25519(t0, x3, z3); |
| 36 |
sub_25519(t1, x2, z2); |
| 37 |
add32(x2, x2, z2); |
| 38 |
add32(z2, x3, z3); |
| 39 |
mul_25519(z3, t0, x2); |
| 40 |
mul_25519(z2, z2, t1); |
| 41 |
add32(x3, z3, z2); |
| 42 |
sub_25519(z2, z3, z2); |
| 43 |
mul_25519(x3, x3, x3); |
| 44 |
mul_25519(z2, z2, z2); / |
| 45 |
mul_25519(t0, t1, t1); |
| 46 |
mul_25519(t1, x2, x2); |
| 47 |
sub_25519(x2, t1, t0); |
| 48 |
mul_25519(z3, xp, z2); |
| 49 |
mul_25519(z2, x2, nr_121666); |
| 50 |
add32(z2, t0, z2); |
| 51 |
mul_25519(z2, x2, z2); |
| 52 |
mul_25519(x2, t1, t0); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
void ladder(uint8_t ssecret[32], const uint8_t *k, size_t len, const uint8_t pubkey[32]) |
| 66 |
{ |
| 67 |
uint32_t R0x[10] = { 1 }; |
| 68 |
uint32_t R0z[10] = { 0 }; |
| 69 |
uint32_t R1x[10]; |
| 70 |
uint32_t R1z[10] = { 1 }; |
| 71 |
uint32_t xp[10]; |
| 72 |
uint32_t invz[10]; |
| 73 |
uint32_t affx[10]; |
| 74 |
uint64_t tmp_64[4]; |
| 75 |
unsigned bit_idx, swap; |
| 76 |
unsigned i; |
| 77 |
|
| 78 |
for (i=0; i<4; i++) { |
| 79 |
tmp_64[i] = LOAD_U64_LITTLE(&pubkey[i*8]); |
| 80 |
} |
| 81 |
convert_le64_to_le25p5(xp, tmp_64); |
| 82 |
|
| 83 |
memcpy(R1x, xp, sizeof R1x); |
| 84 |
bit_idx = 7; |
| 85 |
swap = 0; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
while (len>0) { |
| 90 |
unsigned bit; |
| 91 |
|
| 92 |
bit = (k[len-1] >> bit_idx) & 1; |
| 93 |
swap ^= bit; |
| 94 |
|
| 95 |
cswap(R0x, R0z, R1x, R1z, swap); |
| 96 |
ladder_step(R0x, R0z, R1x, R1z, xp); |
| 97 |
swap = bit; |
| 98 |
|
| 99 |
if (bit_idx-- == 0) { |
| 100 |
bit_idx = 7; |
| 101 |
len--; |
| 102 |
} |
| 103 |
} |
| 104 |
cswap(R0x, R0z, R1x, R1z, swap); |
| 105 |
|
| 106 |
invert_25519(invz, R0z); |
| 107 |
mul_25519(affx, R0x, invz); |
| 108 |
convert_le25p5_to_le64(tmp_64, affx); |
| 109 |
reduce_25519_le64(tmp_64); |
| 110 |
|
| 111 |
for (i=0; i<4; i++) { |
| 112 |
STORE_U64_LITTLE(&ssecret[i*8], tmp_64[i]); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#ifdef PROFILE |
| 117 |
int main(void) |
| 118 |
{ |
| 119 |
uint8_t pubkey[32]; |
| 120 |
uint8_t secret[32]; |
| 121 |
uint8_t out[32]; |
| 122 |
unsigned i; |
| 123 |
|
| 124 |
secret[0] = pubkey[0] = 0xAA; |
| 125 |
for (i=1; i<32; i++) { |
| 126 |
secret[i] = pubkey[i] = (uint8_t)((secret[i-1] << 1) | (secret[i-1] >> 7)); |
| 127 |
} |
| 128 |
|
| 129 |
for (i=0; i<10000; i++) { |
| 130 |
ladder(out, secret, sizeof secret, pubkey); |
| 131 |
} |
| 132 |
} |
| 133 |
#endif |