Skip to content

Commit c83a51b

Browse files
committed
Add FIPS tests
1 parent 18f67e2 commit c83a51b

File tree

2 files changed

+87
-8
lines changed

2 files changed

+87
-8
lines changed

aws-lc-rs/src/cipher/streaming.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::cipher::{
55
Algorithm, DecryptionContext, EncryptionContext, OperatingMode, UnboundCipherKey,
66
};
77
use crate::error::Unspecified;
8+
use crate::fips::indicator_check;
89
use crate::ptr::{LcPtr, Pointer};
910
use aws_lc::{
1011
EVP_CIPHER_CTX_new, EVP_CIPHER_iv_length, EVP_CIPHER_key_length, EVP_DecryptFinal_ex,
@@ -76,6 +77,7 @@ impl StreamingEncryptingKey {
7677
<usize>::try_from(unsafe { EVP_CIPHER_iv_length(*cipher) }).unwrap()
7778
);
7879

80+
// AWS-LC copies the key and iv values into the EVP_CIPHER_CTX, and thus can be dropped after this.
7981
if 1 != unsafe {
8082
EVP_EncryptInit_ex(
8183
cipher_ctx.as_mut_ptr(),
@@ -155,13 +157,13 @@ impl StreamingEncryptingKey {
155157
}
156158
let mut outlen: i32 = 0;
157159

158-
if 1 != unsafe {
160+
if 1 != indicator_check!(unsafe {
159161
EVP_EncryptFinal_ex(
160162
self.cipher_ctx.as_mut_ptr(),
161163
output.as_mut_ptr(),
162164
&mut outlen,
163165
)
164-
} {
166+
}) {
165167
return Err(Unspecified);
166168
}
167169
let outlen: usize = outlen.try_into()?;
@@ -262,6 +264,7 @@ impl StreamingDecryptingKey {
262264
<usize>::try_from(unsafe { EVP_CIPHER_iv_length(*cipher) }).unwrap()
263265
);
264266

267+
// AWS-LC copies the key and iv values into the EVP_CIPHER_CTX, and thus can be dropped after this.
265268
if 1 != unsafe {
266269
EVP_DecryptInit_ex(
267270
cipher_ctx.as_mut_ptr(),
@@ -336,7 +339,9 @@ impl StreamingDecryptingKey {
336339
}
337340
let mut outlen: i32 = 0;
338341

339-
if 1 != unsafe { EVP_DecryptFinal_ex(*self.cipher_ctx, output.as_mut_ptr(), &mut outlen) } {
342+
if 1 != indicator_check!(unsafe {
343+
EVP_DecryptFinal_ex(*self.cipher_ctx, output.as_mut_ptr(), &mut outlen)
344+
}) {
340345
return Err(Unspecified);
341346
}
342347
let outlen: usize = outlen.try_into()?;

aws-lc-rs/src/cipher/tests/fips.rs

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use crate::{
77
cipher::{
88
DecryptingKey, EncryptingKey, PaddedBlockDecryptingKey, PaddedBlockEncryptingKey,
9-
UnboundCipherKey, AES_128, AES_256,
9+
StreamingDecryptingKey, StreamingEncryptingKey, UnboundCipherKey, AES_128, AES_256,
1010
},
1111
fips::{assert_fips_status_indicator, FipsServiceStatus},
1212
};
@@ -49,30 +49,104 @@ macro_rules! block_api {
4949
};
5050
}
5151

52+
macro_rules! streaming_api {
53+
($name:ident, $alg:expr, $encrypt_mode:path, $decrypt_mode:path, $key:expr) => {
54+
#[test]
55+
fn $name() {
56+
let mut key = $encrypt_mode(UnboundCipherKey::new($alg, $key).unwrap()).unwrap();
57+
58+
let input = TEST_MESSAGE.as_bytes();
59+
let mut encrypt_output = vec![0u8; TEST_MESSAGE.len() + $alg.block_len()];
60+
61+
let mut buffer_update = key.update(&input, &mut encrypt_output).unwrap();
62+
63+
let outlen = buffer_update.written().len();
64+
let (context, buffer_update) = assert_fips_status_indicator!(
65+
key.finish(buffer_update.remainder_mut()),
66+
FipsServiceStatus::Approved
67+
)
68+
.unwrap();
69+
70+
let outlen = outlen + buffer_update.written().len();
71+
72+
let ciphertext = &encrypt_output[0..outlen];
73+
let mut decrypt_output = vec![0u8; outlen + $alg.block_len()];
74+
let mut key =
75+
$decrypt_mode(UnboundCipherKey::new($alg, $key).unwrap(), context).unwrap();
76+
77+
let mut buffer_update = key.update(ciphertext, &mut decrypt_output).unwrap();
78+
79+
let outlen = buffer_update.written().len();
80+
let buffer_update = assert_fips_status_indicator!(
81+
key.finish(buffer_update.remainder_mut()),
82+
FipsServiceStatus::Approved
83+
)
84+
.unwrap();
85+
86+
let outlen = outlen + buffer_update.written().len();
87+
let plaintext = &decrypt_output[0..outlen];
88+
89+
assert_eq!(TEST_MESSAGE.as_bytes(), plaintext);
90+
}
91+
};
92+
}
93+
94+
streaming_api!(
95+
streaming_aes_128_cbc_pkcs7,
96+
&AES_128,
97+
StreamingEncryptingKey::cbc_pkcs7,
98+
StreamingDecryptingKey::cbc_pkcs7,
99+
&TEST_KEY_128_BIT
100+
);
101+
102+
streaming_api!(
103+
streaming_aes_128_ctr,
104+
&AES_128,
105+
StreamingEncryptingKey::ctr,
106+
StreamingDecryptingKey::ctr,
107+
&TEST_KEY_128_BIT
108+
);
109+
110+
streaming_api!(
111+
streaming_aes_256_cbc_pkcs7,
112+
&AES_256,
113+
StreamingEncryptingKey::cbc_pkcs7,
114+
StreamingDecryptingKey::cbc_pkcs7,
115+
&TEST_KEY_256_BIT
116+
);
117+
streaming_api!(
118+
streaming_aes_256_ctr,
119+
&AES_256,
120+
StreamingEncryptingKey::ctr,
121+
StreamingDecryptingKey::ctr,
122+
&TEST_KEY_256_BIT
123+
);
124+
52125
block_api!(
53-
aes_126_cbc_pkcs7,
126+
block_aes_128_cbc_pkcs7,
54127
&AES_128,
55128
PaddedBlockEncryptingKey::cbc_pkcs7,
56129
PaddedBlockDecryptingKey::cbc_pkcs7,
57130
&TEST_KEY_128_BIT
58131
);
132+
59133
block_api!(
60-
aes_126_ctr,
134+
block_aes_128_ctr,
61135
&AES_128,
62136
EncryptingKey::ctr,
63137
DecryptingKey::ctr,
64138
&TEST_KEY_128_BIT
65139
);
66140

67141
block_api!(
68-
aes_256_cbc_pkcs7,
142+
block_aes_256_cbc_pkcs7,
69143
&AES_256,
70144
PaddedBlockEncryptingKey::cbc_pkcs7,
71145
PaddedBlockDecryptingKey::cbc_pkcs7,
72146
&TEST_KEY_256_BIT
73147
);
74148
block_api!(
75-
aes_256_ctr,
149+
block_aes_256_ctr,
76150
&AES_256,
77151
EncryptingKey::ctr,
78152
DecryptingKey::ctr,

0 commit comments

Comments
 (0)