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