blob: 0b826f48dc892c04b3bce8f59bf95a6a5fc36a20 [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
Denis Vlasenko30e1ab62008-11-07 13:36:46 +000023/* Other advanced crypt ids: */
24/* $2$ or $2a$: Blowfish */
25/* $5$: SHA-256 */
26/* $6$: SHA-512 */
27/* TODO: implement SHA - http://people.redhat.com/drepper/SHA-crypt.txt */
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000028
29static struct const_des_ctx *des_cctx;
30static struct des_ctx *des_ctx;
31
32/* my_crypt returns malloc'ed data */
33static char *my_crypt(const char *key, const char *salt)
Eric Andersen27f64e12002-06-23 04:24:25 +000034{
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000035 /* First, check if we are supposed to be using the MD5 replacement
36 * instead of DES... */
37 if (salt[0] == '$' && salt[1] == '1' && salt[2] == '$') {
38 return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
39 }
40
41 {
42 if (!des_cctx)
43 des_cctx = const_des_init();
44 des_ctx = des_init(des_ctx, des_cctx);
45 return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
46 }
47}
48
49/* So far nobody wants to have it public */
50static void my_crypt_cleanup(void)
51{
52 free(des_cctx);
53 free(des_ctx);
54 des_cctx = NULL;
55 des_ctx = NULL;
56}
57
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000058char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000059{
Denis Vlasenkofdddab02008-06-12 16:56:52 +000060 char *encrypted;
Eric Andersen27f64e12002-06-23 04:24:25 +000061
Denis Vlasenkofdddab02008-06-12 16:56:52 +000062 encrypted = my_crypt(clear, salt);
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000063
64 if (cleanup)
65 my_crypt_cleanup();
66
Denis Vlasenkofdddab02008-06-12 16:56:52 +000067 return encrypted;
Eric Andersen27f64e12002-06-23 04:24:25 +000068}
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +000069
70#else /* if !ENABLE_USE_BB_CRYPT */
71
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000072char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +000073{
Denis Vlasenkob4c5bf62008-06-15 18:35:34 +000074 return xstrdup(crypt(clear, salt));
75}
76
77#endif