pw_encrypt.c 3.8 KB

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