📄 radius_message_digest.c
字号:
/* radius_message_digest.c *//* Performs HMAC_MD5 algorithm *//* Copyright 1984 - 2000 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history____________________01a,14mar02,md create for EAP support*/#ifdef __EAP__#include <string.h>#include <vxWorks.h>#include <rwos.h>#include "radius_message_digest.h"static BYTE MD5_PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};/****************************************************************************/static void MD_init (MD_CONTEXT *sptr_context);static void MD_update (MD_CONTEXT *sptr_context, BYTE *input, ULONG inputLen, enum MD_ALGORITHM algorithm);static void MD_final (BYTE *bptr_digest,MD_CONTEXT *sptr_context, enum MD_ALGORITHM algorithm);static void MD4_transform (ULONG state[4], BYTE block[64]);static void MD5_transform (ULONG state[4], BYTE block[64]);static void MD_encode (BYTE *bptr_output, ULONG *ulptr_input, ULONG length);static void MD_decode (ULONG *ulptr_output, BYTE *bptr_input, ULONG length);static void MD_memcpy (BYTE *bptr_output,BYTE *bptr_input,ULONG length);static void MD_memset (BYTE *bptr_output,int value,ULONG length);/****************************************************************************/void radius_hmac_md5(text, text_len, key, key_len, digest)unsigned char* text; /* pointer to data stream */int text_len; /* length of data stream */unsigned char* key; /* pointer to authentication key */int key_len; /* length of authentication key */caddr_t digest; /* caller digest to be filled in */{ MD_CONTEXT context; unsigned char k_ipad[65]; /* inner padding - * key XORd with ipad */ unsigned char k_opad[65]; /* outer padding - * key XORd with opad */ unsigned char tk[16]; int i; /* if key is longer than 64 bytes reset it to key=MD5(key) */ if (key_len > 64) { MD_CONTEXT tctx; MD_init(&tctx); MD_update(&tctx, key, key_len, MD5); MD_final(tk, &tctx, MD5); key = tk; key_len = 16; } /* * the HMAC_MD5 transform looks like: * * MD5(K XOR opad, MD5(K XOR ipad, text)) * * where K is an n byte key * ipad is the byte 0x36 repeated 64 times * opad is the byte 0x5c repeated 64 times * and text is the data being protected */ /* start out by storing key in pads */ bzero( k_ipad, sizeof k_ipad); bzero( k_opad, sizeof k_opad); bcopy( key, k_ipad, key_len); bcopy( key, k_opad, key_len); /* XOR key with ipad and opad values */ for (i=0; i<64; i++) { k_ipad[i] ^= 0x36; k_opad[i] ^= 0x5c; } /* * perform inner MD5 */ MD_init(&context); /* init context for 1st * pass */ MD_update(&context, k_ipad, 64, MD5); /* start with inner pad */ MD_update(&context, text, text_len, MD5); /* then text of datagram */ MD_final(digest, &context, MD5); /* finish up 1st pass */ /* * perform outer MD5 */ MD_init(&context); /* init context for 2nd * pass */ MD_update(&context, k_opad, 64, MD5); /* start with outer pad */ MD_update(&context, digest, 16, MD5); /* then results of 1st * hash */ MD_final(digest, &context, MD5); /* finish up 2nd pass */}/*****************************************************************************//* MD initialization. Begins an MD operation, writing a new sptr_context. *//*****************************************************************************/static void MD_init (MD_CONTEXT *sptr_context){ sptr_context->count[0] = 0x00000000L; sptr_context->count[1] = 0x00000000L; /* Load magic initialization constants.*/ sptr_context->state[0] = 0x67452301; sptr_context->state[1] = 0xefcdab89; sptr_context->state[2] = 0x98badcfe; sptr_context->state[3] = 0x10325476;}/****************************************************************************//* MD block update operation. Continues an MD message-digest operation, processing another message block, and updating the sptr_context. *//****************************************************************************/static void MD_update (MD_CONTEXT *sptr_context, BYTE *bptr_input, ULONG input_length, enum MD_ALGORITHM algorithm) /* sptr_context, input block and length of input block and algorithm to use */{ ULONG i; ULONG index; ULONG part_length; i = 0x00000000L; /* Compute number of bytes mod 64 */ index = (ULONG)((sptr_context->count[0] >> 3) & 0x3F); /* Update number of bits */ if ((sptr_context->count[0] += ((ULONG)input_length << 3)) < ((ULONG)input_length << 3)) { sptr_context->count[1]++; } sptr_context->count[1] += ((ULONG)input_length >> 29); part_length = 64 - index; /* Transform as many times as possible.*/ if (input_length >= part_length) { MD_memcpy ((BYTE *)&sptr_context->buffer[index], (BYTE *)bptr_input, part_length); switch (algorithm) { case MD4: MD4_transform (sptr_context->state, sptr_context->buffer); for (i = part_length; (i + 63) < input_length; i += 64) { MD4_transform (sptr_context->state, &bptr_input[i]); } break; case MD5: MD5_transform (sptr_context->state, sptr_context->buffer); for (i = part_length; (i + 63) < input_length; i += 64) { MD5_transform (sptr_context->state, &bptr_input[i]); } break; default: /* error print */ break; } index = 0x00000000L; } else { i = 0x00000000L; }/* Buffer remaining input */ MD_memcpy ((BYTE *)&sptr_context->buffer[index],(BYTE *)&bptr_input[i],input_length-i);}/****************************************************************************//* MD finalization. Ends an MD5 message-digest operation, writing the message digest and zeroizing the sptr_context. *//****************************************************************************/static void MD_final (BYTE *bptr_digest,MD_CONTEXT *sptr_context, enum MD_ALGORITHM algorithm){ BYTE bits[8]; ULONG index; ULONG padding_length; /* Save number of bits */ MD_encode (bits, sptr_context->count, 8); /* Pad out to 56 mod 64.*/ index = (ULONG)((sptr_context->count[0] >> 3) & 0x3f); padding_length = (index < 56) ? (56 - index) : (120 - index); MD_update (sptr_context, MD5_PADDING, padding_length, algorithm); /* Append length (before padding) */ MD_update (sptr_context, bits, 8, algorithm); /* Store state in digest */ MD_encode (bptr_digest, sptr_context->state, 16); /* Zeroize sensitive information.*/ MD_memset ((BYTE *)sptr_context, 0, sizeof (*sptr_context));}/**********************************************************************//* MD4 Basic transform. Transforms state based on block. *//**********************************************************************/static void MD4_transform (ULONG state[4],BYTE block[64]){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -