From 178241aa0f504d986eab5a26bc203bd063f5f6b5 Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Thu, 7 Mar 2024 17:35:48 +0800 Subject: [PATCH] avoid legacy 32 bit system compile error --- cipher/ccm_64bit_test.go | 31 +++++++++++++++++++++++++++++++ cipher/ccm_test.go | 20 -------------------- kdf/kdf_64bit_test.go | 16 ++++++++++++++++ kdf/kdf_test.go | 7 ------- 4 files changed, 47 insertions(+), 27 deletions(-) create mode 100644 cipher/ccm_64bit_test.go create mode 100644 kdf/kdf_64bit_test.go diff --git a/cipher/ccm_64bit_test.go b/cipher/ccm_64bit_test.go new file mode 100644 index 0000000..62fcb25 --- /dev/null +++ b/cipher/ccm_64bit_test.go @@ -0,0 +1,31 @@ +//go:build !(arm || mips) + +package cipher_test + +import ( + "crypto/aes" + "encoding/hex" + "testing" + + "github.com/emmansun/gmsm/cipher" +) + +func TestCCMLongAd(t *testing.T) { + key, _ := hex.DecodeString("ab72c77b97cb5fe9a382d9fe81ffdbed") + nonce, _ := hex.DecodeString("54cc7dc2c37ec006bcc6d1db") + + c, _ := aes.NewCipher(key) + aesccm, _ := cipher.NewCCM(c) + + ad := make([]byte, 0x10000) + ct := aesccm.Seal(nil, nonce, nil, ad) + if hex.EncodeToString(ct) != "e1ad65c3bfaba94b1085aff8c6ea2698" { + t.Errorf("got %s, want e1ad65c3bfaba94b1085aff8c6ea2698", hex.EncodeToString(ct)) + } + + ad = make([]byte, 1<<32+1) + ct = aesccm.Seal(nil, nonce, nil, ad) + if hex.EncodeToString(ct) != "c1949a661c605ff5640a29dd3e285ddb" { + t.Errorf("got %s, want c1949a661c605ff5640a29dd3e285ddb", hex.EncodeToString(ct)) + } +} diff --git a/cipher/ccm_test.go b/cipher/ccm_test.go index a166b35..41c29be 100644 --- a/cipher/ccm_test.go +++ b/cipher/ccm_test.go @@ -277,23 +277,3 @@ func TestCCMTagFailureOverwrite(t *testing.T) { } } } - -func TestCCMLongAd(t *testing.T) { - key, _ := hex.DecodeString("ab72c77b97cb5fe9a382d9fe81ffdbed") - nonce, _ := hex.DecodeString("54cc7dc2c37ec006bcc6d1db") - - c, _ := aes.NewCipher(key) - aesccm, _ := cipher.NewCCM(c) - - ad := make([]byte, 0x10000) - ct := aesccm.Seal(nil, nonce, nil, ad) - if hex.EncodeToString(ct) != "e1ad65c3bfaba94b1085aff8c6ea2698" { - t.Errorf("got %s, want e1ad65c3bfaba94b1085aff8c6ea2698", hex.EncodeToString(ct)) - } - - ad = make([]byte, 1<<32+1) - ct = aesccm.Seal(nil, nonce, nil, ad) - if hex.EncodeToString(ct) != "c1949a661c605ff5640a29dd3e285ddb" { - t.Errorf("got %s, want c1949a661c605ff5640a29dd3e285ddb", hex.EncodeToString(ct)) - } -} diff --git a/kdf/kdf_64bit_test.go b/kdf/kdf_64bit_test.go new file mode 100644 index 0000000..80372a8 --- /dev/null +++ b/kdf/kdf_64bit_test.go @@ -0,0 +1,16 @@ +//go:build !(arm || mips) + +package kdf + +import ( + "testing" + + "github.com/emmansun/gmsm/sm3" +) + +// This case should be failed on 32bits system. +func TestKdfPanic(t *testing.T) { + shouldPanic(t, func() { + Kdf(sm3.New(), []byte("123456"), 1<<37) + }) +} diff --git a/kdf/kdf_test.go b/kdf/kdf_test.go index 0593201..294ee44 100644 --- a/kdf/kdf_test.go +++ b/kdf/kdf_test.go @@ -60,13 +60,6 @@ func shouldPanic(t *testing.T, f func()) { t.Errorf("should have panicked") } -// This case should be failed on 32bits system. -func TestKdfPanic(t *testing.T) { - shouldPanic(t, func() { - Kdf(sm3.New(), []byte("123456"), 1<<37) - }) -} - func BenchmarkKdf(b *testing.B) { tests := []struct { zLen int