~sircmpwn/hare

hare/crypto/curve25519/curve25519.ha -rw-r--r-- 5.0 KiB
296925c9Drew DeVault net::dns: reuse sendbuf for recv 19 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// License: MPL-2.0
// (c) 2021 Thomas Bracht Laumann Jespersen <t@laumann.xyz>
use bytes;

// Implements the curve25519 elliptic curve

// Implementations used for reference
//
// Kleppmann: https://martin.kleppmann.com/papers/curve25519.pdf
// OpenSSL: https://github.com/openssl/openssl/blob/master/crypto/ec/curve25519.c
// Go: https://github.com/golang/crypto/blob/master/curve25519/curve25519.go
// TweetNaCl: https://tweetnacl.cr.yp.to/

// The size of the scalar input to X25519.
export def SCALARSZ: size = 32;

// The size of the point input to X25519.
export def POINTSZ: size = 32;

// The canonical Curve25519 generator
export const BASEPOINT: [POINTSZ]u8 = [9, 0...];

// An internal representation used for arithmetic operations.
def FIELDSZ: size = 16;
type elem = [FIELDSZ]i64;

// Constant used in scalar multiplication.
const _121665: elem = [0xdb41, 1, 0...];

// Compute the result of the scalar multiplication (scalar * point) and put the
// result in out.
export fn x25519(
	out: *[SCALARSZ]u8,
	scalar: const *[SCALARSZ]u8,
	point: const *[POINTSZ]u8
) void = {
	scalarmult(out, scalar, point);
};

// Compute the result of the scalar multiplication (scalar * point) where point
// is BASEPOINT.
export fn scalarmult_base(
	out: *[SCALARSZ]u8,
	scalar: const *[SCALARSZ]u8
) void = {
	scalarmult(out, scalar, &BASEPOINT);
};

// Prepares the scalar to avoid particular attacks. See the "clamping" section
// in Kleppmann's paper.
export fn clamp(scalar: *[SCALARSZ]u8) void = {
	scalar[0] &= 0xf8;
	scalar[31] = (scalar[31] & 0x7f) | 0x40;
};

// Set out to the product (scalar * point)
export fn scalarmult(
	out: *[SCALARSZ]u8,
	scalar: const *[SCALARSZ]u8,
	point: const *[POINTSZ]u8
) void = {
	let clamped: [SCALARSZ]u8 = *scalar;
	defer bytes::zero(clamped);
	clamp(&clamped);

	let x = unpack25519(point);
	let a: elem = [1, 0...];
	let b: elem = x;
	let c: elem = [0...];
	let d: elem = [1, 0...];
	let e: elem = [0...];
	let f: elem = [0...];

	for (let i = 254i; i >= 0; i -= 1) {
		let iz = i : size;
		let bit = ((clamped[iz >> 3] >> (iz & 7)) & 1) : i64;
		swap25519(&a, &b, bit);
		swap25519(&c, &d, bit);
		addfe(&e, &a, &c);
		subfe(&a, &a, &c);
		addfe(&c, &b, &d);
		subfe(&b, &b, &d);
		mulfe(&d, &e, &e);
		mulfe(&f, &a, &a);
		mulfe(&a, &c, &a);
		mulfe(&c, &b, &e);
		addfe(&e, &a, &c);
		subfe(&a, &a, &c);
		mulfe(&b, &a, &a);
		subfe(&c, &d, &f);
		mulfe(&a, &c, &_121665);
		addfe(&a, &a, &d);
		mulfe(&c, &c, &a);
		mulfe(&a, &d, &f);
		mulfe(&d, &b, &x);
		mulfe(&b, &e, &e);
		swap25519(&a, &b, bit);
		swap25519(&c, &d, bit);
	};

	invfe(&c, &c);
	mulfe(&a, &a, &c);
	pack25519(out, &a);
};

fn unpack25519(in: *[SCALARSZ]u8) elem = {
	let fe: elem = [0...];

	for (let i = 0z; i < FIELDSZ; i += 1) {
		fe[i] = in[2 * i]: i64 + ((in[2 * i + 1]: i64) << 8);
	};
	fe[15] &= 0x7fff;

	return fe;
};

fn carry25519(fe: *elem) void = {
	let carry = 0i64;

	for (let i = 0z; i < FIELDSZ; i += 1) {
		carry = fe[i] >> 16;
		fe[i] -= (carry << 16);
		if (i < 15) {
			fe[i + 1] += carry;
		} else {
			fe[0] += (38 * carry);
		};
	};
};

// Set out = a + b
fn addfe(out: *elem, a: const *elem, b: const *elem) void = {
	for (let i = 0z; i < FIELDSZ; i += 1) {
		out[i] = a[i] + b[i];
	};
};

// Set out = a - b
fn subfe(out: *elem, a: const *elem, b: const *elem) void = {
	for (let i = 0z; i < FIELDSZ; i += 1) {
		out[i] = a[i] - b[i];
	};
};

// Set out = a * b
fn mulfe(out: *elem, a: const *elem, b: const *elem) void = {
	let product: [31]i64 = [0...];

	for (let i = 0z; i < FIELDSZ; i += 1) {
		for (let j = 0z; j < FIELDSZ; j += 1) {
			product[i + j] += a[i] * b[j];
		};
	};

	for (let i = 0z; i < 15; i += 1) {
		product[i] += (38 * product[i + 16]);
	};
	out[0..FIELDSZ] = product[0..FIELDSZ];

	carry25519(out);
	carry25519(out);
};

// Compute the multiplicative inverse
fn invfe(out: *elem, a: const *elem) void = {
	let c: elem = *a;
	for (let i = 253i; i >= 0; i -= 1) {
		mulfe(&c, &c, &c);
		if (i != 2 && i != 4) {
			mulfe(&c, &c, a);
		};
	};
	out[..] = c[..];
};

// Swap inputs p and q, if bit is 1, do nothing if bit is 0.
//
// If bit is 1, this function swaps the content of parameters p and q (both in
// elem representation), and it does nothing if bit is 0. As bit may be
// part of a secret value, this function cannot use a simple if statement,
// because that would not be constant-time
fn swap25519(p: *elem, q: *elem, bit: i64) void = {
	let c = ~(bit : u64 - 1) : i64;
	for (let i = 0z; i < FIELDSZ; i += 1) {
		let t = c & (p[i] ^ q[i]);
		p[i] ^= t;
		q[i] ^= t;
	};
};

fn pack25519(out: *[SCALARSZ]u8, a: const *elem) void = {
	let m: elem = [0...];
	let t: elem = *a;

	carry25519(&t);
	carry25519(&t);
	carry25519(&t);

	for (let _i = 0z; _i < 2; _i += 1) {
		m[0] = t[0] - 0xffed;
		for (let i = 1z; i < 15; i += 1) {
			m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1);
			m[i - 1] &= 0xffff;
		};
		m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1);
		let carry = (m[15] >> 16) & 1;
		m[14] &= 0xffff;
		swap25519(&t, &m, 1 - carry);
	};

	for (let i = 0z; i < FIELDSZ; i += 1) {
		out[2 * i] = (t[i] & 0xff) : u8;
		out[2 * i + 1] = (t[i] >> 8) : u8;
	};
};