github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/crypto/chacha20/chacha20_test.gno (about) 1 // Copyright (c) 2016 Andreas Auernhammer. All rights reserved. 2 // Use of this source code is governed by a license that can be 3 // found in the LICENSE file. 4 5 package chacha20 6 7 import ( 8 "bytes" 9 "crypto/chacha20/chacha" 10 "encoding/hex" 11 "testing" 12 ) 13 14 func toHex(bits []byte) string { 15 return hex.EncodeToString(bits) 16 } 17 18 func fromHex(bits string) []byte { 19 b, err := hex.DecodeString(bits) 20 if err != nil { 21 panic(err) 22 } 23 return b 24 } 25 26 func TestVectors(t *testing.T) { 27 for i, v := range vectors { 28 if len(v.plaintext) == 0 { 29 v.plaintext = make([]byte, len(v.ciphertext)) 30 } 31 32 dst := make([]byte, len(v.ciphertext)) 33 34 XORKeyStream(dst, v.plaintext, v.nonce, v.key) 35 if !bytes.Equal(dst, v.ciphertext) { 36 t.Errorf("Test %d: ciphertext mismatch:\n \t got: %s\n \t want: %s", i, toHex(dst), toHex(v.ciphertext)) 37 } 38 39 c, err := NewCipher(v.nonce, v.key) 40 if err != nil { 41 t.Fatal(err) 42 } 43 c.XORKeyStream(dst[:1], v.plaintext[:1]) 44 c.XORKeyStream(dst[1:], v.plaintext[1:]) 45 if !bytes.Equal(dst, v.ciphertext) { 46 t.Errorf("Test %d: ciphertext mismatch:\n \t got: %s\n \t want: %s", i, toHex(dst), toHex(v.ciphertext)) 47 } 48 } 49 } 50 51 func benchmarkCipher(b *testing.B, size int, nonceSize int) { 52 var key [32]byte 53 nonce := make([]byte, nonceSize) 54 c, _ := NewCipher(nonce, key[:]) 55 buf := make([]byte, size) 56 57 b.SetBytes(int64(len(buf))) 58 b.ResetTimer() 59 for i := 0; i < b.N; i++ { 60 c.XORKeyStream(buf, buf) 61 } 62 } 63 64 func benchmarkXORKeyStream(b *testing.B, size int, nonceSize int) { 65 var key [32]byte 66 nonce := make([]byte, nonceSize) 67 buf := make([]byte, size) 68 b.SetBytes(int64(len(buf))) 69 b.ResetTimer() 70 for i := 0; i < b.N; i++ { 71 XORKeyStream(buf, buf, nonce[:], key[:]) 72 } 73 } 74 75 func BenchmarkChaCha20_64(b *testing.B) { benchmarkCipher(b, 64, chacha.NonceSize) } 76 func BenchmarkChaCha20_1K(b *testing.B) { benchmarkCipher(b, 1024, chacha.NonceSize) } 77 func BenchmarkXChaCha20_64(b *testing.B) { benchmarkXORKeyStream(b, 64, chacha.XNonceSize) } 78 func BenchmarkXChaCha20_1K(b *testing.B) { benchmarkXORKeyStream(b, 1024, chacha.XNonceSize) } 79 func BenchmarkXORKeyStream64(b *testing.B) { benchmarkXORKeyStream(b, 64, chacha.NonceSize) } 80 func BenchmarkXORKeyStream1K(b *testing.B) { benchmarkXORKeyStream(b, 1024, chacha.NonceSize) } 81 func BenchmarkXChaCha20_XORKeyStream64(b *testing.B) { benchmarkXORKeyStream(b, 64, chacha.XNonceSize) } 82 func BenchmarkXChaCha20_XORKeyStream1K(b *testing.B) { 83 benchmarkXORKeyStream(b, 1024, chacha.XNonceSize) 84 } 85 86 var vectors = []struct { 87 key, nonce, plaintext, ciphertext []byte 88 }{ 89 { 90 fromHex("0000000000000000000000000000000000000000000000000000000000000000"), 91 fromHex("0000000000000000"), 92 nil, 93 fromHex("76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586"), 94 }, 95 { 96 fromHex("0000000000000000000000000000000000000000000000000000000000000000"), 97 fromHex("000000000000000000000000"), 98 nil, 99 fromHex("76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586"), 100 }, 101 { 102 fromHex("0000000000000000000000000000000000000000000000000000000000000000"), 103 fromHex("000000000000000000000000000000000000000000000000"), 104 nil, 105 fromHex("bcd02a18bf3f01d19292de30a7a8fdaca4b65e50a6002cc72cd6d2f7c91ac3d5728f83e0aad2bfcf9abd2d2db58faedd65015dd83fc09b131e271043019e8e0f"), 106 }, 107 }