generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 86
Adds CMAC #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Elvrarin
wants to merge
12
commits into
aws:main
Choose a base branch
from
Elvrarin:cmac-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Adds CMAC #903
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fbfe94f
Adds CMAC
9046ea1
change static to const
d4be093
Use LcPtr
04f19b3
Removed SecureRandom parameter
2750fa9
Expose fallibility of Key::new, update, sign
e518c57
Used AlgorithmId enum to provide EVP_CIPHER
c81a8f1
Fix clippy warnings
eca49b3
Minor changes
07a6171
Merge branch 'aws:main' into cmac-2
Elvrarin e525da0
Adding panic section for the docs
c661b48
Fix formatting and Copyright messages
baead26
Merge branch 'main' into cmac-2
justsmth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,40 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR ISC | ||
|
|
||
| #![cfg(debug_assertions)] | ||
|
|
||
| use crate::cmac::{sign, verify, Key, AES_128, AES_192, AES_256, TDES_FOR_LEGACY_USE_ONLY}; | ||
| use crate::fips::{assert_fips_status_indicator, FipsServiceStatus}; | ||
| use crate::rand::{self, SystemRandom}; | ||
|
|
||
| const TEST_MESSAGE: &str = "test message"; | ||
|
|
||
| macro_rules! cmac_api { | ||
| ($name:ident, $alg:expr, $key_len:expr, $expect:path) => { | ||
| #[test] | ||
| fn $name() { | ||
| let rng = SystemRandom::new(); | ||
|
|
||
| let key_value: [u8; $key_len] = rand::generate(&rng).unwrap().expose(); | ||
|
|
||
| let s_key = Key::new($alg, key_value.as_ref()); | ||
|
|
||
| let tag = assert_fips_status_indicator!( | ||
| sign(&s_key, TEST_MESSAGE.as_bytes()), | ||
| $expect | ||
| ); | ||
|
|
||
| let v_key = Key::new($alg, key_value.as_ref()); | ||
|
|
||
| assert_fips_status_indicator!( | ||
| verify(&v_key, TEST_MESSAGE.as_bytes(), tag.as_ref()), | ||
| $expect | ||
| ).unwrap(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| cmac_api!(aes_128, AES_128, 16, FipsServiceStatus::Approved); | ||
| cmac_api!(aes_192, AES_192, 24, FipsServiceStatus::NonApproved); | ||
| cmac_api!(aes_256, AES_256, 32, FipsServiceStatus::Approved); | ||
| cmac_api!(tdes, TDES_FOR_LEGACY_USE_ONLY, 24, FipsServiceStatus::NonApproved); |
This file contains hidden or 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 hidden or 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 hidden or 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,139 @@ | ||
| use aws_lc_rs::{cmac, test, test_file}; | ||
|
|
||
| #[test] | ||
| fn cavp_cmac_aes128_tests() { | ||
| test::run(test_file!("data/cavp_aes128_cmac_tests.txt"), |section, test_case| { | ||
| assert_eq!(section, ""); | ||
|
|
||
| let _count = test_case.consume_usize("Count"); | ||
| let _klen = test_case.consume_usize("Klen"); | ||
| let mlen = test_case.consume_usize("Mlen"); | ||
| let tlen = test_case.consume_usize("Tlen"); | ||
| let key = test_case.consume_bytes("Key"); | ||
| let msg = test_case.consume_bytes("Msg"); | ||
| let mac = test_case.consume_bytes("Mac"); | ||
| let result = test_case.consume_string("Result"); | ||
|
|
||
| let input = if mlen == 0 { Vec::new() } else { msg }; | ||
| let should_pass = result.chars().next().unwrap() == 'P'; | ||
|
|
||
| let cmac_key = cmac::Key::new(cmac::AES_128, &key); | ||
| let signature = cmac::sign(&cmac_key, &input); | ||
|
|
||
| // Truncate to tlen | ||
| let truncated_sig = &signature.as_ref()[..std::cmp::min(signature.as_ref().len(), tlen)]; | ||
|
|
||
| if should_pass { | ||
| assert_eq!(truncated_sig, &mac); | ||
| } else { | ||
| assert_ne!(truncated_sig, &mac); | ||
| } | ||
|
|
||
| Ok(()) | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn cavp_cmac_aes192_tests() { | ||
| test::run(test_file!("data/cavp_aes192_cmac_tests.txt"), |section, test_case| { | ||
| assert_eq!(section, ""); | ||
|
|
||
| let _count = test_case.consume_usize("Count"); | ||
| let _klen = test_case.consume_usize("Klen"); | ||
| let mlen = test_case.consume_usize("Mlen"); | ||
| let tlen = test_case.consume_usize("Tlen"); | ||
| let key = test_case.consume_bytes("Key"); | ||
| let msg = test_case.consume_bytes("Msg"); | ||
| let mac = test_case.consume_bytes("Mac"); | ||
| let result = test_case.consume_string("Result"); | ||
|
|
||
| let input = if mlen == 0 { Vec::new() } else { msg }; | ||
| let should_pass = result.chars().next().unwrap() == 'P'; | ||
|
|
||
| let cmac_key = cmac::Key::new(cmac::AES_192, &key); | ||
| let signature = cmac::sign(&cmac_key, &input); | ||
|
|
||
| // Truncate to tlen | ||
| let truncated_sig = &signature.as_ref()[..std::cmp::min(signature.as_ref().len(), tlen)]; | ||
|
|
||
| if should_pass { | ||
| assert_eq!(truncated_sig, &mac); | ||
| } else { | ||
| assert_ne!(truncated_sig, &mac); | ||
| } | ||
|
|
||
| Ok(()) | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn cavp_cmac_aes256_tests() { | ||
| test::run(test_file!("data/cavp_aes256_cmac_tests.txt"), |section, test_case| { | ||
| assert_eq!(section, ""); | ||
|
|
||
| let _count = test_case.consume_usize("Count"); | ||
| let _klen = test_case.consume_usize("Klen"); | ||
| let mlen = test_case.consume_usize("Mlen"); | ||
| let tlen = test_case.consume_usize("Tlen"); | ||
| let key = test_case.consume_bytes("Key"); | ||
| let msg = test_case.consume_bytes("Msg"); | ||
| let mac = test_case.consume_bytes("Mac"); | ||
| let result = test_case.consume_string("Result"); | ||
|
|
||
| let input = if mlen == 0 { Vec::new() } else { msg }; | ||
| let should_pass = result.chars().next().unwrap() == 'P'; | ||
|
|
||
| let cmac_key = cmac::Key::new(cmac::AES_256, &key); | ||
| let signature = cmac::sign(&cmac_key, &input); | ||
|
|
||
| // Truncate to tlen | ||
| let truncated_sig = &signature.as_ref()[..std::cmp::min(signature.as_ref().len(), tlen)]; | ||
|
|
||
| if should_pass { | ||
| assert_eq!(truncated_sig, &mac); | ||
| } else { | ||
| assert_ne!(truncated_sig, &mac); | ||
| } | ||
|
|
||
| Ok(()) | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn cavp_cmac_3des_tests() { | ||
| test::run(test_file!("data/cavp_3des_cmac_tests.txt"), |section, test_case| { | ||
| assert_eq!(section, ""); | ||
|
|
||
| let _count = test_case.consume_usize("Count"); | ||
| let _klen = test_case.consume_usize("Klen"); | ||
| let mlen = test_case.consume_usize("Mlen"); | ||
| let tlen = test_case.consume_usize("Tlen"); | ||
| let key1 = test_case.consume_bytes("Key1"); | ||
| let key2 = test_case.consume_bytes("Key2"); | ||
| let key3 = test_case.consume_bytes("Key3"); | ||
| let msg = test_case.consume_bytes("Msg"); | ||
| let mac = test_case.consume_bytes("Mac"); | ||
| let result = test_case.consume_string("Result"); | ||
|
|
||
| // Combine 3DES keys | ||
| let mut combined_key = key1; | ||
| combined_key.extend(key2); | ||
| combined_key.extend(key3); | ||
|
|
||
| let input = if mlen == 0 { Vec::new() } else { msg }; | ||
| let should_pass = result.chars().next().unwrap() == 'P'; | ||
|
|
||
| let cmac_key = cmac::Key::new(cmac::TDES_FOR_LEGACY_USE_ONLY, &combined_key); | ||
| let signature = cmac::sign(&cmac_key, &input); | ||
|
|
||
| // Truncate to tlen | ||
| let truncated_sig = &signature.as_ref()[..std::cmp::min(signature.as_ref().len(), tlen)]; | ||
|
|
||
| if should_pass { | ||
| assert_eq!(truncated_sig, &mac); | ||
| } else { | ||
| assert_ne!(truncated_sig, &mac); | ||
| } | ||
| Ok(()) | ||
| }); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The truncation used here and in the other functions is not needed -- it would actually hide a bug were we to produce too long of a signature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied these tests data, and test functions from the aws-lc. The original tests also does a truncation. Without the truncation, the tests start to fail.
https://github.com/aws/aws-lc/blob/main/crypto/fipsmodule/cmac/cmac_test.cc#L226-L227
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, we should just trancate it to
tlenthen -- it should never be longer than the actual tag: