fixed word-order issue in password set and password change.
[Samba.git] / source / lib / hmacmd5.c
blob4e1cd3c9fdaabd11ca28d022aeea19f36d1d2d93
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Interface header: Scheduler service
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
24 * for ntlmv2.
27 #include "includes.h"
29 /***********************************************************************
30 the rfc 2104 version of hmac_md5 initialisation.
31 ***********************************************************************/
32 void hmac_md5_init_rfc2104(uchar* key, int key_len, HMACMD5Context *ctx)
34 int i;
36 /* if key is longer than 64 bytes reset it to key=MD5(key) */
37 if (key_len > 64)
39 uchar tk[16];
40 struct MD5Context tctx;
42 MD5Init(&tctx);
43 MD5Update(&tctx, key, key_len);
44 MD5Final(tk, &tctx);
46 key = tk;
47 key_len = 16;
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);
56 /* XOR key with ipad and opad values */
57 for (i=0; i<64; i++)
59 ctx->k_ipad[i] ^= 0x36;
60 ctx->k_opad[i] ^= 0x5c;
63 MD5Init(&ctx->ctx);
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,
71 HMACMD5Context *ctx)
73 int i;
75 /* if key is longer than 64 bytes truncate it */
76 if (key_len > 64)
78 key_len = 64;
81 /* start out by storing key in pads */
82 bzero( ctx->k_ipad, sizeof ctx->k_ipad);
83 bzero( ctx->k_opad, sizeof 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 */
88 for (i=0; i<64; i++)
90 ctx->k_ipad[i] ^= 0x36;
91 ctx->k_opad[i] ^= 0x5c;
94 MD5Init(&ctx->ctx);
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);
116 MD5Init(&ctx_o);
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)
128 HMACMD5Context ctx;
129 hmac_md5_init_limK_to_64(key, 16, &ctx);
130 if (data_len != 0)
132 hmac_md5_update(data, data_len, &ctx);
134 hmac_md5_final(digest, &ctx);