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
191
192
193
194
195
196
use crate::{aead, bssl, c, error, polyfill};
pub static AES_128_GCM: aead::Algorithm = aead::Algorithm {
key_len: AES_128_KEY_LEN,
init: aes_gcm_init,
seal: aes_gcm_seal,
open: aes_gcm_open,
id: aead::AlgorithmID::AES_128_GCM,
max_input_len: AES_GCM_MAX_INPUT_LEN,
};
pub static AES_256_GCM: aead::Algorithm = aead::Algorithm {
key_len: AES_256_KEY_LEN,
init: aes_gcm_init,
seal: aes_gcm_seal,
open: aes_gcm_open,
id: aead::AlgorithmID::AES_256_GCM,
max_input_len: AES_GCM_MAX_INPUT_LEN,
};
fn aes_gcm_init(ctx_buf: &mut [u8], key: &[u8])
-> Result<(), error::Unspecified> {
Result::from(unsafe {
GFp_aes_gcm_init(ctx_buf.as_mut_ptr(), ctx_buf.len(), key.as_ptr(),
key.len())
})
}
fn aes_gcm_seal(ctx: &[u64; aead::KEY_CTX_BUF_ELEMS],
nonce: &[u8; aead::NONCE_LEN], ad: &[u8], in_out: &mut [u8],
tag: &mut [u8; aead::TAG_LEN])
-> Result<(), error::Unspecified> {
let ctx = polyfill::slice::u64_as_u8(ctx);
Result::from(unsafe {
GFp_aes_gcm_seal(ctx.as_ptr(), in_out.as_mut_ptr(), in_out.len(), tag,
nonce, ad.as_ptr(), ad.len())
})
}
fn aes_gcm_open(ctx: &[u64; aead::KEY_CTX_BUF_ELEMS],
nonce: &[u8; aead::NONCE_LEN], ad: &[u8], in_prefix_len: usize,
in_out: &mut [u8], tag_out: &mut [u8; aead::TAG_LEN])
-> Result<(), error::Unspecified> {
let ctx = polyfill::slice::u64_as_u8(ctx);
Result::from(unsafe {
GFp_aes_gcm_open(ctx.as_ptr(), in_out.as_mut_ptr(),
in_out.len() - in_prefix_len, tag_out, nonce,
in_out[in_prefix_len..].as_ptr(), ad.as_ptr(),
ad.len())
})
}
const AES_128_KEY_LEN: usize = 128 / 8;
const AES_256_KEY_LEN: usize = 32;
pub const AES_KEY_CTX_BUF_LEN: usize = AES_KEY_BUF_LEN + GCM128_SERIALIZED_LEN;
const AES_KEY_BUF_LEN: usize = (4 * 4 * (AES_MAX_ROUNDS + 1)) + 8;
const AES_BLOCK_LEN: u64 = 16;
const AES_GCM_OVERHEAD_BLOCKS_PER_NONCE: u64 = 2;
const AES_GCM_MAX_INPUT_LEN: u64 = max_input_len!(AES_BLOCK_LEN, AES_GCM_OVERHEAD_BLOCKS_PER_NONCE);
const AES_MAX_ROUNDS: usize = 14;
const GCM128_SERIALIZED_LEN: usize = 16 * 16;
extern {
fn GFp_aes_gcm_init(ctx_buf: *mut u8, ctx_buf_len: c::size_t,
key: *const u8, key_len: c::size_t) -> bssl::Result;
fn GFp_aes_gcm_seal(ctx_buf: *const u8, in_out: *mut u8,
in_out_len: c::size_t,
tag_out: &mut [u8; aead::TAG_LEN],
nonce: &[u8; aead::NONCE_LEN], ad: *const u8,
ad_len: c::size_t) -> bssl::Result;
fn GFp_aes_gcm_open(ctx_buf: *const u8, out: *mut u8,
in_out_len: c::size_t,
tag_out: &mut [u8; aead::TAG_LEN],
nonce: &[u8; aead::NONCE_LEN], in_: *const u8,
ad: *const u8, ad_len: c::size_t) -> bssl::Result;
}
#[cfg(test)]
mod tests {
use crate::{c, test};
use bits::BitLength;
use super::AES_MAX_ROUNDS;
#[test]
pub fn test_aes() {
test::from_file("src/aead/aes_tests.txt", |section, test_case| {
assert_eq!(section, "");
let key = test_case.consume_bytes("Key");
let input = test_case.consume_bytes("Input");
let input = slice_as_array_ref!(&input, AES_BLOCK_SIZE).unwrap();
let expected_output = test_case.consume_bytes("Output");
let expected_output =
slice_as_array_ref!(&expected_output, AES_BLOCK_SIZE).unwrap();
let key_bits = BitLength::from_usize_bytes(key.len()).unwrap();
assert!(key_bits == BitLength(128) || key_bits == BitLength(256));
let key_bits = key_bits.as_usize_bits() as c::uint;
let mut aes_key = AES_KEY {
rd_key: [0u32; 4 * (AES_MAX_ROUNDS + 1)],
rounds: 0,
};
unsafe {
GFp_AES_set_encrypt_key(key.as_ptr(), key_bits, &mut aes_key);
}
let mut output_buf = [0u8; AES_BLOCK_SIZE];
unsafe {
GFp_AES_encrypt(input.as_ptr(), output_buf.as_mut_ptr(),
&aes_key);
}
assert_eq!(&output_buf[..], &expected_output[..]);
output_buf.copy_from_slice(&input[..]);
unsafe {
GFp_AES_encrypt(output_buf.as_ptr(), output_buf.as_mut_ptr(),
&aes_key);
}
assert_eq!(&output_buf[..], &expected_output[..]);
Ok(())
})
}
const AES_BLOCK_SIZE: usize = 16;
#[repr(C)]
pub struct AES_KEY {
pub rd_key: [u32; 4 * (AES_MAX_ROUNDS + 1)],
pub rounds: c::uint,
}
extern "C" {
fn GFp_AES_set_encrypt_key(key: *const u8, bits: c::uint,
aes_key: *mut AES_KEY);
fn GFp_AES_encrypt(in_: *const u8, out: *mut u8, key: *const AES_KEY);
}
#[test]
fn max_input_len_test() {
const NIST_SP800_38D_MAX_BITS: u64 = (1u64 << 39) - 256;
assert_eq!(NIST_SP800_38D_MAX_BITS, 549_755_813_632u64);
assert_eq!(super::AES_128_GCM.max_input_len * 8, NIST_SP800_38D_MAX_BITS);
assert_eq!(super::AES_256_GCM.max_input_len * 8, NIST_SP800_38D_MAX_BITS);
}
}