1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 use bssl_crypto::aes::{AesDecryptKey, AesEncryptKey}; 16 use crypto_provider::aes::{ 17 Aes, Aes128Key, Aes256Key, AesBlock, AesCipher, AesDecryptCipher, AesEncryptCipher, AesKey, 18 }; 19 20 /// BoringSSL AES-128 operations 21 pub struct Aes128; 22 impl Aes for Aes128 { 23 type Key = Aes128Key; 24 type EncryptCipher = Aes128EncryptCipher; 25 type DecryptCipher = Aes128DecryptCipher; 26 } 27 28 /// BoringSSL AES-256 operations 29 pub struct Aes256; 30 impl Aes for Aes256 { 31 type Key = Aes256Key; 32 type EncryptCipher = Aes256EncryptCipher; 33 type DecryptCipher = Aes256DecryptCipher; 34 } 35 36 /// A BoringSSL backed AES-128 Encryption Cipher 37 pub struct Aes128EncryptCipher(AesEncryptKey); 38 39 /// A BoringSSL backed AES-128 Decryption Cipher 40 pub struct Aes128DecryptCipher(AesDecryptKey); 41 42 /// A BoringSSL backed AES-256 Encryption Cipher 43 pub struct Aes256EncryptCipher(AesEncryptKey); 44 45 /// A BoringSSL backed AES-256 Decryption Cipher 46 pub struct Aes256DecryptCipher(AesDecryptKey); 47 48 impl AesCipher for Aes128EncryptCipher { 49 type Key = Aes128Key; 50 new(key: &Self::Key) -> Self51 fn new(key: &Self::Key) -> Self { 52 Self(bssl_crypto::aes::AesEncryptKey::new_aes_128( 53 *key.as_array(), 54 )) 55 } 56 } 57 58 impl AesEncryptCipher for Aes128EncryptCipher { encrypt(&self, block: &mut AesBlock)59 fn encrypt(&self, block: &mut AesBlock) { 60 bssl_crypto::aes::Aes::encrypt(&self.0, block) 61 } 62 } 63 64 impl AesCipher for Aes128DecryptCipher { 65 type Key = Aes128Key; 66 new(key: &Self::Key) -> Self67 fn new(key: &Self::Key) -> Self { 68 Self(bssl_crypto::aes::AesDecryptKey::new_aes_128( 69 *key.as_array(), 70 )) 71 } 72 } 73 74 impl AesDecryptCipher for Aes128DecryptCipher { decrypt(&self, block: &mut AesBlock)75 fn decrypt(&self, block: &mut AesBlock) { 76 bssl_crypto::aes::Aes::decrypt(&self.0, block) 77 } 78 } 79 80 impl AesCipher for Aes256EncryptCipher { 81 type Key = Aes256Key; 82 new(key: &Self::Key) -> Self83 fn new(key: &Self::Key) -> Self { 84 Self(bssl_crypto::aes::AesEncryptKey::new_aes_256( 85 *key.as_array(), 86 )) 87 } 88 } 89 90 impl AesEncryptCipher for Aes256EncryptCipher { encrypt(&self, block: &mut AesBlock)91 fn encrypt(&self, block: &mut AesBlock) { 92 bssl_crypto::aes::Aes::encrypt(&self.0, block) 93 } 94 } 95 96 impl AesCipher for Aes256DecryptCipher { 97 type Key = Aes256Key; 98 new(key: &Self::Key) -> Self99 fn new(key: &Self::Key) -> Self { 100 Self(bssl_crypto::aes::AesDecryptKey::new_aes_256( 101 *key.as_array(), 102 )) 103 } 104 } 105 106 impl AesDecryptCipher for Aes256DecryptCipher { decrypt(&self, block: &mut AesBlock)107 fn decrypt(&self, block: &mut AesBlock) { 108 bssl_crypto::aes::Aes::decrypt(&self.0, block) 109 } 110 } 111 112 #[cfg(test)] 113 mod tests { 114 use super::*; 115 use core::marker::PhantomData; 116 use crypto_provider::aes::testing::*; 117 118 #[apply(aes_128_encrypt_test_cases)] aes_128_encrypt_test(testcase: CryptoProviderTestCase<Aes128EncryptCipher>)119 fn aes_128_encrypt_test(testcase: CryptoProviderTestCase<Aes128EncryptCipher>) { 120 testcase(PhantomData); 121 } 122 123 #[apply(aes_128_decrypt_test_cases)] aes_128_decrypt_test(testcase: CryptoProviderTestCase<Aes128DecryptCipher>)124 fn aes_128_decrypt_test(testcase: CryptoProviderTestCase<Aes128DecryptCipher>) { 125 testcase(PhantomData); 126 } 127 128 #[apply(aes_256_encrypt_test_cases)] aes_256_encrypt_test(testcase: CryptoProviderTestCase<Aes256EncryptCipher>)129 fn aes_256_encrypt_test(testcase: CryptoProviderTestCase<Aes256EncryptCipher>) { 130 testcase(PhantomData); 131 } 132 133 #[apply(aes_256_decrypt_test_cases)] aes_256_decrypt_test(testcase: CryptoProviderTestCase<Aes256DecryptCipher>)134 fn aes_256_decrypt_test(testcase: CryptoProviderTestCase<Aes256DecryptCipher>) { 135 testcase(PhantomData); 136 } 137 } 138