|
6 | 6 | use crate::{
|
7 | 7 | cipher::{
|
8 | 8 | DecryptingKey, EncryptingKey, PaddedBlockDecryptingKey, PaddedBlockEncryptingKey,
|
9 |
| - UnboundCipherKey, AES_128, AES_256, |
| 9 | + StreamingDecryptingKey, StreamingEncryptingKey, UnboundCipherKey, AES_128, AES_256, |
10 | 10 | },
|
11 | 11 | fips::{assert_fips_status_indicator, FipsServiceStatus},
|
12 | 12 | };
|
@@ -49,30 +49,104 @@ macro_rules! block_api {
|
49 | 49 | };
|
50 | 50 | }
|
51 | 51 |
|
| 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 | + |
52 | 125 | block_api!(
|
53 |
| - aes_126_cbc_pkcs7, |
| 126 | + block_aes_128_cbc_pkcs7, |
54 | 127 | &AES_128,
|
55 | 128 | PaddedBlockEncryptingKey::cbc_pkcs7,
|
56 | 129 | PaddedBlockDecryptingKey::cbc_pkcs7,
|
57 | 130 | &TEST_KEY_128_BIT
|
58 | 131 | );
|
| 132 | + |
59 | 133 | block_api!(
|
60 |
| - aes_126_ctr, |
| 134 | + block_aes_128_ctr, |
61 | 135 | &AES_128,
|
62 | 136 | EncryptingKey::ctr,
|
63 | 137 | DecryptingKey::ctr,
|
64 | 138 | &TEST_KEY_128_BIT
|
65 | 139 | );
|
66 | 140 |
|
67 | 141 | block_api!(
|
68 |
| - aes_256_cbc_pkcs7, |
| 142 | + block_aes_256_cbc_pkcs7, |
69 | 143 | &AES_256,
|
70 | 144 | PaddedBlockEncryptingKey::cbc_pkcs7,
|
71 | 145 | PaddedBlockDecryptingKey::cbc_pkcs7,
|
72 | 146 | &TEST_KEY_256_BIT
|
73 | 147 | );
|
74 | 148 | block_api!(
|
75 |
| - aes_256_ctr, |
| 149 | + block_aes_256_ctr, |
76 | 150 | &AES_256,
|
77 | 151 | EncryptingKey::ctr,
|
78 | 152 | DecryptingKey::ctr,
|
|
0 commit comments