Debian Codegraph @

script/argon2-calibrate

raw
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Time::HiRes 'time';
7 use Crypt::Argon2;
8
9 sub prompt {
10 my ($mess, $def) = @_;
11
12 my $dispdef = defined $def ? ("[$def] " ) : '';
13
14 local $| = 1;
15 print "$mess ", $dispdef;
16
17 my $ans = <STDIN>;
18 chomp $ans;
19
20 if (not defined $ans or not length $ans) {
21 $ans = defined $def ? $def : '';
22 }
23
24 return $ans;
25 }
26
27 my %encoder = (
28 argon2id => \&Crypt::Argon2::argon2id_raw,
29 argon2i => \&Crypt::Argon2::argon2i_raw,
30 argon2d => \&Crypt::Argon2::argon2d_raw,
31 );
32
33 my $type = prompt('What type of hash?', 'argon2id');
34 my $encoder = $encoder{$type} or die "Invalid type $type";
35
36 my $threads = prompt('How many threads may it use', 1);
37 die "Invalid number '$threads'" unless $threads > 0;
38
39 my $mem_cost = prompt('How much memory may it use (e.g. 32M)');
40 die "Invalid memory usage '$mem_cost'" unless $mem_cost =~ / ^ \d+ [hMG] $ /x;
41
42 my $max_time = prompt('How much time may argon2 take? (in milliseconds)');
43 die 'No time given' unless $max_time > 0;
44
45 my $begin = time;
46 $encoder->("correct horse battery staple", scalar("\x00" x 16), 100, $mem_cost, $threads, 16);
47 my $end = time;
48
49 my $time_per_round = ($end - $begin) * 10;
50 my $time_cost = int($max_time / $time_per_round);
51
52 die "Can't compute a hash in the given time (took $time_per_round milliseconds)" if $time_cost == 0;
53
54 die "Unsafe parameters were computed" if $type eq 'argon2i' && $time_cost < 3;
55
56 print <<"END";
57
58 type = $type
59 threads = $threads
60 mem_cost = $mem_cost
61 time_cost = $time_cost
62 END
63
64 # PODNAME: argon2-calibrate
65 # ABSTRACT: a script to find the appropriate argon2 parameters
66
67 __END__
68
69 =pod
70
71 =encoding UTF-8
72
73 =head1 NAME
74
75 argon2-calibrate - a script to find the appropriate argon2 parameters
76
77 =head1 VERSION
78
79 version 0.031
80
81 =head1 DESCRIPTION
82
83 This program implements the following procedure, as recommended by the argon2 authors:
84
85 =over 4
86
87 =item 1. Select the type C<y>. If you do not know the difference between them, choose Argon2id.
88
89 =item 2. Figure out the maximum number of threads C<h> that can be initiated by each call to Argon2. This is the C<parallelism> argument.
90
91 =item 3. Figure out the maximum amount of memory C<m> that each call can a afford.
92
93 =item 4. Figure out the maximum amount C<x> of time (in seconds) that each call can a afford.
94
95 =item 5. Select the salt length. 16 bytes is suffient for all applications, but can be reduced to 8 bytes in the case of space constraints.
96
97 =item 6. Select the tag (output) size. 16 bytes is suffient for most applications, including key derivation.
98
99 =item 7. Run the scheme of type C<y>, memory C<m> and C<h> lanes and threads, using different number of passes C<t>. Figure out the maximum C<t> such that the running time does not exceed C<x>. If it exceeds C<x> even for C<t = 1>, reduce C<m> accordingly. If using Argon2i, t must be at least 3.
100
101 =item 8. Hash all the passwords with the just determined values C<m>, C<h>, and C<t>.
102
103 =back
104
105 =head1 AUTHOR
106
107 Leon Timmermans <[email protected]>
108
109 =head1 COPYRIGHT AND LICENSE
110
111 This software is Copyright (c) 2013 by Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, Samuel Neves, Thomas Pornin and Leon Timmermans.
112
113 This is free software, licensed under:
114
115 The Apache License, Version 2.0, January 2004
116
117 =cut