Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
lcx_math.h
Go to the documentation of this file.
1
2/*******************************************************************************
3 * Ledger Nano S - Secure firmware
4 * (c) 2022 Ledger
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 ********************************************************************************/
18
24#ifndef LCX_MATH_H
25#define LCX_MATH_H
26
27#ifdef HAVE_MATH
28
29#include "lcx_wrappers.h"
30#include "ox_bn.h"
31
54WARN_UNUSED_RESULT cx_err_t cx_math_cmp_no_throw(const uint8_t *a,
55 const uint8_t *b,
56 size_t length,
57 int *diff);
58
63DEPRECATED static inline int32_t cx_math_cmp(const uint8_t *a, const uint8_t *b, size_t length)
64{
65 int diff;
66 CX_THROW(cx_math_cmp_no_throw(a, b, length, &diff));
67 return diff;
68}
69
89WARN_UNUSED_RESULT cx_err_t cx_math_add_no_throw(uint8_t *r,
90 const uint8_t *a,
91 const uint8_t *b,
92 size_t len);
93
119static inline uint32_t cx_math_add(uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len)
120{
121 cx_err_t error = cx_math_add_no_throw(r, a, b, len);
122 if (error && error != CX_CARRY) {
123 THROW(error);
124 }
125 return (error == CX_CARRY);
126}
127
147WARN_UNUSED_RESULT cx_err_t cx_math_sub_no_throw(uint8_t *r,
148 const uint8_t *a,
149 const uint8_t *b,
150 size_t len);
151
177static inline uint32_t cx_math_sub(uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len)
178{
179 cx_err_t error = cx_math_sub_no_throw(r, a, b, len);
180 if (error && error != CX_CARRY) {
181 THROW(error);
182 }
183 return (error == CX_CARRY);
184}
185
205WARN_UNUSED_RESULT cx_err_t cx_math_mult_no_throw(uint8_t *r,
206 const uint8_t *a,
207 const uint8_t *b,
208 size_t len);
209
214DEPRECATED static inline void cx_math_mult(uint8_t *r,
215 const uint8_t *a,
216 const uint8_t *b,
217 size_t len)
218{
219 CX_THROW(cx_math_mult_no_throw(r, a, b, len));
220}
221
246WARN_UNUSED_RESULT cx_err_t
247cx_math_addm_no_throw(uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len);
248
253DEPRECATED static inline void cx_math_addm(uint8_t *r,
254 const uint8_t *a,
255 const uint8_t *b,
256 const uint8_t *m,
257 size_t len)
258{
259 CX_THROW(cx_math_addm_no_throw(r, a, b, m, len));
260}
261
286WARN_UNUSED_RESULT cx_err_t
287cx_math_subm_no_throw(uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len);
288
293DEPRECATED static inline void cx_math_subm(uint8_t *r,
294 const uint8_t *a,
295 const uint8_t *b,
296 const uint8_t *m,
297 size_t len)
298{
299 CX_THROW(cx_math_subm_no_throw(r, a, b, m, len));
300}
301
326WARN_UNUSED_RESULT cx_err_t cx_math_multm_no_throw(uint8_t *r,
327 const uint8_t *a,
328 const uint8_t *b,
329 const uint8_t *m,
330 size_t len);
331
336DEPRECATED static inline void cx_math_multm(uint8_t *r,
337 const uint8_t *a,
338 const uint8_t *b,
339 const uint8_t *m,
340 size_t len)
341{
342 CX_THROW(cx_math_multm_no_throw(r, a, b, m, len));
343}
344
366WARN_UNUSED_RESULT cx_err_t cx_math_modm_no_throw(uint8_t *v,
367 size_t len_v,
368 const uint8_t *m,
369 size_t len_m);
370
375DEPRECATED static inline void cx_math_modm(uint8_t *v, size_t len_v, const uint8_t *m, size_t len_m)
376{
377 CX_THROW(cx_math_modm_no_throw(v, len_v, m, len_m));
378}
379
405WARN_UNUSED_RESULT cx_err_t cx_math_powm_no_throw(uint8_t *r,
406 const uint8_t *a,
407 const uint8_t *e,
408 size_t len_e,
409 const uint8_t *m,
410 size_t len);
411
416DEPRECATED static inline void cx_math_powm(uint8_t *r,
417 const uint8_t *a,
418 const uint8_t *e,
419 size_t len_e,
420 const uint8_t *m,
421 size_t len)
422{
423 CX_THROW(cx_math_powm_no_throw(r, a, e, len_e, m, len));
424}
425
447WARN_UNUSED_RESULT cx_err_t cx_math_invprimem_no_throw(uint8_t *r,
448 const uint8_t *a,
449 const uint8_t *m,
450 size_t len);
451
456DEPRECATED static inline void cx_math_invprimem(uint8_t *r,
457 const uint8_t *a,
458 const uint8_t *m,
459 size_t len)
460{
461 CX_THROW(cx_math_invprimem_no_throw(r, a, m, len));
462}
463
486WARN_UNUSED_RESULT cx_err_t cx_math_invintm_no_throw(uint8_t *r,
487 uint32_t a,
488 const uint8_t *m,
489 size_t len);
490
495DEPRECATED static inline void cx_math_invintm(uint8_t *r, uint32_t a, const uint8_t *m, size_t len)
496{
497 CX_THROW(cx_math_invintm_no_throw(r, a, m, len));
498}
499
519WARN_UNUSED_RESULT cx_err_t cx_math_is_prime_no_throw(const uint8_t *r, size_t len, bool *prime);
520
525DEPRECATED static inline bool cx_math_is_prime(const uint8_t *r, size_t len)
526{
527 bool prime;
528 CX_THROW(cx_math_is_prime_no_throw(r, len, &prime));
529 return prime;
530}
531
549WARN_UNUSED_RESULT cx_err_t cx_math_next_prime_no_throw(uint8_t *r, uint32_t len);
550
555DEPRECATED static inline void cx_math_next_prime(uint8_t *r, uint32_t len)
556{
557 CX_THROW(cx_math_next_prime_no_throw(r, len));
558}
559
569static inline bool cx_math_is_zero(const uint8_t *a, size_t len)
570{
571 uint8_t acc = 0; // accumulate all the bytes in order to run in constant time
572 for (size_t i = 0; i < len; i++) {
573 acc |= a[i];
574 }
575 return acc == 0;
576}
577
578#endif // HAVE_MATH
579
580#endif // LCX_MATH_H
#define CX_THROW(call)
unsigned char uint8_t
Definition usbd_conf.h:53