1*e9eee4b1Snia /* $NetBSD: t_crypt.c,v 1.6 2021/10/12 13:24:21 nia Exp $ */
207d8486aSchristos
307d8486aSchristos /*
433222336Schristos * This version is derived from the original implementation of FreeSec
533222336Schristos * (release 1.1) by David Burren. I've reviewed the changes made in
633222336Schristos * OpenBSD (as of 2.7) and modified the original code in a similar way
733222336Schristos * where applicable. I've also made it reentrant and made a number of
833222336Schristos * other changes.
933222336Schristos * - Solar Designer <solar at openwall.com>
1033222336Schristos */
1133222336Schristos
1233222336Schristos /*
1333222336Schristos * FreeSec: libcrypt for NetBSD
1407d8486aSchristos *
1533222336Schristos * Copyright (c) 1994 David Burren
1633222336Schristos * All rights reserved.
1707d8486aSchristos *
1807d8486aSchristos * Redistribution and use in source and binary forms, with or without
1933222336Schristos * modification, are permitted provided that the following conditions
2033222336Schristos * are met:
2133222336Schristos * 1. Redistributions of source code must retain the above copyright
2233222336Schristos * notice, this list of conditions and the following disclaimer.
2333222336Schristos * 2. Redistributions in binary form must reproduce the above copyright
2433222336Schristos * notice, this list of conditions and the following disclaimer in the
2533222336Schristos * documentation and/or other materials provided with the distribution.
2633222336Schristos * 3. Neither the name of the author nor the names of other contributors
2733222336Schristos * may be used to endorse or promote products derived from this software
2833222336Schristos * without specific prior written permission.
2907d8486aSchristos *
3033222336Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
3133222336Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3233222336Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3333222336Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3433222336Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3533222336Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3633222336Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3733222336Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3807d8486aSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3907d8486aSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4007d8486aSchristos * SUCH DAMAGE.
4107d8486aSchristos *
4233222336Schristos * $Owl: Owl/packages/glibc/crypt_freesec.c,v 1.6 2010/02/20 14:45:06 solar Exp $
4333222336Schristos * Id: crypt.c,v 1.15 1994/09/13 04:58:49 davidb Exp
4433222336Schristos *
4533222336Schristos * This is an original implementation of the DES and the crypt(3) interfaces
4633222336Schristos * by David Burren <davidb at werj.com.au>.
4733222336Schristos *
4833222336Schristos * An excellent reference on the underlying algorithm (and related
4933222336Schristos * algorithms) is:
5033222336Schristos *
5133222336Schristos * B. Schneier, Applied Cryptography: protocols, algorithms,
5233222336Schristos * and source code in C, John Wiley & Sons, 1994.
5333222336Schristos *
5433222336Schristos * Note that in that book's description of DES the lookups for the initial,
5533222336Schristos * pbox, and final permutations are inverted (this has been brought to the
5633222336Schristos * attention of the author). A list of errata for this book has been
5733222336Schristos * posted to the sci.crypt newsgroup by the author and is available for FTP.
5833222336Schristos *
5933222336Schristos * ARCHITECTURE ASSUMPTIONS:
6033222336Schristos * This code used to have some nasty ones, but these have been removed
6133222336Schristos * by now. The code requires a 32-bit integer type, though.
6207d8486aSchristos */
6333222336Schristos #include <sys/cdefs.h>
64*e9eee4b1Snia __RCSID("$NetBSD: t_crypt.c,v 1.6 2021/10/12 13:24:21 nia Exp $");
6507d8486aSchristos
6607d8486aSchristos #include <atf-c.h>
6707d8486aSchristos #include <stdio.h>
6807d8486aSchristos #include <string.h>
6907d8486aSchristos #include <stdlib.h>
7007d8486aSchristos #include <unistd.h>
7107d8486aSchristos
7207d8486aSchristos static const struct {
7307d8486aSchristos const char *hash;
7407d8486aSchristos const char *pw;
7507d8486aSchristos } tests[] = {
7607d8486aSchristos /* "new"-style */
7707d8486aSchristos /* 0 */ { "_J9..CCCCXBrJUJV154M", "U*U*U*U*" },
7807d8486aSchristos /* 1 */ { "_J9..CCCCXUhOBTXzaiE", "U*U***U" },
7907d8486aSchristos /* 2 */ { "_J9..CCCC4gQ.mB/PffM", "U*U***U*" },
8007d8486aSchristos /* 3 */ { "_J9..XXXXvlzQGqpPPdk", "*U*U*U*U" },
8107d8486aSchristos /* 4 */ { "_J9..XXXXsqM/YSSP..Y", "*U*U*U*U*" },
8207d8486aSchristos /* 5 */ { "_J9..XXXXVL7qJCnku0I", "*U*U*U*U*U*U*U*U" },
8307d8486aSchristos /* 6 */ { "_J9..XXXXAj8cFbP5scI", "*U*U*U*U*U*U*U*U*" },
8407d8486aSchristos /* 7 */ { "_J9..SDizh.vll5VED9g", "ab1234567" },
8507d8486aSchristos /* 8 */ { "_J9..SDizRjWQ/zePPHc", "cr1234567" },
8607d8486aSchristos /* 9 */ { "_J9..SDizxmRI1GjnQuE", "zxyDPWgydbQjgq" },
8707d8486aSchristos /* 10 */ { "_K9..SaltNrQgIYUAeoY", "726 even" },
8807d8486aSchristos /* 11 */ { "_J9..SDSD5YGyRCr4W4c", "" },
8907d8486aSchristos /* "old"-style, valid salts */
9007d8486aSchristos /* 12 */ { "CCNf8Sbh3HDfQ", "U*U*U*U*" },
9107d8486aSchristos /* 13 */ { "CCX.K.MFy4Ois", "U*U***U" },
9207d8486aSchristos /* 14 */ { "CC4rMpbg9AMZ.", "U*U***U*" },
9307d8486aSchristos /* 15 */ { "XXxzOu6maQKqQ", "*U*U*U*U" },
9407d8486aSchristos /* 16 */ { "SDbsugeBiC58A", "" },
9507d8486aSchristos /* 17 */ { "./xZjzHv5vzVE", "password" },
9607d8486aSchristos /* 18 */ { "0A2hXM1rXbYgo", "password" },
9707d8486aSchristos /* 19 */ { "A9RXdR23Y.cY6", "password" },
9807d8486aSchristos /* 20 */ { "ZziFATVXHo2.6", "password" },
9907d8486aSchristos /* 21 */ { "zZDDIZ0NOlPzw", "password" },
10007d8486aSchristos /* "old"-style, "reasonable" invalid salts, UFC-crypt behavior expected */
10107d8486aSchristos /* 22 */ { "\001\002wyd0KZo65Jo", "password" },
10207d8486aSchristos /* 23 */ { "a_C10Dk/ExaG.", "password" },
10307d8486aSchristos /* 24 */ { "~\377.5OTsRVjwLo", "password" },
10407d8486aSchristos /* The below are erroneous inputs, so NULL return is expected/required */
10507d8486aSchristos /* 25 */ { "", "" }, /* no salt */
10607d8486aSchristos /* 26 */ { " ", "" }, /* setting string is too short */
10707d8486aSchristos /* 27 */ { "a:", "" }, /* unsafe character */
10807d8486aSchristos /* 28 */ { "\na", "" }, /* unsafe character */
10907d8486aSchristos /* 29 */ { "_/......", "" }, /* setting string is too short for its type */
11007d8486aSchristos /* 30 */ { "_........", "" }, /* zero iteration count */
11107d8486aSchristos /* 31 */ { "_/!......", "" }, /* invalid character in count */
11207d8486aSchristos /* 32 */ { "_/......!", "" }, /* invalid character in salt */
113c8fefd49Snia #if defined(HAVE_ARGON2)
114c8fefd49Snia /* Argon2i version number 16 */
11580833458Snia /* 33 */ { "$argon2i$v=16$m=256,t=2,p=1$c29tZXNhbHQ$/U3YPXYsSb3q9XxHvc0MLxur+GP960kN9j7emXX8zwY", "password" },
11680833458Snia /* 34 */ { "$argon2i$v=16$m=65536,t=1,p=1$c29tZXNhbHQ$gWMFUrjzsfSM2xmSxMZ4ZD1JCytetP9sSzQ4tWIXJLI", "password" },
11780833458Snia /* 35 */ { "$argon2i$v=16$m=65536,t=2,p=1$ZGlmZnNhbHQ$eaEDuQ/orvhXDLMfyLIiWXeJFvgza3vaw4kladTxxJc", "password" },
11880833458Snia /* 36 */ { "$argon2i$v=16$m=65536,t=2,p=1$c29tZXNhbHQ$6ckCB0tnVFMaOgvlGeW69ASzDOabPwGsO/ISKZYBCaM", "differentpassword" },
119c8fefd49Snia /* Argon2i version number 19 */
12080833458Snia /* 37 */ { "$argon2i$v=19$m=256,t=2,p=1$c29tZXNhbHQ$iekCn0Y3spW+sCcFanM2xBT63UP2sghkUoHLIUpWRS8", "password" },
12180833458Snia /* 38 */ { "$argon2i$v=19$m=65536,t=1,p=1$c29tZXNhbHQ$0WgHXE2YXhPr6uVgz4uUw7XYoWxRkWtvSsLaOsEbvs8", "password" },
12280833458Snia /* 39 */ { "$argon2i$v=19$m=65536,t=2,p=1$ZGlmZnNhbHQ$sDV8zPvvkfOGCw26RHsjSMvv7K2vmQq/6cxAcmxSEnE", "password" },
12380833458Snia /* 40 */ { "$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$FK6NoBr+qHAMI1jc73xTWNkCEoK9iGY6RWL1n7dNIu4", "differentpassword" },
124c8fefd49Snia /* Argon2id version number 19 */
12580833458Snia /* 41 */ { "$argon2id$v=19$m=256,t=2,p=2$c29tZXNhbHQ$bQk8UB/VmZZF4Oo79iDXuL5/0ttZwg2f/5U52iv1cDc", "password" },
12680833458Snia /* 42 */ { "$argon2id$v=19$m=65536,t=4,p=1$c29tZXNhbHQ$kCXUjmjvc5XMqQedpMTsOv+zyJEf5PhtGiUghW9jFyw", "password" },
12780833458Snia /* 43 */ { "$argon2id$v=19$m=65536,t=2,p=1$ZGlmZnNhbHQ$vfMrBczELrFdWP0ZsfhWsRPaHppYdP3MVEMIVlqoFBw", "password" },
12880833458Snia /* 44 */ { "$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$C4TWUs9rDEvq7w3+J4umqA32aWKB1+DSiRuBfYxFj94", "differentpassword" },
129c8fefd49Snia #endif
130c8fefd49Snia /* 45 */ { NULL, NULL }
13107d8486aSchristos };
13207d8486aSchristos
13307d8486aSchristos ATF_TC(crypt_salts);
13407d8486aSchristos
ATF_TC_HEAD(crypt_salts,tc)13507d8486aSchristos ATF_TC_HEAD(crypt_salts, tc)
13607d8486aSchristos {
13707d8486aSchristos
13807d8486aSchristos atf_tc_set_md_var(tc, "descr", "crypt(3) salt consistency checks");
13907d8486aSchristos }
14007d8486aSchristos
ATF_TC_BODY(crypt_salts,tc)14107d8486aSchristos ATF_TC_BODY(crypt_salts, tc)
14207d8486aSchristos {
14307d8486aSchristos for (size_t i = 0; tests[i].hash; i++) {
14407d8486aSchristos char *hash = crypt(tests[i].pw, tests[i].hash);
145e3abe877Schristos if (!hash) {
146e3abe877Schristos ATF_CHECK_MSG(0, "Test %zu NULL\n", i);
147e3abe877Schristos continue;
148e3abe877Schristos }
149e3abe877Schristos if (strcmp(hash, "*0") == 0 && strlen(tests[i].hash) < 13)
15007d8486aSchristos continue; /* expected failure */
151e3abe877Schristos if (strcmp(hash, tests[i].hash))
15207d8486aSchristos ATF_CHECK_MSG(0, "Test %zu %s != %s\n",
15307d8486aSchristos i, hash, tests[i].hash);
15407d8486aSchristos }
15507d8486aSchristos }
15607d8486aSchristos
ATF_TP_ADD_TCS(tp)15707d8486aSchristos ATF_TP_ADD_TCS(tp)
15807d8486aSchristos {
15907d8486aSchristos
16007d8486aSchristos ATF_TP_ADD_TC(tp, crypt_salts);
16107d8486aSchristos return atf_no_error();
16207d8486aSchristos }
163