aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / agora / tls / sha2.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note om/tls/sha2.om: SHA-256, SHA-384, SHA-512 (FIPS 180-4) streaming, as an OM-2 library unit |
| 3 | note (no om2 line, no main, no globals). Module code SH; functions sha2_*, sha256_*, sha384_*, sha512_*. |
| 4 | note |
| 5 | note API |
| 6 | note sha256_init c edit ulong * c: 40 ulong cells (caller owns all state) |
| 7 | note sha256_update c edit ulong * m view uchar * any length, any chunking |
| 8 | note sha256_final c edit ulong * o edit uchar * writes 32 bytes (o shorter traps 3) |
| 9 | note sha384_* / sha512_* the same; final writes 48 / 64 bytes |
| 10 | note sha2_copy d edit ulong * s view ulong * copies len s cells (a context snapshot) |
| 11 | note sha2_digest alg ulong m view uchar * o edit uchar * to ulong |
| 12 | note alg 256|384|512: one-shot digest into o, returns 32|48|64; any other alg returns 0, writes nothing |
| 13 | note After final the context is spent (its state cells hold the last chaining value); init it again. |
| 14 | note A context initialised for one algorithm must be used with that algorithm's update/final; the |
| 15 | note common code reads the algorithm from cell 9, so sha256_update on a sha512 context hashes SHA-512. |
| 16 | note |
| 17 | note Context cells: 0..7 chaining value H (32-bit words zero-extended for SHA-256), 8 = bytes absorbed |
| 18 | note mod 2^64, 9 = algorithm (256, 384, 512), 16..31 = pending block bytes packed big-endian (byte i |
| 19 | note in cell 16 + i/8, bits 56 - 8*(i mod 8)), 10..15 and 32..39 = 0. |
| 20 | note Constant time: every branch, loop count and index depends only on lengths and the algorithm, |
| 21 | note never on message or state bytes. Local schedule/state/padding arrays are zeroed before return |
| 22 | note (HMAC keys pass through them). omc emits plain AArch64; MUL/ADD/EOR/ROR/shift data-independent. |
| 23 | note Message length: any length below 2^64 bytes; update cuts its input into runs of 2^25 bytes so no |
| 24 | note loop passes 2^20 times (a rolled for traps 4 after 10^6 passes; the while bound 999999 of runs |
| 25 | note allows inputs up to 999999 * 32 MiB per update call). |
| 26 | |
| 27 | note K{256}[0..64): first 32 bits of the fractional parts of the cube roots of the first 64 primes |
| 28 | note (FIPS 180-4 4.2.2). Same values as om/sha256/sha256-om2.om; recomputed with Wolfram: |
| 29 | note IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^32],16,8] for i = 1..64. |
| 30 | fn sha2_k256 k edit uint 64 to void |
| 31 | put k 0 0x428a2f98 |
| 32 | put k 1 0x71374491 |
| 33 | put k 2 0xb5c0fbcf |
| 34 | put k 3 0xe9b5dba5 |
| 35 | put k 4 0x3956c25b |
| 36 | put k 5 0x59f111f1 |
| 37 | put k 6 0x923f82a4 |
| 38 | put k 7 0xab1c5ed5 |
| 39 | put k 8 0xd807aa98 |
| 40 | put k 9 0x12835b01 |
| 41 | put k 10 0x243185be |
| 42 | put k 11 0x550c7dc3 |
| 43 | put k 12 0x72be5d74 |
| 44 | put k 13 0x80deb1fe |
| 45 | put k 14 0x9bdc06a7 |
| 46 | put k 15 0xc19bf174 |
| 47 | put k 16 0xe49b69c1 |
| 48 | put k 17 0xefbe4786 |
| 49 | put k 18 0x0fc19dc6 |
| 50 | put k 19 0x240ca1cc |
| 51 | put k 20 0x2de92c6f |
| 52 | put k 21 0x4a7484aa |
| 53 | put k 22 0x5cb0a9dc |
| 54 | put k 23 0x76f988da |
| 55 | put k 24 0x983e5152 |
| 56 | put k 25 0xa831c66d |
| 57 | put k 26 0xb00327c8 |
| 58 | put k 27 0xbf597fc7 |
| 59 | put k 28 0xc6e00bf3 |
| 60 | put k 29 0xd5a79147 |
| 61 | put k 30 0x06ca6351 |
| 62 | put k 31 0x14292967 |
| 63 | put k 32 0x27b70a85 |
| 64 | put k 33 0x2e1b2138 |
| 65 | put k 34 0x4d2c6dfc |
| 66 | put k 35 0x53380d13 |
| 67 | put k 36 0x650a7354 |
| 68 | put k 37 0x766a0abb |
| 69 | put k 38 0x81c2c92e |
| 70 | put k 39 0x92722c85 |
| 71 | put k 40 0xa2bfe8a1 |
| 72 | put k 41 0xa81a664b |
| 73 | put k 42 0xc24b8b70 |
| 74 | put k 43 0xc76c51a3 |
| 75 | put k 44 0xd192e819 |
| 76 | put k 45 0xd6990624 |
| 77 | put k 46 0xf40e3585 |
| 78 | put k 47 0x106aa070 |
| 79 | put k 48 0x19a4c116 |
| 80 | put k 49 0x1e376c08 |
| 81 | put k 50 0x2748774c |
| 82 | put k 51 0x34b0bcb5 |
| 83 | put k 52 0x391c0cb3 |
| 84 | put k 53 0x4ed8aa4a |
| 85 | put k 54 0x5b9cca4f |
| 86 | put k 55 0x682e6ff3 |
| 87 | put k 56 0x748f82ee |
| 88 | put k 57 0x78a5636f |
| 89 | put k 58 0x84c87814 |
| 90 | put k 59 0x8cc70208 |
| 91 | put k 60 0x90befffa |
| 92 | put k 61 0xa4506ceb |
| 93 | put k 62 0xbef9a3f7 |
| 94 | put k 63 0xc67178f2 |
| 95 | end |
| 96 | |
| 97 | note K{512}[0..80): first 64 bits of the fractional parts of the cube roots of the first 80 primes |
| 98 | note (FIPS 180-4 4.2.3). Wolfram: IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^64],16,16]; |
| 99 | note equal to the decimal table of omnet om/sha512/sha512.om. |
| 100 | fn sha2_k512 k edit ulong 80 to void |
| 101 | put k 0 0x428a2f98d728ae22 |
| 102 | put k 1 0x7137449123ef65cd |
| 103 | put k 2 0xb5c0fbcfec4d3b2f |
| 104 | put k 3 0xe9b5dba58189dbbc |
| 105 | put k 4 0x3956c25bf348b538 |
| 106 | put k 5 0x59f111f1b605d019 |
| 107 | put k 6 0x923f82a4af194f9b |
| 108 | put k 7 0xab1c5ed5da6d8118 |
| 109 | put k 8 0xd807aa98a3030242 |
| 110 | put k 9 0x12835b0145706fbe |
| 111 | put k 10 0x243185be4ee4b28c |
| 112 | put k 11 0x550c7dc3d5ffb4e2 |
| 113 | put k 12 0x72be5d74f27b896f |
| 114 | put k 13 0x80deb1fe3b1696b1 |
| 115 | put k 14 0x9bdc06a725c71235 |
| 116 | put k 15 0xc19bf174cf692694 |
| 117 | put k 16 0xe49b69c19ef14ad2 |
| 118 | put k 17 0xefbe4786384f25e3 |
| 119 | put k 18 0x0fc19dc68b8cd5b5 |
| 120 | put k 19 0x240ca1cc77ac9c65 |
| 121 | put k 20 0x2de92c6f592b0275 |
| 122 | put k 21 0x4a7484aa6ea6e483 |
| 123 | put k 22 0x5cb0a9dcbd41fbd4 |
| 124 | put k 23 0x76f988da831153b5 |
| 125 | put k 24 0x983e5152ee66dfab |
| 126 | put k 25 0xa831c66d2db43210 |
| 127 | put k 26 0xb00327c898fb213f |
| 128 | put k 27 0xbf597fc7beef0ee4 |
| 129 | put k 28 0xc6e00bf33da88fc2 |
| 130 | put k 29 0xd5a79147930aa725 |
| 131 | put k 30 0x06ca6351e003826f |
| 132 | put k 31 0x142929670a0e6e70 |
| 133 | put k 32 0x27b70a8546d22ffc |
| 134 | put k 33 0x2e1b21385c26c926 |
| 135 | put k 34 0x4d2c6dfc5ac42aed |
| 136 | put k 35 0x53380d139d95b3df |
| 137 | put k 36 0x650a73548baf63de |
| 138 | put k 37 0x766a0abb3c77b2a8 |
| 139 | put k 38 0x81c2c92e47edaee6 |
| 140 | put k 39 0x92722c851482353b |
| 141 | put k 40 0xa2bfe8a14cf10364 |
| 142 | put k 41 0xa81a664bbc423001 |
| 143 | put k 42 0xc24b8b70d0f89791 |
| 144 | put k 43 0xc76c51a30654be30 |
| 145 | put k 44 0xd192e819d6ef5218 |
| 146 | put k 45 0xd69906245565a910 |
| 147 | put k 46 0xf40e35855771202a |
| 148 | put k 47 0x106aa07032bbd1b8 |
| 149 | put k 48 0x19a4c116b8d2d0c8 |
| 150 | put k 49 0x1e376c085141ab53 |
| 151 | put k 50 0x2748774cdf8eeb99 |
| 152 | put k 51 0x34b0bcb5e19b48a8 |
| 153 | put k 52 0x391c0cb3c5c95a63 |
| 154 | put k 53 0x4ed8aa4ae3418acb |
| 155 | put k 54 0x5b9cca4f7763e373 |
| 156 | put k 55 0x682e6ff3d6b2b8a3 |
| 157 | put k 56 0x748f82ee5defb2fc |
| 158 | put k 57 0x78a5636f43172f60 |
| 159 | put k 58 0x84c87814a1f0ab72 |
| 160 | put k 59 0x8cc702081a6439ec |
| 161 | put k 60 0x90befffa23631e28 |
| 162 | put k 61 0xa4506cebde82bde9 |
| 163 | put k 62 0xbef9a3f7b2c67915 |
| 164 | put k 63 0xc67178f2e372532b |
| 165 | put k 64 0xca273eceea26619c |
| 166 | put k 65 0xd186b8c721c0c207 |
| 167 | put k 66 0xeada7dd6cde0eb1e |
| 168 | put k 67 0xf57d4f7fee6ed178 |
| 169 | put k 68 0x06f067aa72176fba |
| 170 | put k 69 0x0a637dc5a2c898a6 |
| 171 | put k 70 0x113f9804bef90dae |
| 172 | put k 71 0x1b710b35131c471b |
| 173 | put k 72 0x28db77f523047d84 |
| 174 | put k 73 0x32caab7b40c72493 |
| 175 | put k 74 0x3c9ebe0a15c9bebc |
| 176 | put k 75 0x431d67c49c100d4c |
| 177 | put k 76 0x4cc5d4becb3e42b6 |
| 178 | put k 77 0x597f299cfc657e2a |
| 179 | put k 78 0x5fcb6fab3ad6faec |
| 180 | put k 79 0x6c44198c4a475817 |
| 181 | end |
| 182 | |
| 183 | note H(0) (FIPS 180-4 5.3.3 SHA-256, 5.3.4 SHA-384, 5.3.5 SHA-512). Wolfram: fractional parts of |
| 184 | note sqrt(Prime[i]) * 2^32 (i 1..8), * 2^64 (i 1..8 for SHA-512, i 9..16 for SHA-384). |
| 185 | note Any other alg traps 9 (caller contract). |
| 186 | fn sha2_init c edit ulong * alg ulong to void |
| 187 | for i ulong 40 |
| 188 | put c i 0 |
| 189 | end |
| 190 | put c 9 alg |
| 191 | if eq alg 256 |
| 192 | put c 0 0x6a09e667 |
| 193 | put c 1 0xbb67ae85 |
| 194 | put c 2 0x3c6ef372 |
| 195 | put c 3 0xa54ff53a |
| 196 | put c 4 0x510e527f |
| 197 | put c 5 0x9b05688c |
| 198 | put c 6 0x1f83d9ab |
| 199 | put c 7 0x5be0cd19 |
| 200 | ret |
| 201 | end |
| 202 | if eq alg 384 |
| 203 | put c 0 0xcbbb9d5dc1059ed8 |
| 204 | put c 1 0x629a292a367cd507 |
| 205 | put c 2 0x9159015a3070dd17 |
| 206 | put c 3 0x152fecd8f70e5939 |
| 207 | put c 4 0x67332667ffc00b31 |
| 208 | put c 5 0x8eb44a8768581511 |
| 209 | put c 6 0xdb0c2e0d64f98fa7 |
| 210 | put c 7 0x47b5481dbefa4fa4 |
| 211 | ret |
| 212 | end |
| 213 | if eq alg 512 |
| 214 | put c 0 0x6a09e667f3bcc908 |
| 215 | put c 1 0xbb67ae8584caa73b |
| 216 | put c 2 0x3c6ef372fe94f82b |
| 217 | put c 3 0xa54ff53a5f1d36f1 |
| 218 | put c 4 0x510e527fade682d1 |
| 219 | put c 5 0x9b05688c2b3e6c1f |
| 220 | put c 6 0x1f83d9abfb41bd6b |
| 221 | put c 7 0x5be0cd19137e2179 |
| 222 | ret |
| 223 | end |
| 224 | trap 9 |
| 225 | end |
| 226 | |
| 227 | note SHA-256 message schedule (FIPS 180-4 6.2.2 step 1) of the block m[o .. o+64). |
| 228 | fn sha2_s256 w edit uint 64 m view uchar * o ulong to void |
| 229 | q = ulong 0 |
| 230 | x = uint 0 |
| 231 | y = uint 0 |
| 232 | for i ulong 16 unroll |
| 233 | q = add o mul i 4 |
| 234 | x = uint get m q |
| 235 | x = or shl x 8 uint get m add q 1 |
| 236 | x = or shl x 8 uint get m add q 2 |
| 237 | put w i or shl x 8 uint get m add q 3 |
| 238 | end |
| 239 | for i ulong 48 unroll |
| 240 | x = get w add i 1 |
| 241 | y = get w add i 14 |
| 242 | x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3 |
| 243 | put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10 |
| 244 | end |
| 245 | end |
| 246 | |
| 247 | note SHA-256 rounds (6.2.2 steps 2-4). Ch(e,f,g) = g xor (e and (f xor g)); Maj(a,b,c) = |
| 248 | note (a and b) or (c and (a or b)); Sigma1 = ROTR 6,11,25; Sigma0 = ROTR 2,13,22 (4.1.2). |
| 249 | fn sha2_r256 s edit uint 8 w view uint 64 kt view uint 64 to void |
| 250 | a = get s 0 |
| 251 | b = get s 1 |
| 252 | c = get s 2 |
| 253 | d = get s 3 |
| 254 | e = get s 4 |
| 255 | f = get s 5 |
| 256 | g = get s 6 |
| 257 | h = get s 7 |
| 258 | t = uint 0 |
| 259 | for i ulong 64 unroll |
| 260 | h = add h get kt i |
| 261 | h = add h get w i |
| 262 | t = xor rotr e 6 rotr e 11 |
| 263 | t = xor t rotr e 25 |
| 264 | h = add h t |
| 265 | h = add h xor g and e xor f g |
| 266 | d = add d h |
| 267 | t = xor rotr a 2 rotr a 13 |
| 268 | t = xor t rotr a 22 |
| 269 | h = add h t |
| 270 | t = and c or a b |
| 271 | h = add h or and a b t |
| 272 | t = h |
| 273 | h = g |
| 274 | g = f |
| 275 | f = e |
| 276 | e = d |
| 277 | d = c |
| 278 | c = b |
| 279 | b = a |
| 280 | a = t |
| 281 | end |
| 282 | put s 0 add get s 0 a |
| 283 | put s 1 add get s 1 b |
| 284 | put s 2 add get s 2 c |
| 285 | put s 3 add get s 3 d |
| 286 | put s 4 add get s 4 e |
| 287 | put s 5 add get s 5 f |
| 288 | put s 6 add get s 6 g |
| 289 | put s 7 add get s 7 h |
| 290 | end |
| 291 | |
| 292 | note the whole 64-byte blocks of m (len m a multiple of 64, at most 2^25) into c's chaining value |
| 293 | fn sha2_b256 c edit ulong * m view uchar * to void |
| 294 | s = new uint 8 0 |
| 295 | w = new uint 64 0 |
| 296 | kt = new uint 64 0 |
| 297 | sha2_k256 kt |
| 298 | for i ulong 8 unroll |
| 299 | put s i uint get c i |
| 300 | end |
| 301 | for j ulong shr len m 6 |
| 302 | sha2_s256 w m shl j 6 |
| 303 | sha2_r256 s w kt |
| 304 | end |
| 305 | for i ulong 8 unroll |
| 306 | put c i ulong get s i |
| 307 | put s i 0 |
| 308 | end |
| 309 | for i ulong 64 |
| 310 | put w i 0 |
| 311 | end |
| 312 | end |
| 313 | |
| 314 | note SHA-512 message schedule (6.4.2 step 1) of the block m[o .. o+128); sigma0 = ROTR 1,8 SHR 7, |
| 315 | note sigma1 = ROTR 19,61 SHR 6 (4.1.3). |
| 316 | fn sha2_s512 w edit ulong 80 m view uchar * o ulong to void |
| 317 | q = ulong 0 |
| 318 | x = ulong 0 |
| 319 | y = ulong 0 |
| 320 | for i ulong 16 unroll |
| 321 | q = add o mul i 8 |
| 322 | x = ulong get m q |
| 323 | x = or shl x 8 ulong get m add q 1 |
| 324 | x = or shl x 8 ulong get m add q 2 |
| 325 | x = or shl x 8 ulong get m add q 3 |
| 326 | x = or shl x 8 ulong get m add q 4 |
| 327 | x = or shl x 8 ulong get m add q 5 |
| 328 | x = or shl x 8 ulong get m add q 6 |
| 329 | put w i or shl x 8 ulong get m add q 7 |
| 330 | end |
| 331 | for i ulong 64 unroll |
| 332 | x = get w add i 1 |
| 333 | y = get w add i 14 |
| 334 | x = add add get w i get w add i 9 xor xor rotr x 1 rotr x 8 shr x 7 |
| 335 | put w add i 16 add x xor xor rotr y 19 rotr y 61 shr y 6 |
| 336 | end |
| 337 | end |
| 338 | |
| 339 | note SHA-512 rounds (6.4.2 steps 2-4), a rolled loop (unroll allows at most 64 copies); Sigma1 = ROTR 14,18,41; Sigma0 = ROTR 28,34,39 (4.1.3). |
| 340 | fn sha2_r512 s edit ulong 8 w view ulong 80 kt view ulong 80 to void |
| 341 | a = get s 0 |
| 342 | b = get s 1 |
| 343 | c = get s 2 |
| 344 | d = get s 3 |
| 345 | e = get s 4 |
| 346 | f = get s 5 |
| 347 | g = get s 6 |
| 348 | h = get s 7 |
| 349 | t = ulong 0 |
| 350 | for i ulong 80 |
| 351 | h = add h get kt i |
| 352 | h = add h get w i |
| 353 | t = xor rotr e 14 rotr e 18 |
| 354 | t = xor t rotr e 41 |
| 355 | h = add h t |
| 356 | h = add h xor g and e xor f g |
| 357 | d = add d h |
| 358 | t = xor rotr a 28 rotr a 34 |
| 359 | t = xor t rotr a 39 |
| 360 | h = add h t |
| 361 | t = and c or a b |
| 362 | h = add h or and a b t |
| 363 | t = h |
| 364 | h = g |
| 365 | g = f |
| 366 | f = e |
| 367 | e = d |
| 368 | d = c |
| 369 | c = b |
| 370 | b = a |
| 371 | a = t |
| 372 | end |
| 373 | put s 0 add get s 0 a |
| 374 | put s 1 add get s 1 b |
| 375 | put s 2 add get s 2 c |
| 376 | put s 3 add get s 3 d |
| 377 | put s 4 add get s 4 e |
| 378 | put s 5 add get s 5 f |
| 379 | put s 6 add get s 6 g |
| 380 | put s 7 add get s 7 h |
| 381 | end |
| 382 | |
| 383 | note the whole 128-byte blocks of m (len m a multiple of 128, at most 2^25) into c |
| 384 | fn sha2_b512 c edit ulong * m view uchar * to void |
| 385 | s = new ulong 8 0 |
| 386 | w = new ulong 80 0 |
| 387 | kt = new ulong 80 0 |
| 388 | sha2_k512 kt |
| 389 | for i ulong 8 unroll |
| 390 | put s i get c i |
| 391 | end |
| 392 | for j ulong shr len m 7 |
| 393 | sha2_s512 w m shl j 7 |
| 394 | sha2_r512 s w kt |
| 395 | end |
| 396 | for i ulong 8 unroll |
| 397 | put c i get s i |
| 398 | put s i 0 |
| 399 | end |
| 400 | for i ulong 80 |
| 401 | put w i 0 |
| 402 | end |
| 403 | end |
| 404 | |
| 405 | note whole blocks of m (len m a multiple of the block size) in runs of at most 2^25 bytes |
| 406 | fn sha2_run c edit ulong * m view uchar * to void |
| 407 | n = len m |
| 408 | q = ulong 0 |
| 409 | k = ulong 0 |
| 410 | while lt q n bound 999999 |
| 411 | k = sub n q |
| 412 | if gt k 0x2000000 |
| 413 | k = 0x2000000 |
| 414 | end |
| 415 | if eq get c 9 256 |
| 416 | sha2_b256 c part m q k |
| 417 | else |
| 418 | sha2_b512 c part m q k |
| 419 | end |
| 420 | q = add q k |
| 421 | end |
| 422 | end |
| 423 | |
| 424 | note pending byte i (i < 128) of the packed buffer |
| 425 | fn sha2_bget c view ulong * i ulong to uchar |
| 426 | x = shr get c add 16 shr i 3 sub 56 shl and i 7 3 |
| 427 | ret uchar and x 0xff |
| 428 | end |
| 429 | |
| 430 | note set pending byte i of the packed buffer to v |
| 431 | fn sha2_bput c edit ulong * i ulong v uchar to void |
| 432 | q = add 16 shr i 3 |
| 433 | sh = sub 56 shl and i 7 3 |
| 434 | x = and get c q not shl ulong 0xff sh |
| 435 | put c q or x shl ulong v sh |
| 436 | end |
| 437 | |
| 438 | note the bytes m[0..k) into the buffer at bi (bi + k <= block size) |
| 439 | fn sha2_fill c edit ulong * bi ulong m view uchar * k ulong to void |
| 440 | for i ulong k |
| 441 | sha2_bput c add bi i get m i |
| 442 | end |
| 443 | end |
| 444 | |
| 445 | fn sha2_update c edit ulong * m view uchar * to void |
| 446 | blk = new uchar 128 0 |
| 447 | n = len m |
| 448 | cnt = get c 8 |
| 449 | bs = ulong 128 |
| 450 | if eq get c 9 256 |
| 451 | bs = 64 |
| 452 | end |
| 453 | bi = and cnt sub bs 1 |
| 454 | put c 8 add cnt n |
| 455 | off = ulong 0 |
| 456 | if gt bi 0 |
| 457 | off = sub bs bi |
| 458 | if lt n off |
| 459 | off = n |
| 460 | end |
| 461 | sha2_fill c bi m off |
| 462 | if eq add bi off bs |
| 463 | for i ulong bs |
| 464 | put blk i sha2_bget c i |
| 465 | end |
| 466 | sha2_run c part blk 0 bs |
| 467 | for i ulong bs |
| 468 | put blk i 0 |
| 469 | end |
| 470 | end |
| 471 | end |
| 472 | nb = and sub n off not sub bs 1 |
| 473 | sha2_run c part m off nb |
| 474 | off = add off nb |
| 475 | sha2_fill c 0 part m off sub n off sub n off |
| 476 | end |
| 477 | |
| 478 | note H(N) as big-endian bytes: 32 (SHA-256, 8 words of 32 bits), 48 (SHA-384, first 6 words), 64 |
| 479 | fn sha2_put c view ulong * o edit uchar * to ulong |
| 480 | alg = get c 9 |
| 481 | sh = ulong 0 |
| 482 | if eq alg 256 |
| 483 | for i ulong 32 |
| 484 | sh = sub 24 shl and i 3 3 |
| 485 | put o i uchar and shr get c shr i 2 sh 0xff |
| 486 | end |
| 487 | ret 32 |
| 488 | end |
| 489 | dl = ulong 64 |
| 490 | if eq alg 384 |
| 491 | dl = 48 |
| 492 | end |
| 493 | for i ulong dl |
| 494 | sh = sub 56 shl and i 7 3 |
| 495 | put o i uchar and shr get c shr i 3 sh 0xff |
| 496 | end |
| 497 | ret dl |
| 498 | end |
| 499 | |
| 500 | note padding (FIPS 180-4 5.1.1 / 5.1.2): 0x80, zeros, the bit length big-endian in the last 8 (SHA-256) |
| 501 | note or 16 (SHA-384/512) bytes of one or two blocks; returns the digest length |
| 502 | fn sha2_fin c edit ulong * o edit uchar * to ulong |
| 503 | p = new uchar 256 0 |
| 504 | bs = ulong 128 |
| 505 | ls = ulong 16 |
| 506 | if eq get c 9 256 |
| 507 | bs = 64 |
| 508 | ls = 8 |
| 509 | end |
| 510 | cnt = get c 8 |
| 511 | k = and cnt sub bs 1 |
| 512 | for i ulong k |
| 513 | put p i sha2_bget c i |
| 514 | end |
| 515 | put p k 0x80 |
| 516 | e = bs |
| 517 | if ge add k ls bs |
| 518 | e = shl bs 1 |
| 519 | end |
| 520 | x = shl cnt 3 |
| 521 | for i ulong 8 unroll |
| 522 | put p sub e add i 1 uchar and shr x mul i 8 0xff |
| 523 | end |
| 524 | if eq ls 16 |
| 525 | put p sub e 9 uchar shr cnt 61 |
| 526 | end |
| 527 | sha2_run c part p 0 e |
| 528 | for i ulong 256 |
| 529 | put p i 0 |
| 530 | end |
| 531 | ret sha2_put c o |
| 532 | end |
| 533 | |
| 534 | fn sha256_init c edit ulong * to void |
| 535 | sha2_init c 256 |
| 536 | end |
| 537 | |
| 538 | fn sha384_init c edit ulong * to void |
| 539 | sha2_init c 384 |
| 540 | end |
| 541 | |
| 542 | fn sha512_init c edit ulong * to void |
| 543 | sha2_init c 512 |
| 544 | end |
| 545 | |
| 546 | fn sha256_update c edit ulong * m view uchar * to void |
| 547 | sha2_update c m |
| 548 | end |
| 549 | |
| 550 | fn sha384_update c edit ulong * m view uchar * to void |
| 551 | sha2_update c m |
| 552 | end |
| 553 | |
| 554 | fn sha512_update c edit ulong * m view uchar * to void |
| 555 | sha2_update c m |
| 556 | end |
| 557 | |
| 558 | fn sha256_final c edit ulong * o edit uchar * to void |
| 559 | dl = sha2_fin c o |
| 560 | end |
| 561 | |
| 562 | fn sha384_final c edit ulong * o edit uchar * to void |
| 563 | dl = sha2_fin c o |
| 564 | end |
| 565 | |
| 566 | fn sha512_final c edit ulong * o edit uchar * to void |
| 567 | dl = sha2_fin c o |
| 568 | end |
| 569 | |
| 570 | fn sha2_copy d edit ulong * s view ulong * to void |
| 571 | for i ulong len s |
| 572 | put d i get s i |
| 573 | end |
| 574 | end |
| 575 | |
| 576 | fn sha2_digest alg ulong m view uchar * o edit uchar * to ulong |
| 577 | c = new ulong 40 0 |
| 578 | if ne alg 256 |
| 579 | if ne alg 384 |
| 580 | if ne alg 512 |
| 581 | ret 0 |
| 582 | end |
| 583 | end |
| 584 | end |
| 585 | sha2_init c alg |
| 586 | sha2_update c m |
| 587 | dl = sha2_fin c o |
| 588 | for i ulong 40 |
| 589 | put c i 0 |
| 590 | end |
| 591 | ret dl |
| 592 | end |