lib/hmacmd5.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    HMAC MD5 code for use in NTLMv2
00004    Copyright (C) Luke Kenneth Casson Leighton 1996-2000
00005    Copyright (C) Andrew Tridgell 1992-2000
00006    
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016    
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020 */
00021 
00022 /* taken direct from rfc2104 implementation and modified for suitable use
00023  * for ntlmv2.
00024  */
00025 
00026 #include "includes.h"
00027 
00028 /***********************************************************************
00029  the rfc 2104 version of hmac_md5 initialisation.
00030 ***********************************************************************/
00031 
00032 void hmac_md5_init_rfc2104(const unsigned char *key, int key_len, HMACMD5Context *ctx)
00033 {
00034         int i;
00035         unsigned char tk[16];
00036 
00037         /* if key is longer than 64 bytes reset it to key=MD5(key) */
00038         if (key_len > 64) {
00039                 struct MD5Context tctx;
00040 
00041                 MD5Init(&tctx);
00042                 MD5Update(&tctx, key, key_len);
00043                 MD5Final(tk, &tctx);
00044 
00045                 key = tk;
00046                 key_len = 16;
00047         }
00048 
00049         /* start out by storing key in pads */
00050         ZERO_STRUCT(ctx->k_ipad);
00051         ZERO_STRUCT(ctx->k_opad);
00052         memcpy( ctx->k_ipad, key, key_len);
00053         memcpy( ctx->k_opad, key, key_len);
00054 
00055         /* XOR key with ipad and opad values */
00056         for (i=0; i<64; i++) {
00057                 ctx->k_ipad[i] ^= 0x36;
00058                 ctx->k_opad[i] ^= 0x5c;
00059         }
00060 
00061         MD5Init(&ctx->ctx);
00062         MD5Update(&ctx->ctx, ctx->k_ipad, 64);  
00063 }
00064 
00065 /***********************************************************************
00066  the microsoft version of hmac_md5 initialisation.
00067 ***********************************************************************/
00068 
00069 void hmac_md5_init_limK_to_64(const unsigned char* key, int key_len,
00070                         HMACMD5Context *ctx)
00071 {
00072         int i;
00073 
00074         /* if key is longer than 64 bytes truncate it */
00075         if (key_len > 64) {
00076                 key_len = 64;
00077         }
00078 
00079         /* start out by storing key in pads */
00080         ZERO_STRUCT(ctx->k_ipad);
00081         ZERO_STRUCT(ctx->k_opad);
00082         memcpy( ctx->k_ipad, key, key_len);
00083         memcpy( ctx->k_opad, key, key_len);
00084 
00085         /* XOR key with ipad and opad values */
00086         for (i=0; i<64; i++) {
00087                 ctx->k_ipad[i] ^= 0x36;
00088                 ctx->k_opad[i] ^= 0x5c;
00089         }
00090 
00091         MD5Init(&ctx->ctx);
00092         MD5Update(&ctx->ctx, ctx->k_ipad, 64);  
00093 }
00094 
00095 /***********************************************************************
00096  update hmac_md5 "inner" buffer
00097 ***********************************************************************/
00098 
00099 void hmac_md5_update(const unsigned char *text, int text_len, HMACMD5Context *ctx)
00100 {
00101         MD5Update(&ctx->ctx, text, text_len); /* then text of datagram */
00102 }
00103 
00104 /***********************************************************************
00105  finish off hmac_md5 "inner" buffer and generate outer one.
00106 ***********************************************************************/
00107 void hmac_md5_final(unsigned char *digest, HMACMD5Context *ctx)
00108 
00109 {
00110         struct MD5Context ctx_o;
00111 
00112         MD5Final(digest, &ctx->ctx);          
00113 
00114         MD5Init(&ctx_o);
00115         MD5Update(&ctx_o, ctx->k_opad, 64);   
00116         MD5Update(&ctx_o, digest, 16); 
00117         MD5Final(digest, &ctx_o);
00118 }
00119 
00120 /***********************************************************
00121  single function to calculate an HMAC MD5 digest from data.
00122  use the microsoft hmacmd5 init method because the key is 16 bytes.
00123 ************************************************************/
00124 
00125 void hmac_md5( unsigned char key[16], unsigned char *data, int data_len, unsigned char *digest)
00126 {
00127         HMACMD5Context ctx;
00128         hmac_md5_init_limK_to_64(key, 16, &ctx);
00129         if (data_len != 0)
00130         {
00131                 hmac_md5_update(data, data_len, &ctx);
00132         }
00133         hmac_md5_final(digest, &ctx);
00134 }
00135 

Sambaに対してSat Aug 29 21:22:58 2009に生成されました。  doxygen 1.4.7