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
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.
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.
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.
22 /* taken direct from rfc2104 implementation and modified for suitable use
28 /***********************************************************************
29 the rfc 2104 version of hmac_md5 initialisation.
30 ***********************************************************************/
31 void hmac_md5_init_rfc2104(uchar* key, int key_len, HMACMD5Context *ctx)
35 /* if key is longer than 64 bytes reset it to key=MD5(key) */
39 struct MD5Context tctx;
42 MD5Update(&tctx, key, key_len);
49 /* start out by storing key in pads */
50 ZERO_STRUCT(ctx->k_ipad);
51 ZERO_STRUCT(ctx->k_opad);
52 bcopy( key, ctx->k_ipad, key_len);
53 bcopy( key, ctx->k_opad, key_len);
55 /* XOR key with ipad and opad values */
58 ctx->k_ipad[i] ^= 0x36;
59 ctx->k_opad[i] ^= 0x5c;
63 MD5Update(&ctx->ctx, ctx->k_ipad, 64);
66 /***********************************************************************
67 the microsoft version of hmac_md5 initialisation.
68 ***********************************************************************/
69 void hmac_md5_init_limK_to_64(const uchar* key, int key_len,
74 /* if key is longer than 64 bytes truncate it */
80 /* start out by storing key in pads */
81 ZERO_STRUCT(ctx->k_ipad);
82 ZERO_STRUCT(ctx->k_opad);
83 bcopy( key, ctx->k_ipad, key_len);
84 bcopy( key, ctx->k_opad, key_len);
86 /* XOR key with ipad and opad values */
89 ctx->k_ipad[i] ^= 0x36;
90 ctx->k_opad[i] ^= 0x5c;
94 MD5Update(&ctx->ctx, ctx->k_ipad, 64);
97 /***********************************************************************
98 update hmac_md5 "inner" buffer
99 ***********************************************************************/
100 void hmac_md5_update(const uchar* text, int text_len, HMACMD5Context *ctx)
102 MD5Update(&ctx->ctx, text, text_len); /* then text of datagram */
105 /***********************************************************************
106 finish off hmac_md5 "inner" buffer and generate outer one.
107 ***********************************************************************/
108 void hmac_md5_final(uchar *digest, HMACMD5Context *ctx)
111 struct MD5Context ctx_o;
113 MD5Final(digest, &ctx->ctx);
116 MD5Update(&ctx_o, ctx->k_opad, 64);
117 MD5Update(&ctx_o, digest, 16);
118 MD5Final(digest, &ctx_o);
121 /***********************************************************
122 single function to calculate an HMAC MD5 digest from data.
123 use the microsoft hmacmd5 init method because the key is 16 bytes.
124 ************************************************************/
125 void hmac_md5( uchar key[16], uchar* data, int data_len, uchar* digest)
128 hmac_md5_init_limK_to_64(key, 16, &ctx);
131 hmac_md5_update(data, data_len, &ctx);
133 hmac_md5_final(digest, &ctx);