1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 var sha1WithTripleDES = asn1.ObjectIdentifier([]int{1, 2, 840, 113549, 1, 12, 1, 3})
16 func TestPbDecrypterFor(t *testing.T) {
17 params, _ := asn1.Marshal(pbeParams{
18 Salt: []byte{1, 2, 3, 4, 5, 6, 7, 8},
21 alg := pkix.AlgorithmIdentifier{
22 Algorithm: asn1.ObjectIdentifier([]int{1, 2, 3}),
23 Parameters: asn1.RawValue{
28 pass, _ := bmpString("Sesame open")
30 _, _, err := pbDecrypterFor(alg, pass)
31 if _, ok := err.(NotImplementedError); !ok {
32 t.Errorf("expected not implemented error, got: %T %s", err, err)
35 alg.Algorithm = sha1WithTripleDES
36 cbc, blockSize, err := pbDecrypterFor(alg, pass)
38 t.Errorf("unexpected error from pbDecrypterFor %v", err)
41 t.Errorf("unexpected block size %d, wanted 8", blockSize)
44 plaintext := []byte{1, 2, 3, 4, 5, 6, 7, 8}
45 expectedCiphertext := []byte{185, 73, 135, 249, 137, 1, 122, 247}
46 ciphertext := make([]byte, len(plaintext))
47 cbc.CryptBlocks(ciphertext, plaintext)
49 if bytes.Compare(ciphertext, expectedCiphertext) != 0 {
50 t.Errorf("bad ciphertext, got %x but wanted %x", ciphertext, expectedCiphertext)
54 var pbDecryptTests = []struct {
60 []byte("\x33\x73\xf3\x9f\xda\x49\xae\xfc\xa0\x9a\xdf\x5a\x58\xa0\xea\x46"), // 7 padding bytes
65 []byte("\x33\x73\xf3\x9f\xda\x49\xae\xfc\x96\x24\x2f\x71\x7e\x32\x3f\xe7"), // 8 padding bytes
70 []byte("\x35\x0c\xc0\x8d\xab\xa9\x5d\x30\x7f\x9a\xec\x6a\xd8\x9b\x9c\xd9"), // 9 padding bytes, incorrect
75 []byte("\xb2\xf9\x6e\x06\x60\xae\x20\xcf\x08\xa0\x7b\xd9\x6b\x20\xef\x41"), // incorrect padding bytes: [ ... 0x04 0x02 ]
81 func TestPbDecrypt(t *testing.T) {
82 for i, test := range pbDecryptTests {
83 decryptable := testDecryptable{
85 algorithm: pkix.AlgorithmIdentifier{
86 Algorithm: sha1WithTripleDES,
87 Parameters: pbeParams{
88 Salt: []byte("\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8"),
93 password, _ := bmpString("sesame")
95 plaintext, err := pbDecrypt(decryptable, password)
96 if err != test.expectedError {
97 t.Errorf("#%d: got error %q, but wanted %q", i, err, test.expectedError)
101 if !bytes.Equal(plaintext, test.expected) {
102 t.Errorf("#%d: got %x, but wanted %x", i, plaintext, test.expected)
107 type testDecryptable struct {
109 algorithm pkix.AlgorithmIdentifier
112 func (d testDecryptable) Algorithm() pkix.AlgorithmIdentifier { return d.algorithm }
113 func (d testDecryptable) Data() []byte { return d.data }
115 func (params pbeParams) RawASN1() (raw asn1.RawValue) {
116 asn1Bytes, err := asn1.Marshal(params)
120 _, err = asn1.Unmarshal(asn1Bytes, &raw)