github.com/trustbloc/kms-go@v1.1.2/kms/localkms/crypto_box_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package localkms 8 9 import ( 10 "crypto/rand" 11 "testing" 12 13 "github.com/btcsuite/btcutil/base58" 14 "github.com/stretchr/testify/require" 15 16 "github.com/trustbloc/kms-go/secretlock/noop" 17 "github.com/trustbloc/kms-go/util/cryptoutil" 18 19 "github.com/trustbloc/kms-go/spi/kms" 20 "github.com/trustbloc/kms-go/spi/secretlock" 21 ) 22 23 type testProvider struct { 24 storeProvider kms.Store 25 secretLockProvider secretlock.Service 26 } 27 28 func (p *testProvider) StorageProvider() kms.Store { 29 return p.storeProvider 30 } 31 32 func (p *testProvider) SecretLock() secretlock.Service { 33 return p.secretLockProvider 34 } 35 36 func newKMS(t *testing.T) *LocalKMS { 37 testStore := newInMemoryKMSStore() 38 p := testProvider{ 39 storeProvider: testStore, 40 secretLockProvider: &noop.NoLock{}, 41 } 42 43 mainLockURI := "local-lock://test/uri/" 44 ret, err := New(mainLockURI, &p) 45 require.NoError(t, err) 46 47 return ret 48 } 49 50 func TestNewCryptoBox(t *testing.T) { 51 k := newKMS(t) 52 b, err := NewCryptoBox(k) 53 require.NoError(t, err) 54 require.Equal(t, b.km, k) 55 56 _, err = NewCryptoBox(kms.KeyManager(nil)) 57 require.EqualError(t, err, "cannot use parameter argument as KMS") 58 } 59 60 func TestBoxSeal(t *testing.T) { 61 k := newKMS(t) 62 _, rec1PubKey, err := k.CreateAndExportPubKeyBytes(kms.ED25519) 63 require.NoError(t, err) 64 65 rec1EncPubKey, err := cryptoutil.PublicEd25519toCurve25519(rec1PubKey) 66 require.NoError(t, err) 67 68 b, err := NewCryptoBox(k) 69 require.NoError(t, err) 70 71 t.Run("Seal a message with sodiumBoxSeal and unseal it with sodiumBoxSealOpen", func(t *testing.T) { 72 msg := []byte("lorem ipsum dolor sit amet consectetur adipiscing elit ") 73 74 enc, err := b.Seal(msg, rec1EncPubKey, rand.Reader) 75 require.NoError(t, err) 76 dec, err := b.SealOpen(enc, rec1PubKey) 77 require.NoError(t, err) 78 79 require.Equal(t, msg, dec) 80 }) 81 82 t.Run("Failed decrypt, key missing from KMS", func(t *testing.T) { 83 msg := []byte("pretend this is an encrypted message") 84 85 _, err := b.SealOpen(msg, base58.Decode("BADKEY23452345234523452345")) 86 require.NotNil(t, err) 87 require.Contains(t, err.Error(), "sealOpen: failed to exportPriveKeyBytes: getKeySet: "+ 88 "failed to read json keyset from reader") 89 }) 90 91 t.Run("Failed decrypt, short message", func(t *testing.T) { 92 enc := []byte("Bad message") 93 94 _, err := b.SealOpen(enc, rec1PubKey) 95 require.EqualError(t, err, "message too short") 96 }) 97 98 t.Run("Failed decrypt, garbled message", func(t *testing.T) { 99 msg := []byte("lorem ipsum dolor sit amet consectetur adipiscing elit ") 100 101 enc, err := b.Seal(msg, rec1EncPubKey, rand.Reader) 102 require.NoError(t, err) 103 104 enc[0]++ // garbling 105 106 _, err = b.SealOpen(enc, rec1PubKey) 107 require.EqualError(t, err, "failed to unpack") 108 }) 109 } 110 111 /* Cannot convert X25519 keys to ED25519 keys, this test assumes fixed X25519 keys values. The KMS cannot store 112 encryption X25519 keys. The new KMS supports storing only ED25519 keys. For the sake of LegacyPacker, 113 Crypto_Box.go converts from Ed25519 to X25519 only. 114 115 func TestBoxEasy(t *testing.T) { 116 k, _ := newKMS(t) 117 recipient1KID, _, err := k.Create(kms.ED25519) 118 require.NoError(t, err) 119 120 rec1PubKey, err := k.ExportPubKeyBytes(recipient1KID) 121 require.NoError(t, err) 122 123 rec1EncPubKey, err := cryptoutil.PublicEd25519toCurve25519(rec1PubKey) 124 require.NoError(t, err) 125 126 nonce := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23} 127 128 kp1 := cryptoutil.KeyPair{ 129 Priv: base58.Decode("4BsY8pbXj2fjSnAafAvBL2qChnePw5cZML9qjQgAJrUd"), 130 Pub: base58.Decode("7cWi6z8efvAHwjNzkdjZe8huoJtqpy6zihsKANJmcAnD"), 131 } 132 kp1Combo := &cryptoutil.MessagingKeys{ 133 EncKeyPair: &cryptoutil.EncKeyPair{ 134 KeyPair: kp1, 135 Alg: cryptoutil.Curve25519, 136 }, 137 } 138 kp2 := cryptoutil.KeyPair{ 139 Priv: base58.Decode("2U3zcoveWe1BAGem9ije1WwRvDguTPyXCvJRytWcEnS7"), 140 Pub: base58.Decode("7usXitPNvWFEyfH3xNvqxtmn6xwt8jggPVTZ56qxM2G8"), 141 } 142 kp2Combo := &cryptoutil.MessagingKeys{ 143 EncKeyPair: &cryptoutil.EncKeyPair{ 144 KeyPair: kp2, 145 Alg: cryptoutil.Curve25519, 146 }, 147 } 148 149 err = kms.persist(w.keystore, base58.Encode(kp1.Pub), kp1Combo) 150 require.NoError(t, err) 151 err = kms.persist(w.keystore, base58.Encode(kp2.Pub), kp2Combo) 152 require.NoError(t, err) 153 154 b, err := NewCryptoBox(w) 155 require.NoError(t, err) 156 157 t.Run("Failed encrypt, key missing from KMS", func(t *testing.T) { 158 msg := []byte("pretend this is an encrypted message") 159 160 _, err := b.Easy(msg, nonce, base58.Decode("BADKEY1"), base58.Decode("BADKEY2")) 161 require.NotNil(t, err) 162 require.EqualError(t, err, "key not found") 163 }) 164 165 t.Run("Failed decrypt, key missing from KMS", func(t *testing.T) { 166 msg := []byte("pretend this is an encrypted message") 167 168 _, err := b.EasyOpen(msg, nonce, base58.Decode("BADKEY1"), base58.Decode("BADKEY2")) 169 require.NotNil(t, err) 170 require.EqualError(t, err, "key not found") 171 }) 172 173 t.Run("Failed decrypt, garbled message", func(t *testing.T) { 174 msg := []byte("lorem ipsum dolor sit amet consectetur adipiscing elit ") 175 176 enc, err := b.Easy(msg, nonce, kp1.Pub, kp2.Pub) 177 require.NoError(t, err) 178 179 enc[0]++ // garbling 180 181 _, err = b.EasyOpen(enc, nonce, kp2.Pub, kp1.Pub) 182 require.EqualError(t, err, "failed to unpack") 183 }) 184 185 t.Run("success: Easy encrypt and compare against const", func(t *testing.T) { 186 nonce := []byte("abcdefghijklmnopqrstuvwx") 187 payload := []byte("hjlp! my angry fez vows quit xkcd") 188 189 enc, err := b.Easy(payload, nonce, kp1.Pub, kp2.Pub) 190 require.NoError(t, err) 191 192 correct := base58.Decode("GpYpRShjVgLjs9e5mXm85GaQpVqsqmTiaJgsvWCNUsfDQU7fWR89kf6CPfFpPtWGJUR") 193 require.ElementsMatch(t, correct, enc) 194 }) 195 196 t.Run("success: Easy decrypt and compare against const", func(t *testing.T) { 197 nonce := []byte("abcdefghijklmnopqrstuvwx") 198 payload := base58.Decode("GpYpRShjVgLjs9e5mXm85GaQpVqsqmTiaJgsvWCNUsfDQU7fWR89kf6CPfFpPtWGJUR") 199 200 dec, err := b.EasyOpen(payload, nonce, kp2.Pub, kp1.Pub) 201 require.NoError(t, err) 202 203 correct := []byte("hjlp! my angry fez vows quit xkcd") 204 require.ElementsMatch(t, correct, dec) 205 }) 206 } 207 */