| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Utilities; |
| | | 4 | | using Renci.SshNet.Security.Org.BouncyCastle.Utilities; |
| | | 5 | | |
| | | 6 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Crypto.Digests |
| | | 7 | | { |
| | | 8 | | internal class Sha256Digest |
| | | 9 | | : GeneralDigest |
| | | 10 | | { |
| | | 11 | | private const int DigestLength = 32; |
| | | 12 | | |
| | | 13 | | private uint H1, H2, H3, H4, H5, H6, H7, H8; |
| | 10 | 14 | | private uint[] X = new uint[64]; |
| | | 15 | | private int xOff; |
| | | 16 | | |
| | 10 | 17 | | public Sha256Digest() |
| | 10 | 18 | | { |
| | 10 | 19 | | initHs(); |
| | 10 | 20 | | } |
| | | 21 | | |
| | | 22 | | /** |
| | | 23 | | * Copy constructor. This will copy the state of the provided |
| | | 24 | | * message digest. |
| | | 25 | | */ |
| | 0 | 26 | | public Sha256Digest(Sha256Digest t) : base(t) |
| | 0 | 27 | | { |
| | 0 | 28 | | CopyIn(t); |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | private void CopyIn(Sha256Digest t) |
| | 0 | 32 | | { |
| | 0 | 33 | | base.CopyIn(t); |
| | | 34 | | |
| | 0 | 35 | | H1 = t.H1; |
| | 0 | 36 | | H2 = t.H2; |
| | 0 | 37 | | H3 = t.H3; |
| | 0 | 38 | | H4 = t.H4; |
| | 0 | 39 | | H5 = t.H5; |
| | 0 | 40 | | H6 = t.H6; |
| | 0 | 41 | | H7 = t.H7; |
| | 0 | 42 | | H8 = t.H8; |
| | | 43 | | |
| | 0 | 44 | | Array.Copy(t.X, 0, X, 0, t.X.Length); |
| | 0 | 45 | | xOff = t.xOff; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | public override string AlgorithmName |
| | | 49 | | { |
| | 0 | 50 | | get { return "SHA-256"; } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public override int GetDigestSize() |
| | 30 | 54 | | { |
| | 30 | 55 | | return DigestLength; |
| | 30 | 56 | | } |
| | | 57 | | |
| | | 58 | | internal override void ProcessWord( |
| | | 59 | | byte[] input, |
| | | 60 | | int inOff) |
| | 622 | 61 | | { |
| | 622 | 62 | | X[xOff] = Pack.BE_To_UInt32(input, inOff); |
| | | 63 | | |
| | 622 | 64 | | if (++xOff == 16) |
| | 28 | 65 | | { |
| | 28 | 66 | | ProcessBlock(); |
| | 28 | 67 | | } |
| | 622 | 68 | | } |
| | | 69 | | |
| | | 70 | | internal override void ProcessLength( |
| | | 71 | | long bitLength) |
| | 38 | 72 | | { |
| | 38 | 73 | | if (xOff > 14) |
| | 0 | 74 | | { |
| | 0 | 75 | | ProcessBlock(); |
| | 0 | 76 | | } |
| | | 77 | | |
| | 38 | 78 | | X[14] = (uint)((ulong)bitLength >> 32); |
| | 38 | 79 | | X[15] = (uint)((ulong)bitLength); |
| | 38 | 80 | | } |
| | | 81 | | |
| | | 82 | | public override int DoFinal( |
| | | 83 | | byte[] output, |
| | | 84 | | int outOff) |
| | 38 | 85 | | { |
| | 38 | 86 | | Finish(); |
| | | 87 | | |
| | 38 | 88 | | Pack.UInt32_To_BE((uint)H1, output, outOff); |
| | 38 | 89 | | Pack.UInt32_To_BE((uint)H2, output, outOff + 4); |
| | 38 | 90 | | Pack.UInt32_To_BE((uint)H3, output, outOff + 8); |
| | 38 | 91 | | Pack.UInt32_To_BE((uint)H4, output, outOff + 12); |
| | 38 | 92 | | Pack.UInt32_To_BE((uint)H5, output, outOff + 16); |
| | 38 | 93 | | Pack.UInt32_To_BE((uint)H6, output, outOff + 20); |
| | 38 | 94 | | Pack.UInt32_To_BE((uint)H7, output, outOff + 24); |
| | 38 | 95 | | Pack.UInt32_To_BE((uint)H8, output, outOff + 28); |
| | | 96 | | |
| | 38 | 97 | | Reset(); |
| | | 98 | | |
| | 38 | 99 | | return DigestLength; |
| | 38 | 100 | | } |
| | | 101 | | |
| | | 102 | | public override void Reset() |
| | 38 | 103 | | { |
| | 38 | 104 | | base.Reset(); |
| | | 105 | | |
| | 38 | 106 | | initHs(); |
| | | 107 | | |
| | 38 | 108 | | xOff = 0; |
| | 38 | 109 | | Array.Clear(X, 0, X.Length); |
| | 38 | 110 | | } |
| | | 111 | | |
| | | 112 | | private void initHs() |
| | 48 | 113 | | { |
| | | 114 | | /* SHA-256 initial hash value |
| | | 115 | | * The first 32 bits of the fractional parts of the square roots |
| | | 116 | | * of the first eight prime numbers |
| | | 117 | | */ |
| | 48 | 118 | | H1 = 0x6a09e667; |
| | 48 | 119 | | H2 = 0xbb67ae85; |
| | 48 | 120 | | H3 = 0x3c6ef372; |
| | 48 | 121 | | H4 = 0xa54ff53a; |
| | 48 | 122 | | H5 = 0x510e527f; |
| | 48 | 123 | | H6 = 0x9b05688c; |
| | 48 | 124 | | H7 = 0x1f83d9ab; |
| | 48 | 125 | | H8 = 0x5be0cd19; |
| | 48 | 126 | | } |
| | | 127 | | |
| | | 128 | | internal override void ProcessBlock() |
| | 66 | 129 | | { |
| | | 130 | | // |
| | | 131 | | // expand 16 word block into 64 word blocks. |
| | | 132 | | // |
| | 6468 | 133 | | for (int ti = 16; ti <= 63; ti++) |
| | 3168 | 134 | | { |
| | 3168 | 135 | | X[ti] = Theta1(X[ti - 2]) + X[ti - 7] + Theta0(X[ti - 15]) + X[ti - 16]; |
| | 3168 | 136 | | } |
| | | 137 | | |
| | | 138 | | // |
| | | 139 | | // set up working variables. |
| | | 140 | | // |
| | 66 | 141 | | uint a = H1; |
| | 66 | 142 | | uint b = H2; |
| | 66 | 143 | | uint c = H3; |
| | 66 | 144 | | uint d = H4; |
| | 66 | 145 | | uint e = H5; |
| | 66 | 146 | | uint f = H6; |
| | 66 | 147 | | uint g = H7; |
| | 66 | 148 | | uint h = H8; |
| | | 149 | | |
| | 66 | 150 | | int t = 0; |
| | 1188 | 151 | | for(int i = 0; i < 8; ++i) |
| | 528 | 152 | | { |
| | | 153 | | // t = 8 * i |
| | 528 | 154 | | h += Sum1Ch(e, f, g) + K[t] + X[t]; |
| | 528 | 155 | | d += h; |
| | 528 | 156 | | h += Sum0Maj(a, b, c); |
| | 528 | 157 | | ++t; |
| | | 158 | | |
| | | 159 | | // t = 8 * i + 1 |
| | 528 | 160 | | g += Sum1Ch(d, e, f) + K[t] + X[t]; |
| | 528 | 161 | | c += g; |
| | 528 | 162 | | g += Sum0Maj(h, a, b); |
| | 528 | 163 | | ++t; |
| | | 164 | | |
| | | 165 | | // t = 8 * i + 2 |
| | 528 | 166 | | f += Sum1Ch(c, d, e) + K[t] + X[t]; |
| | 528 | 167 | | b += f; |
| | 528 | 168 | | f += Sum0Maj(g, h, a); |
| | 528 | 169 | | ++t; |
| | | 170 | | |
| | | 171 | | // t = 8 * i + 3 |
| | 528 | 172 | | e += Sum1Ch(b, c, d) + K[t] + X[t]; |
| | 528 | 173 | | a += e; |
| | 528 | 174 | | e += Sum0Maj(f, g, h); |
| | 528 | 175 | | ++t; |
| | | 176 | | |
| | | 177 | | // t = 8 * i + 4 |
| | 528 | 178 | | d += Sum1Ch(a, b, c) + K[t] + X[t]; |
| | 528 | 179 | | h += d; |
| | 528 | 180 | | d += Sum0Maj(e, f, g); |
| | 528 | 181 | | ++t; |
| | | 182 | | |
| | | 183 | | // t = 8 * i + 5 |
| | 528 | 184 | | c += Sum1Ch(h, a, b) + K[t] + X[t]; |
| | 528 | 185 | | g += c; |
| | 528 | 186 | | c += Sum0Maj(d, e, f); |
| | 528 | 187 | | ++t; |
| | | 188 | | |
| | | 189 | | // t = 8 * i + 6 |
| | 528 | 190 | | b += Sum1Ch(g, h, a) + K[t] + X[t]; |
| | 528 | 191 | | f += b; |
| | 528 | 192 | | b += Sum0Maj(c, d, e); |
| | 528 | 193 | | ++t; |
| | | 194 | | |
| | | 195 | | // t = 8 * i + 7 |
| | 528 | 196 | | a += Sum1Ch(f, g, h) + K[t] + X[t]; |
| | 528 | 197 | | e += a; |
| | 528 | 198 | | a += Sum0Maj(b, c, d); |
| | 528 | 199 | | ++t; |
| | 528 | 200 | | } |
| | | 201 | | |
| | 66 | 202 | | H1 += a; |
| | 66 | 203 | | H2 += b; |
| | 66 | 204 | | H3 += c; |
| | 66 | 205 | | H4 += d; |
| | 66 | 206 | | H5 += e; |
| | 66 | 207 | | H6 += f; |
| | 66 | 208 | | H7 += g; |
| | 66 | 209 | | H8 += h; |
| | | 210 | | |
| | | 211 | | // |
| | | 212 | | // reset the offset and clean out the word buffer. |
| | | 213 | | // |
| | 66 | 214 | | xOff = 0; |
| | 66 | 215 | | Array.Clear(X, 0, 16); |
| | 66 | 216 | | } |
| | | 217 | | |
| | | 218 | | private static uint Sum1Ch( |
| | | 219 | | uint x, |
| | | 220 | | uint y, |
| | | 221 | | uint z) |
| | 4224 | 222 | | { |
| | 4224 | 223 | | return (((x >> 6) | (x << 26)) ^ ((x >> 11) | (x << 21)) ^ ((x >> 25) | (x << 7))) |
| | 4224 | 224 | | + ((x & y) ^ ((~x) & z)); |
| | 4224 | 225 | | } |
| | | 226 | | |
| | | 227 | | private static uint Sum0Maj( |
| | | 228 | | uint x, |
| | | 229 | | uint y, |
| | | 230 | | uint z) |
| | 4224 | 231 | | { |
| | 4224 | 232 | | return (((x >> 2) | (x << 30)) ^ ((x >> 13) | (x << 19)) ^ ((x >> 22) | (x << 10))) |
| | 4224 | 233 | | + ((x & y) ^ (x & z) ^ (y & z)); |
| | 4224 | 234 | | } |
| | | 235 | | |
| | | 236 | | private static uint Theta0( |
| | | 237 | | uint x) |
| | 3168 | 238 | | { |
| | 3168 | 239 | | return ((x >> 7) | (x << 25)) ^ ((x >> 18) | (x << 14)) ^ (x >> 3); |
| | 3168 | 240 | | } |
| | | 241 | | |
| | | 242 | | private static uint Theta1( |
| | | 243 | | uint x) |
| | 3168 | 244 | | { |
| | 3168 | 245 | | return ((x >> 17) | (x << 15)) ^ ((x >> 19) | (x << 13)) ^ (x >> 10); |
| | 3168 | 246 | | } |
| | | 247 | | |
| | 1 | 248 | | private static readonly uint[] K = { |
| | 1 | 249 | | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
| | 1 | 250 | | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| | 1 | 251 | | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
| | 1 | 252 | | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| | 1 | 253 | | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
| | 1 | 254 | | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| | 1 | 255 | | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
| | 1 | 256 | | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| | 1 | 257 | | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
| | 1 | 258 | | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| | 1 | 259 | | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, |
| | 1 | 260 | | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| | 1 | 261 | | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, |
| | 1 | 262 | | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| | 1 | 263 | | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
| | 1 | 264 | | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
| | 1 | 265 | | }; |
| | | 266 | | |
| | | 267 | | public override IMemoable Copy() |
| | 0 | 268 | | { |
| | 0 | 269 | | return new Sha256Digest(this); |
| | 0 | 270 | | } |
| | | 271 | | |
| | | 272 | | public override void Reset(IMemoable other) |
| | 0 | 273 | | { |
| | 0 | 274 | | Sha256Digest d = (Sha256Digest)other; |
| | | 275 | | |
| | 0 | 276 | | CopyIn(d); |
| | 0 | 277 | | } |
| | | 278 | | |
| | | 279 | | } |
| | | 280 | | } |