static void sha256_transform(uint32_t *state, const uint8_t buffer[64]) { unsigned int i, a, b, c, d, e, f, g, h; uint32_t block[64]; uint32_t T1; a = state[0]; b = state[1]; c = state[2]; d = state[3]; e = state[4]; f = state[5]; g = state[6]; h = state[7]; #if CONFIG_SMALL for (i = 0; i < 64; i++) { uint32_t T2; if (i < 16) T1 = blk0(i); else T1 = blk(i); T1 += h + Sigma1_256(e) + Ch(e, f, g) + K256[i]; T2 = Sigma0_256(a) + Maj(a, b, c); h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2; } #else for (i = 0; i < 16;) { ROUND256_0_TO_15(a, b, c, d, e, f, g, h); ROUND256_0_TO_15(h, a, b, c, d, e, f, g); ROUND256_0_TO_15(g, h, a, b, c, d, e, f); ROUND256_0_TO_15(f, g, h, a, b, c, d, e); ROUND256_0_TO_15(e, f, g, h, a, b, c, d); ROUND256_0_TO_15(d, e, f, g, h, a, b, c); ROUND256_0_TO_15(c, d, e, f, g, h, a, b); ROUND256_0_TO_15(b, c, d, e, f, g, h, a); } for (; i < 64;) { ROUND256_16_TO_63(a, b, c, d, e, f, g, h); ROUND256_16_TO_63(h, a, b, c, d, e, f, g); ROUND256_16_TO_63(g, h, a, b, c, d, e, f); ROUND256_16_TO_63(f, g, h, a, b, c, d, e); ROUND256_16_TO_63(e, f, g, h, a, b, c, d); ROUND256_16_TO_63(d, e, f, g, h, a, b, c); ROUND256_16_TO_63(c, d, e, f, g, h, a, b); ROUND256_16_TO_63(b, c, d, e, f, g, h, a); } #endif state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; state[5] += f; state[6] += g; state[7] += h; }
static void sha512_transform(uint64_t *state, const uint8_t buffer[128]) { uint64_t a, b, c, d, e, f, g, h; uint64_t block[80]; uint64_t T1; int i; a = state[0]; b = state[1]; c = state[2]; d = state[3]; e = state[4]; f = state[5]; g = state[6]; h = state[7]; #if CONFIG_SMALL for (i = 0; i < 80; i++) { uint64_t T2; if (i < 16) T1 = blk0(i); else T1 = blk(i); T1 += h + Sigma1_512(e) + Ch(e, f, g) + K512[i]; T2 = Sigma0_512(a) + Maj(a, b, c); h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2; } #else #define R512_0 \ ROUND512_0_TO_15(a, b, c, d, e, f, g, h); \ ROUND512_0_TO_15(h, a, b, c, d, e, f, g); \ ROUND512_0_TO_15(g, h, a, b, c, d, e, f); \ ROUND512_0_TO_15(f, g, h, a, b, c, d, e); \ ROUND512_0_TO_15(e, f, g, h, a, b, c, d); \ ROUND512_0_TO_15(d, e, f, g, h, a, b, c); \ ROUND512_0_TO_15(c, d, e, f, g, h, a, b); \ ROUND512_0_TO_15(b, c, d, e, f, g, h, a) i = 0; R512_0; R512_0; #define R512_16 \ ROUND512_16_TO_80(a, b, c, d, e, f, g, h); \ ROUND512_16_TO_80(h, a, b, c, d, e, f, g); \ ROUND512_16_TO_80(g, h, a, b, c, d, e, f); \ ROUND512_16_TO_80(f, g, h, a, b, c, d, e); \ ROUND512_16_TO_80(e, f, g, h, a, b, c, d); \ ROUND512_16_TO_80(d, e, f, g, h, a, b, c); \ ROUND512_16_TO_80(c, d, e, f, g, h, a, b); \ ROUND512_16_TO_80(b, c, d, e, f, g, h, a) R512_16; R512_16; R512_16; R512_16; R512_16; R512_16; R512_16; R512_16; #endif state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; state[5] += f; state[6] += g; state[7] += h; }
/* transform one 512bit block. this is the core of the algorithm. */ static void sha1_transform(uint32_t state[5], char buf[SHA1_BLOCKSIZE]) { uint32_t a, b, c, d, e; uint32_t *block = (uint32_t *)buf; #ifdef SHA1_SHORTCODE uint8_t i; #endif /* copy state to working vars */ a = state[0]; b = state[1]; c = state[2]; d = state[3]; e = state[4]; #ifdef SHA1_SHORTCODE for (i = 0; i < 80; i++) { uint32_t t; if (i < 20) t = ((b & (c ^ d)) ^ d) + 0x5A827999; else if (i < 40) t = (b ^ c ^ d) + 0x6ED9EBA1; else if (i < 60) t = (((b | c) & d) | (b & c)) + 0x8F1BBCDC; else t = (b ^ c ^ d) + 0xCA62C1D6; t += (i < 16) ? blk0(i) : blk(i); t += e + rol(a, 5); e = d; d = c; c = rol(b, 30); b = a; a = t; } #else /* R0 and R1, R2, R3, R4 are the different operations used in SHA1 */ #define R0(v,w,x,y,z,i) \ z += ((w&(x^y))^y) + blk0(i) + 0x5A827999 + rol(v, 5); w = rol(w, 30) #define R1(v,w,x,y,z,i) \ z += ((w&(x^y))^y) + blk(i) + 0x5A827999 + rol(v, 5); w = rol(w, 30) #define R2(v,w,x,y,z,i) \ z += (w^x^y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); w = rol(w, 30) #define R3(v,w,x,y,z,i) \ z += (((w|x)&y)|(w&x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); w = rol(w, 30) #define R4(v,w,x,y,z,i) \ z += (w^x^y) + blk(i) + 0xCA62C1D6 + rol(v, 5); w = rol(w, 30) /* 4 rounds of 20 operations each. loop unrolled. */ R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); #endif /* add the working vars back into state */ state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; }