git ssb

0+

dangerousbeans / %aPBe2k3ugtjBr4rrsU1…



Tree: ae87ba58f416c1874af79af3b07e26073441c010

Files: ae87ba58f416c1874af79af3b07e26073441c010 / ext / mri / crypt_gensalt.c

3348 bytesRaw
1/*
2 * Written by Solar Designer and placed in the public domain.
3 * See crypt_blowfish.c for more information.
4 *
5 * This file contains salt generation functions for the traditional and
6 * other common crypt(3) algorithms, except for bcrypt which is defined
7 * entirely in crypt_blowfish.c.
8 */
9
10#include <string.h>
11
12#include <errno.h>
13#ifndef __set_errno
14#define __set_errno(val) errno = (val)
15#endif
16
17#undef __CONST
18#ifdef __GNUC__
19#define __CONST __const
20#else
21#define __CONST
22#endif
23
24unsigned char _crypt_itoa64[64 + 1] =
25 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
26
27char *_crypt_gensalt_traditional_rn(unsigned long count,
28 __CONST char *input, int size, char *output, int output_size)
29{
30 if (size < 2 || output_size < 2 + 1 || (count && count != 25)) {
31 if (output_size > 0) output[0] = '\0';
32 __set_errno((output_size < 2 + 1) ? ERANGE : EINVAL);
33 return NULL;
34 }
35
36 output[0] = _crypt_itoa64[(unsigned int)input[0] & 0x3f];
37 output[1] = _crypt_itoa64[(unsigned int)input[1] & 0x3f];
38 output[2] = '\0';
39
40 return output;
41}
42
43char *_crypt_gensalt_extended_rn(unsigned long count,
44 __CONST char *input, int size, char *output, int output_size)
45{
46 unsigned long value;
47
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
57 if (!count) count = 725;
58
59 output[0] = '_';
60 output[1] = _crypt_itoa64[count & 0x3f];
61 output[2] = _crypt_itoa64[(count >> 6) & 0x3f];
62 output[3] = _crypt_itoa64[(count >> 12) & 0x3f];
63 output[4] = _crypt_itoa64[(count >> 18) & 0x3f];
64 value = (unsigned long)(unsigned char)input[0] |
65 ((unsigned long)(unsigned char)input[1] << 8) |
66 ((unsigned long)(unsigned char)input[2] << 16);
67 output[5] = _crypt_itoa64[value & 0x3f];
68 output[6] = _crypt_itoa64[(value >> 6) & 0x3f];
69 output[7] = _crypt_itoa64[(value >> 12) & 0x3f];
70 output[8] = _crypt_itoa64[(value >> 18) & 0x3f];
71 output[9] = '\0';
72
73 return output;
74}
75
76char *_crypt_gensalt_md5_rn(unsigned long count,
77 __CONST char *input, int size, char *output, int output_size)
78{
79 unsigned long value;
80
81 if (size < 3 || output_size < 3 + 4 + 1 || (count && count != 1000)) {
82 if (output_size > 0) output[0] = '\0';
83 __set_errno((output_size < 3 + 4 + 1) ? ERANGE : EINVAL);
84 return NULL;
85 }
86
87 output[0] = '$';
88 output[1] = '1';
89 output[2] = '$';
90 value = (unsigned long)(unsigned char)input[0] |
91 ((unsigned long)(unsigned char)input[1] << 8) |
92 ((unsigned long)(unsigned char)input[2] << 16);
93 output[3] = _crypt_itoa64[value & 0x3f];
94 output[4] = _crypt_itoa64[(value >> 6) & 0x3f];
95 output[5] = _crypt_itoa64[(value >> 12) & 0x3f];
96 output[6] = _crypt_itoa64[(value >> 18) & 0x3f];
97 output[7] = '\0';
98
99 if (size >= 6 && output_size >= 3 + 4 + 4 + 1) {
100 value = (unsigned long)(unsigned char)input[3] |
101 ((unsigned long)(unsigned char)input[4] << 8) |
102 ((unsigned long)(unsigned char)input[5] << 16);
103 output[7] = _crypt_itoa64[value & 0x3f];
104 output[8] = _crypt_itoa64[(value >> 6) & 0x3f];
105 output[9] = _crypt_itoa64[(value >> 12) & 0x3f];
106 output[10] = _crypt_itoa64[(value >> 18) & 0x3f];
107 output[11] = '\0';
108 }
109
110 return output;
111}
112

Built with git-ssb-web