|
37 | 37 | end_per_testcase/2, |
38 | 38 |
|
39 | 39 | %% Test cases: |
40 | | - aead_bad_tag/1, |
41 | 40 | aead_ng/1, |
| 41 | + aead_bad_tag/1, |
| 42 | + aead_bad_key_length/1, |
42 | 43 | all_ciphers/1, |
43 | 44 | api_errors_ecdh/1, |
44 | 45 | api_errors_aead/1, |
@@ -483,9 +484,9 @@ groups() -> |
483 | 484 | {sm4_ofb, [], [api_ng, api_ng_one_shot]}, |
484 | 485 | {sm4_cfb, [], [api_ng, api_ng_one_shot]}, |
485 | 486 | {sm4_ctr, [], [api_ng, api_ng_one_shot]}, |
486 | | - {sm4_gcm, [], [aead_ng, aead_bad_tag]}, |
487 | | - {sm4_ccm, [], [aead_ng, aead_bad_tag]}, |
488 | | - {chacha20_poly1305, [], [aead_ng, aead_bad_tag]}, |
| 487 | + {sm4_gcm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 488 | + {sm4_ccm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 489 | + {chacha20_poly1305, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
489 | 490 | {chacha20, [], [api_ng, api_ng_one_shot]}, |
490 | 491 | {poly1305, [], [poly1305]}, |
491 | 492 | {no_poly1305, [], [no_poly1305]}, |
@@ -534,15 +535,15 @@ groups() -> |
534 | 535 | {aes_128_ctr, [], [api_ng, api_ng_one_shot]}, |
535 | 536 | {aes_192_ctr, [], [api_ng, api_ng_one_shot]}, |
536 | 537 | {aes_256_ctr, [], [api_ng, api_ng_one_shot]}, |
537 | | - {aes_128_ccm, [], [aead_ng, aead_bad_tag]}, |
538 | | - {aes_192_ccm, [], [aead_ng, aead_bad_tag]}, |
539 | | - {aes_256_ccm, [], [aead_ng, aead_bad_tag]}, |
| 538 | + {aes_128_ccm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 539 | + {aes_192_ccm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 540 | + {aes_256_ccm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
540 | 541 | {aes_128_ecb, [], [api_ng, api_ng_one_shot]}, |
541 | 542 | {aes_192_ecb, [], [api_ng, api_ng_one_shot]}, |
542 | 543 | {aes_256_ecb, [], [api_ng, api_ng_one_shot]}, |
543 | | - {aes_128_gcm, [], [aead_ng, aead_bad_tag]}, |
544 | | - {aes_192_gcm, [], [aead_ng, aead_bad_tag]}, |
545 | | - {aes_256_gcm, [], [aead_ng, aead_bad_tag]}, |
| 544 | + {aes_128_gcm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 545 | + {aes_192_gcm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 546 | + {aes_256_gcm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
546 | 547 | {aes_128_ofb, [], [api_ng, api_ng_one_shot]}, |
547 | 548 | {aes_192_ofb, [], [api_ng, api_ng_one_shot]}, |
548 | 549 | {aes_256_ofb, [], [api_ng, api_ng_one_shot]} |
@@ -1291,6 +1292,23 @@ aead_bad_tag(Config) -> |
1291 | 1292 | end, |
1292 | 1293 | do_cipher_tests(fun aead_cipher_bad_tag/1, FilteredAEADs). |
1293 | 1294 |
|
| 1295 | +%%-------------------------------------------------------------------- |
| 1296 | +aead_bad_key_length(Config) -> |
| 1297 | + [_|_] = AEADs = lazy_eval(proplists:get_value(cipher, Config)), |
| 1298 | + FilteredAEADs = |
| 1299 | + case proplists:get_bool(fips, Config) of |
| 1300 | + false -> |
| 1301 | + AEADs; |
| 1302 | + true -> |
| 1303 | + %% In FIPS mode, the IV length must be at least 12 bytes. |
| 1304 | + lists:filter( |
| 1305 | + fun(Tuple) -> |
| 1306 | + IVLen = byte_size(element(4, Tuple)), |
| 1307 | + IVLen >= 12 |
| 1308 | + end, AEADs) |
| 1309 | + end, |
| 1310 | + do_cipher_tests(fun aead_cipher_bad_key_length/1, FilteredAEADs). |
| 1311 | + |
1294 | 1312 | %%-------------------------------------------------------------------- |
1295 | 1313 | sign_verify() -> |
1296 | 1314 | [{doc, "Sign/verify digital signatures"}]. |
@@ -1989,6 +2007,39 @@ aead_cipher_bad_tag({Type, Key, _PlainText, IV, AAD, CipherText, CipherTag, TagL |
1989 | 2007 | fun() -> crypto:crypto_one_time_aead(Type, Key, IV, CipherText, AAD, BadTruncatedTag, false) end, |
1990 | 2008 | error). |
1991 | 2009 |
|
| 2010 | +aead_cipher_bad_key_length({Type, Key, PlainText, IV, AAD, _CipherText, _CipherTag, _Info}) -> |
| 2011 | + F = fun(K) -> |
| 2012 | + try crypto:crypto_one_time_aead(Type, K, IV, PlainText, AAD, true) of |
| 2013 | + Res1 -> ct:fail("Call should fail, but succeeded with return: ~p~n", [Res1]) |
| 2014 | + catch error : {badarg, _, _} -> ok |
| 2015 | + end, |
| 2016 | + try crypto:crypto_one_time_aead_init(Type, K, 1, true) of |
| 2017 | + Res2 -> ct:fail("Call should fail, but succeeded with return: ~p~n", [Res2]) |
| 2018 | + catch error : {badarg, _, _} -> ok |
| 2019 | + end |
| 2020 | + end, |
| 2021 | + KeyExtended = <<Key/binary, 1>>, |
| 2022 | + F(KeyExtended), |
| 2023 | + KeyTruncatedSize = byte_size(Key) - 1, |
| 2024 | + <<KeyTruncated:KeyTruncatedSize/binary, _/binary>> = Key, |
| 2025 | + F(KeyTruncated); |
| 2026 | +aead_cipher_bad_key_length({Type, Key, PlainText, IV, AAD, _CipherText, _CipherTag, TagLen, _Info}) -> |
| 2027 | + F = fun(K) -> |
| 2028 | + try crypto:crypto_one_time_aead(Type, K, IV, PlainText, AAD, TagLen, true) of |
| 2029 | + Res1 -> ct:fail("Call should fail, but succeeded with return: ~p~n", [Res1]) |
| 2030 | + catch error : {badarg, _, _} -> ok |
| 2031 | + end, |
| 2032 | + try crypto:crypto_one_time_aead_init(Type, K, TagLen, true) of |
| 2033 | + Res2 -> ct:fail("Call should fail, but succeeded with return: ~p~n", [Res2]) |
| 2034 | + catch error : {badarg, _, _} -> ok |
| 2035 | + end |
| 2036 | + end, |
| 2037 | + KeyExtended = <<Key/binary, 1>>, |
| 2038 | + F(KeyExtended), |
| 2039 | + KeyTruncatedSize = byte_size(Key) - 1, |
| 2040 | + <<KeyTruncated:KeyTruncatedSize/binary, _/binary>> = Key, |
| 2041 | + F(KeyTruncated). |
| 2042 | + |
1992 | 2043 |
|
1993 | 2044 | cipher_test(T, Fe, Ee, Fd, Ed) -> |
1994 | 2045 | %% Test encrypt |
|
0 commit comments