| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package jose |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"crypto/cipher" |
| 22 |
"crypto/rand" |
| 23 |
"io" |
| 24 |
"testing" |
| 25 |
) |
| 26 |
|
| 27 |
func TestInvalidSymmetricAlgorithms(t *testing.T) { |
| 28 |
_, err := newSymmetricRecipient("XYZ", []byte{}) |
| 29 |
if err != ErrUnsupportedAlgorithm { |
| 30 |
t.Error("should not accept invalid algorithm") |
| 31 |
} |
| 32 |
|
| 33 |
enc := &symmetricKeyCipher{} |
| 34 |
_, err = enc.encryptKey([]byte{}, "XYZ") |
| 35 |
if err != ErrUnsupportedAlgorithm { |
| 36 |
t.Error("should not accept invalid algorithm") |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func TestAeadErrors(t *testing.T) { |
| 41 |
aead := &aeadContentCipher{ |
| 42 |
keyBytes: 16, |
| 43 |
authtagBytes: 16, |
| 44 |
getAead: func(key []byte) (cipher.AEAD, error) { |
| 45 |
return nil, ErrCryptoFailure |
| 46 |
}, |
| 47 |
} |
| 48 |
|
| 49 |
parts, err := aead.encrypt([]byte{}, []byte{}, []byte{}) |
| 50 |
if err != ErrCryptoFailure { |
| 51 |
t.Error("should handle aead failure") |
| 52 |
} |
| 53 |
|
| 54 |
_, err = aead.decrypt([]byte{}, []byte{}, parts) |
| 55 |
if err != ErrCryptoFailure { |
| 56 |
t.Error("should handle aead failure") |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func TestInvalidKey(t *testing.T) { |
| 61 |
gcm := newAESGCM(16).(*aeadContentCipher) |
| 62 |
_, err := gcm.getAead([]byte{}) |
| 63 |
if err == nil { |
| 64 |
t.Error("should not accept invalid key") |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func TestStaticKeyGen(t *testing.T) { |
| 69 |
key := make([]byte, 32) |
| 70 |
io.ReadFull(rand.Reader, key) |
| 71 |
|
| 72 |
gen := &staticKeyGenerator{key: key} |
| 73 |
if gen.keySize() != len(key) { |
| 74 |
t.Error("static key generator reports incorrect size") |
| 75 |
} |
| 76 |
|
| 77 |
generated, _, err := gen.genKey() |
| 78 |
if err != nil { |
| 79 |
t.Error("static key generator should always succeed", err) |
| 80 |
} |
| 81 |
if !bytes.Equal(generated, key) { |
| 82 |
t.Error("static key generator returns different data") |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func TestAeadInvalidInput(t *testing.T) { |
| 87 |
aead := newAESGCM(16).(*aeadContentCipher) |
| 88 |
key := []byte("1234567890123456") |
| 89 |
_, err := aead.decrypt(key, []byte{}, &aeadParts{[]byte{}, []byte{}, []byte{}}) |
| 90 |
if err != ErrCryptoFailure { |
| 91 |
t.Error("should handle aead failure") |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func TestVectorsAESGCM(t *testing.T) { |
| 96 |
|
| 97 |
plaintext := []byte{ |
| 98 |
84, 104, 101, 32, 116, 114, 117, 101, 32, 115, 105, 103, 110, 32, |
| 99 |
111, 102, 32, 105, 110, 116, 101, 108, 108, 105, 103, 101, 110, 99, |
| 100 |
101, 32, 105, 115, 32, 110, 111, 116, 32, 107, 110, 111, 119, 108, |
| 101 |
101, 100, 103, 101, 32, 98, 117, 116, 32, 105, 109, 97, 103, 105, |
| 102 |
110, 97, 116, 105, 111, 110, 46} |
| 103 |
|
| 104 |
aad := []byte{ |
| 105 |
101, 121, 74, 104, 98, 71, 99, 105, 79, 105, 74, 83, 85, 48, 69, |
| 106 |
116, 84, 48, 70, 70, 85, 67, 73, 115, 73, 109, 86, 117, 89, 121, 73, |
| 107 |
54, 73, 107, 69, 121, 78, 84, 90, 72, 81, 48, 48, 105, 102, 81} |
| 108 |
|
| 109 |
expectedCiphertext := []byte{ |
| 110 |
229, 236, 166, 241, 53, 191, 115, 196, 174, 43, 73, 109, 39, 122, |
| 111 |
233, 96, 140, 206, 120, 52, 51, 237, 48, 11, 190, 219, 186, 80, 111, |
| 112 |
104, 50, 142, 47, 167, 59, 61, 181, 127, 196, 21, 40, 82, 242, 32, |
| 113 |
123, 143, 168, 226, 73, 216, 176, 144, 138, 247, 106, 60, 16, 205, |
| 114 |
160, 109, 64, 63, 192} |
| 115 |
|
| 116 |
expectedAuthtag := []byte{ |
| 117 |
92, 80, 104, 49, 133, 25, 161, 215, 173, 101, 219, 211, 136, 91, 210, 145} |
| 118 |
|
| 119 |
|
| 120 |
randReader = bytes.NewReader([]byte{ |
| 121 |
177, 161, 244, 128, 84, 143, 225, 115, 63, 180, 3, 255, 107, 154, |
| 122 |
212, 246, 138, 7, 110, 91, 112, 46, 34, 105, 47, 130, 203, 46, 122, |
| 123 |
234, 64, 252, 227, 197, 117, 252, 2, 219, 233, 68, 180, 225, 77, 219}) |
| 124 |
defer resetRandReader() |
| 125 |
|
| 126 |
enc := newAESGCM(32) |
| 127 |
key, _, _ := randomKeyGenerator{size: 32}.genKey() |
| 128 |
out, err := enc.encrypt(key, aad, plaintext) |
| 129 |
if err != nil { |
| 130 |
t.Error("Unable to encrypt:", err) |
| 131 |
return |
| 132 |
} |
| 133 |
|
| 134 |
if bytes.Compare(out.ciphertext, expectedCiphertext) != 0 { |
| 135 |
t.Error("Ciphertext did not match") |
| 136 |
} |
| 137 |
if bytes.Compare(out.tag, expectedAuthtag) != 0 { |
| 138 |
t.Error("Auth tag did not match") |
| 139 |
} |
| 140 |
} |