diff options
author | Jakub Jelinek <jakub@redhat.com> | 2009-04-07 06:20:59 +0000 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2009-04-07 06:20:59 +0000 |
commit | 11799e4168a3ef36af3cb7ffb4f5cfaed7e244d7 (patch) | |
tree | dbf8c4b86a0b68b7754f43e579c8f035d47d1ec5 /crypt/sha512-crypt.c | |
parent | 1a6da537df1fe1726d95407ec04cf65caa1f8121 (diff) | |
download | glibc-11799e4168a3ef36af3cb7ffb4f5cfaed7e244d7.tar.gz glibc-11799e4168a3ef36af3cb7ffb4f5cfaed7e244d7.tar.xz glibc-11799e4168a3ef36af3cb7ffb4f5cfaed7e244d7.zip |
Updated to fedora-glibc-20090407T0545
Diffstat (limited to 'crypt/sha512-crypt.c')
-rw-r--r-- | crypt/sha512-crypt.c | 149 |
1 files changed, 106 insertions, 43 deletions
diff --git a/crypt/sha512-crypt.c b/crypt/sha512-crypt.c index ea96e525fb..523659e0cc 100644 --- a/crypt/sha512-crypt.c +++ b/crypt/sha512-crypt.c @@ -1,5 +1,5 @@ /* One way encryption based on SHA512 sum. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007, 2009 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Ulrich Drepper <drepper@redhat.com>, 2007. @@ -28,6 +28,52 @@ #include "sha512.h" +#ifdef USE_NSS +typedef int PRBool; +# include <hasht.h> +# include <nsslowhash.h> + +# define sha512_init_ctx(ctxp, nss_ctxp) \ + do \ + { \ + if (((nss_ctxp = NSSLOWHASH_NewContext (nss_ictx, HASH_AlgSHA512)) \ + == NULL)) \ + { \ + if (nss_ctx != NULL) \ + NSSLOWHASH_Destroy (nss_ctx); \ + if (nss_alt_ctx != NULL) \ + NSSLOWHASH_Destroy (nss_alt_ctx); \ + return NULL; \ + } \ + NSSLOWHASH_Begin (nss_ctxp); \ + } \ + while (0) + +# define sha512_process_bytes(buf, len, ctxp, nss_ctxp) \ + NSSLOWHASH_Update (nss_ctxp, (const unsigned char *) buf, len) + +# define sha512_finish_ctx(ctxp, nss_ctxp, result) \ + do \ + { \ + unsigned int ret; \ + NSSLOWHASH_End (nss_ctxp, result, &ret, sizeof (result)); \ + assert (ret == sizeof (result)); \ + NSSLOWHASH_Destroy (nss_ctxp); \ + nss_ctxp = NULL; \ + } \ + while (0) +#else +# define sha512_init_ctx(ctxp, nss_ctxp) \ + __sha512_init_ctx (ctxp) + +# define sha512_process_bytes(buf, len, ctxp, nss_ctxp) \ + __sha512_process_bytes(buf, len, ctxp) + +# define sha512_finish_ctx(ctxp, nss_ctxp, result) \ + __sha512_finish_ctx (ctxp, result) +#endif + + /* Define our magic string to mark salt for SHA512 "encryption" replacement. */ static const char sha512_salt_prefix[] = "$6$"; @@ -66,8 +112,6 @@ __sha512_crypt_r (key, salt, buffer, buflen) __attribute__ ((__aligned__ (__alignof__ (uint64_t)))); unsigned char temp_result[64] __attribute__ ((__aligned__ (__alignof__ (uint64_t)))); - struct sha512_ctx ctx; - struct sha512_ctx alt_ctx; size_t salt_len; size_t key_len; size_t cnt; @@ -123,59 +167,71 @@ __sha512_crypt_r (key, salt, buffer, buflen) assert ((salt - (char *) 0) % __alignof__ (uint64_t) == 0); } +#ifdef USE_NSS + /* Initialize libfreebl3. */ + NSSLOWInitContext *nss_ictx = NSSLOW_Init (); + if (nss_ictx == NULL) + return NULL; + NSSLOWHASHContext *nss_ctx = NULL; + NSSLOWHASHContext *nss_alt_ctx = NULL; +#else + struct sha512_ctx ctx; + struct sha512_ctx alt_ctx; +#endif + /* Prepare for the real work. */ - __sha512_init_ctx (&ctx); + sha512_init_ctx (&ctx, nss_ctx); /* Add the key string. */ - __sha512_process_bytes (key, key_len, &ctx); + sha512_process_bytes (key, key_len, &ctx, nss_ctx); /* The last part is the salt string. This must be at most 16 characters and it ends at the first `$' character. */ - __sha512_process_bytes (salt, salt_len, &ctx); + sha512_process_bytes (salt, salt_len, &ctx, nss_ctx); /* Compute alternate SHA512 sum with input KEY, SALT, and KEY. The final result will be added to the first context. */ - __sha512_init_ctx (&alt_ctx); + sha512_init_ctx (&alt_ctx, nss_alt_ctx); /* Add key. */ - __sha512_process_bytes (key, key_len, &alt_ctx); + sha512_process_bytes (key, key_len, &alt_ctx, nss_alt_ctx); /* Add salt. */ - __sha512_process_bytes (salt, salt_len, &alt_ctx); + sha512_process_bytes (salt, salt_len, &alt_ctx, nss_alt_ctx); /* Add key again. */ - __sha512_process_bytes (key, key_len, &alt_ctx); + sha512_process_bytes (key, key_len, &alt_ctx, nss_alt_ctx); /* Now get result of this (64 bytes) and add it to the other context. */ - __sha512_finish_ctx (&alt_ctx, alt_result); + sha512_finish_ctx (&alt_ctx, nss_alt_ctx, alt_result); /* Add for any character in the key one byte of the alternate sum. */ for (cnt = key_len; cnt > 64; cnt -= 64) - __sha512_process_bytes (alt_result, 64, &ctx); - __sha512_process_bytes (alt_result, cnt, &ctx); + sha512_process_bytes (alt_result, 64, &ctx, nss_ctx); + sha512_process_bytes (alt_result, cnt, &ctx, nss_ctx); /* Take the binary representation of the length of the key and for every 1 add the alternate sum, for every 0 the key. */ for (cnt = key_len; cnt > 0; cnt >>= 1) if ((cnt & 1) != 0) - __sha512_process_bytes (alt_result, 64, &ctx); + sha512_process_bytes (alt_result, 64, &ctx, nss_ctx); else - __sha512_process_bytes (key, key_len, &ctx); + sha512_process_bytes (key, key_len, &ctx, nss_ctx); /* Create intermediate result. */ - __sha512_finish_ctx (&ctx, alt_result); + sha512_finish_ctx (&ctx, nss_ctx, alt_result); /* Start computation of P byte sequence. */ - __sha512_init_ctx (&alt_ctx); + sha512_init_ctx (&alt_ctx, nss_alt_ctx); /* For every character in the password add the entire password. */ for (cnt = 0; cnt < key_len; ++cnt) - __sha512_process_bytes (key, key_len, &alt_ctx); + sha512_process_bytes (key, key_len, &alt_ctx, nss_alt_ctx); /* Finish the digest. */ - __sha512_finish_ctx (&alt_ctx, temp_result); + sha512_finish_ctx (&alt_ctx, nss_alt_ctx, temp_result); /* Create byte sequence P. */ cp = p_bytes = alloca (key_len); @@ -184,14 +240,14 @@ __sha512_crypt_r (key, salt, buffer, buflen) memcpy (cp, temp_result, cnt); /* Start computation of S byte sequence. */ - __sha512_init_ctx (&alt_ctx); + sha512_init_ctx (&alt_ctx, nss_alt_ctx); /* For every character in the password add the entire password. */ for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt) - __sha512_process_bytes (salt, salt_len, &alt_ctx); + sha512_process_bytes (salt, salt_len, &alt_ctx, nss_alt_ctx); /* Finish the digest. */ - __sha512_finish_ctx (&alt_ctx, temp_result); + sha512_finish_ctx (&alt_ctx, nss_alt_ctx, temp_result); /* Create byte sequence S. */ cp = s_bytes = alloca (salt_len); @@ -204,32 +260,37 @@ __sha512_crypt_r (key, salt, buffer, buflen) for (cnt = 0; cnt < rounds; ++cnt) { /* New context. */ - __sha512_init_ctx (&ctx); + sha512_init_ctx (&ctx, nss_ctx); /* Add key or last result. */ if ((cnt & 1) != 0) - __sha512_process_bytes (p_bytes, key_len, &ctx); + sha512_process_bytes (p_bytes, key_len, &ctx, nss_ctx); else - __sha512_process_bytes (alt_result, 64, &ctx); + sha512_process_bytes (alt_result, 64, &ctx, nss_ctx); /* Add salt for numbers not divisible by 3. */ if (cnt % 3 != 0) - __sha512_process_bytes (s_bytes, salt_len, &ctx); + sha512_process_bytes (s_bytes, salt_len, &ctx, nss_ctx); /* Add key for numbers not divisible by 7. */ if (cnt % 7 != 0) - __sha512_process_bytes (p_bytes, key_len, &ctx); + sha512_process_bytes (p_bytes, key_len, &ctx, nss_ctx); /* Add key or last result. */ if ((cnt & 1) != 0) - __sha512_process_bytes (alt_result, 64, &ctx); + sha512_process_bytes (alt_result, 64, &ctx, nss_ctx); else - __sha512_process_bytes (p_bytes, key_len, &ctx); + sha512_process_bytes (p_bytes, key_len, &ctx, nss_ctx); /* Create intermediate result. */ - __sha512_finish_ctx (&ctx, alt_result); + sha512_finish_ctx (&ctx, nss_ctx, alt_result); } +#ifdef USE_NSS + /* Free libfreebl3 resources. */ + NSSLOW_Shutdown (nss_ictx); +#endif + /* Now we can construct the result string. It consists of three parts. */ cp = __stpncpy (buffer, sha512_salt_prefix, MAX (0, buflen)); @@ -252,17 +313,17 @@ __sha512_crypt_r (key, salt, buffer, buflen) --buflen; } -#define b64_from_24bit(B2, B1, B0, N) \ - do { \ - unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \ - int n = (N); \ - while (n-- > 0 && buflen > 0) \ - { \ - *cp++ = b64t[w & 0x3f]; \ - --buflen; \ - w >>= 6; \ - } \ - } while (0) + void b64_from_24bit (unsigned int b2, unsigned int b1, unsigned int b0, + int n) + { + unsigned int w = (b2 << 16) | (b1 << 8) | b0; + while (n-- > 0 && buflen > 0) + { + *cp++ = b64t[w & 0x3f]; + --buflen; + w >>= 6; + } + } b64_from_24bit (alt_result[0], alt_result[21], alt_result[42], 4); b64_from_24bit (alt_result[22], alt_result[43], alt_result[1], 4); @@ -299,13 +360,15 @@ __sha512_crypt_r (key, salt, buffer, buflen) attaching to processes or reading core dumps cannot get any information. We do it in this way to clear correct_words[] inside the SHA512 implementation as well. */ +#ifndef USE_NSS __sha512_init_ctx (&ctx); __sha512_finish_ctx (&ctx, alt_result); + memset (&ctx, '\0', sizeof (ctx)); + memset (&alt_ctx, '\0', sizeof (alt_ctx)); +#endif memset (temp_result, '\0', sizeof (temp_result)); memset (p_bytes, '\0', key_len); memset (s_bytes, '\0', salt_len); - memset (&ctx, '\0', sizeof (ctx)); - memset (&alt_ctx, '\0', sizeof (alt_ctx)); if (copied_key != NULL) memset (copied_key, '\0', key_len); if (copied_salt != NULL) |