tls: prepare for ECDH_anon ciphers
[oweals/busybox.git] / libbb / pw_encrypt_sha.c
index e46848b71b6a852077ed60ef684fc0e9ae287dc6..5457d7ab6a8a37613e46fcf49e58002cfeaba48a 100644 (file)
@@ -3,7 +3,7 @@
  */
 
 /* Prefix for optional rounds specification.  */
-static const char str_rounds[] = "rounds=%u$";
+static const char str_rounds[] ALIGN1 = "rounds=%u$";
 
 /* Maximum salt string length.  */
 #define SALT_LEN_MAX 16
@@ -18,9 +18,10 @@ static char *
 NOINLINE
 sha_crypt(/*const*/ char *key_data, /*const*/ char *salt_data)
 {
+#undef sha_end
        void (*sha_begin)(void *ctx) FAST_FUNC;
-       void (*sha_hash)(const void *buffer, size_t len, void *ctx) FAST_FUNC;
-       void (*sha_end)(void *resbuf, void *ctx) FAST_FUNC;
+       void (*sha_hash)(void *ctx, const void *buffer, size_t len) FAST_FUNC;
+       unsigned (*sha_end)(void *ctx, void *resbuf) FAST_FUNC;
        int _32or64;
 
        char *result, *resptr;
@@ -47,16 +48,17 @@ sha_crypt(/*const*/ char *key_data, /*const*/ char *salt_data)
        unsigned cnt;
        unsigned rounds;
        char *cp;
-       char is_sha512;
 
        /* Analyze salt, construct already known part of result */
        cnt = strlen(salt_data) + 1 + 43 + 1;
-       is_sha512 = salt_data[1];
-       if (is_sha512 == '6')
+       _32or64 = 32;
+       if (salt_data[1] == '6') { /* sha512 */
+               _32or64 *= 2; /*64*/
                cnt += 43;
+       }
        result = resptr = xzalloc(cnt); /* will provide NUL terminator */
        *resptr++ = '$';
-       *resptr++ = is_sha512;
+       *resptr++ = salt_data[1];
        *resptr++ = '$';
        rounds = ROUNDS_DEFAULT;
        salt_data += 3;
@@ -93,50 +95,48 @@ sha_crypt(/*const*/ char *key_data, /*const*/ char *salt_data)
        sha_begin = (void*)sha256_begin;
        sha_hash = (void*)sha256_hash;
        sha_end = (void*)sha256_end;
-       _32or64 = 32;
-       if (is_sha512 == '6') {
+       if (_32or64 != 32) {
                sha_begin = (void*)sha512_begin;
                sha_hash = (void*)sha512_hash;
                sha_end = (void*)sha512_end;
-               _32or64 = 64;
        }
 
        /* Add KEY, SALT.  */
        sha_begin(&ctx);
-       sha_hash(key_data, key_len, &ctx);
-       sha_hash(salt_data, salt_len, &ctx);
+       sha_hash(&ctx, key_data, key_len);
+       sha_hash(&ctx, salt_data, salt_len);
 
        /* Compute alternate SHA sum with input KEY, SALT, and KEY.
           The final result will be added to the first context.  */
        sha_begin(&alt_ctx);
-       sha_hash(key_data, key_len, &alt_ctx);
-       sha_hash(salt_data, salt_len, &alt_ctx);
-       sha_hash(key_data, key_len, &alt_ctx);
-       sha_end(alt_result, &alt_ctx);
+       sha_hash(&alt_ctx, key_data, key_len);
+       sha_hash(&alt_ctx, salt_data, salt_len);
+       sha_hash(&alt_ctx, key_data, key_len);
+       sha_end(&alt_ctx, alt_result);
 
        /* Add result of this to the other context.  */
        /* Add for any character in the key one byte of the alternate sum.  */
        for (cnt = key_len; cnt > _32or64; cnt -= _32or64)
-               sha_hash(alt_result, _32or64, &ctx);
-       sha_hash(alt_result, cnt, &ctx);
+               sha_hash(&ctx, alt_result, _32or64);
+       sha_hash(&ctx, alt_result, cnt);
 
        /* 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)
-                       sha_hash(alt_result, _32or64, &ctx);
+                       sha_hash(&ctx, alt_result, _32or64);
                else
-                       sha_hash(key_data, key_len, &ctx);
+                       sha_hash(&ctx, key_data, key_len);
 
        /* Create intermediate result.  */
-       sha_end(alt_result, &ctx);
+       sha_end(&ctx, alt_result);
 
        /* Start computation of P byte sequence.  */
        /* For every character in the password add the entire password.  */
        sha_begin(&alt_ctx);
        for (cnt = 0; cnt < key_len; ++cnt)
-               sha_hash(key_data, key_len, &alt_ctx);
-       sha_end(temp_result, &alt_ctx);
+               sha_hash(&alt_ctx, key_data, key_len);
+       sha_end(&alt_ctx, temp_result);
 
        /* NB: past this point, raw key_data is not used anymore */
 
@@ -153,8 +153,8 @@ sha_crypt(/*const*/ char *key_data, /*const*/ char *salt_data)
        /* For every character in the password add the entire password.  */
        sha_begin(&alt_ctx);
        for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt)
-               sha_hash(salt_data, salt_len, &alt_ctx);
-       sha_end(temp_result, &alt_ctx);
+               sha_hash(&alt_ctx, salt_data, salt_len);
+       sha_end(&alt_ctx, temp_result);
 
        /* NB: past this point, raw salt_data is not used anymore */
 
@@ -174,22 +174,22 @@ sha_crypt(/*const*/ char *key_data, /*const*/ char *salt_data)
 
                /* Add key or last result.  */
                if ((cnt & 1) != 0)
-                       sha_hash(p_bytes, key_len, &ctx);
+                       sha_hash(&ctx, p_bytes, key_len);
                else
-                       sha_hash(alt_result, _32or64, &ctx);
+                       sha_hash(&ctx, alt_result, _32or64);
                /* Add salt for numbers not divisible by 3.  */
                if (cnt % 3 != 0)
-                       sha_hash(s_bytes, salt_len, &ctx);
+                       sha_hash(&ctx, s_bytes, salt_len);
                /* Add key for numbers not divisible by 7.  */
                if (cnt % 7 != 0)
-                       sha_hash(p_bytes, key_len, &ctx);
+                       sha_hash(&ctx, p_bytes, key_len);
                /* Add key or last result.  */
                if ((cnt & 1) != 0)
-                       sha_hash(alt_result, _32or64, &ctx);
+                       sha_hash(&ctx, alt_result, _32or64);
                else
-                       sha_hash(p_bytes, key_len, &ctx);
+                       sha_hash(&ctx, p_bytes, key_len);
 
-               sha_end(alt_result, &ctx);
+               sha_end(&ctx, alt_result);
        }
 
        /* Append encrypted password to result buffer */
@@ -200,7 +200,7 @@ do { \
        unsigned w = ((B2) << 16) | ((B1) << 8) | (B0); \
        resptr = to64(resptr, w, N); \
 } while (0)
-       if (is_sha512 == '5') {
+       if (_32or64 == 32) { /* sha256 */
                unsigned i = 0;
                while (1) {
                        unsigned j = i + 10;