149a4b861c194a3bc561b12b7aa5149b491ef769
[metze/samba/wip.git] / source4 / lib / crypto / hmacmd5.c
1 /* 
2    Unix SMB/CIFS implementation.
3    HMAC MD5 code for use in NTLMv2
4    Copyright (C) Luke Kenneth Casson Leighton 1996-2000
5    Copyright (C) Andrew Tridgell 1992-2000
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /* taken direct from rfc2104 implementation and modified for suitable use
23  * for ntlmv2.
24  */
25
26 #include "includes.h"
27 #include "lib/crypto/crypto.h"
28
29 /***********************************************************************
30  the rfc 2104 version of hmac_md5 initialisation.
31 ***********************************************************************/
32 _PUBLIC_ void hmac_md5_init_rfc2104(const uint8_t *key, int key_len, HMACMD5Context *ctx)
33 {
34         int i;
35         uint8_t tk[16];
36
37         /* if key is longer than 64 bytes reset it to key=MD5(key) */
38         if (key_len > 64)
39         {
40                 struct MD5Context tctx;
41
42                 MD5Init(&tctx);
43                 MD5Update(&tctx, key, key_len);
44                 MD5Final(tk, &tctx);
45
46                 key = tk;
47                 key_len = 16;
48         }
49
50         /* start out by storing key in pads */
51         ZERO_STRUCT(ctx->k_ipad);
52         ZERO_STRUCT(ctx->k_opad);
53         memcpy( ctx->k_ipad, key, key_len);
54         memcpy( ctx->k_opad, key, key_len);
55
56         /* XOR key with ipad and opad values */
57         for (i=0; i<64; i++)
58         {
59                 ctx->k_ipad[i] ^= 0x36;
60                 ctx->k_opad[i] ^= 0x5c;
61         }
62
63         MD5Init(&ctx->ctx);
64         MD5Update(&ctx->ctx, ctx->k_ipad, 64);  
65 }
66
67 /***********************************************************************
68  the microsoft version of hmac_md5 initialisation.
69 ***********************************************************************/
70 _PUBLIC_ void hmac_md5_init_limK_to_64(const uint8_t *key, int key_len,
71                         HMACMD5Context *ctx)
72 {
73         /* if key is longer than 64 bytes truncate it */
74         if (key_len > 64)
75         {
76                 key_len = 64;
77         }
78
79         hmac_md5_init_rfc2104(key, key_len, ctx);
80 }
81
82 /***********************************************************************
83  update hmac_md5 "inner" buffer
84 ***********************************************************************/
85 _PUBLIC_ void hmac_md5_update(const uint8_t *text, int text_len, HMACMD5Context *ctx)
86 {
87         MD5Update(&ctx->ctx, text, text_len); /* then text of datagram */
88 }
89
90 /***********************************************************************
91  finish off hmac_md5 "inner" buffer and generate outer one.
92 ***********************************************************************/
93 _PUBLIC_ void hmac_md5_final(uint8_t *digest, HMACMD5Context *ctx)
94 {
95         struct MD5Context ctx_o;
96
97         MD5Final(digest, &ctx->ctx);          
98
99         MD5Init(&ctx_o);
100         MD5Update(&ctx_o, ctx->k_opad, 64);   
101         MD5Update(&ctx_o, digest, 16); 
102         MD5Final(digest, &ctx_o);
103 }
104
105 /***********************************************************
106  single function to calculate an HMAC MD5 digest from data.
107  use the microsoft hmacmd5 init method because the key is 16 bytes.
108 ************************************************************/
109 _PUBLIC_ void hmac_md5(const uint8_t key[16], const uint8_t *data, int data_len, uint8_t *digest)
110 {
111         HMACMD5Context ctx;
112         hmac_md5_init_limK_to_64(key, 16, &ctx);
113         if (data_len != 0)
114         {
115                 hmac_md5_update(data, data_len, &ctx);
116         }
117         hmac_md5_final(digest, &ctx);
118 }