omega-gpu-driver
aien-dev/omega-gpu-driver
Brought by drakestapleton
omega-gpu-driver / gsp / sha2.om
| 1 | note gsp/sha2.om: SHA-256 and SHA-384 (FIPS 180-4) in seeded Omega (OM-1), for the GB10 firmware |
| 2 | note inputs. Ports the digests the aienux PR #441 tools use: argus/sha256.c (sha256_init/update/final, |
| 3 | note used by ck_gsp_fw_inspect.c and ck_gsp_bindata_extract.c) and the SHA-384 of |
| 4 | note ck_gsp_bindata_extract.c lines 84-197 (sha512_block, sha384). Library unit: no main. |
| 5 | note Prefix sh. Input is always the window shin[off..off+n), n <= SHWIN; callers copy or pread into it. |
| 6 | note SHA-256: eight independent streaming contexts cx 0..7 (shh, shtot, shbn, shbf), so one pass over a |
| 7 | note file can feed several digests. SHA-384: one streaming context (sh5h, sh5t, sh5n, sh5b). |
| 8 | note A digest lands in shout (32 or 48 bytes); shhex n writes its lowercase hex into shhx[0..2n). |
| 9 | note Constants: K256 = first 32 bits of the fractional parts of the cube roots of the first 64 primes, |
| 10 | note K512 = first 64 bits for the first 80 primes, IV256 = square roots of the first 8 primes (32 bits), |
| 11 | note IV384 = square roots of primes 9..16 (64 bits). All decimal, computed by WolframLanguageEvaluator: |
| 12 | note Table[Floor[FractionalPart[N[CubeRoot[Prime[i]],120]]*2^64],{i,80}] (and the 2^32 / Sqrt forms); |
| 13 | note spot checks against the C hex: K512[0] 0x428a2f98d728ae22, K512[79] 0x6c44198c4a475817, |
| 14 | note IV384[0] 0xcbbb9d5dc1059ed8, IV384[7] 0x47b5481dbefa4fa4, K256[63] 0xc67178f2. |
| 15 | const SHWIN ulong 524288 |
| 16 | global shin uchar 524288 |
| 17 | global shok ulong 1 |
| 18 | global shk uint 64 |
| 19 | global shk5 ulong 80 |
| 20 | global shh uint 64 |
| 21 | global shv uint 8 |
| 22 | global shtot ulong 8 |
| 23 | global shbn ulong 8 |
| 24 | global shbf uchar 512 |
| 25 | global shw uint 64 |
| 26 | global sh5h ulong 8 |
| 27 | global sh5t ulong 1 |
| 28 | global sh5n ulong 1 |
| 29 | global sh5b uchar 128 |
| 30 | global sh5w ulong 80 |
| 31 | global shout uchar 64 |
| 32 | global shhx uchar 128 |
| 33 | fn shsetup to void |
| 34 | if eq get shok 0 1 |
| 35 | ret |
| 36 | end |
| 37 | put shk 0 1116352408 |
| 38 | put shk 1 1899447441 |
| 39 | put shk 2 3049323471 |
| 40 | put shk 3 3921009573 |
| 41 | put shk 4 961987163 |
| 42 | put shk 5 1508970993 |
| 43 | put shk 6 2453635748 |
| 44 | put shk 7 2870763221 |
| 45 | put shk 8 3624381080 |
| 46 | put shk 9 310598401 |
| 47 | put shk 10 607225278 |
| 48 | put shk 11 1426881987 |
| 49 | put shk 12 1925078388 |
| 50 | put shk 13 2162078206 |
| 51 | put shk 14 2614888103 |
| 52 | put shk 15 3248222580 |
| 53 | put shk 16 3835390401 |
| 54 | put shk 17 4022224774 |
| 55 | put shk 18 264347078 |
| 56 | put shk 19 604807628 |
| 57 | put shk 20 770255983 |
| 58 | put shk 21 1249150122 |
| 59 | put shk 22 1555081692 |
| 60 | put shk 23 1996064986 |
| 61 | put shk 24 2554220882 |
| 62 | put shk 25 2821834349 |
| 63 | put shk 26 2952996808 |
| 64 | put shk 27 3210313671 |
| 65 | put shk 28 3336571891 |
| 66 | put shk 29 3584528711 |
| 67 | put shk 30 113926993 |
| 68 | put shk 31 338241895 |
| 69 | put shk 32 666307205 |
| 70 | put shk 33 773529912 |
| 71 | put shk 34 1294757372 |
| 72 | put shk 35 1396182291 |
| 73 | put shk 36 1695183700 |
| 74 | put shk 37 1986661051 |
| 75 | put shk 38 2177026350 |
| 76 | put shk 39 2456956037 |
| 77 | put shk 40 2730485921 |
| 78 | put shk 41 2820302411 |
| 79 | put shk 42 3259730800 |
| 80 | put shk 43 3345764771 |
| 81 | put shk 44 3516065817 |
| 82 | put shk 45 3600352804 |
| 83 | put shk 46 4094571909 |
| 84 | put shk 47 275423344 |
| 85 | put shk 48 430227734 |
| 86 | put shk 49 506948616 |
| 87 | put shk 50 659060556 |
| 88 | put shk 51 883997877 |
| 89 | put shk 52 958139571 |
| 90 | put shk 53 1322822218 |
| 91 | put shk 54 1537002063 |
| 92 | put shk 55 1747873779 |
| 93 | put shk 56 1955562222 |
| 94 | put shk 57 2024104815 |
| 95 | put shk 58 2227730452 |
| 96 | put shk 59 2361852424 |
| 97 | put shk 60 2428436474 |
| 98 | put shk 61 2756734187 |
| 99 | put shk 62 3204031479 |
| 100 | put shk 63 3329325298 |
| 101 | put shk5 0 4794697086780616226 |
| 102 | put shk5 1 8158064640168781261 |
| 103 | put shk5 2 13096744586834688815 |
| 104 | put shk5 3 16840607885511220156 |
| 105 | put shk5 4 4131703408338449720 |
| 106 | put shk5 5 6480981068601479193 |
| 107 | put shk5 6 10538285296894168987 |
| 108 | put shk5 7 12329834152419229976 |
| 109 | put shk5 8 15566598209576043074 |
| 110 | put shk5 9 1334009975649890238 |
| 111 | put shk5 10 2608012711638119052 |
| 112 | put shk5 11 6128411473006802146 |
| 113 | put shk5 12 8268148722764581231 |
| 114 | put shk5 13 9286055187155687089 |
| 115 | put shk5 14 11230858885718282805 |
| 116 | put shk5 15 13951009754708518548 |
| 117 | put shk5 16 16472876342353939154 |
| 118 | put shk5 17 17275323862435702243 |
| 119 | put shk5 18 1135362057144423861 |
| 120 | put shk5 19 2597628984639134821 |
| 121 | put shk5 20 3308224258029322869 |
| 122 | put shk5 21 5365058923640841347 |
| 123 | put shk5 22 6679025012923562964 |
| 124 | put shk5 23 8573033837759648693 |
| 125 | put shk5 24 10970295158949994411 |
| 126 | put shk5 25 12119686244451234320 |
| 127 | put shk5 26 12683024718118986047 |
| 128 | put shk5 27 13788192230050041572 |
| 129 | put shk5 28 14330467153632333762 |
| 130 | put shk5 29 15395433587784984357 |
| 131 | put shk5 30 489312712824947311 |
| 132 | put shk5 31 1452737877330783856 |
| 133 | put shk5 32 2861767655752347644 |
| 134 | put shk5 33 3322285676063803686 |
| 135 | put shk5 34 5560940570517711597 |
| 136 | put shk5 35 5996557281743188959 |
| 137 | put shk5 36 7280758554555802590 |
| 138 | put shk5 37 8532644243296465576 |
| 139 | put shk5 38 9350256976987008742 |
| 140 | put shk5 39 10552545826968843579 |
| 141 | put shk5 40 11727347734174303076 |
| 142 | put shk5 41 12113106623233404929 |
| 143 | put shk5 42 14000437183269869457 |
| 144 | put shk5 43 14369950271660146224 |
| 145 | put shk5 44 15101387698204529176 |
| 146 | put shk5 45 15463397548674623760 |
| 147 | put shk5 46 17586052441742319658 |
| 148 | put shk5 47 1182934255886127544 |
| 149 | put shk5 48 1847814050463011016 |
| 150 | put shk5 49 2177327727835720531 |
| 151 | put shk5 50 2830643537854262169 |
| 152 | put shk5 51 3796741975233480872 |
| 153 | put shk5 52 4115178125766777443 |
| 154 | put shk5 53 5681478168544905931 |
| 155 | put shk5 54 6601373596472566643 |
| 156 | put shk5 55 7507060721942968483 |
| 157 | put shk5 56 8399075790359081724 |
| 158 | put shk5 57 8693463985226723168 |
| 159 | put shk5 58 9568029438360202098 |
| 160 | put shk5 59 10144078919501101548 |
| 161 | put shk5 60 10430055236837252648 |
| 162 | put shk5 61 11840083180663258601 |
| 163 | put shk5 62 13761210420658862357 |
| 164 | put shk5 63 14299343276471374635 |
| 165 | put shk5 64 14566680578165727644 |
| 166 | put shk5 65 15097957966210449927 |
| 167 | put shk5 66 16922976911328602910 |
| 168 | put shk5 67 17689382322260857208 |
| 169 | put shk5 68 500013540394364858 |
| 170 | put shk5 69 748580250866718886 |
| 171 | put shk5 70 1242879168328830382 |
| 172 | put shk5 71 1977374033974150939 |
| 173 | put shk5 72 2944078676154940804 |
| 174 | put shk5 73 3659926193048069267 |
| 175 | put shk5 74 4368137639120453308 |
| 176 | put shk5 75 4836135668995329356 |
| 177 | put shk5 76 5532061633213252278 |
| 178 | put shk5 77 6448918945643986474 |
| 179 | put shk5 78 6902733635092675308 |
| 180 | put shk5 79 7801388544844847127 |
| 181 | put shok 0 1 |
| 182 | end |
| 183 | note SHA-256 context cx (0..7): chaining value shh[8cx..8cx+8), bytes absorbed shtot[cx], pending |
| 184 | note block shbf[64cx..64cx+shbn[cx]) |
| 185 | fn sh2init cx ulong to void |
| 186 | shsetup |
| 187 | b = mul cx 8 |
| 188 | put shh b 1779033703 |
| 189 | put shh add b 1 3144134277 |
| 190 | put shh add b 2 1013904242 |
| 191 | put shh add b 3 2773480762 |
| 192 | put shh add b 4 1359893119 |
| 193 | put shh add b 5 2600822924 |
| 194 | put shh add b 6 528734635 |
| 195 | put shh add b 7 1541459225 |
| 196 | put shtot cx 0 |
| 197 | put shbn cx 0 |
| 198 | end |
| 199 | note the 64 rounds over the schedule in shw, folded into the chaining value in shv (16 registers: |
| 200 | note the state lives in a..h, the sums are built one term at a time) |
| 201 | fn sh2rnd to void |
| 202 | a = get shv 0 |
| 203 | b = get shv 1 |
| 204 | c = get shv 2 |
| 205 | d = get shv 3 |
| 206 | e = get shv 4 |
| 207 | f = get shv 5 |
| 208 | g = get shv 6 |
| 209 | h = get shv 7 |
| 210 | for t ulong 64 |
| 211 | x = or shr e 6 shl e 26 |
| 212 | x = xor x or shr e 11 shl e 21 |
| 213 | x = xor x or shr e 25 shl e 7 |
| 214 | x = add x h |
| 215 | x = add x xor and e f and not e g |
| 216 | x = add x get shk t |
| 217 | x = add x get shw t |
| 218 | h = g |
| 219 | g = f |
| 220 | f = e |
| 221 | e = add d x |
| 222 | d = c |
| 223 | c = b |
| 224 | b = a |
| 225 | y = or shr b 2 shl b 30 |
| 226 | y = xor y or shr b 13 shl b 19 |
| 227 | y = xor y or shr b 22 shl b 10 |
| 228 | x = add x y |
| 229 | y = xor and b c and b d |
| 230 | y = xor y and c d |
| 231 | a = add x y |
| 232 | end |
| 233 | put shv 0 add get shv 0 a |
| 234 | put shv 1 add get shv 1 b |
| 235 | put shv 2 add get shv 2 c |
| 236 | put shv 3 add get shv 3 d |
| 237 | put shv 4 add get shv 4 e |
| 238 | put shv 5 add get shv 5 f |
| 239 | put shv 6 add get shv 6 g |
| 240 | put shv 7 add get shv 7 h |
| 241 | end |
| 242 | note one 64-byte block: src 0 = shin[off..off+64), src 1 = shbf[off..off+64) |
| 243 | fn sh2comp cx ulong src ulong off ulong to void |
| 244 | for t ulong 16 |
| 245 | p = add off mul t 4 |
| 246 | if eq src 0 |
| 247 | v = or shl uint get shin p 24 shl uint get shin add p 1 16 |
| 248 | v = or v or shl uint get shin add p 2 8 uint get shin add p 3 |
| 249 | put shw t v |
| 250 | else |
| 251 | v = or shl uint get shbf p 24 shl uint get shbf add p 1 16 |
| 252 | v = or v or shl uint get shbf add p 2 8 uint get shbf add p 3 |
| 253 | put shw t v |
| 254 | end |
| 255 | end |
| 256 | for u ulong 48 |
| 257 | i = add u 16 |
| 258 | x = get shw sub i 2 |
| 259 | s = or shr x 17 shl x 15 |
| 260 | s = xor s or shr x 19 shl x 13 |
| 261 | s = xor s shr x 10 |
| 262 | y = get shw sub i 15 |
| 263 | r = or shr y 7 shl y 25 |
| 264 | r = xor r or shr y 18 shl y 14 |
| 265 | r = xor r shr y 3 |
| 266 | s = add s get shw sub i 7 |
| 267 | r = add r get shw sub i 16 |
| 268 | put shw i add s r |
| 269 | end |
| 270 | bs = mul cx 8 |
| 271 | for j ulong 8 |
| 272 | put shv j get shh add bs j |
| 273 | end |
| 274 | sh2rnd |
| 275 | for j2 ulong 8 |
| 276 | put shh add bs j2 get shv j2 |
| 277 | end |
| 278 | end |
| 279 | note absorb shin[off..off+n) into context cx; n <= SHWIN |
| 280 | fn sh2upd cx ulong off ulong n ulong to void |
| 281 | if gt n SHWIN |
| 282 | trap 3 |
| 283 | end |
| 284 | bo = mul cx 64 |
| 285 | i = ulong 0 |
| 286 | while lt i n bound 999999 |
| 287 | bn = get shbn cx |
| 288 | if and eq bn 0 le add i 64 n |
| 289 | sh2comp cx 0 add off i |
| 290 | i = add i 64 |
| 291 | else |
| 292 | put shbf add bo bn get shin add off i |
| 293 | bn = add bn 1 |
| 294 | i = add i 1 |
| 295 | if eq bn 64 |
| 296 | sh2comp cx 1 bo |
| 297 | bn = ulong 0 |
| 298 | end |
| 299 | put shbn cx bn |
| 300 | end |
| 301 | end |
| 302 | put shtot cx add get shtot cx n |
| 303 | end |
| 304 | note pad and finish context cx: the 32-byte digest goes to shout[0..32) |
| 305 | fn sh2fin cx ulong to void |
| 306 | bo = mul cx 64 |
| 307 | bits = mul get shtot cx 8 |
| 308 | bn = get shbn cx |
| 309 | put shbf add bo bn 128 |
| 310 | bn = add bn 1 |
| 311 | if gt bn 56 |
| 312 | while lt bn 64 bound 64 |
| 313 | put shbf add bo bn 0 |
| 314 | bn = add bn 1 |
| 315 | end |
| 316 | sh2comp cx 1 bo |
| 317 | bn = ulong 0 |
| 318 | end |
| 319 | while lt bn 56 bound 64 |
| 320 | put shbf add bo bn 0 |
| 321 | bn = add bn 1 |
| 322 | end |
| 323 | for k ulong 8 |
| 324 | bn = and shr bits mul sub 7 k 8 255 |
| 325 | put shbf add bo add 56 k uchar bn |
| 326 | end |
| 327 | sh2comp cx 1 bo |
| 328 | put shbn cx 0 |
| 329 | bo = mul cx 8 |
| 330 | for j ulong 8 |
| 331 | bits = ulong get shh add bo j |
| 332 | bn = mul j 4 |
| 333 | put shout bn uchar and shr bits 24 255 |
| 334 | put shout add bn 1 uchar and shr bits 16 255 |
| 335 | put shout add bn 2 uchar and shr bits 8 255 |
| 336 | put shout add bn 3 uchar and bits 255 |
| 337 | end |
| 338 | end |
| 339 | note hash bytes [off, off+n) of the open file fd into context cx through shin; returns 0, the negative |
| 340 | note errno of a failed pread, or -5 (EIO) when the file ends early |
| 341 | fn sh2file cx ulong fd int off ulong n ulong to long |
| 342 | p = ulong 0 |
| 343 | while lt p n bound 999999 |
| 344 | k = sub n p |
| 345 | if gt k SHWIN |
| 346 | k = SHWIN |
| 347 | end |
| 348 | q = ulong 0 |
| 349 | while lt q k bound 999999 |
| 350 | r = pread fd shin q sub k q long add add off p q |
| 351 | if lt r 0 |
| 352 | ret r |
| 353 | end |
| 354 | if eq r 0 |
| 355 | ret -5 |
| 356 | end |
| 357 | q = add q ulong r |
| 358 | end |
| 359 | sh2upd cx 0 k |
| 360 | p = add p k |
| 361 | end |
| 362 | ret 0 |
| 363 | end |
| 364 | note SHA-384 (one context) |
| 365 | fn sh5init to void |
| 366 | shsetup |
| 367 | put sh5h 0 14680500436340154072 |
| 368 | put sh5h 1 7105036623409894663 |
| 369 | put sh5h 2 10473403895298186519 |
| 370 | put sh5h 3 1526699215303891257 |
| 371 | put sh5h 4 7436329637833083697 |
| 372 | put sh5h 5 10282925794625328401 |
| 373 | put sh5h 6 15784041429090275239 |
| 374 | put sh5h 7 5167115440072839076 |
| 375 | put sh5t 0 0 |
| 376 | put sh5n 0 0 |
| 377 | end |
| 378 | fn sh5rnd to void |
| 379 | a = get sh5h 0 |
| 380 | b = get sh5h 1 |
| 381 | c = get sh5h 2 |
| 382 | d = get sh5h 3 |
| 383 | e = get sh5h 4 |
| 384 | f = get sh5h 5 |
| 385 | g = get sh5h 6 |
| 386 | h = get sh5h 7 |
| 387 | for t ulong 80 |
| 388 | x = or shr e 14 shl e 50 |
| 389 | x = xor x or shr e 18 shl e 46 |
| 390 | x = xor x or shr e 41 shl e 23 |
| 391 | x = add x h |
| 392 | x = add x xor and e f and not e g |
| 393 | x = add x get shk5 t |
| 394 | x = add x get sh5w t |
| 395 | h = g |
| 396 | g = f |
| 397 | f = e |
| 398 | e = add d x |
| 399 | d = c |
| 400 | c = b |
| 401 | b = a |
| 402 | y = or shr b 28 shl b 36 |
| 403 | y = xor y or shr b 34 shl b 30 |
| 404 | y = xor y or shr b 39 shl b 25 |
| 405 | x = add x y |
| 406 | y = xor and b c and b d |
| 407 | y = xor y and c d |
| 408 | a = add x y |
| 409 | end |
| 410 | put sh5h 0 add get sh5h 0 a |
| 411 | put sh5h 1 add get sh5h 1 b |
| 412 | put sh5h 2 add get sh5h 2 c |
| 413 | put sh5h 3 add get sh5h 3 d |
| 414 | put sh5h 4 add get sh5h 4 e |
| 415 | put sh5h 5 add get sh5h 5 f |
| 416 | put sh5h 6 add get sh5h 6 g |
| 417 | put sh5h 7 add get sh5h 7 h |
| 418 | end |
| 419 | note one 128-byte block: src 0 = shin[off..off+128), src 1 = sh5b |
| 420 | fn sh5comp src ulong off ulong to void |
| 421 | for t ulong 16 |
| 422 | v = ulong 0 |
| 423 | for k ulong 8 |
| 424 | if eq src 0 |
| 425 | v = or shl v 8 ulong get shin add add off mul t 8 k |
| 426 | else |
| 427 | v = or shl v 8 ulong get sh5b add mul t 8 k |
| 428 | end |
| 429 | end |
| 430 | put sh5w t v |
| 431 | end |
| 432 | for u ulong 64 |
| 433 | i = add u 16 |
| 434 | x = get sh5w sub i 2 |
| 435 | s = or shr x 19 shl x 45 |
| 436 | s = xor s or shr x 61 shl x 3 |
| 437 | s = xor s shr x 6 |
| 438 | y = get sh5w sub i 15 |
| 439 | r = or shr y 1 shl y 63 |
| 440 | r = xor r or shr y 8 shl y 56 |
| 441 | r = xor r shr y 7 |
| 442 | s = add s get sh5w sub i 7 |
| 443 | r = add r get sh5w sub i 16 |
| 444 | put sh5w i add s r |
| 445 | end |
| 446 | sh5rnd |
| 447 | end |
| 448 | fn sh5upd off ulong n ulong to void |
| 449 | if gt n SHWIN |
| 450 | trap 3 |
| 451 | end |
| 452 | i = ulong 0 |
| 453 | while lt i n bound 999999 |
| 454 | bn = get sh5n 0 |
| 455 | if and eq bn 0 le add i 128 n |
| 456 | sh5comp 0 add off i |
| 457 | i = add i 128 |
| 458 | else |
| 459 | put sh5b bn get shin add off i |
| 460 | bn = add bn 1 |
| 461 | i = add i 1 |
| 462 | if eq bn 128 |
| 463 | sh5comp 1 0 |
| 464 | bn = ulong 0 |
| 465 | end |
| 466 | put sh5n 0 bn |
| 467 | end |
| 468 | end |
| 469 | put sh5t 0 add get sh5t 0 n |
| 470 | end |
| 471 | note pad and finish: the 48-byte digest goes to shout[0..48) (message length below 2^61 bytes) |
| 472 | fn sh5fin to void |
| 473 | bits = mul get sh5t 0 8 |
| 474 | bn = get sh5n 0 |
| 475 | put sh5b bn 128 |
| 476 | bn = add bn 1 |
| 477 | if gt bn 112 |
| 478 | while lt bn 128 bound 128 |
| 479 | put sh5b bn 0 |
| 480 | bn = add bn 1 |
| 481 | end |
| 482 | sh5comp 1 0 |
| 483 | bn = ulong 0 |
| 484 | end |
| 485 | while lt bn 120 bound 128 |
| 486 | put sh5b bn 0 |
| 487 | bn = add bn 1 |
| 488 | end |
| 489 | for k ulong 8 |
| 490 | bn = and shr bits mul sub 7 k 8 255 |
| 491 | put sh5b add 120 k uchar bn |
| 492 | end |
| 493 | sh5comp 1 0 |
| 494 | put sh5n 0 0 |
| 495 | for j ulong 48 |
| 496 | bits = get sh5h div j 8 |
| 497 | bn = and shr bits mul sub 7 rem j 8 8 255 |
| 498 | put shout j uchar bn |
| 499 | end |
| 500 | end |
| 501 | note lowercase hex of shout[0..n) into shhx[0..2n), n <= 64 |
| 502 | fn shhex n ulong to void |
| 503 | for i ulong n |
| 504 | b = get shout i |
| 505 | hi = shr b 4 |
| 506 | lo = and b 15 |
| 507 | if lt hi 10 |
| 508 | put shhx mul i 2 add hi 48 |
| 509 | else |
| 510 | put shhx mul i 2 add hi 87 |
| 511 | end |
| 512 | if lt lo 10 |
| 513 | put shhx add mul i 2 1 add lo 48 |
| 514 | else |
| 515 | put shhx add mul i 2 1 add lo 87 |
| 516 | end |
| 517 | end |
| 518 | end |