pw_encrypt.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "libbb.h"
  10. #if ENABLE_USE_BB_CRYPT
  11. /*
  12. * DES and MD5 crypt implementations are taken from uclibc.
  13. * They were modified to not use static buffers.
  14. */
  15. /* Common for them */
  16. static const uint8_t ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  17. #include "pw_encrypt_des.c"
  18. #include "pw_encrypt_md5.c"
  19. static struct const_des_ctx *des_cctx;
  20. static struct des_ctx *des_ctx;
  21. /* my_crypt returns malloc'ed data */
  22. static char *my_crypt(const char *key, const char *salt)
  23. {
  24. /* First, check if we are supposed to be using the MD5 replacement
  25. * instead of DES... */
  26. if (salt[0] == '$' && salt[1] == '1' && salt[2] == '$') {
  27. return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
  28. }
  29. {
  30. if (!des_cctx)
  31. des_cctx = const_des_init();
  32. des_ctx = des_init(des_ctx, des_cctx);
  33. return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
  34. }
  35. }
  36. /* So far nobody wants to have it public */
  37. static void my_crypt_cleanup(void)
  38. {
  39. free(des_cctx);
  40. free(des_ctx);
  41. des_cctx = NULL;
  42. des_ctx = NULL;
  43. }
  44. char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
  45. {
  46. char *encrypted;
  47. #if 0 /* was CONFIG_FEATURE_SHA1_PASSWORDS, but there is no such thing??? */
  48. if (strncmp(salt, "$2$", 3) == 0) {
  49. return sha1_crypt(clear);
  50. }
  51. #endif
  52. encrypted = my_crypt(clear, salt);
  53. if (cleanup)
  54. my_crypt_cleanup();
  55. return encrypted;
  56. }
  57. #else /* if !ENABLE_USE_BB_CRYPT */
  58. char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
  59. {
  60. #if 0 /* was CONFIG_FEATURE_SHA1_PASSWORDS, but there is no such thing??? */
  61. if (strncmp(salt, "$2$", 3) == 0) {
  62. return xstrdup(sha1_crypt(clear));
  63. }
  64. #endif
  65. return xstrdup(crypt(clear, salt));
  66. }
  67. #endif