## Secrets and hashing for auth (DESIGN 3.2, #17): the player/GM key generator, the session token ## generator and SHA-256 for session-token hashes. ## Choices recorded FIXED (owner-delegated, #17), DESIGN 3.2/8: ## - Player key: 20 secret characters in 5 groups of 4 joined by '-', alphabet `KeyAlphabet` ## (no 0/O, 1/I/L), from the OS CSPRNG via std/sysrand (getrandom), ~99 bits. ## - Session token: 32 random bytes, hex encoded (64 chars). Only `sha256Hex(token)` is stored. ## - SHA-256 is implemented here (FIPS 180-4) because the pinned Nim stdlib has no SHA-2 and the ## project takes no crypto dependency. Known-answer tests in tests/tcrypto.nim. import std/[strutils, sysrand] const KeyAlphabet* = "ABCDEFGHJKMNPQRSTUVWXYZ23456789" ## 31 symbols: A-Z without I, L, O; digits 2-9. 31^20 ~ 2^99. KeyLength* = 20 KeyGroups* = 5 KeyGroupSize* = 4 SessionTokenBytes* = 32 const HexDigits = "0123456789abcdef" proc toHexLower*(bytes: openArray[byte]): string = result = newStringOfCap(bytes.len * 2) for b in bytes: result.add HexDigits[int(b shr 4)] result.add HexDigits[int(b and 0x0f)] proc bytesOf*(s: string): seq[byte] = result = newSeq[byte](s.len) for i, c in s: result[i] = byte(c) const Sha256K: array[64, uint32] = [ 0x428a2f98'u32, 0x71374491'u32, 0xb5c0fbcf'u32, 0xe9b5dba5'u32, 0x3956c25b'u32, 0x59f111f1'u32, 0x923f82a4'u32, 0xab1c5ed5'u32, 0xd807aa98'u32, 0x12835b01'u32, 0x243185be'u32, 0x550c7dc3'u32, 0x72be5d74'u32, 0x80deb1fe'u32, 0x9bdc06a7'u32, 0xc19bf174'u32, 0xe49b69c1'u32, 0xefbe4786'u32, 0x0fc19dc6'u32, 0x240ca1cc'u32, 0x2de92c6f'u32, 0x4a7484aa'u32, 0x5cb0a9dc'u32, 0x76f988da'u32, 0x983e5152'u32, 0xa831c66d'u32, 0xb00327c8'u32, 0xbf597fc7'u32, 0xc6e00bf3'u32, 0xd5a79147'u32, 0x06ca6351'u32, 0x14292967'u32, 0x27b70a85'u32, 0x2e1b2138'u32, 0x4d2c6dfc'u32, 0x53380d13'u32, 0x650a7354'u32, 0x766a0abb'u32, 0x81c2c92e'u32, 0x92722c85'u32, 0xa2bfe8a1'u32, 0xa81a664b'u32, 0xc24b8b70'u32, 0xc76c51a3'u32, 0xd192e819'u32, 0xd6990624'u32, 0xf40e3585'u32, 0x106aa070'u32, 0x19a4c116'u32, 0x1e376c08'u32, 0x2748774c'u32, 0x34b0bcb5'u32, 0x391c0cb3'u32, 0x4ed8aa4a'u32, 0x5b9cca4f'u32, 0x682e6ff3'u32, 0x748f82ee'u32, 0x78a5636f'u32, 0x84c87814'u32, 0x8cc70208'u32, 0x90befffa'u32, 0xa4506ceb'u32, 0xbef9a3f7'u32, 0xc67178f2'u32] proc rotr(x: uint32, n: int): uint32 {.inline.} = (x shr n) or (x shl (32 - n)) proc sha256*(data: openArray[byte]): array[32, byte] = var h: array[8, uint32] = [ 0x6a09e667'u32, 0xbb67ae85'u32, 0x3c6ef372'u32, 0xa54ff53a'u32, 0x510e527f'u32, 0x9b05688c'u32, 0x1f83d9ab'u32, 0x5be0cd19'u32] var msg = newSeq[byte](data.len + 1) for i, b in data: msg[i] = b msg[data.len] = 0x80'u8 let bitLen = uint64(data.len) * 8'u64 while msg.len mod 64 != 56: msg.add 0'u8 for shift in countdown(7, 0): msg.add byte((bitLen shr uint64(shift * 8)) and 0xff'u64) var w: array[64, uint32] var offset = 0 while offset < msg.len: for j in 0 ..< 16: let k = offset + j * 4 w[j] = (uint32(msg[k]) shl 24) or (uint32(msg[k + 1]) shl 16) or (uint32(msg[k + 2]) shl 8) or uint32(msg[k + 3]) for j in 16 ..< 64: let s0 = rotr(w[j - 15], 7) xor rotr(w[j - 15], 18) xor (w[j - 15] shr 3) let s1 = rotr(w[j - 2], 17) xor rotr(w[j - 2], 19) xor (w[j - 2] shr 10) w[j] = w[j - 16] + s0 + w[j - 7] + s1 var a = h[0] var b = h[1] var c = h[2] var d = h[3] var e = h[4] var f = h[5] var g = h[6] var hh = h[7] for j in 0 ..< 64: let s1 = rotr(e, 6) xor rotr(e, 11) xor rotr(e, 25) let ch = (e and f) xor ((not e) and g) let t1 = hh + s1 + ch + Sha256K[j] + w[j] let s0 = rotr(a, 2) xor rotr(a, 13) xor rotr(a, 22) let maj = (a and b) xor (a and c) xor (b and c) let t2 = s0 + maj hh = g; g = f; f = e; e = d + t1 d = c; c = b; b = a; a = t1 + t2 h[0] = h[0] + a; h[1] = h[1] + b; h[2] = h[2] + c; h[3] = h[3] + d h[4] = h[4] + e; h[5] = h[5] + f; h[6] = h[6] + g; h[7] = h[7] + hh offset += 64 for j in 0 ..< 8: result[j * 4] = byte(h[j] shr 24) result[j * 4 + 1] = byte((h[j] shr 16) and 0xff) result[j * 4 + 2] = byte((h[j] shr 8) and 0xff) result[j * 4 + 3] = byte(h[j] and 0xff) proc sha256Hex*(data: openArray[byte]): string = toHexLower(sha256(data)) proc sha256Hex*(s: string): string = sha256Hex(bytesOf(s)) proc randomBytes*(n: int): seq[byte] = ## OS CSPRNG (std/sysrand: getrandom(2) on Linux). Raises OSError if unavailable. urandom(n) proc randomHexToken*(bytes = SessionTokenBytes): string = toHexLower(randomBytes(bytes)) proc randomSessionToken*(): string = randomHexToken(SessionTokenBytes) proc formatPlayerKey*(raw: string): string = ## "abcd...20 chars" -> "ABCD-EFGH-..." (5 groups of 4). Input is the 20 raw symbols. doAssert raw.len == KeyLength result = "" for i in 0 ..< KeyGroups: if i > 0: result.add '-' result.add raw[i * KeyGroupSize ..< (i + 1) * KeyGroupSize].toUpperAscii proc generatePlayerKey*(): string = ## 20 symbols from KeyAlphabet, uniform (rejection sampling: 31*8 = 248 < 256). var raw = newStringOfCap(KeyLength) while raw.len < KeyLength: for b in randomBytes(32): if raw.len == KeyLength: break if int(b) >= KeyAlphabet.len * 8: continue raw.add KeyAlphabet[int(b) mod KeyAlphabet.len] formatPlayerKey(raw) proc normalizeKey*(input: string): string = ## Key comparison ignores case, dashes and spaces (DESIGN 6.6). result = newStringOfCap(input.len) for c in input: if c in {'-', ' ', '\t', '\r', '\n'}: continue result.add c.toLowerAscii proc constantTimeEqual*(a, b: string): bool = ## Length-independent comparison for secrets: never exits on the first difference. var diff = a.len xor b.len let n = max(a.len, b.len) for i in 0 ..< n: let ca = if i < a.len: uint8(a[i]) else: 0'u8 let cb = if i < b.len: uint8(b[i]) else: 0'u8 diff = diff or int(ca xor cb) diff == 0