首页 > 编程语言 >HMAC与Hash算法——C语言实现

HMAC与Hash算法——C语言实现

时间:2024-06-20 21:22:27浏览次数:26  
标签:uint32 SHA256 Hash C语言 len HMAC sha256 buf hmac

hash算法是HMac的Mac

 

hmacsha256.h

 1 /**
 2  * @file hmacsha256.h
 3  * @author your name ([email protected])
 4  * @brief 
 5  * @version 0.1
 6  * @date 2024-06-20
 7  * 
 8  * @copyright Copyright (c) 2024
 9  * 
10  */
11 
12 #ifndef _HMAC_SHA_256_H_
13 #define _HMAC_SHA_256_H_
14  
15 #define SHA256_BLOCKLEN  64ul //size of message block buffer
16 #define SHA256_DIGESTLEN 32ul //size of digest in uint8_t
17 #define SHA256_DIGESTINT 8ul  //size of digest in uint32_t
18  
19 #include <stdint.h>
20 #include <string.h>
21  
22 typedef struct sha256_ctx_t
23 {
24     uint64_t len;                 // processed message length
25     uint32_t h[SHA256_DIGESTINT]; // hash state
26     uint8_t buf[SHA256_BLOCKLEN]; // message block buffer
27 } SHA256_CTX;
28  
29 void sha256_init(SHA256_CTX *ctx);
30 void sha256_update(SHA256_CTX *ctx, const uint8_t *m, uint32_t mlen);
31 // resets state: calls sha256_init
32 void sha256_final(SHA256_CTX *ctx, uint8_t *md);
33  
34 typedef struct hmac_sha256_ctx_t
35 {
36     uint8_t buf[SHA256_BLOCKLEN]; // key block buffer, not needed after init
37     uint32_t h_inner[SHA256_DIGESTINT];
38     uint32_t h_outer[SHA256_DIGESTINT];
39     SHA256_CTX sha;
40 } HMAC_SHA256_CTX;
41  
42 void hmac_sha256_init(HMAC_SHA256_CTX *hmac, const uint8_t *key, uint32_t keylen);
43 void hmac_sha256_update(HMAC_SHA256_CTX *hmac, const uint8_t *m, uint32_t mlen);
44 void hmac_sha256_final(HMAC_SHA256_CTX *hmac, uint8_t *md);
45  
46 void pbkdf2_sha256(HMAC_SHA256_CTX *ctx,const uint8_t *key, uint32_t keylen, const uint8_t *salt, uint32_t saltlen, uint32_t rounds,uint8_t *dk, uint32_t dklen);
47  
48 #endif // _HMAC_SHA_256_H_

 

hmachas256.c

  1 /**
  2  * @file hmacsha256.c
  3  * @author your name ([email protected])
  4  * @brief 
  5  * @version 0.1
  6  * @date 2024-06-20
  7  * 
  8  * @copyright Copyright (c) 2024
  9  * 
 10  */
 11 
 12 #include "hmacsha256.h"
 13 
 14 static uint32_t ror(uint32_t n, uint32_t k)
 15 {
 16     return (n >> k) | (n << (32 - k));
 17 }
 18  
 19 #define ROR(n,k) ror(n,k)
 20  
 21 #define CH(x,y,z)  (z ^ (x & (y ^ z)))
 22 #define MAJ(x,y,z) ((x & y) | (z & (x | y)))
 23 #define S0(x)      (ROR(x, 2) ^ ROR(x,13) ^ ROR(x,22))
 24 #define S1(x)      (ROR(x, 6) ^ ROR(x,11) ^ ROR(x,25))
 25 #define R0(x)      (ROR(x, 7) ^ ROR(x,18) ^ (x>>3))
 26 #define R1(x)      (ROR(x,17) ^ ROR(x,19) ^ (x>>10))
 27  
 28 static const uint32_t K[64] =
 29 {
 30     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
 31     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
 32     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
 33     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
 34     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
 35     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
 36     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
 37     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
 38 };
 39  
 40 static void sha256_transform(SHA256_CTX *s, const uint8_t *buf)
 41 {
 42     uint32_t t1, t2, a, b, c, d, e, f, g, h, m[64];
 43     uint32_t i, j;
 44     
 45     for (i = 0, j = 0; i < 16; i++, j += 4)
 46     {
 47         m[i] = (uint32_t) buf[j] << 24 | (uint32_t) buf[j + 1] << 16 |
 48                (uint32_t) buf[j + 2] << 8 | (uint32_t) buf[j + 3];
 49     }
 50     for (; i < 64; i++)
 51     {
 52         m[i] = R1(m[i - 2]) + m[i - 7] + R0(m[i - 15]) + m[i - 16];
 53     }
 54     a = s->h[0];
 55     b = s->h[1];
 56     c = s->h[2];
 57     d = s->h[3];
 58     e = s->h[4];
 59     f = s->h[5];
 60     g = s->h[6];
 61     h = s->h[7];
 62     for (i = 0; i < 64; i++)
 63     {
 64         t1 = h + S1(e) + CH(e, f, g) + K[i] + m[i];
 65         t2 = S0(a) + MAJ(a, b, c);
 66         h = g;
 67         g = f;
 68         f = e;
 69         e = d + t1;
 70         d = c;
 71         c = b;
 72         b = a;
 73         a = t1 + t2;
 74     }
 75     s->h[0] += a;
 76     s->h[1] += b;
 77     s->h[2] += c;
 78     s->h[3] += d;
 79     s->h[4] += e;
 80     s->h[5] += f;
 81     s->h[6] += g;
 82     s->h[7] += h;
 83 }
 84  
 85 void sha256_init(SHA256_CTX *s)
 86 {
 87     s->len = 0;
 88     
 89     s->h[0] = 0x6a09e667;
 90     s->h[1] = 0xbb67ae85;
 91     s->h[2] = 0x3c6ef372;
 92     s->h[3] = 0xa54ff53a;
 93     s->h[4] = 0x510e527f;
 94     s->h[5] = 0x9b05688c;
 95     s->h[6] = 0x1f83d9ab;
 96     s->h[7] = 0x5be0cd19;
 97 }
 98  
 99 void sha256_final(SHA256_CTX *s, uint8_t *md)
100 {
101     uint32_t r = s->len % SHA256_BLOCKLEN;
102     int i;
103     
104     //pad
105     s->buf[r++] = 0x80;
106     if (r > 56)
107     {
108         memset(s->buf + r, 0, SHA256_BLOCKLEN - r);
109         r = 0;
110         sha256_transform(s, s->buf);
111     }
112     memset(s->buf + r, 0, 56 - r);
113     s->len *= 8;
114     s->buf[56] = s->len >> 56;
115     s->buf[57] = s->len >> 48;
116     s->buf[58] = s->len >> 40;
117     s->buf[59] = s->len >> 32;
118     s->buf[60] = s->len >> 24;
119     s->buf[61] = s->len >> 16;
120     s->buf[62] = s->len >> 8;
121     s->buf[63] = s->len;
122     sha256_transform(s, s->buf);
123     
124     for (i = 0; i < SHA256_DIGESTINT; i++)
125     {
126         md[4 * i    ] = s->h[i] >> 24;
127         md[4 * i + 1] = s->h[i] >> 16;
128         md[4 * i + 2] = s->h[i] >> 8;
129         md[4 * i + 3] = s->h[i];
130     }
131     sha256_init(s);
132 }
133  
134 void sha256_update(SHA256_CTX *s, const uint8_t *m, uint32_t len)
135 {
136     const uint8_t *p = m;
137     uint32_t r = s->len % SHA256_BLOCKLEN;
138     
139     s->len += len;
140     if (r)
141     {
142         if (len + r < SHA256_BLOCKLEN)
143         {
144             memcpy(s->buf + r, p, len);
145             return;
146         }
147         memcpy(s->buf + r, p, SHA256_BLOCKLEN - r);
148         len -= SHA256_BLOCKLEN - r;
149         p += SHA256_BLOCKLEN - r;
150         sha256_transform(s, s->buf);
151     }
152     for (; len >= SHA256_BLOCKLEN; len -= SHA256_BLOCKLEN, p += SHA256_BLOCKLEN)
153     {
154         sha256_transform(s, p);
155     }
156     memcpy(s->buf, p, len);
157 }
158  
159 #define INNER_PAD '\x36'
160 #define OUTER_PAD '\x5c'
161  
162 void hmac_sha256_init(HMAC_SHA256_CTX *hmac, const uint8_t *key, uint32_t keylen)
163 {
164     SHA256_CTX *sha = &hmac->sha;
165     uint32_t i;
166     
167     if (keylen <= SHA256_BLOCKLEN)
168     {
169         memcpy(hmac->buf, key, keylen);
170         memset(hmac->buf + keylen, '\0', SHA256_BLOCKLEN - keylen);
171     }
172     else
173     {
174         sha256_init(sha);
175         sha256_update(sha, key, keylen);
176         sha256_final(sha, hmac->buf);
177         memset(hmac->buf + SHA256_DIGESTLEN, '\0', SHA256_BLOCKLEN - SHA256_DIGESTLEN);
178     }
179     
180     for (i = 0; i < SHA256_BLOCKLEN; i++)
181     {
182         hmac->buf[ i ] = hmac->buf[ i ] ^ OUTER_PAD;
183     }
184     
185     sha256_init(sha);
186     sha256_update(sha, hmac->buf, SHA256_BLOCKLEN);
187     // copy outer state
188     memcpy(hmac->h_outer, sha->h, SHA256_DIGESTLEN);
189     
190     for (i = 0; i < SHA256_BLOCKLEN; i++)
191     {
192         hmac->buf[ i ] = (hmac->buf[ i ] ^ OUTER_PAD) ^ INNER_PAD;
193     }
194     
195     sha256_init(sha);
196     sha256_update(sha, hmac->buf, SHA256_BLOCKLEN);
197     // copy inner state
198     memcpy(hmac->h_inner, sha->h, SHA256_DIGESTLEN);
199 }
200  
201 void hmac_sha256_update(HMAC_SHA256_CTX *hmac, const uint8_t *m, uint32_t mlen)
202 {
203     sha256_update(&hmac->sha, m, mlen);
204 }
205  
206 void hmac_sha256_final(HMAC_SHA256_CTX *hmac, uint8_t *md)
207 {
208     SHA256_CTX *sha = &hmac->sha;
209     sha256_final(sha, md);
210     
211     // reset sha to outer state
212     memcpy(sha->h, hmac->h_outer, SHA256_DIGESTLEN);
213     sha->len = SHA256_BLOCKLEN;
214     
215     sha256_update(sha, md, SHA256_DIGESTLEN);
216     sha256_final(sha, md); // md = D(outer || D(inner || msg))
217     
218     // reset sha to inner state -> reset hmac
219     memcpy(sha->h, hmac->h_inner, SHA256_DIGESTLEN);
220     sha->len = SHA256_BLOCKLEN;
221 }
222  
223 void pbkdf2_sha256(HMAC_SHA256_CTX *hmac,
224     const uint8_t *key, uint32_t keylen, const uint8_t *salt, uint32_t saltlen, uint32_t rounds,
225     uint8_t *dk, uint32_t dklen)
226 {
227     uint8_t *U;
228     uint8_t *T;
229     uint8_t count[4];
230     uint32_t i, j, k;
231     uint32_t len;
232  
233     uint32_t hlen = SHA256_DIGESTLEN;
234     uint32_t l = dklen / hlen + ((dklen % hlen) ? 1 : 0);
235     uint32_t r = dklen - (l - 1) * hlen;
236     
237     hmac_sha256_init(hmac, key, keylen);
238     
239     U = hmac->buf;
240     T = dk;
241     
242     len = hlen;
243     for (i = 1; i <= l; i++)
244     {
245         if (i == l) { len = r; }
246         count[0] = (i >> 24) & 0xFF;
247         count[1] = (i >> 16) & 0xFF;
248         count[2] = (i >>  8) & 0xFF;
249         count[3] = (i) & 0xFF;
250         hmac_sha256_update(hmac, salt, saltlen);
251         hmac_sha256_update(hmac, count, 4);
252         hmac_sha256_final(hmac, U);
253         memcpy(T, U, len);
254         for (j = 1; j < rounds; j++)
255         {
256             hmac_sha256_update(hmac, U, hlen);
257             hmac_sha256_final(hmac, U);
258             for (k = 0; k < len; k++)
259             {
260                 T[k] ^= U[k];
261             }
262         }
263         T += len;
264     }
265     
266 }

 

 main.c

 1 /**
 2  * @file main.c
 3  * @author your name ([email protected])
 4  * @brief 
 5  * @version 0.1
 6  * @date 2024-06-20
 7  * 
 8  * @copyright Copyright (c) 2024
 9  * 
10  */
11 
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "hmacsha256.h"
16 
17 /**
18  * @brief           计算hmac值,基于sha256
19  * 
20  * @param key       输入数据
21  * @param value     加盐数据
22  * @param out        输出数据
23  */
24 void hmac(const char* key, const char* value, unsigned char* out)
25 {
26     int keyLen = strlen((char *)key);
27     int valueLen = strlen((char *)value);
28     HMAC_SHA256_CTX hmac;
29     hmac_sha256_init(&hmac, key, keyLen);
30     hmac_sha256_update(&hmac, value, valueLen);
31     hmac_sha256_final(&hmac, out);
32     return;
33 }
34 
35 /**
36  * @brief           计算哈希值,基于sha256
37  * 
38  * @param key       输入数据
39  * @param out       输出数据
40  */
41 void hash(const unsigned char *key, unsigned char *out) {
42     SHA256_CTX sha;
43     sha256_init(&sha);
44     sha256_update(&sha, key, strlen(key));
45     sha256_final(&sha, out);
46     return;
47 }
48 
49 int main()
50 {
51     uint8_t hexData[65] = {0};                          // 十六进制形式的字符串用作打印输出
52     unsigned char md[32] = {0};
53     unsigned char test_val[] = "where am i";
54     unsigned char test_key[] = "who am i";
55     hmac(test_val, test_key, md);
56     for(int i = 0; i < 32; i++)
57     {      
58         sprintf(hexData + 2 * i, "%02x", md[i]);
59     }
60     hexData[64] = '\0';
61     printf("HmacDate:\n%s\n",hexData);
62 
63     hash(test_key, md);
64     for(int i = 0; i < 32; i++)
65     {      
66         sprintf(hexData + 2 * i, "%02x", md[i]);
67     }
68     hexData[64] = '\0';
69     printf("HashDate:\n%s\n",hexData);
70 
71     return 0;
72 }
73  
74  
75 // HmacDate:
76 // 476eb8bdb7b96a9f229d7123703910c3e8d11f26d09c4b9867a05e6d52370df9
77 // HashDate:
78 // 73d7b7288d31175792d8a1f51b63936d5683718082f5a401b4e9d6829de967d3

在线HMAC和哈希值计算:

在线HMAC计算工具 (lddgo.net)

 在线哈希值计算 (lddgo.net)

 

参考自:

HMAC-SHA256签名加密 C语言实现+例子_hmacsha256加密在线-CSDN博客

SHA256 和 HMAC-SHA256 的C语言实现_hmacsha256 c-CSDN博客

标签:uint32,SHA256,Hash,C语言,len,HMAC,sha256,buf,hmac
From: https://www.cnblogs.com/GREEN-FIEID/p/18057396

相关文章

  • 【C语言】自定义类型
    目录一、结构体:1、结构体的声明:2、结构体的自引用:3、结构体变量的定义和初始化:4、结构体内存对齐:5、结构体传参:6、位段:二、枚举类型:三、联合体:一、结构体:1、结构体的声明:首先要了解什么是结构:结构是一些值的集合,与数组不同的是结构的每一个成员变量可以使不同......
  • 探究C语言函数栈帧的创建和销毁
    引言在C语言程序中,每当一个函数被调用时,系统都会在栈上为该函数分配一块内存空间,这块内存空间就被称为栈帧。栈帧中包含了函数执行所需的所有信息,如局部变量、参数、返回地址等。栈帧的创建和销毁是函数调用的核心部分,它们确保了函数能够正确地执行和返回。本文将在VS2013环......
  • C语言基础入门 -1
    一.计算机中单位bit -比特位(最小单位);byte-字节kb-千字节mb-兆字节gb-千兆字节tb-太字节1字节=8比特位;其他单位之间换算为1024;二.数据类型与所占字节char//字符型   1字节short//短整型   2字节int  //整型   4字节long//长整型  ......
  • C语言----自定义类型:联合和枚举
     1.联合体联合体的特点像结构体一样,联合体也是一个或者多个成员构成的,这些成员可以是不同的类型联合体的关键字:union结构体的关键字:struct枚举的关键字:enum但是编译器只为最⼤的成员分配⾜够的内存空间。联合体的特点是所有成员共⽤同⼀块内存空间。所以联合体也叫:共......
  • 【C语言】初识C语言 - 数组
    一、一维数组的创建和初始化1.1一维数组的创建数组的定义:是一种线性数据结构,用于存储相同数据类型的元素的集合。数组中的元素在内存中是连续存储的,并且通过索引来访问。#include<stdio.h>intmain(){ intarr[1]={2}; //数组数据类型为:int //数组名为:arr //......
  • C语言期末复习笔记
    目录一,基础介绍。二,标识符起名规范。三,数据类型。四,变量。五,运算符和表达式1,加减乘除​编辑  /为整除,%为余数,*为乘号2,关系运算符3,逻辑运算符4,运算符优先级5,前自增,后自增6,三目运算符。7,符合运算符。六,控制语句。1,if判断2,多重判断。3,for循环4,while循环5,d......
  • C语言程序设计-11 结构体与共用体
    11.1定义一个结构的一般形式11.2结构类型变量的说明1.先定义结构,再说明结构变量。2.在定义结构类型的同时说明结构变量。3.直接说明结构变量。11.3结构变量成员的表示方法11.4结构变量的赋值结构变量的赋值就是给各成员赋值。可用输入语句或赋值语句来完成。......
  • C语言程序设计-10 指针
    指针是C语言中广泛使用的一种数据类型。运用指针编程是C语言最主要的风格之一。利用指针变量可以表示各种数据结构;能很方便地使用数组和字符串;并能象汇编语言一样处理内存地址,从而编出精练而高效的程序。指针极大地丰富了C语言的功能。学习指针是学习C语言中最重要的一环,能......
  • C语言程序设计-8 函 数
    8.1概述在前面已经介绍过,C源程序是由函数组成的。虽然在前面各章的程序中大都只有一个主函数main(),但实用程序往往由多个函数组成。函数是C源程序的基本模块,通过对函数模块的调用实现特定的功能。C语言中的函数相当于其它高级语言的子程序。C语言不仅提供了极为丰富的库函......
  • linux下C语言如何操作文件(四)
    structstring_segment*parse_path(constchar*file_path)函数,解析路径,将一个linux的路径分成两部分路径和文件名。返回一个字符串分段接头体指针,里边有三个字符串。第0个是原始的file_path,第二个是已经拆分好的路径,第三个是文件名部分。代码如下:/***解析linux下的路径......