OpenVAS Scanner  7.0.1~git
hmacmd5.c
Go to the documentation of this file.
1 /* Copyright (C) Luke Kenneth Casson Leighton 1996-2000
2  * Copyright (C) Andrew Tridgell 1992-2000
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
29 #include "hmacmd5.h"
30 
31 #include <string.h> /* for memset */
32 
36 void
37 hmac_md5_init_limK_to_64 (const uchar *key, int key_len, HMACMD5Context *ctx)
38 {
39  int i;
40 
41  /* if key is longer than 64 bytes truncate it */
42  if (key_len > 64)
43  {
44  key_len = 64;
45  }
46 
47  /* start out by storing key in pads */
48  ZERO_STRUCT (ctx->k_ipad);
49  ZERO_STRUCT (ctx->k_opad);
50  memcpy (ctx->k_ipad, key, key_len);
51  memcpy (ctx->k_opad, key, key_len);
52 
53  /* XOR key with ipad and opad values */
54  for (i = 0; i < 64; i++)
55  {
56  ctx->k_ipad[i] ^= 0x36;
57  ctx->k_opad[i] ^= 0x5c;
58  }
59 
60  MD5Init (&ctx->ctx);
61  MD5Update (&ctx->ctx, ctx->k_ipad, 64);
62 }
63 
67 void
68 hmac_md5_update (const uchar *text, int text_len, HMACMD5Context *ctx)
69 {
70  MD5Update (&ctx->ctx, text, text_len); /* then text of datagram */
71 }
72 
76 void
78 
79 {
80  struct MD5Context ctx_o;
81 
82  MD5Final (digest, &ctx->ctx);
83 
84  MD5Init (&ctx_o);
85  MD5Update (&ctx_o, ctx->k_opad, 64);
86  MD5Update (&ctx_o, digest, 16);
87  MD5Final (digest, &ctx_o);
88 }
89 
94 void
95 hmac_md5 (uchar key[16], uchar *data, int data_len, uchar *digest)
96 {
97  HMACMD5Context ctx;
98  hmac_md5_init_limK_to_64 (key, 16, &ctx);
99  if (data_len != 0)
100  {
101  hmac_md5_update (data, data_len, &ctx);
102  }
103  hmac_md5_final (digest, &ctx);
104 }
HMACMD5Context
Definition: hmacmd5.h:41
ZERO_STRUCT
#define ZERO_STRUCT(x)
Definition: genrand.c:70
uchar
#define uchar
Definition: hmacmd5.h:35
HMACMD5Context::k_ipad
uchar k_ipad[65]
Definition: hmacmd5.h:44
hmac_md5_init_limK_to_64
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:37
MD5Update
void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
Definition: md5.c:66
hmac_md5_update
void hmac_md5_update(const uchar *text, int text_len, HMACMD5Context *ctx)
Update hmac_md5 "inner" buffer.
Definition: hmacmd5.c:68
HMACMD5Context::k_opad
uchar k_opad[65]
Definition: hmacmd5.h:45
hmacmd5.h
Unix SMB/CIFS implementation. HMAC MD5 code for use in NTLMv2.
hmac_md5_final
void hmac_md5_final(uchar *digest, HMACMD5Context *ctx)
Finish off hmac_md5 "inner" buffer and generate outer one.
Definition: hmacmd5.c:77
MD5Init
void MD5Init(struct MD5Context *ctx)
Definition: md5.c:50
hmac_md5
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:95
MD5Context
Definition: md5.h:46
MD5Final
void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
Definition: md5.c:118
HMACMD5Context::ctx
struct MD5Context ctx
Definition: hmacmd5.h:43