hmac md5 c php,hmacmd5.c

/*

Unix SMB/CIFS implementation.

HMAC MD5 code for use in NTLMv2

Copyright (C) Luke Kenneth Casson Leighton 1996-2000

Copyright (C) Andrew Tridgell 1992-2000

This program is free software; you can redistribute it and/or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation; either version 2 of the License, or

(at your option) any later version.

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU General Public License for more details.

You should have received a copy of the GNU General Public License

along with this program; if not, write to the Free Software

Foundation, Inc.

Free Software Foundation

51 Franklin Street, Fifth Floor

Boston, MA 02110-1335

USA

Telephone: +1-617-542-5942

Fax: +1-617-542-2652

General email: info@fsf.org

*/

/* taken direct from rfc2104 implementation and modified for suitable use

* for ntlmv2.

*/

#ifdef LIBOPENSSL

#include "hmacmd5.h"

#include

#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))

/***********************************************************************

the rfc 2104 version of hmac_md5 initialisation.

***********************************************************************/

void hmac_md5_init_rfc2104(const unsigned char *key, int32_t key_len, HMACMD5Context *ctx) {

int32_t i;

unsigned char tk[16];

/* if key is longer than 64 bytes reset it to key=MD5(key) */

if (key_len > 64) {

MD5_CTX tctx;

MD5_Init(&tctx);

MD5_Update(&tctx, (void *)key, key_len);

MD5_Final(tk, &tctx);

key = tk;

key_len = 16;

}

/* start out by storing key in pads */

ZERO_STRUCT(ctx->k_ipad);

ZERO_STRUCT(ctx->k_opad);

memcpy(ctx->k_ipad, key, key_len);

memcpy(ctx->k_opad, key, key_len);

/* XOR key with ipad and opad values */

for (i = 0; i < 64; i++) {

ctx->k_ipad[i] ^= 0x36;

ctx->k_opad[i] ^= 0x5c;

}

MD5_Init(&ctx->ctx);

MD5_Update(&ctx->ctx, ctx->k_ipad, 64);

}

/***********************************************************************

the microsoft version of hmac_md5 initialisation.

***********************************************************************/

void hmac_md5_init_limK_to_64(const unsigned char *key, int32_t key_len, HMACMD5Context *ctx) {

int32_t i;

/* if key is longer than 64 bytes truncate it */

if (key_len > 64) {

key_len = 64;

}

/* start out by storing key in pads */

ZERO_STRUCT(ctx->k_ipad);

ZERO_STRUCT(ctx->k_opad);

memcpy(ctx->k_ipad, key, key_len);

memcpy(ctx->k_opad, key, key_len);

/* XOR key with ipad and opad values */

for (i = 0; i < 64; i++) {

ctx->k_ipad[i] ^= 0x36;

ctx->k_opad[i] ^= 0x5c;

}

MD5_Init(&ctx->ctx);

MD5_Update(&ctx->ctx, ctx->k_ipad, 64);

}

/***********************************************************************

update hmac_md5 "inner" buffer

***********************************************************************/

void hmac_md5_update(const unsigned char *text, int32_t text_len, HMACMD5Context *ctx) { MD5_Update(&ctx->ctx, (void *)text, text_len); /* then text of datagram */ }

/***********************************************************************

finish off hmac_md5 "inner" buffer and generate outer one.

***********************************************************************/

void hmac_md5_final(unsigned char *digest, HMACMD5Context *ctx) {

MD5_CTX ctx_o;

MD5_Final(digest, &ctx->ctx);

MD5_Init(&ctx_o);

MD5_Update(&ctx_o, ctx->k_opad, 64);

MD5_Update(&ctx_o, digest, 16);

MD5_Final(digest, &ctx_o);

}

/***********************************************************

single function to calculate an HMAC MD5 digest from data.

use the microsoft hmacmd5 init method because the key is 16 bytes.

************************************************************/

void hmac_md5(unsigned char key[16], unsigned char *data, int32_t data_len, unsigned char *digest) {

HMACMD5Context ctx;

hmac_md5_init_limK_to_64(key, 16, &ctx);

if (data_len != 0) {

hmac_md5_update(data, data_len, &ctx);

}

hmac_md5_final(digest, &ctx);

}

#endif

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HMAC(Hash-based Message Authentication Code)是一种基于散列函数的消息认证码。MD5(Message Digest Algorithm 5)是一种流行的散列函数,常用于生成消息摘要。Verilog是一种硬件描述语言,用于编写数字电路的模块化描述。 HMAC MD5 Verilog是指使用Verilog语言实现HMAC MD5算法。 HMAC MD5算法的步骤如下: 1. 初始化:设置密钥和填充常量,并进行预处理。 2. 转换:将消息分割成64字节的块,并进行一系列的变换操作。 3. 输出:输出最终的消息摘要。 在使用Verilog语言实现HMAC MD5算法时,我们可以按照以下步骤进行设计: 1. 设计输入和输出接口:设计模块的输入包括密钥和消息,输出为生成的消息摘要。 2. 实现预处理步骤:对输入的密钥和消息进行预处理,包括对密钥的长度进行调整和填充常量的设置。 3. 实现主要的转换操作:将消息分割成块并进行一系列的变换操作,包括将消息块进行扩展、散列函数的调用以及结果的累加操作。 4. 设计输出模块:输出最终的消息摘要。 5. 进行模块的集成和验证:将各个模块进行集成,并进行功能验证和性能测试。 通过按照以上步骤进行设计和实现,我们可以使用Verilog语言构建HMAC MD5算法的硬件模块。这样的设计可以在硬件上高效地进行HMAC MD5的计算任务,提高系统的安全性和性能。 总结:HMAC MD5 Verilog是指使用Verilog语言实现HMAC MD5算法。通过遵循HMAC MD5算法的步骤,我们可以设计和实现相应的硬件模块,从而在硬件上高效地运行HMAC MD5算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值