7#include "core/fdrm/fx_crypt.h"
11#define SHA_GET_UINT32(n, b, i)
13 (n) = ((uint32_t)(b)[(i)] << 24
) | ((uint32_t)(b)[(i) + 1
] << 16
) |
14 ((uint32_t)(b)[(i) + 2
] << 8
) | ((uint32_t)(b)[(i) + 3
]);
16#define SHA_PUT_UINT32(n, b, i)
18 (b)[(i)] = (uint8_t)((n) >> 24
);
19 (b)[(i) + 1
] = (uint8_t)((n) >> 16
);
20 (b)[(i) + 2
] = (uint8_t)((n) >> 8
);
21 (b)[(i) + 3
] = (uint8_t)((n));
23#define SHA_GET_UINT64(n, b, i)
25 (n) = ((uint64_t)(b)[(i)] << 56
) | ((uint64_t)(b)[(i) + 1
] << 48
) |
26 ((uint64_t)(b)[(i) + 2
] << 40
) | ((uint64_t)(b)[(i) + 3
] << 32
) |
27 ((uint64_t)(b)[(i) + 4
] << 24
) | ((uint64_t)(b)[(i) + 5
] << 16
) |
28 ((uint64_t)(b)[(i) + 6
] << 8
) | ((uint64_t)(b)[(i) + 7
]);
30#define SHA_PUT_UINT64(n, b, i)
32 (b)[(i)] = (uint8_t)((n) >> 56
);
33 (b)[(i) + 1
] = (uint8_t)((n) >> 48
);
34 (b)[(i) + 2
] = (uint8_t)((n) >> 40
);
35 (b)[(i) + 3
] = (uint8_t)((n) >> 32
);
36 (b)[(i) + 4
] = (uint8_t)((n) >> 24
);
37 (b)[(i) + 5
] = (uint8_t)((n) >> 16
);
38 (b)[(i) + 6
] = (uint8_t)((n) >> 8
);
39 (b)[(i) + 7
] = (uint8_t)((n));
42#define SHA384_F0(x, y, z) ((x & y) | (z & (x | y)))
43#define SHA384_F1(x, y, z) (z ^ (x & (y ^ z)))
44#define SHA384_SHR(x, n) (x >> n)
45#define SHA384_ROTR(x, n) (SHA384_SHR(x, n) | x << (64
- n))
53#define SHA384_P(a, b, c, d, e, f, g, h, x, K)
63#define rol(x, y) (((x) << (y)) | (((unsigned int)x) >> (32
- y)))
64#define SHR(x, n) ((x & 0xFFFFFFFF
) >> n)
65#define ROTR(x, n) (SHR(x, n) | (x << (32
- n)))
70#define F0(x, y, z) ((x & y) | (z & (x | y)))
71#define F1(x, y, z) (z ^ (x & (y ^ z)))
72#define R(t) (W[t] = S1(W[t - 2
]) + W[t - 7
] + S0(W[t - 15
]) + W[t - 16
])
73#define PS(a, b, c, d, e, f, g, h, x, K)
75 uint32_t temp1 = h + S3(e) + F1(e, f, g) + K + x;
76 uint32_t temp2 = S2(a) + F0(a, b, c);
83void SHA_Core_Init(
unsigned int h[5]) {
91void SHATransform(
unsigned int* digest,
unsigned int* block) {
94 for (t = 0; t < 16; t++) {
97 for (t = 16; t < 80; t++) {
98 unsigned int tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
101 unsigned int a = digest[0];
102 unsigned int b = digest[1];
103 unsigned int c = digest[2];
104 unsigned int d = digest[3];
105 unsigned int e = digest[4];
106 for (t = 0; t < 20; t++) {
107 unsigned int tmp =
rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999;
114 for (t = 20; t < 40; t++) {
115 unsigned int tmp =
rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1;
122 for (t = 40; t < 60; t++) {
124 rol(a, 5) + ((b & c) | (b & d) | (c & d)) + e + w[t] + 0x8f1bbcdc;
131 for (t = 60; t < 80; t++) {
132 unsigned int tmp =
rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6;
165 uint32_t A =
static_cast<uint32_t>(ctx
->state[0]);
166 uint32_t B =
static_cast<uint32_t>(ctx
->state[1]);
167 uint32_t C =
static_cast<uint32_t>(ctx
->state[2]);
168 uint32_t D =
static_cast<uint32_t>(ctx
->state[3]);
169 uint32_t E =
static_cast<uint32_t>(ctx
->state[4]);
170 uint32_t F =
static_cast<uint32_t>(ctx
->state[5]);
171 uint32_t G =
static_cast<uint32_t>(ctx
->state[6]);
172 uint32_t H =
static_cast<uint32_t>(ctx
->state[7]);
173 PS(A, B, C, D, E, F, G, H, W[0], 0x428A2F98);
174 PS(H, A, B, C, D, E, F, G, W[1], 0x71374491);
175 PS(G, H, A, B, C, D, E, F, W[2], 0xB5C0FBCF);
176 PS(F, G, H, A, B, C, D, E, W[3], 0xE9B5DBA5);
177 PS(E, F, G, H, A, B, C, D, W[4], 0x3956C25B);
178 PS(D, E, F, G, H, A, B, C, W[5], 0x59F111F1);
179 PS(C, D, E, F, G, H, A, B, W[6], 0x923F82A4);
180 PS(B, C, D, E, F, G, H, A, W[7], 0xAB1C5ED5);
181 PS(A, B, C, D, E, F, G, H, W[8], 0xD807AA98);
182 PS(H, A, B, C, D, E, F, G, W[9], 0x12835B01);
183 PS(G, H, A, B, C, D, E, F, W[10], 0x243185BE);
184 PS(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3);
185 PS(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74);
186 PS(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE);
187 PS(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7);
188 PS(B, C, D, E, F, G, H, A, W[15], 0xC19BF174);
189 PS(A, B, C, D, E, F, G, H,
R(16), 0xE49B69C1);
190 PS(H, A, B, C, D, E, F, G,
R(17), 0xEFBE4786);
191 PS(G, H, A, B, C, D, E, F,
R(18), 0x0FC19DC6);
192 PS(F, G, H, A, B, C, D, E,
R(19), 0x240CA1CC);
193 PS(E, F, G, H, A, B, C, D,
R(20), 0x2DE92C6F);
194 PS(D, E, F, G, H, A, B, C,
R(21), 0x4A7484AA);
195 PS(C, D, E, F, G, H, A, B,
R(22), 0x5CB0A9DC);
196 PS(B, C, D, E, F, G, H, A,
R(23), 0x76F988DA);
197 PS(A, B, C, D, E, F, G, H,
R(24), 0x983E5152);
198 PS(H, A, B, C, D, E, F, G,
R(25), 0xA831C66D);
199 PS(G, H, A, B, C, D, E, F,
R(26), 0xB00327C8);
200 PS(F, G, H, A, B, C, D, E,
R(27), 0xBF597FC7);
201 PS(E, F, G, H, A, B, C, D,
R(28), 0xC6E00BF3);
202 PS(D, E, F, G, H, A, B, C,
R(29), 0xD5A79147);
203 PS(C, D, E, F, G, H, A, B,
R(30), 0x06CA6351);
204 PS(B, C, D, E, F, G, H, A,
R(31), 0x14292967);
205 PS(A, B, C, D, E, F, G, H,
R(32), 0x27B70A85);
206 PS(H, A, B, C, D, E, F, G,
R(33), 0x2E1B2138);
207 PS(G, H, A, B, C, D, E, F,
R(34), 0x4D2C6DFC);
208 PS(F, G, H, A, B, C, D, E,
R(35), 0x53380D13);
209 PS(E, F, G, H, A, B, C, D,
R(36), 0x650A7354);
210 PS(D, E, F, G, H, A, B, C,
R(37), 0x766A0ABB);
211 PS(C, D, E, F, G, H, A, B,
R(38), 0x81C2C92E);
212 PS(B, C, D, E, F, G, H, A,
R(39), 0x92722C85);
213 PS(A, B, C, D, E, F, G, H,
R(40), 0xA2BFE8A1);
214 PS(H, A, B, C, D, E, F, G,
R(41), 0xA81A664B);
215 PS(G, H, A, B, C, D, E, F,
R(42), 0xC24B8B70);
216 PS(F, G, H, A, B, C, D, E,
R(43), 0xC76C51A3);
217 PS(E, F, G, H, A, B, C, D,
R(44), 0xD192E819);
218 PS(D, E, F, G, H, A, B, C,
R(45), 0xD6990624);
219 PS(C, D, E, F, G, H, A, B,
R(46), 0xF40E3585);
220 PS(B, C, D, E, F, G, H, A,
R(47), 0x106AA070);
221 PS(A, B, C, D, E, F, G, H,
R(48), 0x19A4C116);
222 PS(H, A, B, C, D, E, F, G,
R(49), 0x1E376C08);
223 PS(G, H, A, B, C, D, E, F,
R(50), 0x2748774C);
224 PS(F, G, H, A, B, C, D, E,
R(51), 0x34B0BCB5);
225 PS(E, F, G, H, A, B, C, D,
R(52), 0x391C0CB3);
226 PS(D, E, F, G, H, A, B, C,
R(53), 0x4ED8AA4A);
227 PS(C, D, E, F, G, H, A, B,
R(54), 0x5B9CCA4F);
228 PS(B, C, D, E, F, G, H, A,
R(55), 0x682E6FF3);
229 PS(A, B, C, D, E, F, G, H,
R(56), 0x748F82EE);
230 PS(H, A, B, C, D, E, F, G,
R(57), 0x78A5636F);
231 PS(G, H, A, B, C, D, E, F,
R(58), 0x84C87814);
232 PS(F, G, H, A, B, C, D, E,
R(59), 0x8CC70208);
233 PS(E, F, G, H, A, B, C, D,
R(60), 0x90BEFFFA);
234 PS(D, E, F, G, H, A, B, C,
R(61), 0xA4506CEB);
235 PS(C, D, E, F, G, H, A, B,
R(62), 0xBEF9A3F7);
236 PS(B, C, D, E, F, G, H, A,
R(63), 0xC67178F2);
247const uint8_t sha256_padding[64] = {
248 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
249 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
250 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
252const uint8_t sha384_padding[128] = {
253 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
254 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
255 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
261uint64_t
const constants[] = {
262 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL,
263 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
264 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL,
265 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
266 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL,
267 0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
268 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL,
269 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
270 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL,
271 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
272 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL,
273 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
274 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL,
275 0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
276 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL,
277 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
278 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL,
279 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
280 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL,
281 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
282 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL,
283 0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
284 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL,
285 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
286 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL,
287 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
288 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL,
317 for (
int i = 0; i < 10; ++i) {
321 temp[1] = W[i * 8 + 1];
322 temp[2] = W[i * 8 + 2];
323 temp[3] = W[i * 8 + 3];
324 temp[4] = W[i * 8 + 4];
325 temp[5] = W[i * 8 + 5];
326 temp[6] = W[i * 8 + 6];
327 temp[7] = W[i * 8 + 7];
338 SHA384_P(A, B, C, D, E, F, G, H, temp[0], constants[i * 8]);
339 SHA384_P(H, A, B, C, D, E, F, G, temp[1], constants[i * 8 + 1]);
340 SHA384_P(G, H, A, B, C, D, E, F, temp[2], constants[i * 8 + 2]);
341 SHA384_P(F, G, H, A, B, C, D, E, temp[3], constants[i * 8 + 3]);
342 SHA384_P(E, F, G, H, A, B, C, D, temp[4], constants[i * 8 + 4]);
343 SHA384_P(D, E, F, G, H, A, B, C, temp[5], constants[i * 8 + 5]);
344 SHA384_P(C, D, E, F, G, H, A, B, temp[6], constants[i * 8 + 6]);
345 SHA384_P(B, C, D, E, F, G, H, A, temp[7], constants[i * 8 + 7]);
360 SHA_Core_Init(context
->h);
374 uint32_t wordblock[16];
379 for (
int i = 0; i < 16; i++) {
380 wordblock[i] = (((uint32_t)context
->block[i * 4 + 0]) << 24) |
381 (((uint32_t)context
->block[i * 4 + 1]) << 16) |
382 (((uint32_t)context
->block[i * 4 + 2]) << 8) |
383 (((uint32_t)context
->block[i * 4 + 3]) << 0);
385 SHATransform(context
->h, wordblock);
388 memcpy(context
->block, data, size);
404 c[0] = (total_bits >> 56) & 0xFF;
405 c[1] = (total_bits >> 48) & 0xFF;
406 c[2] = (total_bits >> 40) & 0xFF;
407 c[3] = (total_bits >> 32) & 0xFF;
408 c[4] = (total_bits >> 24) & 0xFF;
409 c[5] = (total_bits >> 16) & 0xFF;
410 c[6] = (total_bits >> 8) & 0xFF;
411 c[7] = (total_bits >> 0) & 0xFF;
413 for (
int i = 0; i < 5; i++) {
414 digest[i * 4] = (context
->h[i] >> 24) & 0xFF;
415 digest[i * 4 + 1] = (context
->h[i] >> 16) & 0xFF;
416 digest[i * 4 + 2] = (context
->h[i] >> 8) & 0xFF;
417 digest[i * 4 + 3] = (context
->h[i]) & 0xFF;
423 uint8_t digest[20]) {
432 context
->state[0] = 0x6A09E667;
433 context
->state[1] = 0xBB67AE85;
434 context
->state[2] = 0x3C6EF372;
435 context
->state[3] = 0xA54FF53A;
436 context
->state[4] = 0x510E527F;
437 context
->state[5] = 0x9B05688C;
438 context
->state[6] = 0x1F83D9AB;
439 context
->state[7] = 0x5BE0CD19;
450 uint32_t fill = 64 - left;
452 if (left && size >= fill) {
453 memcpy(context
->buffer + left, data, fill);
454 sha256_process(context, context
->buffer);
460 sha256_process(context, data);
465 memcpy(context
->buffer + left, data, size);
473 uint32_t padn = (last < 56) ? (56 - last) : (120 - last);
488 uint8_t digest[32]) {
497 context
->state[0] = 0xcbbb9d5dc1059ed8ULL;
498 context
->state[1] = 0x629a292a367cd507ULL;
499 context
->state[2] = 0x9159015a3070dd17ULL;
500 context
->state[3] = 0x152fecd8f70e5939ULL;
501 context
->state[4] = 0x67332667ffc00b31ULL;
502 context
->state[5] = 0x8eb44a8768581511ULL;
503 context
->state[6] = 0xdb0c2e0d64f98fa7ULL;
504 context
->state[7] = 0x47b5481dbefa4fa4ULL;
515 uint32_t fill = 128 - left;
517 if (left && size >= fill) {
518 memcpy(context
->buffer + left, data, fill);
519 sha384_process(context, context
->buffer);
524 while (size >= 128) {
525 sha384_process(context, data);
530 memcpy(context
->buffer + left, data, size);
539 uint32_t padn = (last < 112) ? (112 - last) : (240 - last);
552 uint8_t digest[48]) {
561 context
->state[0] = 0x6a09e667f3bcc908ULL;
562 context
->state[1] = 0xbb67ae8584caa73bULL;
563 context
->state[2] = 0x3c6ef372fe94f82bULL;
564 context
->state[3] = 0xa54ff53a5f1d36f1ULL;
565 context
->state[4] = 0x510e527fade682d1ULL;
566 context
->state[5] = 0x9b05688c2b3e6c1fULL;
567 context
->state[6] = 0x1f83d9abfb41bd6bULL;
568 context
->state[7] = 0x5be0cd19137e2179ULL;
584 uint32_t padn = (last < 112) ? (112 - last) : (240 - last);
599 uint8_t digest[64]) {
void CRYPT_SHA1Finish(CRYPT_sha1_context *context, uint8_t digest[20])
void CRYPT_SHA384Finish(CRYPT_sha2_context *context, uint8_t digest[48])
void CRYPT_SHA512Update(CRYPT_sha2_context *context, const uint8_t *data, uint32_t size)
void CRYPT_SHA1Generate(const uint8_t *data, uint32_t size, uint8_t digest[20])
void CRYPT_SHA256Generate(const uint8_t *data, uint32_t size, uint8_t digest[32])
void CRYPT_SHA384Start(CRYPT_sha2_context *context)
void CRYPT_SHA512Generate(const uint8_t *data, uint32_t size, uint8_t digest[64])
void CRYPT_SHA512Finish(CRYPT_sha2_context *context, uint8_t digest[64])
void CRYPT_SHA256Update(CRYPT_sha2_context *context, const uint8_t *data, uint32_t size)
void CRYPT_SHA1Update(CRYPT_sha1_context *context, const uint8_t *data, uint32_t size)
void CRYPT_SHA384Update(CRYPT_sha2_context *context, const uint8_t *data, uint32_t size)
void CRYPT_SHA512Start(CRYPT_sha2_context *context)
void CRYPT_SHA256Finish(CRYPT_sha2_context *context, uint8_t digest[32])
void CRYPT_SHA256Start(CRYPT_sha2_context *context)
void CRYPT_SHA1Start(CRYPT_sha1_context *context)
void CRYPT_SHA384Generate(const uint8_t *data, uint32_t size, uint8_t digest[48])
#define SHA_PUT_UINT64(n, b, i)
#define SHA384_F0(x, y, z)
#define SHA_GET_UINT32(n, b, i)
#define SHA_PUT_UINT32(n, b, i)
#define SHA384_P(a, b, c, d, e, f, g, h, x, K)
#define SHA384_F1(x, y, z)
#define PS(a, b, c, d, e, f, g, h, x, K)
#define SHA384_ROTR(x, n)
#define SHA_GET_UINT64(n, b, i)