-
Notifications
You must be signed in to change notification settings - Fork 297
/
util_helpers.c
108 lines (99 loc) · 2.46 KB
/
util_helpers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#if defined(__i386__)
uint32_t rust_crypto_util_supports_aesni() {
#ifdef __SSE__
uint32_t flags;
asm(
"pushl %%ebx; \
mov $1, %%eax; cpuid; \
popl %%ebx;"
: "=c" (flags) // output
: // input
: "eax", "edx" // clobbers
);
return flags & 0x02000000;
#else
return 0;
#endif
}
#endif
#if defined(__x86_64__)
uint32_t rust_crypto_util_supports_aesni() {
#ifdef __SSE__
uint32_t flags;
asm(
"mov $1, %%eax; cpuid;"
: "=c" (flags) // output
: // input
: "eax", "ebx", "edx" // clobbers
);
return flags & 0x02000000;
#else
return 0;
#endif
}
#endif
#if defined(__i386__) || defined(__x86_64__)
uint32_t rust_crypto_util_fixed_time_eq_asm(uint8_t* lhsp, uint8_t* rhsp, size_t count) {
if (count == 0) {
return 1;
}
uint8_t result = 0;
asm(
" \
1: \
\
mov (%1), %%cl; \
xor (%2), %%cl; \
or %%cl, %0; \
\
inc %1; \
inc %2; \
dec %3; \
jnz 1b; \
"
: "+&r" (result), "+&r" (lhsp), "+&r" (rhsp), "+&r" (count) // all input and output
: // input
: "cl", "cc" // clobbers
);
return result;
}
#endif
#ifdef __arm__
uint32_t rust_crypto_util_fixed_time_eq_asm(uint8_t* lhsp, uint8_t* rhsp, size_t count) {
if (count == 0) {
return 1;
}
uint8_t result = 0;
asm(
" \
1: \
\
ldrb r4, [%1]; \
ldrb r5, [%2]; \
eor r4, r4, r5; \
orr %0, %0, r4; \
\
add %1, %1, #1; \
add %2, %2, #1; \
subs %3, %3, #1; \
bne 1b; \
"
: "+&r" (result), "+&r" (lhsp), "+&r" (rhsp), "+&r" (count) // all input and output
: // input
: "r4", "r5", "cc" // clobbers
);
return result;
}
#endif
void rust_crypto_util_secure_memset(uint8_t* dst, uint8_t val, size_t count) {
memset(dst, val, count);
asm volatile("" : : "g" (dst) : "memory");
}