|
| 1 | +package encryption_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/des" |
| 6 | + "crypto/rand" |
| 7 | + "io" |
| 8 | + |
| 9 | + "github.com/cloudfoundry-incubator/bbs/encryption" |
| 10 | + "github.com/cloudfoundry-incubator/bbs/encryption/fakes" |
| 11 | + |
| 12 | + . "github.com/onsi/ginkgo" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | +) |
| 15 | + |
| 16 | +var _ = Describe("Crypt", func() { |
| 17 | + var cryptor encryption.Cryptor |
| 18 | + var keyManager encryption.KeyManager |
| 19 | + var prng io.Reader |
| 20 | + |
| 21 | + BeforeEach(func() { |
| 22 | + key, err := encryption.NewKey("label", "pass phrase") |
| 23 | + Expect(err).NotTo(HaveOccurred()) |
| 24 | + keyManager, err = encryption.NewKeyManager(key, nil) |
| 25 | + Expect(err).NotTo(HaveOccurred()) |
| 26 | + prng = rand.Reader |
| 27 | + }) |
| 28 | + |
| 29 | + JustBeforeEach(func() { |
| 30 | + cryptor = encryption.NewCryptor(keyManager, prng) |
| 31 | + }) |
| 32 | + |
| 33 | + It("successfully encrypts and decrypts with a key", func() { |
| 34 | + input := []byte("some plaintext data") |
| 35 | + |
| 36 | + encrypted, err := cryptor.Encrypt(input) |
| 37 | + Expect(err).NotTo(HaveOccurred()) |
| 38 | + Expect(encrypted.CipherText).NotTo(HaveLen(0)) |
| 39 | + Expect(encrypted.CipherText).NotTo(Equal(input)) |
| 40 | + |
| 41 | + plaintext, err := cryptor.Decrypt(encrypted) |
| 42 | + Expect(err).NotTo(HaveOccurred()) |
| 43 | + Expect(plaintext).NotTo(HaveLen(0)) |
| 44 | + Expect(plaintext).To(Equal(input)) |
| 45 | + }) |
| 46 | + |
| 47 | + Context("when the nonce is incorrect", func() { |
| 48 | + It("fails to decrypt", func() { |
| 49 | + input := []byte("some plaintext data") |
| 50 | + |
| 51 | + encrypted, err := cryptor.Encrypt(input) |
| 52 | + Expect(err).NotTo(HaveOccurred()) |
| 53 | + Expect(encrypted.CipherText).NotTo(HaveLen(0)) |
| 54 | + Expect(encrypted.CipherText).NotTo(Equal(input)) |
| 55 | + |
| 56 | + encrypted.Nonce = []byte("123456789012") |
| 57 | + |
| 58 | + _, err = cryptor.Decrypt(encrypted) |
| 59 | + Expect(err).To(HaveOccurred()) |
| 60 | + Expect(err).To(MatchError("cipher: message authentication failed")) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + Context("when the key is not found", func() { |
| 65 | + It("fails to decrypt", func() { |
| 66 | + encrypted := encryption.Encrypted{ |
| 67 | + KeyLabel: "doesnt-exist", |
| 68 | + Nonce: []byte("123456789012"), |
| 69 | + } |
| 70 | + |
| 71 | + _, err := cryptor.Decrypt(encrypted) |
| 72 | + Expect(err).To(HaveOccurred()) |
| 73 | + Expect(err).To(MatchError(`Key with label "doesnt-exist" was not found`)) |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + Context("when the ciphertext is modified", func() { |
| 78 | + It("fails to decrypt", func() { |
| 79 | + input := []byte("some plaintext data") |
| 80 | + |
| 81 | + encrypted, err := cryptor.Encrypt(input) |
| 82 | + Expect(err).NotTo(HaveOccurred()) |
| 83 | + Expect(encrypted.CipherText).NotTo(HaveLen(0)) |
| 84 | + Expect(encrypted.CipherText).NotTo(Equal(input)) |
| 85 | + |
| 86 | + encrypted.CipherText[0] ^= 1 |
| 87 | + |
| 88 | + _, err = cryptor.Decrypt(encrypted) |
| 89 | + Expect(err).To(HaveOccurred()) |
| 90 | + Expect(err).To(MatchError("cipher: message authentication failed")) |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + Context("when the random number generator fails", func() { |
| 95 | + BeforeEach(func() { |
| 96 | + prng = bytes.NewBuffer([]byte{}) |
| 97 | + }) |
| 98 | + |
| 99 | + It("fails to encrypt", func() { |
| 100 | + input := []byte("some plaintext data") |
| 101 | + |
| 102 | + _, err := cryptor.Encrypt(input) |
| 103 | + Expect(err).To(MatchError(`Unable to generate random nonce: "EOF"`)) |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + Context("when the random number generator fails to generate enough data", func() { |
| 108 | + BeforeEach(func() { |
| 109 | + prng = bytes.NewBufferString("goo") |
| 110 | + }) |
| 111 | + |
| 112 | + It("fails to encrypt", func() { |
| 113 | + input := []byte("some plaintext data") |
| 114 | + |
| 115 | + _, err := cryptor.Encrypt(input) |
| 116 | + Expect(err).To(MatchError("Unable to generate random nonce")) |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + Context("when the encryption key is invalid", func() { |
| 121 | + var key *fakes.FakeKey |
| 122 | + |
| 123 | + BeforeEach(func() { |
| 124 | + desCipher, err := des.NewCipher([]byte("12345678")) |
| 125 | + Expect(err).NotTo(HaveOccurred()) |
| 126 | + |
| 127 | + key = &fakes.FakeKey{} |
| 128 | + key.BlockReturns(desCipher) |
| 129 | + keyManager, err = encryption.NewKeyManager(key, nil) |
| 130 | + Expect(err).NotTo(HaveOccurred()) |
| 131 | + }) |
| 132 | + |
| 133 | + It("returns an error", func() { |
| 134 | + input := []byte("some plaintext data") |
| 135 | + |
| 136 | + _, err := cryptor.Encrypt(input) |
| 137 | + Expect(err).To(MatchError(HavePrefix("Unable to create GCM-wrapped cipher:"))) |
| 138 | + }) |
| 139 | + }) |
| 140 | +}) |
0 commit comments