Decrypt decrypts an encrypted element with key. If the ciphertext contains an EncryptedKey element, then the type of `key` is determined by the registered Decryptor for the EncryptedKey element. Otherwise, `key` must be a []byte of length KeySize().
(key interface{}, ciphertextEl *etree.Element)
| 82 | // Decryptor for the EncryptedKey element. Otherwise, `key` must be a []byte of |
| 83 | // length KeySize(). |
| 84 | func (e CBC) Decrypt(key interface{}, ciphertextEl *etree.Element) ([]byte, error) { |
| 85 | // If the key is encrypted, decrypt it. |
| 86 | if encryptedKeyEl := ciphertextEl.FindElement("./KeyInfo/EncryptedKey"); encryptedKeyEl != nil { |
| 87 | var err error |
| 88 | key, err = Decrypt(key, encryptedKeyEl) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | keyBuf, ok := key.([]byte) |
| 95 | if !ok { |
| 96 | return nil, ErrIncorrectKeyType("[]byte") |
| 97 | } |
| 98 | if len(keyBuf) != e.KeySize() { |
| 99 | return nil, ErrIncorrectKeyLength(e.KeySize()) |
| 100 | } |
| 101 | |
| 102 | block, err := e.cipher(keyBuf) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | |
| 107 | ciphertext, err := getCiphertext(ciphertextEl) |
| 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | |
| 112 | if len(ciphertext) < block.BlockSize() { |
| 113 | return nil, errors.New("ciphertext too short") |
| 114 | } |
| 115 | |
| 116 | iv := ciphertext[:aes.BlockSize] |
| 117 | ciphertext = ciphertext[aes.BlockSize:] |
| 118 | |
| 119 | mode := cipher.NewCBCDecrypter(block, iv) |
| 120 | plaintext := make([]byte, len(ciphertext)) |
| 121 | mode.CryptBlocks(plaintext, ciphertext) // decrypt in place |
| 122 | |
| 123 | plaintext, err = stripPadding(plaintext) |
| 124 | if err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | |
| 128 | return plaintext, nil |
| 129 | } |
| 130 | |
| 131 | var ( |
| 132 | // AES128CBC implements AES128-CBC symetric key mode for encryption and decryption |
nothing calls this directly
no test coverage detected