| 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 |
|
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <errno.h> |
| 24 |
#ifndef __set_errno |
| 25 |
#define __set_errno(val) errno = (val) |
| 26 |
#endif |
| 27 |
|
| 28 |
/* Just to make sure the prototypes match the actual definitions */ |
| 29 |
#include "crypt_gensalt.h" |
| 30 |
|
| 31 |
unsigned char _crypt_itoa64[64 + 1] = |
| 32 |
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 33 |
|
| 34 |
char *_crypt_gensalt_traditional_rn(const char *prefix, unsigned long count, |
| 35 |
const char *input, int size, char *output, int output_size) |
| 36 |
{ |
| 37 |
(void) prefix; |
| 38 |
|
| 39 |
if (size < 2 || output_size < 2 + 1 || (count && count != 25)) { |
| 40 |
if (output_size > 0) output[0] = '\0'; |
| 41 |
__set_errno((output_size < 2 + 1) ? ERANGE : EINVAL); |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
|
| 45 |
output[0] = _crypt_itoa64[(unsigned int)input[0] & 0x3f]; |
| 46 |
output[1] = _crypt_itoa64[(unsigned int)input[1] & 0x3f]; |
| 47 |
output[2] = '\0'; |
| 48 |
|
| 49 |
return output; |
| 50 |
} |
| 51 |
|
| 52 |
char *_crypt_gensalt_extended_rn(const char *prefix, unsigned long count, |
| 53 |
const char *input, int size, char *output, int output_size) |
| 54 |
{ |
| 55 |
unsigned long value; |
| 56 |
|
| 57 |
(void) prefix; |
| 58 |
|
| 59 |
/* Even iteration counts make it easier to detect weak DES keys from a look |
| 60 |
* at the hash, so they should be avoided */ |
| 61 |
if (size < 3 || output_size < 1 + 4 + 4 + 1 || |
| 62 |
(count && (count > 0xffffff || !(count & 1)))) { |
| 63 |
if (output_size > 0) output[0] = '\0'; |
| 64 |
__set_errno((output_size < 1 + 4 + 4 + 1) ? ERANGE : EINVAL); |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
|
| 68 |
if (!count) count = 725; |
| 69 |
|
| 70 |
output[0] = '_'; |
| 71 |
output[1] = _crypt_itoa64[count & 0x3f]; |
| 72 |
output[2] = _crypt_itoa64[(count >> 6) & 0x3f]; |
| 73 |
output[3] = _crypt_itoa64[(count >> 12) & 0x3f]; |
| 74 |
output[4] = _crypt_itoa64[(count >> 18) & 0x3f]; |
| 75 |
value = (unsigned long)(unsigned char)input[0] | |
| 76 |
((unsigned long)(unsigned char)input[1] << 8) | |
| 77 |
((unsigned long)(unsigned char)input[2] << 16); |
| 78 |
output[5] = _crypt_itoa64[value & 0x3f]; |
| 79 |
output[6] = _crypt_itoa64[(value >> 6) & 0x3f]; |
| 80 |
output[7] = _crypt_itoa64[(value >> 12) & 0x3f]; |
| 81 |
output[8] = _crypt_itoa64[(value >> 18) & 0x3f]; |
| 82 |
output[9] = '\0'; |
| 83 |
|
| 84 |
return output; |
| 85 |
} |
| 86 |
|
| 87 |
char *_crypt_gensalt_md5_rn(const char *prefix, unsigned long count, |
| 88 |
const char *input, int size, char *output, int output_size) |
| 89 |
{ |
| 90 |
unsigned long value; |
| 91 |
|
| 92 |
(void) prefix; |
| 93 |
|
| 94 |
if (size < 3 || output_size < 3 + 4 + 1 || (count && count != 1000)) { |
| 95 |
if (output_size > 0) output[0] = '\0'; |
| 96 |
__set_errno((output_size < 3 + 4 + 1) ? ERANGE : EINVAL); |
| 97 |
return NULL; |
| 98 |
} |
| 99 |
|
| 100 |
output[0] = '$'; |
| 101 |
output[1] = '1'; |
| 102 |
output[2] = '$'; |
| 103 |
value = (unsigned long)(unsigned char)input[0] | |
| 104 |
((unsigned long)(unsigned char)input[1] << 8) | |
| 105 |
((unsigned long)(unsigned char)input[2] << 16); |
| 106 |
output[3] = _crypt_itoa64[value & 0x3f]; |
| 107 |
output[4] = _crypt_itoa64[(value >> 6) & 0x3f]; |
| 108 |
output[5] = _crypt_itoa64[(value >> 12) & 0x3f]; |
| 109 |
output[6] = _crypt_itoa64[(value >> 18) & 0x3f]; |
| 110 |
output[7] = '\0'; |
| 111 |
|
| 112 |
if (size >= 6 && output_size >= 3 + 4 + 4 + 1) { |
| 113 |
value = (unsigned long)(unsigned char)input[3] | |
| 114 |
((unsigned long)(unsigned char)input[4] << 8) | |
| 115 |
((unsigned long)(unsigned char)input[5] << 16); |
| 116 |
output[7] = _crypt_itoa64[value & 0x3f]; |
| 117 |
output[8] = _crypt_itoa64[(value >> 6) & 0x3f]; |
| 118 |
output[9] = _crypt_itoa64[(value >> 12) & 0x3f]; |
| 119 |
output[10] = _crypt_itoa64[(value >> 18) & 0x3f]; |
| 120 |
output[11] = '\0'; |
| 121 |
} |
| 122 |
|
| 123 |
return output; |
| 124 |
} |