storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/config/crypto_test.go (about) 1 // MinIO Cloud Storage, (C) 2021 MinIO, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package config 16 17 import ( 18 "bytes" 19 "encoding/hex" 20 "io" 21 "io/ioutil" 22 "testing" 23 24 "storj.io/minio/pkg/kms" 25 ) 26 27 var encryptDecryptTests = []struct { 28 Data []byte 29 Context kms.Context 30 }{ 31 { 32 Data: nil, 33 Context: nil, 34 }, 35 { 36 Data: []byte{1}, 37 Context: nil, 38 }, 39 { 40 Data: []byte{1}, 41 Context: kms.Context{"key": "value"}, 42 }, 43 { 44 Data: make([]byte, 1<<20), 45 Context: kms.Context{"key": "value", "a": "b"}, 46 }, 47 } 48 49 func TestEncryptDecrypt(t *testing.T) { 50 key, err := hex.DecodeString("ddedadb867afa3f73bd33c25499a723ed7f9f51172ee7b1b679e08dc795debcc") 51 if err != nil { 52 t.Fatalf("Failed to decode master key: %v", err) 53 } 54 KMS, err := kms.New("my-key", key) 55 if err != nil { 56 t.Fatalf("Failed to create KMS: %v", err) 57 } 58 59 for i, test := range encryptDecryptTests { 60 ciphertext, err := Encrypt(KMS, bytes.NewReader(test.Data), test.Context) 61 if err != nil { 62 t.Fatalf("Test %d: failed to encrypt stream: %v", i, err) 63 } 64 data, err := ioutil.ReadAll(ciphertext) 65 if err != nil { 66 t.Fatalf("Test %d: failed to encrypt stream: %v", i, err) 67 } 68 69 plaintext, err := Decrypt(KMS, bytes.NewReader(data), test.Context) 70 if err != nil { 71 t.Fatalf("Test %d: failed to decrypt stream: %v", i, err) 72 } 73 data, err = ioutil.ReadAll(plaintext) 74 if err != nil { 75 t.Fatalf("Test %d: failed to decrypt stream: %v", i, err) 76 } 77 78 if !bytes.Equal(data, test.Data) { 79 t.Fatalf("Test %d: decrypted data does not match original data", i) 80 } 81 } 82 } 83 84 func BenchmarkEncrypt(b *testing.B) { 85 key, err := hex.DecodeString("ddedadb867afa3f73bd33c25499a723ed7f9f51172ee7b1b679e08dc795debcc") 86 if err != nil { 87 b.Fatalf("Failed to decode master key: %v", err) 88 } 89 KMS, err := kms.New("my-key", key) 90 if err != nil { 91 b.Fatalf("Failed to create KMS: %v", err) 92 } 93 94 benchmarkEncrypt := func(size int, b *testing.B) { 95 var ( 96 data = make([]byte, size) 97 plaintext = bytes.NewReader(data) 98 context = kms.Context{"key": "value"} 99 ) 100 b.SetBytes(int64(size)) 101 for i := 0; i < b.N; i++ { 102 ciphertext, err := Encrypt(KMS, plaintext, context) 103 if err != nil { 104 b.Fatal(err) 105 } 106 if _, err = io.Copy(ioutil.Discard, ciphertext); err != nil { 107 b.Fatal(err) 108 } 109 plaintext.Reset(data) 110 } 111 } 112 b.Run("1KB", func(b *testing.B) { benchmarkEncrypt(1*1024, b) }) 113 b.Run("512KB", func(b *testing.B) { benchmarkEncrypt(512*1024, b) }) 114 b.Run("1MB", func(b *testing.B) { benchmarkEncrypt(1024*1024, b) }) 115 b.Run("10MB", func(b *testing.B) { benchmarkEncrypt(10*1024*1024, b) }) 116 }