| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
""" |
| 24 |
Output Feedback (CFB) mode. |
| 25 |
""" |
| 26 |
|
| 27 |
__all__ = ['OfbMode'] |
| 28 |
|
| 29 |
from Crypto.Util.py3compat import _copy_bytes |
| 30 |
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, VoidPointer, |
| 31 |
create_string_buffer, get_raw_buffer, |
| 32 |
SmartPointer, c_size_t, c_uint8_ptr, |
| 33 |
is_writeable_buffer) |
| 34 |
|
| 35 |
from Crypto.Random import get_random_bytes |
| 36 |
|
| 37 |
raw_ofb_lib = load_pycryptodome_raw_lib("Crypto.Cipher._raw_ofb", """ |
| 38 |
int OFB_start_operation(void *cipher, |
| 39 |
const uint8_t iv[], |
| 40 |
size_t iv_len, |
| 41 |
void **pResult); |
| 42 |
int OFB_encrypt(void *ofbState, |
| 43 |
const uint8_t *in, |
| 44 |
uint8_t *out, |
| 45 |
size_t data_len); |
| 46 |
int OFB_decrypt(void *ofbState, |
| 47 |
const uint8_t *in, |
| 48 |
uint8_t *out, |
| 49 |
size_t data_len); |
| 50 |
int OFB_stop_operation(void *state); |
| 51 |
""" |
| 52 |
) |
| 53 |
|
| 54 |
|
| 55 |
class OfbMode(object): |
| 56 |
"""*Output FeedBack (OFB)*. |
| 57 |
|
| 58 |
This mode is very similar to CBC, but it |
| 59 |
transforms the underlying block cipher into a stream cipher. |
| 60 |
|
| 61 |
The keystream is the iterated block encryption of the |
| 62 |
previous ciphertext block. |
| 63 |
|
| 64 |
An Initialization Vector (*IV*) is required. |
| 65 |
|
| 66 |
See `NIST SP800-38A`_ , Section 6.4. |
| 67 |
|
| 68 |
.. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
| 69 |
|
| 70 |
:undocumented: __init__ |
| 71 |
""" |
| 72 |
|
| 73 |
def __init__(self, block_cipher, iv): |
| 74 |
"""Create a new block cipher, configured in OFB mode. |
| 75 |
|
| 76 |
:Parameters: |
| 77 |
block_cipher : C pointer |
| 78 |
A smart pointer to the low-level block cipher instance. |
| 79 |
|
| 80 |
iv : bytes/bytearray/memoryview |
| 81 |
The initialization vector to use for encryption or decryption. |
| 82 |
It is as long as the cipher block. |
| 83 |
|
| 84 |
**The IV must be a nonce, to to be reused for any other |
| 85 |
message**. It shall be a nonce or a random value. |
| 86 |
|
| 87 |
Reusing the *IV* for encryptions performed with the same key |
| 88 |
compromises confidentiality. |
| 89 |
""" |
| 90 |
|
| 91 |
self._state = VoidPointer() |
| 92 |
result = raw_ofb_lib.OFB_start_operation(block_cipher.get(), |
| 93 |
c_uint8_ptr(iv), |
| 94 |
c_size_t(len(iv)), |
| 95 |
self._state.address_of()) |
| 96 |
if result: |
| 97 |
raise ValueError("Error %d while instantiating the OFB mode" |
| 98 |
% result) |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
self._state = SmartPointer(self._state.get(), |
| 103 |
raw_ofb_lib.OFB_stop_operation) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
block_cipher.release() |
| 108 |
|
| 109 |
self.block_size = len(iv) |
| 110 |
"""The block size of the underlying cipher, in bytes.""" |
| 111 |
|
| 112 |
self.iv = _copy_bytes(None, None, iv) |
| 113 |
"""The Initialization Vector originally used to create the object. |
| 114 |
The value does not change.""" |
| 115 |
|
| 116 |
self.IV = self.iv |
| 117 |
"""Alias for `iv`""" |
| 118 |
|
| 119 |
self._next = ["encrypt", "decrypt"] |
| 120 |
|
| 121 |
def encrypt(self, plaintext, output=None): |
| 122 |
"""Encrypt data with the key and the parameters set at initialization. |
| 123 |
|
| 124 |
A cipher object is stateful: once you have encrypted a message |
| 125 |
you cannot encrypt (or decrypt) another message using the same |
| 126 |
object. |
| 127 |
|
| 128 |
The data to encrypt can be broken up in two or |
| 129 |
more pieces and `encrypt` can be called multiple times. |
| 130 |
|
| 131 |
That is, the statement: |
| 132 |
|
| 133 |
>>> c.encrypt(a) + c.encrypt(b) |
| 134 |
|
| 135 |
is equivalent to: |
| 136 |
|
| 137 |
>>> c.encrypt(a+b) |
| 138 |
|
| 139 |
This function does not add any padding to the plaintext. |
| 140 |
|
| 141 |
:Parameters: |
| 142 |
plaintext : bytes/bytearray/memoryview |
| 143 |
The piece of data to encrypt. |
| 144 |
It can be of any length. |
| 145 |
:Keywords: |
| 146 |
output : bytearray/memoryview |
| 147 |
The location where the ciphertext must be written to. |
| 148 |
If ``None``, the ciphertext is returned. |
| 149 |
:Return: |
| 150 |
If ``output`` is ``None``, the ciphertext is returned as ``bytes``. |
| 151 |
Otherwise, ``None``. |
| 152 |
""" |
| 153 |
|
| 154 |
if "encrypt" not in self._next: |
| 155 |
raise TypeError("encrypt() cannot be called after decrypt()") |
| 156 |
self._next = ["encrypt"] |
| 157 |
|
| 158 |
if output is None: |
| 159 |
ciphertext = create_string_buffer(len(plaintext)) |
| 160 |
else: |
| 161 |
ciphertext = output |
| 162 |
|
| 163 |
if not is_writeable_buffer(output): |
| 164 |
raise TypeError("output must be a bytearray or a writeable memoryview") |
| 165 |
|
| 166 |
if len(plaintext) != len(output): |
| 167 |
raise ValueError("output must have the same length as the input" |
| 168 |
" (%d bytes)" % len(plaintext)) |
| 169 |
|
| 170 |
result = raw_ofb_lib.OFB_encrypt(self._state.get(), |
| 171 |
c_uint8_ptr(plaintext), |
| 172 |
c_uint8_ptr(ciphertext), |
| 173 |
c_size_t(len(plaintext))) |
| 174 |
if result: |
| 175 |
raise ValueError("Error %d while encrypting in OFB mode" % result) |
| 176 |
|
| 177 |
if output is None: |
| 178 |
return get_raw_buffer(ciphertext) |
| 179 |
else: |
| 180 |
return None |
| 181 |
|
| 182 |
def decrypt(self, ciphertext, output=None): |
| 183 |
"""Decrypt data with the key and the parameters set at initialization. |
| 184 |
|
| 185 |
A cipher object is stateful: once you have decrypted a message |
| 186 |
you cannot decrypt (or encrypt) another message with the same |
| 187 |
object. |
| 188 |
|
| 189 |
The data to decrypt can be broken up in two or |
| 190 |
more pieces and `decrypt` can be called multiple times. |
| 191 |
|
| 192 |
That is, the statement: |
| 193 |
|
| 194 |
>>> c.decrypt(a) + c.decrypt(b) |
| 195 |
|
| 196 |
is equivalent to: |
| 197 |
|
| 198 |
>>> c.decrypt(a+b) |
| 199 |
|
| 200 |
This function does not remove any padding from the plaintext. |
| 201 |
|
| 202 |
:Parameters: |
| 203 |
ciphertext : bytes/bytearray/memoryview |
| 204 |
The piece of data to decrypt. |
| 205 |
It can be of any length. |
| 206 |
:Keywords: |
| 207 |
output : bytearray/memoryview |
| 208 |
The location where the plaintext is written to. |
| 209 |
If ``None``, the plaintext is returned. |
| 210 |
:Return: |
| 211 |
If ``output`` is ``None``, the plaintext is returned as ``bytes``. |
| 212 |
Otherwise, ``None``. |
| 213 |
""" |
| 214 |
|
| 215 |
if "decrypt" not in self._next: |
| 216 |
raise TypeError("decrypt() cannot be called after encrypt()") |
| 217 |
self._next = ["decrypt"] |
| 218 |
|
| 219 |
if output is None: |
| 220 |
plaintext = create_string_buffer(len(ciphertext)) |
| 221 |
else: |
| 222 |
plaintext = output |
| 223 |
|
| 224 |
if not is_writeable_buffer(output): |
| 225 |
raise TypeError("output must be a bytearray or a writeable memoryview") |
| 226 |
|
| 227 |
if len(ciphertext) != len(output): |
| 228 |
raise ValueError("output must have the same length as the input" |
| 229 |
" (%d bytes)" % len(plaintext)) |
| 230 |
|
| 231 |
result = raw_ofb_lib.OFB_decrypt(self._state.get(), |
| 232 |
c_uint8_ptr(ciphertext), |
| 233 |
c_uint8_ptr(plaintext), |
| 234 |
c_size_t(len(ciphertext))) |
| 235 |
if result: |
| 236 |
raise ValueError("Error %d while decrypting in OFB mode" % result) |
| 237 |
|
| 238 |
if output is None: |
| 239 |
return get_raw_buffer(plaintext) |
| 240 |
else: |
| 241 |
return None |
| 242 |
|
| 243 |
|
| 244 |
def _create_ofb_cipher(factory, **kwargs): |
| 245 |
"""Instantiate a cipher object that performs OFB encryption/decryption. |
| 246 |
|
| 247 |
:Parameters: |
| 248 |
factory : module |
| 249 |
The underlying block cipher, a module from ``Crypto.Cipher``. |
| 250 |
|
| 251 |
:Keywords: |
| 252 |
iv : bytes/bytearray/memoryview |
| 253 |
The IV to use for OFB. |
| 254 |
|
| 255 |
IV : bytes/bytearray/memoryview |
| 256 |
Alias for ``iv``. |
| 257 |
|
| 258 |
Any other keyword will be passed to the underlying block cipher. |
| 259 |
See the relevant documentation for details (at least ``key`` will need |
| 260 |
to be present). |
| 261 |
""" |
| 262 |
|
| 263 |
cipher_state = factory._create_base_cipher(kwargs) |
| 264 |
iv = kwargs.pop("IV", None) |
| 265 |
IV = kwargs.pop("iv", None) |
| 266 |
|
| 267 |
if (None, None) == (iv, IV): |
| 268 |
iv = get_random_bytes(factory.block_size) |
| 269 |
if iv is not None: |
| 270 |
if IV is not None: |
| 271 |
raise TypeError("You must either use 'iv' or 'IV', not both") |
| 272 |
else: |
| 273 |
iv = IV |
| 274 |
|
| 275 |
if len(iv) != factory.block_size: |
| 276 |
raise ValueError("Incorrect IV length (it must be %d bytes long)" % |
| 277 |
factory.block_size) |
| 278 |
|
| 279 |
if kwargs: |
| 280 |
raise TypeError("Unknown parameters for OFB: %s" % str(kwargs)) |
| 281 |
|
| 282 |
return OfbMode(cipher_state, iv) |