diff options
author | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2016-11-08 19:27:58 +0100 |
---|---|---|
committer | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2016-11-08 19:27:58 +0100 |
commit | 8860e991f6650e47cfe6c1af595fe4fe92a4edfd (patch) | |
tree | 18f49bb3a21739a1a596b54d9f65e82cff4fc09f /lib/sha512.c | |
parent | cc5b93f72db80abd1262a0a5e1d8400ceef54385 (diff) | |
parent | c8cafc8ebb5320ac7c6117c17e6460036f0fdf62 (diff) |
Merge branch 'master' into int-new
Diffstat (limited to 'lib/sha512.c')
-rw-r--r-- | lib/sha512.c | 150 |
1 files changed, 14 insertions, 136 deletions
diff --git a/lib/sha512.c b/lib/sha512.c index 37e660f7..576ae1e8 100644 --- a/lib/sha512.c +++ b/lib/sha512.c @@ -1,6 +1,5 @@ /* - * BIRD Library -- SHA-512 and SHA-384 Hash Functions, - * HMAC-SHA-512 and HMAC-SHA-384 Functions + * BIRD Library -- SHA-512 and SHA-384 Hash Functions * * (c) 2015 CZ.NIC z.s.p.o. * @@ -17,8 +16,10 @@ // #define SHA512_UNROLLED void -sha512_init(struct sha512_context *ctx) +sha512_init(struct hash_context *CTX) { + struct sha512_context *ctx = (void *) CTX; + ctx->h0 = U64(0x6a09e667f3bcc908); ctx->h1 = U64(0xbb67ae8584caa73b); ctx->h2 = U64(0x3c6ef372fe94f82b); @@ -33,8 +34,10 @@ sha512_init(struct sha512_context *ctx) } void -sha384_init(struct sha384_context *ctx) +sha384_init(struct hash_context *CTX) { + struct sha384_context *ctx = (void *) CTX; + ctx->h0 = U64(0xcbbb9d5dc1059ed8); ctx->h1 = U64(0x629a292a367cd507); ctx->h2 = U64(0x9159015a3070dd17); @@ -389,8 +392,10 @@ sha512_transform(struct sha512_context *ctx, const byte *data) } void -sha512_update(struct sha512_context *ctx, const byte *buf, size_t len) +sha512_update(struct hash_context *CTX, const byte *buf, uint len) { + struct sha512_context *ctx = (void *) CTX; + if (ctx->count) { /* Fill rest of internal buffer */ @@ -432,11 +437,12 @@ sha512_update(struct sha512_context *ctx, const byte *buf, size_t len) * first 48 of those bytes. */ byte * -sha512_final(struct sha512_context *ctx) +sha512_final(struct hash_context *CTX) { + struct sha512_context *ctx = (void *) CTX; u64 t, th, msb, lsb; - sha512_update(ctx, NULL, 0); /* flush */ + sha512_update(CTX, NULL, 0); /* flush */ t = ctx->nblocks; th = 0; @@ -467,7 +473,7 @@ sha512_final(struct sha512_context *ctx) ctx->buf[ctx->count++] = 0x80; /* pad character */ while(ctx->count < 128) ctx->buf[ctx->count++] = 0; - sha512_update(ctx, NULL, 0); /* flush */ + sha512_update(CTX, NULL, 0); /* flush */ memset(ctx->buf, 0, 112); /* fill next block with zeroes */ } @@ -490,131 +496,3 @@ sha512_final(struct sha512_context *ctx) return ctx->buf; } - - -/* - * SHA512-HMAC - */ - -static void -sha512_hash_buffer(byte *outbuf, const byte *buffer, size_t length) -{ - struct sha512_context ctx; - - sha512_init(&ctx); - sha512_update(&ctx, buffer, length); - memcpy(outbuf, sha512_final(&ctx), SHA512_SIZE); -} - -void -sha512_hmac_init(struct sha512_hmac_context *ctx, const byte *key, size_t keylen) -{ - byte keybuf[SHA512_BLOCK_SIZE], buf[SHA512_BLOCK_SIZE]; - - /* Hash the key if necessary */ - if (keylen <= SHA512_BLOCK_SIZE) - { - memcpy(keybuf, key, keylen); - memset(keybuf + keylen, 0, SHA512_BLOCK_SIZE - keylen); - } - else - { - sha512_hash_buffer(keybuf, key, keylen); - memset(keybuf + SHA512_SIZE, 0, SHA512_BLOCK_SIZE - SHA512_SIZE); - } - - /* Initialize the inner digest */ - sha512_init(&ctx->ictx); - int i; - for (i = 0; i < SHA512_BLOCK_SIZE; i++) - buf[i] = keybuf[i] ^ 0x36; - sha512_update(&ctx->ictx, buf, SHA512_BLOCK_SIZE); - - /* Initialize the outer digest */ - sha512_init(&ctx->octx); - for (i = 0; i < SHA512_BLOCK_SIZE; i++) - buf[i] = keybuf[i] ^ 0x5c; - sha512_update(&ctx->octx, buf, SHA512_BLOCK_SIZE); -} - -void -sha512_hmac_update(struct sha512_hmac_context *ctx, const byte *buf, size_t buflen) -{ - /* Just update the inner digest */ - sha512_update(&ctx->ictx, buf, buflen); -} - -byte * -sha512_hmac_final(struct sha512_hmac_context *ctx) -{ - /* Finish the inner digest */ - byte *isha = sha512_final(&ctx->ictx); - - /* Finish the outer digest */ - sha512_update(&ctx->octx, isha, SHA512_SIZE); - return sha512_final(&ctx->octx); -} - - -/* - * SHA384-HMAC - */ - -static void -sha384_hash_buffer(byte *outbuf, const byte *buffer, size_t length) -{ - struct sha384_context ctx; - - sha384_init(&ctx); - sha384_update(&ctx, buffer, length); - memcpy(outbuf, sha384_final(&ctx), SHA384_SIZE); -} - -void -sha384_hmac_init(struct sha384_hmac_context *ctx, const byte *key, size_t keylen) -{ - byte keybuf[SHA384_BLOCK_SIZE], buf[SHA384_BLOCK_SIZE]; - - /* Hash the key if necessary */ - if (keylen <= SHA384_BLOCK_SIZE) - { - memcpy(keybuf, key, keylen); - memset(keybuf + keylen, 0, SHA384_BLOCK_SIZE - keylen); - } - else - { - sha384_hash_buffer(keybuf, key, keylen); - memset(keybuf + SHA384_SIZE, 0, SHA384_BLOCK_SIZE - SHA384_SIZE); - } - - /* Initialize the inner digest */ - sha384_init(&ctx->ictx); - int i; - for (i = 0; i < SHA384_BLOCK_SIZE; i++) - buf[i] = keybuf[i] ^ 0x36; - sha384_update(&ctx->ictx, buf, SHA384_BLOCK_SIZE); - - /* Initialize the outer digest */ - sha384_init(&ctx->octx); - for (i = 0; i < SHA384_BLOCK_SIZE; i++) - buf[i] = keybuf[i] ^ 0x5c; - sha384_update(&ctx->octx, buf, SHA384_BLOCK_SIZE); -} - -void -sha384_hmac_update(struct sha384_hmac_context *ctx, const byte *buf, size_t buflen) -{ - /* Just update the inner digest */ - sha384_update(&ctx->ictx, buf, buflen); -} - -byte * -sha384_hmac_final(struct sha384_hmac_context *ctx) -{ - /* Finish the inner digest */ - byte *isha = sha384_final(&ctx->ictx); - - /* Finish the outer digest */ - sha384_update(&ctx->octx, isha, SHA384_SIZE); - return sha384_final(&ctx->octx); -} |