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