From 6431d11a2c7752e021d6b7ee1401a65a65503a78 Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Wed, 21 Oct 2026 14:16:02 +0800 Subject: [PATCH] test(zuc): add EEA and EIA semantics vectors Official GB/T 33133 EEA and EIA test sets, eleven constructed MACs at the 1/7/8/31/32/33/63/64/65/127/128-bit boundaries, and the ZUC-256 sets for 4-, 8- and 16-byte tags. EEA cases run through both the frame API and the raw key/IV API with the frame IV derived in the generator; that pins the two distinct direction-bit layouts, for which official test set 2 exercises direction = 1 for EEA and EIA alike. EIA cases also cross a split write, EEA cases require XORKeyStreamAt to match a sequential pass, and invalid key, IV and tag sizes must be rejected without panicking. --- .../vectors/testdata/zuc_semantics.json | 449 ++++++++++++++++++ internal/cryptotest/zucvectors/generate.go | 319 +++++++++++++ .../cryptotest/zucvectors/generate_test.go | 98 ++++ .../cryptotest/zucvectors/official_vectors.go | 111 +++++ zuc/semantics_vectors_test.go | 255 ++++++++++ 5 files changed, 1232 insertions(+) create mode 100644 internal/cryptotest/vectors/testdata/zuc_semantics.json create mode 100644 internal/cryptotest/zucvectors/generate.go create mode 100644 internal/cryptotest/zucvectors/generate_test.go create mode 100644 internal/cryptotest/zucvectors/official_vectors.go create mode 100644 zuc/semantics_vectors_test.go diff --git a/internal/cryptotest/vectors/testdata/zuc_semantics.json b/internal/cryptotest/vectors/testdata/zuc_semantics.json new file mode 100644 index 00000000..58a604aa --- /dev/null +++ b/internal/cryptotest/vectors/testdata/zuc_semantics.json @@ -0,0 +1,449 @@ +{ + "schema": "gmsm-semantics-vectors/v1", + "algorithm": "zuc", + "generator": "go test ./internal/cryptotest/zucvectors -run TestVectorsUpToDate -update", + "testGroups": [ + { + "type": "ZUCEEA", + "comment": "ZUC-128 EEA keystream from the GB/T 33133 test sets. Each case carries the frame parameters and the frame IV derived from them, so the frame API and the raw key/IV API must agree with the published keystream.", + "tests": [ + { + "tcId": 1, + "comment": "GB/T 33133 EEA test set 1", + "expected": "a6c85fc66afb8533aafc2518dfe784940ee1e4b030238cc8", + "result": "valid", + "key": "173d14ba5003731d7a60049470f00a29", + "nonce": "66035492780000006603549278000000", + "input": "6cf65340735552ab0c9752fa6f9025fe0bd675d9005875b2", + "count": 1711494290, + "bearer": 15 + }, + { + "tcId": 2, + "comment": "GB/T 33133 EEA test set 2", + "expected": "131d43e0dea1be5c5a1bfd971d852cbf712d7b4f57961fea3208afa8bca433f456ad09c7417e58bc69cf8866d1353f74865e80781d202dfb3ecff7fcbc3b190fe82a204ed0e350fc0f6f2613b2f2bca6df5a473a57a4a00d985ebad880d6f23864a07b01", + "result": "valid", + "key": "e5bd3ea0eb55ade866c6ac58bd54302a", + "nonce": "00056823c400000000056823c4000000", + "input": "14a8ef693d678507bbe7270a7f67ff5006c3525b9807e467c4e56000ba338f5d429559036751822246c80d3b38f07f4be2d8ff5805f5132229bde93bbbdcaf382bf1ee972fbf9977bada8945847a2a6c9ad34a667554e04d1f7fa2c33241bd8f01ba220d", + "count": 354339, + "bearer": 24, + "direction": 1 + }, + { + "tcId": 3, + "comment": "GB/T 33133 EEA test set 3", + "expected": "94eaa4aa30a57137ddf09b97b25618a20a13e2f10fa5bf8161a879cc2ae797a6b4cf2d9df31debb9905ccfec97de605d21c61ab8531b7f3c9da5f03931f8a0642de48211f5f52ffea10f392a047669985da454a28f080961a6c2b62daa17f33cd60a4971f48d2d909394a55f48117ace43d708e6b77d3dc46d8bc017d4d1abb77b7428c042b06f2f99d8d07c9879d99600127a31985f1099bbd7d6c1519ede8f5eeb4a610b349ac01ea2350691756bd105c974a53eddb35d1d4100b012e522ab41f4c5f2fde76b59cb8b96d885cfe4080d1328a0d636cc0edc05800b76acca8fef672084d1f52a8bbd8e0993320992c7ffbae17c408441e0ee883fc8a8b05e22f5ff7f8d1b48c74c468c467a028f09fd7ce91109a570a2d5c4d5f4fa18c5dd3e4562afe24ef771901f59af645898acef088abae07e92d52eb2de55045bb1b7c4164ef2d7a6cac15eeb926d7ea2f08b66e1f759f3aee44614725aa3c7482b30844c143ff87b53f1e583c501257dddd096b81268daa303f17234c2333541f0bb8e190648c5807c866d7193228609adb948686f7de294a802cc38f7fe5208f5ea3196d0167b9bdd02f0d2a5221ca508f893af5c4b4bb9f4f520fd84289b3dbe7e61497a7e2a584037ea637b6981127174af57b471df4b2768fd79c1540fb3edf2ea22cb69bec0cf8d933d9c6fdd645e850591cca3d62c0c", + "result": "valid", + "key": "e13fed21b46e4e7ec31253b2bb17b3e0", + "nonce": "2738cdaad00000002738cdaad0000000", + "input": "8d74e20d54894e06d3cb13cb3933065e8674be62adb1c72b3a646965ab63cb7b7854dfdc27e84929f49c64b872a490b13f957b64827e71f41fbd4269a42c97f824537027f86e9f4ad82d1df451690fdd98b6d03f3a0ebe3a312d6b840ba5a1820b2a2c9709c090d245ed267cf845ae41fa975d3333ac3009fd40eba9eb5b885714b768b697138baf21380eca49f644d48689e4215760b906739f0d2b3f091133ca15d981cbe401baf72d05ace05cccb2d297f4ef6a5f58d91246cfa77215b892ab441d5278452795ccb7f5d79057a1c4f77f80d46db2033cb79bedf8e60551ce10c667f62a97abafabbcd6772018df96a282ea737ce2cb331211f60d5354ce78f9918d9c206ca042c9b62387dd709604a50af16d8d35a8906be484cf2e74a9289940364353249b27b4c9ae29eddfc7da6418791a4e7baa0660fa64511f2d685cc3a5ff70e0d2b74292e3b8a0cd6b04b1c790b8ead2703708540dea2fc09c3da770f65449c84d817a4f551055e19ab85018a0028b71a144d96791e9a3577933504eee0060340c69d274e1bf9d805dcbcc1a6faa976800b6ff2b671dc463652fa8a33ee50974c1c21be01eabb2167430269d72ee511c9dde30797c9a25d86ce74f5b961be5fdfb6807814039e7137636bd1d7fa9e09efd2007505906a5ac45dfdeed7757bbee745749c29633350bee0ea6f409df458016", + "count": 658034090, + "bearer": 26 + } + ] + }, + { + "type": "ZUCEEAInit", + "comment": "Stream cipher key and IV length checks. A 32-byte key with a 23-byte IV selects ZUC-256; every mismatched combination must be rejected.", + "tests": [ + { + "tcId": 1, + "comment": "15-byte key", + "result": "invalid", + "key": "111111111111111111111111111111", + "nonce": "22222222222222222222222222222222" + }, + { + "tcId": 2, + "comment": "17-byte key", + "result": "invalid", + "key": "1111111111111111111111111111111111", + "nonce": "22222222222222222222222222222222" + }, + { + "tcId": 3, + "comment": "15-byte iv", + "result": "invalid", + "key": "11111111111111111111111111111111", + "nonce": "222222222222222222222222222222" + }, + { + "tcId": 4, + "comment": "17-byte iv", + "result": "invalid", + "key": "11111111111111111111111111111111", + "nonce": "2222222222222222222222222222222222" + }, + { + "tcId": 5, + "comment": "32-byte key with a 16-byte iv", + "result": "invalid", + "key": "3333333333333333333333333333333333333333333333333333333333333333", + "nonce": "22222222222222222222222222222222" + }, + { + "tcId": 6, + "comment": "16-byte key with a 23-byte iv", + "result": "invalid", + "key": "11111111111111111111111111111111", + "nonce": "4444444444444444444444444444444444444444444444" + }, + { + "tcId": 7, + "comment": "31-byte key with a 23-byte iv", + "result": "invalid", + "key": "33333333333333333333333333333333333333333333333333333333333333", + "nonce": "4444444444444444444444444444444444444444444444" + }, + { + "tcId": 8, + "comment": "32-byte key with a 23-byte iv selects ZUC-256", + "result": "valid", + "key": "3333333333333333333333333333333333333333333333333333333333333333", + "nonce": "4444444444444444444444444444444444444444444444" + } + ] + }, + { + "type": "ZUCEIA", + "comment": "ZUC-128 EIA MAC. The official 1-, 577- and 5670-bit vectors anchor the bit-oriented path; the constructed cases pin the word and block boundaries between them.", + "tests": [ + { + "tcId": 1, + "comment": "GB/T 33133 EIA test set 1, 1-bit message", + "expected": "c8a9595e", + "result": "valid", + "key": "00000000000000000000000000000000", + "nonce": "00000000000000000000000000000000", + "input": "00000000", + "bitLen": 1 + }, + { + "tcId": 2, + "comment": "GB/T 33133 EIA test set 2, 577-bit message", + "expected": "fae8ff0b", + "result": "valid", + "key": "c9e6cec4607c72db000aefa88385ab0a", + "nonce": "a94059da50000000294059da50008000", + "input": "983b41d47d780c9e1ad11d7eb70391b1de0b35da2dc62f83e7b78d6306ca0ea07e941b7be91348f9fcb170e2217fecd97f9f68adb16e5d7d21e569d280ed775cebde3f4093c5388100000000", + "bitLen": 577, + "count": 2839566810, + "bearer": 10, + "direction": 1 + }, + { + "tcId": 3, + "comment": "GB/T 33133 EIA test set 3, 5670-bit message", + "expected": "0ca12792", + "result": "valid", + "key": "6b8b08ee79e0b5982d6d128ea9f220cb", + "nonce": "561eb2dde0000000561eb2dde0000000", + "input": "5bad724710ba1c56d5a315f8d40f6e093780be8e8de07b6992432018e08ed96a5734af8bad8a575d3a1f162f85045cc770925571d9f5b94e454a77c16e72936bf016ae157499f0543b5d52caa6dbeab697d2bb73e41b8075dce79b4b86044f661d4485a543dd78606e0419e8059859d3cb2b67ce0977603f81ff839e331859544cfbc8d00fef1a4c8510fb547d6b06c611ef44f1bce107cfa45a06aab360152b28dc1ebe6f7fe09b0516f9a5b02a1bd84bb0181e2e89e19bd8125930d178682f3862dc51b636f04e720c47c3ce51ad70d94b9b2255fbae906549f499f8c6d39947ed5e5df8e2def113253e7b08d0a76b6bfc68c812f375c79b8fe5fd85976aa6d46b4a2339d8ae5147f680fbe70f978b38effd7b2f7866a22554e193a94e98a68b74bd25bb2b3f5fb0a5fd59887f9ab68159b7178d5b7b677cb546bf41eadca216fc10850128f8bdef5c8d89f96afa4fa8b54885565ed838a950fee5f1c3b0a4f6fb71e54dfd169e82cecc7266c850e67c5ef0ba960f5214060e71eb172a75fc1486835cbea6534465b055c96a72e4105224182325d830414b40214daa8091d2e0fb010ae15c6de90850973bdf1e423be148a237b87a0c9f34d4b47605b803d743a86a90399a4af396d3a1200a62f3d9507962e8e5bee6d3da2bb3f7237664ac7a292823900bc63503b29e80d63f6067bf8e1716ac25beba350deb62a99fe03185eb4f69937ecd387941fda544ba67db0911774938b01827bcc69c92b3f772a9d2859ef003398b1f6bbad7b574f7989a1d10b2df798e0dbf30d6587464d24878cd00c0eaee8a1a0cc753a27979e11b41db1de3d5038afaf49f5c682c3748d8a3a9ec54e6a371275f1683510f8e4f90938f9ab6e134c2cfdf4841cba88e0cff2b0bcc8e6adcb71109b5198fecf1bb7e5c531aca50a56a8a3b6de59862d41fa113d9cd957808f08571d9a4bb792af271f6cc6dbb8dc7ec36e36be1ed308164c31c7c0afc541c000000", + "bitLen": 5670, + "count": 1444852445, + "bearer": 28 + }, + { + "tcId": 4, + "comment": "constructed: 1 bits of a 32-byte message", + "expected": "bebdc477", + "result": "valid", + "key": "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a", + "nonce": "00000000000000000000000000000000", + "input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "bitLen": 1 + }, + { + "tcId": 5, + "comment": "constructed: 7 bits of a 32-byte message", + "expected": "a725890b", + "result": "valid", + "key": "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a", + "nonce": "00000000000000000000000000000000", + "input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "bitLen": 7 + }, + { + "tcId": 6, + "comment": "constructed: 8 bits of a 32-byte message", + "expected": "a9b714f9", + "result": "valid", + "key": "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a", + "nonce": "00000000000000000000000000000000", + "input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "bitLen": 8 + }, + { + "tcId": 7, + "comment": "constructed: 31 bits of a 32-byte message", + "expected": "100f8983", + "result": "valid", + "key": "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a", + "nonce": "00000000000000000000000000000000", + "input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "bitLen": 31 + }, + { + "tcId": 8, + "comment": "constructed: 32 bits of a 32-byte message", + "expected": "b350646f", + "result": "valid", + "key": "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a", + "nonce": "00000000000000000000000000000000", + "input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "bitLen": 32 + }, + { + "tcId": 9, + "comment": "constructed: 33 bits of a 32-byte message", + "expected": "e597c564", + "result": "valid", + "key": "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a", + "nonce": "00000000000000000000000000000000", + "input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "bitLen": 33 + }, + { + "tcId": 10, + "comment": "constructed: 63 bits of a 32-byte message", + "expected": "b7577847", + "result": "valid", + "key": "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a", + "nonce": "00000000000000000000000000000000", + "input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "bitLen": 63 + }, + { + "tcId": 11, + "comment": "constructed: 64 bits of a 32-byte message", + "expected": "ea037a1d", + "result": "valid", + "key": "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a", + "nonce": "00000000000000000000000000000000", + "input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "bitLen": 64 + }, + { + "tcId": 12, + "comment": "constructed: 65 bits of a 32-byte message", + "expected": "af290d0b", + "result": "valid", + "key": "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a", + "nonce": "00000000000000000000000000000000", + "input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "bitLen": 65 + }, + { + "tcId": 13, + "comment": "constructed: 127 bits of a 32-byte message", + "expected": "e8410d0e", + "result": "valid", + "key": "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a", + "nonce": "00000000000000000000000000000000", + "input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "bitLen": 127 + }, + { + "tcId": 14, + "comment": "constructed: 128 bits of a 32-byte message", + "expected": "a4e4e992", + "result": "valid", + "key": "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a", + "nonce": "00000000000000000000000000000000", + "input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "bitLen": 128 + } + ] + }, + { + "type": "ZUC256EIA", + "comment": "ZUC-256 EIA MAC from the GB/T 33133 test sets, once per supported tag size (4, 8 and 16 bytes), plus the rejected tag, key and IV sizes.", + "tests": [ + { + "tcId": 1, + "comment": "GB/T 33133 ZUC-256 test set 1: zero key and iv, 4-byte tag", + "expected": "9b972a74", + "result": "valid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0000000000000000000000000000000000000000000000", + "input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "tagSize": 4 + }, + { + "tcId": 2, + "comment": "GB/T 33133 ZUC-256 test set 1: zero key and iv, 8-byte tag", + "expected": "673e54990034d38c", + "result": "valid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0000000000000000000000000000000000000000000000", + "input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "tagSize": 8 + }, + { + "tcId": 3, + "comment": "GB/T 33133 ZUC-256 test set 1: zero key and iv, 16-byte tag", + "expected": "d85e54bbcb9600967084c952a1654b26", + "result": "valid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0000000000000000000000000000000000000000000000", + "input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "tagSize": 16 + }, + { + "tcId": 4, + "comment": "GB/T 33133 ZUC-256 test set 2: zero key and iv, ten 0x11 messages, 4-byte tag", + "expected": "8754f5cf", + "result": "valid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0000000000000000000000000000000000000000000000", + "input": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", + "tagSize": 4 + }, + { + "tcId": 5, + "comment": "GB/T 33133 ZUC-256 test set 2: zero key and iv, ten 0x11 messages, 8-byte tag", + "expected": "130dc225e72240cc", + "result": "valid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0000000000000000000000000000000000000000000000", + "input": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", + "tagSize": 8 + }, + { + "tcId": 6, + "comment": "GB/T 33133 ZUC-256 test set 2: zero key and iv, ten 0x11 messages, 16-byte tag", + "expected": "df1e8307b31cc62beca1ac6f8190c22f", + "result": "valid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0000000000000000000000000000000000000000000000", + "input": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", + "tagSize": 16 + }, + { + "tcId": 7, + "comment": "GB/T 33133 ZUC-256 test set 3: all-ones key and iv, 4-byte tag", + "expected": "1f3079b4", + "result": "valid", + "key": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "nonce": "ffffffffffffffffffffffffffffffffffffffffffffff", + "input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "tagSize": 4 + }, + { + "tcId": 8, + "comment": "GB/T 33133 ZUC-256 test set 3: all-ones key and iv, 8-byte tag", + "expected": "8c71394d39957725", + "result": "valid", + "key": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "nonce": "ffffffffffffffffffffffffffffffffffffffffffffff", + "input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "tagSize": 8 + }, + { + "tcId": 9, + "comment": "GB/T 33133 ZUC-256 test set 3: all-ones key and iv, 16-byte tag", + "expected": "a35bb274b567c48b28319f111af34fbd", + "result": "valid", + "key": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "nonce": "ffffffffffffffffffffffffffffffffffffffffffffff", + "input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "tagSize": 16 + }, + { + "tcId": 10, + "comment": "GB/T 33133 ZUC-256 test set 4: all-ones key and iv, ten 0x11 messages, 4-byte tag", + "expected": "5c7c8b88", + "result": "valid", + "key": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "nonce": "ffffffffffffffffffffffffffffffffffffffffffffff", + "input": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", + "tagSize": 4 + }, + { + "tcId": 11, + "comment": "GB/T 33133 ZUC-256 test set 4: all-ones key and iv, ten 0x11 messages, 8-byte tag", + "expected": "ea1dee544bb6223b", + "result": "valid", + "key": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "nonce": "ffffffffffffffffffffffffffffffffffffffffffffff", + "input": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", + "tagSize": 8 + }, + { + "tcId": 12, + "comment": "GB/T 33133 ZUC-256 test set 4: all-ones key and iv, ten 0x11 messages, 16-byte tag", + "expected": "3a83b554be408ca5494124ed9d473205", + "result": "valid", + "key": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "nonce": "ffffffffffffffffffffffffffffffffffffffffffffff", + "input": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", + "tagSize": 16 + }, + { + "tcId": 13, + "comment": "1-byte tag", + "result": "invalid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0000000000000000000000000000000000000000000000", + "input": "00000000000000000000000000000000", + "tagSize": 1 + }, + { + "tcId": 14, + "comment": "15-byte tag", + "result": "invalid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0000000000000000000000000000000000000000000000", + "input": "00000000000000000000000000000000", + "tagSize": 15 + }, + { + "tcId": 15, + "comment": "17-byte tag", + "result": "invalid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0000000000000000000000000000000000000000000000", + "input": "00000000000000000000000000000000", + "tagSize": 17 + }, + { + "tcId": 16, + "comment": "32-byte tag", + "result": "invalid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0000000000000000000000000000000000000000000000", + "input": "00000000000000000000000000000000", + "tagSize": 32 + }, + { + "tcId": 17, + "comment": "31-byte key", + "result": "invalid", + "key": "00000000000000000000000000000000000000000000000000000000000000", + "nonce": "0000000000000000000000000000000000000000000000", + "input": "00000000000000000000000000000000", + "tagSize": 4 + }, + { + "tcId": 18, + "comment": "22-byte iv", + "result": "invalid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "00000000000000000000000000000000000000000000", + "input": "00000000000000000000000000000000", + "tagSize": 4 + }, + { + "tcId": 19, + "comment": "24-byte iv", + "result": "invalid", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "000000000000000000000000000000000000000000000000", + "input": "00000000000000000000000000000000", + "tagSize": 4 + } + ] + } + ] +} diff --git a/internal/cryptotest/zucvectors/generate.go b/internal/cryptotest/zucvectors/generate.go new file mode 100644 index 00000000..10dda68a --- /dev/null +++ b/internal/cryptotest/zucvectors/generate.go @@ -0,0 +1,319 @@ +// Copyright 2026 Sun Yimin. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +// Package zucvectors builds the ZUC EEA and EIA vector suite. +// +// The keystream and MAC cases are the official GB/T 33133 vectors already +// committed in zuc/eea_test.go and zuc/eia_test.go. The frame IV that the +// EEA and EIA entry points derive from (count, bearer, direction) is computed +// here from the published bit layout, so the suite checks the library against +// the standard twice: once through the frame API, which derives the IV +// itself, and once through the raw key/IV API fed with the IV derived here. +// +// The constructed EIA cases at 1, 7, 8, 31, 32, 33, 63, 64, 65, 127 and 128 +// bits are regression pins: the official 577- and 5670-bit vectors anchor +// the bit-oriented path, and these cases pin the block and word boundaries +// between them. Their expected values were recorded from the implementation +// before it was frozen here. +package zucvectors + +import ( + "encoding/binary" + "encoding/hex" + "encoding/json" + "fmt" + + "github.com/emmansun/gmsm/internal/cryptotest/vectors" +) + +const fileName = "zuc_semantics.json" + +func mustHex(s string) []byte { + b, err := hex.DecodeString(s) + if err != nil { + panic("zucvectors: invalid hex constant: " + err.Error()) + } + return b +} + +func hexStr(b []byte) string { return hex.EncodeToString(b) } + +func repeat(b byte, n int) []byte { + out := make([]byte, n) + for i := range out { + out[i] = b + } + return out +} + +func seq(n int) []byte { + out := make([]byte, n) + for i := range out { + out[i] = byte(i) + } + return out +} + +// eeaFrameIV derives the 16-byte ZUC-128 EEA frame IV from the 32-bit frame +// counter, the 5-bit bearer identity and the 1-bit direction flag. The bearer +// and direction bits share the octet that follows the counter, and the counter +// followed by that octet is repeated to fill the 128-bit IV. +func eeaFrameIV(count, bearer, direction uint32) []byte { + iv := make([]byte, 16) + binary.BigEndian.PutUint32(iv, count) + copy(iv[8:], iv[:4]) + iv[4] = byte(((bearer << 1) | (direction & 1)) << 2) + iv[12] = iv[4] + return iv +} + +// eiaFrameIV derives the 16-byte ZUC-128 EIA frame IV. The layout differs from +// the EEA one: the direction bit stays out of the bearer octet and appears at +// the top of the second counter copy and in the fourteenth octet. Both layouts +// are anchored by the official test sets, which cover direction = 1 for EEA +// (test set 2) and EIA (test set 2). +func eiaFrameIV(count, bearer, direction uint32) []byte { + iv := make([]byte, 16) + binary.BigEndian.PutUint32(iv, count) + copy(iv[9:12], iv[1:4]) + iv[4] = byte(bearer << 3) + iv[12] = iv[4] + iv[8] = iv[0] ^ byte(direction<<7) + iv[14] = byte(direction << 7) + return iv +} + +// buildEEA pins the EEA keystream. Each case carries the frame parameters and +// the IV derived from them, so the consumer can run both entry points. +func buildEEA() []vectors.Case { + out := make([]vectors.Case, 0, len(officialEEA)) + for i, v := range officialEEA { + out = append(out, vectors.Case{ + TCID: i + 1, + Comment: fmt.Sprintf("GB/T 33133 EEA test set %d", i+1), + Result: "valid", + Key: v.key, + Nonce: hexStr(eeaFrameIV(v.count, v.bearer, v.direction)), + Count: v.count, + Bearer: v.bearer, + Direction: v.direction, + Input: v.in, + Expected: v.out, + }) + } + return out +} + +// buildEEAInit pins the key and IV length checks of the stream cipher +// constructor. The 32/23 combination selects ZUC-256. +func buildEEAInit() []vectors.Case { + key16 := hexStr(repeat(0x11, 16)) + key31 := hexStr(repeat(0x33, 31)) + key32 := hexStr(repeat(0x33, 32)) + iv16 := hexStr(repeat(0x22, 16)) + iv23 := hexStr(repeat(0x44, 23)) + + cases := []struct { + tcID int + comment string + key string + nonce string + result string + }{ + {1, "15-byte key", hexStr(repeat(0x11, 15)), iv16, "invalid"}, + {2, "17-byte key", hexStr(repeat(0x11, 17)), iv16, "invalid"}, + {3, "15-byte iv", key16, hexStr(repeat(0x22, 15)), "invalid"}, + {4, "17-byte iv", key16, hexStr(repeat(0x22, 17)), "invalid"}, + {5, "32-byte key with a 16-byte iv", key32, iv16, "invalid"}, + {6, "16-byte key with a 23-byte iv", key16, iv23, "invalid"}, + {7, "31-byte key with a 23-byte iv", key31, iv23, "invalid"}, + {8, "32-byte key with a 23-byte iv selects ZUC-256", key32, iv23, "valid"}, + } + out := make([]vectors.Case, 0, len(cases)) + for _, c := range cases { + out = append(out, vectors.Case{ + TCID: c.tcID, + Comment: c.comment, + Result: c.result, + Key: c.key, + Nonce: c.nonce, + }) + } + return out +} + +// constructedEIA holds the pinned MACs for the 32-byte message and the +// all-zero IV under the 0x5a key. +var constructedEIA = []struct { + nbits int + mac string +}{ + {1, "bebdc477"}, + {7, "a725890b"}, + {8, "a9b714f9"}, + {31, "100f8983"}, + {32, "b350646f"}, + {33, "e597c564"}, + {63, "b7577847"}, + {64, "ea037a1d"}, + {65, "af290d0b"}, + {127, "e8410d0e"}, + {128, "a4e4e992"}, +} + +// buildEIA pins the ZUC-128 MAC. The official cases use the published frame +// parameters; the constructed cases use a zero count, bearer and direction, +// whose frame IV is all zero, and a fixed 32-byte message. +func buildEIA() []vectors.Case { + var out []vectors.Case + for i, v := range officialEIA { + out = append(out, vectors.Case{ + TCID: i + 1, + Comment: fmt.Sprintf("GB/T 33133 EIA test set %d, %d-bit message", i+1, v.nbits), + Result: "valid", + Key: v.key, + Nonce: hexStr(eiaFrameIV(v.count, v.bearer, v.direction)), + Count: v.count, + Bearer: v.bearer, + Direction: v.direction, + Input: v.in, + BitLen: v.nbits, + Expected: v.mac, + }) + } + key := hexStr(repeat(0x5a, 16)) + msg := hexStr(seq(32)) + for i, c := range constructedEIA { + out = append(out, vectors.Case{ + TCID: len(officialEIA) + i + 1, + Comment: fmt.Sprintf("constructed: %d bits of a 32-byte message", c.nbits), + Result: "valid", + Key: key, + Nonce: hexStr(eiaFrameIV(0, 0, 0)), + Input: msg, + BitLen: c.nbits, + Expected: c.mac, + }) + } + return out +} + +// buildEIA256 pins the ZUC-256 MAC for the three supported tag sizes. Each +// official case is emitted once per tag size. +func buildEIA256() []vectors.Case { + cases := []struct { + keyHex string + ivHex string + msgHex string + nMsgs int + macs [3]string + comment string + }{ + {hexStr(repeat(0x00, 32)), hexStr(repeat(0x00, 23)), hexStr(repeat(0x00, 50)), 1, + [3]string{"9b972a74", "673e54990034d38c", "d85e54bbcb9600967084c952a1654b26"}, + "GB/T 33133 ZUC-256 test set 1: zero key and iv"}, + {hexStr(repeat(0x00, 32)), hexStr(repeat(0x00, 23)), hexStr(repeat(0x11, 50)), 10, + [3]string{"8754f5cf", "130dc225e72240cc", "df1e8307b31cc62beca1ac6f8190c22f"}, + "GB/T 33133 ZUC-256 test set 2: zero key and iv, ten 0x11 messages"}, + {hexStr(repeat(0xff, 32)), hexStr(repeat(0xff, 23)), hexStr(repeat(0x00, 50)), 1, + [3]string{"1f3079b4", "8c71394d39957725", "a35bb274b567c48b28319f111af34fbd"}, + "GB/T 33133 ZUC-256 test set 3: all-ones key and iv"}, + {hexStr(repeat(0xff, 32)), hexStr(repeat(0xff, 23)), hexStr(repeat(0x11, 50)), 10, + [3]string{"5c7c8b88", "ea1dee544bb6223b", "3a83b554be408ca5494124ed9d473205"}, + "GB/T 33133 ZUC-256 test set 4: all-ones key and iv, ten 0x11 messages"}, + } + tagSizes := [3]int{4, 8, 16} + var out []vectors.Case + for _, c := range cases { + msg := mustHex(c.msgHex) + input := make([]byte, 0, len(msg)*c.nMsgs) + for range c.nMsgs { + input = append(input, msg...) + } + for i, tagSize := range tagSizes { + out = append(out, vectors.Case{ + TCID: len(out) + 1, + Comment: fmt.Sprintf("%s, %d-byte tag", c.comment, tagSize), + Result: "valid", + Key: c.keyHex, + Nonce: c.ivHex, + Input: hexStr(input), + TagSize: tagSize, + Expected: c.macs[i], + }) + } + } + + invalid := []struct { + tcID int + comment string + key string + nonce string + tagSize int + }{ + {len(out) + 1, "1-byte tag", hexStr(repeat(0x00, 32)), hexStr(repeat(0x00, 23)), 1}, + {len(out) + 2, "15-byte tag", hexStr(repeat(0x00, 32)), hexStr(repeat(0x00, 23)), 15}, + {len(out) + 3, "17-byte tag", hexStr(repeat(0x00, 32)), hexStr(repeat(0x00, 23)), 17}, + {len(out) + 4, "32-byte tag", hexStr(repeat(0x00, 32)), hexStr(repeat(0x00, 23)), 32}, + {len(out) + 5, "31-byte key", hexStr(repeat(0x00, 31)), hexStr(repeat(0x00, 23)), 4}, + {len(out) + 6, "22-byte iv", hexStr(repeat(0x00, 32)), hexStr(repeat(0x00, 22)), 4}, + {len(out) + 7, "24-byte iv", hexStr(repeat(0x00, 32)), hexStr(repeat(0x00, 24)), 4}, + } + for _, c := range invalid { + out = append(out, vectors.Case{ + TCID: c.tcID, + Comment: c.comment, + Result: "invalid", + Key: c.key, + Nonce: c.nonce, + TagSize: c.tagSize, + Input: hexStr(repeat(0x00, 16)), + }) + } + return out +} + +// BuildSuite builds the ZUC suite. +func BuildSuite() (*vectors.Suite, error) { + suite := &vectors.Suite{ + Schema: vectors.SemanticsSchema, + Algorithm: "zuc", + Generator: "go test ./internal/cryptotest/zucvectors -run TestVectorsUpToDate -update", + Groups: []vectors.Group{ + { + Type: "ZUCEEA", + Comment: "ZUC-128 EEA keystream from the GB/T 33133 test sets. Each case carries the frame parameters and the frame IV derived from them, so the frame API and the raw key/IV API must agree with the published keystream.", + Tests: buildEEA(), + }, + { + Type: "ZUCEEAInit", + Comment: "Stream cipher key and IV length checks. A 32-byte key with a 23-byte IV selects ZUC-256; every mismatched combination must be rejected.", + Tests: buildEEAInit(), + }, + { + Type: "ZUCEIA", + Comment: "ZUC-128 EIA MAC. The official 1-, 577- and 5670-bit vectors anchor the bit-oriented path; the constructed cases pin the word and block boundaries between them.", + Tests: buildEIA(), + }, + { + Type: "ZUC256EIA", + Comment: "ZUC-256 EIA MAC from the GB/T 33133 test sets, once per supported tag size (4, 8 and 16 bytes), plus the rejected tag, key and IV sizes.", + Tests: buildEIA256(), + }, + }, + } + if err := suite.Validate(); err != nil { + return nil, err + } + return suite, nil +} + +// MarshalSuite renders a suite the way the committed files are stored. +func MarshalSuite(s *vectors.Suite) ([]byte, error) { + data, err := json.MarshalIndent(s, "", " ") + if err != nil { + return nil, err + } + return append(data, '\n'), nil +} diff --git a/internal/cryptotest/zucvectors/generate_test.go b/internal/cryptotest/zucvectors/generate_test.go new file mode 100644 index 00000000..1cd84fa5 --- /dev/null +++ b/internal/cryptotest/zucvectors/generate_test.go @@ -0,0 +1,98 @@ +// Copyright 2026 Sun Yimin. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package zucvectors + +import ( + "bytes" + "flag" + "os" + "path/filepath" + "testing" +) + +var update = flag.Bool("update", false, "rewrite the committed vector files") + +// TestVectorsUpToDate pins the committed vector file to the deterministic +// output of the builders. Run `go test ./internal/cryptotest/zucvectors -run +// TestVectorsUpToDate -update` after changing a case so the regenerated +// vectors are reviewed like code. +func TestVectorsUpToDate(t *testing.T) { + suite, err := BuildSuite() + if err != nil { + t.Fatal(err) + } + want, err := MarshalSuite(suite) + if err != nil { + t.Fatal(err) + } + path := filepath.Join("..", "vectors", "testdata", fileName) + + if *update { + if err := os.WriteFile(path, want, 0o644); err != nil { + t.Fatalf("writing %s: %v", path, err) + } + t.Logf("updated %s", path) + return + } + + got, err := os.ReadFile(path) + if err != nil { + t.Fatalf("reading %s (run with -update to create it): %v", path, err) + } + if !bytes.Equal(got, want) { + t.Fatalf("%s is stale: run `go test ./internal/cryptotest/zucvectors -run TestVectorsUpToDate -update` and review the diff", path) + } +} + +// TestFrameIV pins both frame IV layouts independently of the library. The +// EEA layout puts the bearer in the top five bits of the octet after the +// counter and the direction below it; the EIA layout leaves the bearer octet +// alone and carries the direction at the top of the second counter copy and +// in the fourteenth octet. +func TestFrameIV(t *testing.T) { + eea := eeaFrameIV(0x66035492, 0xf, 1) + wantEEA := []byte{ + 0x66, 0x03, 0x54, 0x92, + 0x7c, 0x00, 0x00, 0x00, + 0x66, 0x03, 0x54, 0x92, + 0x7c, 0x00, 0x00, 0x00, + } + if !bytes.Equal(eea, wantEEA) { + t.Fatalf("eeaFrameIV() = %x, want %x", eea, wantEEA) + } + + eia := eiaFrameIV(0xa94059da, 0x0a, 1) + wantEIA := []byte{ + 0xa9, 0x40, 0x59, 0xda, + 0x50, 0x00, 0x00, 0x00, + 0xa9 ^ 0x80, 0x40, 0x59, 0xda, + 0x50, 0x00, 0x80, 0x00, + } + if !bytes.Equal(eia, wantEIA) { + t.Fatalf("eiaFrameIV() = %x, want %x", eia, wantEIA) + } + + zero := make([]byte, 16) + if got := eeaFrameIV(0, 0, 0); !bytes.Equal(got, zero) { + t.Fatalf("eeaFrameIV(0, 0, 0) = %x, want %x", got, zero) + } + if got := eiaFrameIV(0, 0, 0); !bytes.Equal(got, zero) { + t.Fatalf("eiaFrameIV(0, 0, 0) = %x, want %x", got, zero) + } +} + +// TestSuiteGroups checks that every group the consumer test asks for exists +// exactly once. +func TestSuiteGroups(t *testing.T) { + suite, err := BuildSuite() + if err != nil { + t.Fatal(err) + } + for _, name := range []string{"ZUCEEA", "ZUCEEAInit", "ZUCEIA", "ZUC256EIA"} { + if _, err := suite.Group(name); err != nil { + t.Fatal(err) + } + } +} diff --git a/internal/cryptotest/zucvectors/official_vectors.go b/internal/cryptotest/zucvectors/official_vectors.go new file mode 100644 index 00000000..ac834b1c --- /dev/null +++ b/internal/cryptotest/zucvectors/official_vectors.go @@ -0,0 +1,111 @@ +// Copyright 2026 Sun Yimin. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +// Official ZUC vectors transcribed from the KATs committed in +// zuc/eea_test.go and zuc/eia_test.go. The values are the published ones; +// the consumer test replays them so a transcription error fails there. +package zucvectors + +type eeaVector struct { + key string + count uint32 + bearer uint32 + direction uint32 + in string + out string +} + +type eiaVector struct { + key string + count uint32 + bearer uint32 + direction uint32 + in string + nbits int + mac string +} + +var officialEEA = []eeaVector{ + {key: "173d14ba5003731d7a60049470f00a29", count: 0x66035492, bearer: 0xf, direction: 0, + in: "6cf65340735552ab0c9752fa6f9025fe0bd675d9005875b2", + out: "a6c85fc66afb8533aafc2518dfe784940ee1e4b030238cc8"}, + {key: "e5bd3ea0eb55ade866c6ac58bd54302a", count: 0x56823, bearer: 0x18, direction: 1, + in: "14a8ef693d678507bbe7270a7f67ff5006c3525b9807e467c4e56000ba338f5d" + + "429559036751822246c80d3b38f07f4be2d8ff5805f5132229bde93bbbdcaf38" + + "2bf1ee972fbf9977bada8945847a2a6c9ad34a667554e04d1f7fa2c33241bd8f" + + "01ba220d", + out: "131d43e0dea1be5c5a1bfd971d852cbf712d7b4f57961fea3208afa8bca433f4" + + "56ad09c7417e58bc69cf8866d1353f74865e80781d202dfb3ecff7fcbc3b190f" + + "e82a204ed0e350fc0f6f2613b2f2bca6df5a473a57a4a00d985ebad880d6f238" + + "64a07b01"}, + {key: "e13fed21b46e4e7ec31253b2bb17b3e0", count: 0x2738cdaa, bearer: 0x1a, direction: 0, + in: "8d74e20d54894e06d3cb13cb3933065e8674be62adb1c72b3a646965ab63cb7b" + + "7854dfdc27e84929f49c64b872a490b13f957b64827e71f41fbd4269a42c97f8" + + "24537027f86e9f4ad82d1df451690fdd98b6d03f3a0ebe3a312d6b840ba5a182" + + "0b2a2c9709c090d245ed267cf845ae41fa975d3333ac3009fd40eba9eb5b8857" + + "14b768b697138baf21380eca49f644d48689e4215760b906739f0d2b3f091133" + + "ca15d981cbe401baf72d05ace05cccb2d297f4ef6a5f58d91246cfa77215b892" + + "ab441d5278452795ccb7f5d79057a1c4f77f80d46db2033cb79bedf8e60551ce" + + "10c667f62a97abafabbcd6772018df96a282ea737ce2cb331211f60d5354ce78" + + "f9918d9c206ca042c9b62387dd709604a50af16d8d35a8906be484cf2e74a928" + + "9940364353249b27b4c9ae29eddfc7da6418791a4e7baa0660fa64511f2d685c" + + "c3a5ff70e0d2b74292e3b8a0cd6b04b1c790b8ead2703708540dea2fc09c3da7" + + "70f65449c84d817a4f551055e19ab85018a0028b71a144d96791e9a357793350" + + "4eee0060340c69d274e1bf9d805dcbcc1a6faa976800b6ff2b671dc463652fa8" + + "a33ee50974c1c21be01eabb2167430269d72ee511c9dde30797c9a25d86ce74f" + + "5b961be5fdfb6807814039e7137636bd1d7fa9e09efd2007505906a5ac45dfde" + + "ed7757bbee745749c29633350bee0ea6f409df458016", + out: "94eaa4aa30a57137ddf09b97b25618a20a13e2f10fa5bf8161a879cc2ae797a6" + + "b4cf2d9df31debb9905ccfec97de605d21c61ab8531b7f3c9da5f03931f8a064" + + "2de48211f5f52ffea10f392a047669985da454a28f080961a6c2b62daa17f33c" + + "d60a4971f48d2d909394a55f48117ace43d708e6b77d3dc46d8bc017d4d1abb7" + + "7b7428c042b06f2f99d8d07c9879d99600127a31985f1099bbd7d6c1519ede8f" + + "5eeb4a610b349ac01ea2350691756bd105c974a53eddb35d1d4100b012e522ab" + + "41f4c5f2fde76b59cb8b96d885cfe4080d1328a0d636cc0edc05800b76acca8f" + + "ef672084d1f52a8bbd8e0993320992c7ffbae17c408441e0ee883fc8a8b05e22" + + "f5ff7f8d1b48c74c468c467a028f09fd7ce91109a570a2d5c4d5f4fa18c5dd3e" + + "4562afe24ef771901f59af645898acef088abae07e92d52eb2de55045bb1b7c4" + + "164ef2d7a6cac15eeb926d7ea2f08b66e1f759f3aee44614725aa3c7482b3084" + + "4c143ff87b53f1e583c501257dddd096b81268daa303f17234c2333541f0bb8e" + + "190648c5807c866d7193228609adb948686f7de294a802cc38f7fe5208f5ea31" + + "96d0167b9bdd02f0d2a5221ca508f893af5c4b4bb9f4f520fd84289b3dbe7e61" + + "497a7e2a584037ea637b6981127174af57b471df4b2768fd79c1540fb3edf2ea" + + "22cb69bec0cf8d933d9c6fdd645e850591cca3d62c0c"}, +} + +var officialEIA = []eiaVector{ + {key: "00000000000000000000000000000000", count: 0x0, bearer: 0x0, direction: 0, + in: "00000000", + nbits: 1, mac: "c8a9595e"}, + {key: "c9e6cec4607c72db000aefa88385ab0a", count: 0xa94059da, bearer: 0xa, direction: 1, + in: "983b41d47d780c9e1ad11d7eb70391b1de0b35da2dc62f83e7b78d6306ca0ea0" + + "7e941b7be91348f9fcb170e2217fecd97f9f68adb16e5d7d21e569d280ed775c" + + "ebde3f4093c5388100000000", + nbits: 577, mac: "fae8ff0b"}, + {key: "6b8b08ee79e0b5982d6d128ea9f220cb", count: 0x561eb2dd, bearer: 0x1c, direction: 0, + in: "5bad724710ba1c56d5a315f8d40f6e093780be8e8de07b6992432018e08ed96a" + + "5734af8bad8a575d3a1f162f85045cc770925571d9f5b94e454a77c16e72936b" + + "f016ae157499f0543b5d52caa6dbeab697d2bb73e41b8075dce79b4b86044f66" + + "1d4485a543dd78606e0419e8059859d3cb2b67ce0977603f81ff839e33185954" + + "4cfbc8d00fef1a4c8510fb547d6b06c611ef44f1bce107cfa45a06aab360152b" + + "28dc1ebe6f7fe09b0516f9a5b02a1bd84bb0181e2e89e19bd8125930d178682f" + + "3862dc51b636f04e720c47c3ce51ad70d94b9b2255fbae906549f499f8c6d399" + + "47ed5e5df8e2def113253e7b08d0a76b6bfc68c812f375c79b8fe5fd85976aa6" + + "d46b4a2339d8ae5147f680fbe70f978b38effd7b2f7866a22554e193a94e98a6" + + "8b74bd25bb2b3f5fb0a5fd59887f9ab68159b7178d5b7b677cb546bf41eadca2" + + "16fc10850128f8bdef5c8d89f96afa4fa8b54885565ed838a950fee5f1c3b0a4" + + "f6fb71e54dfd169e82cecc7266c850e67c5ef0ba960f5214060e71eb172a75fc" + + "1486835cbea6534465b055c96a72e4105224182325d830414b40214daa8091d2" + + "e0fb010ae15c6de90850973bdf1e423be148a237b87a0c9f34d4b47605b803d7" + + "43a86a90399a4af396d3a1200a62f3d9507962e8e5bee6d3da2bb3f7237664ac" + + "7a292823900bc63503b29e80d63f6067bf8e1716ac25beba350deb62a99fe031" + + "85eb4f69937ecd387941fda544ba67db0911774938b01827bcc69c92b3f772a9" + + "d2859ef003398b1f6bbad7b574f7989a1d10b2df798e0dbf30d6587464d24878" + + "cd00c0eaee8a1a0cc753a27979e11b41db1de3d5038afaf49f5c682c3748d8a3" + + "a9ec54e6a371275f1683510f8e4f90938f9ab6e134c2cfdf4841cba88e0cff2b" + + "0bcc8e6adcb71109b5198fecf1bb7e5c531aca50a56a8a3b6de59862d41fa113" + + "d9cd957808f08571d9a4bb792af271f6cc6dbb8dc7ec36e36be1ed308164c31c" + + "7c0afc541c000000", + nbits: 5670, mac: "0ca12792"}, +} diff --git a/zuc/semantics_vectors_test.go b/zuc/semantics_vectors_test.go new file mode 100644 index 00000000..be263d40 --- /dev/null +++ b/zuc/semantics_vectors_test.go @@ -0,0 +1,255 @@ +// Copyright 2026 Sun Yimin. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package zuc + +import ( + "bytes" + "encoding/hex" + "fmt" + "testing" + + "github.com/emmansun/gmsm/internal/cryptotest/vectors" +) + +// TestSemanticsVectors replays the committed ZUC vectors. The EEA cases must +// produce the published keystream through both the frame API and the raw +// key/IV API, and seeking into the stream must agree with a sequential pass. +// The EIA cases must produce the published MAC through both entry points and +// across a split write. A panic on a rejected parameter counts as a failure. +func TestSemanticsVectors(t *testing.T) { + suite, err := vectors.Load("zuc_semantics.json") + if err != nil { + t.Fatal(err) + } + t.Run("ZUCEEA", func(t *testing.T) { testEEAVectors(t, suite) }) + t.Run("ZUCEEAInit", func(t *testing.T) { testEEAInitVectors(t, suite) }) + t.Run("ZUCEIA", func(t *testing.T) { testEIAVectors(t, suite) }) + t.Run("ZUC256EIA", func(t *testing.T) { testEIA256Vectors(t, suite) }) +} + +func testEEAVectors(t *testing.T, suite *vectors.Suite) { + group, err := suite.Group("ZUCEEA") + if err != nil { + t.Fatal(err) + } + for _, tc := range group.Tests { + t.Run(fmt.Sprintf("tc%d", tc.TCID), func(t *testing.T) { + key := semanticsHex(t, "key", tc.Key) + iv := semanticsHex(t, "nonce", tc.Nonce) + input := semanticsHex(t, "input", tc.Input) + want := semanticsHex(t, "expected", tc.Expected) + + frame, err := NewEEACipher(key, tc.Count, tc.Bearer, tc.Direction) + if err != nil { + t.Fatalf("NewEEACipher() error: %v: %s", err, tc.Comment) + } + got := make([]byte, len(input)) + semanticsCall(t, tc, func() { frame.XORKeyStream(got, input) }) + if !bytes.Equal(got, want) { + t.Fatalf("frame XORKeyStream() = %x, want %x", got, want) + } + + raw, err := NewCipher(key, iv) + if err != nil { + t.Fatalf("NewCipher() error: %v", err) + } + sequential := make([]byte, len(input)) + semanticsCall(t, tc, func() { raw.XORKeyStream(sequential, input) }) + if !bytes.Equal(sequential, want) { + t.Fatalf("raw XORKeyStream() = %x, want %x: the frame IV derived in the generator and the one derived by the library disagree", sequential, want) + } + + cut := len(input) / 2 + first := make([]byte, cut) + second := make([]byte, len(input)-cut) + semanticsCall(t, tc, func() { + frame.XORKeyStreamAt(first, input[:cut], 0) + frame.XORKeyStreamAt(second, input[cut:], uint64(cut)) + }) + if seeked := append(first, second...); !bytes.Equal(seeked, want) { + t.Fatalf("XORKeyStreamAt() split at %d = %x, want %x", cut, seeked, want) + } + + const offset = 7 + if len(input) > offset { + tail := make([]byte, len(input)-offset) + semanticsCall(t, tc, func() { frame.XORKeyStreamAt(tail, input[offset:], offset) }) + if !bytes.Equal(tail, want[offset:]) { + t.Fatalf("XORKeyStreamAt(offset %d) = %x, want %x", offset, tail, want[offset:]) + } + } + }) + } +} + +func testEEAInitVectors(t *testing.T, suite *vectors.Suite) { + group, err := suite.Group("ZUCEEAInit") + if err != nil { + t.Fatal(err) + } + for _, tc := range group.Tests { + t.Run(fmt.Sprintf("tc%d", tc.TCID), func(t *testing.T) { + key := semanticsHex(t, "key", tc.Key) + iv := semanticsHex(t, "nonce", tc.Nonce) + err := semanticsCallErr(t, tc, func() error { + _, err := NewCipher(key, iv) + return err + }) + if want := tc.Result == "valid"; (err == nil) != want { + t.Fatalf("NewCipher(key %d bytes, iv %d bytes) error = %v, want valid=%v: %s", + len(key), len(iv), err, want, tc.Comment) + } + }) + } +} + +func testEIAVectors(t *testing.T, suite *vectors.Suite) { + group, err := suite.Group("ZUCEIA") + if err != nil { + t.Fatal(err) + } + for _, tc := range group.Tests { + t.Run(fmt.Sprintf("tc%d", tc.TCID), func(t *testing.T) { + key := semanticsHex(t, "key", tc.Key) + iv := semanticsHex(t, "nonce", tc.Nonce) + input := semanticsHex(t, "input", tc.Input) + want := semanticsHex(t, "expected", tc.Expected) + nbits := tc.BitLen + if nbits == 0 { + nbits = len(input) * 8 + } + + frame, err := NewEIAHash(key, tc.Count, tc.Bearer, tc.Direction) + if err != nil { + t.Fatalf("NewEIAHash() error: %v", err) + } + got := semanticsFinish(t, tc, frame, input, nbits) + if !bytes.Equal(got, want) { + t.Fatalf("frame Finish(%d bits) = %x, want %x: %s", nbits, got, want, tc.Comment) + } + + raw, err := NewHash(key, iv) + if err != nil { + t.Fatalf("NewHash() error: %v", err) + } + got = semanticsFinish(t, tc, raw, input, nbits) + if !bytes.Equal(got, want) { + t.Fatalf("raw Finish(%d bits) = %x, want %x: the frame IV derived in the generator and the one derived by the library disagree", nbits, got, want) + } + + cut := min(len(input)/2, nbits/8) + split, err := NewHash(key, iv) + if err != nil { + t.Fatalf("NewHash() error: %v", err) + } + if _, err := split.Write(input[:cut]); err != nil { + t.Fatalf("Write() error: %v", err) + } + got = semanticsFinish(t, tc, split, input[cut:], nbits-cut*8) + if !bytes.Equal(got, want) { + t.Fatalf("split Finish(%d bits after a %d-byte write) = %x, want %x", nbits-cut*8, cut, got, want) + } + }) + } +} + +func testEIA256Vectors(t *testing.T, suite *vectors.Suite) { + group, err := suite.Group("ZUC256EIA") + if err != nil { + t.Fatal(err) + } + for _, tc := range group.Tests { + t.Run(fmt.Sprintf("tc%d", tc.TCID), func(t *testing.T) { + key := semanticsHex(t, "key", tc.Key) + iv := semanticsHex(t, "nonce", tc.Nonce) + input := semanticsHex(t, "input", tc.Input) + want := semanticsHex(t, "expected", tc.Expected) + + var h EIA + err := semanticsCallErr(t, tc, func() (err error) { + h, err = NewHash256(key, iv, tc.TagSize) + return err + }) + if tc.Result == "invalid" { + if err == nil { + t.Fatalf("NewHash256() accepted tag %d, key %d bytes, iv %d bytes: %s", + tc.TagSize, len(key), len(iv), tc.Comment) + } + return + } + if err != nil { + t.Fatalf("NewHash256() error: %v: %s", err, tc.Comment) + } + + // Sum must return the full tag without disturbing the state. + if _, err := h.Write(input); err != nil { + t.Fatalf("Write() error: %v", err) + } + if got := h.Sum(nil); !bytes.Equal(got, want) { + t.Fatalf("Sum() = %x, want %x: %s", got, want, tc.Comment) + } + + fresh, err := NewHash256(key, iv, tc.TagSize) + if err != nil { + t.Fatalf("NewHash256() error: %v", err) + } + got := semanticsFinish(t, tc, fresh, input, len(input)*8) + if !bytes.Equal(got, want) { + t.Fatalf("Finish(%d bits) = %x, want %x: %s", len(input)*8, got, want, tc.Comment) + } + if len(got) != tc.TagSize { + t.Fatalf("Finish() returned %d bytes, want %d", len(got), tc.TagSize) + } + + // Finish resets the instance, so a second call must repeat the tag. + if got := semanticsFinish(t, tc, fresh, input, len(input)*8); !bytes.Equal(got, want) { + t.Fatalf("second Finish() = %x, want %x after the internal reset", got, want) + } + }) + } +} + +func semanticsFinish(t *testing.T, tc vectors.Case, h EIA, input []byte, nbits int) []byte { + t.Helper() + var mac []byte + semanticsCall(t, tc, func() { mac = h.Finish(input, nbits) }) + return mac +} + +func semanticsHex(t *testing.T, name, value string) []byte { + t.Helper() + if value == "" { + return nil + } + data, err := hex.DecodeString(value) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + return data +} + +// semanticsCallErr runs one vector call that reports an error and turns a +// panic into a test failure that names the case. +func semanticsCallErr(t *testing.T, tc vectors.Case, call func() error) (err error) { + t.Helper() + defer func() { + if p := recover(); p != nil { + t.Fatalf("tc%d panicked: %v", tc.TCID, p) + } + }() + return call() +} + +// semanticsCall runs one vector call and turns a panic into a test failure +// that names the case. +func semanticsCall(t *testing.T, tc vectors.Case, call func()) { + t.Helper() + defer func() { + if p := recover(); p != nil { + t.Fatalf("tc%d panicked: %v", tc.TCID, p) + } + }() + call() +}