crypt_gensalt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Written by Solar Designer <solar at openwall.com> in 2000-2011.
  3. * No copyright is claimed, and the software is hereby placed in the public
  4. * domain. In case this attempt to disclaim copyright and place the software
  5. * in the public domain is deemed null and void, then the software is
  6. * Copyright (c) 2000-2011 Solar Designer and it is hereby released to the
  7. * general public under the following terms:
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted.
  11. *
  12. * There's ABSOLUTELY NO WARRANTY, express or implied.
  13. *
  14. * See crypt_blowfish.c for more information.
  15. *
  16. * This file contains salt generation functions for the traditional and
  17. * other common crypt(3) algorithms, except for bcrypt which is defined
  18. * entirely in crypt_blowfish.c.
  19. */
  20. #include <string.h>
  21. #include <errno.h>
  22. #ifndef __set_errno
  23. #define __set_errno(val) errno = (val)
  24. #endif
  25. /* Just to make sure the prototypes match the actual definitions */
  26. #include "crypt_gensalt.h"
  27. unsigned char _crypt_itoa64[64 + 1] =
  28. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  29. char *_crypt_gensalt_traditional_rn(const char *prefix, unsigned long count,
  30. const char *input, int size, char *output, int output_size)
  31. {
  32. (void) prefix;
  33. if (size < 2 || output_size < 2 + 1 || (count && count != 25)) {
  34. if (output_size > 0) output[0] = '\0';
  35. __set_errno((output_size < 2 + 1) ? ERANGE : EINVAL);
  36. return NULL;
  37. }
  38. output[0] = _crypt_itoa64[(unsigned int)input[0] & 0x3f];
  39. output[1] = _crypt_itoa64[(unsigned int)input[1] & 0x3f];
  40. output[2] = '\0';
  41. return output;
  42. }
  43. char *_crypt_gensalt_extended_rn(const char *prefix, unsigned long count,
  44. const char *input, int size, char *output, int output_size)
  45. {
  46. unsigned long value;
  47. (void) prefix;
  48. /* Even iteration counts make it easier to detect weak DES keys from a look
  49. * at the hash, so they should be avoided */
  50. if (size < 3 || output_size < 1 + 4 + 4 + 1 ||
  51. (count && (count > 0xffffff || !(count & 1)))) {
  52. if (output_size > 0) output[0] = '\0';
  53. __set_errno((output_size < 1 + 4 + 4 + 1) ? ERANGE : EINVAL);
  54. return NULL;
  55. }
  56. if (!count) count = 725;
  57. output[0] = '_';
  58. output[1] = _crypt_itoa64[count & 0x3f];
  59. output[2] = _crypt_itoa64[(count >> 6) & 0x3f];
  60. output[3] = _crypt_itoa64[(count >> 12) & 0x3f];
  61. output[4] = _crypt_itoa64[(count >> 18) & 0x3f];
  62. value = (unsigned long)(unsigned char)input[0] |
  63. ((unsigned long)(unsigned char)input[1] << 8) |
  64. ((unsigned long)(unsigned char)input[2] << 16);
  65. output[5] = _crypt_itoa64[value & 0x3f];
  66. output[6] = _crypt_itoa64[(value >> 6) & 0x3f];
  67. output[7] = _crypt_itoa64[(value >> 12) & 0x3f];
  68. output[8] = _crypt_itoa64[(value >> 18) & 0x3f];
  69. output[9] = '\0';
  70. return output;
  71. }
  72. char *_crypt_gensalt_md5_rn(const char *prefix, unsigned long count,
  73. const char *input, int size, char *output, int output_size)
  74. {
  75. unsigned long value;
  76. (void) prefix;
  77. if (size < 3 || output_size < 3 + 4 + 1 || (count && count != 1000)) {
  78. if (output_size > 0) output[0] = '\0';
  79. __set_errno((output_size < 3 + 4 + 1) ? ERANGE : EINVAL);
  80. return NULL;
  81. }
  82. output[0] = '$';
  83. output[1] = '1';
  84. output[2] = '$';
  85. value = (unsigned long)(unsigned char)input[0] |
  86. ((unsigned long)(unsigned char)input[1] << 8) |
  87. ((unsigned long)(unsigned char)input[2] << 16);
  88. output[3] = _crypt_itoa64[value & 0x3f];
  89. output[4] = _crypt_itoa64[(value >> 6) & 0x3f];
  90. output[5] = _crypt_itoa64[(value >> 12) & 0x3f];
  91. output[6] = _crypt_itoa64[(value >> 18) & 0x3f];
  92. output[7] = '\0';
  93. if (size >= 6 && output_size >= 3 + 4 + 4 + 1) {
  94. value = (unsigned long)(unsigned char)input[3] |
  95. ((unsigned long)(unsigned char)input[4] << 8) |
  96. ((unsigned long)(unsigned char)input[5] << 16);
  97. output[7] = _crypt_itoa64[value & 0x3f];
  98. output[8] = _crypt_itoa64[(value >> 6) & 0x3f];
  99. output[9] = _crypt_itoa64[(value >> 12) & 0x3f];
  100. output[10] = _crypt_itoa64[(value >> 18) & 0x3f];
  101. output[11] = '\0';
  102. }
  103. return output;
  104. }