blob: bfc7030a8387f81daa44ce8f1ccc6f50e3380233 [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 */
9
Rob Landleyea224be2006-06-18 20:20:07 +000010#include "libbb.h"
Eric Andersen27f64e12002-06-23 04:24:25 +000011
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000012/* static const uint8_t ascii64[] =
13 * "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
14 */
15
16static int i64c(int i)
17{
18 i &= 0x3f;
19 if (i == 0)
20 return '.';
21 if (i == 1)
22 return '/';
23 if (i < 12)
24 return ('0' - 2 + i);
25 if (i < 38)
26 return ('A' - 12 + i);
27 return ('a' - 38 + i);
28}
29
Denys Vlasenko12a43272011-05-13 03:19:01 +020030int FAST_FUNC crypt_make_salt(char *p, int cnt /*, int x */)
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000031{
Denys Vlasenko12a43272011-05-13 03:19:01 +020032 /* was: x += ... */
33 int x = getpid() + monotonic_us();
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000034 do {
35 /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
36 * (low-order bit is not "random", etc...),
37 * but for our purposes it is good enough */
38 x = x*1664525 + 1013904223;
39 /* BTW, Park and Miller's "minimal standard generator" is
40 * x = x*16807 % ((2^31)-1)
41 * It has no problem with visibly alternating lowest bit
42 * but is also weak in cryptographic sense + needs div,
43 * which needs more code (and slower) on many CPUs */
44 *p++ = i64c(x >> 16);
45 *p++ = i64c(x >> 22);
46 } while (--cnt);
47 *p = '\0';
48 return x;
49}
50
Denys Vlasenko12a43272011-05-13 03:19:01 +020051char* FAST_FUNC crypt_make_pw_salt(char salt[MAX_PW_SALT_LEN], const char *algo)
52{
53 int len = 2/2;
54 char *salt_ptr = salt;
55 if (algo[0] != 'd') { /* not des */
56 len = 8/2; /* so far assuming md5 */
57 *salt_ptr++ = '$';
58 *salt_ptr++ = '1';
59 *salt_ptr++ = '$';
60#if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
61 if (algo[0] == 's') { /* sha */
62 salt[1] = '5' + (strcmp(algo, "sha512") == 0);
63 len = 16/2;
64 }
65#endif
66 }
67 crypt_make_salt(salt_ptr, len);
68 return salt_ptr;
69}
70
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +000071#if ENABLE_USE_BB_CRYPT
72
Denis Vlasenkod1a84a22008-12-07 01:16:34 +000073static char*
74to64(char *s, unsigned v, int n)
75{
76 while (--n >= 0) {
77 /* *s++ = ascii64[v & 0x3f]; */
78 *s++ = i64c(v);
79 v >>= 6;
80 }
81 return s;
82}
83
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000084/*
85 * DES and MD5 crypt implementations are taken from uclibc.
86 * They were modified to not use static buffers.
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000087 */
Denis Vlasenko2211d522008-11-10 18:52:35 +000088
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000089#include "pw_encrypt_des.c"
90#include "pw_encrypt_md5.c"
Denis Vlasenko2211d522008-11-10 18:52:35 +000091#if ENABLE_USE_BB_CRYPT_SHA
92#include "pw_encrypt_sha.c"
93#endif
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000094
Denis Vlasenko2211d522008-11-10 18:52:35 +000095/* Other advanced crypt ids (TODO?): */
Denis Vlasenko30e1ab62008-11-07 13:36:46 +000096/* $2$ or $2a$: Blowfish */
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000097
98static struct const_des_ctx *des_cctx;
99static struct des_ctx *des_ctx;
100
101/* my_crypt returns malloc'ed data */
102static char *my_crypt(const char *key, const char *salt)
Eric Andersen27f64e12002-06-23 04:24:25 +0000103{
Denis Vlasenko2211d522008-11-10 18:52:35 +0000104 /* MD5 or SHA? */
105 if (salt[0] == '$' && salt[1] && salt[2] == '$') {
106 if (salt[1] == '1')
107 return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
108#if ENABLE_USE_BB_CRYPT_SHA
109 if (salt[1] == '5' || salt[1] == '6')
110 return sha_crypt((char*)key, (char*)salt);
111#endif
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000112 }
113
Denis Vlasenko2211d522008-11-10 18:52:35 +0000114 if (!des_cctx)
115 des_cctx = const_des_init();
116 des_ctx = des_init(des_ctx, des_cctx);
117 return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000118}
119
120/* So far nobody wants to have it public */
121static void my_crypt_cleanup(void)
122{
123 free(des_cctx);
124 free(des_ctx);
125 des_cctx = NULL;
126 des_ctx = NULL;
127}
128
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000129char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000130{
Denis Vlasenkofdddab02008-06-12 16:56:52 +0000131 char *encrypted;
Eric Andersen27f64e12002-06-23 04:24:25 +0000132
Denis Vlasenkofdddab02008-06-12 16:56:52 +0000133 encrypted = my_crypt(clear, salt);
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000134
135 if (cleanup)
136 my_crypt_cleanup();
137
Denis Vlasenkofdddab02008-06-12 16:56:52 +0000138 return encrypted;
Eric Andersen27f64e12002-06-23 04:24:25 +0000139}
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +0000140
141#else /* if !ENABLE_USE_BB_CRYPT */
142
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000143char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +0000144{
Denys Vlasenko8ed96722014-02-09 14:38:03 +0100145 char *s;
146
147 s = crypt(clear, salt);
148 /*
149 * glibc used to return "" on malformed salts (for example, ""),
150 * but since 2.17 it returns NULL.
151 */
152 return xstrdup(s ? s : "");
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +0000153}
154
155#endif