forked from hacspec/hacspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aesgcm.rs
156 lines (141 loc) · 3.54 KB
/
aesgcm.rs
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
// Import hacspec and all needed definitions.
use hacspec_lib::*;
// Import aes and gcm
use super::aes::{self, aes_ctr_keyblock, aes_encrypt, Block};
use super::gf128::{gmac, Key, Tag};
fn pad_aad_msg(aad: &ByteSeq, msg: &ByteSeq) -> ByteSeq {
let laad = aad.len();
let lmsg = msg.len();
let pad_aad = if laad % 16 == 0 {
laad
} else {
laad + (16 - (laad % 16))
};
let pad_msg = if lmsg % 16 == 0 {
lmsg
} else {
lmsg + (16 - (lmsg % 16))
};
let mut padded_msg = ByteSeq::new(pad_aad + pad_msg + 16);
padded_msg = padded_msg.update(0, aad);
padded_msg = padded_msg.update(pad_aad, msg);
padded_msg = padded_msg.update(
pad_aad + pad_msg,
&U64_to_be_bytes(U64(laad as u64) * U64(8)),
);
padded_msg = padded_msg.update(
pad_aad + pad_msg + 8,
&U64_to_be_bytes(U64(lmsg as u64) * U64(8)),
);
padded_msg
}
pub(crate) fn encrypt_aes(
key: &ByteSeq,
iv: aes::Nonce,
aad: &ByteSeq,
msg: &ByteSeq,
alg: aes::AesVariant,
) -> (ByteSeq, Tag) {
let iv0 = aes::Nonce::new();
let mac_key = aes_ctr_keyblock(
key,
iv0,
U32(0),
aes::key_length(alg),
aes::rounds(alg),
alg,
);
let tag_mix = aes_ctr_keyblock(key, iv, U32(1), aes::key_length(alg), aes::rounds(alg), alg);
let cipher_text = aes_encrypt(key, iv, U32(2), msg, alg);
let padded_msg = pad_aad_msg(aad, &cipher_text);
let tag = gmac(&padded_msg, Key::from_seq(&mac_key));
let tag = aes::xor_block(Block::from_seq(&tag), tag_mix);
(cipher_text, Tag::from_seq(&tag))
}
pub fn encrypt_aes128(
key: aes::Key128,
iv: aes::Nonce,
aad: &ByteSeq,
msg: &ByteSeq,
) -> (ByteSeq, Tag) {
encrypt_aes(
&ByteSeq::from_seq(&key),
iv,
aad,
msg,
aes::AesVariant::Aes128,
)
}
pub fn encrypt_aes256(
key: aes::Key256,
iv: aes::Nonce,
aad: &ByteSeq,
msg: &ByteSeq,
) -> (ByteSeq, Tag) {
encrypt_aes(
&ByteSeq::from_seq(&key),
iv,
aad,
msg,
aes::AesVariant::Aes256,
)
}
pub(crate) fn decrypt_aes(
key: &ByteSeq,
iv: aes::Nonce,
aad: &ByteSeq,
cipher_text: &ByteSeq,
tag: Tag,
alg: aes::AesVariant,
) -> Result<ByteSeq, String> {
let iv0 = aes::Nonce::new();
let mac_key = aes_ctr_keyblock(
key,
iv0,
U32(0),
aes::key_length(alg),
aes::rounds(alg),
alg,
);
let tag_mix = aes_ctr_keyblock(key, iv, U32(1), aes::key_length(alg), aes::rounds(alg), alg);
let padded_msg = pad_aad_msg(aad, cipher_text);
let my_tag = gmac(&padded_msg, Key::from_seq(&mac_key));
let my_tag = aes::xor_block(Block::from_seq(&my_tag), tag_mix);
if my_tag.declassify_eq(&Block::from_seq(&tag)) {
Ok(aes::aes_decrypt(key, iv, U32(2), cipher_text, alg))
} else {
Err("Mac verification failed".to_string())
}
}
pub fn decrypt_aes128(
key: aes::Key128,
iv: aes::Nonce,
aad: &ByteSeq,
cipher_text: &ByteSeq,
tag: Tag,
) -> Result<ByteSeq, String> {
decrypt_aes(
&ByteSeq::from_seq(&key),
iv,
aad,
cipher_text,
tag,
aes::AesVariant::Aes128,
)
}
pub fn decrypt_aes256(
key: aes::Key256,
iv: aes::Nonce,
aad: &ByteSeq,
cipher_text: &ByteSeq,
tag: Tag,
) -> Result<ByteSeq, String> {
decrypt_aes(
&ByteSeq::from_seq(&key),
iv,
aad,
cipher_text,
tag,
aes::AesVariant::Aes256,
)
}