aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'lib/crypto')
-rw-r--r--lib/crypto/c_src/crypto.c29
-rw-r--r--lib/crypto/src/crypto.erl40
-rw-r--r--lib/crypto/test/crypto_SUITE.erl6
3 files changed, 41 insertions, 34 deletions
diff --git a/lib/crypto/c_src/crypto.c b/lib/crypto/c_src/crypto.c
index 1e6c2f24e7..e6bbfd8c38 100644
--- a/lib/crypto/c_src/crypto.c
+++ b/lib/crypto/c_src/crypto.c
@@ -2907,9 +2907,8 @@ static ERL_NIF_TERM bn2term(ErlNifEnv* env, const BIGNUM *bn)
return atom_undefined;
dlen = BN_num_bytes(bn);
- ptr = enif_make_new_binary(env, dlen+4, &ret);
- put_int32(ptr, dlen);
- BN_bn2bin(bn, ptr+4);
+ ptr = enif_make_new_binary(env, dlen, &ret);
+ BN_bn2bin(bn, ptr);
return ret;
}
@@ -3111,7 +3110,7 @@ static ERL_NIF_TERM term_to_ec_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_T
EC_GROUP *group = NULL;
EC_POINT *point = NULL;
- if (!(argv[1] == atom_undefined || get_bn_from_mpint(env, argv[1], &priv_key))
+ if (!(argv[1] == atom_undefined || get_bn_from_bin(env, argv[1], &priv_key))
|| !(argv[2] == atom_undefined || enif_is_binary(env, argv[2]))) {
printf("#1\n");
goto out_err;
@@ -3131,8 +3130,8 @@ static ERL_NIF_TERM term_to_ec_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_T
else if (enif_is_tuple(env, argv[0])
&& enif_get_tuple(env,argv[0],&c_arity,&curve)
&& c_arity == 5
- && get_bn_from_mpint(env, curve[3], &bn_order)
- && (curve[4] != atom_none && get_bn_from_mpint(env, curve[4], &cofactor))) {
+ && get_bn_from_bin(env, curve[3], &bn_order)
+ && (curve[4] != atom_none && get_bn_from_bin(env, curve[4], &cofactor))) {
//* {Field, Prime, Point, Order, CoFactor} = Curve */
int f_arity = -1;
@@ -3144,8 +3143,8 @@ static ERL_NIF_TERM term_to_ec_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_T
/* {A, B, Seed} = Prime */
if (!enif_get_tuple(env,curve[1],&p_arity,&prime)
- || !get_bn_from_mpint(env, prime[0], &a)
- || !get_bn_from_mpint(env, prime[1], &b))
+ || !get_bn_from_bin(env, prime[0], &a)
+ || !get_bn_from_bin(env, prime[1], &b))
goto out_err;
if (!enif_get_tuple(env,curve[0],&f_arity,&field))
@@ -3154,7 +3153,7 @@ static ERL_NIF_TERM term_to_ec_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_T
if (f_arity == 2 && field[0] == atom_prime_field) {
/* {prime_field, Prime} */
- if (!get_bn_from_mpint(env, field[1], &p))
+ if (!get_bn_from_bin(env, field[1], &p))
goto out_err;
if (BN_is_negative(p) || BN_is_zero(p))
@@ -3378,11 +3377,11 @@ static ERL_NIF_TERM ecdsa_sign_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM
digest = data_bin.data;
}
else {
- if (!inspect_mpint(env,argv[1],&data_bin)) {
+ if (!enif_inspect_binary(env,argv[1],&data_bin)) {
return enif_make_badarg(env);
}
digest = hmacbuf;
- digp->funcp(data_bin.data+4, data_bin.size-4, digest);
+ digp->funcp(data_bin.data, data_bin.size, digest);
}
enif_alloc_binary(ECDSA_size(obj->key), &ret_bin);
@@ -3425,7 +3424,7 @@ static ERL_NIF_TERM ecdsa_verify_nif(ErlNifEnv* env, int argc, const ERL_NIF_TER
return atom_notsup;
}
- if (!inspect_mpint(env, argv[2], &sign_bin)
+ if (!enif_inspect_binary(env, argv[2], &sign_bin)
|| !enif_get_resource(env, argv[3], res_type_ec_key, (void **)&obj))
return enif_make_badarg(env);
@@ -3438,16 +3437,16 @@ static ERL_NIF_TERM ecdsa_verify_nif(ErlNifEnv* env, int argc, const ERL_NIF_TER
}
digest = data_bin.data;
}
- else if (inspect_mpint(env, argv[1], &data_bin)) {
+ else if (enif_inspect_binary(env, argv[1], &data_bin)) {
digest = hmacbuf;
- digp->funcp(data_bin.data+4, data_bin.size-4, digest);
+ digp->funcp(data_bin.data, data_bin.size, digest);
}
else {
return enif_make_badarg(env);
}
i = ECDSA_verify(digp->NID_type, digest, digp->len,
- sign_bin.data+4, sign_bin.size-4, obj->key);
+ sign_bin.data, sign_bin.size, obj->key);
return (i==1 ? atom_true : atom_false);
#else
diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl
index 7558087d99..9503d0fcaa 100644
--- a/lib/crypto/src/crypto.erl
+++ b/lib/crypto/src/crypto.erl
@@ -839,9 +839,17 @@ dss_verify(_Type,_Data,_Signature,_Key) -> ?nif_stub.
% Key = [E,N] E=PublicExponent N=PublicModulus
rsa_verify(Data,Signature,Key) ->
- rsa_verify_nif(sha, Data,Signature,Key).
-rsa_verify(Type, DataOrDigest, Signature, Key) ->
- case rsa_verify_nif(Type, DataOrDigest, Signature, Key) of
+ rsa_verify(sha, Data,Signature,Key).
+rsa_verify(Type, Data, Signature, Key) when is_binary(Data) ->
+ verify(rsa, Type, mpint_to_bin(Data), mpint_to_bin(Signature), map_mpint_to_bin(Key));
+rsa_verify(Type, Digest, Signature, Key) ->
+ verify(rsa, Type, Digest, mpint_to_bin(Signature), map_mpint_to_bin(Key)).
+
+
+verify(dss, Type, Data, Signature, Key) ->
+ dss_verify_nif(Type, Data, Signature, map_ensure_int_as_bin(Key));
+verify(rsa, Type, DataOrDigest, Signature, Key) ->
+ case rsa_verify_nif(Type, DataOrDigest, Signature, map_ensure_int_as_bin(Key)) of
notsup -> erlang:error(notsup);
Bool -> Bool
end.
@@ -1178,13 +1186,13 @@ ec_key_new(_Curve) -> ?nif_stub.
ec_key_generate(_Key) -> ?nif_stub.
nif_prime_to_term({prime_field, Prime}) ->
- {prime_field, erlint(Prime)};
+ {prime_field, bin_to_int(Prime)};
nif_prime_to_term(PrimeField) ->
PrimeField.
nif_curve_to_term({A, B, Seed}) ->
- {erlint(A), erlint(B), Seed}.
+ {bin_to_int(A), bin_to_int(B), Seed}.
nif_curve_parameters_to_term({PrimeField, Curve, BasePoint, Order, CoFactor}) ->
- {nif_prime_to_term(PrimeField), nif_curve_to_term(Curve), BasePoint, erlint(Order), erlint(CoFactor)};
+ {nif_prime_to_term(PrimeField), nif_curve_to_term(Curve), BasePoint, bin_to_int(Order), bin_to_int(CoFactor)};
nif_curve_parameters_to_term(Curve) when is_atom(Curve) ->
%% named curve
Curve.
@@ -1193,7 +1201,7 @@ nif_curve_parameters_to_term(Curve) when is_atom(Curve) ->
ec_key_to_term(Key) ->
case ec_key_to_term_nif(Key) of
{Curve, PrivKey, PubKey} ->
- {nif_curve_parameters_to_term(Curve), erlint(PrivKey), PubKey};
+ {nif_curve_parameters_to_term(Curve), bin_to_int(PrivKey), PubKey};
_ ->
erlang:error(conversion_failed)
end.
@@ -1201,13 +1209,13 @@ ec_key_to_term(Key) ->
ec_key_to_term_nif(_Key) -> ?nif_stub.
term_to_nif_prime({prime_field, Prime}) ->
- {prime_field, mpint(Prime)};
+ {prime_field, int_to_bin(Prime)};
term_to_nif_prime(PrimeField) ->
PrimeField.
term_to_nif_curve({A, B, Seed}) ->
- {mpint(A), mpint(B), Seed}.
+ {int_to_bin(A), int_to_bin(B), Seed}.
term_to_nif_curve_parameters({PrimeField, Curve, BasePoint, Order, CoFactor}) ->
- {term_to_nif_prime(PrimeField), term_to_nif_curve(Curve), BasePoint, mpint(Order), mpint(CoFactor)};
+ {term_to_nif_prime(PrimeField), term_to_nif_curve(Curve), BasePoint, int_to_bin(Order), int_to_bin(CoFactor)};
term_to_nif_curve_parameters(Curve) when is_atom(Curve) ->
%% named curve
Curve.
@@ -1216,7 +1224,7 @@ term_to_nif_curve_parameters(Curve) when is_atom(Curve) ->
term_to_ec_key({Curve, undefined, PubKey}) ->
term_to_ec_key_nif(term_to_nif_curve_parameters(Curve), undefined, PubKey);
term_to_ec_key({Curve, PrivKey, PubKey}) ->
- term_to_ec_key_nif(term_to_nif_curve_parameters(Curve), mpint(PrivKey), PubKey).
+ term_to_ec_key_nif(term_to_nif_curve_parameters(Curve), int_to_bin(PrivKey), PubKey).
term_to_ec_key_nif(_Curve, _PrivKey, _PubKey) -> ?nif_stub.
@@ -1356,18 +1364,18 @@ int_to_bin_neg(X,Ds) ->
int_to_bin_neg(X bsr 8, [(X band 255)|Ds]).
-bin_to_int(Bin) ->
+bin_to_int(Bin) when is_binary(Bin) ->
Bits = bit_size(Bin),
<<Integer:Bits/integer>> = Bin,
- Integer.
+ Integer;
+bin_to_int(undefined) ->
+ undefined.
%% int from integer in a binary with 32bit length
erlint(<<MPIntSize:32/integer,MPIntValue/binary>>) ->
Bits= MPIntSize * 8,
<<Integer:Bits/integer>> = MPIntValue,
- Integer;
-erlint(undefined) ->
- undefined.
+ Integer.
mpint_to_bin(<<Len:32, Bin:Len/binary>>) ->
Bin.
diff --git a/lib/crypto/test/crypto_SUITE.erl b/lib/crypto/test/crypto_SUITE.erl
index c5597be34c..b8a041cf8f 100644
--- a/lib/crypto/test/crypto_SUITE.erl
+++ b/lib/crypto/test/crypto_SUITE.erl
@@ -1906,9 +1906,9 @@ ec_do() ->
?line CsCaKey = crypto:ec_key_to_term(T3),
Msg = <<99,234,6,64,190,237,201,99,80,248,58,40,70,45,149,218,5,246,242,63>>,
- Sign = crypto:ecdsa_sign(sha, sized_binary(Msg), L2),
- ?line true = crypto:ecdsa_verify(sha, sized_binary(Msg), sized_binary(Sign), L2),
- ?line false = crypto:ecdsa_verify(sha, sized_binary(Msg), sized_binary(<<10,20>>), L2),
+ Sign = crypto:ecdsa_sign(sha, Msg, L2),
+ ?line true = crypto:ecdsa_verify(sha, Msg, Sign, L2),
+ ?line false = crypto:ecdsa_verify(sha, Msg, <<10,20>>, L2),
ok.