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