Software APIs
hardened_memory.c
1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
6
10
11// NOTE: The three hardened_mem* functions have similar contents, but the parts
12// that are shared between them are commented only in `memcpy()`.
13status_t hardened_memcpy(uint32_t *restrict dest, const uint32_t *restrict src,
14 size_t word_len) {
15 random_order_t order;
16 random_order_init(&order, word_len);
17
18 size_t count = 0;
19
20 // Immediately convert `src` and `dest` to addresses, which erases their
21 // provenance and causes their addresses to be exposed (in the provenance
22 // sense).
23 uintptr_t src_addr = (uintptr_t)src;
24 uintptr_t dest_addr = (uintptr_t)dest;
25
26 // We need to launder `count`, so that the SW.LOOP-COMPLETION check is not
27 // deleted by the compiler.
28 for (; launderw(count) < word_len; count = launderw(count) + 1) {
29 // The order values themselves are in units of words, but we need `byte_idx`
30 // to be in units of bytes.
31 //
32 // The value obtained from `advance()` is laundered, to prevent
33 // implementation details from leaking across procedures.
34 size_t byte_idx = launderw(random_order_advance(&order)) * sizeof(uint32_t);
35
36 // Prevent the compiler from reordering the loop; this ensures a
37 // happens-before among indices consistent with `order`.
38 barrierw(byte_idx);
39
40 // Calculate pointers.
41 void *src_word = (void *)launderw(src_addr + byte_idx);
42 void *dest_word = (void *)launderw(dest_addr + byte_idx);
43
44 // Perform the copy, without performing a typed dereference operation.
45 write_32(read_32(src_word), dest_word);
46 }
48 HARDENED_CHECK_EQ(count, word_len);
49
50 return (status_t){.value = (int32_t)launder32((uint32_t)OTCRYPTO_OK.value)};
51}
52
53void hardened_memshred(uint32_t *dest, size_t word_len) {
54 random_order_t order;
55 random_order_init(&order, word_len);
56
57 size_t count = 0;
58
59 uintptr_t data_addr = (uintptr_t)dest;
60
61 for (; count < word_len; count = launderw(count) + 1) {
62 size_t byte_idx = launderw(random_order_advance(&order)) * sizeof(uint32_t);
63 barrierw(byte_idx);
64
65 // Calculate pointer.
66 void *data = (void *)launderw(data_addr + byte_idx);
67
68 // Write a freshly-generated random word to `*data`.
69 write_32(hardened_memshred_random_word(), data);
70 }
72
73 HARDENED_CHECK_EQ(count, word_len);
74}
75
76hardened_bool_t hardened_memeq(const uint32_t *lhs, const uint32_t *rhs,
77 size_t word_len) {
78 random_order_t order;
79 random_order_init(&order, word_len);
80
81 size_t count = 0;
82
83 uintptr_t lhs_addr = (uintptr_t)lhs;
84 uintptr_t rhs_addr = (uintptr_t)rhs;
85
86 uint32_t zeros = launder32(0);
87 uint32_t ones = launder32(UINT32_MAX);
88
89 // The loop is almost token-for-token the one above, but the copy is
90 // replaced with something else.
91 for (; count < word_len; count = launderw(count) + 1) {
92 size_t byte_idx = launderw(random_order_advance(&order)) * sizeof(uint32_t);
93 barrierw(byte_idx);
94
95 // Calculate pointers.
96 void *av = (void *)launderw(lhs_addr + byte_idx);
97 void *bv = (void *)launderw(rhs_addr + byte_idx);
98
99 uint32_t a = read_32(av);
100 uint32_t b = read_32(bv);
101
102 // Launder one of the operands, so that the compiler cannot cache the result
103 // of the xor for use in the next operation.
104 //
105 // We launder `zeroes` so that compiler cannot learn that `zeroes` has
106 // strictly more bits set at the end of the loop.
107 zeros = launder32(zeros) | (launder32(a) ^ b);
108
109 // Same as above. The compiler can cache the value of `a[offset]`, but it
110 // has no chance to strength-reduce this operation.
111 ones = launder32(ones) & (launder32(a) ^ ~b);
112 }
114
115 HARDENED_CHECK_EQ(count, word_len);
116 HARDENED_CHECK_NE(lhs_addr, rhs_addr);
117 HARDENED_CHECK_NE(word_len, 0);
118 uint32_t diff = launder32(zeros) | launder32(~ones);
119 if (launder32(diff) == 0) {
120 HARDENED_CHECK_EQ(ones, UINT32_MAX);
121 return launderw(kHardenedBoolTrue ^ diff);
122 }
123
124 HARDENED_CHECK_NE(ones, UINT32_MAX);
125 return kHardenedBoolFalse;
126}
127
128hardened_bool_t consttime_memeq_byte(const void *lhs, const void *rhs,
129 size_t len) {
130 uint32_t zeros = launder32(0);
131 uint32_t ones = launder32(UINT32_MAX);
132
133 random_order_t order;
134 random_order_init(&order, len);
135
136 size_t count = 0;
137
138 uintptr_t lhs_addr = (uintptr_t)lhs;
139 uintptr_t rhs_addr = (uintptr_t)rhs;
140
141 for (; launderw(count) < len; count = launderw(count) + 1) {
142 size_t byte_idx = launderw(random_order_advance(&order));
143 barrierw(byte_idx);
144
145 uint8_t *a = (uint8_t *)launderw(lhs_addr + byte_idx);
146 uint8_t *b = (uint8_t *)launderw(rhs_addr + byte_idx);
147
148 // Launder one of the operands, so that the compiler cannot cache the result
149 // of the xor for use in the next operation.
150 //
151 // We launder `zeroes` so that compiler cannot learn that `zeroes` has
152 // strictly more bits set at the end of the loop.
153 zeros = launder32(zeros) | (launder32((uint32_t)*a) ^ *b);
154
155 // Same as above. The compiler can cache the value of `a[offset]`, but it
156 // has no chance to strength-reduce this operation.
157 ones = launder32(ones) & (launder32((uint32_t)*a) ^ ~(uint32_t)*b);
158 }
159
161 HARDENED_CHECK_EQ(count, len);
162
163 uint32_t diff = launder32(zeros) | launder32(~ones);
164 if (launder32(diff) == 0) {
165 HARDENED_CHECK_EQ(ones, UINT32_MAX);
166 return launderw(kHardenedBoolTrue ^ diff);
167 }
168
169 HARDENED_CHECK_NE(ones, UINT32_MAX);
170 return kHardenedBoolFalse;
171}
172
173status_t hardened_xor(const uint32_t *restrict x, const uint32_t *restrict y,
174 size_t word_len, uint32_t *restrict dest) {
175 // Randomize the content of the output buffer before writing to it.
176 hardened_memshred(dest, word_len);
177
178 // Create a random variable rand.
179 uint32_t rand[word_len];
180 hardened_memshred(rand, word_len);
181
182 // Cast pointers to `uintptr_t` to erase their provenance.
183 uintptr_t x_addr = (uintptr_t)x;
184 uintptr_t y_addr = (uintptr_t)y;
185 uintptr_t dest_addr = (uintptr_t)dest;
186 uintptr_t rand_addr = (uintptr_t)&rand;
187
188 // Generate a random ordering.
189 random_order_t order;
190 random_order_init(&order, word_len);
191 size_t count = 0;
192
193 // XOR the mask with the first share. This loop is modelled off the one in
194 // `hardened_memcpy`; see the comments there for more details.
195 for (; launderw(count) < word_len; count = launderw(count) + 1) {
196 size_t byte_idx = launderw(random_order_advance(&order)) * sizeof(uint32_t);
197
198 // Prevent the compiler from re-ordering the loop.
199 barrierw(byte_idx);
200
201 // Calculate pointers.
202 uintptr_t xp = x_addr + byte_idx;
203 uintptr_t yp = y_addr + byte_idx;
204 uintptr_t destp = dest_addr + byte_idx;
205 uintptr_t randp = rand_addr + byte_idx;
206
207 // Set the pointers.
208 void *xv = (void *)launderw(xp);
209 void *yv = (void *)launderw(yp);
210 void *destv = (void *)launderw(destp);
211 void *randv = (void *)launderw(randp);
212
213 // Perform the XORs: dest = ((x ^ rand) ^ y) ^ rand
214 write_32(read_32(xv) ^ read_32(randv), destv);
215 write_32(read_32(destv) ^ read_32(yv), destv);
216 write_32(read_32(destv) ^ read_32(randv), destv);
217 }
219 HARDENED_CHECK_EQ(count, word_len);
220
221 return (status_t){.value = (int32_t)launder32((uint32_t)OTCRYPTO_OK.value)};
222}
223
224status_t hardened_xor_in_place(uint32_t *restrict x, const uint32_t *restrict y,
225 size_t word_len) {
226 // Generate a random ordering.
227 random_order_t order;
228 random_order_init(&order, word_len);
229 size_t count = 0;
230
231 // Cast pointers to `uintptr_t` to erase their provenance.
232 uintptr_t x_addr = (uintptr_t)x;
233 uintptr_t y_addr = (uintptr_t)y;
234
235 // XOR the mask with the first share. This loop is modelled off the one in
236 // `hardened_memcpy`; see the comments there for more details.
237 for (; launderw(count) < word_len; count = launderw(count) + 1) {
238 size_t byte_idx = launderw(random_order_advance(&order)) * sizeof(uint32_t);
239
240 // Prevent the compiler from re-ordering the loop.
241 barrierw(byte_idx);
242
243 // Calculate pointers.
244 void *xv = (void *)launderw(x_addr + byte_idx);
245 void *yv = (void *)launderw(y_addr + byte_idx);
246
247 // Perform an XOR in the array.
248 write_32(read_32(xv) ^ read_32(yv), xv);
249 }
251 HARDENED_CHECK_EQ(count, word_len);
252
253 return (status_t){.value = (int32_t)launder32((uint32_t)OTCRYPTO_OK.value)};
254}
255
256status_t randomized_bytecopy(void *restrict dest, const void *restrict src,
257 size_t byte_len) {
258 random_order_t order;
259 random_order_init(&order, byte_len);
260
261 size_t count = 0;
262
263 uintptr_t src_addr = (uintptr_t)src;
264 uintptr_t dest_addr = (uintptr_t)dest;
265
266 for (; launderw(count) < byte_len; count = launderw(count) + 1) {
267 size_t byte_idx = launderw(random_order_advance(&order));
268 barrierw(byte_idx);
269
270 uint8_t *src_byte_idx = (uint8_t *)launderw(src_addr + byte_idx);
271 uint8_t *dest_byte_idx = (uint8_t *)launderw(dest_addr + byte_idx);
272
273 *(dest_byte_idx) = *(src_byte_idx);
274 }
276 HARDENED_CHECK_EQ(count, byte_len);
277
278 return (status_t){.value = (int32_t)launder32((uint32_t)OTCRYPTO_OK.value)};
279}
280
281status_t randomized_bytexor_in_place(void *restrict x, const void *restrict y,
282 size_t byte_len) {
283 random_order_t order;
284 random_order_init(&order, byte_len);
285
286 size_t count = 0;
287
288 uintptr_t x_addr = (uintptr_t)x;
289 uintptr_t y_addr = (uintptr_t)y;
290
291 for (; launderw(count) < byte_len; count = launderw(count) + 1) {
292 size_t byte_idx = launderw(random_order_advance(&order));
293 barrierw(byte_idx);
294
295 // TODO(#8815) byte writes vs. word-wise integrity
296 uint8_t *x_byte_idx = (uint8_t *)launderw(x_addr + byte_idx);
297 uint8_t *y_byte_idx = (uint8_t *)launderw(y_addr + byte_idx);
298
299 *(x_byte_idx) = *(x_byte_idx) ^ *(y_byte_idx);
300 }
302 HARDENED_CHECK_EQ(count, byte_len);
303
304 return (status_t){.value = (int32_t)launder32((uint32_t)OTCRYPTO_OK.value)};
305}
306
307#ifdef OT_PLATFORM_RV32
308/**
309 * Call the RISC-V addition with carry.
310 *
311 * @param x First input of the addition.
312 * @param y Second input of the addition.
313 * @param carry The carry-in, updates to the carry-out.
314 * @return The addition of x, y, and carry.
315 */
316static inline uint32_t rv32_addc(uint32_t x, uint32_t y, uint32_t *carry) {
317 uint32_t res, next_carry, c1, c2;
318 __asm__ __volatile__(
319 "add %[res], %[x], %[c_in]\n\t"
320 "sltu %[c1], %[res], %[c_in]\n\t"
321 "add %[res], %[res], %[y]\n\t"
322 "sltu %[c2], %[res], %[y]\n\t"
323 "or %[next_c], %[c1], %[c2]"
324 : [res] "=&r"(res), [next_c] "=&r"(next_carry), [c1] "=&r"(c1),
325 [c2] "=&r"(c2)
326 : [x] "r"(x), [y] "r"(y), [c_in] "r"(*carry));
327 *carry = next_carry;
328 return res;
329}
330
331/**
332 * Call the RISC-V subtraction with borrow.
333 *
334 * @param x First input of the subtraction.
335 * @param y Second input of the subtraction.
336 * @param borrow The borrow-in, updates to the borrow-out.
337 * @return The subtraction of x, y, and borrow.
338 */
339static inline uint32_t rv32_subc(uint32_t x, uint32_t y, uint32_t *borrow) {
340 uint32_t res, next_borrow, b1, b2;
341 __asm__ __volatile__(
342 "sltu %[b1], %[x], %[b_in]\n\t"
343 "sub %[res], %[x], %[b_in]\n\t"
344 "sltu %[b2], %[res], %[y]\n\t"
345 "sub %[res], %[res], %[y]\n\t"
346 "or %[next_b], %[b1], %[b2]"
347 : [res] "=&r"(res), [next_b] "=&r"(next_borrow), [b1] "=&r"(b1),
348 [b2] "=&r"(b2)
349 : [x] "r"(x), [y] "r"(y), [b_in] "r"(*borrow));
350 *borrow = next_borrow;
351 return res;
352}
353
354/**
355 * Call the RISC-V select.
356 *
357 *
358 * @param a First input of the select.
359 * @param b Second input of the select.
360 * @param cond The condition to select.
361 * @return `a` if `cond == 1`, or `b` if `cond == 0`.
362 */
363static inline uint32_t rv32_sel(uint32_t cond, uint32_t a, uint32_t b) {
364 uint32_t res, mask, tmp;
365 __asm__ __volatile__(
366 "neg %[mask], %[cond]\n\t" // mask = 0 - cond (0xFFFFFFFF if 1,
367 // 0x00000000 if 0)
368 "xor %[tmp], %[a], %[b]\n\t" // tmp = a ^ b
369 "and %[tmp], %[tmp], %[mask]\n\t" // tmp = (a ^ b) & mask
370 "xor %[res], %[b], %[tmp]" // out = b ^ ((a ^ b) & mask)
371 : [res] "=r"(res), [mask] "=&r"(mask), [tmp] "=&r"(tmp)
372 : [cond] "r"(cond), [a] "r"(a), [b] "r"(b));
373 return res;
374}
375#endif
376
377status_t hardened_add(const uint32_t *restrict x, const uint32_t *restrict y,
378 size_t word_len, uint32_t *restrict dest) {
379 // Randomize the content of the output buffer before writing to it.
380 hardened_memshred(dest, word_len);
381
382 uint32_t carry = 0;
383 size_t count = 0;
384
385 for (; launderw(count) < word_len; count = launderw(count) + 1) {
386#ifdef OT_PLATFORM_RV32
387 dest[count] = rv32_addc(x[count], y[count], &carry);
388#else
389 uint32_t x_val = x[count];
390 uint32_t y_val = y[count];
391
392 uint32_t res = x_val + carry;
393 uint32_t next_carry = (res < carry);
394
395 res += y_val;
396 next_carry += (res < y_val);
397
398 dest[count] = res;
399 carry = next_carry;
400#endif
401 }
402 HARDENED_CHECK_EQ(count, word_len);
403
404 return (status_t){.value = (int32_t)launder32((uint32_t)OTCRYPTO_OK.value)};
405}
406
407status_t hardened_sub(const uint32_t *restrict x, const uint32_t *restrict y,
408 size_t word_len, uint32_t *restrict dest) {
409 // Randomize the content of the output buffer before writing to it.
410 hardened_memshred(dest, word_len);
411
412 uint32_t borrow = 0;
413 size_t count = 0;
414
415 for (; launderw(count) < word_len; count = launderw(count) + 1) {
416#ifdef OT_PLATFORM_RV32
417 dest[count] = rv32_subc(x[count], y[count], &borrow);
418#else
419 uint32_t x_val = x[count];
420 uint32_t y_val = y[count];
421
422 uint32_t res = x_val - borrow;
423
424 uint32_t next_borrow = (x_val < borrow);
425
426 next_borrow += (res < y_val);
427 res -= y_val;
428
429 dest[count] = res;
430 borrow = next_borrow;
431#endif
432 }
433 HARDENED_CHECK_EQ(count, word_len);
434
435 return (status_t){.value = (int32_t)launder32((uint32_t)OTCRYPTO_OK.value)};
436}
437
438status_t hardened_sub_mod(const uint32_t *restrict x,
439 const uint32_t *restrict y,
440 const uint32_t *restrict n, size_t word_len,
441 uint32_t *restrict dest) {
442 // Randomize the content of the output buffer before writing to it.
443 hardened_memshred(dest, word_len);
444
445 // temp_sub = x - y
446 uint32_t temp_sub[word_len];
447 uint32_t borrow = 0;
448 size_t count = 0;
449 for (; launderw(count) < word_len; count = launderw(count) + 1) {
450#ifdef OT_PLATFORM_RV32
451 temp_sub[count] = rv32_subc(x[count], y[count], &borrow);
452#else
453 uint32_t x_val = x[count];
454 uint32_t y_val = y[count];
455 uint32_t res = x_val - borrow;
456 uint32_t next_borrow = (x_val < borrow);
457 next_borrow += (res < y_val);
458 res -= y_val;
459 temp_sub[count] = res;
460 borrow = next_borrow;
461#endif
462 }
463 HARDENED_CHECK_EQ(count, word_len);
464
465 // temp_add = temp_sub + n
466 uint32_t temp_add[word_len];
467 uint32_t carry = 0;
468 count = 0;
469 for (; launderw(count) < word_len; count = launderw(count) + 1) {
470#ifdef OT_PLATFORM_RV32
471 temp_add[count] = rv32_addc(temp_sub[count], n[count], &carry);
472#else
473 uint32_t x_val = temp_sub[count];
474 uint32_t y_val = n[count];
475 uint32_t res = x_val + carry;
476 uint32_t next_carry = (res < carry);
477 res += y_val;
478 next_carry += (res < y_val);
479 temp_add[count] = res;
480 carry = next_carry;
481#endif
482 }
483 HARDENED_CHECK_EQ(count, word_len);
484
485 // If borrow is 1, choose temp_add, otherwise choose temp_sub.
486 uint32_t is_borrow = launder32(borrow);
487
488 count = 0;
489 for (; launderw(count) < word_len; count = launderw(count) + 1) {
490#ifdef OT_PLATFORM_RV32
491 dest[count] = rv32_sel(is_borrow, temp_add[count], temp_sub[count]);
492#else
493 // The mask is all 1s if borrow is 1, and all 0s if borrow is 0.
494 uint32_t mask = ~(is_borrow - 1);
495 // Prevent optimizations of mask.
496 mask = launder32(mask);
497 dest[count] = (temp_add[count] & launder32(mask)) |
498 (temp_sub[count] & launder32(~mask));
499#endif
500 }
501 HARDENED_CHECK_EQ(count, word_len);
502
503 return (status_t){.value = (int32_t)launder32((uint32_t)OTCRYPTO_OK.value)};
504}
505
506status_t hardened_add_mod(const uint32_t *restrict x,
507 const uint32_t *restrict y,
508 const uint32_t *restrict n, size_t word_len,
509 uint32_t *restrict dest) {
510 // Randomize the content of the output buffer before writing to it.
511 hardened_memshred(dest, word_len);
512
513 // temp_add = x + y
514 uint32_t temp_add[word_len];
515 uint32_t carry = 0;
516 size_t count = 0;
517 for (; launderw(count) < word_len; count = launderw(count) + 1) {
518#ifdef OT_PLATFORM_RV32
519 temp_add[count] = rv32_addc(x[count], y[count], &carry);
520#else
521 uint32_t x_val = x[count];
522 uint32_t y_val = y[count];
523 uint32_t res = x_val + carry;
524 uint32_t next_carry = (res < carry);
525 res += y_val;
526 next_carry += (res < y_val);
527 temp_add[count] = res;
528 carry = next_carry;
529#endif
530 }
531 HARDENED_CHECK_EQ(count, word_len);
532
533 // temp_sub = temp_add - n
534 uint32_t temp_sub[word_len];
535 uint32_t borrow = 0;
536 count = 0;
537 for (; launderw(count) < word_len; count = launderw(count) + 1) {
538#ifdef OT_PLATFORM_RV32
539 temp_sub[count] = rv32_subc(temp_add[count], n[count], &borrow);
540#else
541 uint32_t x_val = temp_add[count];
542 uint32_t y_val = n[count];
543 uint32_t res = x_val - borrow;
544 uint32_t next_borrow = (x_val < borrow);
545 next_borrow += (res < y_val);
546 res -= y_val;
547 temp_sub[count] = res;
548 borrow = next_borrow;
549#endif
550 }
551 HARDENED_CHECK_EQ(count, word_len);
552
553 uint32_t is_ge = launder32(carry) | (1 - launder32(borrow));
554
555 count = 0;
556 for (; launderw(count) < word_len; count = launderw(count) + 1) {
557#ifdef OT_PLATFORM_RV32
558 dest[count] = rv32_sel(is_ge, temp_sub[count], temp_add[count]);
559#else
560 uint32_t mask = ~(is_ge - 1);
561 // Prevent optimizations of mask
562 mask = launder32(mask);
563 dest[count] = (temp_sub[count] & launder32(mask)) |
564 (temp_add[count] & launder32(~mask));
565#endif
566 }
567 HARDENED_CHECK_EQ(count, word_len);
568
569 return (status_t){.value = (int32_t)launder32((uint32_t)OTCRYPTO_OK.value)};
570}
571
572status_t hardened_range_check(const uint32_t *value, const uint32_t *N,
573 size_t word_len) {
574 uint32_t borrow = 0;
575 uint32_t is_zero_acc = 0;
576 size_t count = 0;
577
578 for (; launderw(count) < word_len; count = launderw(count) + 1) {
579 uint32_t val_word = value[count];
580 uint32_t n_word = N[count];
581
582 // Accumulate bits to check if value is zero.
583 is_zero_acc = launder32(is_zero_acc) | launder32(val_word);
584
585#ifdef OT_PLATFORM_RV32
586 (void)rv32_subc(val_word, n_word, &borrow);
587#else
588 // Compute borrow to check if value < N.
589 uint32_t res = val_word - borrow;
590 uint32_t next_borrow = (val_word < borrow);
591 next_borrow += (res < n_word);
592
593 borrow = next_borrow;
594#endif
595 }
596 HARDENED_CHECK_EQ(count, word_len);
597
598 uint32_t is_zero = (launder32(is_zero_acc) == 0);
599 uint32_t is_greater_or_eq = (launder32(borrow) == 0);
600 uint32_t is_bad = launder32(is_zero) | launder32(is_greater_or_eq);
601
602 if (launder32(is_bad) != 0) {
603 return (status_t){
604 .value = (int32_t)launder32((uint32_t)OTCRYPTO_BAD_ARGS.value)};
605 }
606 HARDENED_CHECK_EQ(is_bad, 0);
607
608 return (status_t){.value = (int32_t)launder32((uint32_t)OTCRYPTO_OK.value)};
609}
610
611status_t hardened_mod_reduce(const uint32_t *value, const uint32_t *n,
612 size_t word_len, uint32_t *result) {
613 // This function computes modular reduction (value % n).
614 // It implements a constant-time shift-and-subtract division. It iterates
615 // through the bits of the dividend (`value`) from MSB to LSB, shifting them
616 // into a remainder `r`, and subtracting the divisor `n` in constant time.
617
618 // Remainder, twice the size of the modulus to handle the left shift.
619 uint32_t r[2 * word_len];
620 // Intermediate storing of (r - n).
621 uint32_t r_sub[2 * word_len];
622
623 size_t i = 0;
624
625 // Initialize remainder arrays to zero.
626 for (; launderw(i) < 2 * word_len; i = launderw(i) + 1) {
627 r[i] = 0;
628 r_sub[i] = 0;
629 }
630 HARDENED_CHECK_EQ(i, 2 * word_len);
631
632 // Process each bit of `value` from Most Significant Bit (MSB) down to LSB.
633 i = word_len * 32;
634 for (; launderw(i) > 0; i = launderw(i) - 1) {
635 size_t bit_idx = i - 1;
636 size_t word_idx = bit_idx >> 5;
637 size_t bit_in_word = bit_idx % 32;
638
639 // Shift the current remainder `r` left by 1 bit.
640 uint32_t carry = 0;
641 size_t j = 0;
642 for (; launderw(j) < 2 * word_len; j = launderw(j) + 1) {
643 uint32_t next_carry = (r[j] >> 31);
644 r[j] = (r[j] << 1) | carry;
645 carry = next_carry;
646 }
647 HARDENED_CHECK_EQ(j, 2 * word_len);
648
649 // Inject the current top bit of `value` into the LSB of `r`.
650 uint32_t bit = (value[word_idx] >> bit_in_word) & 1;
651 r[0] = r[0] ^ ((r[0] ^ bit) & 1);
652
653 // Compute `r_sub = r - n`.
654 uint32_t borrow = 0;
655 j = 0;
656 for (; launderw(j) < word_len; j = launderw(j) + 1) {
657#ifdef OT_PLATFORM_RV32
658 r_sub[j] = rv32_subc(r[j], n[j], &borrow);
659#else
660 uint32_t r_word = r[j];
661 uint32_t n_word = n[j];
662 uint32_t res = r_word - borrow;
663 uint32_t next_borrow = (r_word < borrow);
664 next_borrow |= (res < n_word);
665 res -= n_word;
666 r_sub[j] = res;
667 borrow = next_borrow;
668#endif
669 }
670 HARDENED_CHECK_EQ(j, word_len);
671
672 // Propagate the borrow through the upper half of r
673 j = word_len;
674 for (; launderw(j) < 2 * word_len; j = launderw(j) + 1) {
675#ifdef OT_PLATFORM_RV32
676 r_sub[j] = rv32_subc(r[j], 0, &borrow);
677#else
678 uint32_t res = r[j] - borrow;
679 borrow = (r[j] < borrow);
680 r_sub[j] = res;
681#endif
682 }
683 HARDENED_CHECK_EQ(j, 2 * word_len);
684
685 // Conditional swap.
686 // If r < n, the final borrow is 1. If r >= n, the final borrow is 0.
687#ifdef OT_PLATFORM_RV32
688 uint32_t cond = launder32(1 - launder32(borrow));
689#else
690 uint32_t mask = borrow - 1;
691 // Prevent compiler optimizations of the mask.
692 mask = launder32(mask);
693#endif
694
695 j = 0;
696 for (; launderw(j) < 2 * word_len; j = launderw(j) + 1) {
697#ifdef OT_PLATFORM_RV32
698 r[j] = rv32_sel(cond, r_sub[j], r[j]);
699#else
700 r[j] = (r_sub[j] & launder32(mask)) | (r[j] & launder32(~mask));
701#endif
702 }
703 HARDENED_CHECK_EQ(j, 2 * word_len);
704 }
705 HARDENED_CHECK_EQ(i, 0);
706
707 // Copy the lower word_len elements of the final remainder into the result
708 // array.
709 TRY(hardened_memcpy(result, r, word_len));
710
711 return (status_t){.value = (int32_t)launder32((uint32_t)OTCRYPTO_OK.value)};
712}