blob: 3463fd95ba91f29e9a3e59be801db64565319ad0 [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
Alex Samorukov09aba8b2021-01-04 01:36:05 +010010# if !defined(__FreeBSD__)
11# include <crypt.h>
12# endif
Denys Vlasenko4767a532019-07-16 15:09:06 +020013#endif
Rob Landleyea224be2006-06-18 20:20:07 +000014#include "libbb.h"
Eric Andersen27f64e12002-06-23 04:24:25 +000015
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020016/* static const uint8_t ascii64[] ALIGN1 =
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000017 * "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
18 */
19
20static int i64c(int i)
21{
22 i &= 0x3f;
23 if (i == 0)
24 return '.';
25 if (i == 1)
26 return '/';
27 if (i < 12)
28 return ('0' - 2 + i);
29 if (i < 38)
30 return ('A' - 12 + i);
31 return ('a' - 38 + i);
32}
33
Denys Vlasenko12a43272011-05-13 03:19:01 +020034int FAST_FUNC crypt_make_salt(char *p, int cnt /*, int x */)
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000035{
Denys Vlasenko12a43272011-05-13 03:19:01 +020036 /* was: x += ... */
Rostislav Skudnov87625122017-02-01 18:35:13 +000037 unsigned x = getpid() + monotonic_us();
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000038 do {
39 /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
40 * (low-order bit is not "random", etc...),
41 * but for our purposes it is good enough */
42 x = x*1664525 + 1013904223;
43 /* BTW, Park and Miller's "minimal standard generator" is
44 * x = x*16807 % ((2^31)-1)
45 * It has no problem with visibly alternating lowest bit
46 * but is also weak in cryptographic sense + needs div,
47 * which needs more code (and slower) on many CPUs */
48 *p++ = i64c(x >> 16);
49 *p++ = i64c(x >> 22);
50 } while (--cnt);
51 *p = '\0';
52 return x;
53}
54
Denys Vlasenko12a43272011-05-13 03:19:01 +020055char* FAST_FUNC crypt_make_pw_salt(char salt[MAX_PW_SALT_LEN], const char *algo)
56{
57 int len = 2/2;
58 char *salt_ptr = salt;
Pascal Bach2c0d3f52015-12-18 19:01:14 +010059
60 /* Standard chpasswd uses uppercase algos ("MD5", not "md5").
61 * Need to be case-insensitive in the code below.
62 */
63 if ((algo[0]|0x20) != 'd') { /* not des */
Denys Vlasenko12a43272011-05-13 03:19:01 +020064 len = 8/2; /* so far assuming md5 */
65 *salt_ptr++ = '$';
66 *salt_ptr++ = '1';
67 *salt_ptr++ = '$';
68#if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
Pascal Bach2c0d3f52015-12-18 19:01:14 +010069 if ((algo[0]|0x20) == 's') { /* sha */
70 salt[1] = '5' + (strcasecmp(algo, "sha512") == 0);
Denys Vlasenko12a43272011-05-13 03:19:01 +020071 len = 16/2;
72 }
73#endif
74 }
75 crypt_make_salt(salt_ptr, len);
76 return salt_ptr;
77}
78
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +000079#if ENABLE_USE_BB_CRYPT
80
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000081static char*
82to64(char *s, unsigned v, int n)
83{
84 while (--n >= 0) {
85 /* *s++ = ascii64[v & 0x3f]; */
86 *s++ = i64c(v);
87 v >>= 6;
88 }
89 return s;
90}
91
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000092/*
93 * DES and MD5 crypt implementations are taken from uclibc.
94 * They were modified to not use static buffers.
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000095 */
Denis Vlasenko2211d522008-11-10 18:52:35 +000096
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000097#include "pw_encrypt_des.c"
98#include "pw_encrypt_md5.c"
Denis Vlasenko2211d522008-11-10 18:52:35 +000099#if ENABLE_USE_BB_CRYPT_SHA
100#include "pw_encrypt_sha.c"
101#endif
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000102
Denis Vlasenko2211d522008-11-10 18:52:35 +0000103/* Other advanced crypt ids (TODO?): */
Denis Vlasenko30e1ab62008-11-07 13:36:46 +0000104/* $2$ or $2a$: Blowfish */
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000105
106static struct const_des_ctx *des_cctx;
107static struct des_ctx *des_ctx;
108
109/* my_crypt returns malloc'ed data */
110static char *my_crypt(const char *key, const char *salt)
Eric Andersen27f64e12002-06-23 04:24:25 +0000111{
Denis Vlasenko2211d522008-11-10 18:52:35 +0000112 /* MD5 or SHA? */
113 if (salt[0] == '$' && salt[1] && salt[2] == '$') {
114 if (salt[1] == '1')
115 return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
116#if ENABLE_USE_BB_CRYPT_SHA
117 if (salt[1] == '5' || salt[1] == '6')
118 return sha_crypt((char*)key, (char*)salt);
119#endif
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000120 }
121
Denis Vlasenko2211d522008-11-10 18:52:35 +0000122 if (!des_cctx)
123 des_cctx = const_des_init();
124 des_ctx = des_init(des_ctx, des_cctx);
Denys Vlasenko73d93d92020-12-15 23:19:22 +0100125 /* Can return NULL if salt is bad ("" or "<one_char>") */
Denis Vlasenko2211d522008-11-10 18:52:35 +0000126 return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000127}
128
129/* So far nobody wants to have it public */
130static void my_crypt_cleanup(void)
131{
132 free(des_cctx);
133 free(des_ctx);
134 des_cctx = NULL;
135 des_ctx = NULL;
136}
137
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000138char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000139{
Denis Vlasenkofdddab02008-06-12 16:56:52 +0000140 char *encrypted;
Eric Andersen27f64e12002-06-23 04:24:25 +0000141
Denis Vlasenkofdddab02008-06-12 16:56:52 +0000142 encrypted = my_crypt(clear, salt);
Denys Vlasenko73d93d92020-12-15 23:19:22 +0100143 if (!encrypted)
144 bb_simple_error_msg_and_die("bad salt");
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000145
146 if (cleanup)
147 my_crypt_cleanup();
148
Denis Vlasenkofdddab02008-06-12 16:56:52 +0000149 return encrypted;
Eric Andersen27f64e12002-06-23 04:24:25 +0000150}
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +0000151
152#else /* if !ENABLE_USE_BB_CRYPT */
153
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000154char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +0000155{
Denys Vlasenko73d93d92020-12-15 23:19:22 +0100156 char *encrypted;
Denys Vlasenko8ed96722014-02-09 14:38:03 +0100157
Denys Vlasenko73d93d92020-12-15 23:19:22 +0100158 encrypted = crypt(clear, salt);
Denys Vlasenko8ed96722014-02-09 14:38:03 +0100159 /*
160 * glibc used to return "" on malformed salts (for example, ""),
161 * but since 2.17 it returns NULL.
162 */
Denys Vlasenko73d93d92020-12-15 23:19:22 +0100163 if (!encrypted || !encrypted[0])
164 bb_simple_error_msg_and_die("bad salt");
165 return xstrdup(encrypted);
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +0000166}
167
168#endif