storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/kms/single-key_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 kms
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/base64"
    20  	"testing"
    21  )
    22  
    23  func TestSingleKeyRoundtrip(t *testing.T) {
    24  	KMS, err := Parse("my-key:eEm+JI9/q4JhH8QwKvf3LKo4DEBl6QbfvAl1CAbMIv8=")
    25  	if err != nil {
    26  		t.Fatalf("Failed to initialize KMS: %v", err)
    27  	}
    28  
    29  	key, err := KMS.GenerateKey("my-key", Context{})
    30  	if err != nil {
    31  		t.Fatalf("Failed to generate key: %v", err)
    32  	}
    33  	plaintext, err := KMS.DecryptKey(key.KeyID, key.Ciphertext, Context{})
    34  	if err != nil {
    35  		t.Fatalf("Failed to decrypt key: %v", err)
    36  	}
    37  	if !bytes.Equal(key.Plaintext, plaintext) {
    38  		t.Fatalf("Decrypted key does not match generated one: got %x - want %x", key.Plaintext, plaintext)
    39  	}
    40  }
    41  
    42  func TestDecryptKey(t *testing.T) {
    43  	KMS, err := Parse("my-key:eEm+JI9/q4JhH8QwKvf3LKo4DEBl6QbfvAl1CAbMIv8=")
    44  	if err != nil {
    45  		t.Fatalf("Failed to initialize KMS: %v", err)
    46  	}
    47  
    48  	for i, test := range decryptKeyTests {
    49  		dataKey, err := base64.StdEncoding.DecodeString(test.Plaintext)
    50  		if err != nil {
    51  			t.Fatalf("Test %d: failed to decode plaintext key: %v", i, err)
    52  		}
    53  		ciphertext, err := base64.StdEncoding.DecodeString(test.Ciphertext)
    54  		if err != nil {
    55  			t.Fatalf("Test %d: failed to decode ciphertext key: %v", i, err)
    56  		}
    57  		plaintext, err := KMS.DecryptKey(test.KeyID, ciphertext, test.Context)
    58  		if err != nil {
    59  			t.Fatalf("Test %d: failed to decrypt key: %v", i, err)
    60  		}
    61  		if !bytes.Equal(plaintext, dataKey) {
    62  			t.Fatalf("Test %d: decrypted key does not generated one: got %x - want %x", i, plaintext, dataKey)
    63  		}
    64  	}
    65  }
    66  
    67  var decryptKeyTests = []struct {
    68  	KeyID      string
    69  	Plaintext  string
    70  	Ciphertext string
    71  	Context    Context
    72  }{
    73  	{
    74  		KeyID:      "my-key",
    75  		Plaintext:  "zmS7NrG765UZ0ZN85oPjybelxqVvpz01vxsSpOISy2M=",
    76  		Ciphertext: "eyJhZWFkIjoiQ2hhQ2hhMjBQb2x5MTMwNSIsIml2IjoiSmJJK3Z3dll3dzFsQ2I1VnBrQUZ1UT09Iiwibm9uY2UiOiJBUmpJakp4QlNENTQxR3o4IiwiYnl0ZXMiOiJLQ2JFYzJzQTBUTHZBN2FXVFdhMjNBZGNjVmZKTXBPeHdnRzhobSs0UGFOcnhZZnkxeEZXWmcyZ0VlblZyT2d2In0=",
    77  	},
    78  	{
    79  		KeyID:      "my-key",
    80  		Plaintext:  "UnPWsZgVI+T4L9WGNzFlP1PsP1Z6hn2Fx8ISeZfDGnA=",
    81  		Ciphertext: "eyJhZWFkIjoiQ2hhQ2hhMjBQb2x5MTMwNSIsIml2IjoicjQreWZpVmJWSVlSMFoySTlGcSs2Zz09Iiwibm9uY2UiOiIyWXB3R3dFNTlHY1ZyYUkzIiwiYnl0ZXMiOiJrL3N2TWdsT1U3L0tnd3Y3M2hlRzM4TldXNTc1WExjRnAzU2F4UUhETWpKR1l5UkkzRml5Z3UyT2V1dEdQWE5MIn0=",
    82  		Context:    Context{"key": "value"},
    83  	},
    84  }