| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, c_size_t, |
| 32 |
create_string_buffer, get_raw_buffer, |
| 33 |
c_uint8_ptr, is_writeable_buffer) |
| 34 |
|
| 35 |
_raw_strxor = load_pycryptodome_raw_lib( |
| 36 |
"Crypto.Util._strxor", |
| 37 |
""" |
| 38 |
void strxor(const uint8_t *in1, |
| 39 |
const uint8_t *in2, |
| 40 |
uint8_t *out, size_t len); |
| 41 |
void strxor_c(const uint8_t *in, |
| 42 |
uint8_t c, |
| 43 |
uint8_t *out, |
| 44 |
size_t len); |
| 45 |
""") |
| 46 |
|
| 47 |
|
| 48 |
def strxor(term1, term2, output=None): |
| 49 |
"""From two byte strings of equal length, |
| 50 |
create a third one which is the byte-by-byte XOR of the two. |
| 51 |
|
| 52 |
Args: |
| 53 |
term1 (bytes/bytearray/memoryview): |
| 54 |
The first byte string to XOR. |
| 55 |
term2 (bytes/bytearray/memoryview): |
| 56 |
The second byte string to XOR. |
| 57 |
output (bytearray/memoryview): |
| 58 |
The location where the result will be written to. |
| 59 |
It must have the same length as ``term1`` and ``term2``. |
| 60 |
If ``None``, the result is returned. |
| 61 |
:Return: |
| 62 |
If ``output`` is ``None``, a new byte string with the result. |
| 63 |
Otherwise ``None``. |
| 64 |
|
| 65 |
.. note:: |
| 66 |
``term1`` and ``term2`` must have the same length. |
| 67 |
""" |
| 68 |
|
| 69 |
if len(term1) != len(term2): |
| 70 |
raise ValueError("Only byte strings of equal length can be xored") |
| 71 |
|
| 72 |
if output is None: |
| 73 |
result = create_string_buffer(len(term1)) |
| 74 |
else: |
| 75 |
|
| 76 |
result = output |
| 77 |
|
| 78 |
if not is_writeable_buffer(output): |
| 79 |
raise TypeError("output must be a bytearray or a writeable memoryview") |
| 80 |
|
| 81 |
if len(term1) != len(output): |
| 82 |
raise ValueError("output must have the same length as the input" |
| 83 |
" (%d bytes)" % len(term1)) |
| 84 |
|
| 85 |
_raw_strxor.strxor(c_uint8_ptr(term1), |
| 86 |
c_uint8_ptr(term2), |
| 87 |
c_uint8_ptr(result), |
| 88 |
c_size_t(len(term1))) |
| 89 |
|
| 90 |
if output is None: |
| 91 |
return get_raw_buffer(result) |
| 92 |
else: |
| 93 |
return None |
| 94 |
|
| 95 |
|
| 96 |
def strxor_c(term, c, output=None): |
| 97 |
"""From a byte string, create a second one of equal length |
| 98 |
where each byte is XOR-red with the same value. |
| 99 |
|
| 100 |
Args: |
| 101 |
term(bytes/bytearray/memoryview): |
| 102 |
The byte string to XOR. |
| 103 |
c (int): |
| 104 |
Every byte in the string will be XOR-ed with this value. |
| 105 |
It must be between 0 and 255 (included). |
| 106 |
output (None or bytearray/memoryview): |
| 107 |
The location where the result will be written to. |
| 108 |
It must have the same length as ``term``. |
| 109 |
If ``None``, the result is returned. |
| 110 |
|
| 111 |
Return: |
| 112 |
If ``output`` is ``None``, a new ``bytes`` string with the result. |
| 113 |
Otherwise ``None``. |
| 114 |
""" |
| 115 |
|
| 116 |
if not 0 <= c < 256: |
| 117 |
raise ValueError("c must be in range(256)") |
| 118 |
|
| 119 |
if output is None: |
| 120 |
result = create_string_buffer(len(term)) |
| 121 |
else: |
| 122 |
|
| 123 |
result = output |
| 124 |
|
| 125 |
if not is_writeable_buffer(output): |
| 126 |
raise TypeError("output must be a bytearray or a writeable memoryview") |
| 127 |
|
| 128 |
if len(term) != len(output): |
| 129 |
raise ValueError("output must have the same length as the input" |
| 130 |
" (%d bytes)" % len(term)) |
| 131 |
|
| 132 |
_raw_strxor.strxor_c(c_uint8_ptr(term), |
| 133 |
c, |
| 134 |
c_uint8_ptr(result), |
| 135 |
c_size_t(len(term)) |
| 136 |
) |
| 137 |
|
| 138 |
if output is None: |
| 139 |
return get_raw_buffer(result) |
| 140 |
else: |
| 141 |
return None |
| 142 |
|
| 143 |
|
| 144 |
def _strxor_direct(term1, term2, result): |
| 145 |
"""Very fast XOR - check conditions!""" |
| 146 |
_raw_strxor.strxor(term1, term2, result, c_size_t(len(term1))) |