-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: pull request #22 from namib-project/rustcrypto_backend
Add a RustCrypto based backend for cryptographic COSE operations
- Loading branch information
Showing
38 changed files
with
1,367 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2024 The NAMIB Project Developers. | ||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
* option. This file may not be copied, modified, or distributed | ||
* except according to those terms. | ||
* | ||
* SPDX-License-Identifier: MIT OR Apache-2.0 | ||
*/ | ||
use cfg_aliases::cfg_aliases; | ||
|
||
fn main() { | ||
// Set up some aliases for conditional compilation (in order to avoid repetition). | ||
cfg_aliases! { | ||
rustcrypto_encrypt_base: { | ||
any( | ||
feature = "rustcrypto-aes-gcm" | ||
) | ||
}, | ||
rustcrypto_sign_base: { | ||
any( | ||
feature = "rustcrypto-ecdsa" | ||
) | ||
}, | ||
rustcrypto_key_distribution_base: { | ||
any( | ||
feature = "rustcrypto-aes-kw" | ||
) | ||
}, | ||
rustcrypto_mac_base: { | ||
any( | ||
feature = "rustcrypto-hmac" | ||
) | ||
}, | ||
rustcrypto_base: { | ||
any( | ||
rustcrypto_encrypt_base, | ||
rustcrypto_sign_base, | ||
rustcrypto_key_distribution_base, | ||
rustcrypto_mac_base | ||
) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/token/cose/crypto_impl/rustcrypto/encrypt/aes_gcm.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (c) 2024 The NAMIB Project Developers. | ||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
* option. This file may not be copied, modified, or distributed | ||
* except according to those terms. | ||
* | ||
* SPDX-License-Identifier: MIT OR Apache-2.0 | ||
*/ | ||
use aead::{Aead, AeadCore, Key, KeyInit, Nonce, Payload}; | ||
use aes::Aes192; | ||
use aes_gcm::{Aes128Gcm, Aes256Gcm, AesGcm}; | ||
use coset::{iana, Algorithm}; | ||
use rand::CryptoRng; | ||
use rand::RngCore; | ||
use typenum::consts::U12; | ||
|
||
use crate::error::CoseCipherError; | ||
use crate::token::cose::{CoseSymmetricKey, CryptoBackend}; | ||
|
||
use super::RustCryptoContext; | ||
|
||
impl<RNG: RngCore + CryptoRng> RustCryptoContext<RNG> { | ||
/// Perform an AEAD encryption operation on `plaintext` and the additional authenticated | ||
/// data `aad` using the given `iv` and `key`. | ||
fn encrypt_aead<AEAD: Aead + AeadCore + KeyInit>( | ||
key: &CoseSymmetricKey<'_, <RustCryptoContext<RNG> as CryptoBackend>::Error>, | ||
plaintext: &[u8], | ||
aad: &[u8], | ||
iv: &[u8], | ||
) -> Result<Vec<u8>, CoseCipherError<<Self as CryptoBackend>::Error>> { | ||
let aes_key = Key::<AEAD>::from_slice(key.k); | ||
let cipher = AEAD::new(aes_key); | ||
let nonce = Nonce::<AEAD>::from_slice(iv); | ||
let payload = Payload { | ||
msg: plaintext, | ||
aad, | ||
}; | ||
cipher | ||
.encrypt(nonce, payload) | ||
.map_err(CoseCipherError::from) | ||
} | ||
|
||
/// Perform an AEAD decryption operation on `ciphertext` and the additional authenticated | ||
/// data `aad` using the given `iv` and `key`. | ||
fn decrypt_aead<AEAD: Aead + AeadCore + KeyInit>( | ||
key: &CoseSymmetricKey<'_, <RustCryptoContext<RNG> as CryptoBackend>::Error>, | ||
ciphertext: &[u8], | ||
aad: &[u8], | ||
iv: &[u8], | ||
) -> Result<Vec<u8>, CoseCipherError<<Self as CryptoBackend>::Error>> { | ||
let aes_key = Key::<AEAD>::from_slice(key.k); | ||
let cipher = AEAD::new(aes_key); | ||
let nonce = Nonce::<AEAD>::from_slice(iv); | ||
let payload = Payload { | ||
msg: ciphertext, | ||
aad, | ||
}; | ||
cipher | ||
.decrypt(nonce, payload) | ||
.map_err(CoseCipherError::from) | ||
} | ||
|
||
/// Perform an AES-GCM encryption operation on `plaintext` and the additional authenticated | ||
/// data `aad` using the given `iv` and `key` with the given `algorithm` variant of AES-GCM. | ||
pub(super) fn encrypt_aes_gcm( | ||
algorithm: iana::Algorithm, | ||
key: &CoseSymmetricKey<'_, <Self as CryptoBackend>::Error>, | ||
plaintext: &[u8], | ||
aad: &[u8], | ||
iv: &[u8], | ||
) -> Result<Vec<u8>, CoseCipherError<<Self as CryptoBackend>::Error>> { | ||
match algorithm { | ||
iana::Algorithm::A128GCM => Self::encrypt_aead::<Aes128Gcm>(key, plaintext, aad, iv), | ||
iana::Algorithm::A192GCM => { | ||
Self::encrypt_aead::<AesGcm<Aes192, U12>>(key, plaintext, aad, iv) | ||
} | ||
iana::Algorithm::A256GCM => Self::encrypt_aead::<Aes256Gcm>(key, plaintext, aad, iv), | ||
a => Err(CoseCipherError::UnsupportedAlgorithm(Algorithm::Assigned( | ||
a, | ||
))), | ||
} | ||
} | ||
|
||
/// Perform an AES-GCM decryption operation on `ciphertext` and the additional authenticated | ||
/// data `aad` using the given `iv` and `key` with the given `algorithm` variant of AES-GCM. | ||
pub(super) fn decrypt_aes_gcm( | ||
algorithm: iana::Algorithm, | ||
key: &CoseSymmetricKey<'_, <Self as CryptoBackend>::Error>, | ||
ciphertext_with_tag: &[u8], | ||
aad: &[u8], | ||
iv: &[u8], | ||
) -> Result<Vec<u8>, CoseCipherError<<Self as CryptoBackend>::Error>> { | ||
match algorithm { | ||
iana::Algorithm::A128GCM => { | ||
Self::decrypt_aead::<Aes128Gcm>(key, ciphertext_with_tag, aad, iv) | ||
} | ||
iana::Algorithm::A192GCM => { | ||
Self::decrypt_aead::<AesGcm<Aes192, U12>>(key, ciphertext_with_tag, aad, iv) | ||
} | ||
iana::Algorithm::A256GCM => { | ||
Self::decrypt_aead::<Aes256Gcm>(key, ciphertext_with_tag, aad, iv) | ||
} | ||
a => Err(CoseCipherError::UnsupportedAlgorithm(Algorithm::Assigned( | ||
a, | ||
))), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2024 The NAMIB Project Developers. | ||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
* option. This file may not be copied, modified, or distributed | ||
* except according to those terms. | ||
* | ||
* SPDX-License-Identifier: MIT OR Apache-2.0 | ||
*/ | ||
use coset::iana; | ||
use rand::{CryptoRng, RngCore}; | ||
|
||
use crate::error::CoseCipherError; | ||
use crate::token::cose::crypto_impl::rustcrypto::RustCryptoContext; | ||
use crate::token::cose::{CoseSymmetricKey, EncryptCryptoBackend}; | ||
|
||
#[cfg(feature = "rustcrypto-aes-gcm")] | ||
mod aes_gcm; | ||
|
||
impl<RNG: RngCore + CryptoRng> EncryptCryptoBackend for RustCryptoContext<RNG> { | ||
#[cfg(feature = "rustcrypto-aes-gcm")] | ||
fn encrypt_aes_gcm( | ||
&mut self, | ||
algorithm: iana::Algorithm, | ||
key: CoseSymmetricKey<'_, Self::Error>, | ||
plaintext: &[u8], | ||
aad: &[u8], | ||
iv: &[u8], | ||
) -> Result<Vec<u8>, CoseCipherError<Self::Error>> { | ||
Self::encrypt_aes_gcm(algorithm, &key, plaintext, aad, iv) | ||
} | ||
|
||
#[cfg(feature = "rustcrypto-aes-gcm")] | ||
fn decrypt_aes_gcm( | ||
&mut self, | ||
algorithm: iana::Algorithm, | ||
key: CoseSymmetricKey<'_, Self::Error>, | ||
ciphertext_with_tag: &[u8], | ||
aad: &[u8], | ||
iv: &[u8], | ||
) -> Result<Vec<u8>, CoseCipherError<Self::Error>> { | ||
Self::decrypt_aes_gcm(algorithm, &key, ciphertext_with_tag, aad, iv) | ||
} | ||
} |
Oops, something went wrong.