Debian Codegraph @

boringssl_notes.md

raw symbols SCIP
1 Notes from a deep dive into BoringSSL's Curve25519 code on June 19, 2020, with
2 an eye for finding the path to a fully verified high-level crypto operation
3 using bedrock2, fiat-crypto, and rupicola.
4
5 # Crypto operations
6
7 These are the top-level operations, and depend on *scalar* (function names
8 usually include `sc`), *group* (`ge`), and *field* (`fe`) operations. Note that
9 "scalar" here does not actually mean scalar in the usual sense; the "scalars"
10 are members of a field modulo a parameter called _l_, which is simply a prime
11 field with a different modulus than the one used for "field" elements.
12
13 There are two top-level schemes provided in BoringSSL for Curve25519: Ed25519
14 and X25519 ([more on Curve25519
15 naming](https://mailarchive.ietf.org/arch/msg/cfrg/-9LEdnzVrE5RORux3Oo_oDDRksU/)).
16 Ed25519 is the signature system using Edwards coordinates, and X25519 is the
17 scheme using Montgomery X-coordinates and Diffie-Hellman. Both operations use
18 the same underlying curve.
19
20 These functions also depend on some SHA512 operations which are mentioned here
21 in parentheses but not expanded. `x25519_NEON` is also omitted (it is only used
22 if `BORINGSSL_X25519_NEON` is defined).
23
24 ## Implementation links
25
26 [ED25519_keypair](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#1879)
27 [ED25519_sign](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#1885)
28 [ED25519_verify](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#1924)
29 [X25519_keypair](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#2100)
30
31 ## Callees
32
33 ### Ed25519
34
35 - Scalar : `x25519_sc_reduce` `scmuladd`
36 - Group : `x25519_ge_scalarmult_base` `x25519_ge_frombytes_vartime` `ge_double_scalarmult_vartime` `x25519_ge_tobytes` `ge_p3_tobytes`
37 - Field : `fe_neg` `fe_carry`
38
39 ### X25519
40
41 - Group : `x25519_ge_scalarmult_base` `x25519_scalar_mult`
42 - Field : `fe_add` `fe_sub` `fe_loose_invert` `fe_mul_tlt` `fe_tobytes`
43
44 ## Code graph
45
46 Here, and elsewhere, a `*` indicates a function that is low-level enough to
47 generate in fiat-crypto's AST but is currently not generated, and `+` indicates
48 a function that is already implemented with fiat-crypto's generated code.
49
50 ```
51 ED25519_keypair
52 | (RAND_bytes)
53 | ED25519_keypair_from_seed
54 | (SHA512, OPENSSL_memcpy)
55 | x25519_ge_scalarmult_base
56 | ge_p3_tobytes
57
58 ED25519_sign
59 | (SHA512, SHA512_Init, SHA512_Update, SHA512_Final)
60 | * x25519_sc_reduce
61 | x25519_ge_scalarmult_base
62 | * sc_muladd
63
64 ED25519_verify
65 | (OPENSSL_memcpy, SHA512_Init, SHA512_Update, SHA512_Final, CRYPTO_memcmp)
66 | x25519_ge_frombytes_vartime
67 | + fe_neg
68 | + fe_carry
69 | * x25519_sc_reduce
70 | ge_double_scalarmult_vartime
71 | x25519_ge_tobytes
72
73 X25519_keypair
74 | (RAND_bytes)
75 | X25519_public_from_private
76 | (OPENSSL_memcpy)
77 | x25519_ge_scalarmult_base
78 | + fe_add
79 | + fe_sub
80 | + fe_loose_invert
81 | + fe_mul_tlt
82 | + fe_tobytes
83
84 X25519
85 | (CRYPTO_memcmp)
86 | x25519_scalar_mult
87 ```
88
89 # Scalar Operations
90
91 The top-level functions rely on just two scalar operations: `x25519_sc_reduce`
92 and `sc_muladd`. Both are low-level enough to be generated by fiat-crypto, in
93 theory, and could probably reuse parts of the field arithmetic infrastructure.
94 However, they might also include low-level optimizations at a similar level of
95 complexity to field arithmetic. (See
96 [https://github.com/mit-plv/fiat-crypto/issues/805](https://github.com/mit-plv/fiat-crypto/issues/805)
97 for some discussion).
98
99 `x25519_sc_reduce` implements full modular reduction (like `freeze`).
100
101 `sc_muladd(a, b, c)` produces `(ab + c) mod l`. Also full modular reduction,
102 although implementation looks like Solinas.
103
104 ## Implementation links
105
106 [x25519_sc_reduce](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#1057)
107 [sc_muladd](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#1398)
108
109 ## Callees
110
111 None
112
113 # Group Operations
114
115 These operations manipulate curve points (members of the curve algebraic
116 group). They depend on field operations mostly, but also a few uncategorized
117 helpers (here listed as "misc").
118
119 The function `x25519_scalar_mult` corresponds to `montladder` in the
120 fiat-crypto repository and a bedrock2 version exists in rupicola, but has not
121 at the time of writing been integrated to BoringSSL or proven to connect to the
122 specifications of the field operations.
123
124 Note" `x25519_ge_scalarmult` is also defined and used in the SPAKE2 protocol,
125 but not for the core Ed25519.
126
127 ## Implementation links
128
129 [x25519_ge_tobytes](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#479)
130 [ge_p3_tobytes](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#491)
131 [x25519_ge_frombytes_vartime](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#503)
132 [ge_p3_0](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#555)
133 [ge_cached_0](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#569)
134 [ge_p3_to_p2](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#576)
135 [x25519_ge_p3_to_cached](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#583)
136 [x25519_ge_p1p1_to_p2](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#591)
137 [x25519_ge_p1p1_to_p3](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#598)
138 [ge_p2_dbl](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#613)
139 [ge_p3_dbl](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#632)
140 [ge_madd](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#639)
141 [ge_msub](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#656)
142 [x25519_ge_add](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#673)
143 [x25519_ge_sub](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#691)
144 [cmov](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#718)
145 [x25519_ge_scalarmult_small_precomp](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#724)
146 [x25519_ge_scalarmult_base (if OPENSSL_SMALL defined)](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#780)
147 [table_select](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#792)
148 [x25519_ge_scalarmult_base (if OPENSSL_SMALL undefined)](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#823)
149 [ge_double_scalarmult_vartime](https://boringssl.googlesource.com/boringssl/+/cbac9c3a2d20a05d5c634eaab8f8b07589838d57/crypto/curve25519/curve25519.c#972)
150
151 ## Callees
152
153 - Field: `fe_frombytes` `fe_0` `fe_1` `fe_cswap` `fe_add` `fe_sub` `fe_mul_impl` `fe_sq_tl` `fe_mul121666` `fe_invert` `fe_tobytes` `fe_frombytes_strict` `fe_cmov` `fe_copy_ll` `fe_carry` `fe_neg` `fe_copy_lt` `fe_copy` `fe_sq_tt` `fe_sq2_tt` `fe_isnonzero` `fe_isnegative` `fe_invert` `fe_loose_0` `fe_loose_1` `fe_pow22523`
154 - Misc: `equal` `negative` `slide`
155
156 Note: `fe_mul_impl` is called through various wrappers: `fe_mul_ltt` `fe_mul_llt` `fe_mul_ttt` `fe_mul_tlt` `fe_mul_ttl` `fe_mul_tll`.
157
158 ## Code graph
159
160 Starting from callees of crypto operations.
161
162 ```
163 x25519_scalar_mult
164 | x25519_scalar_mult_generic
165 | + fe_frombytes
166 | * fe_1
167 | * fe_0
168 | * fe_copy
169 | * fe_cswap
170 | + fe_sub
171 | + fe_add
172 | + fe_mul_tll
173 | + fe_sq_tl
174 | + fe_mul121666
175 | + fe_mul_ttt
176 | fe_invert
177 | + fe_tobytes
178
179 x25519_ge_scalarmult_base (if OPENSSSL_SMALL defined)
180 | x25519_ge_scalarmult_small_precomp
181 | + fe_frombytes_strict
182 | + fe_add
183 | + fe_sub
184 | + fe_mul_ltt
185 | ge_p3_0
186 | * fe_0
187 | * fe_1
188 | * equal
189 | cmov
190 | * fe_cmov
191 | * fe_copy_ll
192 | + fe_carry
193 | + fe_neg
194 | x25519_ge_p3_to_cached
195 | + fe_add
196 | + fe_sub
197 | * fe_copy_lt
198 | + fe_mul_llt
199 | x25519_ge_add
200 | + fe_add
201 | + fe_sub
202 | + fe_mul_tll
203 | + fe_carry
204 | x25519_ge_p1p1_to_p3
205 | + fe_mul_tll
206
207 x25519_ge_scalarmult_base (if OPENSSL_SMALL undefined)
208 | ge_p3_0
209 | * fe_0
210 | * fe_1
211 | table_select
212 | ge_precomp_0
213 | * fe_loose_1
214 | * fe_loose_0
215 | * negative
216 | * equal
217 | cmov
218 | * fe_cmov
219 | * fe_copy_ll
220 | + fe_carry
221 | + fe_neg
222 | * fe_copy_ll
223 | + fe_carry
224 | + fe_neg
225 | ge_madd
226 | + fe_add
227 | + fe_sub
228 | + fe_mul_tll
229 | + fe_carry
230 | x25519_ge_p1p1_to_p3
231 | + fe_mul_tll
232 | ge_p3_dbl
233 | ge_p3_to_p2
234 | * fe_copy
235 | ge_p2_dbl
236 | + fe_sq_tt
237 | fe_sq2_tt
238 | + fe_add
239 | + fe_sq_tl
240 | + fe_sub
241 | + fe_carry
242 | + fe_sub
243 | x25519_ge_p1p1_to_p2
244 | + fe_mul_tll
245
246 x25519_ge_frombytes_vartime
247 | + fe_frombytes
248 | * fe_1
249 | + fe_sq_tt
250 | + fe_mul_ttt
251 | + fe_sub
252 | + fe_carry
253 | + fe_add
254 | + fe_sq_tl
255 | + fe_mul_ttl
256 | * fe_pow22523
257 | fe_isnonzero
258 | fe_isnegative
259
260 ge_double_scalarmult_vartime
261 | * slide
262 | x25519_ge_p3_to_cached
263 | + fe_add
264 | + fe_sub
265 | * fe_copy_lt
266 | + fe_mul_llt
267 | ge_p3_dbl
268 | ge_p3_to_p2
269 | * fe_copy
270 | ge_p2_dbl
271 | + fe_sq_tt
272 | fe_sq2_tt
273 | + fe_add
274 | + fe_sq_tl
275 | + fe_sub
276 | + fe_carry
277 | + fe_sub
278 | x25519_ge_p1p1_to_p3
279 | + fe_mul_tll
280 | x25519_ge_add
281 | + fe_add
282 | + fe_sub
283 | + fe_mul_tll
284 | + fe_mul_tlt
285 | + fe_mul_ttl
286 | + fe_carry
287 | x25519_ge_sub
288 | + fe_add
289 | + fe_sub
290 | + fe_mul_tll
291 | + fe_mul_tlt
292 | + fe_mul_ttl
293 | + fe_carry
294 | ge_p2_dbl
295 | + fe_sq_tt
296 | fe_sq2_tt
297 | + fe_add
298 | + fe_sq_tl
299 | + fe_sub
300 | + fe_carry
301 | + fe_sub
302 | ge_madd
303 | + fe_add
304 | + fe_sub
305 | + fe_mul_tll
306 | + fe_carry
307 | ge_msub
308 | + fe_add
309 | + fe_sub
310 | + fe_mul_tll
311 | + fe_mul_tlt
312 | + fe_carry
313
314 x25519_ge_tobytes
315 | fe_invert
316 | + fe_mul_ttt
317 | + fe_tobytes
318 | fe_isnegative
319
320 ge_p3_tobytes
321 | fe_invert
322 | + fe_mul_ttt
323 | + fe_tobytes
324 | fe_isnegative
325 ```
326
327 # Field Operations
328
329 Mostly, these are complete. However, there are a couple that aren't and seem
330 easy to verify given our existing proven operations.
331
332 The functions that are just wrappers (with bounds assertions) for calls to
333 fiat-crypto generated code are: `fe_frombytes_strict` `fe_tobytes` `fe_add`
334 `fe_sub` `fe_carry` `fe_mul_impl` `fe_sq_tl` `fe_sq_tt` `fe_mul_121666`
335 `fe_neg`
336
337 Additionally, the following functions are fairly trivial wrappers for the ones
338 listed above: `fe_frombytes` `fe_mul_ltt` `fe_mul_llt` `fe_mul_ttt`
339 `fe_mul_tlt` `fe_mul_ttl` `fe_mul_tll`
340
341 There's a bit of a special case with`fe_cmov`; it should use fiat-crypto's
342 `selectznz`, but there's a TODO in the codebase to make the calling conventions
343 match up.
344
345 That leaves the following field operations:
346
347 - `fe_0` `fe_loose_0` `fe_1` `fe_loose_1` : literal 0s and 1s
348 - `fe_cswap`: conditional swap of field elements
349 - `fe_copy` `fe_copy_lt` `fe_copy_ll`: copying content of field elements
350 - `fe_loose_invert` : inversion (there's a PR on fiat-crypto that does inversion -- is it the same algorithm?)
351 - `fe_invert` : inversion (wrapper for `fe_loose_invert`)
352 - `fe_isnonzero` : converts to bytes and checks if the bytes are 0
353 - `fe_isnegative` : converts to bytes and checks if odd
354 - `fe_sq2_tt` : calls `fe_sq_tt` and adds result to itself
355 - `fe_pow22523`: sequence of squares and multiplications
356
357 All of these should be very reasonable to write in bedrock2 and prove correct
358 against existing field operations (with the exception of inversion, if the PR
359 is a different algorithm). In terms of value added by verification,
360 `fe_pow22523` is probably the best, while `fe_sq2_tt`, `fe_isnonzero`, and
361 `fe_isnegative` would be reasonable.
362
363 ### Code graph
364
365 Operations not listed here have no callees at the field level or above.
366
367 ```
368 fe_sq2_tt
369 | + fe_sq_tt
370 | + fe_add
371 | + fe_carry
372
373 fe_invert
374 | * fe_copy_lt
375 | fe_loose_invert
376 | + fe_sq_tl
377 | + fe_sq_tt
378 | + fe_mul_tlt
379 | + fe_mul_ttt
380
381 fe_isnonzero
382 | + fe_carry
383 | + fe_tobytes
384
385 fe_isnegative
386 | + fe_tobytes
387
388 fe_pow22523
389 | + fe_sq_tt
390 | + fe_mul_ttt
391 ```
392
393 # Misc Operations
394
395 The group level relies on a few self-contained helper functions:
396
397 - `slide` is probably something to do with "sliding window"; it transforms an
398 array of bytes with low-level computations. Unclear what the specification
399 should be, but otherwise reasonable to write up in bedrock2.
400 - `negative` computes whether a signed byte is negative (not suitable for
401 bedrock2, probably, because it involves typecasting to larger integers)
402 - `equal` computes whether two signed bytes are equal (not suitable for
403 bedrock2, probably, because it involves typecasting to larger integers)
404