SPB Git

Repository Public

SP800185Tests.swift 12.4 KB 208 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Tests/QyvoraKeccakTests/SP800185Tests.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0413//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 Foundation42import Testing43import QyvoraMath44@testable import QyvoraKeccak4546private func h(_ s: String) -> Bytes { Bytes(hex: s.replacingOccurrences(of: " ", with: ""))! }4748/// Samples of the NIST example files cSHAKE_samples.pdf, KMAC_samples.pdf and KMACXOF_samples.pdf (SP 800-185).49@Suite struct SP800185Tests {50    static let data4: Bytes = [0x00, 0x01, 0x02, 0x03]51    static let data200: Bytes = (0..<200).map { UInt8($0) }52    static let key: Bytes = (0x40...0x5f).map { UInt8($0) }53    static let tagged = Array("My Tagged Application".utf8)54    static let email = Array("Email Signature".utf8)5556    @Test func encodings() {57        // SP 800-185, section 2.3.1: left_encode(0) = 01 00, right_encode(0) = 00 0158        #expect(SP800185.leftEncode(0) == [0x01, 0x00])59        #expect(SP800185.rightEncode(0) == [0x00, 0x01])60        #expect(SP800185.leftEncode(255) == [0x01, 0xff])61        #expect(SP800185.leftEncode(256) == [0x02, 0x01, 0x00])62        #expect(SP800185.rightEncode(256) == [0x01, 0x00, 0x02])63        #expect(SP800185.leftEncode(0x0102030405060708) == [0x08, 1, 2, 3, 4, 5, 6, 7, 8])64        #expect(SP800185.leftEncode(UInt64.max) == [0x08] + Bytes(repeating: 0xff, count: 8))65        #expect(SP800185.encodeString([]) == [0x01, 0x00])66        // encode_string("KMAC") as shown in the KMAC sample files67        #expect(SP800185.encodeString(Array("KMAC".utf8)) == [0x01, 0x20, 0x4b, 0x4d, 0x41, 0x43])68        // "Encoded K" of the sample files starts with 02 01 00 (256 bits)69        #expect(Bytes(SP800185.encodeString(Self.key).prefix(4)) == [0x02, 0x01, 0x00, 0x40])70        let padded = SP800185.bytepad([0xaa, 0xbb], 8)71        #expect(padded == [0x01, 0x08, 0xaa, 0xbb, 0, 0, 0, 0])72        #expect(SP800185.bytepad(Bytes(repeating: 1, count: 6), 8).count == 8)73        #expect(SP800185.bytepad(Bytes(repeating: 1, count: 7), 8).count == 16)74        #expect(SP800185.bytepad([], 168).count == 168)75    }7677    @Test func cshakeSamples() {78        #expect(CSHAKE.cshake128(Self.data4, outputLength: 32, customization: Self.email)79            == h("C1C36925B6409A04F1B504FCBCA9D82B4017277CB5ED2B2065FC1D3814D5AAF5"))80        #expect(CSHAKE.cshake128(Self.data200, outputLength: 32, customization: Self.email)81            == h("C5221D50E4F822D96A2E8881A961420F294B7B24FE3D2094BAED2C6524CC166B"))82        #expect(CSHAKE.cshake256(Self.data4, outputLength: 64, customization: Self.email)83            == h("D008828E2B80AC9D2218FFEE1D070C48B8E4C87BFF32C9699D5B6896EEE0EDD164020E2BE0560858D9C00C037E34A96937C561A74C412BB4C746469527281C8C"))84        #expect(CSHAKE.cshake256(Self.data200, outputLength: 64, customization: Self.email)85            == h("07DC27B11E51FBAC75BC7B3C1D983E8B4B85FB1DEFAF218912AC86430273091727F42B17ED1DF63E8EC118F04B23633C1DFB1574C8FB55CB45DA8E25AFB092BB"))86    }8788    @Test func cshakeWithoutCustomizationIsShake() {89        let m = Self.data20090        #expect(CSHAKE.cshake128(m, outputLength: 77) == SHAKE.shake128(m, outputLength: 77))91        #expect(CSHAKE.cshake256(m, outputLength: 77) == SHAKE.shake256(m, outputLength: 77))92        // a non-empty N or S changes the function93        #expect(CSHAKE.cshake128(m, outputLength: 32, functionName: [0x41]) != SHAKE.shake128(m, outputLength: 32))94        #expect(CSHAKE.cshake128(m, outputLength: 32, functionName: [0x41]) != CSHAKE.cshake128(m, outputLength: 32, customization: [0x41]))95    }9697    @Test func cshakeIncremental() {98        var x = CSHAKE(256, functionName: Array("Fn".utf8), customization: Self.email)99        x.absorb(Array(Self.data200.prefix(13)))100        x.absorb(Array(Self.data200.dropFirst(13)))101        var out = x.squeeze(5)102        out += x.squeeze(200)103        out += x.squeeze(1)104        #expect(out == CSHAKE.cshake256(Self.data200, outputLength: 206, functionName: Array("Fn".utf8), customization: Self.email))105    }106107    @Test func kmacSamples() {108        #expect(KMAC.kmac128(key: Self.key, message: Self.data4, outputLength: 32)109            == h("E5780B0D3EA6F7D3A429C5706AA43A00FADBD7D49628839E3187243F456EE14E"))110        #expect(KMAC.kmac128(key: Self.key, message: Self.data4, outputLength: 32, customization: Self.tagged)111            == h("3B1FBA963CD8B0B59E8C1A6D71888B7143651AF8BA0A7070C0979E2811324AA5"))112        #expect(KMAC.kmac128(key: Self.key, message: Self.data200, outputLength: 32, customization: Self.tagged)113            == h("1F5B4E6CCA02209E0DCB5CA635B89A15E271ECC760071DFD805FAA38F9729230"))114        #expect(KMAC.kmac256(key: Self.key, message: Self.data4, outputLength: 64, customization: Self.tagged)115            == h("20C570C31346F703C9AC36C61C03CB64C3970D0CFC787E9B79599D273A68D2F7F69D4CC3DE9D104A351689F27CF6F5951F0103F33F4F24871024D9C27773A8DD"))116        #expect(KMAC.kmac256(key: Self.key, message: Self.data200, outputLength: 64)117            == h("75358CF39E41494E949707927CEE0AF20A3FF553904C86B08F21CC414BCFD691589D27CF5E15369CBBFF8B9A4C2EB17800855D0235FF635DA82533EC6B759B69"))118        #expect(KMAC.kmac256(key: Self.key, message: Self.data200, outputLength: 64, customization: Self.tagged)119            == h("B58618F71F92E1D56C1B8C55DDD7CD188B97B4CA4D99831EB2699A837DA2E4D970FBACFDE50033AEA585F1A2708510C32D07880801BD182898FE476876FC8965"))120    }121122    @Test func kmacXofSamples() {123        #expect(KMAC.kmacxof128(key: Self.key, message: Self.data4, outputLength: 32)124            == h("CD83740BBD92CCC8CF032B1481A0F4460E7CA9DD12B08A0C4031178BACD6EC35"))125        #expect(KMAC.kmacxof128(key: Self.key, message: Self.data4, outputLength: 32, customization: Self.tagged)126            == h("31A44527B4ED9F5C6101D11DE6D26F0620AA5C341DEF41299657FE9DF1A3B16C"))127        #expect(KMAC.kmacxof128(key: Self.key, message: Self.data200, outputLength: 32, customization: Self.tagged)128            == h("47026C7CD793084AA0283C253EF658490C0DB61438B8326FE9BDDF281B83AE0F"))129        #expect(KMAC.kmacxof256(key: Self.key, message: Self.data4, outputLength: 64, customization: Self.tagged)130            == h("1755133F1534752AAD0748F2C706FB5C784512CAB835CD15676B16C0C6647FA96FAA7AF634A0BF8FF6DF39374FA00FAD9A39E322A7C92065A64EB1FB0801EB2B"))131        #expect(KMAC.kmacxof256(key: Self.key, message: Self.data200, outputLength: 64)132            == h("FF7B171F1E8A2B24683EED37830EE797538BA8DC563F6DA1E667391A75EDC02CA633079F81CE12A25F45615EC89972031D18337331D24CEB8F8CA8E6A19FD98B"))133        #expect(KMAC.kmacxof256(key: Self.key, message: Self.data200, outputLength: 64, customization: Self.tagged)134            == h("D5BE731C954ED7732846BB59DBE3A8E30F83E77A4BFF4459F2F1C2B4ECEBB8CE67BA01C62E8AB8578D2D499BD1BB276768781190020A306A97DE281DCC30305D"))135    }136137    @Test func kmacIncrementalAndXofStreaming() {138        var m = KMAC(128, key: Self.key, customization: Self.tagged)139        m.update(Array(Self.data200.prefix(99)))140        m.update([])141        m.update(Array(Self.data200.dropFirst(99)))142        #expect(m.finalize(outputLength: 32) == KMAC.kmac128(key: Self.key, message: Self.data200, outputLength: 32, customization: Self.tagged))143144        var x = KMAC(256, key: Self.key)145        x.update(Self.data200)146        x.finalizeXOF()147        let streamed = x.squeeze(1) + x.squeeze(135) + x.squeeze(137) + x.squeeze(27)148        #expect(streamed == KMAC.kmacxof256(key: Self.key, message: Self.data200, outputLength: 300))149        // the XOF output is prefix-consistent, the fixed-length output is not150        #expect(Bytes(streamed.prefix(64)) == KMAC.kmacxof256(key: Self.key, message: Self.data200, outputLength: 64))151        let t32 = KMAC.kmac256(key: Self.key, message: Self.data200, outputLength: 32)152        let t64 = KMAC.kmac256(key: Self.key, message: Self.data200, outputLength: 64)153        #expect(t32 != Bytes(t64.prefix(32)))154    }155156    @Test func kmacVerifyRejectsTampering() {157        let tag = KMAC.kmac256(key: Self.key, message: Self.data200, outputLength: 32, customization: Self.tagged)158        #expect(KMAC.verify(256, key: Self.key, message: Self.data200, tag: tag, customization: Self.tagged))159        for i in 0..<tag.count {160            var bad = tag161            bad[i] ^= 0x01162            #expect(!KMAC.verify(256, key: Self.key, message: Self.data200, tag: bad, customization: Self.tagged))163        }164        var otherKey = Self.key165        otherKey[0] ^= 0x80166        #expect(!KMAC.verify(256, key: otherKey, message: Self.data200, tag: tag, customization: Self.tagged))167        #expect(!KMAC.verify(256, key: Self.key, message: Array(Self.data200.dropLast()), tag: tag, customization: Self.tagged))168        #expect(!KMAC.verify(256, key: Self.key, message: Self.data200, tag: tag))169        #expect(!KMAC.verify(256, key: Self.key, message: Self.data200, tag: Array(tag.dropLast()), customization: Self.tagged))170        #expect(!KMAC.verify(128, key: Self.key, message: Self.data200, tag: tag, customization: Self.tagged))171    }172173    /// Regression test of QYV-CRY-001: the tag length is chosen by the sender. An empty tag used to verify174    /// against every message and key (KMAC with L = 0 is the empty string), a one-byte tag in 256 tries.175    @Test func kmacVerifyRefusesShortTags() {176        #expect(!KMAC.verify(256, key: Self.key, message: Self.data200, tag: []))177        #expect(!KMAC.verify(128, key: Self.key, message: Self.data4, tag: [], customization: Self.tagged))178        for length in 1..<KMAC.minimumTagSize {179            // the genuine tag of that length is refused too: the limit is on the length, not on the value180            let genuine = KMAC.kmac256(key: Self.key, message: Self.data200, outputLength: length)181            #expect(!KMAC.verify(256, key: Self.key, message: Self.data200, tag: genuine))182        }183        let shortest = KMAC.kmac256(key: Self.key, message: Self.data200, outputLength: KMAC.minimumTagSize)184        #expect(KMAC.verify(256, key: Self.key, message: Self.data200, tag: shortest))185    }186187    /// Regression test of QYV-CRY-002: the one-shot functions erase the sponge, whose state could otherwise be188    /// run backwards (Keccak-f is a permutation) to the last absorbed block, a seed or a key.189    @Test func squeezeAndWipeErasesTheState() {190        var sponge = KeccakSponge(rate: 136, suffix: 0x06)191        sponge.absorb(Self.key)192        var reference = KeccakSponge(rate: 136, suffix: 0x06)193        reference.absorb(Self.key)194        #expect(sponge.squeezeAndWipe(32) == reference.squeeze(32))195        #expect(reference.state.contains { $0 != 0 })196        #expect(sponge.state.allSatisfy { $0 == 0 })197        #expect(sponge.position == 0)198        // the public one-shots still give the standard values199        #expect(SHA3.sha3_256([]).hex == "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a")200        #expect(SHAKE.shake128([], outputLength: 16).hex == "7f9c2ba4e88f827d616045507605853e")201        #expect(SHA3.keccak256([]).hex == "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")202        var incremental = SHA3_256()203        incremental.update(Self.data200)204        #expect(incremental.finalize() == SHA3.sha3_256(Self.data200))205        #expect(incremental.sponge.state.allSatisfy { $0 == 0 })206    }207}208
Files main
Menu

Theme