From f706e003a2f6cb3f1f31b9d1294b379c2ab3affe Mon Sep 17 00:00:00 2001
From: Ingela Anderton Andin
Date: Thu, 16 May 2013 12:20:36 +0200
Subject: ssh & crypto: Remove use of deprecated crypto functions from ssh
---
lib/crypto/doc/src/crypto.xml | 16 +++++++-
lib/crypto/src/crypto.erl | 6 ++-
lib/ssh/src/ssh_bits.erl | 27 +-----------
lib/ssh/src/ssh_math.erl | 96 ++-----------------------------------------
lib/ssh/src/ssh_transport.erl | 41 +++++++++---------
5 files changed, 44 insertions(+), 142 deletions(-)
(limited to 'lib')
diff --git a/lib/crypto/doc/src/crypto.xml b/lib/crypto/doc/src/crypto.xml
index df765ade87..cac8f6ef28 100644
--- a/lib/crypto/doc/src/crypto.xml
+++ b/lib/crypto/doc/src/crypto.xml
@@ -186,6 +186,20 @@
+
+
+ binary_to_integer(Bin) -> Integer
+ Convert binary representation, of an integer, to an Erlang integer.
+
+ Bin = binary() - as returned by crypto functions
+
+ Integer = integer()
+
+
+ Convert binary representation, of an integer, to an Erlang integer.
+
+
+
compute_key(Type, OthersPublicKey, MyPrivateKey, Params) -> SharedSecret
@@ -410,7 +424,7 @@
next_iv(Type, Data) ->
- Type = des_cbc | aes_cbc
+ Type = des_cbc | des3_cbc |aes_cbc | des_cfb
Data = iodata()
diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl
index f4e157198c..dde53b1217 100644
--- a/lib/crypto/src/crypto.erl
+++ b/lib/crypto/src/crypto.erl
@@ -21,7 +21,7 @@
-module(crypto).
--export([start/0, stop/0, info_lib/0, algorithms/0, version/0]).
+-export([start/0, stop/0, info_lib/0, algorithms/0, version/0, binary_to_integer/1]).
-export([hash/2, hash_init/1, hash_update/2, hash_final/1]).
-export([sign/4, verify/5]).
-export([generate_key/2, generate_key/3, compute_key/4]).
@@ -33,9 +33,9 @@
-export([stream_init/2, stream_init/3, stream_encrypt/2, stream_decrypt/2]).
-export([public_encrypt/4, private_decrypt/4]).
-export([private_encrypt/4, public_decrypt/4]).
-
-export([dh_generate_parameters/2, dh_check/1]). %% Testing see
+
%% DEPRECATED
%% Replaced by hash_*
-export([md4/1, md4_init/0, md4_update/2, md4_final/1]).
@@ -1598,6 +1598,8 @@ int_to_bin_neg(-1, Ds=[MSB|_]) when MSB >= 16#80 ->
int_to_bin_neg(X,Ds) ->
int_to_bin_neg(X bsr 8, [(X band 255)|Ds]).
+binary_to_integer(Bin) ->
+ bin_to_int(Bin).
bin_to_int(Bin) when is_binary(Bin) ->
Bits = bit_size(Bin),
diff --git a/lib/ssh/src/ssh_bits.erl b/lib/ssh/src/ssh_bits.erl
index 5841f06d70..fc6efc817f 100644
--- a/lib/ssh/src/ssh_bits.erl
+++ b/lib/ssh/src/ssh_bits.erl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2005-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2005-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -33,7 +33,6 @@
%% integer utils
-export([isize/1]).
--export([irandom/1, irandom/3]).
-export([random/1]).
-export([xor_bits/2, fill_bits/2]).
-export([i2bin/2, bin2i/1]).
@@ -387,31 +386,7 @@ xor_bits(XBits, YBits) ->
<> = YBits,
<<(X bxor Y):Sz>>.
-%%
-%% irandom(N)
-%%
-%% Generate a N bits size random number
-%% note that the top most bit is always set
-%% to guarantee that the number is N bits
-%%
-irandom(Bits) ->
- irandom(Bits, 1, 0).
-
-%%
-%% irandom(N, Top, Bottom)
-%%
-%% Generate a N bits size random number
-%% Where Top = 0 - do not set top bit
-%% = 1 - set the most significant bit
-%% = 2 - set two most significant bits
-%% Bot = 0 - do not set the least signifcant bit
-%% Bot = 1 - set the least signifcant bit (i.e always odd)
-%%
-irandom(Bits, Top, Bottom) when is_integer(Top),
- 0 =< Top, Top =< 2 ->
- crypto:erlint(crypto:strong_rand_mpint(Bits, Top - 1, Bottom)).
-%%
%% random/1
%% Generate N random bytes
%%
diff --git a/lib/ssh/src/ssh_math.erl b/lib/ssh/src/ssh_math.erl
index 4aa385b18d..e05964daa1 100644
--- a/lib/ssh/src/ssh_math.erl
+++ b/lib/ssh/src/ssh_math.erl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2005-2011. All Rights Reserved.
+%% Copyright Ericsson AB 2005-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -23,109 +23,19 @@
-module(ssh_math).
--export([ilog2/1, ipow/3, invert/2, ipow2/3]).
+-export([ipow/3]).
-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% INTEGER utils
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% number of bits (used) in a integer = isize(N) = |log2(N)|+1
-ilog2(N) ->
- ssh_bits:isize(N) - 1.
-
-
%% calculate A^B mod M
ipow(A, B, M) when M > 0, B >= 0 ->
- crypto:mod_exp(A, B, M).
-
-ipow2(A, B, M) when M > 0, B >= 0 ->
- if A == 1 ->
- 1;
- true ->
- ipow2(A, B, M, 1)
- end.
-
-ipow2(A, 1, M, Prod) ->
- (A*Prod) rem M;
-ipow2(_A, 0, _M, Prod) ->
- Prod;
-ipow2(A, B, M, Prod) ->
- B1 = B bsr 1,
- A1 = (A*A) rem M,
- if B - B1 == B1 ->
- ipow2(A1, B1, M, Prod);
- true ->
- ipow2(A1, B1, M, (A*Prod) rem M)
- end.
-
-%% %%
-%% %% Normal gcd
-%% %%
-%% gcd(R, Q) when abs(Q) < abs(R) -> gcd1(Q,R);
-%% gcd(R, Q) -> gcd1(R,Q).
-
-%% gcd1(0, Q) -> Q;
-%% gcd1(R, Q) ->
-%% gcd1(Q rem R, R).
-
-
-%% %%
-%% %% Least common multiple of (R,Q)
-%% %%
-%% lcm(0, _Q) -> 0;
-%% lcm(_R, 0) -> 0;
-%% lcm(R, Q) ->
-%% (Q div gcd(R, Q)) * R.
-
-%% %%
-%% %% Extended gcd gcd(R,Q) -> {G, {A,B}} such that G == R*A + Q*B
-%% %%
-%% %% Here we could have use for a bif divrem(Q, R) -> {Quote, Remainder}
-%% %%
-%% egcd(R,Q) when abs(Q) < abs(R) -> egcd1(Q,R,1,0,0,1);
-%% egcd(R,Q) -> egcd1(R,Q,0,1,1,0).
-
-%% egcd1(0,Q,_,_,Q1,Q2) -> {Q, {Q2,Q1}};
-%% egcd1(R,Q,R1,R2,Q1,Q2) ->
-%% D = Q div R,
-%% egcd1(Q rem R, R, Q1-D*R1, Q2-D*R2, R1, R2).
-
-%%
-%% Invert an element X mod P
-%% Calculated as {1, {A,B}} = egcd(X,P),
-%% 1 == P*A + X*B == X*B (mod P) i.e B is the inverse element
-%%
-%% X > 0, P > 0, X < P (P should be prime)
-%%
-invert(X,P) when X > 0, P > 0, X < P ->
- I = inv(X,P,1,0),
- if
- I < 0 -> P + I;
- true -> I
- end.
-
-inv(0,_,_,Q) -> Q;
-inv(X,P,R1,Q1) ->
- D = P div X,
- inv(P rem X, X, Q1 - D*R1, R1).
-
+ crypto:binary_to_integer(crypto:mod_pow(A, B, M)).
-%% %%
-%% %% Integer square root
-%% %%
-%% isqrt(0) -> 0;
-%% isqrt(1) -> 1;
-%% isqrt(X) when X >= 0 ->
-%% R = X div 2,
-%% isqrt(X div R, R, X).
-%% isqrt(Q,R,X) when Q < R ->
-%% R1 = (R+Q) div 2,
-%% isqrt(X div R1, R1, X);
-%% isqrt(_, R, _) -> R.
diff --git a/lib/ssh/src/ssh_transport.erl b/lib/ssh/src/ssh_transport.erl
index 98d59d01de..e3d1d3c8a0 100644
--- a/lib/ssh/src/ssh_transport.erl
+++ b/lib/ssh/src/ssh_transport.erl
@@ -792,14 +792,14 @@ encrypt(#ssh{encrypt = none} = Ssh, Data) ->
encrypt(#ssh{encrypt = '3des-cbc',
encrypt_keys = {K1,K2,K3},
encrypt_ctx = IV0} = Ssh, Data) ->
- Enc = crypto:des3_cbc_encrypt(K1,K2,K3,IV0,Data),
- IV = crypto:des_cbc_ivec(Enc),
+ Enc = crypto:block_encrypt(des3_cbc, [K1,K2,K3], IV0, Data),
+ IV = crypto:next_iv(des3_cbc, Enc),
{Ssh#ssh{encrypt_ctx = IV}, Enc};
encrypt(#ssh{encrypt = 'aes128-cbc',
encrypt_keys = K,
encrypt_ctx = IV0} = Ssh, Data) ->
- Enc = crypto:aes_cbc_128_encrypt(K,IV0,Data),
- IV = crypto:aes_cbc_ivec(Enc),
+ Enc = crypto:block_encrypt(aes_cbc128, K,IV0,Data),
+ IV = crypto:next_iv(aes_cbc, Enc),
{Ssh#ssh{encrypt_ctx = IV}, Enc}.
@@ -846,13 +846,13 @@ decrypt(#ssh{decrypt = none} = Ssh, Data) ->
decrypt(#ssh{decrypt = '3des-cbc', decrypt_keys = Keys,
decrypt_ctx = IV0} = Ssh, Data) ->
{K1, K2, K3} = Keys,
- Dec = crypto:des3_cbc_decrypt(K1,K2,K3,IV0,Data),
- IV = crypto:des_cbc_ivec(Data),
+ Dec = crypto:block_decrypt(des3_cbc, [K1,K2,K3], IV0, Data),
+ IV = crypto:next_iv(des3_cbc, Data),
{Ssh#ssh{decrypt_ctx = IV}, Dec};
decrypt(#ssh{decrypt = 'aes128-cbc', decrypt_keys = Key,
decrypt_ctx = IV0} = Ssh, Data) ->
- Dec = crypto:aes_cbc_128_decrypt(Key,IV0,Data),
- IV = crypto:aes_cbc_ivec(Data),
+ Dec = crypto:block_decrypt(aes_cbc128, Key,IV0,Data),
+ IV = crypto:next_iv(aes_cbc, Data),
{Ssh#ssh{decrypt_ctx = IV}, Dec}.
@@ -954,22 +954,22 @@ recv_mac_final(SSH) ->
mac(none, _ , _, _) ->
<<>>;
mac('hmac-sha1', Key, SeqNum, Data) ->
- crypto:sha_mac(Key, [<>, Data]);
+ crypto:hmac(sha, Key, [<>, Data]);
mac('hmac-sha1-96', Key, SeqNum, Data) ->
- crypto:sha_mac_96(Key, [<>, Data]);
+ crypto:hmac(sha, Key, [<>, Data], mac_digest_size('hmac-sha1-96'));
mac('hmac-md5', Key, SeqNum, Data) ->
- crypto:md5_mac(Key, [<>, Data]);
+ crypto:hmac(md5, Key, [<>, Data]);
mac('hmac-md5-96', Key, SeqNum, Data) ->
- crypto:md5_mac_96(Key, [<>, Data]).
+ crypto:hmac(md5, Key, [<>, Data], mac_digest_size('hmac-md5-96')).
%% return N hash bytes (HASH)
hash(SSH, Char, Bits) ->
HASH =
case SSH#ssh.kex of
'diffie-hellman-group1-sha1' ->
- fun(Data) -> crypto:sha(Data) end;
+ fun(Data) -> crypto:hash(sha, Data) end;
'diffie-hellman-group-exchange-sha1' ->
- fun(Data) -> crypto:sha(Data) end;
+ fun(Data) -> crypto:hash(sha, Data) end;
_ ->
exit({bad_algorithm,SSH#ssh.kex})
end,
@@ -998,7 +998,7 @@ kex_h(SSH, K_S, E, F, K) ->
K_S, E,F,K],
[string,string,binary,binary,binary,
mpint,mpint,mpint]),
- crypto:sha(L).
+ crypto:hash(sha,L).
kex_h(SSH, K_S, Min, NBits, Max, Prime, Gen, E, F, K) ->
@@ -1019,7 +1019,7 @@ kex_h(SSH, K_S, Min, NBits, Max, Prime, Gen, E, F, K) ->
K_S, Min, NBits, Max,
Prime, Gen, E,F,K], Ts)
end,
- crypto:sha(L).
+ crypto:hash(sha,L).
mac_key_size('hmac-sha1') -> 20*8;
mac_key_size('hmac-sha1-96') -> 20*8;
@@ -1045,10 +1045,9 @@ peer_name({Host, _}) ->
dh_group1() ->
{2, 16#FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF}.
-dh_gen_key(G, P, _Bits) ->
- Private = ssh_bits:irandom(ssh_bits:isize(P)-1, 1, 1),
- Public = ssh_math:ipow(G, Private, P),
- {Private,Public}.
+dh_gen_key(G, P, _) ->
+ {Public, Private} = crypto:generate_key(dh, [P, G]),
+ {crypto:binary_to_integer(Private), crypto:binary_to_integer(Public)}.
trim_tail(Str) ->
lists:reverse(trim_head(lists:reverse(Str))).
@@ -1058,3 +1057,5 @@ trim_head([$\t|Cs]) -> trim_head(Cs);
trim_head([$\n|Cs]) -> trim_head(Cs);
trim_head([$\r|Cs]) -> trim_head(Cs);
trim_head(Cs) -> Cs.
+
+
--
cgit v1.2.3