Embedded SDK
Embedded SDK
cx_utils.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 
19 #ifndef CX_UTILS_H
20 #define CX_UTILS_H
21 
22 #include "lcx_common.h"
23 #include "errors.h"
24 #include "exceptions.h"
25 #include <stddef.h>
26 #include <stdint.h>
27 
28 /* ======================================================================= */
29 /* 32 BITS manipulation */
30 /* ======================================================================= */
31 uint32_t cx_swap_uint32(uint32_t v);
32 void cx_swap_buffer32(uint32_t *v, size_t len);
33 
34 #define _CX_INLINE_U32
35 #define cx_rotl(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
36 #define cx_rotr(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
37 #define cx_shr(x, n) ((x) >> (n))
38 
39 #define htole32(value) ((uint32_t) (value))
40 
41 /* ======================================================================= */
42 /* 64 BITS manipulation */
43 /* ======================================================================= */
44 
45 #ifndef NATIVE_64BITS // NO 64BITS
46 
47 #ifdef ARCH_LITTLE_ENDIAN
48 #define _64BITS(h, l) \
49  { \
50  l, h \
51  }
52 #else
53 #define _64BITS(h, l) \
54  { \
55  h, l \
56  }
57 #endif
58 
59 #define CLR64(x) \
60  (x).l = 0; \
61  (x).h = 0
62 #define ADD64(x, y) cx_add_64(&(x), &(y))
63 #define ASSIGN64(r, x) \
64  (r).l = (x).l; \
65  (r).h = (x).h
66 
67 void cx_rotr64(uint64bits_t *x, unsigned char n);
68 
69 void cx_shr64(uint64bits_t *x, unsigned char n);
70 
73 void cx_swap_buffer64(uint64bits_t *v, int len);
74 
75 #else
76 
77 #define _64BITS(h, l) (h##ULL << 32) | (l##ULL)
78 
79 #define _CX_INLINE_U64
80 
81 #define cx_rotr64(x, n) (((x) >> (n)) | ((x) << ((64) - (n))))
82 
83 #define cx_rotl64(x, n) (((x) << (n)) | ((x) >> ((64) - (n))))
84 
85 #define cx_shr64(x, n) ((x) >> (n))
86 
88 void cx_swap_buffer64(uint64bits_t *v, int len);
89 
90 #endif
91 
92 void cx_memxor(uint8_t *buf1, const uint8_t *buf2, size_t len);
93 
94 uint8_t cx_constant_time_eq(const uint8_t *buf1, uint8_t *buf2, size_t len);
95 
96 #endif // CX_UTILS_H
void cx_swap_buffer64(uint64bits_t *v, int len)
Definition: cx_utils.c:140
void cx_shr64(uint64bits_t *x, unsigned char n)
Definition: cx_utils.c:83
void cx_rotr64(uint64bits_t *x, unsigned char n)
uint32_t cx_swap_uint32(uint32_t v)
Definition: cx_utils.c:41
uint8_t cx_constant_time_eq(const uint8_t *buf1, uint8_t *buf2, size_t len)
Definition: cx_utils.c:181
void cx_swap_uint64(uint64bits_t *v)
Definition: cx_utils.c:118
void cx_memxor(uint8_t *buf1, const uint8_t *buf2, size_t len)
Definition: cx_utils.c:173
void cx_swap_buffer32(uint32_t *v, size_t len)
Definition: cx_utils.c:47
void cx_add_64(uint64bits_t *a, uint64bits_t *b)
Definition: cx_utils.c:156
Cryptography flags.
64-bit types, native or by-hands, depending on target and/or compiler support.
Definition: lcx_common.h:49
unsigned char uint8_t
Definition: usbd_conf.h:53