|
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, |
@@ -477,9 +478,9 @@ groups() -> |
477 | 478 | {sm4_ofb, [], [api_ng, api_ng_one_shot]}, |
478 | 479 | {sm4_cfb, [], [api_ng, api_ng_one_shot]}, |
479 | 480 | {sm4_ctr, [], [api_ng, api_ng_one_shot]}, |
480 | | - {sm4_gcm, [], [aead_ng, aead_bad_tag]}, |
481 | | - {sm4_ccm, [], [aead_ng, aead_bad_tag]}, |
482 | | - {chacha20_poly1305, [], [aead_ng, aead_bad_tag]}, |
| 481 | + {sm4_gcm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 482 | + {sm4_ccm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 483 | + {chacha20_poly1305, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
483 | 484 | {chacha20, [], [api_ng, api_ng_one_shot]}, |
484 | 485 | {poly1305, [], [poly1305]}, |
485 | 486 | {no_poly1305, [], [no_poly1305]}, |
@@ -526,15 +527,15 @@ groups() -> |
526 | 527 | {aes_128_ctr, [], [api_ng, api_ng_one_shot]}, |
527 | 528 | {aes_192_ctr, [], [api_ng, api_ng_one_shot]}, |
528 | 529 | {aes_256_ctr, [], [api_ng, api_ng_one_shot]}, |
529 | | - {aes_128_ccm, [], [aead_ng, aead_bad_tag]}, |
530 | | - {aes_192_ccm, [], [aead_ng, aead_bad_tag]}, |
531 | | - {aes_256_ccm, [], [aead_ng, aead_bad_tag]}, |
| 530 | + {aes_128_ccm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 531 | + {aes_192_ccm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 532 | + {aes_256_ccm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
532 | 533 | {aes_128_ecb, [], [api_ng, api_ng_one_shot]}, |
533 | 534 | {aes_192_ecb, [], [api_ng, api_ng_one_shot]}, |
534 | 535 | {aes_256_ecb, [], [api_ng, api_ng_one_shot]}, |
535 | | - {aes_128_gcm, [], [aead_ng, aead_bad_tag]}, |
536 | | - {aes_192_gcm, [], [aead_ng, aead_bad_tag]}, |
537 | | - {aes_256_gcm, [], [aead_ng, aead_bad_tag]}, |
| 536 | + {aes_128_gcm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 537 | + {aes_192_gcm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
| 538 | + {aes_256_gcm, [], [aead_ng, aead_bad_tag, aead_bad_key_length]}, |
538 | 539 | {aes_128_ofb, [], [api_ng, api_ng_one_shot]}, |
539 | 540 | {aes_192_ofb, [], [api_ng, api_ng_one_shot]}, |
540 | 541 | {aes_256_ofb, [], [api_ng, api_ng_one_shot]} |
@@ -1211,6 +1212,23 @@ aead_bad_tag(Config) -> |
1211 | 1212 | end, |
1212 | 1213 | do_cipher_tests(fun aead_cipher_bad_tag/1, FilteredAEADs). |
1213 | 1214 |
|
| 1215 | +%%-------------------------------------------------------------------- |
| 1216 | +aead_bad_key_length(Config) -> |
| 1217 | + [_|_] = AEADs = lazy_eval(proplists:get_value(cipher, Config)), |
| 1218 | + FilteredAEADs = |
| 1219 | + case proplists:get_bool(fips, Config) of |
| 1220 | + false -> |
| 1221 | + AEADs; |
| 1222 | + true -> |
| 1223 | + %% In FIPS mode, the IV length must be at least 12 bytes. |
| 1224 | + lists:filter( |
| 1225 | + fun(Tuple) -> |
| 1226 | + IVLen = byte_size(element(4, Tuple)), |
| 1227 | + IVLen >= 12 |
| 1228 | + end, AEADs) |
| 1229 | + end, |
| 1230 | + do_cipher_tests(fun aead_cipher_bad_key_length/1, FilteredAEADs). |
| 1231 | + |
1214 | 1232 | %%-------------------------------------------------------------------- |
1215 | 1233 | sign_verify() -> |
1216 | 1234 | [{doc, "Sign/verify digital signatures"}]. |
@@ -1909,6 +1927,39 @@ aead_cipher_bad_tag({Type, Key, _PlainText, IV, AAD, CipherText, CipherTag, TagL |
1909 | 1927 | fun() -> crypto:crypto_one_time_aead(Type, Key, IV, CipherText, AAD, BadTruncatedTag, false) end, |
1910 | 1928 | error). |
1911 | 1929 |
|
| 1930 | +aead_cipher_bad_key_length({Type, Key, PlainText, IV, AAD, _CipherText, _CipherTag, _Info}) -> |
| 1931 | + F = fun(K) -> |
| 1932 | + try crypto:crypto_one_time_aead(Type, K, IV, PlainText, AAD, true) of |
| 1933 | + Res1 -> ct:fail("Call should fail, but succeeded with return: ~p~n", [Res1]) |
| 1934 | + catch error : {badarg, _, _} -> ok |
| 1935 | + end, |
| 1936 | + try crypto:crypto_one_time_aead_init(Type, K, 1, true) of |
| 1937 | + Res2 -> ct:fail("Call should fail, but succeeded with return: ~p~n", [Res2]) |
| 1938 | + catch error : {badarg, _, _} -> ok |
| 1939 | + end |
| 1940 | + end, |
| 1941 | + KeyExtended = <<Key/binary, 1>>, |
| 1942 | + F(KeyExtended), |
| 1943 | + KeyTruncatedSize = byte_size(Key) - 1, |
| 1944 | + <<KeyTruncated:KeyTruncatedSize/binary, _/binary>> = Key, |
| 1945 | + F(KeyTruncated); |
| 1946 | +aead_cipher_bad_key_length({Type, Key, PlainText, IV, AAD, _CipherText, _CipherTag, TagLen, _Info}) -> |
| 1947 | + F = fun(K) -> |
| 1948 | + try crypto:crypto_one_time_aead(Type, K, IV, PlainText, AAD, TagLen, true) of |
| 1949 | + Res1 -> ct:fail("Call should fail, but succeeded with return: ~p~n", [Res1]) |
| 1950 | + catch error : {badarg, _, _} -> ok |
| 1951 | + end, |
| 1952 | + try crypto:crypto_one_time_aead_init(Type, K, TagLen, true) of |
| 1953 | + Res2 -> ct:fail("Call should fail, but succeeded with return: ~p~n", [Res2]) |
| 1954 | + catch error : {badarg, _, _} -> ok |
| 1955 | + end |
| 1956 | + end, |
| 1957 | + KeyExtended = <<Key/binary, 1>>, |
| 1958 | + F(KeyExtended), |
| 1959 | + KeyTruncatedSize = byte_size(Key) - 1, |
| 1960 | + <<KeyTruncated:KeyTruncatedSize/binary, _/binary>> = Key, |
| 1961 | + F(KeyTruncated). |
| 1962 | + |
1912 | 1963 |
|
1913 | 1964 | cipher_test(T, Fe, Ee, Fd, Ed) -> |
1914 | 1965 | %% Test encrypt |
|
0 commit comments