| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
import os |
| 26 |
|
| 27 |
from Crypto.Cipher._mode_ecb import _create_ecb_cipher |
| 28 |
from Crypto.Cipher._mode_cbc import _create_cbc_cipher |
| 29 |
from Crypto.Cipher._mode_cfb import _create_cfb_cipher |
| 30 |
from Crypto.Cipher._mode_ofb import _create_ofb_cipher |
| 31 |
from Crypto.Cipher._mode_ctr import _create_ctr_cipher |
| 32 |
from Crypto.Cipher._mode_openpgp import _create_openpgp_cipher |
| 33 |
from Crypto.Cipher._mode_ccm import _create_ccm_cipher |
| 34 |
from Crypto.Cipher._mode_eax import _create_eax_cipher |
| 35 |
from Crypto.Cipher._mode_siv import _create_siv_cipher |
| 36 |
from Crypto.Cipher._mode_gcm import _create_gcm_cipher |
| 37 |
from Crypto.Cipher._mode_ocb import _create_ocb_cipher |
| 38 |
|
| 39 |
_modes = { 1:_create_ecb_cipher, |
| 40 |
2:_create_cbc_cipher, |
| 41 |
3:_create_cfb_cipher, |
| 42 |
5:_create_ofb_cipher, |
| 43 |
6:_create_ctr_cipher, |
| 44 |
7:_create_openpgp_cipher, |
| 45 |
9:_create_eax_cipher |
| 46 |
} |
| 47 |
|
| 48 |
_extra_modes = { 8:_create_ccm_cipher, |
| 49 |
10:_create_siv_cipher, |
| 50 |
11:_create_gcm_cipher, |
| 51 |
12:_create_ocb_cipher |
| 52 |
} |
| 53 |
|
| 54 |
def _create_cipher(factory, key, mode, *args, **kwargs): |
| 55 |
|
| 56 |
kwargs["key"] = key |
| 57 |
|
| 58 |
modes = dict(_modes) |
| 59 |
if kwargs.pop("add_aes_modes", False): |
| 60 |
modes.update(_extra_modes) |
| 61 |
if not mode in modes: |
| 62 |
raise ValueError("Mode not supported") |
| 63 |
|
| 64 |
if args: |
| 65 |
if mode in (8, 9, 10, 11, 12): |
| 66 |
if len(args) > 1: |
| 67 |
raise TypeError("Too many arguments for this mode") |
| 68 |
kwargs["nonce"] = args[0] |
| 69 |
elif mode in (2, 3, 5, 7): |
| 70 |
if len(args) > 1: |
| 71 |
raise TypeError("Too many arguments for this mode") |
| 72 |
kwargs["IV"] = args[0] |
| 73 |
elif mode == 6: |
| 74 |
if len(args) > 0: |
| 75 |
raise TypeError("Too many arguments for this mode") |
| 76 |
elif mode == 1: |
| 77 |
raise TypeError("IV is not meaningful for the ECB mode") |
| 78 |
|
| 79 |
return modes[mode](factory, **kwargs) |