pw_encrypt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /* was: x += ... */
  29. int x = getpid() + monotonic_us();
  30. do {
  31. /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
  32. * (low-order bit is not "random", etc...),
  33. * but for our purposes it is good enough */
  34. x = x*1664525 + 1013904223;
  35. /* BTW, Park and Miller's "minimal standard generator" is
  36. * x = x*16807 % ((2^31)-1)
  37. * It has no problem with visibly alternating lowest bit
  38. * but is also weak in cryptographic sense + needs div,
  39. * which needs more code (and slower) on many CPUs */
  40. *p++ = i64c(x >> 16);
  41. *p++ = i64c(x >> 22);
  42. } while (--cnt);
  43. *p = '\0';
  44. return x;
  45. }
  46. char* FAST_FUNC crypt_make_pw_salt(char salt[MAX_PW_SALT_LEN], const char *algo)
  47. {
  48. int len = 2/2;
  49. char *salt_ptr = salt;
  50. if (algo[0] != 'd') { /* not des */
  51. len = 8/2; /* so far assuming md5 */
  52. *salt_ptr++ = '$';
  53. *salt_ptr++ = '1';
  54. *salt_ptr++ = '$';
  55. #if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
  56. if (algo[0] == 's') { /* sha */
  57. salt[1] = '5' + (strcmp(algo, "sha512") == 0);
  58. len = 16/2;
  59. }
  60. #endif
  61. }
  62. crypt_make_salt(salt_ptr, len);
  63. return salt_ptr;
  64. }
  65. #if ENABLE_USE_BB_CRYPT
  66. static char*
  67. to64(char *s, unsigned v, int n)
  68. {
  69. while (--n >= 0) {
  70. /* *s++ = ascii64[v & 0x3f]; */
  71. *s++ = i64c(v);
  72. v >>= 6;
  73. }
  74. return s;
  75. }
  76. /*
  77. * DES and MD5 crypt implementations are taken from uclibc.
  78. * They were modified to not use static buffers.
  79. */
  80. #include "pw_encrypt_des.c"
  81. #include "pw_encrypt_md5.c"
  82. #if ENABLE_USE_BB_CRYPT_SHA
  83. #include "pw_encrypt_sha.c"
  84. #endif
  85. /* Other advanced crypt ids (TODO?): */
  86. /* $2$ or $2a$: Blowfish */
  87. static struct const_des_ctx *des_cctx;
  88. static struct des_ctx *des_ctx;
  89. /* my_crypt returns malloc'ed data */
  90. static char *my_crypt(const char *key, const char *salt)
  91. {
  92. /* MD5 or SHA? */
  93. if (salt[0] == '$' && salt[1] && salt[2] == '$') {
  94. if (salt[1] == '1')
  95. return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
  96. #if ENABLE_USE_BB_CRYPT_SHA
  97. if (salt[1] == '5' || salt[1] == '6')
  98. return sha_crypt((char*)key, (char*)salt);
  99. #endif
  100. }
  101. if (!des_cctx)
  102. des_cctx = const_des_init();
  103. des_ctx = des_init(des_ctx, des_cctx);
  104. return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
  105. }
  106. /* So far nobody wants to have it public */
  107. static void my_crypt_cleanup(void)
  108. {
  109. free(des_cctx);
  110. free(des_ctx);
  111. des_cctx = NULL;
  112. des_ctx = NULL;
  113. }
  114. char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
  115. {
  116. char *encrypted;
  117. encrypted = my_crypt(clear, salt);
  118. if (cleanup)
  119. my_crypt_cleanup();
  120. return encrypted;
  121. }
  122. #else /* if !ENABLE_USE_BB_CRYPT */
  123. char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
  124. {
  125. char *s;
  126. s = crypt(clear, salt);
  127. /*
  128. * glibc used to return "" on malformed salts (for example, ""),
  129. * but since 2.17 it returns NULL.
  130. */
  131. return xstrdup(s ? s : "");
  132. }
  133. #endif