2 #define CONFIG "config.h"
6 #if !defined(_CRYPTO_OPENSSL) && !defined(_CRYPTO_POLARSSL) && !defined(_CRYPTO_WINDOWS)
7 #include "crypto_internal.h"
10 #define F0(x, y, z) ( ((x) & (y)) | (~(x) & (z)) )
11 #define F1(x, y, z) ( ((x) & (y)) | ((x) & (z)) | ((y) & (z)) )
13 #define SI1(x) ( ROR32(x, 2 ) ^ ROR32(x, 13) ^ ROR32(x, 22) )
14 #define SI2(x) ( ROR32(x, 6 ) ^ ROR32(x, 11) ^ ROR32(x, 25) )
15 #define SI3(x) ( ROR32(x, 7 ) ^ ROR32(x, 18) ^ ((x) >> 3 ) )
16 #define SI4(x) ( ROR32(x, 17) ^ ROR32(x, 19) ^ ((x) >> 10) )
18 static const DWORD k
[] = {
19 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1,
20 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
21 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786,
22 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
23 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147,
24 0x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
25 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B,
26 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
27 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A,
28 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
29 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
33 static void Sha256Init(Sha256Ctx
*Ctx
)
35 Ctx
->State
[0] = 0x6A09E667;
36 Ctx
->State
[1] = 0xBB67AE85;
37 Ctx
->State
[2] = 0x3C6EF372;
38 Ctx
->State
[3] = 0xA54FF53A;
39 Ctx
->State
[4] = 0x510E527F;
40 Ctx
->State
[5] = 0x9B05688C;
41 Ctx
->State
[6] = 0x1F83D9AB;
42 Ctx
->State
[7] = 0x5BE0CD19;
47 static void Sha256ProcessBlock(Sha256Ctx
*Ctx
, BYTE
*block
)
50 DWORD w
[64], temp1
, temp2
;
51 DWORD a
= Ctx
->State
[0];
52 DWORD b
= Ctx
->State
[1];
53 DWORD c
= Ctx
->State
[2];
54 DWORD d
= Ctx
->State
[3];
55 DWORD e
= Ctx
->State
[4];
56 DWORD f
= Ctx
->State
[5];
57 DWORD g
= Ctx
->State
[6];
58 DWORD h
= Ctx
->State
[7];
60 for (i
= 0; i
< 16; i
++)
61 //w[ i ] = GET_UAA32BE(block, i);
62 w
[i
] = BE32(((DWORD
*)block
)[i
]);
64 for (i
= 16; i
< 64; i
++)
65 w
[ i
] = SI4(w
[ i
- 2 ]) + w
[ i
- 7 ] + SI3(w
[ i
- 15 ]) + w
[ i
- 16 ];
67 for (i
= 0; i
< 64; i
++)
69 temp1
= h
+ SI2(e
) + F0(e
, f
, g
) + k
[ i
] + w
[ i
];
70 temp2
= SI1(a
) + F1(a
, b
, c
);
93 static void Sha256Update(Sha256Ctx
*Ctx
, BYTE
*data
, size_t len
)
95 unsigned int b_len
= Ctx
->Len
& 63,
96 r_len
= (b_len
^ 63) + 1;
102 memcpy(Ctx
->Buffer
+ b_len
, data
, len
);
108 memcpy(Ctx
->Buffer
+ b_len
, data
, r_len
);
111 Sha256ProcessBlock(Ctx
, Ctx
->Buffer
);
114 for (; len
>= 64; len
-= 64, data
+= 64)
115 Sha256ProcessBlock(Ctx
, data
);
117 if ( len
) memcpy(Ctx
->Buffer
, data
, len
);
121 static void Sha256Finish(Sha256Ctx
*Ctx
, BYTE
*hash
)
123 unsigned int i
, b_len
= Ctx
->Len
& 63;
125 Ctx
->Buffer
[ b_len
] = 0x80;
126 if ( b_len
^ 63 ) memset(Ctx
->Buffer
+ b_len
+ 1, 0, b_len
^ 63);
130 Sha256ProcessBlock(Ctx
, Ctx
->Buffer
);
131 memset(Ctx
->Buffer
, 0, 56);
134 //PUT_UAA64BE(Ctx->Buffer, (unsigned long long)(Ctx->Len * 8), 7);
135 ((uint64_t*)Ctx
->Buffer
)[7] = BE64((uint64_t)Ctx
->Len
<< 3);
136 Sha256ProcessBlock(Ctx
, Ctx
->Buffer
);
138 for (i
= 0; i
< 8; i
++)
139 //PUT_UAA32BE(hash, Ctx->State[i], i);
140 ((DWORD
*)hash
)[i
] = BE32(Ctx
->State
[i
]);
145 void Sha256(BYTE
*data
, size_t len
, BYTE
*hash
)
150 Sha256Update(&Ctx
, data
, len
);
151 Sha256Finish(&Ctx
, hash
);
155 static void _Sha256HmacInit(Sha256HmacCtx
*Ctx
, BYTE
*key
, size_t klen
)
160 memset(IPad
, 0x36, sizeof(IPad
));
161 memset(Ctx
->OPad
, 0x5C, sizeof(Ctx
->OPad
));
165 BYTE
*temp
= (BYTE
*)alloca(32);
166 Sha256(key
, klen
, temp
);
171 for (i
= 0; i
< klen
; i
++)
173 IPad
[ i
] ^= key
[ i
];
174 Ctx
->OPad
[ i
] ^= key
[ i
];
177 Sha256Init(&Ctx
->ShaCtx
);
178 Sha256Update(&Ctx
->ShaCtx
, IPad
, sizeof(IPad
));
182 static void _Sha256HmacUpdate(Sha256HmacCtx
*Ctx
, BYTE
*data
, size_t len
)
184 Sha256Update(&Ctx
->ShaCtx
, data
, len
);
188 static void _Sha256HmacFinish(Sha256HmacCtx
*Ctx
, BYTE
*hmac
)
192 Sha256Finish(&Ctx
->ShaCtx
, temp
);
193 Sha256Init(&Ctx
->ShaCtx
);
194 Sha256Update(&Ctx
->ShaCtx
, Ctx
->OPad
, sizeof(Ctx
->OPad
));
195 Sha256Update(&Ctx
->ShaCtx
, temp
, sizeof(temp
));
196 Sha256Finish(&Ctx
->ShaCtx
, hmac
);
201 int_fast8_t Sha256Hmac(BYTE
* key
, BYTE
* restrict data
, DWORD len
, BYTE
* restrict hmac
)
204 _Sha256HmacInit(&Ctx
, key
, 16);
205 _Sha256HmacUpdate(&Ctx
, data
, len
);
206 _Sha256HmacFinish(&Ctx
, hmac
);
211 #endif // No external Crypto