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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#![warn(missing_docs)]
#![deny(rustdoc::missing_crate_level_docs)]
#![doc(test(attr(deny(warnings))))]
#![deny(clippy::all)]
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::*;
fn field_names(data: Data) -> Vec<String> {
match data {
Data::Struct(DataStruct { fields, .. }) => match fields {
Fields::Named(FieldsNamed { named, .. }) => named
.iter()
.map(|Field { ident, .. }| {
ident
.as_ref()
.expect("named fields have idents")
.to_string()
})
.collect(),
Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => unnamed
.iter()
.enumerate()
.map(|(i, _)| i.to_string())
.collect(),
Fields::Unit => Vec::new(),
},
_ => panic!("this macro does not support enums or unions for constant-time operations"),
}
}
#[proc_macro_derive(ConstantTimeEq)]
pub fn derive_eq(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
let eq_block = {
let field_names = field_names(data);
let mut eq_stmts: Vec<Stmt> = vec![parse_str("let mut ret: u8 = 1;").unwrap()];
eq_stmts.extend(field_names.into_iter().map(|name| {
parse_str(&format!(
"ret &= self.{}.ct_eq(&other.{}).unwrap_u8();",
name, name
))
.unwrap()
}));
eq_stmts.push(parse_str("return ret.into();").unwrap());
Block {
brace_token: token::Brace {
span: Span::mixed_site(),
},
stmts: eq_stmts,
}
};
let output = if cfg!(feature = "with-ng") {
quote! {
impl ::subtle_ng::ConstantTimeEq for #ident {
#[inline]
fn ct_eq(&self, other: &Self) -> ::subtle_ng::Choice {
#eq_block
}
}
}
} else {
quote! {
impl ::subtle::ConstantTimeEq for #ident {
#[inline]
fn ct_eq(&self, other: &Self) -> ::subtle_ng::Choice {
#eq_block
}
}
}
};
output.into()
}
#[proc_macro_derive(ConstantTimeGreater)]
pub fn derive_gt(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
let gt_block = {
let field_names = field_names(data);
let mut gt_stmts: Vec<Stmt> = vec![
parse_str("let mut still_at_least_eq: u8 = 1;").unwrap(),
parse_str("let mut was_gt: u8 = 0;").unwrap(),
];
for name in field_names.into_iter() {
gt_stmts.push(
parse_str(&format!(
"was_gt |= still_at_least_eq & self.{}.ct_gt(&other.{}).unwrap_u8();",
name, name,
))
.unwrap(),
);
gt_stmts.push(
parse_str(&format!(
"still_at_least_eq &= self.{}.ct_eq(&other.{}).unwrap_u8();",
name, name,
))
.unwrap(),
);
}
gt_stmts.push(parse_str("return was_gt.into();").unwrap());
Block {
brace_token: token::Brace {
span: Span::mixed_site(),
},
stmts: gt_stmts,
}
};
let output = if cfg!(feature = "with-ng") {
quote! {
impl ::subtle_ng::ConstantTimeGreater for #ident {
#[inline]
fn ct_gt(&self, other: &Self) -> ::subtle_ng::Choice {
use ::subtle_ng::{ConstantTimeEq, ConstantTimeGreater};
#gt_block
}
}
}
} else {
quote! {
impl ::subtle::ConstantTimeGreater for #ident {
#[inline]
fn ct_gt(&self, other: &Self) -> ::subtle_ng::Choice {
use ::subtle::{ConstantTimeEq, ConstantTimeGreater};
#gt_block
}
}
}
};
output.into()
}