s3:build fix autoconf build on RHEL5
[Samba/bb.git] / lib / crypto / hmacmd5.c
blobcfbd428014baa8ae791e49517570dae077ab1045
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
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /* taken direct from rfc2104 implementation and modified for suitable use
22 * for ntlmv2.
25 #include "replace.h"
26 #include "../lib/crypto/hmacmd5.h"
28 /***********************************************************************
29 the rfc 2104 version of hmac_md5 initialisation.
30 ***********************************************************************/
31 _PUBLIC_ void hmac_md5_init_rfc2104(const uint8_t *key, int key_len, HMACMD5Context *ctx)
33 int i;
34 uint8_t tk[16];
36 /* if key is longer than 64 bytes reset it to key=MD5(key) */
37 if (key_len > 64)
39 struct MD5Context tctx;
41 MD5Init(&tctx);
42 MD5Update(&tctx, key, key_len);
43 MD5Final(tk, &tctx);
45 key = tk;
46 key_len = 16;
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);
55 /* XOR key with ipad and opad values */
56 for (i=0; i<64; i++)
58 ctx->k_ipad[i] ^= 0x36;
59 ctx->k_opad[i] ^= 0x5c;
62 MD5Init(&ctx->ctx);
63 MD5Update(&ctx->ctx, ctx->k_ipad, 64);
66 /***********************************************************************
67 the microsoft version of hmac_md5 initialisation.
68 ***********************************************************************/
69 _PUBLIC_ void hmac_md5_init_limK_to_64(const uint8_t *key, int key_len,
70 HMACMD5Context *ctx)
72 /* if key is longer than 64 bytes truncate it */
73 if (key_len > 64)
75 key_len = 64;
78 hmac_md5_init_rfc2104(key, key_len, ctx);
81 /***********************************************************************
82 update hmac_md5 "inner" buffer
83 ***********************************************************************/
84 _PUBLIC_ void hmac_md5_update(const uint8_t *text, int text_len, HMACMD5Context *ctx)
86 MD5Update(&ctx->ctx, text, text_len); /* then text of datagram */
89 /***********************************************************************
90 finish off hmac_md5 "inner" buffer and generate outer one.
91 ***********************************************************************/
92 _PUBLIC_ void hmac_md5_final(uint8_t *digest, HMACMD5Context *ctx)
94 struct MD5Context ctx_o;
96 MD5Final(digest, &ctx->ctx);
98 MD5Init(&ctx_o);
99 MD5Update(&ctx_o, ctx->k_opad, 64);
100 MD5Update(&ctx_o, digest, 16);
101 MD5Final(digest, &ctx_o);
104 /***********************************************************
105 single function to calculate an HMAC MD5 digest from data.
106 use the microsoft hmacmd5 init method because the key is 16 bytes.
107 ************************************************************/
108 _PUBLIC_ void hmac_md5(const uint8_t key[16], const uint8_t *data, int data_len, uint8_t *digest)
110 HMACMD5Context ctx;
111 hmac_md5_init_limK_to_64(key, 16, &ctx);
112 if (data_len != 0)
114 hmac_md5_update(data, data_len, &ctx);
116 hmac_md5_final(digest, &ctx);