blob: 469e71f6ccdc9606a8f895d9db2d07127a17419f [file] [log] [blame]
Eric Andersen27f64e12002-06-23 04:24:25 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routine.
4 *
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 *
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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 Vlasenkob4c5bf62008-06-15 18:35:34 +000012#if ENABLE_USE_BB_CRYPT
13
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000014/*
15 * DES and MD5 crypt implementations are taken from uclibc.
16 * They were modified to not use static buffers.
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000017 */
18/* Common for them */
19static const uint8_t ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
20#include "pw_encrypt_des.c"
21#include "pw_encrypt_md5.c"
22
23
24static struct const_des_ctx *des_cctx;
25static struct des_ctx *des_ctx;
26
27/* my_crypt returns malloc'ed data */
28static char *my_crypt(const char *key, const char *salt)
Eric Andersen27f64e12002-06-23 04:24:25 +000029{
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000030 /* 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 */
45static void my_crypt_cleanup(void)
46{
47 free(des_cctx);
48 free(des_ctx);
49 des_cctx = NULL;
50 des_ctx = NULL;
51}
52
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000053char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000054{
Denis Vlasenkofdddab02008-06-12 16:56:52 +000055 char *encrypted;
Eric Andersen27f64e12002-06-23 04:24:25 +000056
Denis Vlasenkode9ec922006-09-27 23:31:59 +000057#if 0 /* was CONFIG_FEATURE_SHA1_PASSWORDS, but there is no such thing??? */
Eric Andersen27f64e12002-06-23 04:24:25 +000058 if (strncmp(salt, "$2$", 3) == 0) {
59 return sha1_crypt(clear);
60 }
61#endif
Denis Vlasenko91e149a2007-06-18 10:35:06 +000062
Denis Vlasenkofdddab02008-06-12 16:56:52 +000063 encrypted = my_crypt(clear, salt);
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000064
65 if (cleanup)
66 my_crypt_cleanup();
67
Denis Vlasenkofdddab02008-06-12 16:56:52 +000068 return encrypted;
Eric Andersen27f64e12002-06-23 04:24:25 +000069}
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +000070
71#else /* if !ENABLE_USE_BB_CRYPT */
72
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000073char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +000074{
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