Format with .clang-format, included herein for reference
authorRonald G. Minnich <rminnich@gmail.com>
Thu, 4 Feb 2016 02:22:18 +0000 (18:22 -0800)
committerBarret Rhoden <brho@cs.berkeley.edu>
Thu, 4 Feb 2016 19:56:13 +0000 (14:56 -0500)
BasedOnStyle: LLVM
IndentWidth: 4
TabWidth: 4
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
AlignAfterOpenBracket: true

Signed-off-by: Ronald G. Minnich <rminnich@gmail.com>
Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/include/random/rijndael.h
kern/include/random/sha2.h
kern/lib/random/fortuna.c
kern/lib/random/rijndael.c
kern/lib/random/sha2.c

index a284de8..dfc45ef 100644 (file)
 
 typedef struct rijndaelCtx rijndaelCtx;
 
-struct rijndaelCtx
-{
-       uint32_t                k_len;
-       int                             decrypt;
-       uint32_t                e_key[64];
-       uint32_t                d_key[64];
+struct rijndaelCtx {
+       uint32_t k_len;
+       int decrypt;
+       uint32_t e_key[64];
+       uint32_t d_key[64];
 };
 
-
 /* Standard interface for AES cryptographic routines                           */
 
 /* These are all based on 32 bit unsigned values and will therefore */
 /* require endian conversions for big-endian architectures                     */
 
-rijndaelCtx *
-                       rijndael_set_key(rijndaelCtx *, const uint32_t *, const uint32_t, int);
-void           rijndael_encrypt(rijndaelCtx *, const uint32_t *, uint32_t *);
-void           rijndael_decrypt(rijndaelCtx *, const uint32_t *, uint32_t *);
+rijndaelCtx *rijndael_set_key(rijndaelCtx *, const uint32_t *, const uint32_t,
+                                                         int);
+void rijndael_encrypt(rijndaelCtx *, const uint32_t *, uint32_t *);
+void rijndael_decrypt(rijndaelCtx *, const uint32_t *, uint32_t *);
 
 /* conventional interface */
 
-void           aes_set_key(rijndaelCtx *ctx, const uint8_t *key, unsigned keybits, int enc);
-void           aes_ecb_encrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len);
-void           aes_ecb_decrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len);
-void           aes_cbc_encrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data, unsigned len);
-void           aes_cbc_decrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data, unsigned len);
+void aes_set_key(rijndaelCtx *ctx, const uint8_t *key, unsigned keybits,
+                                int enc);
+void aes_ecb_encrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len);
+void aes_ecb_decrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len);
+void aes_cbc_encrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data,
+                                        unsigned len);
+void aes_cbc_decrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data,
+                                        unsigned len);
index e6ff365..3fa9ffa 100644 (file)
  * $From: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
  */
 
-
 /*** SHA-224/256/384/512 Various Length Definitions ***********************/
-enum{
-       SHA224BlockLength               = 64,
-       SHA224DigestLength      = 28,
+enum {
+       SHA224BlockLength = 64,
+       SHA224DigestLength = 28,
        SHA224_digest_string_length = (SHA224DigestLength * 2 + 1),
-       SHA256BlockLength               = 64,
-       SHA256DigestLength      = 32,
+       SHA256BlockLength = 64,
+       SHA256DigestLength = 32,
        SHA256DigestStringLength = (SHA256DigestLength * 2 + 1),
-       SHA384BlockLength               = 128,
-       SHA384DigestLength      = 48,
+       SHA384BlockLength = 128,
+       SHA384DigestLength = 48,
        SHA384_digest_string_length = (SHA384DigestLength * 2 + 1),
-       SHA512_block_length             = 128,
-       SHA512DigestLength      = 64,
+       SHA512_block_length = 128,
+       SHA512DigestLength = 64,
        SHA512DigestStringLength = (SHA512DigestLength * 2 + 1)
 };
 
@@ -59,36 +58,30 @@ typedef struct SHA512Ctx SHA512Ctx;
 typedef SHA256Ctx SHA224Ctx;
 typedef SHA512Ctx SHA384Ctx;
 
-struct SHA256Ctx
-{
-       uint32_t                state[8];
-       uint64_t                bitcount;
-       uint8_t         buffer[SHA256BlockLength];
+struct SHA256Ctx {
+       uint32_t state[8];
+       uint64_t bitcount;
+       uint8_t buffer[SHA256BlockLength];
 };
 
-
-
-struct SHA512Ctx
-{
-       uint64_t                state[8];
-       uint64_t                bitcount[2];
-       uint8_t         buffer[SHA512_block_length];
+struct SHA512Ctx {
+       uint64_t state[8];
+       uint64_t bitcount[2];
+       uint8_t buffer[SHA512_block_length];
 };
 
+void SHA224_Init(SHA224Ctx *);
+void SHA224_Update(SHA224Ctx *, const uint8_t *, size_t);
+void SHA224_Final(uint8_t[SHA224DigestLength], SHA224Ctx *);
 
-void           SHA224_Init(SHA224Ctx *);
-void           SHA224_Update(SHA224Ctx *, const uint8_t *, size_t);
-void           SHA224_Final(uint8_t[SHA224DigestLength], SHA224Ctx *);
-
-void           SHA256_Init(SHA256Ctx *);
-void           SHA256_Update(SHA256Ctx *, const uint8_t *, size_t);
-void           SHA256_Final(uint8_t[SHA256DigestLength], SHA256Ctx *);
-
-void           SHA384_Init(SHA384Ctx *);
-void           SHA384_Update(SHA384Ctx *, const uint8_t *, size_t);
-void           SHA384_Final(uint8_t[SHA384DigestLength], SHA384Ctx *);
+void SHA256_Init(SHA256Ctx *);
+void SHA256_Update(SHA256Ctx *, const uint8_t *, size_t);
+void SHA256_Final(uint8_t[SHA256DigestLength], SHA256Ctx *);
 
-void           SHA512_Init(SHA512Ctx *);
-void           SHA512_Update(SHA512Ctx *, const uint8_t *, size_t);
-void           SHA512_Final(uint8_t[SHA512DigestLength], SHA512Ctx *);
+void SHA384_Init(SHA384Ctx *);
+void SHA384_Update(SHA384Ctx *, const uint8_t *, size_t);
+void SHA384_Final(uint8_t[SHA384DigestLength], SHA384Ctx *);
 
+void SHA512_Init(SHA512Ctx *);
+void SHA512_Update(SHA512Ctx *, const uint8_t *, size_t);
+void SHA512_Final(uint8_t[SHA512DigestLength], SHA512Ctx *);
index 71334bd..ab54c99 100644 (file)
@@ -29,7 +29,6 @@
  * contrib/pgcrypto/fortuna.c
  */
 
-
 #include <u.h>
 #include <rijndael.h>
 #include <sha2.h>
@@ -39,9 +38,6 @@
 #include "dat.h"
 #include "fns.h"
 
-
-
-
 /*
  * Why Fortuna-like: There does not seem to be any definitive reference
  * on Fortuna in the net.  Instead this implementation is based on
@@ -70,7 +66,6 @@
  * results, but continuing with old state.
  */
 
-
 /*
  * Algorithm parameters
  */
  * In our case the minimal cycle time would be bit longer
  * than the system-randomness feeding frequency.
  */
-enum{
-       numPools        = 23,
+enum {
+       numPools = 23,
 
        /* in microseconds */
-       reseedInterval = 100000,        /* 0.1 sec */
+       reseedInterval = 100000, /* 0.1 sec */
 
        /* for one big request, reseed after this many bytes */
-       reseedBytes     = (1024*1024),
+       reseedBytes = (1024 * 1024),
 
        /*
        * Skip reseed if pool 0 has less than this many
        * bytes added since last reseed.
        */
-       pool0Fill               = (256/8),
+       pool0Fill = (256 / 8),
 
-/*
-* Algorithm constants
-*/
+       /*
+       * Algorithm constants
+       */
 
        /* Both cipher key size and hash result size */
-       block                   = 32,
+       block = 32,
 
        /* cipher block size */
-       ciphBlock               = 16
+       ciphBlock = 16
 };
 
-
-
-
 /* for internal wrappers */
 typedef SHA256Ctx mdCtx;
 typedef rijndaelCtx ciphCtx;
 
-struct FState
-{
-       uint8_t         counter[ciphBlock];
-       uint8_t         result[ciphBlock];
-       uint8_t         key[block];
-       mdCtx           pool[numPools];
-       ciphCtx         ciph;
-       unsigned        reseedCount;
-       int32_t         lastReseedTime;
-       unsigned        pool0Bytes;
-       unsigned        rndPos;
-       int                     tricksDone;
+struct FState {
+       uint8_t counter[ciphBlock];
+       uint8_t result[ciphBlock];
+       uint8_t key[block];
+       mdCtx pool[numPools];
+       ciphCtx ciph;
+       unsigned reseedCount;
+       int32_t lastReseedTime;
+       unsigned pool0Bytes;
+       unsigned rndPos;
+       int tricksDone;
 };
 typedef struct FState FState;
 
-
 /*
  * Use our own wrappers here.
  * - Need to get intermediate result from digest, without affecting it.
@@ -146,34 +136,26 @@ typedef struct FState FState;
  * - No memory allocations.
  */
 
-static void
-ciph_init(ciphCtx * ctx, const uint8_t *key, int klen)
+static void ciph_init(ciphCtx *ctx, const uint8_t *key, int klen)
 {
-       rijndael_set_key(ctx, (const uint32_t *) key, klen, 1);
+       rijndael_set_key(ctx, (const uint32_t *)key, klen, 1);
 }
 
-static void
-ciph_encrypt(ciphCtx * ctx, const uint8_t *in, uint8_t *out)
+static void ciph_encrypt(ciphCtx *ctx, const uint8_t *in, uint8_t *out)
 {
-       rijndael_encrypt(ctx, (const uint32_t *) in, (uint32_t *) out);
+       rijndael_encrypt(ctx, (const uint32_t *)in, (uint32_t *)out);
 }
 
-static void
-md_init(mdCtx * ctx)
-{
-       SHA256_Init(ctx);
-}
+static void md_init(mdCtx *ctx) { SHA256_Init(ctx); }
 
-static void
-md_update(mdCtx * ctx, const uint8_t *data, int len)
+static void md_update(mdCtx *ctx, const uint8_t *data, int len)
 {
        SHA256_Update(ctx, data, len);
 }
 
-static void
-md_result(mdCtx * ctx, uint8_t *dst)
+static void md_result(mdCtx *ctx, uint8_t *dst)
 {
-       SHA256Ctx       tmp;
+       SHA256Ctx tmp;
 
        memmove(&tmp, ctx, sizeof(*ctx));
        SHA256_Final(dst, &tmp);
@@ -183,10 +165,9 @@ md_result(mdCtx * ctx, uint8_t *dst)
 /*
  * initialize state
  */
-static void
-init_state(FState *st)
+static void init_state(FState *st)
 {
-       int                     i;
+       int i;
 
        memset(st, 0, sizeof(*st));
        for (i = 0; i < numPools; i++)
@@ -197,10 +178,9 @@ init_state(FState *st)
  * Endianess does not matter.
  * It just needs to change without repeating.
  */
-static void
-inc_counter(FState *st)
+static void inc_counter(FState *st)
 {
-       uint32_t           *val = (uint32_t *) st->counter;
+       uint32_t *val = (uint32_t *)st->counter;
 
        if (++val[0])
                return;
@@ -214,50 +194,45 @@ inc_counter(FState *st)
 /*
  * This is called 'cipher in counter mode'.
  */
-static void
-encrypt_counter(FState *st, uint8_t *dst)
+static void encrypt_counter(FState *st, uint8_t *dst)
 {
        ciph_encrypt(&st->ciph, st->counter, dst);
        inc_counter(st);
 }
 
-
 /*
  * The time between reseed must be at least reseedInterval
  * microseconds.
  */
-static int
-enough_time_passed(FState *st)
+static int enough_time_passed(FState *st)
 {
-               int                     ok;
-        int32_t now;
-        int32_t last = st->lastReseedTime;
-
-        now = seconds();
+       int ok;
+       int32_t now;
+       int32_t last = st->lastReseedTime;
 
-        /* check how much time has passed */
-        ok = 0;
-        if (now - last >= reseedInterval)
-                ok = 1;
+       now = seconds();
 
-        /* reseed will happen, update lastReseedTime */
-        if (ok)
-                st->lastReseedTime=now;
+       /* check how much time has passed */
+       ok = 0;
+       if (now - last >= reseedInterval)
+               ok = 1;
 
-        return ok;
+       /* reseed will happen, update lastReseedTime */
+       if (ok)
+               st->lastReseedTime = now;
 
+       return ok;
 }
 
 /*
  * generate new key from all the pools
  */
-static void
-reseed(FState *st)
+static void reseed(FState *st)
 {
-       unsigned        k;
-       unsigned        n;
-       mdCtx           key_md;
-       uint8_t         buf[block];
+       unsigned k;
+       unsigned n;
+       mdCtx key_md;
+       uint8_t buf[block];
 
        /* set pool as empty */
        st->pool0Bytes = 0;
@@ -271,8 +246,7 @@ reseed(FState *st)
         * The goal: use k-th pool only 1/(2^k) of the time.
         */
        md_init(&key_md);
-       for (k = 0; k < numPools; k++)
-       {
+       for (k = 0; k < numPools; k++) {
                md_result(&st->pool[k], buf);
                md_update(&key_md, buf, block);
 
@@ -297,10 +271,9 @@ reseed(FState *st)
 /*
  * Pick a random pool. This uses key bytes as random source.
  */
-static unsigned
-get_rand_pool(FState *st)
+static unsigned get_rand_pool(FState *st)
 {
-       unsigned        rnd;
+       unsigned rnd;
 
        /*
         * This slightly prefers lower pools - thats OK.
@@ -317,12 +290,11 @@ get_rand_pool(FState *st)
 /*
  * update pools
  */
-static void
-add_entropy(FState *st, const uint8_t *data, unsigned len)
+static void add_entropy(FState *st, const uint8_t *data, unsigned len)
 {
-       unsigned        pos;
-       uint8_t         hash[block];
-       mdCtx           md;
+       unsigned pos;
+       uint8_t hash[block];
+       mdCtx md;
 
        /* hash given data */
        md_init(&md);
@@ -348,8 +320,7 @@ add_entropy(FState *st, const uint8_t *data, unsigned len)
 /*
  * Just take 2 next blocks as new key
  */
-static void
-rekey(FState *st)
+static void rekey(FState *st)
 {
        encrypt_counter(st, st->key);
        encrypt_counter(st, st->key + ciphBlock);
@@ -362,18 +333,16 @@ rekey(FState *st)
  * This can also be viewed as spreading the startup
  * entropy over all of the components.
  */
-static void
-startup_tricks(FState *st)
+static void startup_tricks(FState *st)
 {
-       int                     i;
-       uint8_t         buf[block];
+       int i;
+       uint8_t buf[block];
 
        /* Use next block as counter. */
        encrypt_counter(st, st->counter);
 
        /* Now shuffle pools, excluding #0 */
-       for (i = 1; i < numPools; i++)
-       {
+       for (i = 1; i < numPools; i++) {
                encrypt_counter(st, buf);
                encrypt_counter(st, buf + ciphBlock);
                md_update(&st->pool[i], buf, block);
@@ -387,11 +356,10 @@ startup_tricks(FState *st)
        st->tricksDone = 1;
 }
 
-static void
-extract_data(FState *st, unsigned count, uint8_t *dst)
+static void extract_data(FState *st, unsigned count, uint8_t *dst)
 {
-       unsigned        n;
-       unsigned        block_nr = 0;
+       unsigned n;
+       unsigned block_nr = 0;
 
        /* Should we reseed? */
        if (st->pool0Bytes >= pool0Fill || st->reseedCount == 0)
@@ -402,8 +370,7 @@ extract_data(FState *st, unsigned count, uint8_t *dst)
        if (!st->tricksDone)
                startup_tricks(st);
 
-       while (count > 0)
-       {
+       while (count > 0) {
                /* produce bytes */
                encrypt_counter(st, st->result);
 
@@ -418,8 +385,7 @@ extract_data(FState *st, unsigned count, uint8_t *dst)
 
                /* must not give out too many bytes with one key */
                block_nr++;
-               if (block_nr > (reseedBytes / ciphBlock))
-               {
+               if (block_nr > (reseedBytes / ciphBlock)) {
                        rekey(st);
                        block_nr = 0;
                }
@@ -429,9 +395,10 @@ extract_data(FState *st, unsigned count, uint8_t *dst)
 }
 
 static FState mainState;
-static int     initDone = 0;
+static int initDone;
 
-static void init(){
+static void init()
+{
        init_state(&mainState);
        initDone = 1;
 }
@@ -440,27 +407,21 @@ static void init(){
  * public interface
  */
 
-
-
-void
-fortuna_add_entropy(const uint8_t *data, unsigned len)
+void fortuna_add_entropy(const uint8_t *data, unsigned len)
 {
        if (!initDone)
-       {
                init();
-       }
+
        if (!data || !len)
                return;
        add_entropy(&mainState, data, len);
 }
 
-void
-fortuna_get_bytes(unsigned len, uint8_t *dst)
+void fortuna_get_bytes(unsigned len, uint8_t *dst)
 {
        if (!initDone)
-       {
                init();
-       }
+
        if (!dst || !len)
                return;
        extract_data(&mainState, len, dst);
index 0cc8218..f65261d 100644 (file)
@@ -50,109 +50,121 @@ Mean:                500 cycles =    51.2 mbits/sec
 
 /* Circular rotate of 32 bit values                                                                    */
 
-#define rotr(x,n)      (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
-#define rotl(x,n)      (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))
+#define rotr(x, n) (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
+#define rotl(x, n) (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))
 
 /* Invert byte order in a 32 bit variable                                                      */
 
-#define bswap(x)       ((rotl((x), 8) & 0x00ff00ff) | (rotr((x), 8) & 0xff00ff00))
+#define bswap(x) ((rotl((x), 8) & 0x00ff00ff) | (rotr((x), 8) & 0xff00ff00))
 
 /* Extract byte from a 32 bit quantity (little endian notation)                */
 
-#define byte(x,n)      ((uint8_t)((x) >> (8 * (n))))
-
-#define io_swap(x)     (x)
-
-#define ff_mult(a,b)   ((a) && (b) ? pow_tab[(log_tab[a] + log_tab[b]) % 255] : 0)
-
-#define f_rn(bo, bi, n, k)                                                             \
-       (bo)[n] =  ft_tab[0][byte((bi)[n],0)] ^                         \
-                        ft_tab[1][byte((bi)[((n) + 1) & 3],1)] ^       \
-                        ft_tab[2][byte((bi)[((n) + 2) & 3],2)] ^       \
-                        ft_tab[3][byte((bi)[((n) + 3) & 3],3)] ^ *((k) + (n))
-
-#define i_rn(bo, bi, n, k)                                                     \
-       (bo)[n] =  it_tab[0][byte((bi)[n],0)] ^                         \
-                        it_tab[1][byte((bi)[((n) + 3) & 3],1)] ^       \
-                        it_tab[2][byte((bi)[((n) + 2) & 3],2)] ^       \
-                        it_tab[3][byte((bi)[((n) + 1) & 3],3)] ^ *((k) + (n))
-
-#define ls_box(x)                               \
-       ( fl_tab[0][byte(x, 0)] ^        \
-         fl_tab[1][byte(x, 1)] ^        \
-         fl_tab[2][byte(x, 2)] ^        \
-         fl_tab[3][byte(x, 3)] )
-
-#define f_rl(bo, bi, n, k)                                                             \
-       (bo)[n] =  fl_tab[0][byte((bi)[n],0)] ^                         \
-                        fl_tab[1][byte((bi)[((n) + 1) & 3],1)] ^       \
-                        fl_tab[2][byte((bi)[((n) + 2) & 3],2)] ^       \
-                        fl_tab[3][byte((bi)[((n) + 3) & 3],3)] ^ *((k) + (n))
-
-#define i_rl(bo, bi, n, k)                                                             \
-       (bo)[n] =  il_tab[0][byte((bi)[n],0)] ^                         \
-                        il_tab[1][byte((bi)[((n) + 3) & 3],1)] ^       \
-                        il_tab[2][byte((bi)[((n) + 2) & 3],2)] ^       \
-                        il_tab[3][byte((bi)[((n) + 1) & 3],3)] ^ *((k) + (n))
-
-#define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
-
-#define imix_col(y,x)          \
-do { \
-       u       = star_x(x);            \
-       v       = star_x(u);            \
-       w       = star_x(v);            \
-       t       = w ^ (x);                      \
-   (y) = u ^ v ^ w;            \
-   (y) ^= rotr(u ^ t,  8) ^ \
-                 rotr(v ^ t, 16) ^ \
-                 rotr(t,24);           \
-} while (0)
+#define byte(x, n) ((uint8_t)((x) >> (8 * (n))))
+
+#define io_swap(x) (x)
+
+#define ff_mult(a, b)                                                          \
+       ((a) && (b) ? pow_tab[(log_tab[a] + log_tab[b]) % 255] : 0)
+
+#define f_rn(bo, bi, n, k)                                                     \
+       (bo)[n] = ft_tab[0][byte((bi)[n], 0)] ^                                    \
+                         ft_tab[1][byte((bi)[((n) + 1) & 3], 1)] ^                        \
+                         ft_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
+                         ft_tab[3][byte((bi)[((n) + 3) & 3], 3)] ^ *((k) + (n))
+
+#define i_rn(bo, bi, n, k)                                                     \
+       (bo)[n] = it_tab[0][byte((bi)[n], 0)] ^                                    \
+                         it_tab[1][byte((bi)[((n) + 3) & 3], 1)] ^                        \
+                         it_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
+                         it_tab[3][byte((bi)[((n) + 1) & 3], 3)] ^ *((k) + (n))
+
+#define ls_box(x)                                                              \
+       (fl_tab[0][byte(x, 0)] ^ fl_tab[1][byte(x, 1)] ^ fl_tab[2][byte(x, 2)] ^   \
+        fl_tab[3][byte(x, 3)])
+
+#define f_rl(bo, bi, n, k)                                                     \
+       (bo)[n] = fl_tab[0][byte((bi)[n], 0)] ^                                    \
+                         fl_tab[1][byte((bi)[((n) + 1) & 3], 1)] ^                        \
+                         fl_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
+                         fl_tab[3][byte((bi)[((n) + 3) & 3], 3)] ^ *((k) + (n))
+
+#define i_rl(bo, bi, n, k)                                                     \
+       (bo)[n] = il_tab[0][byte((bi)[n], 0)] ^                                    \
+                         il_tab[1][byte((bi)[((n) + 3) & 3], 1)] ^                        \
+                         il_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
+                         il_tab[3][byte((bi)[((n) + 1) & 3], 3)] ^ *((k) + (n))
+
+#define star_x(x) (((x)&0x7f7f7f7f) << 1) ^ ((((x)&0x80808080) >> 7) * 0x1b)
+
+#define imix_col(y, x)                                                         \
+       do {                                                                       \
+               u = star_x(x);                                                         \
+               v = star_x(u);                                                         \
+               w = star_x(v);                                                         \
+               t = w ^ (x);                                                           \
+               (y) = u ^ v ^ w;                                                       \
+               (y) ^= rotr(u ^ t, 8) ^ rotr(v ^ t, 16) ^ rotr(t, 24);                 \
+       } while (0)
 
 /* initialise the key schedule from the user supplied key      */
 
-#define loop4(i)                                                                       \
-do {   t = ls_box(rotr(t,  8)) ^ rco_tab[i];              \
-       t ^= e_key[4 * i];         e_key[4 * i + 4] = t;        \
-       t ^= e_key[4 * i + 1]; e_key[4 * i + 5] = t;    \
-       t ^= e_key[4 * i + 2]; e_key[4 * i + 6] = t;    \
-       t ^= e_key[4 * i + 3]; e_key[4 * i + 7] = t;    \
-} while (0)
-
-#define loop6(i)                                                                       \
-do {   t = ls_box(rotr(t,  8)) ^ rco_tab[i];              \
-       t ^= e_key[6 * (i)];       e_key[6 * (i) + 6] = t;      \
-       t ^= e_key[6 * (i) + 1]; e_key[6 * (i) + 7] = t;        \
-       t ^= e_key[6 * (i) + 2]; e_key[6 * (i) + 8] = t;        \
-       t ^= e_key[6 * (i) + 3]; e_key[6 * (i) + 9] = t;        \
-       t ^= e_key[6 * (i) + 4]; e_key[6 * (i) + 10] = t;       \
-       t ^= e_key[6 * (i) + 5]; e_key[6 * (i) + 11] = t;       \
-} while (0)
-
-#define loop8(i)                                                                       \
-do {   t = ls_box(rotr(t,  8)) ^ rco_tab[i];              \
-       t ^= e_key[8 * (i)];     e_key[8 * (i) + 8] = t;        \
-       t ^= e_key[8 * (i) + 1]; e_key[8 * (i) + 9] = t;        \
-       t ^= e_key[8 * (i) + 2]; e_key[8 * (i) + 10] = t;       \
-       t ^= e_key[8 * (i) + 3]; e_key[8 * (i) + 11] = t;       \
-       t  = e_key[8 * (i) + 4] ^ ls_box(t);                            \
-       e_key[8 * (i) + 12] = t;                                                        \
-       t ^= e_key[8 * (i) + 5]; e_key[8 * (i) + 13] = t;       \
-       t ^= e_key[8 * (i) + 6]; e_key[8 * (i) + 14] = t;       \
-       t ^= e_key[8 * (i) + 7]; e_key[8 * (i) + 15] = t;       \
-} while (0)
-
-rijndaelCtx *
-rijndael_set_key(rijndaelCtx *ctx, const uint32_t *in_key, const uint32_t key_len,
-                                int encrypt)
+#define loop4(i)                                                               \
+       do {                                                                       \
+               t = ls_box(rotr(t, 8)) ^ rco_tab[i];                                   \
+               t ^= e_key[4 * i];                                                     \
+               e_key[4 * i + 4] = t;                                                  \
+               t ^= e_key[4 * i + 1];                                                 \
+               e_key[4 * i + 5] = t;                                                  \
+               t ^= e_key[4 * i + 2];                                                 \
+               e_key[4 * i + 6] = t;                                                  \
+               t ^= e_key[4 * i + 3];                                                 \
+               e_key[4 * i + 7] = t;                                                  \
+       } while (0)
+
+#define loop6(i)                                                               \
+       do {                                                                       \
+               t = ls_box(rotr(t, 8)) ^ rco_tab[i];                                   \
+               t ^= e_key[6 * (i)];                                                   \
+               e_key[6 * (i) + 6] = t;                                                \
+               t ^= e_key[6 * (i) + 1];                                               \
+               e_key[6 * (i) + 7] = t;                                                \
+               t ^= e_key[6 * (i) + 2];                                               \
+               e_key[6 * (i) + 8] = t;                                                \
+               t ^= e_key[6 * (i) + 3];                                               \
+               e_key[6 * (i) + 9] = t;                                                \
+               t ^= e_key[6 * (i) + 4];                                               \
+               e_key[6 * (i) + 10] = t;                                               \
+               t ^= e_key[6 * (i) + 5];                                               \
+               e_key[6 * (i) + 11] = t;                                               \
+       } while (0)
+
+#define loop8(i)                                                               \
+       do {                                                                       \
+               t = ls_box(rotr(t, 8)) ^ rco_tab[i];                                   \
+               t ^= e_key[8 * (i)];                                                   \
+               e_key[8 * (i) + 8] = t;                                                \
+               t ^= e_key[8 * (i) + 1];                                               \
+               e_key[8 * (i) + 9] = t;                                                \
+               t ^= e_key[8 * (i) + 2];                                               \
+               e_key[8 * (i) + 10] = t;                                               \
+               t ^= e_key[8 * (i) + 3];                                               \
+               e_key[8 * (i) + 11] = t;                                               \
+               t = e_key[8 * (i) + 4] ^ ls_box(t);                                    \
+               e_key[8 * (i) + 12] = t;                                               \
+               t ^= e_key[8 * (i) + 5];                                               \
+               e_key[8 * (i) + 13] = t;                                               \
+               t ^= e_key[8 * (i) + 6];                                               \
+               e_key[8 * (i) + 14] = t;                                               \
+               t ^= e_key[8 * (i) + 7];                                               \
+               e_key[8 * (i) + 15] = t;                                               \
+       } while (0)
+
+rijndaelCtx *rijndael_set_key(rijndaelCtx *ctx, const uint32_t *in_key,
+                                                         const uint32_t key_len, int encrypt)
 {
-       uint32_t                i,
-                               t,
-                               u,
-                               v,
-                               w;
-       uint32_t           *e_key = ctx->e_key;
-       uint32_t           *d_key = ctx->d_key;
+       uint32_t i, t, u, v, w;
+       uint32_t *e_key = ctx->e_key;
+       uint32_t *d_key = ctx->d_key;
 
        ctx->decrypt = !encrypt;
 
@@ -163,33 +175,31 @@ rijndael_set_key(rijndaelCtx *ctx, const uint32_t *in_key, const uint32_t key_le
        e_key[2] = io_swap(in_key[2]);
        e_key[3] = io_swap(in_key[3]);
 
-       switch (ctx->k_len)
-       {
-               case 4:
-                       t = e_key[3];
-                       for (i = 0; i < 10; ++i)
-                               loop4(i);
-                       break;
-
-               case 6:
-                       e_key[4] = io_swap(in_key[4]);
-                       t = e_key[5] = io_swap(in_key[5]);
-                       for (i = 0; i < 8; ++i)
-                               loop6(i);
-                       break;
-
-               case 8:
-                       e_key[4] = io_swap(in_key[4]);
-                       e_key[5] = io_swap(in_key[5]);
-                       e_key[6] = io_swap(in_key[6]);
-                       t = e_key[7] = io_swap(in_key[7]);
-                       for (i = 0; i < 7; ++i)
-                               loop8(i);
-                       break;
+       switch (ctx->k_len) {
+       case 4:
+               t = e_key[3];
+               for (i = 0; i < 10; ++i)
+                       loop4(i);
+               break;
+
+       case 6:
+               e_key[4] = io_swap(in_key[4]);
+               t = e_key[5] = io_swap(in_key[5]);
+               for (i = 0; i < 8; ++i)
+                       loop6(i);
+               break;
+
+       case 8:
+               e_key[4] = io_swap(in_key[4]);
+               e_key[5] = io_swap(in_key[5]);
+               e_key[6] = io_swap(in_key[6]);
+               t = e_key[7] = io_swap(in_key[7]);
+               for (i = 0; i < 7; ++i)
+                       loop8(i);
+               break;
        }
 
-       if (!encrypt)
-       {
+       if (!encrypt) {
                d_key[0] = e_key[0];
                d_key[1] = e_key[1];
                d_key[2] = e_key[2];
@@ -204,31 +214,29 @@ rijndael_set_key(rijndaelCtx *ctx, const uint32_t *in_key, const uint32_t key_le
 
 /* encrypt a block of text     */
 
-#define f_nround(bo, bi, k) \
-do { \
-       f_rn(bo, bi, 0, k);             \
-       f_rn(bo, bi, 1, k);             \
-       f_rn(bo, bi, 2, k);             \
-       f_rn(bo, bi, 3, k);             \
-       k += 4;                                 \
-} while (0)
-
-#define f_lround(bo, bi, k) \
-do { \
-       f_rl(bo, bi, 0, k);             \
-       f_rl(bo, bi, 1, k);             \
-       f_rl(bo, bi, 2, k);             \
-       f_rl(bo, bi, 3, k);             \
-} while (0)
-
-void
-rijndael_encrypt(rijndaelCtx *ctx, const uint32_t *in_blk, uint32_t *out_blk)
+#define f_nround(bo, bi, k)                                                    \
+       do {                                                                       \
+               f_rn(bo, bi, 0, k);                                                    \
+               f_rn(bo, bi, 1, k);                                                    \
+               f_rn(bo, bi, 2, k);                                                    \
+               f_rn(bo, bi, 3, k);                                                    \
+               k += 4;                                                                \
+       } while (0)
+
+#define f_lround(bo, bi, k)                                                    \
+       do {                                                                       \
+               f_rl(bo, bi, 0, k);                                                    \
+               f_rl(bo, bi, 1, k);                                                    \
+               f_rl(bo, bi, 2, k);                                                    \
+               f_rl(bo, bi, 3, k);                                                    \
+       } while (0)
+
+void rijndael_encrypt(rijndaelCtx *ctx, const uint32_t *in_blk,
+                                         uint32_t *out_blk)
 {
-       uint32_t                k_len = ctx->k_len;
-       uint32_t           *e_key = ctx->e_key;
-       uint32_t                b0[4],
-                               b1[4],
-                          *kp;
+       uint32_t k_len = ctx->k_len;
+       uint32_t *e_key = ctx->e_key;
+       uint32_t b0[4], b1[4], *kp;
 
        b0[0] = io_swap(in_blk[0]) ^ e_key[0];
        b0[1] = io_swap(in_blk[1]) ^ e_key[1];
@@ -237,14 +245,12 @@ rijndael_encrypt(rijndaelCtx *ctx, const uint32_t *in_blk, uint32_t *out_blk)
 
        kp = e_key + 4;
 
-       if (k_len > 6)
-       {
+       if (k_len > 6) {
                f_nround(b1, b0, kp);
                f_nround(b0, b1, kp);
        }
 
-       if (k_len > 4)
-       {
+       if (k_len > 4) {
                f_nround(b1, b0, kp);
                f_nround(b0, b1, kp);
        }
@@ -268,32 +274,30 @@ rijndael_encrypt(rijndaelCtx *ctx, const uint32_t *in_blk, uint32_t *out_blk)
 
 /* decrypt a block of text     */
 
-#define i_nround(bo, bi, k) \
-do { \
-       i_rn(bo, bi, 0, k);             \
-       i_rn(bo, bi, 1, k);             \
-       i_rn(bo, bi, 2, k);             \
-       i_rn(bo, bi, 3, k);             \
-       k -= 4;                                 \
-} while (0)
-
-#define i_lround(bo, bi, k) \
-do { \
-       i_rl(bo, bi, 0, k);             \
-       i_rl(bo, bi, 1, k);             \
-       i_rl(bo, bi, 2, k);             \
-       i_rl(bo, bi, 3, k);             \
-} while (0)
-
-void
-rijndael_decrypt(rijndaelCtx *ctx, const uint32_t *in_blk, uint32_t *out_blk)
+#define i_nround(bo, bi, k)                                                    \
+       do {                                                                       \
+               i_rn(bo, bi, 0, k);                                                    \
+               i_rn(bo, bi, 1, k);                                                    \
+               i_rn(bo, bi, 2, k);                                                    \
+               i_rn(bo, bi, 3, k);                                                    \
+               k -= 4;                                                                \
+       } while (0)
+
+#define i_lround(bo, bi, k)                                                    \
+       do {                                                                       \
+               i_rl(bo, bi, 0, k);                                                    \
+               i_rl(bo, bi, 1, k);                                                    \
+               i_rl(bo, bi, 2, k);                                                    \
+               i_rl(bo, bi, 3, k);                                                    \
+       } while (0)
+
+void rijndael_decrypt(rijndaelCtx *ctx, const uint32_t *in_blk,
+                                         uint32_t *out_blk)
 {
-       uint32_t                b0[4],
-                               b1[4],
-                          *kp;
-       uint32_t                k_len = ctx->k_len;
-       uint32_t           *e_key = ctx->e_key;
-       uint32_t           *d_key = ctx->d_key;
+       uint32_t b0[4], b1[4], *kp;
+       uint32_t k_len = ctx->k_len;
+       uint32_t *e_key = ctx->e_key;
+       uint32_t *d_key = ctx->d_key;
 
        b0[0] = io_swap(in_blk[0]) ^ e_key[4 * k_len + 24];
        b0[1] = io_swap(in_blk[1]) ^ e_key[4 * k_len + 25];
@@ -302,14 +306,12 @@ rijndael_decrypt(rijndaelCtx *ctx, const uint32_t *in_blk, uint32_t *out_blk)
 
        kp = d_key + 4 * (k_len + 5);
 
-       if (k_len > 6)
-       {
+       if (k_len > 6) {
                i_nround(b1, b0, kp);
                i_nround(b0, b1, kp);
        }
 
-       if (k_len > 4)
-       {
+       if (k_len > 4) {
                i_nround(b1, b0, kp);
                i_nround(b0, b1, kp);
        }
@@ -338,24 +340,22 @@ rijndael_decrypt(rijndaelCtx *ctx, const uint32_t *in_blk, uint32_t *out_blk)
  * should be true for PX.  -marko
  */
 
-void
-aes_set_key(rijndaelCtx *ctx, const uint8_t *key, unsigned keybits, int enc)
+void aes_set_key(rijndaelCtx *ctx, const uint8_t *key, unsigned keybits,
+                                int enc)
 {
-       uint32_t           *k;
+       uint32_t *k;
 
-       k = (uint32_t *) key;
+       k = (uint32_t *)key;
        rijndael_set_key(ctx, k, keybits, enc);
 }
 
-void
-aes_ecb_encrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len)
+void aes_ecb_encrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len)
 {
-       unsigned        bs = 16;
-       uint32_t           *d;
+       unsigned bs = 16;
+       uint32_t *d;
 
-       while (len >= bs)
-       {
-               d = (uint32_t *) data;
+       while (len >= bs) {
+               d = (uint32_t *)data;
                rijndael_encrypt(ctx, d, d);
 
                len -= bs;
@@ -363,15 +363,13 @@ aes_ecb_encrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len)
        }
 }
 
-void
-aes_ecb_decrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len)
+void aes_ecb_decrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len)
 {
-       unsigned        bs = 16;
-       uint32_t           *d;
+       unsigned bs = 16;
+       uint32_t *d;
 
-       while (len >= bs)
-       {
-               d = (uint32_t *) data;
+       while (len >= bs) {
+               d = (uint32_t *)data;
                rijndael_decrypt(ctx, d, d);
 
                len -= bs;
@@ -379,15 +377,14 @@ aes_ecb_decrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len)
        }
 }
 
-void
-aes_cbc_encrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data, unsigned len)
+void aes_cbc_encrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data,
+                                        unsigned len)
 {
-       uint32_t           *iv = (uint32_t *) iva;
-       uint32_t           *d = (uint32_t *) data;
-       unsigned        bs = 16;
+       uint32_t *iv = (uint32_t *)iva;
+       uint32_t *d = (uint32_t *)data;
+       unsigned bs = 16;
 
-       while (len >= bs)
-       {
+       while (len >= bs) {
                d[0] ^= iv[0];
                d[1] ^= iv[1];
                d[2] ^= iv[2];
@@ -401,17 +398,15 @@ aes_cbc_encrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data, unsigned len)
        }
 }
 
-void
-aes_cbc_decrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data, unsigned len)
+void aes_cbc_decrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data,
+                                        unsigned len)
 {
-       uint32_t           *d = (uint32_t *) data;
-       unsigned        bs = 16;
-       uint32_t                buf[4],
-                               iv[4];
+       uint32_t *d = (uint32_t *)data;
+       unsigned bs = 16;
+       uint32_t buf[4], iv[4];
 
        memcpy(iv, iva, bs);
-       while (len >= bs)
-       {
+       while (len >= bs) {
                buf[0] = d[0];
                buf[1] = d[1];
                buf[2] = d[2];
index 2a8a726..d22603f 100644 (file)
 #include <libc.h>
 #include "sha2.h"
 
-
 /*** SHA-256/512 Various Length Definitions ***********************/
-enum{
-       SHA256ShortBlockLength  = (SHA256BlockLength - 8),
-       SHA512ShortBlockLength  = (SHA512_block_length - 16)
+enum {
+       SHA256ShortBlockLength = (SHA256BlockLength - 8),
+       SHA512ShortBlockLength = (SHA512_block_length - 16)
 };
 
 /*
@@ -52,12 +51,13 @@ enum{
  * unsigned 128-bit integer (represented using a two-element array of
  * 64-bit words):
  */
-#define ADDINC128(w,n) { \
-       (w)[0] += (uint64_t)(n); \
-       if ((w)[0] < (n)) { \
-               (w)[1]++; \
-       } \
-}
+#define ADDINC128(w, n)                                                        \
+       {                                                                          \
+               (w)[0] += (uint64_t)(n);                                               \
+               if ((w)[0] < (n)) {                                                    \
+                       (w)[1]++;                                                          \
+               }                                                                      \
+       }
 
 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
 /*
@@ -69,154 +69,108 @@ enum{
  *   same "backwards" definition.
  */
 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
-#define R(b,x)                 ((x) >> (b))
+#define R(b, x) ((x) >> (b))
 /* 32-bit Rotate-right (used in SHA-256): */
-#define S32(b,x)       (((x) >> (b)) | ((x) << (32 - (b))))
+#define S32(b, x) (((x) >> (b)) | ((x) << (32 - (b))))
 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
-#define S64(b,x)       (((x) >> (b)) | ((x) << (64 - (b))))
+#define S64(b, x) (((x) >> (b)) | ((x) << (64 - (b))))
 
 /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
-#define Ch(x,y,z)      (((x) & (y)) ^ ((~(x)) & (z)))
-#define Maj(x,y,z)     (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
+#define Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z)))
+#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
 
 /* Four of six logical functions used in SHA-256: */
-#define Sigma0_256(x)  (S32(2,  (x)) ^ S32(13, (x)) ^ S32(22, (x)))
-#define Sigma1_256(x)  (S32(6,  (x)) ^ S32(11, (x)) ^ S32(25, (x)))
-#define sigma0_256(x)  (S32(7,  (x)) ^ S32(18, (x)) ^ R(3 ,   (x)))
-#define sigma1_256(x)  (S32(17, (x)) ^ S32(19, (x)) ^ R(10,   (x)))
+#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
+#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
+#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3, (x)))
+#define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
 
 /* Four of six logical functions used in SHA-384 and SHA-512: */
-#define Sigma0_512(x)  (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
-#define Sigma1_512(x)  (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
-#define sigma0_512(x)  (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
-#define sigma1_512(x)  (S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
+#define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
+#define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
+#define sigma0_512(x) (S64(1, (x)) ^ S64(8, (x)) ^ R(7, (x)))
+#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R(6, (x)))
 
 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
 /* NOTE: These should not be accessed directly from outside this
  * library -- they are intended for private internal visibility/use
  * only.
  */
-static void SHA512_Last(SHA512Ctx*);
-static void SHA256_Transform(SHA256Ctx*, const uint32_t*);
-static void SHA512_Transform(SHA512Ctx*, const uint64_t*);
+static void SHA512_Last(SHA512Ctx *);
+static void SHA256_Transform(SHA256Ctx *, const uint32_t *);
+static void SHA512_Transform(SHA512Ctx *, const uint64_t *);
 
 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
 /* Hash constant words K for SHA-256: */
 const uint32_t K256[64] = {
-       0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
-       0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
-       0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
-       0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
-       0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
-       0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
-       0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
-       0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
-       0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
-       0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
-       0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
-       0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
-       0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
-       0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
-       0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
-       0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
-};
+       0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
+       0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
+       0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
+       0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
+       0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
+       0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
+       0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
+       0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
+       0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
+       0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
+       0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
+       0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
+       0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL};
 
 /* Initial hash value H for SHA-224: */
 const uint32_t sha224_initial_hash_value[8] = {
-       0xc1059ed8UL,
-       0x367cd507UL,
-       0x3070dd17UL,
-       0xf70e5939UL,
-       0xffc00b31UL,
-       0x68581511UL,
-       0x64f98fa7UL,
-       0xbefa4fa4UL
-};
+       0xc1059ed8UL, 0x367cd507UL, 0x3070dd17UL, 0xf70e5939UL,
+       0xffc00b31UL, 0x68581511UL, 0x64f98fa7UL, 0xbefa4fa4UL};
 
 /* Initial hash value H for SHA-256: */
 static const uint32_t sha256_initial_hash_value[8] = {
-       0x6a09e667UL,
-       0xbb67ae85UL,
-       0x3c6ef372UL,
-       0xa54ff53aUL,
-       0x510e527fUL,
-       0x9b05688cUL,
-       0x1f83d9abUL,
-       0x5be0cd19UL
-};
+       0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL,
+       0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL};
 
 /* Hash constant words K for SHA-384 and SHA-512: */
 static const uint64_t K512[80] = {
-       0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
-       0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
-       0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
-       0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
-       0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
-       0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
-       0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
-       0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
-       0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
-       0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
-       0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
-       0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
-       0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
-       0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
-       0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
-       0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
-       0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
-       0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
-       0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
-       0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
-       0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
-       0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
-       0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
-       0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
-       0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
-       0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
-       0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
-       0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
-       0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
-       0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
-       0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
-       0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
-       0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
-       0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
-       0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
-       0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
-       0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
-       0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
-       0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
-       0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
-};
+       0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL,
+       0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
+       0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL,
+       0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
+       0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL,
+       0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
+       0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL,
+       0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
+       0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL,
+       0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
+       0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL,
+       0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
+       0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL,
+       0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
+       0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL,
+       0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
+       0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL,
+       0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
+       0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL,
+       0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
+       0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL,
+       0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
+       0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL,
+       0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
+       0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL,
+       0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
+       0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL};
 
 /* Initial hash value H for SHA-384 */
 static const uint64_t sha384_initial_hash_value[8] = {
-       0xcbbb9d5dc1059ed8ULL,
-       0x629a292a367cd507ULL,
-       0x9159015a3070dd17ULL,
-       0x152fecd8f70e5939ULL,
-       0x67332667ffc00b31ULL,
-       0x8eb44a8768581511ULL,
-       0xdb0c2e0d64f98fa7ULL,
-       0x47b5481dbefa4fa4ULL
-};
+       0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL,
+       0x152fecd8f70e5939ULL, 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL,
+       0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL};
 
 /* Initial hash value H for SHA-512 */
 static const uint64_t sha512_initial_hash_value[8] = {
-       0x6a09e667f3bcc908ULL,
-       0xbb67ae8584caa73bULL,
-       0x3c6ef372fe94f82bULL,
-       0xa54ff53a5f1d36f1ULL,
-       0x510e527fade682d1ULL,
-       0x9b05688c2b3e6c1fULL,
-       0x1f83d9abfb41bd6bULL,
-       0x5be0cd19137e2179ULL
-};
-
+       0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL,
+       0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
+       0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
 
 /*** SHA-256: *********************************************************/
-void
-SHA256_Init(SHA256Ctx *context)
+void SHA256_Init(SHA256Ctx *context)
 {
        if (context == nil)
                return;
@@ -224,25 +178,13 @@ SHA256_Init(SHA256Ctx *context)
        memset(context->buffer, 0, SHA256BlockLength);
        context->bitcount = 0;
 }
-static void
-SHA256_Transform(SHA256Ctx *context, const uint32_t *data)
+static void SHA256_Transform(SHA256Ctx *context, const uint32_t *data)
 {
-       uint32_t                a,
-                               b,
-                               c,
-                               d,
-                               e,
-                               f,
-                               g,
-                               h,
-                               s0,
-                               s1;
-       uint32_t                T1,
-                               T2,
-                          *W256;
-       int                     j;
-
-       W256 = (uint32_t *) context->buffer;
+       uint32_t a, b, c, d, e, f, g, h, s0, s1;
+       uint32_t T1, T2, *W256;
+       int j;
+
+       W256 = (uint32_t *)context->buffer;
 
        /* Initialize registers with the prev. intermediate value */
        a = context->state[0];
@@ -255,10 +197,9 @@ SHA256_Transform(SHA256Ctx *context, const uint32_t *data)
        h = context->state[7];
 
        j = 0;
-       do
-       {
-               W256[j] = (uint32_t) data[3] | ((uint32_t) data[2] << 8) |
-                       ((uint32_t) data[1] << 16) | ((uint32_t) data[0] << 24);
+       do {
+               W256[j] = (uint32_t)data[3] | ((uint32_t)data[2] << 8) |
+                                 ((uint32_t)data[1] << 16) | ((uint32_t)data[0] << 24);
                data += 4;
                /* Apply the SHA-256 compression function to update a..h */
                T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
@@ -275,8 +216,7 @@ SHA256_Transform(SHA256Ctx *context, const uint32_t *data)
                j++;
        } while (j < 16);
 
-       do
-       {
+       do {
                /* Part of the message block expansion: */
                s0 = W256[(j + 1) & 0x0f];
                s0 = sigma0_256(s0);
@@ -285,7 +225,7 @@ SHA256_Transform(SHA256Ctx *context, const uint32_t *data)
 
                /* Apply the SHA-256 compression function to update a..h */
                T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
-                       (W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0);
+                        (W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0);
                T2 = Sigma0_256(a) + Maj(a, b, c);
                h = g;
                g = f;
@@ -313,33 +253,27 @@ SHA256_Transform(SHA256Ctx *context, const uint32_t *data)
        a = b = c = d = e = f = g = h = T1 = T2 = 0;
 }
 
-void
-SHA256_Update(SHA256Ctx *context, const uint8_t *data, size_t len)
+void SHA256_Update(SHA256Ctx *context, const uint8_t *data, size_t len)
 {
-       size_t          freespace,
-                               usedspace;
+       size_t freespace, usedspace;
 
        /* Calling with no data is valid (we do nothing) */
        if (len == 0)
                return;
 
        usedspace = (context->bitcount >> 3) % SHA256BlockLength;
-       if (usedspace > 0)
-       {
+       if (usedspace > 0) {
                /* Calculate how much free space is available in the buffer */
                freespace = SHA256BlockLength - usedspace;
 
-               if (len >= freespace)
-               {
+               if (len >= freespace) {
                        /* Fill the buffer completely and process it */
                        memmove(&context->buffer[usedspace], data, freespace);
                        context->bitcount += freespace << 3;
                        len -= freespace;
                        data += freespace;
-                       SHA256_Transform(context, (uint32_t*)context->buffer);
-               }
-               else
-               {
+                       SHA256_Transform(context, (uint32_t *)context->buffer);
+               } else {
                        /* The buffer is not yet full */
                        memmove(&context->buffer[usedspace], data, len);
                        context->bitcount += len << 3;
@@ -348,16 +282,14 @@ SHA256_Update(SHA256Ctx *context, const uint8_t *data, size_t len)
                        return;
                }
        }
-       while (len >= SHA256BlockLength)
-       {
+       while (len >= SHA256BlockLength) {
                /* Process as many complete blocks as we can */
-               SHA256_Transform(context, (const uint32_t*)data);
+               SHA256_Transform(context, (const uint32_t *)data);
                context->bitcount += SHA256BlockLength << 3;
                len -= SHA256BlockLength;
                data += SHA256BlockLength;
        }
-       if (len > 0)
-       {
+       if (len > 0) {
                /* There's left-overs, so save 'em */
                memmove(context->buffer, data, len);
                context->bitcount += len << 3;
@@ -366,38 +298,32 @@ SHA256_Update(SHA256Ctx *context, const uint8_t *data, size_t len)
        usedspace = freespace = 0;
 }
 
-static void
-SHA256_Last(SHA256Ctx *context)
+static void SHA256_Last(SHA256Ctx *context)
 {
        unsigned int usedspace;
 
        usedspace = (context->bitcount >> 3) % SHA256BlockLength;
 
-       if (usedspace > 0)
-       {
+       if (usedspace > 0) {
                /* Begin padding with a 1 bit: */
                context->buffer[usedspace++] = 0x80;
 
-               if (usedspace <= SHA256ShortBlockLength)
-               {
+               if (usedspace <= SHA256ShortBlockLength) {
                        /* Set-up for the last transform: */
-                       memset(&context->buffer[usedspace], 0, SHA256ShortBlockLength - usedspace);
-               }
-               else
-               {
-                       if (usedspace < SHA256BlockLength)
-                       {
-                               memset(&context->buffer[usedspace], 0, SHA256BlockLength - usedspace);
+                       memset(&context->buffer[usedspace], 0,
+                                  SHA256ShortBlockLength - usedspace);
+               } else {
+                       if (usedspace < SHA256BlockLength) {
+                               memset(&context->buffer[usedspace], 0,
+                                          SHA256BlockLength - usedspace);
                        }
                        /* Do second-to-last transform: */
-                       SHA256_Transform(context, (uint32_t*)context->buffer);
+                       SHA256_Transform(context, (uint32_t *)context->buffer);
 
                        /* And set-up for the last transform: */
                        memset(context->buffer, 0, SHA256ShortBlockLength);
                }
-       }
-       else
-       {
+       } else {
                /* Set-up for the last transform: */
                memset(context->buffer, 0, SHA256ShortBlockLength);
 
@@ -405,18 +331,16 @@ SHA256_Last(SHA256Ctx *context)
                *context->buffer = 0x80;
        }
        /* Set the bit count: */
-       *(uint64_t *) &context->buffer[SHA256ShortBlockLength] = context->bitcount;
+       *(uint64_t *)&context->buffer[SHA256ShortBlockLength] = context->bitcount;
 
        /* Final transform: */
-       SHA256_Transform(context, (uint32_t*)context->buffer);
+       SHA256_Transform(context, (uint32_t *)context->buffer);
 }
 
-void
-SHA256_Final(uint8_t digest[], SHA256Ctx *context)
+void SHA256_Final(uint8_t digest[], SHA256Ctx *context)
 {
        /* If no digest buffer is passed, we don't bother doing this: */
-       if (digest != nil)
-       {
+       if (digest != nil) {
                SHA256_Last(context);
 
                memmove(digest, context->state, SHA256DigestLength);
@@ -426,10 +350,8 @@ SHA256_Final(uint8_t digest[], SHA256Ctx *context)
        memset(context, 0, sizeof(*context));
 }
 
-
 /*** SHA-512: *********************************************************/
-void
-SHA512_Init(SHA512Ctx *context)
+void SHA512_Init(SHA512Ctx *context)
 {
        if (context == nil)
                return;
@@ -438,23 +360,11 @@ SHA512_Init(SHA512Ctx *context)
        context->bitcount[0] = context->bitcount[1] = 0;
 }
 
-static void
-SHA512_Transform(SHA512Ctx *context, const uint64_t *data)
+static void SHA512_Transform(SHA512Ctx *context, const uint64_t *data)
 {
-       uint64_t                a,
-                               b,
-                               c,
-                               d,
-                               e,
-                               f,
-                               g,
-                               h,
-                               s0,
-                               s1;
-       uint64_t                T1,
-                               T2,
-                          *W512 = (uint64_t *) context->buffer;
-       int                     j;
+       uint64_t a, b, c, d, e, f, g, h, s0, s1;
+       uint64_t T1, T2, *W512 = (uint64_t *)context->buffer;
+       int j;
 
        /* Initialize registers with the prev. intermediate value */
        a = context->state[0];
@@ -467,12 +377,11 @@ SHA512_Transform(SHA512Ctx *context, const uint64_t *data)
        h = context->state[7];
 
        j = 0;
-       do
-       {
-               W512[j] = (uint64_t) data[7] | ((uint64_t) data[6] << 8) |
-                       ((uint64_t) data[5] << 16) | ((uint64_t) data[4] << 24) |
-                       ((uint64_t) data[3] << 32) | ((uint64_t) data[2] << 40) |
-                       ((uint64_t) data[1] << 48) | ((uint64_t) data[0] << 56);
+       do {
+               W512[j] = (uint64_t)data[7] | ((uint64_t)data[6] << 8) |
+                                 ((uint64_t)data[5] << 16) | ((uint64_t)data[4] << 24) |
+                                 ((uint64_t)data[3] << 32) | ((uint64_t)data[2] << 40) |
+                                 ((uint64_t)data[1] << 48) | ((uint64_t)data[0] << 56);
                data += 8;
                /* Apply the SHA-512 compression function to update a..h */
                T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
@@ -489,8 +398,7 @@ SHA512_Transform(SHA512Ctx *context, const uint64_t *data)
                j++;
        } while (j < 16);
 
-       do
-       {
+       do {
                /* Part of the message block expansion: */
                s0 = W512[(j + 1) & 0x0f];
                s0 = sigma0_512(s0);
@@ -499,7 +407,7 @@ SHA512_Transform(SHA512Ctx *context, const uint64_t *data)
 
                /* Apply the SHA-512 compression function to update a..h */
                T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
-                       (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0);
+                        (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0);
                T2 = Sigma0_512(a) + Maj(a, b, c);
                h = g;
                g = f;
@@ -527,33 +435,27 @@ SHA512_Transform(SHA512Ctx *context, const uint64_t *data)
        a = b = c = d = e = f = g = h = T1 = T2 = 0;
 }
 
-void
-SHA512_Update(SHA512Ctx *context, const uint8_t *data, size_t len)
+void SHA512_Update(SHA512Ctx *context, const uint8_t *data, size_t len)
 {
-       size_t          freespace,
-                               usedspace;
+       size_t freespace, usedspace;
 
        /* Calling with no data is valid (we do nothing) */
        if (len == 0)
                return;
 
        usedspace = (context->bitcount[0] >> 3) % SHA512_block_length;
-       if (usedspace > 0)
-       {
+       if (usedspace > 0) {
                /* Calculate how much free space is available in the buffer */
                freespace = SHA512_block_length - usedspace;
 
-               if (len >= freespace)
-               {
+               if (len >= freespace) {
                        /* Fill the buffer completely and process it */
                        memmove(&context->buffer[usedspace], data, freespace);
                        ADDINC128(context->bitcount, freespace << 3);
                        len -= freespace;
                        data += freespace;
-                       SHA512_Transform(context, (uint64_t*)context->buffer);
-               }
-               else
-               {
+                       SHA512_Transform(context, (uint64_t *)context->buffer);
+               } else {
                        /* The buffer is not yet full */
                        memmove(&context->buffer[usedspace], data, len);
                        ADDINC128(context->bitcount, len << 3);
@@ -562,16 +464,14 @@ SHA512_Update(SHA512Ctx *context, const uint8_t *data, size_t len)
                        return;
                }
        }
-       while (len >= SHA512_block_length)
-       {
+       while (len >= SHA512_block_length) {
                /* Process as many complete blocks as we can */
-               SHA512_Transform(context, (const uint64_t*)data);
+               SHA512_Transform(context, (const uint64_t *)data);
                ADDINC128(context->bitcount, SHA512_block_length << 3);
                len -= SHA512_block_length;
                data += SHA512_block_length;
        }
-       if (len > 0)
-       {
+       if (len > 0) {
                /* There's left-overs, so save 'em */
                memmove(context->buffer, data, len);
                ADDINC128(context->bitcount, len << 3);
@@ -580,38 +480,32 @@ SHA512_Update(SHA512Ctx *context, const uint8_t *data, size_t len)
        usedspace = freespace = 0;
 }
 
-static void
-SHA512_Last(SHA512Ctx *context)
+static void SHA512_Last(SHA512Ctx *context)
 {
        unsigned int usedspace;
 
        usedspace = (context->bitcount[0] >> 3) % SHA512_block_length;
 
-       if (usedspace > 0)
-       {
+       if (usedspace > 0) {
                /* Begin padding with a 1 bit: */
                context->buffer[usedspace++] = 0x80;
 
-               if (usedspace <= SHA512ShortBlockLength)
-               {
+               if (usedspace <= SHA512ShortBlockLength) {
                        /* Set-up for the last transform: */
-                       memset(&context->buffer[usedspace], 0, SHA512ShortBlockLength - usedspace);
-               }
-               else
-               {
-                       if (usedspace < SHA512_block_length)
-                       {
-                               memset(&context->buffer[usedspace], 0, SHA512_block_length - usedspace);
+                       memset(&context->buffer[usedspace], 0,
+                                  SHA512ShortBlockLength - usedspace);
+               } else {
+                       if (usedspace < SHA512_block_length) {
+                               memset(&context->buffer[usedspace], 0,
+                                          SHA512_block_length - usedspace);
                        }
                        /* Do second-to-last transform: */
-                       SHA512_Transform(context, (uint64_t*)context->buffer);
+                       SHA512_Transform(context, (uint64_t *)context->buffer);
 
                        /* And set-up for the last transform: */
                        memset(context->buffer, 0, SHA512_block_length - 2);
                }
-       }
-       else
-       {
+       } else {
                /* Prepare for final transform: */
                memset(context->buffer, 0, SHA512ShortBlockLength);
 
@@ -619,19 +513,19 @@ SHA512_Last(SHA512Ctx *context)
                *context->buffer = 0x80;
        }
        /* Store the length of input data (in bits): */
-       *(uint64_t *) &context->buffer[SHA512ShortBlockLength] = context->bitcount[1];
-       *(uint64_t *) &context->buffer[SHA512ShortBlockLength + 8] = context->bitcount[0];
+       *(uint64_t *)&context->buffer[SHA512ShortBlockLength] =
+               context->bitcount[1];
+       *(uint64_t *)&context->buffer[SHA512ShortBlockLength + 8] =
+               context->bitcount[0];
 
        /* Final transform: */
-       SHA512_Transform(context, (uint64_t*)context->buffer);
+       SHA512_Transform(context, (uint64_t *)context->buffer);
 }
 
-void
-SHA512_Final(uint8_t digest[], SHA512Ctx *context)
+void SHA512_Final(uint8_t digest[], SHA512Ctx *context)
 {
        /* If no digest buffer is passed, we don't bother doing this: */
-       if (digest != nil)
-       {
+       if (digest != nil) {
                SHA512_Last(context);
 
                /* Save the hash data for output: */
@@ -642,10 +536,8 @@ SHA512_Final(uint8_t digest[], SHA512Ctx *context)
        memset(context, 0, sizeof(*context));
 }
 
-
 /*** SHA-384: *********************************************************/
-void
-SHA384_Init(SHA384Ctx *context)
+void SHA384_Init(SHA384Ctx *context)
 {
        if (context == nil)
                return;
@@ -654,19 +546,16 @@ SHA384_Init(SHA384Ctx *context)
        context->bitcount[0] = context->bitcount[1] = 0;
 }
 
-void
-SHA384_Update(SHA384Ctx *context, const uint8_t *data, size_t len)
+void SHA384_Update(SHA384Ctx *context, const uint8_t *data, size_t len)
 {
-       SHA512_Update((SHA512Ctx *) context, data, len);
+       SHA512_Update((SHA512Ctx *)context, data, len);
 }
 
-void
-SHA384_Final(uint8_t digest[], SHA384Ctx *context)
+void SHA384_Final(uint8_t digest[], SHA384Ctx *context)
 {
        /* If no digest buffer is passed, we don't bother doing this: */
-       if (digest != nil)
-       {
-               SHA512_Last((SHA512Ctx *) context);
+       if (digest != nil) {
+               SHA512_Last((SHA512Ctx *)context);
 
                /* Save the hash data for output: */
                memmove(digest, context->state, SHA384DigestLength);
@@ -677,8 +566,7 @@ SHA384_Final(uint8_t digest[], SHA384Ctx *context)
 }
 
 /*** SHA-224: *********************************************************/
-void
-SHA224_Init(SHA224Ctx *context)
+void SHA224_Init(SHA224Ctx *context)
 {
        if (context == nil)
                return;
@@ -687,18 +575,15 @@ SHA224_Init(SHA224Ctx *context)
        context->bitcount = 0;
 }
 
-void
-SHA224_Update(SHA224Ctx *context, const uint8_t *data, size_t len)
+void SHA224_Update(SHA224Ctx *context, const uint8_t *data, size_t len)
 {
-       SHA256_Update((SHA256Ctx *) context, data, len);
+       SHA256_Update((SHA256Ctx *)context, data, len);
 }
 
-void
-SHA224_Final(uint8_t digest[], SHA224Ctx *context)
+void SHA224_Final(uint8_t digest[], SHA224Ctx *context)
 {
        /* If no digest buffer is passed, we don't bother doing this: */
-       if (digest != nil)
-       {
+       if (digest != nil) {
                SHA256_Last(context);
 
                memmove(digest, context->state, SHA224DigestLength);