1 |
/* vi: set sw=4 ts=4: */ |
/* vi: set sw=4 ts=4: */ |
2 |
/* |
/* |
3 |
* Utility routine. |
* Utility routines. |
4 |
* |
* |
5 |
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
6 |
* |
* |
9 |
|
|
10 |
#include "libbb.h" |
#include "libbb.h" |
11 |
|
|
12 |
|
/* static const uint8_t ascii64[] = |
13 |
|
* "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
14 |
|
*/ |
15 |
|
|
16 |
|
static 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 |
|
|
30 |
|
int FAST_FUNC crypt_make_salt(char *p, int cnt, int x) |
31 |
|
{ |
32 |
|
x += getpid() + time(NULL); |
33 |
|
do { |
34 |
|
/* x = (x*1664525 + 1013904223) % 2^32 generator is lame |
35 |
|
* (low-order bit is not "random", etc...), |
36 |
|
* but for our purposes it is good enough */ |
37 |
|
x = x*1664525 + 1013904223; |
38 |
|
/* BTW, Park and Miller's "minimal standard generator" is |
39 |
|
* x = x*16807 % ((2^31)-1) |
40 |
|
* It has no problem with visibly alternating lowest bit |
41 |
|
* but is also weak in cryptographic sense + needs div, |
42 |
|
* which needs more code (and slower) on many CPUs */ |
43 |
|
*p++ = i64c(x >> 16); |
44 |
|
*p++ = i64c(x >> 22); |
45 |
|
} while (--cnt); |
46 |
|
*p = '\0'; |
47 |
|
return x; |
48 |
|
} |
49 |
|
|
50 |
#if ENABLE_USE_BB_CRYPT |
#if ENABLE_USE_BB_CRYPT |
51 |
|
|
52 |
|
static char* |
53 |
|
to64(char *s, unsigned v, int n) |
54 |
|
{ |
55 |
|
while (--n >= 0) { |
56 |
|
/* *s++ = ascii64[v & 0x3f]; */ |
57 |
|
*s++ = i64c(v); |
58 |
|
v >>= 6; |
59 |
|
} |
60 |
|
return s; |
61 |
|
} |
62 |
|
|
63 |
/* |
/* |
64 |
* DES and MD5 crypt implementations are taken from uclibc. |
* DES and MD5 crypt implementations are taken from uclibc. |
65 |
* They were modified to not use static buffers. |
* They were modified to not use static buffers. |
66 |
*/ |
*/ |
67 |
/* Common for them */ |
|
|
static const uint8_t ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
|
68 |
#include "pw_encrypt_des.c" |
#include "pw_encrypt_des.c" |
69 |
#include "pw_encrypt_md5.c" |
#include "pw_encrypt_md5.c" |
70 |
|
#if ENABLE_USE_BB_CRYPT_SHA |
71 |
|
#include "pw_encrypt_sha.c" |
72 |
|
#endif |
73 |
|
|
74 |
/* Other advanced crypt ids: */ |
/* Other advanced crypt ids (TODO?): */ |
75 |
/* $2$ or $2a$: Blowfish */ |
/* $2$ or $2a$: Blowfish */ |
|
/* $5$: SHA-256 */ |
|
|
/* $6$: SHA-512 */ |
|
|
/* TODO: implement SHA - http://people.redhat.com/drepper/SHA-crypt.txt */ |
|
76 |
|
|
77 |
static struct const_des_ctx *des_cctx; |
static struct const_des_ctx *des_cctx; |
78 |
static struct des_ctx *des_ctx; |
static struct des_ctx *des_ctx; |
80 |
/* my_crypt returns malloc'ed data */ |
/* my_crypt returns malloc'ed data */ |
81 |
static char *my_crypt(const char *key, const char *salt) |
static char *my_crypt(const char *key, const char *salt) |
82 |
{ |
{ |
83 |
/* First, check if we are supposed to be using the MD5 replacement |
/* MD5 or SHA? */ |
84 |
* instead of DES... */ |
if (salt[0] == '$' && salt[1] && salt[2] == '$') { |
85 |
if (salt[0] == '$' && salt[1] == '1' && salt[2] == '$') { |
if (salt[1] == '1') |
86 |
return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt); |
return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt); |
87 |
|
#if ENABLE_USE_BB_CRYPT_SHA |
88 |
|
if (salt[1] == '5' || salt[1] == '6') |
89 |
|
return sha_crypt((char*)key, (char*)salt); |
90 |
|
#endif |
91 |
} |
} |
92 |
|
|
93 |
{ |
if (!des_cctx) |
94 |
if (!des_cctx) |
des_cctx = const_des_init(); |
95 |
des_cctx = const_des_init(); |
des_ctx = des_init(des_ctx, des_cctx); |
96 |
des_ctx = des_init(des_ctx, des_cctx); |
return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt); |
|
return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt); |
|
|
} |
|
97 |
} |
} |
98 |
|
|
99 |
/* So far nobody wants to have it public */ |
/* So far nobody wants to have it public */ |