summaryrefslogtreecommitdiff
path: root/lib/ssh/src/ssh_dsa.erl
blob: 1b9a396f0cdfd40f5c527fdfae510501e9a58e9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2005-2010. 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
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%

%%

%%% Description: dsa public-key sign and verify

-module(ssh_dsa).

-export([verify/3]).
-export([sign/2]).
-export([alg_name/0]).

-include("ssh.hrl").

%% start() ->
%%     crypto:start().

%% sign_file(File, Opts) ->
%%     start(),
%%     {ok,Bin} = file:read_file(File),
%%     {ok,Key} = ssh_file:private_host_dsa_key(user, Opts),
%%     sign(Key, Bin).

%% verify_file(File, Sig) ->
%%     start(),
%%     {ok,Bin} = file:read_file(File),
%%     {ok,Key} = ssh_file:public_host_key(user, dsa),
%%     verify(Key, Bin, Sig).

sign(_Private=#ssh_key { private={P,Q,G,X} },Mb) ->
    K = ssh_bits:irandom(160) rem Q,
    R = ssh_math:ipow(G, K, P) rem Q,
    Ki = ssh_math:invert(K, Q),
    <<M:160/big-unsigned-integer>> = crypto:sha(Mb),
    S = (Ki * (M + X*R)) rem Q,
    <<R:160/big-unsigned-integer, S:160/big-unsigned-integer>>.


%% the paramiko client sends a bad sig sometimes, 
%% instead of crashing, we nicely return error, the
%% typcally manifests itself as Sb being 39 bytes
%% instead of 40.

verify(Public, Mb, Sb) ->
    case catch xverify(Public, Mb, Sb) of
	{'EXIT', _Reason} ->
            %store({Public, Mb, Sb, _Reason}),
	    {error, inconsistent_key};
	ok ->
  	    %store({Public, Mb, Sb, ok})
	    ok
    end.

%% store(Term) ->
%%     {ok, Fd} = file:open("/tmp/dsa", [append]),
%%     io:format(Fd, "~p~n~n~n", [Term]),
%%     file:close(Fd).


xverify(_Public=#ssh_key { public={P,Q,G,Y} },Mb,Sb) ->
    <<R0:160/big-unsigned-integer, S0:160/big-unsigned-integer>> = Sb,
    ?ssh_assert(R0 >= 0 andalso R0 < Q andalso
		S0 >= 0 andalso S0 < Q, out_of_range),
    W = ssh_math:invert(S0,Q),
    <<M0:160/big-unsigned-integer>> = crypto:sha(Mb),
    U1 = (M0*W) rem Q,
    U2 = (R0*W) rem Q,
    T1 = ssh_math:ipow(G,U1,P),
    T2 = ssh_math:ipow(Y,U2,P),
    V = ((T1*T2) rem P) rem Q,
    if V == R0 ->
	    ok;
       true ->
	    {error, inconsistent_key}
    end.

alg_name() ->
    "ssh-dss".