| 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 Triple DES: |
| 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, bchr, bord, bstr |
| 38 |
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, |
| 39 |
VoidPointer, SmartPointer, |
| 40 |
c_size_t) |
| 41 |
|
| 42 |
_raw_des3_lib = load_pycryptodome_raw_lib( |
| 43 |
"Crypto.Cipher._raw_des3", |
| 44 |
""" |
| 45 |
int DES3_start_operation(const uint8_t key[], |
| 46 |
size_t key_len, |
| 47 |
void **pResult); |
| 48 |
int DES3_encrypt(const void *state, |
| 49 |
const uint8_t *in, |
| 50 |
uint8_t *out, |
| 51 |
size_t data_len); |
| 52 |
int DES3_decrypt(const void *state, |
| 53 |
const uint8_t *in, |
| 54 |
uint8_t *out, |
| 55 |
size_t data_len); |
| 56 |
int DES3_stop_operation(void *state); |
| 57 |
""") |
| 58 |
|
| 59 |
|
| 60 |
def adjust_key_parity(key_in): |
| 61 |
"""Set the parity bits in a TDES key. |
| 62 |
|
| 63 |
:param key_in: the TDES key whose bits need to be adjusted |
| 64 |
:type key_in: byte string |
| 65 |
|
| 66 |
:returns: a copy of ``key_in``, with the parity bits correctly set |
| 67 |
:rtype: byte string |
| 68 |
|
| 69 |
:raises ValueError: if the TDES key is not 16 or 24 bytes long |
| 70 |
:raises ValueError: if the TDES key degenerates into Single DES |
| 71 |
""" |
| 72 |
|
| 73 |
def parity_byte(key_byte): |
| 74 |
parity = 1 |
| 75 |
for i in range(1, 8): |
| 76 |
parity ^= (key_byte >> i) & 1 |
| 77 |
return (key_byte & 0xFE) | parity |
| 78 |
|
| 79 |
if len(key_in) not in key_size: |
| 80 |
raise ValueError("Not a valid TDES key") |
| 81 |
|
| 82 |
key_out = b"".join([ bchr(parity_byte(bord(x))) for x in key_in ]) |
| 83 |
|
| 84 |
if key_out[:8] == key_out[8:16] or key_out[-16:-8] == key_out[-8:]: |
| 85 |
raise ValueError("Triple DES key degenerates to single DES") |
| 86 |
|
| 87 |
return key_out |
| 88 |
|
| 89 |
|
| 90 |
def _create_base_cipher(dict_parameters): |
| 91 |
"""This method instantiates and returns a handle to a low-level base cipher. |
| 92 |
It will absorb named parameters in the process.""" |
| 93 |
|
| 94 |
try: |
| 95 |
key_in = dict_parameters.pop("key") |
| 96 |
except KeyError: |
| 97 |
raise TypeError("Missing 'key' parameter") |
| 98 |
|
| 99 |
key = adjust_key_parity(bstr(key_in)) |
| 100 |
|
| 101 |
start_operation = _raw_des3_lib.DES3_start_operation |
| 102 |
stop_operation = _raw_des3_lib.DES3_stop_operation |
| 103 |
|
| 104 |
cipher = VoidPointer() |
| 105 |
result = start_operation(key, |
| 106 |
c_size_t(len(key)), |
| 107 |
cipher.address_of()) |
| 108 |
if result: |
| 109 |
raise ValueError("Error %X while instantiating the TDES cipher" |
| 110 |
% result) |
| 111 |
return SmartPointer(cipher.get(), stop_operation) |
| 112 |
|
| 113 |
|
| 114 |
def new(key, mode, *args, **kwargs): |
| 115 |
"""Create a new Triple DES cipher. |
| 116 |
|
| 117 |
:param key: |
| 118 |
The secret key to use in the symmetric cipher. |
| 119 |
It must be 16 or 24 byte long. The parity bits will be ignored. |
| 120 |
:type key: bytes/bytearray/memoryview |
| 121 |
|
| 122 |
:param mode: |
| 123 |
The chaining mode to use for encryption or decryption. |
| 124 |
:type mode: One of the supported ``MODE_*`` constants |
| 125 |
|
| 126 |
:Keyword Arguments: |
| 127 |
* **iv** (*bytes*, *bytearray*, *memoryview*) -- |
| 128 |
(Only applicable for ``MODE_CBC``, ``MODE_CFB``, ``MODE_OFB``, |
| 129 |
and ``MODE_OPENPGP`` modes). |
| 130 |
|
| 131 |
The initialization vector to use for encryption or decryption. |
| 132 |
|
| 133 |
For ``MODE_CBC``, ``MODE_CFB``, and ``MODE_OFB`` it must be 8 bytes long. |
| 134 |
|
| 135 |
For ``MODE_OPENPGP`` mode only, |
| 136 |
it must be 8 bytes long for encryption |
| 137 |
and 10 bytes for decryption (in the latter case, it is |
| 138 |
actually the *encrypted* IV which was prefixed to the ciphertext). |
| 139 |
|
| 140 |
If not provided, a random byte string is generated (you must then |
| 141 |
read its value with the :attr:`iv` attribute). |
| 142 |
|
| 143 |
* **nonce** (*bytes*, *bytearray*, *memoryview*) -- |
| 144 |
(Only applicable for ``MODE_EAX`` and ``MODE_CTR``). |
| 145 |
|
| 146 |
A value that must never be reused for any other encryption done |
| 147 |
with this key. |
| 148 |
|
| 149 |
For ``MODE_EAX`` there are no |
| 150 |
restrictions on its length (recommended: **16** bytes). |
| 151 |
|
| 152 |
For ``MODE_CTR``, its length must be in the range **[0..7]**. |
| 153 |
|
| 154 |
If not provided for ``MODE_EAX``, a random byte string is generated (you |
| 155 |
can read it back via the ``nonce`` attribute). |
| 156 |
|
| 157 |
* **segment_size** (*integer*) -- |
| 158 |
(Only ``MODE_CFB``).The number of **bits** the plaintext and ciphertext |
| 159 |
are segmented in. It must be a multiple of 8. |
| 160 |
If not specified, it will be assumed to be 8. |
| 161 |
|
| 162 |
* **mac_len** : (*integer*) -- |
| 163 |
(Only ``MODE_EAX``) |
| 164 |
Length of the authentication tag, in bytes. |
| 165 |
It must be no longer than 8 (default). |
| 166 |
|
| 167 |
* **initial_value** : (*integer*) -- |
| 168 |
(Only ``MODE_CTR``). The initial value for the counter within |
| 169 |
the counter block. By default it is **0**. |
| 170 |
|
| 171 |
:Return: a Triple DES object, of the applicable mode. |
| 172 |
""" |
| 173 |
|
| 174 |
return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs) |
| 175 |
|
| 176 |
MODE_ECB = 1 |
| 177 |
MODE_CBC = 2 |
| 178 |
MODE_CFB = 3 |
| 179 |
MODE_OFB = 5 |
| 180 |
MODE_CTR = 6 |
| 181 |
MODE_OPENPGP = 7 |
| 182 |
MODE_EAX = 9 |
| 183 |
|
| 184 |
|
| 185 |
block_size = 8 |
| 186 |
|
| 187 |
key_size = (16, 24) |