github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/crypto/xchacha20poly1305/xchachapoly_test.go (about) 1 package xchacha20poly1305 2 3 import ( 4 "bytes" 5 cr "crypto/rand" 6 mr "math/rand" 7 "testing" 8 ) 9 10 // The following test is taken from 11 // https://github.com/golang/crypto/blob/master/chacha20poly1305/chacha20poly1305_test.go#L69 12 // It requires the below copyright notice, where "this source code" refers to the following function. 13 // Copyright 2016 The Go Authors. All rights reserved. 14 // Use of this source code is governed by a BSD-style 15 // license that can be found at the bottom of this file. 16 func TestRandom(t *testing.T) { 17 t.Parallel() 18 19 // Some random tests to verify Open(Seal) == Plaintext 20 for i := 0; i < 256; i++ { 21 var nonce [24]byte 22 var key [32]byte 23 24 al := mr.Intn(128) 25 pl := mr.Intn(16384) 26 ad := make([]byte, al) 27 plaintext := make([]byte, pl) 28 cr.Read(key[:]) 29 cr.Read(nonce[:]) 30 cr.Read(ad) 31 cr.Read(plaintext) 32 33 aead, err := New(key[:]) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 ct := aead.Seal(nil, nonce[:], plaintext, ad) 39 40 plaintext2, err := aead.Open(nil, nonce[:], ct, ad) 41 if err != nil { 42 t.Errorf("Random #%d: Open failed", i) 43 continue 44 } 45 46 if !bytes.Equal(plaintext, plaintext2) { 47 t.Errorf("Random #%d: plaintext's don't match: got %x vs %x", i, plaintext2, plaintext) 48 continue 49 } 50 51 if len(ad) > 0 { 52 alterAdIdx := mr.Intn(len(ad)) 53 ad[alterAdIdx] ^= 0x80 54 if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil { 55 t.Errorf("Random #%d: Open was successful after altering additional data", i) 56 } 57 ad[alterAdIdx] ^= 0x80 58 } 59 60 alterNonceIdx := mr.Intn(aead.NonceSize()) 61 nonce[alterNonceIdx] ^= 0x80 62 if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil { 63 t.Errorf("Random #%d: Open was successful after altering nonce", i) 64 } 65 nonce[alterNonceIdx] ^= 0x80 66 67 alterCtIdx := mr.Intn(len(ct)) 68 ct[alterCtIdx] ^= 0x80 69 if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil { 70 t.Errorf("Random #%d: Open was successful after altering ciphertext", i) 71 } 72 ct[alterCtIdx] ^= 0x80 73 } 74 } 75 76 // AFOREMENTIONED LICENCE 77 // Copyright (c) 2009 The Go Authors. All rights reserved. 78 // 79 // Redistribution and use in source and binary forms, with or without 80 // modification, are permitted provided that the following conditions are 81 // met: 82 // 83 // * Redistributions of source code must retain the above copyright 84 // notice, this list of conditions and the following disclaimer. 85 // * Redistributions in binary form must reproduce the above 86 // copyright notice, this list of conditions and the following disclaimer 87 // in the documentation and/or other materials provided with the 88 // distribution. 89 // * Neither the name of Google Inc. nor the names of its 90 // contributors may be used to endorse or promote products derived from 91 // this software without specific prior written permission. 92 // 93 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 94 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 95 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 96 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 97 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 98 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 99 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 100 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 101 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 102 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 103 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.