Skip to content

Commit 6f8ac59

Browse files
committed
Merge branch 'maint'
2 parents f629eb6 + 6aac650 commit 6f8ac59

3 files changed

Lines changed: 66 additions & 11 deletions

File tree

lib/crypto/c_src/aead.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ ERL_NIF_TERM aead_cipher_init_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM a
134134
{ret = EXCP_ERROR(env, "Can't allocate ctx"); goto done;}
135135
if (EVP_CipherInit_ex(ctx_res->ctx, ctx_res->cipherp->cipher.p, NULL, NULL, NULL, ctx_res->encflg) != 1)
136136
{ret = EXCP_ERROR(env, "CipherInit failed"); goto done;}
137+
if (!EVP_CIPHER_CTX_set_key_length(ctx_res->ctx, (int)key.size))
138+
{ret = EXCP_BADARG_N(env, 1, "Bad Key length"); goto done;}
137139

138140
ret = enif_make_resource(env, ctx_res);
139141

@@ -233,6 +235,8 @@ ERL_NIF_TERM aead_cipher_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]
233235
{ret = EXCP_ERROR(env, "Can't allocate ctx"); goto done;}
234236
if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, encflg) != 1)
235237
{ret = EXCP_ERROR(env, "CipherInit failed"); goto done;}
238+
if (!EVP_CIPHER_CTX_set_key_length(ctx, (int)key.size))
239+
{ret = EXCP_BADARG_N(env, 1, "Bad Key length"); goto done;}
236240

237241
} else {
238242
/* argc = 4 {state, IV, InData, AAD } */

lib/crypto/c_src/cipher.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static struct cipher_type_t cipher_types[] =
127127

128128
/*==== AEAD ciphers ====*/
129129
#if defined(HAVE_CHACHA20_POLY1305)
130-
{{"chacha20_poly1305"}, "chacha20-poly1305", {&EVP_chacha20_poly1305}, 0, NO_FIPS_CIPHER | AEAD_CIPHER, AEAD_CTRL},
130+
{{"chacha20_poly1305"}, "chacha20-poly1305", {&EVP_chacha20_poly1305}, 32, NO_FIPS_CIPHER | AEAD_CIPHER, AEAD_CTRL},
131131
#else
132132
{{"chacha20_poly1305"}, "chacha20-poly1305", {NULL}, 0, NO_FIPS_CIPHER | AEAD_CIPHER, {{0,0,0}}},
133133
#endif

lib/crypto/test/crypto_SUITE.erl

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
end_per_testcase/2,
3838

3939
%% Test cases:
40-
aead_bad_tag/1,
4140
aead_ng/1,
41+
aead_bad_tag/1,
42+
aead_bad_key_length/1,
4243
all_ciphers/1,
4344
api_errors_ecdh/1,
4445
api_errors_aead/1,
@@ -483,9 +484,9 @@ groups() ->
483484
{sm4_ofb, [], [api_ng, api_ng_one_shot]},
484485
{sm4_cfb, [], [api_ng, api_ng_one_shot]},
485486
{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]},
489490
{chacha20, [], [api_ng, api_ng_one_shot]},
490491
{poly1305, [], [poly1305]},
491492
{no_poly1305, [], [no_poly1305]},
@@ -534,15 +535,15 @@ groups() ->
534535
{aes_128_ctr, [], [api_ng, api_ng_one_shot]},
535536
{aes_192_ctr, [], [api_ng, api_ng_one_shot]},
536537
{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]},
540541
{aes_128_ecb, [], [api_ng, api_ng_one_shot]},
541542
{aes_192_ecb, [], [api_ng, api_ng_one_shot]},
542543
{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]},
546547
{aes_128_ofb, [], [api_ng, api_ng_one_shot]},
547548
{aes_192_ofb, [], [api_ng, api_ng_one_shot]},
548549
{aes_256_ofb, [], [api_ng, api_ng_one_shot]}
@@ -1291,6 +1292,23 @@ aead_bad_tag(Config) ->
12911292
end,
12921293
do_cipher_tests(fun aead_cipher_bad_tag/1, FilteredAEADs).
12931294

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+
12941312
%%--------------------------------------------------------------------
12951313
sign_verify() ->
12961314
[{doc, "Sign/verify digital signatures"}].
@@ -1989,6 +2007,39 @@ aead_cipher_bad_tag({Type, Key, _PlainText, IV, AAD, CipherText, CipherTag, TagL
19892007
fun() -> crypto:crypto_one_time_aead(Type, Key, IV, CipherText, AAD, BadTruncatedTag, false) end,
19902008
error).
19912009

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+
19922043

19932044
cipher_test(T, Fe, Ee, Fd, Ed) ->
19942045
%% Test encrypt

0 commit comments

Comments
 (0)