From fcc89ded7f5cb5f81533883c30e7daa89195970d Mon Sep 17 00:00:00 2001 From: Hans Nilsson Date: Thu, 28 Feb 2019 19:59:30 +0100 Subject: crypto: Remove condition of block size Unnecessary, because the underlying crypto libraries handles this case. Also: - Relax the condition of binary Key and IV -Fix bug for empty data on historic cryptolibs because tests fails for empty data on at least aes_cfb8 on OpenSSL 0.9.8h. It does not fail on OpenSSL 0.9.8zh. --- lib/crypto/c_src/api_ng.c | 2 -- lib/crypto/src/crypto.erl | 21 +++++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/crypto/c_src/api_ng.c b/lib/crypto/c_src/api_ng.c index c4114d1626..f24aded748 100644 --- a/lib/crypto/c_src/api_ng.c +++ b/lib/crypto/c_src/api_ng.c @@ -165,8 +165,6 @@ ERL_NIF_TERM ng_crypto_update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[ ASSERT(in_data_bin.size =< INT_MAX); block_size = EVP_CIPHER_CTX_block_size(ctx->ctx); - if (in_data_bin.size % (size_t)block_size != 0) - return ERROR_Str(env, "Data not a multiple of block size"); if (argc==3) { if (!enif_inspect_iolist_as_binary(env, argv[2], &ivec_bin)) diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl index 97a4a7a3f0..8d6d24210a 100644 --- a/lib/crypto/src/crypto.erl +++ b/lib/crypto/src/crypto.erl @@ -2252,15 +2252,15 @@ check_otp_test_engine(LibDir) -> | block_cipher_with_iv() | block_cipher_without_iv() , Key :: iodata(), - IV :: binary(), + IV :: iodata(), EncryptFlag :: boolean() | undefined, State :: crypto_state() . -crypto_init(Cipher, Key, IV, EncryptFlag) when is_atom(Cipher), - is_binary(Key), - is_binary(IV), - is_atom(EncryptFlag) -> - case ng_crypto_init_nif(alias(Cipher), Key, IV, EncryptFlag) of +crypto_init(Cipher, Key, IV, EncryptFlag) -> + case ng_crypto_init_nif(alias(Cipher), + iolist_to_binary(Key), + iolist_to_binary(IV), + EncryptFlag) of {error,Error} -> {error,Error}; undefined -> % For compatibility function crypto_stream_init/3 @@ -2284,8 +2284,13 @@ crypto_init(Cipher, Key, IV, EncryptFlag) when is_atom(Cipher), when State :: crypto_state(), Data :: iodata(), Result :: binary() | {crypto_state(),binary()}. -crypto_update(State, Data) -> - mk_ret(ng_crypto_update_nif(State, Data)). +crypto_update(State, Data0) -> + case iolist_to_binary(Data0) of + <<>> -> + <<>>; % Known to fail on OpenSSL 0.9.8h + Data -> + mk_ret(ng_crypto_update_nif(State, Data)) + end. %%%---------------------------------------------------------------- %%% -- cgit v1.2.3