pw_encrypt.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. /* static const uint8_t ascii64[] =
  11. * "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  12. */
  13. static int i64c(int i)
  14. {
  15. i &= 0x3f;
  16. if (i == 0)
  17. return '.';
  18. if (i == 1)
  19. return '/';
  20. if (i < 12)
  21. return ('0' - 2 + i);
  22. if (i < 38)
  23. return ('A' - 12 + i);
  24. return ('a' - 38 + i);
  25. }
  26. int FAST_FUNC crypt_make_salt(char *p, int cnt, int x)
  27. {
  28. x += getpid() + time(NULL);
  29. do {
  30. /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
  31. * (low-order bit is not "random", etc...),
  32. * but for our purposes it is good enough */
  33. x = x*1664525 + 1013904223;
  34. /* BTW, Park and Miller's "minimal standard generator" is
  35. * x = x*16807 % ((2^31)-1)
  36. * It has no problem with visibly alternating lowest bit
  37. * but is also weak in cryptographic sense + needs div,
  38. * which needs more code (and slower) on many CPUs */
  39. *p++ = i64c(x >> 16);
  40. *p++ = i64c(x >> 22);
  41. } while (--cnt);
  42. *p = '\0';
  43. return x;
  44. }
  45. #if ENABLE_USE_BB_CRYPT
  46. static char*
  47. to64(char *s, unsigned v, int n)
  48. {
  49. while (--n >= 0) {
  50. /* *s++ = ascii64[v & 0x3f]; */
  51. *s++ = i64c(v);
  52. v >>= 6;
  53. }
  54. return s;
  55. }
  56. /*
  57. * DES and MD5 crypt implementations are taken from uclibc.
  58. * They were modified to not use static buffers.
  59. */
  60. #include "pw_encrypt_des.c"
  61. #include "pw_encrypt_md5.c"
  62. #if ENABLE_USE_BB_CRYPT_SHA
  63. #include "pw_encrypt_sha.c"
  64. #endif
  65. /* Other advanced crypt ids (TODO?): */
  66. /* $2$ or $2a$: Blowfish */
  67. static struct const_des_ctx *des_cctx;
  68. static struct des_ctx *des_ctx;
  69. /* my_crypt returns malloc'ed data */
  70. static char *my_crypt(const char *key, const char *salt)
  71. {
  72. /* MD5 or SHA? */
  73. if (salt[0] == '$' && salt[1] && salt[2] == '$') {
  74. if (salt[1] == '1')
  75. return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
  76. #if ENABLE_USE_BB_CRYPT_SHA
  77. if (salt[1] == '5' || salt[1] == '6')
  78. return sha_crypt((char*)key, (char*)salt);
  79. #endif
  80. }
  81. if (!des_cctx)
  82. des_cctx = const_des_init();
  83. des_ctx = des_init(des_ctx, des_cctx);
  84. return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
  85. }
  86. /* So far nobody wants to have it public */
  87. static void my_crypt_cleanup(void)
  88. {
  89. free(des_cctx);
  90. free(des_ctx);
  91. des_cctx = NULL;
  92. des_ctx = NULL;
  93. }
  94. char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
  95. {
  96. char *encrypted;
  97. encrypted = my_crypt(clear, salt);
  98. if (cleanup)
  99. my_crypt_cleanup();
  100. return encrypted;
  101. }
  102. #else /* if !ENABLE_USE_BB_CRYPT */
  103. char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
  104. {
  105. return xstrdup(crypt(clear, salt));
  106. }
  107. #endif