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 SHA512_SIZE 64
47d8ee3b5dSSamuel J. Greear
48d8ee3b5dSSamuel J. Greear char*
crypt_deprecated_sha512(const char * pw,const char * salt)49d8ee3b5dSSamuel J. Greear crypt_deprecated_sha512(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 */
605108d56fSSamuel J. Greear static const char *magic = "$4$\0/etc";
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[SHA512_SIZE];
65d8ee3b5dSSamuel J. Greear int sl, i;
66*0fe46dc6SMatthew Dillon struct sha512_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__sha512_init_ctx(&ctx);
84d8ee3b5dSSamuel J. Greear
85d8ee3b5dSSamuel J. Greear /* Hash in the password first. */
86*0fe46dc6SMatthew Dillon __crypt__sha512_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__sha512_process_bytes(magic, sizeof(magic), &ctx);
94d8ee3b5dSSamuel J. Greear
95d8ee3b5dSSamuel J. Greear /* Then the raw salt. */
96*0fe46dc6SMatthew Dillon __crypt__sha512_process_bytes(sp, sl, &ctx);
97d8ee3b5dSSamuel J. Greear
98d8ee3b5dSSamuel J. Greear /* Finish and create the output string. */
99*0fe46dc6SMatthew Dillon __crypt__sha512_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 /*
107d8ee3b5dSSamuel J. Greear * For-loop form of the algorithm in sha256.c;
108d8ee3b5dSSamuel J. Greear * breaks the final output up into 3cols and then base64's each row.
109d8ee3b5dSSamuel J. Greear */
110d8ee3b5dSSamuel J. Greear for (i = 0; i < 20; i++) {
111d8ee3b5dSSamuel J. Greear l = (final[i] << 16) | (final[i + 21] << 8) | final[i + 42];
112d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
113d8ee3b5dSSamuel J. Greear }
114d8ee3b5dSSamuel J. Greear l = (final[20] << 16) | (final[41] << 8);
115d8ee3b5dSSamuel J. Greear _crypt_to64(p, l, 4); p += 4;
116d8ee3b5dSSamuel J. Greear *p = '\0';
117d8ee3b5dSSamuel J. Greear
118d8ee3b5dSSamuel J. Greear /* Clear memory. */
119d8ee3b5dSSamuel J. Greear memset(final, 0, sizeof(final));
120d8ee3b5dSSamuel J. Greear
121d8ee3b5dSSamuel J. Greear return (passwd);
122d8ee3b5dSSamuel J. Greear }
123