OpenVAS Libraries  9.0.3
hmacmd5.c
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21 
22 /* taken direct from rfc2104 implementation and modified for suitable use
23  * for ntlmv2.
24  */
25 
26 #include <string.h> /* for memset */
27 
28 #include "hmacmd5.h"
29 
33 void hmac_md5_init_limK_to_64(const uchar* key, int key_len,
34  HMACMD5Context *ctx)
35 {
36  int i;
37 
38  /* if key is longer than 64 bytes truncate it */
39  if (key_len > 64)
40  {
41  key_len = 64;
42  }
43 
44  /* start out by storing key in pads */
45  ZERO_STRUCT(ctx->k_ipad);
46  ZERO_STRUCT(ctx->k_opad);
47  memcpy( ctx->k_ipad, key, key_len);
48  memcpy( ctx->k_opad, key, key_len);
49 
50  /* XOR key with ipad and opad values */
51  for (i=0; i<64; i++) {
52  ctx->k_ipad[i] ^= 0x36;
53  ctx->k_opad[i] ^= 0x5c;
54  }
55 
56  MD5Init(&ctx->ctx);
57  MD5Update(&ctx->ctx, ctx->k_ipad, 64);
58 }
59 
63 void hmac_md5_update(const uchar* text, int text_len, HMACMD5Context *ctx)
64 {
65  MD5Update(&ctx->ctx, text, text_len); /* then text of datagram */
66 }
67 
71 void hmac_md5_final(uchar *digest, HMACMD5Context *ctx)
72 
73 {
74  struct MD5Context ctx_o;
75 
76  MD5Final(digest, &ctx->ctx);
77 
78  MD5Init(&ctx_o);
79  MD5Update(&ctx_o, ctx->k_opad, 64);
80  MD5Update(&ctx_o, digest, 16);
81  MD5Final(digest, &ctx_o);
82 }
83 
88 void hmac_md5( uchar key[16], uchar* data, int data_len, uchar* digest)
89 {
90  HMACMD5Context ctx;
91  hmac_md5_init_limK_to_64(key, 16, &ctx);
92  if (data_len != 0)
93  {
94  hmac_md5_update(data, data_len, &ctx);
95  }
96  hmac_md5_final(digest, &ctx);
97 }
uchar k_ipad[65]
Definition: hmacmd5.h:37
#define uchar
Definition: hmacmd5.h:28
void hmac_md5_init_limK_to_64(const uchar *key, int key_len, HMACMD5Context *ctx)
The microsoft version of hmac_md5 initialisation.
Definition: hmacmd5.c:33
void hmac_md5_final(uchar *digest, HMACMD5Context *ctx)
Finish off hmac_md5 "inner" buffer and generate outer one.
Definition: hmacmd5.c:71
struct MD5Context ctx
Definition: hmacmd5.h:36
void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
Definition: md5.c:107
void hmac_md5_update(const uchar *text, int text_len, HMACMD5Context *ctx)
Update hmac_md5 "inner" buffer.
Definition: hmacmd5.c:63
void MD5Init(struct MD5Context *ctx)
Definition: md5.c:44
void hmac_md5(uchar key[16], uchar *data, int data_len, uchar *digest)
Function to calculate an HMAC MD5 digest from data. Use the microsoft hmacmd5 init method because the...
Definition: hmacmd5.c:88
Definition: md5.h:46
void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
Definition: md5.c:59
#define ZERO_STRUCT(x)
Definition: genrand.c:65
uchar k_opad[65]
Definition: hmacmd5.h:38