2 Unix SMB/Netbios implementation.
4 HMAC MD5 code for use in NTLMv2
5 Copyright (C) Luke Kenneth Casson Leighton 1996-2000
6 Copyright (C) Andrew Tridgell 1992-2000
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.
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.
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.
23 /* taken direct from rfc2104 implementation and modified for suitable use
29 /***********************************************************************
30 the rfc 2104 version of hmac_md5 initialisation.
31 ***********************************************************************/
32 void hmac_md5_init_rfc2104(uchar* key, int key_len, HMACMD5Context *ctx)
36 /* if key is longer than 64 bytes reset it to key=MD5(key) */
40 struct MD5Context tctx;
43 MD5Update(&tctx, key, key_len);
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);
56 /* XOR key with ipad and opad values */
59 ctx->k_ipad[i] ^= 0x36;
60 ctx->k_opad[i] ^= 0x5c;
64 MD5Update(&ctx->ctx, ctx->k_ipad, 64);
67 /***********************************************************************
68 the microsoft version of hmac_md5 initialisation.
69 ***********************************************************************/
70 void hmac_md5_init_limK_to_64(const uchar* key, int key_len,
75 /* if key is longer than 64 bytes truncate it */
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);
87 /* XOR key with ipad and opad values */
90 ctx->k_ipad[i] ^= 0x36;
91 ctx->k_opad[i] ^= 0x5c;
95 MD5Update(&ctx->ctx, ctx->k_ipad, 64);
98 /***********************************************************************
99 update hmac_md5 "inner" buffer
100 ***********************************************************************/
101 void hmac_md5_update(const uchar* text, int text_len, HMACMD5Context *ctx)
103 MD5Update(&ctx->ctx, text, text_len); /* then text of datagram */
106 /***********************************************************************
107 finish off hmac_md5 "inner" buffer and generate outer one.
108 ***********************************************************************/
109 void hmac_md5_final(uchar *digest, HMACMD5Context *ctx)
112 struct MD5Context ctx_o;
114 MD5Final(digest, &ctx->ctx);
117 MD5Update(&ctx_o, ctx->k_opad, 64);
118 MD5Update(&ctx_o, digest, 16);
119 MD5Final(digest, &ctx_o);
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)
129 hmac_md5_init_limK_to_64(key, 16, &ctx);
132 hmac_md5_update(data, data_len, &ctx);
134 hmac_md5_final(digest, &ctx);