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