md5 and hmac_md5
[nivanova/samba-autobuild/.git] / source3 / lib / hmacmd5.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Interface header: Scheduler service
5    Copyright (C) Luke Kenneth Casson Leighton 1996-1999
6    Copyright (C) Andrew Tridgell 1992-1999
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         bzero( ctx->k_ipad, sizeof ctx->k_ipad);
52         bzero( ctx->k_opad, sizeof 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(uchar*  key, int key_len, HMACMD5Context *ctx)
71 {
72         int i;
73
74         /* if key is longer than 64 bytes truncate it */
75         if (key_len > 64)
76         {
77                 key_len = 64;
78         }
79
80         /* start out by storing key in pads */
81         bzero( ctx->k_ipad, sizeof ctx->k_ipad);
82         bzero( ctx->k_opad, sizeof ctx->k_opad);
83         bcopy( key, ctx->k_ipad, key_len);
84         bcopy( key, ctx->k_opad, key_len);
85
86         /* XOR key with ipad and opad values */
87         for (i=0; i<64; i++)
88         {
89                 ctx->k_ipad[i] ^= 0x36;
90                 ctx->k_opad[i] ^= 0x5c;
91         }
92
93         MD5Init(&ctx->ctx);
94         MD5Update(&ctx->ctx, ctx->k_ipad, 64);  
95 }
96
97 /***********************************************************************
98  update hmac_md5 "inner" buffer
99 ***********************************************************************/
100 void hmac_md5_update(uchar* text, int text_len, HMACMD5Context *ctx)
101 {
102         MD5Update(&ctx->ctx, text, text_len); /* then text of datagram */
103 }
104
105 /***********************************************************************
106  finish off hmac_md5 "inner" buffer and generate outer one.
107 ***********************************************************************/
108 void hmac_md5_final(caddr_t digest, HMACMD5Context *ctx)
109
110 {
111         struct MD5Context ctx_o;
112
113         MD5Final(digest, &ctx->ctx);          
114
115         MD5Init(&ctx_o);
116         MD5Update(&ctx_o, ctx->k_opad, 64);   
117         MD5Update(&ctx_o, digest, 16); 
118         MD5Final(digest, &ctx_o);
119 }