SPB Git

Repository Public

SHA2BasicTests.swift 7.3 KB 135 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Tests/QyvoraSymmetricTests/SHA2BasicTests.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0513//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import CryptoKit42import Foundation43import Testing44import QyvoraMath45@testable import QyvoraSymmetric4647@Suite struct SHA2BasicTests {48    @Test func knownAnswers() {49        #expect(QyvoraSymmetric.SHA256.hash(Array("abc".utf8)).hex == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")50        #expect(QyvoraSymmetric.SHA512.hash(Array("abc".utf8)).hex == "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f")51    }5253    @Test func differentialAgainstCryptoKit() {54        var rng = SystemRandomNumberGenerator()55        for n in [0, 1, 55, 56, 57, 63, 64, 65, 111, 112, 113, 127, 128, 129, 1000, 5000] {56            let m = (0..<n).map { _ in UInt8.random(in: 0...255, using: &rng) }57            #expect(QyvoraSymmetric.SHA256.hash(m) == Array(CryptoKit.SHA256.hash(data: Data(m))))58            #expect(QyvoraSymmetric.SHA512.hash(m) == Array(CryptoKit.SHA512.hash(data: Data(m))))59            var h = QyvoraSymmetric.SHA512()60            h.update(Array(m.prefix(n / 3))); h.update(Array(m.dropFirst(n / 3)))61            #expect(h.finalize() == Array(CryptoKit.SHA512.hash(data: Data(m))))62            let key = (0..<(n % 200)).map { _ in UInt8.random(in: 0...255, using: &rng) }63            let ref = CryptoKit.HMAC<CryptoKit.SHA256>.authenticationCode(for: Data(m), using: SymmetricKey(data: Data(key)))64            #expect(QyvoraSymmetric.HMAC<QyvoraSymmetric.SHA256>.authenticate(key: key, message: m) == Array(ref))65            let ref5 = CryptoKit.HMAC<CryptoKit.SHA512>.authenticationCode(for: Data(m), using: SymmetricKey(data: Data(key)))66            #expect(QyvoraSymmetric.HMAC<QyvoraSymmetric.SHA512>.authenticate(key: key, message: m) == Array(ref5))67        }68    }6970    /// Regression test of QYV-CRY-002: after `finalize` the pending buffer (the end of the message, key71    /// material inside HMAC or RFC 6979) holds nothing but zeros, and a buffer that was compressed during72    /// `update` is erased before it is reused. Every split of the input still gives the CryptoKit digest.73    /// Regression test of QYV-CRY-005: after `finalize` the chaining state, which equals the digest, is erased74    /// (SHA-512(seed) is the expanded Ed25519 key, an HMAC-SHA512 output is a SLIP-0010 or BIP-32 child key), and75    /// the digests are unchanged, including through HMAC, the truncated variants and a state copied before `finalize`.76    @Test func chainingStateIsErasedAfterFinalize() {77        let m = (0..<300).map { UInt8(truncatingIfNeeded: $0 &* 11 &+ 3) }78        for length in [0, 1, 55, 56, 63, 64, 111, 112, 127, 128, 129, 300] {79            let data = Array(m.prefix(length))80            var a = QyvoraSymmetric.SHA256()81            a.update(data)82            #expect(a.h.contains { $0 != 0 })83            #expect(a.finalize() == Array(CryptoKit.SHA256.hash(data: Data(data))))84            #expect(a.h.count == 8 && a.h.allSatisfy { $0 == 0 })85            var b = QyvoraSymmetric.SHA512()86            b.update(data)87            #expect(b.h.contains { $0 != 0 })88            #expect(b.finalize() == Array(CryptoKit.SHA512.hash(data: Data(data))))89            #expect(b.h.count == 8 && b.h.allSatisfy { $0 == 0 })90            #expect(QyvoraSymmetric.SHA384.hash(data) == Array(CryptoKit.SHA384.hash(data: Data(data))))91        }92        // a state copied before finalize (PBKDF2 and HMAC keep such copies) is not affected by the erasure of the copy93        var base = QyvoraSymmetric.SHA512()94        base.update(Array(m.prefix(200)))95        var first = base, second = base96        let d1 = first.finalize()97        #expect(first.h.allSatisfy { $0 == 0 } && base.h.contains { $0 != 0 })98        #expect(second.finalize() == d1 && d1 == Array(CryptoKit.SHA512.hash(data: Data(m.prefix(200)))))99        // HMAC over both hashes still matches CryptoKit100        let key = Array(m.prefix(40)), message = Array(m.suffix(100))101        let k = SymmetricKey(data: Data(key))102        #expect(QyvoraSymmetric.HMAC<QyvoraSymmetric.SHA512>.authenticate(key: key, message: message) == Array(CryptoKit.HMAC<CryptoKit.SHA512>.authenticationCode(for: Data(message), using: k)))103        #expect(QyvoraSymmetric.HMAC<QyvoraSymmetric.SHA256>.authenticate(key: key, message: message) == Array(CryptoKit.HMAC<CryptoKit.SHA256>.authenticationCode(for: Data(message), using: k)))104    }105106    @Test func pendingBufferIsErased() {107        let m = (0..<300).map { UInt8(truncatingIfNeeded: $0 &* 7 &+ 1) }108        let ref256 = Array(CryptoKit.SHA256.hash(data: Data(m)))109        let ref512 = Array(CryptoKit.SHA512.hash(data: Data(m)))110        for split in [0, 1, 17, 63, 64, 65, 100, 127, 128, 129, 200, 299, 300] {111            var a = QyvoraSymmetric.SHA256()112            a.update(Array(m.prefix(split)))113            a.update(Array(m.dropFirst(split)))114            #expect(a.finalize() == ref256)115            #expect(a.buffer.allSatisfy { $0 == 0 })116            var b = QyvoraSymmetric.SHA512()117            b.update(Array(m.prefix(split)))118            b.update(Array(m.dropFirst(split)))119            #expect(b.finalize() == ref512)120            #expect(b.buffer.allSatisfy { $0 == 0 })121        }122        // two updates that fill the buffer exactly leave it empty123        var c = QyvoraSymmetric.SHA256()124        c.update(Array(m[0..<10]))125        c.update(Array(m[10..<64]))126        #expect(c.buffer.isEmpty)127        c.update(Array(m[64...]))128        #expect(c.finalize() == ref256)129        // HMAC and the derivations built on it are unchanged (RFC 4231, test case 2)130        let tag = QyvoraSymmetric.HMAC<QyvoraSymmetric.SHA256>.authenticate(131            key: Array("Jefe".utf8), message: Array("what do ya want for nothing?".utf8))132        #expect(tag.hex == "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843")133    }134}135
Files main
Menu

Theme