annotate src/win32/7zip/7z/C/Sha256.c @ 2:3549bbe597ed

adding makefile.am
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:47 -0600
parents f9f4f1b99eed
children
rev   line source
rlm@1 1 /* Crypto/Sha256.c -- SHA-256 Hash function
rlm@1 2 2008-11-06 : Igor Pavlov : Public domain
rlm@1 3 This code is based on public domain code from Wei Dai's Crypto++ library. */
rlm@1 4
rlm@1 5 #include "Sha256.h"
rlm@1 6 #include "RotateDefs.h"
rlm@1 7
rlm@1 8 /* define it for speed optimization */
rlm@1 9 /* #define _SHA256_UNROLL */
rlm@1 10 /* #define _SHA256_UNROLL2 */
rlm@1 11
rlm@1 12 void Sha256_Init(CSha256 *p)
rlm@1 13 {
rlm@1 14 p->state[0] = 0x6a09e667;
rlm@1 15 p->state[1] = 0xbb67ae85;
rlm@1 16 p->state[2] = 0x3c6ef372;
rlm@1 17 p->state[3] = 0xa54ff53a;
rlm@1 18 p->state[4] = 0x510e527f;
rlm@1 19 p->state[5] = 0x9b05688c;
rlm@1 20 p->state[6] = 0x1f83d9ab;
rlm@1 21 p->state[7] = 0x5be0cd19;
rlm@1 22 p->count = 0;
rlm@1 23 }
rlm@1 24
rlm@1 25 #define S0(x) (rotrFixed(x, 2) ^ rotrFixed(x,13) ^ rotrFixed(x, 22))
rlm@1 26 #define S1(x) (rotrFixed(x, 6) ^ rotrFixed(x,11) ^ rotrFixed(x, 25))
rlm@1 27 #define s0(x) (rotrFixed(x, 7) ^ rotrFixed(x,18) ^ (x >> 3))
rlm@1 28 #define s1(x) (rotrFixed(x,17) ^ rotrFixed(x,19) ^ (x >> 10))
rlm@1 29
rlm@1 30 #define blk0(i) (W[i] = data[i])
rlm@1 31 #define blk2(i) (W[i&15] += s1(W[(i-2)&15]) + W[(i-7)&15] + s0(W[(i-15)&15]))
rlm@1 32
rlm@1 33 #define Ch(x,y,z) (z^(x&(y^z)))
rlm@1 34 #define Maj(x,y,z) ((x&y)|(z&(x|y)))
rlm@1 35
rlm@1 36 #define a(i) T[(0-(i))&7]
rlm@1 37 #define b(i) T[(1-(i))&7]
rlm@1 38 #define c(i) T[(2-(i))&7]
rlm@1 39 #define d(i) T[(3-(i))&7]
rlm@1 40 #define e(i) T[(4-(i))&7]
rlm@1 41 #define f(i) T[(5-(i))&7]
rlm@1 42 #define g(i) T[(6-(i))&7]
rlm@1 43 #define h(i) T[(7-(i))&7]
rlm@1 44
rlm@1 45
rlm@1 46 #ifdef _SHA256_UNROLL2
rlm@1 47
rlm@1 48 #define R(a,b,c,d,e,f,g,h, i) h += S1(e) + Ch(e,f,g) + K[i+j] + (j?blk2(i):blk0(i));\
rlm@1 49 d += h; h += S0(a) + Maj(a, b, c)
rlm@1 50
rlm@1 51 #define RX_8(i) \
rlm@1 52 R(a,b,c,d,e,f,g,h, i); \
rlm@1 53 R(h,a,b,c,d,e,f,g, i+1); \
rlm@1 54 R(g,h,a,b,c,d,e,f, i+2); \
rlm@1 55 R(f,g,h,a,b,c,d,e, i+3); \
rlm@1 56 R(e,f,g,h,a,b,c,d, i+4); \
rlm@1 57 R(d,e,f,g,h,a,b,c, i+5); \
rlm@1 58 R(c,d,e,f,g,h,a,b, i+6); \
rlm@1 59 R(b,c,d,e,f,g,h,a, i+7)
rlm@1 60
rlm@1 61 #else
rlm@1 62
rlm@1 63 #define R(i) h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[i+j] + (j?blk2(i):blk0(i));\
rlm@1 64 d(i) += h(i); h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))
rlm@1 65
rlm@1 66 #ifdef _SHA256_UNROLL
rlm@1 67
rlm@1 68 #define RX_8(i) R(i+0); R(i+1); R(i+2); R(i+3); R(i+4); R(i+5); R(i+6); R(i+7);
rlm@1 69
rlm@1 70 #endif
rlm@1 71
rlm@1 72 #endif
rlm@1 73
rlm@1 74 const UInt32 K[64] = {
rlm@1 75 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
rlm@1 76 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
rlm@1 77 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
rlm@1 78 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
rlm@1 79 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
rlm@1 80 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
rlm@1 81 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
rlm@1 82 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
rlm@1 83 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
rlm@1 84 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
rlm@1 85 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
rlm@1 86 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
rlm@1 87 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
rlm@1 88 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
rlm@1 89 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
rlm@1 90 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
rlm@1 91 };
rlm@1 92
rlm@1 93 static void Sha256_Transform(UInt32 *state, const UInt32 *data)
rlm@1 94 {
rlm@1 95 UInt32 W[16];
rlm@1 96 unsigned j;
rlm@1 97 #ifdef _SHA256_UNROLL2
rlm@1 98 UInt32 a,b,c,d,e,f,g,h;
rlm@1 99 a = state[0];
rlm@1 100 b = state[1];
rlm@1 101 c = state[2];
rlm@1 102 d = state[3];
rlm@1 103 e = state[4];
rlm@1 104 f = state[5];
rlm@1 105 g = state[6];
rlm@1 106 h = state[7];
rlm@1 107 #else
rlm@1 108 UInt32 T[8];
rlm@1 109 for (j = 0; j < 8; j++)
rlm@1 110 T[j] = state[j];
rlm@1 111 #endif
rlm@1 112
rlm@1 113 for (j = 0; j < 64; j += 16)
rlm@1 114 {
rlm@1 115 #if defined(_SHA256_UNROLL) || defined(_SHA256_UNROLL2)
rlm@1 116 RX_8(0); RX_8(8);
rlm@1 117 #else
rlm@1 118 unsigned i;
rlm@1 119 for (i = 0; i < 16; i++) { R(i); }
rlm@1 120 #endif
rlm@1 121 }
rlm@1 122
rlm@1 123 #ifdef _SHA256_UNROLL2
rlm@1 124 state[0] += a;
rlm@1 125 state[1] += b;
rlm@1 126 state[2] += c;
rlm@1 127 state[3] += d;
rlm@1 128 state[4] += e;
rlm@1 129 state[5] += f;
rlm@1 130 state[6] += g;
rlm@1 131 state[7] += h;
rlm@1 132 #else
rlm@1 133 for (j = 0; j < 8; j++)
rlm@1 134 state[j] += T[j];
rlm@1 135 #endif
rlm@1 136
rlm@1 137 /* Wipe variables */
rlm@1 138 /* memset(W, 0, sizeof(W)); */
rlm@1 139 /* memset(T, 0, sizeof(T)); */
rlm@1 140 }
rlm@1 141
rlm@1 142 #undef S0
rlm@1 143 #undef S1
rlm@1 144 #undef s0
rlm@1 145 #undef s1
rlm@1 146
rlm@1 147 static void Sha256_WriteByteBlock(CSha256 *p)
rlm@1 148 {
rlm@1 149 UInt32 data32[16];
rlm@1 150 unsigned i;
rlm@1 151 for (i = 0; i < 16; i++)
rlm@1 152 data32[i] =
rlm@1 153 ((UInt32)(p->buffer[i * 4 ]) << 24) +
rlm@1 154 ((UInt32)(p->buffer[i * 4 + 1]) << 16) +
rlm@1 155 ((UInt32)(p->buffer[i * 4 + 2]) << 8) +
rlm@1 156 ((UInt32)(p->buffer[i * 4 + 3]));
rlm@1 157 Sha256_Transform(p->state, data32);
rlm@1 158 }
rlm@1 159
rlm@1 160 void Sha256_Update(CSha256 *p, const Byte *data, size_t size)
rlm@1 161 {
rlm@1 162 UInt32 curBufferPos = (UInt32)p->count & 0x3F;
rlm@1 163 while (size > 0)
rlm@1 164 {
rlm@1 165 p->buffer[curBufferPos++] = *data++;
rlm@1 166 p->count++;
rlm@1 167 size--;
rlm@1 168 if (curBufferPos == 64)
rlm@1 169 {
rlm@1 170 curBufferPos = 0;
rlm@1 171 Sha256_WriteByteBlock(p);
rlm@1 172 }
rlm@1 173 }
rlm@1 174 }
rlm@1 175
rlm@1 176 void Sha256_Final(CSha256 *p, Byte *digest)
rlm@1 177 {
rlm@1 178 UInt64 lenInBits = (p->count << 3);
rlm@1 179 UInt32 curBufferPos = (UInt32)p->count & 0x3F;
rlm@1 180 unsigned i;
rlm@1 181 p->buffer[curBufferPos++] = 0x80;
rlm@1 182 while (curBufferPos != (64 - 8))
rlm@1 183 {
rlm@1 184 curBufferPos &= 0x3F;
rlm@1 185 if (curBufferPos == 0)
rlm@1 186 Sha256_WriteByteBlock(p);
rlm@1 187 p->buffer[curBufferPos++] = 0;
rlm@1 188 }
rlm@1 189 for (i = 0; i < 8; i++)
rlm@1 190 {
rlm@1 191 p->buffer[curBufferPos++] = (Byte)(lenInBits >> 56);
rlm@1 192 lenInBits <<= 8;
rlm@1 193 }
rlm@1 194 Sha256_WriteByteBlock(p);
rlm@1 195
rlm@1 196 for (i = 0; i < 8; i++)
rlm@1 197 {
rlm@1 198 *digest++ = (Byte)((p->state[i] >> 24) & 0xFF);
rlm@1 199 *digest++ = (Byte)((p->state[i] >> 16) & 0xFF);
rlm@1 200 *digest++ = (Byte)((p->state[i] >> 8) & 0xFF);
rlm@1 201 *digest++ = (Byte)((p->state[i]) & 0xFF);
rlm@1 202 }
rlm@1 203 Sha256_Init(p);
rlm@1 204 }