source: branches/samba-3.0/source/lib/hmacmd5.c@ 352

Last change on this file since 352 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 4.2 KB
Line 
1/*
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
6
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.
11
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.
16
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.
20*/
21
22/* taken direct from rfc2104 implementation and modified for suitable use
23 * for ntlmv2.
24 */
25
26#include "includes.h"
27
28/***********************************************************************
29 the rfc 2104 version of hmac_md5 initialisation.
30***********************************************************************/
31
32void hmac_md5_init_rfc2104(const unsigned char *key, int key_len, HMACMD5Context *ctx)
33{
34 int i;
35 unsigned char tk[16];
36
37 /* if key is longer than 64 bytes reset it to key=MD5(key) */
38 if (key_len > 64) {
39 struct MD5Context tctx;
40
41 MD5Init(&tctx);
42 MD5Update(&tctx, key, key_len);
43 MD5Final(tk, &tctx);
44
45 key = tk;
46 key_len = 16;
47 }
48
49 /* start out by storing key in pads */
50 ZERO_STRUCT(ctx->k_ipad);
51 ZERO_STRUCT(ctx->k_opad);
52 memcpy( ctx->k_ipad, key, key_len);
53 memcpy( ctx->k_opad, key, key_len);
54
55 /* XOR key with ipad and opad values */
56 for (i=0; i<64; i++) {
57 ctx->k_ipad[i] ^= 0x36;
58 ctx->k_opad[i] ^= 0x5c;
59 }
60
61 MD5Init(&ctx->ctx);
62 MD5Update(&ctx->ctx, ctx->k_ipad, 64);
63}
64
65/***********************************************************************
66 the microsoft version of hmac_md5 initialisation.
67***********************************************************************/
68
69void hmac_md5_init_limK_to_64(const unsigned char* key, int key_len,
70 HMACMD5Context *ctx)
71{
72 int i;
73
74 /* if key is longer than 64 bytes truncate it */
75 if (key_len > 64) {
76 key_len = 64;
77 }
78
79 /* start out by storing key in pads */
80 ZERO_STRUCT(ctx->k_ipad);
81 ZERO_STRUCT(ctx->k_opad);
82 memcpy( ctx->k_ipad, key, key_len);
83 memcpy( ctx->k_opad, key, key_len);
84
85 /* XOR key with ipad and opad values */
86 for (i=0; i<64; i++) {
87 ctx->k_ipad[i] ^= 0x36;
88 ctx->k_opad[i] ^= 0x5c;
89 }
90
91 MD5Init(&ctx->ctx);
92 MD5Update(&ctx->ctx, ctx->k_ipad, 64);
93}
94
95/***********************************************************************
96 update hmac_md5 "inner" buffer
97***********************************************************************/
98
99void hmac_md5_update(const unsigned char *text, int text_len, HMACMD5Context *ctx)
100{
101 MD5Update(&ctx->ctx, text, text_len); /* then text of datagram */
102}
103
104/***********************************************************************
105 finish off hmac_md5 "inner" buffer and generate outer one.
106***********************************************************************/
107void hmac_md5_final(unsigned char *digest, HMACMD5Context *ctx)
108
109{
110 struct MD5Context ctx_o;
111
112 MD5Final(digest, &ctx->ctx);
113
114 MD5Init(&ctx_o);
115 MD5Update(&ctx_o, ctx->k_opad, 64);
116 MD5Update(&ctx_o, digest, 16);
117 MD5Final(digest, &ctx_o);
118}
119
120/***********************************************************
121 single function to calculate an HMAC MD5 digest from data.
122 use the microsoft hmacmd5 init method because the key is 16 bytes.
123************************************************************/
124
125void hmac_md5( unsigned char key[16], unsigned char *data, int data_len, unsigned char *digest)
126{
127 HMACMD5Context ctx;
128 hmac_md5_init_limK_to_64(key, 16, &ctx);
129 if (data_len != 0)
130 {
131 hmac_md5_update(data, data_len, &ctx);
132 }
133 hmac_md5_final(digest, &ctx);
134}
135
Note: See TracBrowser for help on using the repository browser.