| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
""" |
| 23 |
Module's constants for the modes of operation supported with CAST: |
| 24 |
|
| 25 |
:var MODE_ECB: :ref:`Electronic Code Book (ECB) <ecb_mode>` |
| 26 |
:var MODE_CBC: :ref:`Cipher-Block Chaining (CBC) <cbc_mode>` |
| 27 |
:var MODE_CFB: :ref:`Cipher FeedBack (CFB) <cfb_mode>` |
| 28 |
:var MODE_OFB: :ref:`Output FeedBack (OFB) <ofb_mode>` |
| 29 |
:var MODE_CTR: :ref:`CounTer Mode (CTR) <ctr_mode>` |
| 30 |
:var MODE_OPENPGP: :ref:`OpenPGP Mode <openpgp_mode>` |
| 31 |
:var MODE_EAX: :ref:`EAX Mode <eax_mode>` |
| 32 |
""" |
| 33 |
|
| 34 |
import sys |
| 35 |
|
| 36 |
from Crypto.Cipher import _create_cipher |
| 37 |
from Crypto.Util.py3compat import byte_string |
| 38 |
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, |
| 39 |
VoidPointer, SmartPointer, |
| 40 |
c_size_t, c_uint8_ptr) |
| 41 |
|
| 42 |
_raw_cast_lib = load_pycryptodome_raw_lib( |
| 43 |
"Crypto.Cipher._raw_cast", |
| 44 |
""" |
| 45 |
int CAST_start_operation(const uint8_t key[], |
| 46 |
size_t key_len, |
| 47 |
void **pResult); |
| 48 |
int CAST_encrypt(const void *state, |
| 49 |
const uint8_t *in, |
| 50 |
uint8_t *out, |
| 51 |
size_t data_len); |
| 52 |
int CAST_decrypt(const void *state, |
| 53 |
const uint8_t *in, |
| 54 |
uint8_t *out, |
| 55 |
size_t data_len); |
| 56 |
int CAST_stop_operation(void *state); |
| 57 |
""") |
| 58 |
|
| 59 |
|
| 60 |
def _create_base_cipher(dict_parameters): |
| 61 |
"""This method instantiates and returns a handle to a low-level |
| 62 |
base cipher. It will absorb named parameters in the process.""" |
| 63 |
|
| 64 |
try: |
| 65 |
key = dict_parameters.pop("key") |
| 66 |
except KeyError: |
| 67 |
raise TypeError("Missing 'key' parameter") |
| 68 |
|
| 69 |
if len(key) not in key_size: |
| 70 |
raise ValueError("Incorrect CAST key length (%d bytes)" % len(key)) |
| 71 |
|
| 72 |
start_operation = _raw_cast_lib.CAST_start_operation |
| 73 |
stop_operation = _raw_cast_lib.CAST_stop_operation |
| 74 |
|
| 75 |
cipher = VoidPointer() |
| 76 |
result = start_operation(c_uint8_ptr(key), |
| 77 |
c_size_t(len(key)), |
| 78 |
cipher.address_of()) |
| 79 |
if result: |
| 80 |
raise ValueError("Error %X while instantiating the CAST cipher" |
| 81 |
% result) |
| 82 |
|
| 83 |
return SmartPointer(cipher.get(), stop_operation) |
| 84 |
|
| 85 |
|
| 86 |
def new(key, mode, *args, **kwargs): |
| 87 |
"""Create a new CAST cipher |
| 88 |
|
| 89 |
:param key: |
| 90 |
The secret key to use in the symmetric cipher. |
| 91 |
Its length can vary from 5 to 16 bytes. |
| 92 |
:type key: bytes, bytearray, memoryview |
| 93 |
|
| 94 |
:param mode: |
| 95 |
The chaining mode to use for encryption or decryption. |
| 96 |
:type mode: One of the supported ``MODE_*`` constants |
| 97 |
|
| 98 |
:Keyword Arguments: |
| 99 |
* **iv** (*bytes*, *bytearray*, *memoryview*) -- |
| 100 |
(Only applicable for ``MODE_CBC``, ``MODE_CFB``, ``MODE_OFB``, |
| 101 |
and ``MODE_OPENPGP`` modes). |
| 102 |
|
| 103 |
The initialization vector to use for encryption or decryption. |
| 104 |
|
| 105 |
For ``MODE_CBC``, ``MODE_CFB``, and ``MODE_OFB`` it must be 8 bytes long. |
| 106 |
|
| 107 |
For ``MODE_OPENPGP`` mode only, |
| 108 |
it must be 8 bytes long for encryption |
| 109 |
and 10 bytes for decryption (in the latter case, it is |
| 110 |
actually the *encrypted* IV which was prefixed to the ciphertext). |
| 111 |
|
| 112 |
If not provided, a random byte string is generated (you must then |
| 113 |
read its value with the :attr:`iv` attribute). |
| 114 |
|
| 115 |
* **nonce** (*bytes*, *bytearray*, *memoryview*) -- |
| 116 |
(Only applicable for ``MODE_EAX`` and ``MODE_CTR``). |
| 117 |
|
| 118 |
A value that must never be reused for any other encryption done |
| 119 |
with this key. |
| 120 |
|
| 121 |
For ``MODE_EAX`` there are no |
| 122 |
restrictions on its length (recommended: **16** bytes). |
| 123 |
|
| 124 |
For ``MODE_CTR``, its length must be in the range **[0..7]**. |
| 125 |
|
| 126 |
If not provided for ``MODE_EAX``, a random byte string is generated (you |
| 127 |
can read it back via the ``nonce`` attribute). |
| 128 |
|
| 129 |
* **segment_size** (*integer*) -- |
| 130 |
(Only ``MODE_CFB``).The number of **bits** the plaintext and ciphertext |
| 131 |
are segmented in. It must be a multiple of 8. |
| 132 |
If not specified, it will be assumed to be 8. |
| 133 |
|
| 134 |
* **mac_len** : (*integer*) -- |
| 135 |
(Only ``MODE_EAX``) |
| 136 |
Length of the authentication tag, in bytes. |
| 137 |
It must be no longer than 8 (default). |
| 138 |
|
| 139 |
* **initial_value** : (*integer*) -- |
| 140 |
(Only ``MODE_CTR``). The initial value for the counter within |
| 141 |
the counter block. By default it is **0**. |
| 142 |
|
| 143 |
:Return: a CAST object, of the applicable mode. |
| 144 |
""" |
| 145 |
|
| 146 |
return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs) |
| 147 |
|
| 148 |
MODE_ECB = 1 |
| 149 |
MODE_CBC = 2 |
| 150 |
MODE_CFB = 3 |
| 151 |
MODE_OFB = 5 |
| 152 |
MODE_CTR = 6 |
| 153 |
MODE_OPENPGP = 7 |
| 154 |
MODE_EAX = 9 |
| 155 |
|
| 156 |
|
| 157 |
block_size = 8 |
| 158 |
|
| 159 |
key_size = range(5, 16 + 1) |