Moves us from e=3 to e=65537

This is designed to increase our resistance to key recovery and
bring us closer to the version of libmincrypt used elsewhere in
Google.

Change-Id: I9a7d9dc78956686bd898c3587e7cb40c231fc520
diff --git a/libc/rsa.c b/libc/rsa.c
index 2aea562..9c5636b 100644
--- a/libc/rsa.c
+++ b/libc/rsa.c
@@ -1,199 +1,168 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the 
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
+// Copyright 2008-2010 Google Inc. All Rights Reserved.
+// Author: mschilder@google.com (Marius Schilder)
 
 #include "rsa.h"
 #include "sha.h"
 
-/* a[] -= mod */
-static void subM(RSAPublicKey *key, uint32_t *a) {
-    int64_t A = 0;
-    int i;
-    for (i = 0; i < key->len; ++i) {
-        A += (uint64_t)a[i] - key->n[i];
-        a[i] = (uint32_t)A;
-        A >>= 32;
-    }
+// a[] -= mod
+static void subM(RSAPublicKey key,
+                 uint32_t* a) {
+  int64_t A = 0;
+  int i;
+  for (i = 0; i < key->len; ++i) {
+    A += (uint64_t)a[i] - key->n[i];
+    a[i] = (uint32_t)A;
+    A >>= 32;
+  }
 }
 
-/* return a[] >= mod */
-static int geM(RSAPublicKey *key, const uint32_t *a) {
-    int i;
-    for (i = key->len; i;) {
-        --i;
-        if (a[i] < key->n[i]) return 0;
-        if (a[i] > key->n[i]) return 1;
-    }
-    return 1;  /* equal */
+// return a[] >= mod
+static int geM(RSAPublicKey key,
+               const uint32_t* a) {
+  int i;
+  for (i = key->len; i;) {
+    --i;
+    if (a[i] < key->n[i]) return 0;
+    if (a[i] > key->n[i]) return 1;
+  }
+  return 1;  // equal
 }
 
-/* montgomery c[] += a * b[] / R % mod */
-static void montMulAdd(RSAPublicKey *key,
+// montgomery c[] += a * b[] / R % mod
+static void montMulAdd(RSAPublicKey key,
                        uint32_t* c,
                        const uint32_t a,
                        const uint32_t* b) {
-    uint64_t A = (uint64_t)a * b[0] + c[0];
-    uint32_t d0 = (uint32_t)A * key->n0inv;
-    uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
-    int i;
+  uint64_t A = (uint64_t)a * b[0] + c[0];
+  uint32_t d0 = (uint32_t)A * key->n0inv;
+  uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
+  int i;
 
-    for (i = 1; i < key->len; ++i) {
-        A = (A >> 32) + (uint64_t)a * b[i] + c[i];
-        B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
-        c[i - 1] = (uint32_t)B;
-    }
+  for (i = 1; i < key->len; ++i) {
+    A = (A >> 32) + (uint64_t)a * b[i] + c[i];
+    B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
+    c[i - 1] = (uint32_t)B;
+  }
 
-    A = (A >> 32) + (B >> 32);
+  A = (A >> 32) + (B >> 32);
 
-    c[i - 1] = (uint32_t)A;
+  c[i - 1] = (uint32_t)A;
 
-    if (A >> 32) {
-        subM(key, c);
-    }
+  if (A >> 32) {
+    subM(key, c);
+  }
 }
 
-/* montgomery c[] = a[] * b[] / R % mod */
-static void montMul(RSAPublicKey *key,
+// montgomery c[] = a[] * b[] / R % mod
+static void montMul(RSAPublicKey key,
                     uint32_t* c,
                     const uint32_t* a,
                     const uint32_t* b) {
-    int i;
-    for (i = 0; i < key->len; ++i) {
-        c[i] = 0;
-    }
-    for (i = 0; i < key->len; ++i) {
-        montMulAdd(key, c, a[i], b);
-    }
+  int i;
+  for (i = 0; i < key->len; ++i) {
+    c[i] = 0;
+  }
+  for (i = 0; i < key->len; ++i) {
+    montMulAdd(key, c, a[i], b);
+  }
 }
 
-/* In-place public exponentiation.
-** Input and output big-endian byte array in inout.
-*/
-static void modpow3(RSAPublicKey *key,
-                    uint8_t* inout) {
-    uint32_t a[RSANUMWORDS];
-    uint32_t aR[RSANUMWORDS];
-    uint32_t aaR[RSANUMWORDS];
-    uint32_t *aaa = aR;  /* Re-use location. */
-    int i;
+// In-place public exponentiation.
+// Input and output big-endian byte array in inout.
+static void modpowF4(RSAPublicKey key,
+                 uint8_t* inout) {
+  uint32_t a[RSANUMWORDS];
+  uint32_t aR[RSANUMWORDS];
+  uint32_t aaR[RSANUMWORDS];
+  uint32_t* aaa = aaR;  // Re-use location.
+  int i;
 
-    /* Convert from big endian byte array to little endian word array. */
-    for (i = 0; i < key->len; ++i) {
-        uint32_t tmp =
-            (inout[((key->len - 1 - i) * 4) + 0] << 24) |
-            (inout[((key->len - 1 - i) * 4) + 1] << 16) |
-            (inout[((key->len - 1 - i) * 4) + 2] << 8) |
-            (inout[((key->len - 1 - i) * 4) + 3] << 0);
-        a[i] = tmp;
-    }
+  // Convert from big endian byte array to little endian word array.
+  for (i = 0; i < key->len; ++i) {
+    uint32_t tmp =
+      (inout[((key->len - 1 - i) * 4) + 0] << 24) |
+      (inout[((key->len - 1 - i) * 4) + 1] << 16) |
+      (inout[((key->len - 1 - i) * 4) + 2] << 8) |
+      (inout[((key->len - 1 - i) * 4) + 3] << 0);
+    a[i] = tmp;
+  }
 
-    montMul(key, aR, a, key->rr);  /* aR = a * RR / R mod M   */
-    montMul(key, aaR, aR, aR);     /* aaR = aR * aR / R mod M */
-    montMul(key, aaa, aaR, a);     /* aaa = aaR * a / R mod M */
+  montMul(key, aR, a, key->rr);  // aR = a * RR / R mod M
+  for (i = 0; i < 16; i += 2) {
+    montMul(key, aaR, aR, aR);  // aaR = aR * aR / R mod M
+    montMul(key, aR, aaR, aaR);  // aR = aaR * aaR / R mod M
+  }
+  montMul(key, aaa, aR, a);  // aaa = aR * a / R mod M
 
-    /* Make sure aaa < mod; aaa is at most 1x mod too large. */
-    if (geM(key, aaa)) {
-        subM(key, aaa);
-    }
+  // Make sure aaa < mod; aaa is at most 1x mod too large.
+  if (geM(key, aaa)) {
+    subM(key, aaa);
+  }
 
-    /* Convert to bigendian byte array */
-    for (i = key->len - 1; i >= 0; --i) {
-        uint32_t tmp = aaa[i];
-        *inout++ = tmp >> 24;
-        *inout++ = tmp >> 16;
-        *inout++ = tmp >> 8;
-        *inout++ = tmp >> 0;
-    }
+  // Convert to bigendian byte array
+  for (i = key->len - 1; i >= 0; --i) {
+    uint32_t tmp = aaa[i];
+    *inout++ = tmp >> 24;
+    *inout++ = tmp >> 16;
+    *inout++ = tmp >> 8;
+    *inout++ = tmp >> 0;
+  }
 }
 
-/* Expected PKCS1.5 signature padding bytes, for a keytool RSA signature.
-** Has the 0-length optional parameter encoded in the ASN1 (as opposed to the
-** other flavor which omits the optional parameter entirely). This code does not
-** accept signatures without the optional parameter.
+// Expected PKCS1.5 signature padding bytes, for a keytool RSA signature.
+// Has the 0-length optional parameter encoded in the ASN1 (as opposed to the
+// other flavor which omits the optional parameter entirely). This code does not
+// accept signatures without the optional parameter.
+/*
+static const uint8_t padding[RSANUMBYTES] = {
+0x00,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1a,0x05,0x00,0x04,0x14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
+};
 */
-static const uint8_t padding[RSANUMBYTES - SHA_DIGEST_SIZE] = {
-    0x00,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
-    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,
-    0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1a,0x05,0x00,
-    0x04,0x14
+
+// SHA-1 of PKCS1.5 signature padding for 2048 bit, as above.
+// At the location of the bytes of the hash all 00 are hashed.
+static const uint8_t kExpectedPadShaRsa2048[SHA_DIGEST_SIZE] = {
+  0xdc, 0xbd, 0xbe, 0x42, 0xd5, 0xf5, 0xa7, 0x2e, 0x6e, 0xfc,
+  0xf5, 0x5d, 0xaf, 0x9d, 0xea, 0x68, 0x7c, 0xfb, 0xf1, 0x67
 };
 
-/* Verify a 2048 bit RSA PKCS1.5 signature against an expected SHA-1 hash.
-** Returns 0 on failure, 1 on success.
-*/
-int RSA_verify(RSAPublicKey *key,
-               const uint8_t *signature,
+// Verify a 2048 bit RSA PKCS1.5 signature against an expected SHA-1 hash.
+// Returns 0 on failure, 1 on success.
+int RSA_verify(RSAPublicKey key,
+               const uint8_t* signature,
                const int len,
-               const uint8_t *sha) {
-    uint8_t buf[RSANUMBYTES];
-    int i;
+               const uint8_t* sha) {
+  uint8_t buf[RSANUMBYTES];
+  int i;
 
-    if (key->len != RSANUMWORDS) {
-        return 0;  /* Wrong key passed in. */
+  if (key->len != RSANUMWORDS) {
+    return 0;  // Wrong key passed in.
+  }
+
+  if (len != sizeof(buf)) {
+    return 0;  // Wrong input length.
+  }
+
+  for (i = 0; i < len; ++i) {  // Copy input to local workspace.
+    buf[i] = signature[i];
+  }
+
+  modpowF4(key, buf);  // In-place exponentiation.
+
+  // Xor sha portion, so it all becomes 00 iff equal.
+  for (i = len - SHA_DIGEST_SIZE; i < len; ++i) {
+    buf[i] ^= *sha++;
+  }
+
+  // Hash resulting buf, in-place.
+  SHA(buf, len, buf);
+
+  // Compare against expected hash value.
+  for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
+    if (buf[i] != kExpectedPadShaRsa2048[i]) {
+      return 0;
     }
+  }
 
-    if (len != sizeof(buf)) {
-        return 0;  /* Wrong input length. */
-    }
-
-    for (i = 0; i < len; ++i) {
-        buf[i] = signature[i];
-    }
-
-    modpow3(key, buf);
-
-    /* Check pkcs1.5 padding bytes. */
-    for (i = 0; i < (int) sizeof(padding); ++i) {
-        if (buf[i] != padding[i]) {
-            return 0;
-        }
-    }
-
-    /* Check sha digest matches. */
-    for (; i < len; ++i) {
-        if (buf[i] != *sha++) {
-            return 0;
-        }
-    }
-
-    return 1;
+  return 1;  // All checked out OK.
 }
diff --git a/libc/rsa.h b/libc/rsa.h
index ebe3881..e420ad7 100644
--- a/libc/rsa.h
+++ b/libc/rsa.h
@@ -1,30 +1,5 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the 
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
+// Copyright 2008 Google Inc. All Rights Reserved.
+// Author: mschilder@google.com (Marius Schilder)
 
 #ifndef _EMBEDDED_RSA_H_
 #define _EMBEDDED_RSA_H_
@@ -33,25 +8,27 @@
 
 #ifdef __cplusplus
 extern "C" {
-#endif
+#endif  // __cplusplus
 
-#define RSANUMBYTES 256           /* 2048 bit key length */
+#define RSANUMBYTES 256  // 2048 bit key length
 #define RSANUMWORDS (RSANUMBYTES / sizeof(uint32_t))
 
-typedef struct RSAPublicKey {
-    int len;                  /* Length of n[] in number of uint32_t */
-    uint32_t n0inv;           /* -1 / n[0] mod 2^32 */
-    uint32_t n[RSANUMWORDS];  /* modulus as little endian array */
-    uint32_t rr[RSANUMWORDS]; /* R^2 as little endian array */
-} RSAPublicKey;
+typedef struct RSAPublicKeyInstance {
+  int len;  // Length of n[] in number of uint32_t
+  uint32_t n0inv;  // -1 / n[0] mod 2^32
+  uint32_t n[RSANUMWORDS];  // modulus as little endian array
+  uint32_t rr[RSANUMWORDS];  // R^2 as little endian array
+} RSAPublicKeyInstance;
 
-int RSA_verify(RSAPublicKey *key,
+typedef const RSAPublicKeyInstance * const RSAPublicKey;
+
+int RSA_verify(RSAPublicKey mod,
                const uint8_t* signature,
                const int len,
                const uint8_t* sha);
 
 #ifdef __cplusplus
 }
-#endif
+#endif  // __cplusplus
 
-#endif
+#endif  // _EMBEDDED_RSA_H_
diff --git a/libc/sha.c b/libc/sha.c
index 69e1f58..6e09380 100644
--- a/libc/sha.c
+++ b/libc/sha.c
@@ -1,143 +1,123 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the 
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
+// Copyright 2008 Google Inc. All Rights Reserved.
+// Author: mschilder@google.com (Marius Schilder)
+//
+// Optimized for minimal code size.
 
 #include "sha.h"
 
 #define rol(bits, value) (((value) << (bits)) | ((value) >> (32 - (bits))))
 
-static void SHA1_transform(SHA_CTX *ctx) {
-    uint32_t W[80];
-    uint32_t A, B, C, D, E;
-    uint8_t *p = ctx->buf;
-    int t;
+static void SHA1_Transform(SHA_CTX* ctx) {
+  uint32_t W[80];
+  uint32_t A, B, C, D, E;
+  uint8_t* p = ctx->buf.b;
+  int t;
 
-    for(t = 0; t < 16; ++t) {
-        uint32_t tmp =  *p++ << 24;
-        tmp |= *p++ << 16;
-        tmp |= *p++ << 8;
-        tmp |= *p++;
-        W[t] = tmp;
-    }
+  for(t = 0; t < 16; ++t) {
+    uint32_t tmp =  *p++ << 24;
+    tmp |= *p++ << 16;
+    tmp |= *p++ << 8;
+    tmp |= *p++;
+    W[t] = tmp;
+  }
 
-    for(; t < 80; t++) {
-        W[t] = rol(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
-    }
+  for(; t < 80; t++) {
+    W[t] = rol(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
+  }
 
-    A = ctx->state[0];
-    B = ctx->state[1];
-    C = ctx->state[2];
-    D = ctx->state[3];
-    E = ctx->state[4];
+  A = ctx->state[0];
+  B = ctx->state[1];
+  C = ctx->state[2];
+  D = ctx->state[3];
+  E = ctx->state[4];
 
-    for(t = 0; t < 80; t++) {
-        uint32_t tmp = rol(5,A) + E + W[t];
+  for(t = 0; t < 80; t++) {
+    uint32_t tmp = rol(5,A) + E + W[t];
 
-        if (t < 20)
-            tmp += (D^(B&(C^D))) + 0x5A827999;
-        else if ( t < 40)
-            tmp += (B^C^D) + 0x6ED9EBA1;
-        else if ( t < 60)
-            tmp += ((B&C)|(D&(B|C))) + 0x8F1BBCDC;
-        else
-            tmp += (B^C^D) + 0xCA62C1D6;
+    if (t < 20)
+      tmp += (D^(B&(C^D))) + 0x5A827999;
+    else if ( t < 40)
+      tmp += (B^C^D) + 0x6ED9EBA1;
+    else if ( t < 60)
+      tmp += ((B&C)|(D&(B|C))) + 0x8F1BBCDC;
+    else
+      tmp += (B^C^D) + 0xCA62C1D6;
 
-        E = D;
-        D = C;
-        C = rol(30,B);
-        B = A;
-        A = tmp;
-    }
+    E = D;
+    D = C;
+    C = rol(30,B);
+    B = A;
+    A = tmp;
+  }
 
-    ctx->state[0] += A;
-    ctx->state[1] += B;
-    ctx->state[2] += C;
-    ctx->state[3] += D;
-    ctx->state[4] += E;
+  ctx->state[0] += A;
+  ctx->state[1] += B;
+  ctx->state[2] += C;
+  ctx->state[3] += D;
+  ctx->state[4] += E;
 }
 
-void SHA_init(SHA_CTX *ctx) {
-    ctx->state[0] = 0x67452301;
-    ctx->state[1] = 0xEFCDAB89;
-    ctx->state[2] = 0x98BADCFE;
-    ctx->state[3] = 0x10325476;
-    ctx->state[4] = 0xC3D2E1F0;
-    ctx->count = 0;
+void SHA_init(SHA_CTX* ctx) {
+  ctx->state[0] = 0x67452301;
+  ctx->state[1] = 0xEFCDAB89;
+  ctx->state[2] = 0x98BADCFE;
+  ctx->state[3] = 0x10325476;
+  ctx->state[4] = 0xC3D2E1F0;
+  ctx->count = 0;
 }
 
-void SHA_update(SHA_CTX *ctx, const void *data, int len) {
-    int i = ctx->count % sizeof(ctx->buf);
-    const uint8_t* p = (const uint8_t*)data;
 
-    ctx->count += len;
+void SHA_update(SHA_CTX* ctx, const void* data, int len) {
+  int i = ctx->count % sizeof(ctx->buf);
+  const uint8_t* p = (const uint8_t*)data;
 
-    while (len--) {
-        ctx->buf[i++] = *p++;
-        if (i == sizeof(ctx->buf)) {
-            SHA1_transform(ctx);
-            i = 0;
-        }
+  ctx->count += len;
+
+  while (len--) {
+    ctx->buf.b[i++] = *p++;
+    if (i == sizeof(ctx->buf)) {
+      SHA1_Transform(ctx);
+      i = 0;
     }
+  }
 }
-const uint8_t *SHA_final(SHA_CTX *ctx) {
-    uint8_t *p = ctx->buf;
-    uint64_t cnt = ctx->count * 8;
-    int i;
 
-    SHA_update(ctx, (uint8_t*)"\x80", 1);
-    while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8)) {
-        SHA_update(ctx, (uint8_t*)"\0", 1);
-    }
-    for (i = 0; i < 8; ++i) {
-        uint8_t tmp = cnt >> ((7 - i) * 8);
-        SHA_update(ctx, &tmp, 1);
-    }
 
-    for (i = 0; i < 5; i++) {
-        uint32_t tmp = ctx->state[i];
-        *p++ = tmp >> 24;
-        *p++ = tmp >> 16;
-        *p++ = tmp >> 8;
-        *p++ = tmp >> 0;
-    }
+const uint8_t* SHA_final(SHA_CTX* ctx) {
+  uint8_t *p = ctx->buf.b;
+  uint64_t cnt = ctx->count * 8;
+  int i;
 
-    return ctx->buf;
+  SHA_update(ctx, (uint8_t*)"\x80", 1);
+  while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8)) {
+    SHA_update(ctx, (uint8_t*)"\0", 1);
+  }
+  for (i = 0; i < 8; ++i) {
+    uint8_t tmp = cnt >> ((7 - i) * 8);
+    SHA_update(ctx, &tmp, 1);
+  }
+
+  for (i = 0; i < 5; i++) {
+    uint32_t tmp = ctx->state[i];
+    *p++ = tmp >> 24;
+    *p++ = tmp >> 16;
+    *p++ = tmp >> 8;
+    *p++ = tmp >> 0;
+  }
+
+  return ctx->buf.b;
 }
 
 /* Convenience function */
-const uint8_t* SHA(const void *data, int len, uint8_t *digest) {
-    const uint8_t *p;
-    int i;
-    SHA_CTX ctx;
-    SHA_init(&ctx);
-    SHA_update(&ctx, data, len);
-    p = SHA_final(&ctx);
-    for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
-        digest[i] = *p++;
-    }
-    return digest;
+const uint8_t* SHA(const void* data, int len, uint8_t* digest) {
+  const uint8_t* p;
+  int i;
+  SHA_CTX ctx;
+  SHA_init(&ctx);
+  SHA_update(&ctx, data, len);
+  p = SHA_final(&ctx);
+  for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
+    digest[i] = *p++;
+  }
+  return digest;
 }
diff --git a/libc/sha.h b/libc/sha.h
index 1761a7b..4f66e53 100644
--- a/libc/sha.h
+++ b/libc/sha.h
@@ -1,30 +1,5 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the 
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
+// Copyright 2008 Google Inc. All Rights Reserved.
+// Author: mschilder@google.com (Marius Schilder)
 
 #ifndef _EMBEDDED_SHA_H_
 #define _EMBEDDED_SHA_H_
@@ -33,25 +8,28 @@
 
 #ifdef __cplusplus
 extern "C" {
-#endif
+#endif  // __cplusplus
 
 typedef struct SHA_CTX {
-    uint64_t count;
-    uint8_t buf[64];
-    uint32_t state[5];
+  uint64_t count;
+  uint32_t state[5];
+  union {
+    uint8_t b[64];
+    uint32_t w[16];
+  } buf;
 } SHA_CTX;
 
-void SHA_init(SHA_CTX *ctx);
-void SHA_update(SHA_CTX *ctx, const void* data, int len);
-const uint8_t* SHA_final(SHA_CTX *ctx);
+void SHA_init(SHA_CTX* ctx);
+void SHA_update(SHA_CTX* ctx, const void* data, int len);
+const uint8_t* SHA_final(SHA_CTX* ctx);
 
-/* Convenience method. Returns digest parameter value. */
-const uint8_t* SHA(const void *data, int len, uint8_t *digest);
+// Convenience method. Returns digest parameter value.
+const uint8_t* SHA(const void* data, int len, uint8_t* digest);
 
 #define SHA_DIGEST_SIZE 20
 
 #ifdef __cplusplus
 }
-#endif
+#endif  // __cplusplus
 
-#endif
+#endif  // _EMBEDDED_SHA_H_