2 Unix SMB/CIFS implementation.
4 Interface header: HMAC SHA-256 code
6 Copyright (C) Andrew Tridgell 2008
8 based in hmacsha1.c which is:
9 Copyright (C) Stefan Metzmacher
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 taken direct from rfc2202 implementation and modified for suitable use
30 #include "lib/crypto/crypto.h"
31 #include "heimdal/lib/hcrypto/sha.h"
33 /***********************************************************************
34 the rfc 2104/2202 version of hmac_sha256 initialisation.
35 ***********************************************************************/
36 _PUBLIC_ void hmac_sha256_init(const uint8_t *key, size_t key_len, struct HMACSHA256Context *ctx)
39 uint8_t tk[SHA256_DIGEST_LENGTH];
41 /* if key is longer than 64 bytes reset it to key=HASH(key) */
47 SHA256_Update(&tctx, key, key_len);
48 SHA256_Final(tk, &tctx);
51 key_len = SHA256_DIGEST_LENGTH;
54 /* start out by storing key in pads */
55 ZERO_STRUCT(ctx->k_ipad);
56 ZERO_STRUCT(ctx->k_opad);
57 memcpy( ctx->k_ipad, key, key_len);
58 memcpy( ctx->k_opad, key, key_len);
60 /* XOR key with ipad and opad values */
63 ctx->k_ipad[i] ^= 0x36;
64 ctx->k_opad[i] ^= 0x5c;
67 SHA256_Init(&ctx->ctx);
68 SHA256_Update(&ctx->ctx, ctx->k_ipad, 64);
71 /***********************************************************************
72 update hmac_sha256 "inner" buffer
73 ***********************************************************************/
74 _PUBLIC_ void hmac_sha256_update(const uint8_t *data, size_t data_len, struct HMACSHA256Context *ctx)
76 SHA256_Update(&ctx->ctx, data, data_len); /* then text of datagram */
79 /***********************************************************************
80 finish off hmac_sha256 "inner" buffer and generate outer one.
81 ***********************************************************************/
82 _PUBLIC_ void hmac_sha256_final(uint8_t digest[SHA256_DIGEST_LENGTH], struct HMACSHA256Context *ctx)
86 SHA256_Final(digest, &ctx->ctx);
89 SHA256_Update(&ctx_o, ctx->k_opad, 64);
90 SHA256_Update(&ctx_o, digest, SHA256_DIGEST_LENGTH);
91 SHA256_Final(digest, &ctx_o);