2 # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
17 # The following tests are an adaptation of those in
18 # https://www.akkadia.org/drepper/SHA-crypt.txt
22 key => 'Hello world!',
23 expected => '$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5' },
25 salt => 'rounds=10000$saltstringsaltstring',
26 key => 'Hello world!',
27 expected => '$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2.opqey6IcA' },
29 salt => 'rounds=5000$toolongsaltstring',
30 key => 'This is just a test',
31 expected => '$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07guHPvOW8mGRcvxa5' },
33 salt => 'rounds=1400$anotherlongsaltstring',
34 key => 'a very much longer text to encrypt. This one even stretches over morethan one line.',
35 expected => '$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIUnzyxf12oP84Bnq1' },
37 salt => 'rounds=77777$short',
38 key => 'we have a short salt string but not a short password',
39 expected => '$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0KQRd/' },
41 salt => 'rounds=123456$asaltof16chars..',
42 key => 'a short string',
43 expected => '$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2jxPyzV/cZKmF/wJvD' },
45 salt => 'rounds=10$roundstoolow',
46 key => 'the minimum number is still observed',
47 expected => '$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY9l/gL972bIC' },
50 key => 'Hello world!',
51 expected => '$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1' },
53 salt => 'rounds=10000$saltstringsaltstring',
54 key => 'Hello world!',
55 expected => '$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v.' },
57 salt => 'rounds=5000$toolongsaltstring',
58 key => 'This is just a test',
59 expected => '$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0' },
61 salt => 'rounds=1400$anotherlongsaltstring',
62 key => 'a very much longer text to encrypt. This one even stretches over morethan one line.',
63 expected => '$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs.wPvMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1' },
65 salt => 'rounds=77777$short',
66 key => 'we have a short salt string but not a short password',
67 expected => '$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkvr0gge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0' },
69 salt => 'rounds=123456$asaltof16chars..',
70 key => 'a short string',
71 expected => '$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwcelCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1' },
73 salt => 'rounds=10$roundstoolow',
74 key => 'the minimum number is still observed',
75 expected => '$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50YhH1xhLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX.' }
78 plan tests => 10 + scalar @sha_tests;
81 ok(compare1stline_re([qw{openssl passwd password}], '^.{13}\R$'),
82 'crypt password with random salt');
83 ok(compare1stline_re([qw{openssl passwd -1 password}], '^\$1\$.{8}\$.{22}\R$'),
84 'BSD style MD5 password with random salt');
85 ok(compare1stline_re([qw{openssl passwd -apr1 password}], '^\$apr1\$.{8}\$.{22}\R$'),
86 'Apache style MD5 password with random salt');
87 ok(compare1stline_re([qw{openssl passwd -5 password}], '^\$5\$.{16}\$.{43}\R$'),
88 'SHA256 password with random salt');
89 ok(compare1stline_re([qw{openssl passwd -6 password}], '^\$6\$.{16}\$.{86}\R$'),
90 'Apache SHA512 password with random salt');
92 ok(compare1stline([qw{openssl passwd -salt xx password}], 'xxj31ZMTZzkVA'),
93 'crypt password with salt xx');
94 ok(compare1stline([qw{openssl passwd -salt xxxxxxxx -1 password}], '$1$xxxxxxxx$UYCIxa628.9qXjpQCjM4a.'),
95 'BSD style MD5 password with salt xxxxxxxx');
96 ok(compare1stline([qw{openssl passwd -salt xxxxxxxx -apr1 password}], '$apr1$xxxxxxxx$dxHfLAsjHkDRmG83UXe8K0'),
97 'Apache style MD5 password with salt xxxxxxxx');
98 ok(compare1stline([qw{openssl passwd -salt xxxxxxxxxxxxxxxx -5 password}], '$5$xxxxxxxxxxxxxxxx$fHytsM.wVD..zPN/h3i40WJRggt/1f73XkAC/gkelkB'),
99 'SHA256 password with salt xxxxxxxxxxxxxxxx');
100 ok(compare1stline([qw{openssl passwd -salt xxxxxxxxxxxxxxxx -6 password}], '$6$xxxxxxxxxxxxxxxx$VjGUrXBG6/8yW0f6ikBJVOb/lK/Tm9LxHJmFfwMvT7cpk64N9BW7ZQhNeMXAYFbOJ6HDG7wb0QpxJyYQn0rh81'),
101 'SHA512 password with salt xxxxxxxxxxxxxxxx');
103 foreach (@sha_tests) {
104 ok(compare1stline([qw{openssl passwd}, '-'.$_->{type}, '-salt', $_->{salt},
105 $_->{key}], $_->{expected}),
106 { 5 => 'SHA256', 6 => 'SHA512' }->{$_->{type}} . ' password with salt ' . $_->{salt});
110 sub compare1stline_re {
111 my ($cmdarray, $regexp) = @_;
112 my @lines = run(app($cmdarray), capture => 1);
114 return $lines[0] =~ m|$regexp|;
118 my ($cmdarray, $str) = @_;
119 my @lines = run(app($cmdarray), capture => 1);
121 return $lines[0] =~ m|^\Q${str}\E\R$|;