blob: 0900420710d60d0324fbea288ece1718a1c9050a [file] [log] [blame]
Nabil Wadih39da68f2023-02-16 18:07:41 -08001/* Copyright (c) 2023, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 */
15
16/// Block size in bytes for AES.
17pub const BLOCK_SIZE: usize = bssl_sys::AES_BLOCK_SIZE as usize;
18
19/// A single AES block.
20pub type AesBlock = [u8; BLOCK_SIZE];
21
22/// AES implementation used for encrypting/decrypting a single `AesBlock` at a time.
23pub struct Aes;
24
25impl Aes {
David Benjamin2a72f972023-06-07 12:21:18 -040026 /// Encrypts `block` in place.
Nabil Wadih39da68f2023-02-16 18:07:41 -080027 pub fn encrypt(key: &AesEncryptKey, block: &mut AesBlock) {
28 let input = *block;
29 // Safety:
30 // - AesBlock is always a valid size and key is guaranteed to already be initialized.
31 unsafe { bssl_sys::AES_encrypt(input.as_ptr(), block.as_mut_ptr(), &key.0) }
32 }
33
David Benjamin2a72f972023-06-07 12:21:18 -040034 /// Decrypts `block` in place.
Nabil Wadih39da68f2023-02-16 18:07:41 -080035 pub fn decrypt(key: &AesDecryptKey, block: &mut AesBlock) {
36 let input = *block;
37 // Safety:
38 // - AesBlock is always a valid size and key is guaranteed to already be initialized.
39 unsafe { bssl_sys::AES_decrypt(input.as_ptr(), block.as_mut_ptr(), &key.0) }
40 }
41}
42
43/// An initialized key which can be used for encrypting.
44pub struct AesEncryptKey(bssl_sys::AES_KEY);
45
46impl AesEncryptKey {
47 /// Initializes an encryption key from an appropriately sized array of bytes for AES-128 operations.
48 pub fn new_aes_128(key: [u8; 16]) -> AesEncryptKey {
49 new_encrypt_key(key)
50 }
51
52 /// Initializes an encryption key from an appropriately sized array of bytes for AES-256 operations.
53 pub fn new_aes_256(key: [u8; 32]) -> AesEncryptKey {
54 new_encrypt_key(key)
55 }
56}
57
58/// An initialized key which can be used for decrypting
59pub struct AesDecryptKey(bssl_sys::AES_KEY);
60
61impl AesDecryptKey {
62 /// Initializes a decryption key from an appropriately sized array of bytes for AES-128 operations.
63 pub fn new_aes_128(key: [u8; 16]) -> AesDecryptKey {
64 new_decrypt_key(key)
65 }
66
67 /// Initializes a decryption key from an appropriately sized array of bytes for AES-256 operations.
68 pub fn new_aes_256(key: [u8; 32]) -> AesDecryptKey {
69 new_decrypt_key(key)
70 }
71}
72
73/// Private generically implemented function for creating a new `AesEncryptKey` from an array of bytes.
74/// This should only be publicly exposed by wrapper types with the correct key lengths
75fn new_encrypt_key<const N: usize>(key: [u8; N]) -> AesEncryptKey {
76 let mut enc_key_uninit = core::mem::MaybeUninit::uninit();
77
78 // Safety:
79 // - key is guaranteed to point to bits/8 bytes determined by the len() * 8 used below.
80 // - bits is always a valid AES key size, as defined by the new_aes_* fns defined on the public
81 // key structs.
Nabil Wadihcc575422023-02-22 17:50:58 -080082 let result = unsafe {
Nabil Wadih39da68f2023-02-16 18:07:41 -080083 bssl_sys::AES_set_encrypt_key(
84 key.as_ptr(),
85 key.len() as core::ffi::c_uint * 8,
86 enc_key_uninit.as_mut_ptr(),
87 )
Nabil Wadihcc575422023-02-22 17:50:58 -080088 };
89 assert_eq!(result, 0, "Error occurred in bssl_sys::AES_set_encrypt_key");
Nabil Wadih39da68f2023-02-16 18:07:41 -080090
91 // Safety:
92 // - since we have checked above that initialization succeeded, this will never be UB
93 let enc_key = unsafe { enc_key_uninit.assume_init() };
94
95 AesEncryptKey(enc_key)
96}
97
98/// Private generically implemented function for creating a new `AesDecryptKey` from an array of bytes.
99/// This should only be publicly exposed by wrapper types with the correct key lengths.
100fn new_decrypt_key<const N: usize>(key: [u8; N]) -> AesDecryptKey {
101 let mut dec_key_uninit = core::mem::MaybeUninit::uninit();
102
103 // Safety:
104 // - key is guaranteed to point to bits/8 bytes determined by the len() * 8 used below.
105 // - bits is always a valid AES key size, as defined by the new_aes_* fns defined on the public
106 // key structs.
Nabil Wadihcc575422023-02-22 17:50:58 -0800107 let result = unsafe {
Nabil Wadih39da68f2023-02-16 18:07:41 -0800108 bssl_sys::AES_set_decrypt_key(
109 key.as_ptr(),
110 key.len() as core::ffi::c_uint * 8,
111 dec_key_uninit.as_mut_ptr(),
112 )
Nabil Wadihcc575422023-02-22 17:50:58 -0800113 };
114 assert_eq!(result, 0, "Error occurred in bssl_sys::AES_set_decrypt_key");
Nabil Wadih39da68f2023-02-16 18:07:41 -0800115
116 // Safety:
117 // - Since we have checked above that initialization succeeded, this will never be UB.
118 let dec_key = unsafe { dec_key_uninit.assume_init() };
119
120 AesDecryptKey(dec_key)
121}
122
123#[cfg(test)]
124mod tests {
Adam Langley190cc522024-01-05 15:53:32 -0800125 use crate::{
126 aes::{Aes, AesDecryptKey, AesEncryptKey},
127 test_helpers::decode_hex,
128 };
Nabil Wadih39da68f2023-02-16 18:07:41 -0800129
130 // test data from https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.1.1
131 #[test]
132 fn aes_128_test_encrypt() {
Nabil Wadih92de1952023-03-20 09:58:31 -0700133 let key = AesEncryptKey::new_aes_128(decode_hex("2b7e151628aed2a6abf7158809cf4f3c"));
Nabil Wadih39da68f2023-02-16 18:07:41 -0800134 let mut block = [0_u8; 16];
135
Nabil Wadih92de1952023-03-20 09:58:31 -0700136 block.copy_from_slice(&decode_hex::<16>("6bc1bee22e409f96e93d7e117393172a"));
Nabil Wadih39da68f2023-02-16 18:07:41 -0800137 Aes::encrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700138 assert_eq!(decode_hex("3ad77bb40d7a3660a89ecaf32466ef97"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800139
Nabil Wadih92de1952023-03-20 09:58:31 -0700140 block.copy_from_slice(&decode_hex::<16>("ae2d8a571e03ac9c9eb76fac45af8e51"));
Nabil Wadih39da68f2023-02-16 18:07:41 -0800141 Aes::encrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700142 assert_eq!(decode_hex("f5d3d58503b9699de785895a96fdbaaf"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800143
Nabil Wadih92de1952023-03-20 09:58:31 -0700144 block.copy_from_slice(&decode_hex::<16>("30c81c46a35ce411e5fbc1191a0a52ef"));
Nabil Wadih39da68f2023-02-16 18:07:41 -0800145 Aes::encrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700146 assert_eq!(decode_hex("43b1cd7f598ece23881b00e3ed030688"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800147
Nabil Wadih92de1952023-03-20 09:58:31 -0700148 block.copy_from_slice(&decode_hex::<16>("f69f2445df4f9b17ad2b417be66c3710"));
Nabil Wadih39da68f2023-02-16 18:07:41 -0800149 Aes::encrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700150 assert_eq!(decode_hex("7b0c785e27e8ad3f8223207104725dd4"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800151 }
152
153 // test data from https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.1.2
154 #[test]
155 fn aes_128_test_decrypt() {
Nabil Wadih92de1952023-03-20 09:58:31 -0700156 let key = AesDecryptKey::new_aes_128(decode_hex("2b7e151628aed2a6abf7158809cf4f3c"));
Nabil Wadih39da68f2023-02-16 18:07:41 -0800157 let mut block = [0_u8; 16];
158
Nabil Wadih92de1952023-03-20 09:58:31 -0700159 block.copy_from_slice(&decode_hex::<16>("3ad77bb40d7a3660a89ecaf32466ef97"));
Nabil Wadih39da68f2023-02-16 18:07:41 -0800160 Aes::decrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700161 assert_eq!(decode_hex::<16>("6bc1bee22e409f96e93d7e117393172a"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800162
Nabil Wadih92de1952023-03-20 09:58:31 -0700163 block.copy_from_slice(&decode_hex::<16>("f5d3d58503b9699de785895a96fdbaaf"));
Nabil Wadih39da68f2023-02-16 18:07:41 -0800164 Aes::decrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700165 assert_eq!(decode_hex::<16>("ae2d8a571e03ac9c9eb76fac45af8e51"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800166
Nabil Wadih92de1952023-03-20 09:58:31 -0700167 block.copy_from_slice(&decode_hex::<16>("43b1cd7f598ece23881b00e3ed030688"));
Nabil Wadih39da68f2023-02-16 18:07:41 -0800168 Aes::decrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700169 assert_eq!(decode_hex::<16>("30c81c46a35ce411e5fbc1191a0a52ef"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800170
Nabil Wadih92de1952023-03-20 09:58:31 -0700171 block.copy_from_slice(&decode_hex::<16>("7b0c785e27e8ad3f8223207104725dd4").as_slice());
Nabil Wadih39da68f2023-02-16 18:07:41 -0800172 Aes::decrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700173 assert_eq!(decode_hex::<16>("f69f2445df4f9b17ad2b417be66c3710"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800174 }
175
176 // test data from https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.1.5
177 #[test]
178 pub fn aes_256_test_encrypt() {
Nabil Wadih92de1952023-03-20 09:58:31 -0700179 let key = AesEncryptKey::new_aes_256(decode_hex(
180 "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
Nabil Wadih39da68f2023-02-16 18:07:41 -0800181 ));
182 let mut block: [u8; 16];
183
Nabil Wadih92de1952023-03-20 09:58:31 -0700184 block = decode_hex("6bc1bee22e409f96e93d7e117393172a");
Nabil Wadih39da68f2023-02-16 18:07:41 -0800185 Aes::encrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700186 assert_eq!(decode_hex("f3eed1bdb5d2a03c064b5a7e3db181f8"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800187
Nabil Wadih92de1952023-03-20 09:58:31 -0700188 block = decode_hex("ae2d8a571e03ac9c9eb76fac45af8e51");
Nabil Wadih39da68f2023-02-16 18:07:41 -0800189 Aes::encrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700190 assert_eq!(decode_hex("591ccb10d410ed26dc5ba74a31362870"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800191
Nabil Wadih92de1952023-03-20 09:58:31 -0700192 block = decode_hex("30c81c46a35ce411e5fbc1191a0a52ef");
Nabil Wadih39da68f2023-02-16 18:07:41 -0800193 Aes::encrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700194 assert_eq!(decode_hex("b6ed21b99ca6f4f9f153e7b1beafed1d"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800195
Nabil Wadih92de1952023-03-20 09:58:31 -0700196 block = decode_hex("f69f2445df4f9b17ad2b417be66c3710");
Nabil Wadih39da68f2023-02-16 18:07:41 -0800197 Aes::encrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700198 assert_eq!(decode_hex("23304b7a39f9f3ff067d8d8f9e24ecc7"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800199 }
200
201 // test data from https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.1.6
202 #[test]
203 fn aes_256_test_decrypt() {
Nabil Wadih92de1952023-03-20 09:58:31 -0700204 let key = AesDecryptKey::new_aes_256(decode_hex(
205 "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
Nabil Wadih39da68f2023-02-16 18:07:41 -0800206 ));
207
208 let mut block: [u8; 16];
209
Nabil Wadih92de1952023-03-20 09:58:31 -0700210 block = decode_hex("f3eed1bdb5d2a03c064b5a7e3db181f8");
Nabil Wadih39da68f2023-02-16 18:07:41 -0800211 Aes::decrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700212 assert_eq!(decode_hex("6bc1bee22e409f96e93d7e117393172a"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800213
Nabil Wadih92de1952023-03-20 09:58:31 -0700214 block = decode_hex("591ccb10d410ed26dc5ba74a31362870");
Nabil Wadih39da68f2023-02-16 18:07:41 -0800215 Aes::decrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700216 assert_eq!(decode_hex("ae2d8a571e03ac9c9eb76fac45af8e51"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800217
Nabil Wadih92de1952023-03-20 09:58:31 -0700218 block = decode_hex("b6ed21b99ca6f4f9f153e7b1beafed1d");
Nabil Wadih39da68f2023-02-16 18:07:41 -0800219 Aes::decrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700220 assert_eq!(decode_hex("30c81c46a35ce411e5fbc1191a0a52ef"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800221
Nabil Wadih92de1952023-03-20 09:58:31 -0700222 block = decode_hex("23304b7a39f9f3ff067d8d8f9e24ecc7");
Nabil Wadih39da68f2023-02-16 18:07:41 -0800223 Aes::decrypt(&key, &mut block);
Nabil Wadih92de1952023-03-20 09:58:31 -0700224 assert_eq!(decode_hex("f69f2445df4f9b17ad2b417be66c3710"), block);
Nabil Wadih39da68f2023-02-16 18:07:41 -0800225 }
226}