xref: /optee_os/lib/libmbedtls/mbedtls/library/constant_time.c (revision b0563631928755fe864b97785160fb3088e9efdc)
1039e02dfSJerome Forissier /**
2039e02dfSJerome Forissier  *  Constant-time functions
3039e02dfSJerome Forissier  *
4039e02dfSJerome Forissier  *  Copyright The Mbed TLS Contributors
5*b0563631STom Van Eyck  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6039e02dfSJerome Forissier  */
7039e02dfSJerome Forissier 
8039e02dfSJerome Forissier /*
9039e02dfSJerome Forissier  * The following functions are implemented without using comparison operators, as those
10039e02dfSJerome Forissier  * might be translated to branches by some compilers on some platforms.
11039e02dfSJerome Forissier  */
12039e02dfSJerome Forissier 
13*b0563631STom Van Eyck #include <stdint.h>
14*b0563631STom Van Eyck #include <limits.h>
15*b0563631STom Van Eyck 
16039e02dfSJerome Forissier #include "common.h"
17039e02dfSJerome Forissier #include "constant_time_internal.h"
18039e02dfSJerome Forissier #include "mbedtls/constant_time.h"
19039e02dfSJerome Forissier #include "mbedtls/error.h"
20039e02dfSJerome Forissier #include "mbedtls/platform_util.h"
21039e02dfSJerome Forissier 
22039e02dfSJerome Forissier #include <string.h>
23*b0563631STom Van Eyck 
24*b0563631STom Van Eyck #if !defined(MBEDTLS_CT_ASM)
25*b0563631STom Van Eyck /*
26*b0563631STom Van Eyck  * Define an object with the value zero, such that the compiler cannot prove that it
27*b0563631STom Van Eyck  * has the value zero (because it is volatile, it "may be modified in ways unknown to
28*b0563631STom Van Eyck  * the implementation").
29*b0563631STom Van Eyck  */
30*b0563631STom Van Eyck volatile mbedtls_ct_uint_t mbedtls_ct_zero = 0;
3132b31808SJens Wiklander #endif
3232b31808SJens Wiklander 
3332b31808SJens Wiklander /*
3432b31808SJens Wiklander  * Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to
3532b31808SJens Wiklander  * perform fast unaligned access to volatile data.
3632b31808SJens Wiklander  *
3732b31808SJens Wiklander  * This is needed because mbedtls_get_unaligned_uintXX etc don't support volatile
3832b31808SJens Wiklander  * memory accesses.
3932b31808SJens Wiklander  *
4032b31808SJens Wiklander  * Some of these definitions could be moved into alignment.h but for now they are
4132b31808SJens Wiklander  * only used here.
4232b31808SJens Wiklander  */
43*b0563631STom Van Eyck #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && \
44*b0563631STom Van Eyck     ((defined(MBEDTLS_CT_ARM_ASM) && (UINTPTR_MAX == 0xfffffffful)) || \
45*b0563631STom Van Eyck     defined(MBEDTLS_CT_AARCH64_ASM))
46*b0563631STom Van Eyck /* We check pointer sizes to avoid issues with them not matching register size requirements */
4732b31808SJens Wiklander #define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS
4832b31808SJens Wiklander 
mbedtls_get_unaligned_volatile_uint32(volatile const unsigned char * p)4932b31808SJens Wiklander static inline uint32_t mbedtls_get_unaligned_volatile_uint32(volatile const unsigned char *p)
5032b31808SJens Wiklander {
5132b31808SJens Wiklander     /* This is UB, even where it's safe:
5232b31808SJens Wiklander      *    return *((volatile uint32_t*)p);
5332b31808SJens Wiklander      * so instead the same thing is expressed in assembly below.
5432b31808SJens Wiklander      */
5532b31808SJens Wiklander     uint32_t r;
56*b0563631STom Van Eyck #if defined(MBEDTLS_CT_ARM_ASM)
5732b31808SJens Wiklander     asm volatile ("ldr %0, [%1]" : "=r" (r) : "r" (p) :);
58*b0563631STom Van Eyck #elif defined(MBEDTLS_CT_AARCH64_ASM)
59*b0563631STom Van Eyck     asm volatile ("ldr %w0, [%1]" : "=r" (r) : MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT(p) :);
60*b0563631STom Van Eyck #else
61*b0563631STom Van Eyck #error "No assembly defined for mbedtls_get_unaligned_volatile_uint32"
6232b31808SJens Wiklander #endif
6332b31808SJens Wiklander     return r;
6432b31808SJens Wiklander }
65*b0563631STom Van Eyck #endif /* defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) &&
66*b0563631STom Van Eyck           (defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM)) */
67039e02dfSJerome Forissier 
mbedtls_ct_memcmp(const void * a,const void * b,size_t n)68039e02dfSJerome Forissier int mbedtls_ct_memcmp(const void *a,
69039e02dfSJerome Forissier                       const void *b,
70039e02dfSJerome Forissier                       size_t n)
71039e02dfSJerome Forissier {
7232b31808SJens Wiklander     size_t i = 0;
7332b31808SJens Wiklander     /*
7432b31808SJens Wiklander      * `A` and `B` are cast to volatile to ensure that the compiler
7532b31808SJens Wiklander      * generates code that always fully reads both buffers.
7632b31808SJens Wiklander      * Otherwise it could generate a test to exit early if `diff` has all
7732b31808SJens Wiklander      * bits set early in the loop.
7832b31808SJens Wiklander      */
79039e02dfSJerome Forissier     volatile const unsigned char *A = (volatile const unsigned char *) a;
80039e02dfSJerome Forissier     volatile const unsigned char *B = (volatile const unsigned char *) b;
8132b31808SJens Wiklander     uint32_t diff = 0;
82039e02dfSJerome Forissier 
8332b31808SJens Wiklander #if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS)
8432b31808SJens Wiklander     for (; (i + 4) <= n; i += 4) {
8532b31808SJens Wiklander         uint32_t x = mbedtls_get_unaligned_volatile_uint32(A + i);
8632b31808SJens Wiklander         uint32_t y = mbedtls_get_unaligned_volatile_uint32(B + i);
8732b31808SJens Wiklander         diff |= x ^ y;
8832b31808SJens Wiklander     }
8932b31808SJens Wiklander #endif
9032b31808SJens Wiklander 
9132b31808SJens Wiklander     for (; i < n; i++) {
92039e02dfSJerome Forissier         /* Read volatile data in order before computing diff.
93039e02dfSJerome Forissier          * This avoids IAR compiler warning:
94039e02dfSJerome Forissier          * 'the order of volatile accesses is undefined ..' */
95039e02dfSJerome Forissier         unsigned char x = A[i], y = B[i];
96039e02dfSJerome Forissier         diff |= x ^ y;
97039e02dfSJerome Forissier     }
98039e02dfSJerome Forissier 
99*b0563631STom Van Eyck 
100*b0563631STom Van Eyck #if (INT_MAX < INT32_MAX)
101*b0563631STom Van Eyck     /* We don't support int smaller than 32-bits, but if someone tried to build
102*b0563631STom Van Eyck      * with this configuration, there is a risk that, for differing data, the
103*b0563631STom Van Eyck      * only bits set in diff are in the top 16-bits, and would be lost by a
104*b0563631STom Van Eyck      * simple cast from uint32 to int.
105*b0563631STom Van Eyck      * This would have significant security implications, so protect against it. */
106*b0563631STom Van Eyck #error "mbedtls_ct_memcmp() requires minimum 32-bit ints"
107*b0563631STom Van Eyck #else
108*b0563631STom Van Eyck     /* The bit-twiddling ensures that when we cast uint32_t to int, we are casting
109*b0563631STom Van Eyck      * a value that is in the range 0..INT_MAX - a value larger than this would
110*b0563631STom Van Eyck      * result in implementation defined behaviour.
111*b0563631STom Van Eyck      *
112*b0563631STom Van Eyck      * This ensures that the value returned by the function is non-zero iff
113*b0563631STom Van Eyck      * diff is non-zero.
114*b0563631STom Van Eyck      */
115*b0563631STom Van Eyck     return (int) ((diff & 0xffff) | (diff >> 16));
116*b0563631STom Van Eyck #endif
117*b0563631STom Van Eyck }
118*b0563631STom Van Eyck 
119*b0563631STom Van Eyck #if defined(MBEDTLS_NIST_KW_C)
120*b0563631STom Van Eyck 
mbedtls_ct_memcmp_partial(const void * a,const void * b,size_t n,size_t skip_head,size_t skip_tail)121*b0563631STom Van Eyck int mbedtls_ct_memcmp_partial(const void *a,
122*b0563631STom Van Eyck                               const void *b,
123*b0563631STom Van Eyck                               size_t n,
124*b0563631STom Van Eyck                               size_t skip_head,
125*b0563631STom Van Eyck                               size_t skip_tail)
126*b0563631STom Van Eyck {
127*b0563631STom Van Eyck     unsigned int diff = 0;
128*b0563631STom Van Eyck 
129*b0563631STom Van Eyck     volatile const unsigned char *A = (volatile const unsigned char *) a;
130*b0563631STom Van Eyck     volatile const unsigned char *B = (volatile const unsigned char *) b;
131*b0563631STom Van Eyck 
132*b0563631STom Van Eyck     size_t valid_end = n - skip_tail;
133*b0563631STom Van Eyck 
134*b0563631STom Van Eyck     for (size_t i = 0; i < n; i++) {
135*b0563631STom Van Eyck         unsigned char x = A[i], y = B[i];
136*b0563631STom Van Eyck         unsigned int d = x ^ y;
137*b0563631STom Van Eyck         mbedtls_ct_condition_t valid = mbedtls_ct_bool_and(mbedtls_ct_uint_ge(i, skip_head),
138*b0563631STom Van Eyck                                                            mbedtls_ct_uint_lt(i, valid_end));
139*b0563631STom Van Eyck         diff |= mbedtls_ct_uint_if_else_0(valid, d);
140*b0563631STom Van Eyck     }
141*b0563631STom Van Eyck 
142*b0563631STom Van Eyck     /* Since we go byte-by-byte, the only bits set will be in the bottom 8 bits, so the
143*b0563631STom Van Eyck      * cast from uint to int is safe. */
14432b31808SJens Wiklander     return (int) diff;
145039e02dfSJerome Forissier }
146039e02dfSJerome Forissier 
147039e02dfSJerome Forissier #endif
148039e02dfSJerome Forissier 
149039e02dfSJerome Forissier #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
150039e02dfSJerome Forissier 
mbedtls_ct_memmove_left(void * start,size_t total,size_t offset)151*b0563631STom Van Eyck void mbedtls_ct_memmove_left(void *start, size_t total, size_t offset)
152039e02dfSJerome Forissier {
153039e02dfSJerome Forissier     volatile unsigned char *buf = start;
154*b0563631STom Van Eyck     for (size_t i = 0; i < total; i++) {
155*b0563631STom Van Eyck         mbedtls_ct_condition_t no_op = mbedtls_ct_uint_gt(total - offset, i);
156039e02dfSJerome Forissier         /* The first `total - offset` passes are a no-op. The last
157039e02dfSJerome Forissier          * `offset` passes shift the data one byte to the left and
158039e02dfSJerome Forissier          * zero out the last byte. */
159*b0563631STom Van Eyck         for (size_t n = 0; n < total - 1; n++) {
160039e02dfSJerome Forissier             unsigned char current = buf[n];
161039e02dfSJerome Forissier             unsigned char next    = buf[n+1];
162039e02dfSJerome Forissier             buf[n] = mbedtls_ct_uint_if(no_op, current, next);
163039e02dfSJerome Forissier         }
164*b0563631STom Van Eyck         buf[total-1] = mbedtls_ct_uint_if_else_0(no_op, buf[total-1]);
165039e02dfSJerome Forissier     }
166039e02dfSJerome Forissier }
167039e02dfSJerome Forissier 
168039e02dfSJerome Forissier #endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
169039e02dfSJerome Forissier 
mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition,unsigned char * dest,const unsigned char * src1,const unsigned char * src2,size_t len)170*b0563631STom Van Eyck void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition,
171*b0563631STom Van Eyck                           unsigned char *dest,
172*b0563631STom Van Eyck                           const unsigned char *src1,
173*b0563631STom Van Eyck                           const unsigned char *src2,
174*b0563631STom Van Eyck                           size_t len)
175039e02dfSJerome Forissier {
176*b0563631STom Van Eyck #if defined(MBEDTLS_CT_SIZE_64)
177*b0563631STom Van Eyck     const uint64_t mask     = (uint64_t) condition;
178*b0563631STom Van Eyck     const uint64_t not_mask = (uint64_t) ~mbedtls_ct_compiler_opaque(condition);
179*b0563631STom Van Eyck #else
180*b0563631STom Van Eyck     const uint32_t mask     = (uint32_t) condition;
181*b0563631STom Van Eyck     const uint32_t not_mask = (uint32_t) ~mbedtls_ct_compiler_opaque(condition);
182*b0563631STom Van Eyck #endif
183*b0563631STom Van Eyck 
184*b0563631STom Van Eyck     /* If src2 is NULL, setup src2 so that we read from the destination address.
185*b0563631STom Van Eyck      *
186*b0563631STom Van Eyck      * This means that if src2 == NULL && condition is false, the result will be a
187*b0563631STom Van Eyck      * no-op because we read from dest and write the same data back into dest.
188*b0563631STom Van Eyck      */
189*b0563631STom Van Eyck     if (src2 == NULL) {
190*b0563631STom Van Eyck         src2 = dest;
191*b0563631STom Van Eyck     }
192039e02dfSJerome Forissier 
193039e02dfSJerome Forissier     /* dest[i] = c1 == c2 ? src[i] : dest[i] */
19432b31808SJens Wiklander     size_t i = 0;
19532b31808SJens Wiklander #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
196*b0563631STom Van Eyck #if defined(MBEDTLS_CT_SIZE_64)
197*b0563631STom Van Eyck     for (; (i + 8) <= len; i += 8) {
198*b0563631STom Van Eyck         uint64_t a = mbedtls_get_unaligned_uint64(src1 + i) & mask;
199*b0563631STom Van Eyck         uint64_t b = mbedtls_get_unaligned_uint64(src2 + i) & not_mask;
200*b0563631STom Van Eyck         mbedtls_put_unaligned_uint64(dest + i, a | b);
20132b31808SJens Wiklander     }
20232b31808SJens Wiklander #else
203*b0563631STom Van Eyck     for (; (i + 4) <= len; i += 4) {
204*b0563631STom Van Eyck         uint32_t a = mbedtls_get_unaligned_uint32(src1 + i) & mask;
205*b0563631STom Van Eyck         uint32_t b = mbedtls_get_unaligned_uint32(src2 + i) & not_mask;
206*b0563631STom Van Eyck         mbedtls_put_unaligned_uint32(dest + i, a | b);
207*b0563631STom Van Eyck     }
208*b0563631STom Van Eyck #endif /* defined(MBEDTLS_CT_SIZE_64) */
20932b31808SJens Wiklander #endif /* MBEDTLS_EFFICIENT_UNALIGNED_ACCESS */
21032b31808SJens Wiklander     for (; i < len; i++) {
211*b0563631STom Van Eyck         dest[i] = (src1[i] & mask) | (src2[i] & not_mask);
212039e02dfSJerome Forissier     }
21332b31808SJens Wiklander }
214039e02dfSJerome Forissier 
mbedtls_ct_memcpy_offset(unsigned char * dest,const unsigned char * src,size_t offset,size_t offset_min,size_t offset_max,size_t len)215039e02dfSJerome Forissier void mbedtls_ct_memcpy_offset(unsigned char *dest,
216039e02dfSJerome Forissier                               const unsigned char *src,
217039e02dfSJerome Forissier                               size_t offset,
218039e02dfSJerome Forissier                               size_t offset_min,
219039e02dfSJerome Forissier                               size_t offset_max,
220039e02dfSJerome Forissier                               size_t len)
221039e02dfSJerome Forissier {
222039e02dfSJerome Forissier     size_t offsetval;
223039e02dfSJerome Forissier 
22432b31808SJens Wiklander     for (offsetval = offset_min; offsetval <= offset_max; offsetval++) {
225*b0563631STom Van Eyck         mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offsetval, offset), dest, src + offsetval, NULL,
226*b0563631STom Van Eyck                              len);
227039e02dfSJerome Forissier     }
228039e02dfSJerome Forissier }
229039e02dfSJerome Forissier 
230039e02dfSJerome Forissier #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
231039e02dfSJerome Forissier 
mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition,void * buf,size_t len)232*b0563631STom Van Eyck void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len)
233039e02dfSJerome Forissier {
234*b0563631STom Van Eyck     uint32_t mask = (uint32_t) ~condition;
235*b0563631STom Van Eyck     uint8_t *p = (uint8_t *) buf;
236*b0563631STom Van Eyck     size_t i = 0;
237*b0563631STom Van Eyck #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
238*b0563631STom Van Eyck     for (; (i + 4) <= len; i += 4) {
239*b0563631STom Van Eyck         mbedtls_put_unaligned_uint32((void *) (p + i),
240*b0563631STom Van Eyck                                      mbedtls_get_unaligned_uint32((void *) (p + i)) & mask);
241*b0563631STom Van Eyck     }
242*b0563631STom Van Eyck #endif
243*b0563631STom Van Eyck     for (; i < len; i++) {
244*b0563631STom Van Eyck         p[i] = p[i] & mask;
245*b0563631STom Van Eyck     }
246039e02dfSJerome Forissier }
247039e02dfSJerome Forissier 
248*b0563631STom Van Eyck #endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */
249