1d8ee3b5dSSamuel J. Greear /*
2d8ee3b5dSSamuel J. Greear * Copyright (c) 2010
3d8ee3b5dSSamuel J. Greear * The DragonFly Project. All rights reserved.
4d8ee3b5dSSamuel J. Greear *
5d8ee3b5dSSamuel J. Greear * This code is derived from software contributed to The DragonFly Project
6d8ee3b5dSSamuel J. Greear * by Nolan Lum <nol888@gmail.com>
7d8ee3b5dSSamuel J. Greear *
8d8ee3b5dSSamuel J. Greear * Redistribution and use in source and binary forms, with or without
9d8ee3b5dSSamuel J. Greear * modification, are permitted provided that the following conditions
10d8ee3b5dSSamuel J. Greear * are met:
11d8ee3b5dSSamuel J. Greear *
12d8ee3b5dSSamuel J. Greear * 1. Redistributions of source code must retain the above copyright
13d8ee3b5dSSamuel J. Greear * notice, this list of conditions and the following disclaimer.
14d8ee3b5dSSamuel J. Greear * 2. Redistributions in binary form must reproduce the above copyright
15d8ee3b5dSSamuel J. Greear * notice, this list of conditions and the following disclaimer in
16d8ee3b5dSSamuel J. Greear * the documentation and/or other materials provided with the
17d8ee3b5dSSamuel J. Greear * distribution.
18d8ee3b5dSSamuel J. Greear * 3. Neither the name of The DragonFly Project nor the names of its
19d8ee3b5dSSamuel J. Greear * contributors may be used to endorse or promote products derived
20d8ee3b5dSSamuel J. Greear * from this software without specific, prior written permission.
21d8ee3b5dSSamuel J. Greear *
22d8ee3b5dSSamuel J. Greear * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23d8ee3b5dSSamuel J. Greear * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24d8ee3b5dSSamuel J. Greear * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25d8ee3b5dSSamuel J. Greear * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26d8ee3b5dSSamuel J. Greear * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27d8ee3b5dSSamuel J. Greear * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28d8ee3b5dSSamuel J. Greear * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29d8ee3b5dSSamuel J. Greear * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30d8ee3b5dSSamuel J. Greear * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31d8ee3b5dSSamuel J. Greear * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32d8ee3b5dSSamuel J. Greear * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33d8ee3b5dSSamuel J. Greear * SUCH DAMAGE.
34d8ee3b5dSSamuel J. Greear */
35d8ee3b5dSSamuel J. Greear
36d8ee3b5dSSamuel J. Greear #include <sys/types.h>
37d8ee3b5dSSamuel J. Greear #include <string.h>
38*0fe46dc6SMatthew Dillon
39d8ee3b5dSSamuel J. Greear #include "crypt.h"
40*0fe46dc6SMatthew Dillon #include "local.h"
41d8ee3b5dSSamuel J. Greear
42d8ee3b5dSSamuel J. Greear /*
43d8ee3b5dSSamuel J. Greear * New password crypt.
44d8ee3b5dSSamuel J. Greear */
45d8ee3b5dSSamuel J. Greear
46d8ee3b5dSSamuel J. Greear #define SHA256_SIZE 32
47d8ee3b5dSSamuel J. Greear
48d8ee3b5dSSamuel J. Greear char*
crypt_deprecated_sha256(const char * pw,const char * salt)49d8ee3b5dSSamuel J. Greear crypt_deprecated_sha256(const char *pw, const char *salt)
50d8ee3b5dSSamuel J. Greear {
515108d56fSSamuel J. Greear /*
525108d56fSSamuel J. Greear * Magic constant (prefix) used to run over the password data.
535108d56fSSamuel J. Greear *
545108d56fSSamuel J. Greear * XXX:
555108d56fSSamuel J. Greear *
565108d56fSSamuel J. Greear * A bug below (sizeof instead of strlen) mandates the extra data after
575108d56fSSamuel J. Greear * the closing $. This data is what just happened to be (consistently
585108d56fSSamuel J. Greear * miraculously) on the stack following magic on 64-bit.
59d8ee3b5dSSamuel J. Greear */
607226fb0cSSamuel J. Greear static const char *magic = "$3$\0sha5";
615108d56fSSamuel J. Greear
62d8ee3b5dSSamuel J. Greear static char passwd[120], *p;
63d8ee3b5dSSamuel J. Greear static const char *sp, *ep;
64d8ee3b5dSSamuel J. Greear unsigned char final[SHA256_SIZE];
65d8ee3b5dSSamuel J. Greear int sl;
66*0fe46dc6SMatthew Dillon struct sha256_ctx ctx;
67d8ee3b5dSSamuel J. Greear unsigned long l;
68d8ee3b5dSSamuel J. Greear
69d8ee3b5dSSamuel J. Greear /* Refine the salt. */
70d8ee3b5dSSamuel J. Greear sp = salt;
71d8ee3b5dSSamuel J. Greear
72d8ee3b5dSSamuel J. Greear /* If it starts with the magic string, then skip that. */
73d8ee3b5dSSamuel J. Greear if (!strncmp(sp, magic, strlen(magic)))
74d8ee3b5dSSamuel J. Greear sp += strlen(magic);
75d8ee3b5dSSamuel J. Greear
76d8ee3b5dSSamuel J. Greear /* Stop at the first '$', max 8 chars. */
77d8ee3b5dSSamuel J. Greear for (ep = sp; *ep && *ep != '$' && ep < (sp + 8); ep++)
78d8ee3b5dSSamuel J. Greear continue;
79d8ee3b5dSSamuel J. Greear
80d8ee3b5dSSamuel J. Greear /* Get the actual salt length. */
81d8ee3b5dSSamuel J. Greear sl = ep - sp;
82d8ee3b5dSSamuel J. Greear
83*0fe46dc6SMatthew Dillon __crypt__sha256_init_ctx(&ctx);
84d8ee3b5dSSamuel J. Greear
85d8ee3b5dSSamuel J. Greear /* Hash in the password first. */
86*0fe46dc6SMatthew Dillon __crypt__sha256_process_bytes(pw, strlen(pw), &ctx);
87d8ee3b5dSSamuel J. Greear
885108d56fSSamuel J. Greear /*
895108d56fSSamuel J. Greear * Then the magic string
905108d56fSSamuel J. Greear *
915108d56fSSamuel J. Greear * XXX: sizeof instead of strlen, must retain
925108d56fSSamuel J. Greear */
93*0fe46dc6SMatthew Dillon __crypt__sha256_process_bytes(magic, sizeof(magic), &ctx);
94d8ee3b5dSSamuel J. Greear
95d8ee3b5dSSamuel J. Greear /* Then the raw salt. */
96*0fe46dc6SMatthew Dillon __crypt__sha256_process_bytes(sp, sl, &ctx);
97d8ee3b5dSSamuel J. Greear
98d8ee3b5dSSamuel J. Greear /* Finish and create the output string. */
99*0fe46dc6SMatthew Dillon __crypt__sha256_finish_ctx(&ctx, final);
100d8ee3b5dSSamuel J. Greear strcpy(passwd, magic);
101d8ee3b5dSSamuel J. Greear strncat(passwd, sp, sl);
102d8ee3b5dSSamuel J. Greear strcat(passwd, "$");
103d8ee3b5dSSamuel J. Greear
104d8ee3b5dSSamuel J. Greear p = passwd + strlen(passwd);
105d8ee3b5dSSamuel J. Greear
106d8ee3b5dSSamuel J. Greear l = (final[ 0] << 16) | (final[11] << 8) | final[21];
107d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
108d8ee3b5dSSamuel J. Greear l = (final[ 1] << 16) | (final[12] << 8) | final[22];
109d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
110d8ee3b5dSSamuel J. Greear l = (final[ 2] << 16) | (final[13] << 8) | final[23];
111d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
112d8ee3b5dSSamuel J. Greear l = (final[ 3] << 16) | (final[14] << 8) | final[24];
113d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
114d8ee3b5dSSamuel J. Greear l = (final[ 4] << 16) | (final[15] << 8) | final[25];
115d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
116d8ee3b5dSSamuel J. Greear l = (final[ 5] << 16) | (final[16] << 8) | final[26];
117d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
118d8ee3b5dSSamuel J. Greear l = (final[ 6] << 16) | (final[17] << 8) | final[27];
119d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
120d8ee3b5dSSamuel J. Greear l = (final[ 7] << 16) | (final[18] << 8) | final[28];
121d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
122d8ee3b5dSSamuel J. Greear l = (final[ 8] << 16) | (final[19] << 8) | final[29];
123d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
124d8ee3b5dSSamuel J. Greear l = (final[ 9] << 16) | (final[20] << 8) | final[30];
125d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
126d8ee3b5dSSamuel J. Greear l = (final[10] << 16) | (final[31] << 8);
127d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
128d8ee3b5dSSamuel J. Greear *p = '\0';
129d8ee3b5dSSamuel J. Greear
130d8ee3b5dSSamuel J. Greear /* Clear memory. */
131d8ee3b5dSSamuel J. Greear memset(final, 0, sizeof(final));
132d8ee3b5dSSamuel J. Greear
133d8ee3b5dSSamuel J. Greear return (passwd);
134d8ee3b5dSSamuel J. Greear }
135