crypto_sha512crypt.c revision 63be61852bd7ad1f74569843fb90629d63adb591
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* This file is based on nss_sha512crypt.c which is based on the work of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Ulrich Drepper (http://people.redhat.com/drepper/SHA-crypt.txt).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * libcrypto is used to provide SHA512 and random number generation.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Sumit Bose <sbose@redhat.com>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * George McCollister <georgem@novatech-llc.com>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* SHA512-based Unix crypt implementation.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Released into the Public Domain by Ulrich Drepper <drepper@redhat.com>. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* Define our magic string to mark salt for SHA512 "encryption" replacement. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* Prefix for optional rounds specification. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* Table with characters for base64 transformation. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* base64 conversion function */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic inline void b64_from_24bit(char **dest, size_t *len, size_t n,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for (i = 0; i < n; i++) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin const char *salt,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin unsigned char temp_result[64] __attribute__((__aligned__(ALIGN64)));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin unsigned char alt_result[64] __attribute__((__aligned__(ALIGN64)));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin bool rounds_custom = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin unsigned int part;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Find beginning of salt string. The prefix should normally always be
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * present. Just in case it is not. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (strncmp(salt, sha512_salt_prefix, SALT_PREF_SIZE) == 0) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Skip salt prefix. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (strncmp(salt, sha512_rounds_prefix, ROUNDS_SIZE) == 0) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin unsigned long int srounds;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin const char *num;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin key = copied_key = memcpy(tmp + ALIGN64 - PTR_2_INT(tmp) % ALIGN64, key, key_len);
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin salt = copied_salt = memcpy(tmp + ALIGN64 - PTR_2_INT(tmp) % ALIGN64, salt, salt_len);
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz /* Prepare for the real work. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Add the key string. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&ctx, (const unsigned char *)key, key_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* The last part is the salt string. This must be at most 16
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * characters and it ends at the first `$' character (for
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin * compatibility with existing implementations). */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&ctx, (const unsigned char *)salt, salt_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Compute alternate SHA512 sum with input KEY, SALT, and KEY.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The final result will be added to the first context. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (!EVP_DigestInit_ex(&alt_ctx, EVP_sha512(), NULL)) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Add key. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&alt_ctx, (const unsigned char *)key, key_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Add salt. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&alt_ctx, (const unsigned char *)salt, salt_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Add key again. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&alt_ctx, (const unsigned char *)key, key_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Now get result of this (64 bytes) and add it to the other context. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Add for any character in the key one byte of the alternate sum. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Take the binary representation of the length of the key and for every
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 1 add the alternate sum, for every 0 the key. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&ctx, (const unsigned char *)key, key_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Create intermediate result. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Start computation of P byte sequence. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (!EVP_DigestInit_ex(&alt_ctx, EVP_sha512(), NULL)) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* For every character in the password add the entire password. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&alt_ctx, (const unsigned char *)key, key_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Finish the digest. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Create byte sequence P. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Start computation of S byte sequence. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (!EVP_DigestInit_ex(&alt_ctx, EVP_sha512(), NULL)) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* For every character in the password add the entire salt. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&alt_ctx, (const unsigned char *)salt, salt_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Finish the digest. */
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz /* Create byte sequence S. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Repeatedly run the collected hash value through SHA512 to burn CPU cycles. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Add key or last result. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&ctx, (const unsigned char *)p_bytes, key_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Add salt for numbers not divisible by 3. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&ctx, (const unsigned char *)s_bytes, salt_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Add key for numbers not divisible by 7. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&ctx, (const unsigned char *)p_bytes, key_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Add key or last result. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin EVP_DigestUpdate(&ctx, (const unsigned char *)p_bytes, key_len);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Create intermediate result. */
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz /* Now we can construct the result string.
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner * It consists of three parts. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin cp = __stpncpy(buffer, sha512_salt_prefix, SALT_PREF_SIZE);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (n < 0 || n >= buflen) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* fuzzyfill the base 64 string */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for (n = 0; n < 21; n++) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin b64_from_24bit(&cp, &buflen, 4, alt_result[p1], alt_result[p2], alt_result[p3]);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (buflen == 0) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* 64th and last byte */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (buflen == 0) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Clear the buffer for the intermediate result so that people attaching
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * to processes or reading core dumps cannot get any information. We do it
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * in this way to clear correct_words[] inside the SHA512 implementation
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * as well. */
return EOK;