blob: a60c33c35b849b053ba303ea8f67f4245d33614e [file] [log] [blame]
Eric Andersen27f64e12002-06-23 04:24:25 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenkod1a84a22008-12-07 01:16:34 +00003 * Utility routines.
Eric Andersen27f64e12002-06-23 04:24:25 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen27f64e12002-06-23 04:24:25 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen27f64e12002-06-23 04:24:25 +00008 */
Denys Vlasenko4767a532019-07-16 15:09:06 +02009#if !ENABLE_USE_BB_CRYPT
Denys Vlasenko57dbe4d2018-06-27 09:43:38 +020010#include <crypt.h>
Denys Vlasenko4767a532019-07-16 15:09:06 +020011#endif
Rob Landleyea224be2006-06-18 20:20:07 +000012#include "libbb.h"
Eric Andersen27f64e12002-06-23 04:24:25 +000013
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020014/* static const uint8_t ascii64[] ALIGN1 =
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000015 * "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
16 */
17
18static int i64c(int i)
19{
20 i &= 0x3f;
21 if (i == 0)
22 return '.';
23 if (i == 1)
24 return '/';
25 if (i < 12)
26 return ('0' - 2 + i);
27 if (i < 38)
28 return ('A' - 12 + i);
29 return ('a' - 38 + i);
30}
31
Denys Vlasenko12a43272011-05-13 03:19:01 +020032int FAST_FUNC crypt_make_salt(char *p, int cnt /*, int x */)
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000033{
Denys Vlasenko12a43272011-05-13 03:19:01 +020034 /* was: x += ... */
Rostislav Skudnov87625122017-02-01 18:35:13 +000035 unsigned x = getpid() + monotonic_us();
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000036 do {
37 /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
38 * (low-order bit is not "random", etc...),
39 * but for our purposes it is good enough */
40 x = x*1664525 + 1013904223;
41 /* BTW, Park and Miller's "minimal standard generator" is
42 * x = x*16807 % ((2^31)-1)
43 * It has no problem with visibly alternating lowest bit
44 * but is also weak in cryptographic sense + needs div,
45 * which needs more code (and slower) on many CPUs */
46 *p++ = i64c(x >> 16);
47 *p++ = i64c(x >> 22);
48 } while (--cnt);
49 *p = '\0';
50 return x;
51}
52
Denys Vlasenko12a43272011-05-13 03:19:01 +020053char* FAST_FUNC crypt_make_pw_salt(char salt[MAX_PW_SALT_LEN], const char *algo)
54{
55 int len = 2/2;
56 char *salt_ptr = salt;
Pascal Bach2c0d3f52015-12-18 19:01:14 +010057
58 /* Standard chpasswd uses uppercase algos ("MD5", not "md5").
59 * Need to be case-insensitive in the code below.
60 */
61 if ((algo[0]|0x20) != 'd') { /* not des */
Denys Vlasenko12a43272011-05-13 03:19:01 +020062 len = 8/2; /* so far assuming md5 */
63 *salt_ptr++ = '$';
64 *salt_ptr++ = '1';
65 *salt_ptr++ = '$';
66#if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
Pascal Bach2c0d3f52015-12-18 19:01:14 +010067 if ((algo[0]|0x20) == 's') { /* sha */
68 salt[1] = '5' + (strcasecmp(algo, "sha512") == 0);
Denys Vlasenko12a43272011-05-13 03:19:01 +020069 len = 16/2;
70 }
71#endif
72 }
73 crypt_make_salt(salt_ptr, len);
74 return salt_ptr;
75}
76
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +000077#if ENABLE_USE_BB_CRYPT
78
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000079static char*
80to64(char *s, unsigned v, int n)
81{
82 while (--n >= 0) {
83 /* *s++ = ascii64[v & 0x3f]; */
84 *s++ = i64c(v);
85 v >>= 6;
86 }
87 return s;
88}
89
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000090/*
91 * DES and MD5 crypt implementations are taken from uclibc.
92 * They were modified to not use static buffers.
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000093 */
Denis Vlasenko2211d522008-11-10 18:52:35 +000094
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000095#include "pw_encrypt_des.c"
96#include "pw_encrypt_md5.c"
Denis Vlasenko2211d522008-11-10 18:52:35 +000097#if ENABLE_USE_BB_CRYPT_SHA
98#include "pw_encrypt_sha.c"
99#endif
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000100
Denis Vlasenko2211d522008-11-10 18:52:35 +0000101/* Other advanced crypt ids (TODO?): */
Denis Vlasenko30e1ab62008-11-07 13:36:46 +0000102/* $2$ or $2a$: Blowfish */
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000103
104static struct const_des_ctx *des_cctx;
105static struct des_ctx *des_ctx;
106
107/* my_crypt returns malloc'ed data */
108static char *my_crypt(const char *key, const char *salt)
Eric Andersen27f64e12002-06-23 04:24:25 +0000109{
Denis Vlasenko2211d522008-11-10 18:52:35 +0000110 /* MD5 or SHA? */
111 if (salt[0] == '$' && salt[1] && salt[2] == '$') {
112 if (salt[1] == '1')
113 return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
114#if ENABLE_USE_BB_CRYPT_SHA
115 if (salt[1] == '5' || salt[1] == '6')
116 return sha_crypt((char*)key, (char*)salt);
117#endif
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000118 }
119
Denis Vlasenko2211d522008-11-10 18:52:35 +0000120 if (!des_cctx)
121 des_cctx = const_des_init();
122 des_ctx = des_init(des_ctx, des_cctx);
Denys Vlasenko73d93d92020-12-15 23:19:22 +0100123 /* Can return NULL if salt is bad ("" or "<one_char>") */
Denis Vlasenko2211d522008-11-10 18:52:35 +0000124 return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000125}
126
127/* So far nobody wants to have it public */
128static void my_crypt_cleanup(void)
129{
130 free(des_cctx);
131 free(des_ctx);
132 des_cctx = NULL;
133 des_ctx = NULL;
134}
135
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000136char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000137{
Denis Vlasenkofdddab02008-06-12 16:56:52 +0000138 char *encrypted;
Eric Andersen27f64e12002-06-23 04:24:25 +0000139
Denis Vlasenkofdddab02008-06-12 16:56:52 +0000140 encrypted = my_crypt(clear, salt);
Denys Vlasenko73d93d92020-12-15 23:19:22 +0100141 if (!encrypted)
142 bb_simple_error_msg_and_die("bad salt");
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000143
144 if (cleanup)
145 my_crypt_cleanup();
146
Denis Vlasenkofdddab02008-06-12 16:56:52 +0000147 return encrypted;
Eric Andersen27f64e12002-06-23 04:24:25 +0000148}
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +0000149
150#else /* if !ENABLE_USE_BB_CRYPT */
151
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000152char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +0000153{
Denys Vlasenko73d93d92020-12-15 23:19:22 +0100154 char *encrypted;
Denys Vlasenko8ed96722014-02-09 14:38:03 +0100155
Denys Vlasenko73d93d92020-12-15 23:19:22 +0100156 encrypted = crypt(clear, salt);
Denys Vlasenko8ed96722014-02-09 14:38:03 +0100157 /*
158 * glibc used to return "" on malformed salts (for example, ""),
159 * but since 2.17 it returns NULL.
160 */
Denys Vlasenko73d93d92020-12-15 23:19:22 +0100161 if (!encrypted || !encrypted[0])
162 bb_simple_error_msg_and_die("bad salt");
163 return xstrdup(encrypted);
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +0000164}
165
166#endif