line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::PK::DH; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
848
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
46
|
|
4
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
210
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.080'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; our @ISA = qw(Exporter); ### use Exporter 5.57 'import'; |
8
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => [qw( dh_shared_secret )] ); |
9
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
10
|
|
|
|
|
|
|
our @EXPORT = qw(); |
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
12
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
109
|
|
13
|
2
|
|
|
2
|
|
15
|
use CryptX; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
45
|
|
14
|
2
|
|
|
2
|
|
9
|
use Crypt::Digest 'digest_data'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
106
|
|
15
|
2
|
|
|
2
|
|
11
|
use Crypt::Misc qw(read_rawfile pem_to_der); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
2434
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my %DH_PARAMS = ( |
18
|
|
|
|
|
|
|
ike768 => { g => 2, p => 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1'. |
19
|
|
|
|
|
|
|
'29024E088A67CC74020BBEA63B139B22514A08798E3404DD'. |
20
|
|
|
|
|
|
|
'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245'. |
21
|
|
|
|
|
|
|
'E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF' |
22
|
|
|
|
|
|
|
}, |
23
|
|
|
|
|
|
|
ike1024 => { g => 2, p => 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1'. |
24
|
|
|
|
|
|
|
'29024E088A67CC74020BBEA63B139B22514A08798E3404DD'. |
25
|
|
|
|
|
|
|
'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245'. |
26
|
|
|
|
|
|
|
'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED'. |
27
|
|
|
|
|
|
|
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381'. |
28
|
|
|
|
|
|
|
'FFFFFFFFFFFFFFFF' |
29
|
|
|
|
|
|
|
}, |
30
|
|
|
|
|
|
|
ike1536 => { g => 2, p => 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1'. |
31
|
|
|
|
|
|
|
'29024E088A67CC74020BBEA63B139B22514A08798E3404DD'. |
32
|
|
|
|
|
|
|
'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245'. |
33
|
|
|
|
|
|
|
'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED'. |
34
|
|
|
|
|
|
|
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D'. |
35
|
|
|
|
|
|
|
'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F'. |
36
|
|
|
|
|
|
|
'83655D23DCA3AD961C62F356208552BB9ED529077096966D'. |
37
|
|
|
|
|
|
|
'670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF' |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
ike2048 => { g => 2, p => 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1'. |
40
|
|
|
|
|
|
|
'29024E088A67CC74020BBEA63B139B22514A08798E3404DD'. |
41
|
|
|
|
|
|
|
'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245'. |
42
|
|
|
|
|
|
|
'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED'. |
43
|
|
|
|
|
|
|
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D'. |
44
|
|
|
|
|
|
|
'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F'. |
45
|
|
|
|
|
|
|
'83655D23DCA3AD961C62F356208552BB9ED529077096966D'. |
46
|
|
|
|
|
|
|
'670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B'. |
47
|
|
|
|
|
|
|
'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9'. |
48
|
|
|
|
|
|
|
'DE2BCBF6955817183995497CEA956AE515D2261898FA0510'. |
49
|
|
|
|
|
|
|
'15728E5A8AACAA68FFFFFFFFFFFFFFFF' |
50
|
|
|
|
|
|
|
}, |
51
|
|
|
|
|
|
|
ike3072 => { g => 2, p => 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1'. |
52
|
|
|
|
|
|
|
'29024E088A67CC74020BBEA63B139B22514A08798E3404DD'. |
53
|
|
|
|
|
|
|
'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245'. |
54
|
|
|
|
|
|
|
'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED'. |
55
|
|
|
|
|
|
|
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D'. |
56
|
|
|
|
|
|
|
'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F'. |
57
|
|
|
|
|
|
|
'83655D23DCA3AD961C62F356208552BB9ED529077096966D'. |
58
|
|
|
|
|
|
|
'670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B'. |
59
|
|
|
|
|
|
|
'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9'. |
60
|
|
|
|
|
|
|
'DE2BCBF6955817183995497CEA956AE515D2261898FA0510'. |
61
|
|
|
|
|
|
|
'15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64'. |
62
|
|
|
|
|
|
|
'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7'. |
63
|
|
|
|
|
|
|
'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B'. |
64
|
|
|
|
|
|
|
'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C'. |
65
|
|
|
|
|
|
|
'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31'. |
66
|
|
|
|
|
|
|
'43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF' |
67
|
|
|
|
|
|
|
}, |
68
|
|
|
|
|
|
|
ike4096 => { g => 2, p => 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1'. |
69
|
|
|
|
|
|
|
'29024E088A67CC74020BBEA63B139B22514A08798E3404DD'. |
70
|
|
|
|
|
|
|
'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245'. |
71
|
|
|
|
|
|
|
'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED'. |
72
|
|
|
|
|
|
|
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D'. |
73
|
|
|
|
|
|
|
'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F'. |
74
|
|
|
|
|
|
|
'83655D23DCA3AD961C62F356208552BB9ED529077096966D'. |
75
|
|
|
|
|
|
|
'670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B'. |
76
|
|
|
|
|
|
|
'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9'. |
77
|
|
|
|
|
|
|
'DE2BCBF6955817183995497CEA956AE515D2261898FA0510'. |
78
|
|
|
|
|
|
|
'15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64'. |
79
|
|
|
|
|
|
|
'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7'. |
80
|
|
|
|
|
|
|
'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B'. |
81
|
|
|
|
|
|
|
'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C'. |
82
|
|
|
|
|
|
|
'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31'. |
83
|
|
|
|
|
|
|
'43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7'. |
84
|
|
|
|
|
|
|
'88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA'. |
85
|
|
|
|
|
|
|
'2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6'. |
86
|
|
|
|
|
|
|
'287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED'. |
87
|
|
|
|
|
|
|
'1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9'. |
88
|
|
|
|
|
|
|
'93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199'. |
89
|
|
|
|
|
|
|
'FFFFFFFFFFFFFFFF' |
90
|
|
|
|
|
|
|
}, |
91
|
|
|
|
|
|
|
ike6144 => { g => 2, p => 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1'. |
92
|
|
|
|
|
|
|
'29024E088A67CC74020BBEA63B139B22514A08798E3404DD'. |
93
|
|
|
|
|
|
|
'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245'. |
94
|
|
|
|
|
|
|
'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED'. |
95
|
|
|
|
|
|
|
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D'. |
96
|
|
|
|
|
|
|
'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F'. |
97
|
|
|
|
|
|
|
'83655D23DCA3AD961C62F356208552BB9ED529077096966D'. |
98
|
|
|
|
|
|
|
'670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B'. |
99
|
|
|
|
|
|
|
'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9'. |
100
|
|
|
|
|
|
|
'DE2BCBF6955817183995497CEA956AE515D2261898FA0510'. |
101
|
|
|
|
|
|
|
'15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64'. |
102
|
|
|
|
|
|
|
'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7'. |
103
|
|
|
|
|
|
|
'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B'. |
104
|
|
|
|
|
|
|
'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C'. |
105
|
|
|
|
|
|
|
'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31'. |
106
|
|
|
|
|
|
|
'43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7'. |
107
|
|
|
|
|
|
|
'88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA'. |
108
|
|
|
|
|
|
|
'2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6'. |
109
|
|
|
|
|
|
|
'287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED'. |
110
|
|
|
|
|
|
|
'1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9'. |
111
|
|
|
|
|
|
|
'93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492'. |
112
|
|
|
|
|
|
|
'36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD'. |
113
|
|
|
|
|
|
|
'F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831'. |
114
|
|
|
|
|
|
|
'179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B'. |
115
|
|
|
|
|
|
|
'DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF'. |
116
|
|
|
|
|
|
|
'5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6'. |
117
|
|
|
|
|
|
|
'D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3'. |
118
|
|
|
|
|
|
|
'23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA'. |
119
|
|
|
|
|
|
|
'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328'. |
120
|
|
|
|
|
|
|
'06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C'. |
121
|
|
|
|
|
|
|
'DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE'. |
122
|
|
|
|
|
|
|
'12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF' |
123
|
|
|
|
|
|
|
}, |
124
|
|
|
|
|
|
|
ike8192 => { g => 2, p => 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1'. |
125
|
|
|
|
|
|
|
'29024E088A67CC74020BBEA63B139B22514A08798E3404DD'. |
126
|
|
|
|
|
|
|
'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245'. |
127
|
|
|
|
|
|
|
'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED'. |
128
|
|
|
|
|
|
|
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D'. |
129
|
|
|
|
|
|
|
'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F'. |
130
|
|
|
|
|
|
|
'83655D23DCA3AD961C62F356208552BB9ED529077096966D'. |
131
|
|
|
|
|
|
|
'670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B'. |
132
|
|
|
|
|
|
|
'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9'. |
133
|
|
|
|
|
|
|
'DE2BCBF6955817183995497CEA956AE515D2261898FA0510'. |
134
|
|
|
|
|
|
|
'15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64'. |
135
|
|
|
|
|
|
|
'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7'. |
136
|
|
|
|
|
|
|
'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B'. |
137
|
|
|
|
|
|
|
'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C'. |
138
|
|
|
|
|
|
|
'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31'. |
139
|
|
|
|
|
|
|
'43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7'. |
140
|
|
|
|
|
|
|
'88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA'. |
141
|
|
|
|
|
|
|
'2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6'. |
142
|
|
|
|
|
|
|
'287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED'. |
143
|
|
|
|
|
|
|
'1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9'. |
144
|
|
|
|
|
|
|
'93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492'. |
145
|
|
|
|
|
|
|
'36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD'. |
146
|
|
|
|
|
|
|
'F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831'. |
147
|
|
|
|
|
|
|
'179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B'. |
148
|
|
|
|
|
|
|
'DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF'. |
149
|
|
|
|
|
|
|
'5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6'. |
150
|
|
|
|
|
|
|
'D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3'. |
151
|
|
|
|
|
|
|
'23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA'. |
152
|
|
|
|
|
|
|
'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328'. |
153
|
|
|
|
|
|
|
'06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C'. |
154
|
|
|
|
|
|
|
'DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE'. |
155
|
|
|
|
|
|
|
'12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4'. |
156
|
|
|
|
|
|
|
'38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300'. |
157
|
|
|
|
|
|
|
'741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568'. |
158
|
|
|
|
|
|
|
'3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9'. |
159
|
|
|
|
|
|
|
'22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B'. |
160
|
|
|
|
|
|
|
'4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A'. |
161
|
|
|
|
|
|
|
'062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36'. |
162
|
|
|
|
|
|
|
'4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1'. |
163
|
|
|
|
|
|
|
'B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92'. |
164
|
|
|
|
|
|
|
'4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47'. |
165
|
|
|
|
|
|
|
'9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71'. |
166
|
|
|
|
|
|
|
'60C980DD98EDD3DFFFFFFFFFFFFFFFFF' |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
); |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub new { |
171
|
35
|
|
|
35
|
1
|
50148
|
my $self = shift->_new(); |
172
|
35
|
100
|
|
|
|
176
|
return @_ > 0 ? $self->import_key(@_) : $self; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub import_key { |
176
|
24
|
|
|
24
|
1
|
62
|
my ($self, $key) = @_; |
177
|
24
|
50
|
|
|
|
73
|
croak "FATAL: undefined key" unless $key; |
178
|
24
|
|
|
|
|
38
|
my $data; |
179
|
24
|
50
|
|
|
|
274
|
if (ref($key) eq 'SCALAR') { |
|
|
50
|
|
|
|
|
|
180
|
0
|
|
|
|
|
0
|
$data = $$key; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
elsif (-f $key) { |
183
|
24
|
|
|
|
|
81
|
$data = read_rawfile($key); |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
else { |
186
|
0
|
|
|
|
|
0
|
croak "FATAL: non-existing file '$key'"; |
187
|
|
|
|
|
|
|
} |
188
|
24
|
50
|
|
|
|
71
|
croak "FATAL: invalid key format" unless $data; |
189
|
24
|
|
|
|
|
20751
|
return $self->_import($data); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub import_key_raw { |
193
|
4
|
|
|
4
|
1
|
26
|
my ($self, $raw_bytes, $type, $param) = @_; |
194
|
4
|
|
|
|
|
7
|
my ($g, $p, $x, $y); |
195
|
|
|
|
|
|
|
|
196
|
4
|
50
|
|
|
|
16
|
if (ref $param eq 'HASH') { |
|
|
50
|
|
|
|
|
|
197
|
0
|
0
|
|
|
|
0
|
$g = $param->{g} or croak "FATAL: 'g' param not specified"; |
198
|
0
|
0
|
|
|
|
0
|
$p = $param->{p} or croak "FATAL: 'p' param not specified"; |
199
|
0
|
|
|
|
|
0
|
$g =~ s/^0x//; |
200
|
0
|
|
|
|
|
0
|
$p =~ s/^0x//; |
201
|
|
|
|
|
|
|
} elsif (my $dhparam = $DH_PARAMS{$param}) { |
202
|
4
|
|
|
|
|
6
|
$g = $dhparam->{g}; |
203
|
4
|
|
|
|
|
8
|
$p = $dhparam->{p}; |
204
|
|
|
|
|
|
|
} else { |
205
|
0
|
|
|
|
|
0
|
croak "FATAL: invalid parameter"; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
4
|
100
|
|
|
|
12
|
if ($type eq 'private') { |
|
|
50
|
|
|
|
|
|
209
|
2
|
|
|
|
|
3
|
$type = 1; |
210
|
|
|
|
|
|
|
} elsif ($type eq 'public') { |
211
|
2
|
|
|
|
|
4
|
$type = 0; |
212
|
|
|
|
|
|
|
} else { |
213
|
0
|
|
|
|
|
0
|
croak "FATAL: invalid key type '$type'"; |
214
|
|
|
|
|
|
|
} |
215
|
4
|
|
|
|
|
19939
|
my $rv = $self->_import_raw($raw_bytes, $type, $g, $p); |
216
|
4
|
|
|
|
|
17
|
return $rv; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
sub generate_key { |
220
|
7
|
|
|
7
|
1
|
60
|
my ($self, $param) = @_; |
221
|
|
|
|
|
|
|
|
222
|
7
|
100
|
|
|
|
29
|
if (!ref $param) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
223
|
|
|
|
|
|
|
# group name |
224
|
3
|
100
|
|
|
|
3409
|
return $self->_generate_key_gp($DH_PARAMS{$param}{g}, $DH_PARAMS{$param}{p}) if $DH_PARAMS{$param}; |
225
|
|
|
|
|
|
|
# size |
226
|
1
|
50
|
33
|
|
|
1658
|
return $self->_generate_key_size($param) if $param && $param =~ /^[0-9]+/; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
elsif (ref $param eq 'SCALAR') { |
229
|
2
|
|
|
|
|
4
|
my $data = $$param; |
230
|
2
|
100
|
|
|
|
15
|
if ($data =~ /-----BEGIN DH PARAMETERS-----\s*(.+)\s*-----END DH PARAMETERS-----/s) { |
231
|
1
|
50
|
|
|
|
6
|
$data = pem_to_der($data) or croak "FATAL: PEM/params decode failed"; |
232
|
|
|
|
|
|
|
} |
233
|
2
|
|
|
|
|
3433
|
return $self->_generate_key_dhparam($data); |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
elsif (ref $param eq 'HASH') { |
236
|
2
|
50
|
|
|
|
8
|
my $g = $param->{g} or croak "FATAL: 'g' param not specified"; |
237
|
2
|
50
|
|
|
|
6
|
my $p = $param->{p} or croak "FATAL: 'p' param not specified"; |
238
|
2
|
|
|
|
|
7
|
$g =~ s/^0x//; |
239
|
2
|
|
|
|
|
5
|
$p =~ s/^0x//; |
240
|
2
|
|
|
|
|
4796
|
return $self->_generate_key_gp($g, $p); |
241
|
|
|
|
|
|
|
} |
242
|
0
|
|
|
|
|
0
|
croak "FATAL: DH generate_key - invalid args"; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
### FUNCTIONS |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
sub dh_shared_secret { |
248
|
4
|
|
|
4
|
1
|
2269
|
my ($privkey, $pubkey) = @_; |
249
|
4
|
50
|
|
|
|
21
|
$privkey = __PACKAGE__->new($privkey) unless ref $privkey; |
250
|
4
|
50
|
|
|
|
42
|
$pubkey = __PACKAGE__->new($pubkey) unless ref $pubkey; |
251
|
4
|
50
|
33
|
|
|
32
|
carp "FATAL: invalid 'privkey' param" unless ref($privkey) eq __PACKAGE__ && $privkey->is_private; |
252
|
4
|
50
|
|
|
|
10
|
carp "FATAL: invalid 'pubkey' param" unless ref($pubkey) eq __PACKAGE__; |
253
|
4
|
|
|
|
|
6701
|
return $privkey->shared_secret($pubkey); |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
0
|
|
|
0
|
|
|
sub CLONE_SKIP { 1 } # prevent cloning |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
### DEPRECATED functions/methods |
259
|
|
|
|
|
|
|
|
260
|
0
|
|
|
0
|
0
|
|
sub encrypt { croak "Crypt::DH::encrypt is deprecated (removed in v0.049)" } |
261
|
0
|
|
|
0
|
0
|
|
sub decrypt { croak "Crypt::DH::decrypt is deprecated (removed in v0.049)" } |
262
|
0
|
|
|
0
|
0
|
|
sub sign_message { croak "Crypt::DH::sign_message is deprecated (removed in v0.049)" } |
263
|
0
|
|
|
0
|
0
|
|
sub verify_message { croak "Crypt::DH::verify_message is deprecated (removed in v0.049)" } |
264
|
0
|
|
|
0
|
0
|
|
sub sign_hash { croak "Crypt::DH::sign_hash is deprecated (removed in v0.049)" } |
265
|
0
|
|
|
0
|
0
|
|
sub verify_hash { croak "Crypt::DH::verify_hash is deprecated (removed in v0.049)" } |
266
|
0
|
|
|
0
|
0
|
|
sub dh_encrypt { croak "Crypt::DH::dh_encrypt is deprecated (removed in v0.049)" } |
267
|
0
|
|
|
0
|
0
|
|
sub dh_decrypt { croak "Crypt::DH::dh_decrypt is deprecated (removed in v0.049)" } |
268
|
0
|
|
|
0
|
0
|
|
sub dh_sign_message { croak "Crypt::DH::dh_sign_message is deprecated (removed in v0.049)" } |
269
|
0
|
|
|
0
|
0
|
|
sub dh_verify_message { croak "Crypt::DH::dh_verify_message is deprecated (removed in v0.049)" } |
270
|
0
|
|
|
0
|
0
|
|
sub dh_sign_hash { croak "Crypt::DH::dh_sign_hash is deprecated (removed in v0.049)" } |
271
|
0
|
|
|
0
|
0
|
|
sub dh_verify_hash { croak "Crypt::DH::dh_verify_hash is deprecated (removed in v0.049)" } |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
1; |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=pod |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=head1 NAME |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
Crypt::PK::DH - Public key cryptography based on Diffie-Hellman |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head1 SYNOPSIS |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
### OO interface |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
#Shared secret |
286
|
|
|
|
|
|
|
my $priv = Crypt::PK::DH->new('Alice_priv_dh1.key'); |
287
|
|
|
|
|
|
|
my $pub = Crypt::PK::DH->new('Bob_pub_dh1.key'); |
288
|
|
|
|
|
|
|
my $shared_secret = $priv->shared_secret($pub); |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
#Key generation |
291
|
|
|
|
|
|
|
my $pk = Crypt::PK::DH->new(); |
292
|
|
|
|
|
|
|
$pk->generate_key(128); |
293
|
|
|
|
|
|
|
my $private = $pk->export_key('private'); |
294
|
|
|
|
|
|
|
my $public = $pk->export_key('public'); |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
or |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
my $pk = Crypt::PK::DH->new(); |
299
|
|
|
|
|
|
|
$pk->generate_key('ike2048'); |
300
|
|
|
|
|
|
|
my $private = $pk->export_key('private'); |
301
|
|
|
|
|
|
|
my $public = $pk->export_key('public'); |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
or |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
my $pk = Crypt::PK::DH->new(); |
306
|
|
|
|
|
|
|
$pk->generate_key({ p => $p, g => $g }); |
307
|
|
|
|
|
|
|
my $private = $pk->export_key('private'); |
308
|
|
|
|
|
|
|
my $public = $pk->export_key('public'); |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
### Functional interface |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
#Shared secret |
313
|
|
|
|
|
|
|
my $shared_secret = dh_shared_secret('Alice_priv_dh1.key', 'Bob_pub_dh1.key'); |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=head1 METHODS |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=head2 new |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
my $pk = Crypt::PK::DH->new(); |
320
|
|
|
|
|
|
|
#or |
321
|
|
|
|
|
|
|
my $pk = Crypt::PK::DH->new($priv_or_pub_key_filename); |
322
|
|
|
|
|
|
|
#or |
323
|
|
|
|
|
|
|
my $pk = Crypt::PK::DH->new(\$buffer_containing_priv_or_pub_key); |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
=head2 generate_key |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
Uses Yarrow-based cryptographically strong random number generator seeded with |
328
|
|
|
|
|
|
|
random data taken from C (UNIX) or C (Win32). |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
$pk->generate_key($groupsize); |
331
|
|
|
|
|
|
|
### $groupsize (in bytes) corresponds to DH parameters (p, g) predefined by libtomcrypt |
332
|
|
|
|
|
|
|
# 96 => DH-768 |
333
|
|
|
|
|
|
|
# 128 => DH-1024 |
334
|
|
|
|
|
|
|
# 192 => DH-1536 |
335
|
|
|
|
|
|
|
# 256 => DH-2048 |
336
|
|
|
|
|
|
|
# 384 => DH-3072 |
337
|
|
|
|
|
|
|
# 512 => DH-4096 |
338
|
|
|
|
|
|
|
# 768 => DH-6144 |
339
|
|
|
|
|
|
|
# 1024 => DH-8192 |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
The following variants are available since CryptX-0.032 |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
$pk->generate_key($groupname) |
344
|
|
|
|
|
|
|
### $groupname corresponds to values defined in RFC7296 and RFC3526 |
345
|
|
|
|
|
|
|
# 'ike768' => 768-bit MODP (Group 1) |
346
|
|
|
|
|
|
|
# 'ike1024' => 1024-bit MODP (Group 2) |
347
|
|
|
|
|
|
|
# 'ike1536' => 1536-bit MODP (Group 5) |
348
|
|
|
|
|
|
|
# 'ike2048' => 2048-bit MODP (Group 14) |
349
|
|
|
|
|
|
|
# 'ike3072' => 3072-bit MODP (Group 15) |
350
|
|
|
|
|
|
|
# 'ike4096' => 4096-bit MODP (Group 16) |
351
|
|
|
|
|
|
|
# 'ike6144' => 6144-bit MODP (Group 17) |
352
|
|
|
|
|
|
|
# 'ike8192' => 8192-bit MODP (Group 18) |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
$pk->generate_key($param_hash) |
355
|
|
|
|
|
|
|
# $param_hash is { g => $g, p => $p } |
356
|
|
|
|
|
|
|
# where $g is the generator (base) in a hex string and $p is the prime in a hex string |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
$pk->generate_key(\$dh_param) |
359
|
|
|
|
|
|
|
# $dh_param is the content of DER or PEM file with DH parameters |
360
|
|
|
|
|
|
|
# e.g. openssl dhparam 2048 |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
=head2 import_key |
363
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
Loads private or public key (exported by L). |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
$pk->import_key($filename); |
367
|
|
|
|
|
|
|
#or |
368
|
|
|
|
|
|
|
$pk->import_key(\$buffer_containing_key); |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
=head2 import_key_raw |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
I |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
$pk->import_key_raw($raw_bytes, $type, $params) |
375
|
|
|
|
|
|
|
### $raw_bytes is a binary string containing the key |
376
|
|
|
|
|
|
|
### $type is either 'private' or 'public' |
377
|
|
|
|
|
|
|
### $param is either a name ('ike2038') or hash containing the p,g values { g=>$g, p=>$p } |
378
|
|
|
|
|
|
|
### in hex strings |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
=head2 export_key |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
B DH key format change - since v0.049 it is compatible with libtomcrypt 1.18. |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
my $private = $pk->export_key('private'); |
385
|
|
|
|
|
|
|
#or |
386
|
|
|
|
|
|
|
my $public = $pk->export_key('public'); |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
=head2 export_key_raw |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
I |
391
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
$raw_bytes = $dh->export_key_raw('public') |
393
|
|
|
|
|
|
|
#or |
394
|
|
|
|
|
|
|
$raw_bytes = $dh->export_key_raw('private') |
395
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
=head2 shared_secret |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
# Alice having her priv key $pk and Bob's public key $pkb |
399
|
|
|
|
|
|
|
my $pk = Crypt::PK::DH->new($priv_key_filename); |
400
|
|
|
|
|
|
|
my $pkb = Crypt::PK::DH->new($pub_key_filename); |
401
|
|
|
|
|
|
|
my $shared_secret = $pk->shared_secret($pkb); |
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
# Bob having his priv key $pk and Alice's public key $pka |
404
|
|
|
|
|
|
|
my $pk = Crypt::PK::DH->new($priv_key_filename); |
405
|
|
|
|
|
|
|
my $pka = Crypt::PK::DH->new($pub_key_filename); |
406
|
|
|
|
|
|
|
my $shared_secret = $pk->shared_secret($pka); # same value as computed by Alice |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
=head2 is_private |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
my $rv = $pk->is_private; |
411
|
|
|
|
|
|
|
# 1 .. private key loaded |
412
|
|
|
|
|
|
|
# 0 .. public key loaded |
413
|
|
|
|
|
|
|
# undef .. no key loaded |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
=head2 size |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
my $size = $pk->size; |
418
|
|
|
|
|
|
|
# returns key size in bytes or undef if no key loaded |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
=head2 key2hash |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
my $hash = $pk->key2hash; |
423
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
# returns hash like this (or undef if no key loaded): |
425
|
|
|
|
|
|
|
{ |
426
|
|
|
|
|
|
|
type => 0, # integer: 1 .. private, 0 .. public |
427
|
|
|
|
|
|
|
size => 256, # integer: key size in bytes |
428
|
|
|
|
|
|
|
x => "FBC1062F73B9A17BB8473A2F5A074911FA7F20D28FB...", #private key |
429
|
|
|
|
|
|
|
y => "AB9AAA40774D3CD476B52F82E7EE2D8A8D40CD88BF4...", #public key |
430
|
|
|
|
|
|
|
g => "2", # generator/base |
431
|
|
|
|
|
|
|
p => "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80D...", # prime |
432
|
|
|
|
|
|
|
} |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
=head2 params2hash |
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
I |
437
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
my $params = $pk->params2hash; |
439
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
# returns hash like this (or undef if no key loaded): |
441
|
|
|
|
|
|
|
{ |
442
|
|
|
|
|
|
|
g => "2", # generator/base |
443
|
|
|
|
|
|
|
p => "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80D...", # prime |
444
|
|
|
|
|
|
|
} |
445
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
=head1 FUNCTIONS |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
=head2 dh_shared_secret |
449
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
DH based shared secret generation. See method L below. |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
#on Alice side |
453
|
|
|
|
|
|
|
my $shared_secret = dh_shared_secret('Alice_priv_dh1.key', 'Bob_pub_dh1.key'); |
454
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
#on Bob side |
456
|
|
|
|
|
|
|
my $shared_secret = dh_shared_secret('Bob_priv_dh1.key', 'Alice_pub_dh1.key'); |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
=head1 DEPRECATED INTERFACE |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
The following functions/methods were removed in removed in v0.049: |
461
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
encrypt |
463
|
|
|
|
|
|
|
decrypt |
464
|
|
|
|
|
|
|
sign_message |
465
|
|
|
|
|
|
|
verify_message |
466
|
|
|
|
|
|
|
sign_hash |
467
|
|
|
|
|
|
|
verify_hash |
468
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
dh_encrypt |
470
|
|
|
|
|
|
|
dh_decrypt |
471
|
|
|
|
|
|
|
dh_sign_message |
472
|
|
|
|
|
|
|
dh_verify_message |
473
|
|
|
|
|
|
|
dh_sign_hash |
474
|
|
|
|
|
|
|
dh_verify_hash |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
=head1 SEE ALSO |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
=over |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
=item * L |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
=back |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
=cut |