pw_encrypt.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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[] ALIGN1 =
  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. /* Standard chpasswd uses uppercase algos ("MD5", not "md5").
  51. * Need to be case-insensitive in the code below.
  52. */
  53. if ((algo[0]|0x20) != 'd') { /* not des */
  54. len = 8/2; /* so far assuming md5 */
  55. *salt_ptr++ = '$';
  56. *salt_ptr++ = '1';
  57. *salt_ptr++ = '$';
  58. #if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
  59. if ((algo[0]|0x20) == 's') { /* sha */
  60. salt[1] = '5' + (strcasecmp(algo, "sha512") == 0);
  61. len = 16/2;
  62. }
  63. #endif
  64. }
  65. crypt_make_salt(salt_ptr, len);
  66. return salt_ptr;
  67. }
  68. #if ENABLE_USE_BB_CRYPT
  69. static char*
  70. to64(char *s, unsigned v, int n)
  71. {
  72. while (--n >= 0) {
  73. /* *s++ = ascii64[v & 0x3f]; */
  74. *s++ = i64c(v);
  75. v >>= 6;
  76. }
  77. return s;
  78. }
  79. /*
  80. * DES and MD5 crypt implementations are taken from uclibc.
  81. * They were modified to not use static buffers.
  82. */
  83. #include "pw_encrypt_des.c"
  84. #include "pw_encrypt_md5.c"
  85. #if ENABLE_USE_BB_CRYPT_SHA
  86. #include "pw_encrypt_sha.c"
  87. #endif
  88. /* Other advanced crypt ids (TODO?): */
  89. /* $2$ or $2a$: Blowfish */
  90. static struct const_des_ctx *des_cctx;
  91. static struct des_ctx *des_ctx;
  92. /* my_crypt returns malloc'ed data */
  93. static char *my_crypt(const char *key, const char *salt)
  94. {
  95. /* MD5 or SHA? */
  96. if (salt[0] == '$' && salt[1] && salt[2] == '$') {
  97. if (salt[1] == '1')
  98. return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
  99. #if ENABLE_USE_BB_CRYPT_SHA
  100. if (salt[1] == '5' || salt[1] == '6')
  101. return sha_crypt((char*)key, (char*)salt);
  102. #endif
  103. }
  104. if (!des_cctx)
  105. des_cctx = const_des_init();
  106. des_ctx = des_init(des_ctx, des_cctx);
  107. return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
  108. }
  109. /* So far nobody wants to have it public */
  110. static void my_crypt_cleanup(void)
  111. {
  112. free(des_cctx);
  113. free(des_ctx);
  114. des_cctx = NULL;
  115. des_ctx = NULL;
  116. }
  117. char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
  118. {
  119. char *encrypted;
  120. encrypted = my_crypt(clear, salt);
  121. if (cleanup)
  122. my_crypt_cleanup();
  123. return encrypted;
  124. }
  125. #else /* if !ENABLE_USE_BB_CRYPT */
  126. char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
  127. {
  128. char *s;
  129. s = crypt(clear, salt);
  130. /*
  131. * glibc used to return "" on malformed salts (for example, ""),
  132. * but since 2.17 it returns NULL.
  133. */
  134. return xstrdup(s ? s : "");
  135. }
  136. #endif