| 1 | from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, c_size_t, |
| 2 | c_uint8_ptr) |
| 3 | |
| 4 | |
| 5 | _raw_pkcs1_decode = load_pycryptodome_raw_lib("Crypto.Cipher._pkcs1_decode", |
| 6 | """ |
| 7 | int pkcs1_decode(const uint8_t *em, size_t len_em, |
| 8 | const uint8_t *sentinel, size_t len_sentinel, |
| 9 | size_t expected_pt_len, |
| 10 | uint8_t *output); |
| 11 | |
| 12 | int oaep_decode(const uint8_t *em, |
| 13 | size_t em_len, |
| 14 | const uint8_t *lHash, |
| 15 | size_t hLen, |
| 16 | const uint8_t *db, |
| 17 | size_t db_len); |
| 18 | """) |
| 19 | |
| 20 | |
| 21 | def pkcs1_decode(em, sentinel, expected_pt_len, output): |
| 22 | if len(em) != len(output): |
| 23 | raise ValueError("Incorrect output length") |
| 24 | |
| 25 | ret = _raw_pkcs1_decode.pkcs1_decode(c_uint8_ptr(em), |
| 26 | c_size_t(len(em)), |
| 27 | c_uint8_ptr(sentinel), |
| 28 | c_size_t(len(sentinel)), |
| 29 | c_size_t(expected_pt_len), |
| 30 | c_uint8_ptr(output)) |
| 31 | return ret |
| 32 | |
| 33 | |
| 34 | def oaep_decode(em, lHash, db): |
| 35 | ret = _raw_pkcs1_decode.oaep_decode(c_uint8_ptr(em), |
| 36 | c_size_t(len(em)), |
| 37 | c_uint8_ptr(lHash), |
| 38 | c_size_t(len(lHash)), |
| 39 | c_uint8_ptr(db), |
| 40 | c_size_t(len(db))) |
| 41 | return ret |