SMB2 signing now works. The spec was wrong (and will be fixed in the
[kai/samba.git] / source / lib / crypto / hmacsha256.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Interface header:    HMAC SHA-256 code
5
6    Copyright (C) Andrew Tridgell 2008
7
8    based in hmacsha1.c which is:
9      Copyright (C) Stefan Metzmacher
10    
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.
15    
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.
20    
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/>.
23 */
24
25 /*
26  taken direct from rfc2202 implementation and modified for suitable use
27  */
28
29 #include "includes.h"
30 #include "lib/crypto/crypto.h"
31 #include "heimdal/lib/hcrypto/sha.h"
32
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)
37 {
38         int i;
39         uint8_t tk[SHA256_DIGEST_LENGTH];
40
41         /* if key is longer than 64 bytes reset it to key=HASH(key) */
42         if (key_len > 64)
43         {
44                 SHA256_CTX tctx;
45
46                 SHA256_Init(&tctx);
47                 SHA256_Update(&tctx, key, key_len);
48                 SHA256_Final(tk, &tctx);
49
50                 key = tk;
51                 key_len = SHA256_DIGEST_LENGTH;
52         }
53
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);
59
60         /* XOR key with ipad and opad values */
61         for (i=0; i<64; i++)
62         {
63                 ctx->k_ipad[i] ^= 0x36;
64                 ctx->k_opad[i] ^= 0x5c;
65         }
66
67         SHA256_Init(&ctx->ctx);
68         SHA256_Update(&ctx->ctx, ctx->k_ipad, 64);  
69 }
70
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)
75 {
76         SHA256_Update(&ctx->ctx, data, data_len); /* then text of datagram */
77 }
78
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)
83 {
84         SHA256_CTX ctx_o;
85
86         SHA256_Final(digest, &ctx->ctx);
87
88         SHA256_Init(&ctx_o);
89         SHA256_Update(&ctx_o, ctx->k_opad, 64);
90         SHA256_Update(&ctx_o, digest, SHA256_DIGEST_LENGTH);
91         SHA256_Final(digest, &ctx_o);
92 }