whitespace and comment fixes, no code changes
[oweals/busybox.git] / libbb / pw_encrypt.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routine.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include "libbb.h"
11
12 #if ENABLE_USE_BB_CRYPT
13
14 /*
15  * DES and MD5 crypt implementations are taken from uclibc.
16  * They were modified to not use static buffers.
17  */
18 /* Common for them */
19 static const uint8_t ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
20 #include "pw_encrypt_des.c"
21 #include "pw_encrypt_md5.c"
22
23
24 static struct const_des_ctx *des_cctx;
25 static struct des_ctx *des_ctx;
26
27 /* my_crypt returns malloc'ed data */
28 static char *my_crypt(const char *key, const char *salt)
29 {
30         /* First, check if we are supposed to be using the MD5 replacement
31          * instead of DES...  */
32         if (salt[0] == '$' && salt[1] == '1' && salt[2] == '$') {
33                 return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
34         }
35
36         {
37                 if (!des_cctx)
38                         des_cctx = const_des_init();
39                 des_ctx = des_init(des_ctx, des_cctx);
40                 return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
41         }
42 }
43
44 /* So far nobody wants to have it public */
45 static void my_crypt_cleanup(void)
46 {
47         free(des_cctx);
48         free(des_ctx);
49         des_cctx = NULL;
50         des_ctx = NULL;
51 }
52
53 char *pw_encrypt(const char *clear, const char *salt, int cleanup)
54 {
55         char *encrypted;
56
57 #if 0 /* was CONFIG_FEATURE_SHA1_PASSWORDS, but there is no such thing??? */
58         if (strncmp(salt, "$2$", 3) == 0) {
59                 return sha1_crypt(clear);
60         }
61 #endif
62
63         encrypted = my_crypt(clear, salt);
64
65         if (cleanup)
66                 my_crypt_cleanup();
67
68         return encrypted;
69 }
70
71 #else /* if !ENABLE_USE_BB_CRYPT */
72
73 char *pw_encrypt(const char *clear, const char *salt, int cleanup)
74 {
75 #if 0 /* was CONFIG_FEATURE_SHA1_PASSWORDS, but there is no such thing??? */
76         if (strncmp(salt, "$2$", 3) == 0) {
77                 return xstrdup(sha1_crypt(clear));
78         }
79 #endif
80
81         return xstrdup(crypt(clear, salt));
82 }
83
84 #endif