ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
hmac_sha512.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file hmac_sha512.cpp
6 * @brief HMAC-SHA2-512 implementation (RFC 2104). See hmac_sha512.h.
7 *
8 * HMAC(K, m) = H((K XOR opad) || H((K XOR ipad) || m)), H = SHA-512, block = 128 bytes,
9 * ipad = 0x36 repeated, opad = 0x5c repeated.
10 */
11
13#include "crypto/crypto_opt.h"
15
16namespace
17{
18// One 128-byte HMAC key block: keys > 128 bytes are pre-hashed (RFC 2104), else zero-padded.
19void build_key_block(const uint8_t *key, size_t key_len, uint8_t block[PC_SHA512_BLOCK_LEN], uint8_t pad_byte)
20{
21 uint8_t k[PC_SHA512_BLOCK_LEN] = {0};
22 if (key_len > PC_SHA512_BLOCK_LEN)
23 {
24 pc_sha512(key, key_len, k); // 64-byte digest; the remaining 64 bytes stay zero
25 }
26 else
27 {
28 for (size_t i = 0; i < key_len; i++)
29 {
30 k[i] = key[i];
31 }
32 }
33 for (int i = 0; i < PC_SHA512_BLOCK_LEN; i++)
34 {
35 block[i] = (uint8_t)(k[i] ^ pad_byte);
36 }
37}
38} // namespace
39
40void pc_hmac_sha512_init(pc_hmac_sha512_ctx *ctx, const uint8_t *key, size_t key_len)
41{
42 uint8_t ikey[PC_SHA512_BLOCK_LEN];
43 build_key_block(key, key_len, ikey, 0x36u); // ipad
44 build_key_block(key, key_len, ctx->okey, 0x5cu); // opad (kept for the final step)
45 pc_sha512_init(&ctx->inner);
47}
48
49void pc_hmac_sha512_update(pc_hmac_sha512_ctx *ctx, const uint8_t *data, size_t len)
50{
51 pc_sha512_update(&ctx->inner, data, len);
52}
53
55{
56 uint8_t inner_digest[PC_SHA512_DIGEST_LEN];
57 pc_sha512_final(&ctx->inner, inner_digest);
58 pc_sha512_ctx outer;
59 pc_sha512_init(&outer);
61 pc_sha512_update(&outer, inner_digest, PC_SHA512_DIGEST_LEN);
62 pc_sha512_final(&outer, mac);
63}
64
65void pc_hmac_sha512(const uint8_t *key, size_t key_len, const uint8_t *data, size_t len,
66 uint8_t mac[PC_HMAC_SHA512_LEN])
67{
69 pc_hmac_sha512_init(&ctx, key, key_len);
70 pc_hmac_sha512_update(&ctx, data, len);
71 pc_hmac_sha512_final(&ctx, mac);
72}
Per-translation-unit optimization override for hot, pure-integer crypto.
void pc_hmac_sha512_final(pc_hmac_sha512_ctx *ctx, uint8_t mac[PC_HMAC_SHA512_LEN])
Finish, writing the 64-byte MAC.
void pc_hmac_sha512_init(pc_hmac_sha512_ctx *ctx, const uint8_t *key, size_t key_len)
Begin an HMAC-SHA2-512 over key (keys > 128 bytes are pre-hashed per RFC 2104).
void pc_hmac_sha512(const uint8_t *key, size_t key_len, const uint8_t *data, size_t len, uint8_t mac[PC_HMAC_SHA512_LEN])
One-shot HMAC-SHA2-512 over a single buffer.
void pc_hmac_sha512_update(pc_hmac_sha512_ctx *ctx, const uint8_t *data, size_t len)
Feed len message bytes.
HMAC-SHA2-512 (RFC 2104 + FIPS 198-1) - streaming context and one-shot API.
#define PC_HMAC_SHA512_LEN
HMAC-SHA2-512 output length in bytes.
Definition hmac_sha512.h:25
void build_key_block(const uint8_t *key, size_t key_len, uint8_t block[64], uint8_t pad_byte, uint8_t scratch[64])
void pc_sha512_update(pc_sha512_ctx *ctx, const uint8_t *data, size_t len)
Feed len bytes of data into the running hash.
Definition sha512.cpp:38
void pc_sha512(const uint8_t *data, size_t len, uint8_t digest[PC_SHA512_DIGEST_LEN])
One-shot SHA-512: hash len bytes of data into digest (64 bytes).
Definition sha512.cpp:57
PC_CRYPTO_HOT void pc_sha512_init(pc_sha512_ctx *ctx)
Initialize a streaming SHA-512 context (ctx must not be NULL).
Definition sha512.cpp:28
void pc_sha512_final(pc_sha512_ctx *ctx, uint8_t digest[PC_SHA512_DIGEST_LEN])
Finalize the hash and write the 64-byte digest. The context is undefined afterwards; call init() agai...
Definition sha512.cpp:47
#define PC_SHA512_DIGEST_LEN
SHA-512 digest length in bytes.
Definition sha512.h:24
#define PC_SHA512_BLOCK_LEN
SHA-512 block size in bytes.
Definition sha512.h:27
Streaming HMAC-SHA2-512 context (stores the opad key block + inner hash state).
Definition hmac_sha512.h:29
uint8_t okey[PC_SHA512_BLOCK_LEN]
(key XOR opad), applied in the final step
Definition hmac_sha512.h:30
pc_sha512_ctx inner
inner hash: H((key XOR ipad) || message)
Definition hmac_sha512.h:31
Streaming SHA-512 context.
Definition sha512.h:33