File Coverage

blib/lib/Crypt/KeyDerivation.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package Crypt::KeyDerivation;
2              
3 2     2   77396 use strict;
  2         4  
  2         51  
4 2     2   10 use warnings;
  2         2  
  2         263  
5             our $VERSION = '0.087_003';
6              
7             require Exporter; our @ISA = qw(Exporter); ### use Exporter 5.57 'import';
8             our %EXPORT_TAGS = ( all => [qw(pbkdf1 pbkdf2 hkdf hkdf_expand hkdf_extract bcrypt_pbkdf scrypt_pbkdf argon2_pbkdf)] );
9             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
10             our @EXPORT = qw();
11              
12 2     2   10 use Carp;
  2         8  
  2         117  
13             $Carp::Internal{(__PACKAGE__)}++;
14 2     2   301 use CryptX;
  2         3  
  2         117  
15              
16             1;
17              
18             =pod
19              
20             =head1 NAME
21              
22             Crypt::KeyDerivation - PBKDF1, PBKDF2, HKDF, Bcrypt, Scrypt, Argon2 key derivation functions
23              
24             =head1 SYNOPSIS
25              
26             use Crypt::KeyDerivation ':all';
27              
28             ### PBKDF1/2
29             $derived_key1 = pbkdf1($password, $salt, $iteration_count, $hash_name, $len);
30             $derived_key2 = pbkdf2($password, $salt, $iteration_count, $hash_name, $len);
31              
32             ### HKDF & co.
33             $derived_key3 = hkdf($keying_material, $salt, $hash_name, $len, $info);
34             $prk = hkdf_extract($keying_material, $salt, $hash_name);
35             $okm1 = hkdf_expand($prk, $hash_name, $len, $info);
36              
37             ### bcrypt / scrypt / argon2
38             $derived_key4 = bcrypt_pbkdf($password, $salt, $rounds, $hash_name, $len);
39             $derived_key5 = scrypt_pbkdf($password, $salt, $N, $r, $p, $len);
40             $derived_key6 = argon2_pbkdf($type, $password, $salt, $t_cost, $m_factor, $parallelism, $len, $secret, $ad);
41              
42             =head1 DESCRIPTION
43              
44             Provides an interface to key derivation functions:
45              
46             =over
47              
48             =item * PBKDF1 and PBKDF2 according to PKCS#5 v2.0 L
49              
50             =item * HKDF (+ related) according to L
51              
52             =item * Bcrypt-PBKDF as defined by the OpenBSD project
53              
54             =item * Scrypt according to L
55              
56             =item * Argon2 according to L
57              
58             =back
59              
60             While primarily designed for key derivation, the functions PBKDF2, Bcrypt, Scrypt and Argon2
61             are also widely used for password hashing. In that use case the derived key serves as the
62             stored password hash.
63              
64             =head1 FUNCTIONS
65              
66             =head2 pbkdf1
67              
68             B if you are not sure, do not use C but rather choose C.
69              
70             $derived_key = pbkdf1($password, $salt, $iteration_count, $hash_name, $len);
71             #or
72             $derived_key = pbkdf1($password, $salt, $iteration_count, $hash_name);
73             #or
74             $derived_key = pbkdf1($password, $salt, $iteration_count);
75             #or
76             $derived_key = pbkdf1($password, $salt);
77              
78             # $password ......... input keying material (password)
79             # $salt ............. salt/nonce (expected length: 8)
80             # $iteration_count .. optional, DEFAULT: 5000
81             # $hash_name ........ optional, DEFAULT: 'SHA256'
82             # $len .............. optional, derived key len, DEFAULT: 32
83              
84             =head2 pbkdf2
85              
86             $derived_key = pbkdf2($password, $salt, $iteration_count, $hash_name, $len);
87             #or
88             $derived_key = pbkdf2($password, $salt, $iteration_count, $hash_name);
89             #or
90             $derived_key = pbkdf2($password, $salt, $iteration_count);
91             #or
92             $derived_key = pbkdf2($password, $salt);
93              
94             # $password ......... input keying material (password)
95             # $salt ............. salt/nonce
96             # $iteration_count .. optional, DEFAULT: 5000
97             # $hash_name ........ optional, DEFAULT: 'SHA256'
98             # $len .............. optional, derived key len, DEFAULT: 32
99              
100             =head2 hkdf
101              
102             $okm2 = hkdf($password, $salt, $hash_name, $len, $info);
103             #or
104             $okm2 = hkdf($password, $salt, $hash_name, $len);
105             #or
106             $okm2 = hkdf($password, $salt, $hash_name);
107             #or
108             $okm2 = hkdf($password, $salt);
109              
110             # $password ... input keying material (password)
111             # $salt ....... salt/nonce, if undef defaults to HashLen zero octets
112             # $hash_name .. optional, DEFAULT: 'SHA256'
113             # $len ........ optional, derived key len, DEFAULT: 32
114             # $info ....... optional context and application specific information, DEFAULT: ''
115              
116             =head2 hkdf_extract
117              
118             $prk = hkdf_extract($password, $salt, $hash_name);
119             #or
120             $prk = hkdf_extract($password, $salt, $hash_name);
121              
122             # $password ... input keying material (password)
123             # $salt ....... salt/nonce, if undef defaults to HashLen zero octets
124             # $hash_name .. optional, DEFAULT: 'SHA256'
125              
126              
127             =head2 hkdf_expand
128              
129             $okm = hkdf_expand($pseudokey, $hash_name, $len, $info);
130             #or
131             $okm = hkdf_expand($pseudokey, $hash_name, $len);
132             #or
133             $okm = hkdf_expand($pseudokey, $hash_name);
134             #or
135             $okm = hkdf_expand($pseudokey);
136              
137             # $pseudokey .. input keying material
138             # $hash_name .. optional, DEFAULT: 'SHA256'
139             # $len ........ optional, derived key len, DEFAULT: 32
140             # $info ....... optional context and application specific information, DEFAULT: ''
141              
142             =head2 bcrypt_pbkdf
143              
144             bcrypt-based key derivation as defined by the OpenBSD project.
145              
146             I
147              
148              
149             $derived_key = bcrypt_pbkdf($password, $salt, $rounds, $hash_name, $len);
150             #or
151             $derived_key = bcrypt_pbkdf($password, $salt, $rounds, $hash_name);
152             #or
153             $derived_key = bcrypt_pbkdf($password, $salt, $rounds);
154             #or
155             $derived_key = bcrypt_pbkdf($password, $salt);
156              
157             # $password ... input keying material (password)
158             # $salt ....... salt/nonce
159             # $rounds ..... optional, number of rounds, DEFAULT: 16
160             # $hash_name .. optional, DEFAULT: 'SHA512'
161             # $len ........ optional, derived key len, DEFAULT: 32
162              
163             =head2 scrypt_pbkdf
164              
165             scrypt key derivation according to L.
166              
167             I
168              
169              
170             $derived_key = scrypt_pbkdf($password, $salt, $N, $r, $p, $len);
171             #or
172             $derived_key = scrypt_pbkdf($password, $salt, $N, $r, $p);
173             #or
174             $derived_key = scrypt_pbkdf($password, $salt, $N);
175             #or
176             $derived_key = scrypt_pbkdf($password, $salt);
177              
178             # $password ... input keying material (password)
179             # $salt ....... salt/nonce
180             # $N .......... optional, CPU/memory cost parameter (power of 2), DEFAULT: 1024
181             # $r .......... optional, block size, DEFAULT: 8
182             # $p .......... optional, parallelization parameter, DEFAULT: 1
183             # $len ........ optional, derived key len, DEFAULT: 32
184              
185             =head2 argon2_pbkdf
186              
187             Argon2 key derivation according to L.
188              
189             I
190              
191              
192             $derived_key = argon2_pbkdf($type, $password, $salt, $t_cost, $m_factor, $parallelism, $len, $secret, $ad);
193             #or
194             $derived_key = argon2_pbkdf($type, $password, $salt, $t_cost, $m_factor, $parallelism, $len);
195             #or
196             $derived_key = argon2_pbkdf($type, $password, $salt, $t_cost, $m_factor, $parallelism);
197             #or
198             $derived_key = argon2_pbkdf($type, $password, $salt);
199              
200             # $type ... one of 'argon2d', 'argon2i', 'argon2id'
201             # $password ... input keying material (password)
202             # $salt ... salt/nonce
203             # $t_cost ... optional, time cost (number of iterations), DEFAULT: 3
204             # $m_factor ... optional, memory cost in kibibytes, DEFAULT: 65536
205             # $parallelism ... optional, degree of parallelism, DEFAULT: 1
206             # $len ... optional, derived key len, DEFAULT: 32
207             # $secret ... optional, secret value, DEFAULT: ''
208             # $ad ... optional, associated data, DEFAULT: ''
209              
210             =cut