Decrypt data with ECIES method using the local private key
(self, data, ciphername='aes-256-cbc')
| 485 | return ciphertext + mac |
| 486 | |
| 487 | def decrypt(self, data, ciphername='aes-256-cbc'): |
| 488 | """ |
| 489 | Decrypt data with ECIES method using the local private key |
| 490 | """ |
| 491 | # pylint: disable=too-many-locals |
| 492 | blocksize = OpenSSL.get_cipher(ciphername).get_blocksize() |
| 493 | iv = data[:blocksize] |
| 494 | i = blocksize |
| 495 | _, pubkey_x, pubkey_y, i2 = ECC._decode_pubkey(data[i:]) |
| 496 | i += i2 |
| 497 | ciphertext = data[i:len(data) - 32] |
| 498 | i += len(ciphertext) |
| 499 | mac = data[i:] |
| 500 | key = sha512(self.raw_get_ecdh_key(pubkey_x, pubkey_y)).digest() |
| 501 | key_e, key_m = key[:32], key[32:] |
| 502 | if not equals(hmac_sha256(key_m, data[:len(data) - 32]), mac): |
| 503 | raise RuntimeError("Fail to verify data") |
| 504 | ctx = Cipher(key_e, iv, 0, ciphername) |
| 505 | return ctx.ciphering(ciphertext) |