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