163
192
des3_cbc_encrypt(Key1, Key2, Key3, IVec, Data) ->
164
193
des_ede3_cbc_encrypt(Key1, Key2, Key3, IVec, Data).
165
194
des_ede3_cbc_encrypt(Key1, Key2, Key3, IVec, Data) ->
195
%%io:format("des_ede3_cbc_encrypt: size(Data)=~p\n", [size(list_to_binary([Data]))]),
166
196
control(?DES_EDE3_CBC_ENCRYPT, [Key1, Key2, Key3, IVec, Data]).
168
198
des3_cbc_decrypt(Key1, Key2, Key3, IVec, Data) ->
170
200
des_ede3_cbc_decrypt(Key1, Key2, Key3, IVec, Data) ->
171
201
control(?DES_EDE3_CBC_DECRYPT, [Key1, Key2, Key3, IVec, Data]).
204
%% AES in cipher feedback mode (CFB)
206
aes_cfb_128_encrypt(Key, IVec, Data) ->
207
control(?AES_CFB_128_ENCRYPT, [Key, IVec, Data]).
209
aes_cfb_128_decrypt(Key, IVec, Data) ->
210
control(?AES_CFB_128_DECRYPT, [Key, IVec, Data]).
214
%% RAND - pseudo random numbers using RN_ functions in crypto lib
218
rand_bytes(Bytes, 0, 0).
219
rand_bytes(Bytes, Topmask, Bottommask) ->
220
control(?RAND_BYTES,[<<Bytes:32/integer,
222
Bottommask:8/integer>>]).
224
rand_uniform(From,To) when binary(From), binary(To) ->
225
case control(?RAND_UNIFORM,[From,To]) of
226
<<Len:32/integer, MSB, Rest/binary>> when MSB > 127 ->
227
<<(Len + 1):32/integer, 0, MSB, Rest/binary>>;
231
rand_uniform(From,To) when integer(From),integer(To) ->
232
BinFrom = mpint(From),
234
case rand_uniform(BinFrom, BinTo) of
235
Result when binary(Result) ->
242
%% mod_exp - utility for rsa generation
244
mod_exp(Base, Exponent, Modulo)
245
when integer(Base), integer(Exponent), integer(Modulo) ->
246
erlint(mod_exp(mpint(Base), mpint(Exponent), mpint(Modulo)));
248
mod_exp(Base, Exponent, Modulo) ->
249
case control(?MOD_EXP,[Base,Exponent,Modulo]) of
250
<<Len:32/integer, MSB, Rest/binary>> when MSB > 127 ->
251
<<(Len + 1):32/integer, 0, MSB, Rest/binary>>;
260
dss_verify(Dgst,Signature,Key) ->
261
control(?DSS_VERIFY, [Dgst,Signature,Key]) == <<1>>.
263
rsa_verify(Dgst,Signature,Key) ->
264
control(?RSA_VERIFY, [Dgst,Signature,Key]) == <<1>>.
267
%% AES - with 128 bit key in cipher block chaining mode (CBC)
270
aes_cbc_128_encrypt(Key, IVec, Data) ->
271
control(?AES_CBC_128_ENCRYPT, [Key, IVec, Data]).
273
aes_cbc_128_decrypt(Key, IVec, Data) ->
274
control(?AES_CBC_128_DECRYPT, [Key, IVec, Data]).
175
279
%% LOCAL FUNCTIONS
177
control_bin(Cmd, Key, Data) when binary(Key) ->
178
control(Cmd, [sizehdr(size(Key)), Key, Data]);
179
control_bin(Cmd, Key, Data) when list(Key) ->
180
control(Cmd, [sizehdr(flen(Key)), Key, Data]).
281
control_bin(Cmd, Key, Data) ->
283
control(Cmd, [<<Sz:32/integer-unsigned>>, Key, Data]).
182
285
control(Cmd, Data) ->
183
286
[{port, Port}| _] = ets:lookup(crypto_server_table, port),
184
287
erlang:port_control(Port, Cmd, Data).
187
[(N bsr 24) band 255,
290
%% [(N bsr 24) band 255,
291
%% (N bsr 16) band 255,
292
%% (N bsr 8) band 255,
192
%% Flat length of IOlist
295
%% Flat length of IOlist (or binary)
296
flen(L) when binary(L) ->
310
%% large integer in a binary with 32bit length
311
%% MP representaion (SSH2)
312
mpint(X) when X < 0 ->
327
-define(UINT32(X), X:32/unsigned-big-integer).
329
mpint_neg(-1,I,Ds=[MSB|_]) ->
330
if MSB band 16#80 =/= 16#80 ->
331
<<?UINT32((I+1)), (list_to_binary([255|Ds]))/binary>>;
333
(<<?UINT32(I), (list_to_binary(Ds))/binary>>)
336
mpint_neg(X bsr 8,I+1,[(X band 255)|Ds]).
338
mpint_pos(0,I,Ds=[MSB|_]) ->
339
if MSB band 16#80 == 16#80 ->
340
<<?UINT32((I+1)), (list_to_binary([0|Ds]))/binary>>;
342
(<<?UINT32(I), (list_to_binary(Ds))/binary>>)
345
mpint_pos(X bsr 8,I+1,[(X band 255)|Ds]).
347
%% int from integer in a binary with 32bit length
348
erlint(<<MPIntSize:32/integer,MPIntValue/binary>>) ->
350
<<Integer:Bits/integer>> = MPIntValue,