aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/ct_utils.h
blob: 4ae735330d9a75da2921835f4393f5fd3be68adf (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Functions for constant time operations on data and testing of
* constant time annotations using ctgrind.
*
* For more information about constant time programming see
* Wagner, Molnar, et al "The Program Counter Security Model"
*
* (C) 2010 Falko Strenzke
* (C) 2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#ifndef BOTAN_TIMING_ATTACK_CM_H__
#define BOTAN_TIMING_ATTACK_CM_H__

#include <botan/types.h>
#include <vector>

#if defined(BOTAN_USE_CTGRIND)

// These are external symbols from libctgrind.so
extern "C" void ct_poison(const void* address, size_t length);
extern "C" void ct_unpoison(const void* address, size_t length);

#endif

namespace Botan {

#if defined(BOTAN_USE_CTGRIND)

#define BOTAN_CONST_TIME_POISON(p, l) ct_poison(p, l)
#define BOTAN_CONST_TIME_UNPOISON(p, l) ct_unpoison(p, l)

#else

#define BOTAN_CONST_TIME_POISON(p, l)
#define BOTAN_CONST_TIME_UNPOISON(p, l)

#endif

/*
* Expand to a mask used for other operations
* @param in an integer
* @return 0 if in == 0 else 0xFFFFFFFF
*/
inline uint32_t ct_expand_mask_32(uint32_t x)
   {
   // First fold x down to a single bit:
   uint32_t r = x;
   r |= r >> 16;
   r |= r >> 8;
   r |= r >> 4;
   r |= r >> 2;
   r |= r >> 1;
   r &= 1;
   // assumes 2s complement signed representation
   r = ~(r - 1);
   return r;
   }

inline uint32_t ct_select_mask_32(uint32_t mask, uint32_t a, uint32_t b)
   {
   return (a & mask) | (b & ~mask);
   }

inline uint32_t ct_is_zero_32(uint32_t x)
   {
   return ~ct_expand_mask_32(x);
   }

inline uint32_t ct_is_equal_32(uint32_t x, uint32_t y)
   {
   return ct_is_zero_32(x ^ y);
   }

inline uint16_t ct_expand_mask_16(uint16_t x)
   {
   uint16_t r = x;
   r |= r >> 8;
   r |= r >> 4;
   r |= r >> 2;
   r |= r >> 1;
   r &= 1;
   r = ~(r - 1);
   return r;
   }

inline uint16_t ct_select_mask_16(uint16_t mask, uint16_t a, uint16_t b)
   {
   return (a & mask) | (b & ~mask);
   }

inline uint16_t ct_is_zero_16(uint16_t x)
   {
   return ~ct_expand_mask_16(x);
   }

inline uint16_t ct_is_equal_16(uint16_t x, uint16_t y)
   {
   return ct_is_zero_16(x ^ y);
   }

inline uint8_t ct_expand_mask_8(uint8_t x)
   {
   uint8_t r = x;
   r |= r >> 4;
   r |= r >> 2;
   r |= r >> 1;
   r &= 1;
   r = ~(r - 1);
   return r;
   }

inline uint8_t ct_select_mask_8(uint8_t mask, uint8_t a, uint8_t b)
   {
   return (a & mask) | (b & ~mask);
   }

inline uint8_t ct_is_zero_8(uint8_t x)
   {
   return ~ct_expand_mask_8(x);
   }

inline uint8_t ct_is_equal_8(uint8_t x, uint8_t y)
   {
   return ct_is_zero_8(x ^ y);
   }

}

#endif