github.com/readium/readium-lcp-server@v0.0.0-20240509124024-799e77a0bbd6/crypto/aes_cbc_test.go (about) 1 // Copyright (c) 2016 Readium Foundation 2 // 3 // Redistribution and use in source and binary forms, with or without modification, 4 // are permitted provided that the following conditions are met: 5 // 6 // 1. Redistributions of source code must retain the above copyright notice, this 7 // list of conditions and the following disclaimer. 8 // 2. Redistributions in binary form must reproduce the above copyright notice, 9 // this list of conditions and the following disclaimer in the documentation and/or 10 // other materials provided with the distribution. 11 // 3. Neither the name of the organization nor the names of its contributors may be 12 // used to endorse or promote products derived from this software without specific 13 // prior written permission 14 // 15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 26 package crypto 27 28 import ( 29 "bytes" 30 "crypto/aes" 31 "crypto/sha256" 32 "testing" 33 ) 34 35 func TestSimpleEncrypt(t *testing.T) { 36 input := bytes.NewBufferString("1234") 37 var output bytes.Buffer 38 var key [32]byte //not a safe key to have 39 40 cbc := NewAESCBCEncrypter() 41 42 err := cbc.Encrypt(key[:], input, &output) 43 44 if err != nil { 45 t.Log(err) 46 t.FailNow() 47 } 48 49 bytes := output.Bytes() 50 51 if len(bytes) != aes.BlockSize*2 { 52 t.Errorf("Expected %d bytes, got %d", aes.BlockSize*2, len(bytes)) 53 } 54 } 55 56 func TestConsecutiveEncrypts(t *testing.T) { 57 input := bytes.NewBufferString("1234") 58 var output bytes.Buffer 59 var key [32]byte //not a safe key to have 60 61 cbc := NewAESCBCEncrypter() 62 63 err := cbc.Encrypt(key[:], input, &output) 64 65 if err != nil { 66 t.Log(err) 67 t.FailNow() 68 } 69 70 input = bytes.NewBufferString("1234") 71 var output2 bytes.Buffer 72 73 err = cbc.Encrypt(key[:], input, &output2) 74 75 if err != nil { 76 t.Log(err) 77 t.FailNow() 78 } 79 80 if bytes.Equal(output.Bytes(), output2.Bytes()) { 81 t.Error("2 calls with the same key should still result in different encryptions") 82 } 83 } 84 85 func TestFailingReaderForEncryption(t *testing.T) { 86 var output bytes.Buffer 87 var key [32]byte //not a safe key to have 88 89 cbc := NewAESCBCEncrypter() 90 91 err := cbc.Encrypt(key[:], failingReader{}, &output) 92 93 if err == nil { 94 t.Error("expected an error from the reader") 95 } 96 } 97 98 func TestDecrypt(t *testing.T) { 99 clear := bytes.NewBufferString("cleartext") 100 key := sha256.Sum256([]byte("password")) 101 var cipher bytes.Buffer 102 103 cbc := &cbcEncrypter{} 104 err := cbc.Encrypt(key[:], clear, &cipher) 105 if err != nil { 106 t.Fatal(err) 107 } 108 109 var res bytes.Buffer 110 err = cbc.Decrypt(key[:], &cipher, &res) 111 if err != nil { 112 t.Fatal(err) 113 } 114 115 if str := res.String(); str != "cleartext" { 116 t.Errorf("Expected 'cleartext', got %s\n", str) 117 } 118 } 119 120 func TestKeyWrap(t *testing.T) { 121 key := []byte{0x00, 0x01, 0x02, 0x03, 122 0x04, 0x05, 0x06, 0x07, 123 0x08, 0x09, 0x0A, 0x0B, 124 0x0C, 0x0D, 0x0E, 0x0F} 125 plain := []byte{0x00, 0x11, 0x22, 0x33, 126 0x44, 0x55, 0x66, 0x77, 127 0x88, 0x99, 0xAA, 0xBB, 128 0xCC, 0xDD, 0xEE, 0xFF} 129 expected := []byte{0x1F, 0xA6, 0x8B, 0x0A, 130 0x81, 0x12, 0xB4, 0x47, 131 0xAE, 0xF3, 0x4B, 0xD8, 132 0xFB, 0x5A, 0x7B, 0x82, 133 0x9D, 0x3E, 0x86, 0x23, 134 0x71, 0xD2, 0xCF, 0xE5} 135 136 out := KeyWrap(key, plain) 137 if !bytes.Equal(out, expected) { 138 t.Errorf("Expected %x, got %x", expected, out) 139 } 140 }