Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +3 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/AES.py +234 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/ARC4.py +136 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/Blowfish.py +159 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/Blowfish.pyi +35 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/CAST.pyi +35 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/ChaCha20_Poly1305.py +334 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/DES3.py +187 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/PKCS1_OAEP.py +231 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/PKCS1_v1_5.pyi +20 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/Salsa20.py +167 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/Salsa20.pyi +26 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__init__.pyi +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/AES.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/ARC2.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/ARC4.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/Blowfish.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/CAST.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/ChaCha20.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/ChaCha20_Poly1305.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/DES.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/DES3.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/PKCS1_OAEP.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/PKCS1_v1_5.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/Salsa20.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_EKSBlowfish.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/__init__.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_cbc.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_ccm.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_cfb.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_ctr.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_eax.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_ecb.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_gcm.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_ocb.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_ofb.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_openpgp.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_siv.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_pkcs1_oaep_decode.cpython-310.pyc +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_chacha20.abi3.so +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_cbc.py +293 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_cfb.py +293 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_eax.py +408 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_ecb.py +220 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_gcm.pyi +45 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_ocb.py +532 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_siv.pyi +38 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_raw_aes.abi3.so +3 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_raw_blowfish.abi3.so +0 -0
- minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_raw_cast.abi3.so +0 -0
.gitattributes
CHANGED
|
@@ -1348,3 +1348,6 @@ llava/lib/python3.10/site-packages/pip/_vendor/idna/__pycache__/uts46data.cpytho
|
|
| 1348 |
llava/lib/python3.10/site-packages/pip/_vendor/idna/__pycache__/idnadata.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1349 |
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_raw_aesni.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 1350 |
minigpt2/lib/python3.10/site-packages/imageio/plugins/__pycache__/_tifffile.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 1348 |
llava/lib/python3.10/site-packages/pip/_vendor/idna/__pycache__/idnadata.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1349 |
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_raw_aesni.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 1350 |
minigpt2/lib/python3.10/site-packages/imageio/plugins/__pycache__/_tifffile.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1351 |
+
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_raw_eksblowfish.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 1352 |
+
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_raw_aes.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 1353 |
+
minigpt2/lib/python3.10/site-packages/Crypto/PublicKey/_ed448.abi3.so filter=lfs diff=lfs merge=lfs -text
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/AES.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Cipher/AES.py : AES
|
| 4 |
+
#
|
| 5 |
+
# ===================================================================
|
| 6 |
+
# The contents of this file are dedicated to the public domain. To
|
| 7 |
+
# the extent that dedication to the public domain is not available,
|
| 8 |
+
# everyone is granted a worldwide, perpetual, royalty-free,
|
| 9 |
+
# non-exclusive license to exercise all rights associated with the
|
| 10 |
+
# contents of this file for any purpose whatsoever.
|
| 11 |
+
# No rights are reserved.
|
| 12 |
+
#
|
| 13 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 14 |
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 15 |
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
| 16 |
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
| 17 |
+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
| 18 |
+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 19 |
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 20 |
+
# SOFTWARE.
|
| 21 |
+
# ===================================================================
|
| 22 |
+
|
| 23 |
+
import sys
|
| 24 |
+
|
| 25 |
+
from Crypto.Cipher import _create_cipher
|
| 26 |
+
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
|
| 27 |
+
VoidPointer, SmartPointer,
|
| 28 |
+
c_size_t, c_uint8_ptr)
|
| 29 |
+
|
| 30 |
+
from Crypto.Util import _cpu_features
|
| 31 |
+
from Crypto.Random import get_random_bytes
|
| 32 |
+
|
| 33 |
+
MODE_ECB = 1 #: Electronic Code Book (:ref:`ecb_mode`)
|
| 34 |
+
MODE_CBC = 2 #: Cipher-Block Chaining (:ref:`cbc_mode`)
|
| 35 |
+
MODE_CFB = 3 #: Cipher Feedback (:ref:`cfb_mode`)
|
| 36 |
+
MODE_OFB = 5 #: Output Feedback (:ref:`ofb_mode`)
|
| 37 |
+
MODE_CTR = 6 #: Counter mode (:ref:`ctr_mode`)
|
| 38 |
+
MODE_OPENPGP = 7 #: OpenPGP mode (:ref:`openpgp_mode`)
|
| 39 |
+
MODE_CCM = 8 #: Counter with CBC-MAC (:ref:`ccm_mode`)
|
| 40 |
+
MODE_EAX = 9 #: :ref:`eax_mode`
|
| 41 |
+
MODE_SIV = 10 #: Synthetic Initialization Vector (:ref:`siv_mode`)
|
| 42 |
+
MODE_GCM = 11 #: Galois Counter Mode (:ref:`gcm_mode`)
|
| 43 |
+
MODE_OCB = 12 #: Offset Code Book (:ref:`ocb_mode`)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
_cproto = """
|
| 47 |
+
int AES_start_operation(const uint8_t key[],
|
| 48 |
+
size_t key_len,
|
| 49 |
+
void **pResult);
|
| 50 |
+
int AES_encrypt(const void *state,
|
| 51 |
+
const uint8_t *in,
|
| 52 |
+
uint8_t *out,
|
| 53 |
+
size_t data_len);
|
| 54 |
+
int AES_decrypt(const void *state,
|
| 55 |
+
const uint8_t *in,
|
| 56 |
+
uint8_t *out,
|
| 57 |
+
size_t data_len);
|
| 58 |
+
int AES_stop_operation(void *state);
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# Load portable AES
|
| 63 |
+
_raw_aes_lib = load_pycryptodome_raw_lib("Crypto.Cipher._raw_aes",
|
| 64 |
+
_cproto)
|
| 65 |
+
|
| 66 |
+
# Try to load AES with AES NI instructions
|
| 67 |
+
try:
|
| 68 |
+
_raw_aesni_lib = None
|
| 69 |
+
if _cpu_features.have_aes_ni():
|
| 70 |
+
_raw_aesni_lib = load_pycryptodome_raw_lib("Crypto.Cipher._raw_aesni",
|
| 71 |
+
_cproto.replace("AES",
|
| 72 |
+
"AESNI"))
|
| 73 |
+
# _raw_aesni may not have been compiled in
|
| 74 |
+
except OSError:
|
| 75 |
+
pass
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def _create_base_cipher(dict_parameters):
|
| 79 |
+
"""This method instantiates and returns a handle to a low-level
|
| 80 |
+
base cipher. It will absorb named parameters in the process."""
|
| 81 |
+
|
| 82 |
+
use_aesni = dict_parameters.pop("use_aesni", True)
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
key = dict_parameters.pop("key")
|
| 86 |
+
except KeyError:
|
| 87 |
+
raise TypeError("Missing 'key' parameter")
|
| 88 |
+
|
| 89 |
+
if len(key) not in key_size:
|
| 90 |
+
raise ValueError("Incorrect AES key length (%d bytes)" % len(key))
|
| 91 |
+
|
| 92 |
+
if use_aesni and _raw_aesni_lib:
|
| 93 |
+
start_operation = _raw_aesni_lib.AESNI_start_operation
|
| 94 |
+
stop_operation = _raw_aesni_lib.AESNI_stop_operation
|
| 95 |
+
else:
|
| 96 |
+
start_operation = _raw_aes_lib.AES_start_operation
|
| 97 |
+
stop_operation = _raw_aes_lib.AES_stop_operation
|
| 98 |
+
|
| 99 |
+
cipher = VoidPointer()
|
| 100 |
+
result = start_operation(c_uint8_ptr(key),
|
| 101 |
+
c_size_t(len(key)),
|
| 102 |
+
cipher.address_of())
|
| 103 |
+
if result:
|
| 104 |
+
raise ValueError("Error %X while instantiating the AES cipher"
|
| 105 |
+
% result)
|
| 106 |
+
return SmartPointer(cipher.get(), stop_operation)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def _derive_Poly1305_key_pair(key, nonce):
|
| 110 |
+
"""Derive a tuple (r, s, nonce) for a Poly1305 MAC.
|
| 111 |
+
|
| 112 |
+
If nonce is ``None``, a new 16-byte nonce is generated.
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
if len(key) != 32:
|
| 116 |
+
raise ValueError("Poly1305 with AES requires a 32-byte key")
|
| 117 |
+
|
| 118 |
+
if nonce is None:
|
| 119 |
+
nonce = get_random_bytes(16)
|
| 120 |
+
elif len(nonce) != 16:
|
| 121 |
+
raise ValueError("Poly1305 with AES requires a 16-byte nonce")
|
| 122 |
+
|
| 123 |
+
s = new(key[:16], MODE_ECB).encrypt(nonce)
|
| 124 |
+
return key[16:], s, nonce
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def new(key, mode, *args, **kwargs):
|
| 128 |
+
"""Create a new AES cipher.
|
| 129 |
+
|
| 130 |
+
Args:
|
| 131 |
+
key(bytes/bytearray/memoryview):
|
| 132 |
+
The secret key to use in the symmetric cipher.
|
| 133 |
+
|
| 134 |
+
It must be 16 (*AES-128)*, 24 (*AES-192*) or 32 (*AES-256*) bytes long.
|
| 135 |
+
|
| 136 |
+
For ``MODE_SIV`` only, it doubles to 32, 48, or 64 bytes.
|
| 137 |
+
mode (a ``MODE_*`` constant):
|
| 138 |
+
The chaining mode to use for encryption or decryption.
|
| 139 |
+
If in doubt, use ``MODE_EAX``.
|
| 140 |
+
|
| 141 |
+
Keyword Args:
|
| 142 |
+
iv (bytes/bytearray/memoryview):
|
| 143 |
+
(Only applicable for ``MODE_CBC``, ``MODE_CFB``, ``MODE_OFB``,
|
| 144 |
+
and ``MODE_OPENPGP`` modes).
|
| 145 |
+
|
| 146 |
+
The initialization vector to use for encryption or decryption.
|
| 147 |
+
|
| 148 |
+
For ``MODE_CBC``, ``MODE_CFB``, and ``MODE_OFB`` it must be 16 bytes long.
|
| 149 |
+
|
| 150 |
+
For ``MODE_OPENPGP`` mode only,
|
| 151 |
+
it must be 16 bytes long for encryption
|
| 152 |
+
and 18 bytes for decryption (in the latter case, it is
|
| 153 |
+
actually the *encrypted* IV which was prefixed to the ciphertext).
|
| 154 |
+
|
| 155 |
+
If not provided, a random byte string is generated (you must then
|
| 156 |
+
read its value with the :attr:`iv` attribute).
|
| 157 |
+
|
| 158 |
+
nonce (bytes/bytearray/memoryview):
|
| 159 |
+
(Only applicable for ``MODE_CCM``, ``MODE_EAX``, ``MODE_GCM``,
|
| 160 |
+
``MODE_SIV``, ``MODE_OCB``, and ``MODE_CTR``).
|
| 161 |
+
|
| 162 |
+
A value that must never be reused for any other encryption done
|
| 163 |
+
with this key (except possibly for ``MODE_SIV``, see below).
|
| 164 |
+
|
| 165 |
+
For ``MODE_EAX``, ``MODE_GCM`` and ``MODE_SIV`` there are no
|
| 166 |
+
restrictions on its length (recommended: **16** bytes).
|
| 167 |
+
|
| 168 |
+
For ``MODE_CCM``, its length must be in the range **[7..13]**.
|
| 169 |
+
Bear in mind that with CCM there is a trade-off between nonce
|
| 170 |
+
length and maximum message size. Recommendation: **11** bytes.
|
| 171 |
+
|
| 172 |
+
For ``MODE_OCB``, its length must be in the range **[1..15]**
|
| 173 |
+
(recommended: **15**).
|
| 174 |
+
|
| 175 |
+
For ``MODE_CTR``, its length must be in the range **[0..15]**
|
| 176 |
+
(recommended: **8**).
|
| 177 |
+
|
| 178 |
+
For ``MODE_SIV``, the nonce is optional, if it is not specified,
|
| 179 |
+
then no nonce is being used, which renders the encryption
|
| 180 |
+
deterministic.
|
| 181 |
+
|
| 182 |
+
If not provided, for modes other than ``MODE_SIV``, a random
|
| 183 |
+
byte string of the recommended length is used (you must then
|
| 184 |
+
read its value with the :attr:`nonce` attribute).
|
| 185 |
+
|
| 186 |
+
segment_size (integer):
|
| 187 |
+
(Only ``MODE_CFB``).The number of **bits** the plaintext and ciphertext
|
| 188 |
+
are segmented in. It must be a multiple of 8.
|
| 189 |
+
If not specified, it will be assumed to be 8.
|
| 190 |
+
|
| 191 |
+
mac_len (integer):
|
| 192 |
+
(Only ``MODE_EAX``, ``MODE_GCM``, ``MODE_OCB``, ``MODE_CCM``)
|
| 193 |
+
Length of the authentication tag, in bytes.
|
| 194 |
+
|
| 195 |
+
It must be even and in the range **[4..16]**.
|
| 196 |
+
The recommended value (and the default, if not specified) is **16**.
|
| 197 |
+
|
| 198 |
+
msg_len (integer):
|
| 199 |
+
(Only ``MODE_CCM``). Length of the message to (de)cipher.
|
| 200 |
+
If not specified, ``encrypt`` must be called with the entire message.
|
| 201 |
+
Similarly, ``decrypt`` can only be called once.
|
| 202 |
+
|
| 203 |
+
assoc_len (integer):
|
| 204 |
+
(Only ``MODE_CCM``). Length of the associated data.
|
| 205 |
+
If not specified, all associated data is buffered internally,
|
| 206 |
+
which may represent a problem for very large messages.
|
| 207 |
+
|
| 208 |
+
initial_value (integer or bytes/bytearray/memoryview):
|
| 209 |
+
(Only ``MODE_CTR``).
|
| 210 |
+
The initial value for the counter. If not present, the cipher will
|
| 211 |
+
start counting from 0. The value is incremented by one for each block.
|
| 212 |
+
The counter number is encoded in big endian mode.
|
| 213 |
+
|
| 214 |
+
counter (object):
|
| 215 |
+
(Only ``MODE_CTR``).
|
| 216 |
+
Instance of ``Crypto.Util.Counter``, which allows full customization
|
| 217 |
+
of the counter block. This parameter is incompatible to both ``nonce``
|
| 218 |
+
and ``initial_value``.
|
| 219 |
+
|
| 220 |
+
use_aesni: (boolean):
|
| 221 |
+
Use Intel AES-NI hardware extensions (default: use if available).
|
| 222 |
+
|
| 223 |
+
Returns:
|
| 224 |
+
an AES object, of the applicable mode.
|
| 225 |
+
"""
|
| 226 |
+
|
| 227 |
+
kwargs["add_aes_modes"] = True
|
| 228 |
+
return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
# Size of a data block (in bytes)
|
| 232 |
+
block_size = 16
|
| 233 |
+
# Size of a key (in bytes)
|
| 234 |
+
key_size = (16, 24, 32)
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/ARC4.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Cipher/ARC4.py : ARC4
|
| 4 |
+
#
|
| 5 |
+
# ===================================================================
|
| 6 |
+
# The contents of this file are dedicated to the public domain. To
|
| 7 |
+
# the extent that dedication to the public domain is not available,
|
| 8 |
+
# everyone is granted a worldwide, perpetual, royalty-free,
|
| 9 |
+
# non-exclusive license to exercise all rights associated with the
|
| 10 |
+
# contents of this file for any purpose whatsoever.
|
| 11 |
+
# No rights are reserved.
|
| 12 |
+
#
|
| 13 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 14 |
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 15 |
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
| 16 |
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
| 17 |
+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
| 18 |
+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 19 |
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 20 |
+
# SOFTWARE.
|
| 21 |
+
# ===================================================================
|
| 22 |
+
|
| 23 |
+
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, VoidPointer,
|
| 24 |
+
create_string_buffer, get_raw_buffer,
|
| 25 |
+
SmartPointer, c_size_t, c_uint8_ptr)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
_raw_arc4_lib = load_pycryptodome_raw_lib("Crypto.Cipher._ARC4", """
|
| 29 |
+
int ARC4_stream_encrypt(void *rc4State, const uint8_t in[],
|
| 30 |
+
uint8_t out[], size_t len);
|
| 31 |
+
int ARC4_stream_init(uint8_t *key, size_t keylen,
|
| 32 |
+
void **pRc4State);
|
| 33 |
+
int ARC4_stream_destroy(void *rc4State);
|
| 34 |
+
""")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class ARC4Cipher:
|
| 38 |
+
"""ARC4 cipher object. Do not create it directly. Use
|
| 39 |
+
:func:`Crypto.Cipher.ARC4.new` instead.
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
def __init__(self, key, *args, **kwargs):
|
| 43 |
+
"""Initialize an ARC4 cipher object
|
| 44 |
+
|
| 45 |
+
See also `new()` at the module level."""
|
| 46 |
+
|
| 47 |
+
if len(args) > 0:
|
| 48 |
+
ndrop = args[0]
|
| 49 |
+
args = args[1:]
|
| 50 |
+
else:
|
| 51 |
+
ndrop = kwargs.pop('drop', 0)
|
| 52 |
+
|
| 53 |
+
if len(key) not in key_size:
|
| 54 |
+
raise ValueError("Incorrect ARC4 key length (%d bytes)" %
|
| 55 |
+
len(key))
|
| 56 |
+
|
| 57 |
+
self._state = VoidPointer()
|
| 58 |
+
result = _raw_arc4_lib.ARC4_stream_init(c_uint8_ptr(key),
|
| 59 |
+
c_size_t(len(key)),
|
| 60 |
+
self._state.address_of())
|
| 61 |
+
if result != 0:
|
| 62 |
+
raise ValueError("Error %d while creating the ARC4 cipher"
|
| 63 |
+
% result)
|
| 64 |
+
self._state = SmartPointer(self._state.get(),
|
| 65 |
+
_raw_arc4_lib.ARC4_stream_destroy)
|
| 66 |
+
|
| 67 |
+
if ndrop > 0:
|
| 68 |
+
# This is OK even if the cipher is used for decryption,
|
| 69 |
+
# since encrypt and decrypt are actually the same thing
|
| 70 |
+
# with ARC4.
|
| 71 |
+
self.encrypt(b'\x00' * ndrop)
|
| 72 |
+
|
| 73 |
+
self.block_size = 1
|
| 74 |
+
self.key_size = len(key)
|
| 75 |
+
|
| 76 |
+
def encrypt(self, plaintext):
|
| 77 |
+
"""Encrypt a piece of data.
|
| 78 |
+
|
| 79 |
+
:param plaintext: The data to encrypt, of any size.
|
| 80 |
+
:type plaintext: bytes, bytearray, memoryview
|
| 81 |
+
:returns: the encrypted byte string, of equal length as the
|
| 82 |
+
plaintext.
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
ciphertext = create_string_buffer(len(plaintext))
|
| 86 |
+
result = _raw_arc4_lib.ARC4_stream_encrypt(self._state.get(),
|
| 87 |
+
c_uint8_ptr(plaintext),
|
| 88 |
+
ciphertext,
|
| 89 |
+
c_size_t(len(plaintext)))
|
| 90 |
+
if result:
|
| 91 |
+
raise ValueError("Error %d while encrypting with RC4" % result)
|
| 92 |
+
return get_raw_buffer(ciphertext)
|
| 93 |
+
|
| 94 |
+
def decrypt(self, ciphertext):
|
| 95 |
+
"""Decrypt a piece of data.
|
| 96 |
+
|
| 97 |
+
:param ciphertext: The data to decrypt, of any size.
|
| 98 |
+
:type ciphertext: bytes, bytearray, memoryview
|
| 99 |
+
:returns: the decrypted byte string, of equal length as the
|
| 100 |
+
ciphertext.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
try:
|
| 104 |
+
return self.encrypt(ciphertext)
|
| 105 |
+
except ValueError as e:
|
| 106 |
+
raise ValueError(str(e).replace("enc", "dec"))
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def new(key, *args, **kwargs):
|
| 110 |
+
"""Create a new ARC4 cipher.
|
| 111 |
+
|
| 112 |
+
:param key:
|
| 113 |
+
The secret key to use in the symmetric cipher.
|
| 114 |
+
Its length must be in the range ``[1..256]``.
|
| 115 |
+
The recommended length is 16 bytes.
|
| 116 |
+
:type key: bytes, bytearray, memoryview
|
| 117 |
+
|
| 118 |
+
:Keyword Arguments:
|
| 119 |
+
* *drop* (``integer``) --
|
| 120 |
+
The amount of bytes to discard from the initial part of the keystream.
|
| 121 |
+
In fact, such part has been found to be distinguishable from random
|
| 122 |
+
data (while it shouldn't) and also correlated to key.
|
| 123 |
+
|
| 124 |
+
The recommended value is 3072_ bytes. The default value is 0.
|
| 125 |
+
|
| 126 |
+
:Return: an `ARC4Cipher` object
|
| 127 |
+
|
| 128 |
+
.. _3072: http://eprint.iacr.org/2002/067.pdf
|
| 129 |
+
"""
|
| 130 |
+
return ARC4Cipher(key, *args, **kwargs)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
# Size of a data block (in bytes)
|
| 134 |
+
block_size = 1
|
| 135 |
+
# Size of a key (in bytes)
|
| 136 |
+
key_size = range(1, 256+1)
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/Blowfish.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Cipher/Blowfish.py : Blowfish
|
| 4 |
+
#
|
| 5 |
+
# ===================================================================
|
| 6 |
+
# The contents of this file are dedicated to the public domain. To
|
| 7 |
+
# the extent that dedication to the public domain is not available,
|
| 8 |
+
# everyone is granted a worldwide, perpetual, royalty-free,
|
| 9 |
+
# non-exclusive license to exercise all rights associated with the
|
| 10 |
+
# contents of this file for any purpose whatsoever.
|
| 11 |
+
# No rights are reserved.
|
| 12 |
+
#
|
| 13 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 14 |
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 15 |
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
| 16 |
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
| 17 |
+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
| 18 |
+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 19 |
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 20 |
+
# SOFTWARE.
|
| 21 |
+
# ===================================================================
|
| 22 |
+
"""
|
| 23 |
+
Module's constants for the modes of operation supported with Blowfish:
|
| 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._raw_api import (load_pycryptodome_raw_lib,
|
| 38 |
+
VoidPointer, SmartPointer, c_size_t,
|
| 39 |
+
c_uint8_ptr)
|
| 40 |
+
|
| 41 |
+
_raw_blowfish_lib = load_pycryptodome_raw_lib(
|
| 42 |
+
"Crypto.Cipher._raw_blowfish",
|
| 43 |
+
"""
|
| 44 |
+
int Blowfish_start_operation(const uint8_t key[],
|
| 45 |
+
size_t key_len,
|
| 46 |
+
void **pResult);
|
| 47 |
+
int Blowfish_encrypt(const void *state,
|
| 48 |
+
const uint8_t *in,
|
| 49 |
+
uint8_t *out,
|
| 50 |
+
size_t data_len);
|
| 51 |
+
int Blowfish_decrypt(const void *state,
|
| 52 |
+
const uint8_t *in,
|
| 53 |
+
uint8_t *out,
|
| 54 |
+
size_t data_len);
|
| 55 |
+
int Blowfish_stop_operation(void *state);
|
| 56 |
+
"""
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def _create_base_cipher(dict_parameters):
|
| 61 |
+
"""This method instantiates and returns a smart pointer to
|
| 62 |
+
a low-level base cipher. It will absorb named parameters in
|
| 63 |
+
the process."""
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
key = dict_parameters.pop("key")
|
| 67 |
+
except KeyError:
|
| 68 |
+
raise TypeError("Missing 'key' parameter")
|
| 69 |
+
|
| 70 |
+
if len(key) not in key_size:
|
| 71 |
+
raise ValueError("Incorrect Blowfish key length (%d bytes)" % len(key))
|
| 72 |
+
|
| 73 |
+
start_operation = _raw_blowfish_lib.Blowfish_start_operation
|
| 74 |
+
stop_operation = _raw_blowfish_lib.Blowfish_stop_operation
|
| 75 |
+
|
| 76 |
+
void_p = VoidPointer()
|
| 77 |
+
result = start_operation(c_uint8_ptr(key),
|
| 78 |
+
c_size_t(len(key)),
|
| 79 |
+
void_p.address_of())
|
| 80 |
+
if result:
|
| 81 |
+
raise ValueError("Error %X while instantiating the Blowfish cipher"
|
| 82 |
+
% result)
|
| 83 |
+
return SmartPointer(void_p.get(), stop_operation)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def new(key, mode, *args, **kwargs):
|
| 87 |
+
"""Create a new Blowfish cipher
|
| 88 |
+
|
| 89 |
+
:param key:
|
| 90 |
+
The secret key to use in the symmetric cipher.
|
| 91 |
+
Its length can vary from 5 to 56 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 Blowfish 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 |
+
# Size of a data block (in bytes)
|
| 157 |
+
block_size = 8
|
| 158 |
+
# Size of a key (in bytes)
|
| 159 |
+
key_size = range(4, 56 + 1)
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/Blowfish.pyi
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Union, Dict, Iterable, Optional
|
| 2 |
+
|
| 3 |
+
Buffer = bytes|bytearray|memoryview
|
| 4 |
+
|
| 5 |
+
from Crypto.Cipher._mode_ecb import EcbMode
|
| 6 |
+
from Crypto.Cipher._mode_cbc import CbcMode
|
| 7 |
+
from Crypto.Cipher._mode_cfb import CfbMode
|
| 8 |
+
from Crypto.Cipher._mode_ofb import OfbMode
|
| 9 |
+
from Crypto.Cipher._mode_ctr import CtrMode
|
| 10 |
+
from Crypto.Cipher._mode_openpgp import OpenPgpMode
|
| 11 |
+
from Crypto.Cipher._mode_eax import EaxMode
|
| 12 |
+
|
| 13 |
+
BlowfishMode = int
|
| 14 |
+
|
| 15 |
+
MODE_ECB: BlowfishMode
|
| 16 |
+
MODE_CBC: BlowfishMode
|
| 17 |
+
MODE_CFB: BlowfishMode
|
| 18 |
+
MODE_OFB: BlowfishMode
|
| 19 |
+
MODE_CTR: BlowfishMode
|
| 20 |
+
MODE_OPENPGP: BlowfishMode
|
| 21 |
+
MODE_EAX: BlowfishMode
|
| 22 |
+
|
| 23 |
+
def new(key: Buffer,
|
| 24 |
+
mode: BlowfishMode,
|
| 25 |
+
iv : Optional[Buffer] = ...,
|
| 26 |
+
IV : Optional[Buffer] = ...,
|
| 27 |
+
nonce : Optional[Buffer] = ...,
|
| 28 |
+
segment_size : int = ...,
|
| 29 |
+
mac_len : int = ...,
|
| 30 |
+
initial_value : Union[int, Buffer] = ...,
|
| 31 |
+
counter : Dict = ...) -> \
|
| 32 |
+
Union[EcbMode, CbcMode, CfbMode, OfbMode, CtrMode, OpenPgpMode]: ...
|
| 33 |
+
|
| 34 |
+
block_size: int
|
| 35 |
+
key_size: Iterable[int]
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/CAST.pyi
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Union, Dict, Iterable, Optional
|
| 2 |
+
|
| 3 |
+
Buffer = bytes|bytearray|memoryview
|
| 4 |
+
|
| 5 |
+
from Crypto.Cipher._mode_ecb import EcbMode
|
| 6 |
+
from Crypto.Cipher._mode_cbc import CbcMode
|
| 7 |
+
from Crypto.Cipher._mode_cfb import CfbMode
|
| 8 |
+
from Crypto.Cipher._mode_ofb import OfbMode
|
| 9 |
+
from Crypto.Cipher._mode_ctr import CtrMode
|
| 10 |
+
from Crypto.Cipher._mode_openpgp import OpenPgpMode
|
| 11 |
+
from Crypto.Cipher._mode_eax import EaxMode
|
| 12 |
+
|
| 13 |
+
CASTMode = int
|
| 14 |
+
|
| 15 |
+
MODE_ECB: CASTMode
|
| 16 |
+
MODE_CBC: CASTMode
|
| 17 |
+
MODE_CFB: CASTMode
|
| 18 |
+
MODE_OFB: CASTMode
|
| 19 |
+
MODE_CTR: CASTMode
|
| 20 |
+
MODE_OPENPGP: CASTMode
|
| 21 |
+
MODE_EAX: CASTMode
|
| 22 |
+
|
| 23 |
+
def new(key: Buffer,
|
| 24 |
+
mode: CASTMode,
|
| 25 |
+
iv : Optional[Buffer] = ...,
|
| 26 |
+
IV : Optional[Buffer] = ...,
|
| 27 |
+
nonce : Optional[Buffer] = ...,
|
| 28 |
+
segment_size : int = ...,
|
| 29 |
+
mac_len : int = ...,
|
| 30 |
+
initial_value : Union[int, Buffer] = ...,
|
| 31 |
+
counter : Dict = ...) -> \
|
| 32 |
+
Union[EcbMode, CbcMode, CfbMode, OfbMode, CtrMode, OpenPgpMode]: ...
|
| 33 |
+
|
| 34 |
+
block_size: int
|
| 35 |
+
key_size : Iterable[int]
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/ChaCha20_Poly1305.py
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ===================================================================
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2018, Helder Eijs <helderijs@gmail.com>
|
| 4 |
+
# All rights reserved.
|
| 5 |
+
#
|
| 6 |
+
# Redistribution and use in source and binary forms, with or without
|
| 7 |
+
# modification, are permitted provided that the following conditions
|
| 8 |
+
# are met:
|
| 9 |
+
#
|
| 10 |
+
# 1. Redistributions of source code must retain the above copyright
|
| 11 |
+
# notice, this list of conditions and the following disclaimer.
|
| 12 |
+
# 2. Redistributions in binary form must reproduce the above copyright
|
| 13 |
+
# notice, this list of conditions and the following disclaimer in
|
| 14 |
+
# the documentation and/or other materials provided with the
|
| 15 |
+
# distribution.
|
| 16 |
+
#
|
| 17 |
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 18 |
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 19 |
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
| 20 |
+
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
| 21 |
+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
| 22 |
+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
| 23 |
+
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
| 24 |
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 25 |
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
| 26 |
+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
| 27 |
+
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 28 |
+
# POSSIBILITY OF SUCH DAMAGE.
|
| 29 |
+
# ===================================================================
|
| 30 |
+
|
| 31 |
+
from binascii import unhexlify
|
| 32 |
+
|
| 33 |
+
from Crypto.Cipher import ChaCha20
|
| 34 |
+
from Crypto.Cipher.ChaCha20 import _HChaCha20
|
| 35 |
+
from Crypto.Hash import Poly1305, BLAKE2s
|
| 36 |
+
|
| 37 |
+
from Crypto.Random import get_random_bytes
|
| 38 |
+
|
| 39 |
+
from Crypto.Util.number import long_to_bytes
|
| 40 |
+
from Crypto.Util.py3compat import _copy_bytes, bord
|
| 41 |
+
from Crypto.Util._raw_api import is_buffer
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _enum(**enums):
|
| 45 |
+
return type('Enum', (), enums)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
_CipherStatus = _enum(PROCESSING_AUTH_DATA=1,
|
| 49 |
+
PROCESSING_CIPHERTEXT=2,
|
| 50 |
+
PROCESSING_DONE=3)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class ChaCha20Poly1305Cipher(object):
|
| 54 |
+
"""ChaCha20-Poly1305 and XChaCha20-Poly1305 cipher object.
|
| 55 |
+
Do not create it directly. Use :py:func:`new` instead.
|
| 56 |
+
|
| 57 |
+
:var nonce: The nonce with length 8, 12 or 24 bytes
|
| 58 |
+
:vartype nonce: byte string
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
def __init__(self, key, nonce):
|
| 62 |
+
"""Initialize a ChaCha20-Poly1305 AEAD cipher object
|
| 63 |
+
|
| 64 |
+
See also `new()` at the module level."""
|
| 65 |
+
|
| 66 |
+
self._next = ("update", "encrypt", "decrypt", "digest",
|
| 67 |
+
"verify")
|
| 68 |
+
|
| 69 |
+
self._authenticator = Poly1305.new(key=key, nonce=nonce, cipher=ChaCha20)
|
| 70 |
+
|
| 71 |
+
self._cipher = ChaCha20.new(key=key, nonce=nonce)
|
| 72 |
+
self._cipher.seek(64) # Block counter starts at 1
|
| 73 |
+
|
| 74 |
+
self._len_aad = 0
|
| 75 |
+
self._len_ct = 0
|
| 76 |
+
self._mac_tag = None
|
| 77 |
+
self._status = _CipherStatus.PROCESSING_AUTH_DATA
|
| 78 |
+
|
| 79 |
+
def update(self, data):
|
| 80 |
+
"""Protect the associated data.
|
| 81 |
+
|
| 82 |
+
Associated data (also known as *additional authenticated data* - AAD)
|
| 83 |
+
is the piece of the message that must stay in the clear, while
|
| 84 |
+
still allowing the receiver to verify its integrity.
|
| 85 |
+
An example is packet headers.
|
| 86 |
+
|
| 87 |
+
The associated data (possibly split into multiple segments) is
|
| 88 |
+
fed into :meth:`update` before any call to :meth:`decrypt` or :meth:`encrypt`.
|
| 89 |
+
If there is no associated data, :meth:`update` is not called.
|
| 90 |
+
|
| 91 |
+
:param bytes/bytearray/memoryview assoc_data:
|
| 92 |
+
A piece of associated data. There are no restrictions on its size.
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
if "update" not in self._next:
|
| 96 |
+
raise TypeError("update() method cannot be called")
|
| 97 |
+
|
| 98 |
+
self._len_aad += len(data)
|
| 99 |
+
self._authenticator.update(data)
|
| 100 |
+
|
| 101 |
+
def _pad_aad(self):
|
| 102 |
+
|
| 103 |
+
assert(self._status == _CipherStatus.PROCESSING_AUTH_DATA)
|
| 104 |
+
if self._len_aad & 0x0F:
|
| 105 |
+
self._authenticator.update(b'\x00' * (16 - (self._len_aad & 0x0F)))
|
| 106 |
+
self._status = _CipherStatus.PROCESSING_CIPHERTEXT
|
| 107 |
+
|
| 108 |
+
def encrypt(self, plaintext, output=None):
|
| 109 |
+
"""Encrypt a piece of data.
|
| 110 |
+
|
| 111 |
+
Args:
|
| 112 |
+
plaintext(bytes/bytearray/memoryview): The data to encrypt, of any size.
|
| 113 |
+
Keyword Args:
|
| 114 |
+
output(bytes/bytearray/memoryview): The location where the ciphertext
|
| 115 |
+
is written to. If ``None``, the ciphertext is returned.
|
| 116 |
+
Returns:
|
| 117 |
+
If ``output`` is ``None``, the ciphertext is returned as ``bytes``.
|
| 118 |
+
Otherwise, ``None``.
|
| 119 |
+
"""
|
| 120 |
+
|
| 121 |
+
if "encrypt" not in self._next:
|
| 122 |
+
raise TypeError("encrypt() method cannot be called")
|
| 123 |
+
|
| 124 |
+
if self._status == _CipherStatus.PROCESSING_AUTH_DATA:
|
| 125 |
+
self._pad_aad()
|
| 126 |
+
|
| 127 |
+
self._next = ("encrypt", "digest")
|
| 128 |
+
|
| 129 |
+
result = self._cipher.encrypt(plaintext, output=output)
|
| 130 |
+
self._len_ct += len(plaintext)
|
| 131 |
+
if output is None:
|
| 132 |
+
self._authenticator.update(result)
|
| 133 |
+
else:
|
| 134 |
+
self._authenticator.update(output)
|
| 135 |
+
return result
|
| 136 |
+
|
| 137 |
+
def decrypt(self, ciphertext, output=None):
|
| 138 |
+
"""Decrypt a piece of data.
|
| 139 |
+
|
| 140 |
+
Args:
|
| 141 |
+
ciphertext(bytes/bytearray/memoryview): The data to decrypt, of any size.
|
| 142 |
+
Keyword Args:
|
| 143 |
+
output(bytes/bytearray/memoryview): The location where the plaintext
|
| 144 |
+
is written to. If ``None``, the plaintext is returned.
|
| 145 |
+
Returns:
|
| 146 |
+
If ``output`` is ``None``, the plaintext is returned as ``bytes``.
|
| 147 |
+
Otherwise, ``None``.
|
| 148 |
+
"""
|
| 149 |
+
|
| 150 |
+
if "decrypt" not in self._next:
|
| 151 |
+
raise TypeError("decrypt() method cannot be called")
|
| 152 |
+
|
| 153 |
+
if self._status == _CipherStatus.PROCESSING_AUTH_DATA:
|
| 154 |
+
self._pad_aad()
|
| 155 |
+
|
| 156 |
+
self._next = ("decrypt", "verify")
|
| 157 |
+
|
| 158 |
+
self._len_ct += len(ciphertext)
|
| 159 |
+
self._authenticator.update(ciphertext)
|
| 160 |
+
return self._cipher.decrypt(ciphertext, output=output)
|
| 161 |
+
|
| 162 |
+
def _compute_mac(self):
|
| 163 |
+
"""Finalize the cipher (if not done already) and return the MAC."""
|
| 164 |
+
|
| 165 |
+
if self._mac_tag:
|
| 166 |
+
assert(self._status == _CipherStatus.PROCESSING_DONE)
|
| 167 |
+
return self._mac_tag
|
| 168 |
+
|
| 169 |
+
assert(self._status != _CipherStatus.PROCESSING_DONE)
|
| 170 |
+
|
| 171 |
+
if self._status == _CipherStatus.PROCESSING_AUTH_DATA:
|
| 172 |
+
self._pad_aad()
|
| 173 |
+
|
| 174 |
+
if self._len_ct & 0x0F:
|
| 175 |
+
self._authenticator.update(b'\x00' * (16 - (self._len_ct & 0x0F)))
|
| 176 |
+
|
| 177 |
+
self._status = _CipherStatus.PROCESSING_DONE
|
| 178 |
+
|
| 179 |
+
self._authenticator.update(long_to_bytes(self._len_aad, 8)[::-1])
|
| 180 |
+
self._authenticator.update(long_to_bytes(self._len_ct, 8)[::-1])
|
| 181 |
+
self._mac_tag = self._authenticator.digest()
|
| 182 |
+
return self._mac_tag
|
| 183 |
+
|
| 184 |
+
def digest(self):
|
| 185 |
+
"""Compute the *binary* authentication tag (MAC).
|
| 186 |
+
|
| 187 |
+
:Return: the MAC tag, as 16 ``bytes``.
|
| 188 |
+
"""
|
| 189 |
+
|
| 190 |
+
if "digest" not in self._next:
|
| 191 |
+
raise TypeError("digest() method cannot be called")
|
| 192 |
+
self._next = ("digest",)
|
| 193 |
+
|
| 194 |
+
return self._compute_mac()
|
| 195 |
+
|
| 196 |
+
def hexdigest(self):
|
| 197 |
+
"""Compute the *printable* authentication tag (MAC).
|
| 198 |
+
|
| 199 |
+
This method is like :meth:`digest`.
|
| 200 |
+
|
| 201 |
+
:Return: the MAC tag, as a hexadecimal string.
|
| 202 |
+
"""
|
| 203 |
+
return "".join(["%02x" % bord(x) for x in self.digest()])
|
| 204 |
+
|
| 205 |
+
def verify(self, received_mac_tag):
|
| 206 |
+
"""Validate the *binary* authentication tag (MAC).
|
| 207 |
+
|
| 208 |
+
The receiver invokes this method at the very end, to
|
| 209 |
+
check if the associated data (if any) and the decrypted
|
| 210 |
+
messages are valid.
|
| 211 |
+
|
| 212 |
+
:param bytes/bytearray/memoryview received_mac_tag:
|
| 213 |
+
This is the 16-byte *binary* MAC, as received from the sender.
|
| 214 |
+
:Raises ValueError:
|
| 215 |
+
if the MAC does not match. The message has been tampered with
|
| 216 |
+
or the key is incorrect.
|
| 217 |
+
"""
|
| 218 |
+
|
| 219 |
+
if "verify" not in self._next:
|
| 220 |
+
raise TypeError("verify() cannot be called"
|
| 221 |
+
" when encrypting a message")
|
| 222 |
+
self._next = ("verify",)
|
| 223 |
+
|
| 224 |
+
secret = get_random_bytes(16)
|
| 225 |
+
|
| 226 |
+
self._compute_mac()
|
| 227 |
+
|
| 228 |
+
mac1 = BLAKE2s.new(digest_bits=160, key=secret,
|
| 229 |
+
data=self._mac_tag)
|
| 230 |
+
mac2 = BLAKE2s.new(digest_bits=160, key=secret,
|
| 231 |
+
data=received_mac_tag)
|
| 232 |
+
|
| 233 |
+
if mac1.digest() != mac2.digest():
|
| 234 |
+
raise ValueError("MAC check failed")
|
| 235 |
+
|
| 236 |
+
def hexverify(self, hex_mac_tag):
|
| 237 |
+
"""Validate the *printable* authentication tag (MAC).
|
| 238 |
+
|
| 239 |
+
This method is like :meth:`verify`.
|
| 240 |
+
|
| 241 |
+
:param string hex_mac_tag:
|
| 242 |
+
This is the *printable* MAC.
|
| 243 |
+
:Raises ValueError:
|
| 244 |
+
if the MAC does not match. The message has been tampered with
|
| 245 |
+
or the key is incorrect.
|
| 246 |
+
"""
|
| 247 |
+
|
| 248 |
+
self.verify(unhexlify(hex_mac_tag))
|
| 249 |
+
|
| 250 |
+
def encrypt_and_digest(self, plaintext):
|
| 251 |
+
"""Perform :meth:`encrypt` and :meth:`digest` in one step.
|
| 252 |
+
|
| 253 |
+
:param plaintext: The data to encrypt, of any size.
|
| 254 |
+
:type plaintext: bytes/bytearray/memoryview
|
| 255 |
+
:return: a tuple with two ``bytes`` objects:
|
| 256 |
+
|
| 257 |
+
- the ciphertext, of equal length as the plaintext
|
| 258 |
+
- the 16-byte MAC tag
|
| 259 |
+
"""
|
| 260 |
+
|
| 261 |
+
return self.encrypt(plaintext), self.digest()
|
| 262 |
+
|
| 263 |
+
def decrypt_and_verify(self, ciphertext, received_mac_tag):
|
| 264 |
+
"""Perform :meth:`decrypt` and :meth:`verify` in one step.
|
| 265 |
+
|
| 266 |
+
:param ciphertext: The piece of data to decrypt.
|
| 267 |
+
:type ciphertext: bytes/bytearray/memoryview
|
| 268 |
+
:param bytes received_mac_tag:
|
| 269 |
+
This is the 16-byte *binary* MAC, as received from the sender.
|
| 270 |
+
:return: the decrypted data (as ``bytes``)
|
| 271 |
+
:raises ValueError:
|
| 272 |
+
if the MAC does not match. The message has been tampered with
|
| 273 |
+
or the key is incorrect.
|
| 274 |
+
"""
|
| 275 |
+
|
| 276 |
+
plaintext = self.decrypt(ciphertext)
|
| 277 |
+
self.verify(received_mac_tag)
|
| 278 |
+
return plaintext
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
def new(**kwargs):
|
| 282 |
+
"""Create a new ChaCha20-Poly1305 or XChaCha20-Poly1305 AEAD cipher.
|
| 283 |
+
|
| 284 |
+
:keyword key: The secret key to use. It must be 32 bytes long.
|
| 285 |
+
:type key: byte string
|
| 286 |
+
|
| 287 |
+
:keyword nonce:
|
| 288 |
+
A value that must never be reused for any other encryption
|
| 289 |
+
done with this key.
|
| 290 |
+
|
| 291 |
+
For ChaCha20-Poly1305, it must be 8 or 12 bytes long.
|
| 292 |
+
|
| 293 |
+
For XChaCha20-Poly1305, it must be 24 bytes long.
|
| 294 |
+
|
| 295 |
+
If not provided, 12 ``bytes`` will be generated randomly
|
| 296 |
+
(you can find them back in the ``nonce`` attribute).
|
| 297 |
+
:type nonce: bytes, bytearray, memoryview
|
| 298 |
+
|
| 299 |
+
:Return: a :class:`Crypto.Cipher.ChaCha20.ChaCha20Poly1305Cipher` object
|
| 300 |
+
"""
|
| 301 |
+
|
| 302 |
+
try:
|
| 303 |
+
key = kwargs.pop("key")
|
| 304 |
+
except KeyError as e:
|
| 305 |
+
raise TypeError("Missing parameter %s" % e)
|
| 306 |
+
|
| 307 |
+
if len(key) != 32:
|
| 308 |
+
raise ValueError("Key must be 32 bytes long")
|
| 309 |
+
|
| 310 |
+
nonce = kwargs.pop("nonce", None)
|
| 311 |
+
if nonce is None:
|
| 312 |
+
nonce = get_random_bytes(12)
|
| 313 |
+
|
| 314 |
+
if len(nonce) in (8, 12):
|
| 315 |
+
chacha20_poly1305_nonce = nonce
|
| 316 |
+
elif len(nonce) == 24:
|
| 317 |
+
key = _HChaCha20(key, nonce[:16])
|
| 318 |
+
chacha20_poly1305_nonce = b'\x00\x00\x00\x00' + nonce[16:]
|
| 319 |
+
else:
|
| 320 |
+
raise ValueError("Nonce must be 8, 12 or 24 bytes long")
|
| 321 |
+
|
| 322 |
+
if not is_buffer(nonce):
|
| 323 |
+
raise TypeError("nonce must be bytes, bytearray or memoryview")
|
| 324 |
+
|
| 325 |
+
if kwargs:
|
| 326 |
+
raise TypeError("Unknown parameters: " + str(kwargs))
|
| 327 |
+
|
| 328 |
+
cipher = ChaCha20Poly1305Cipher(key, chacha20_poly1305_nonce)
|
| 329 |
+
cipher.nonce = _copy_bytes(None, None, nonce)
|
| 330 |
+
return cipher
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
# Size of a key (in bytes)
|
| 334 |
+
key_size = 32
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/DES3.py
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Cipher/DES3.py : DES3
|
| 4 |
+
#
|
| 5 |
+
# ===================================================================
|
| 6 |
+
# The contents of this file are dedicated to the public domain. To
|
| 7 |
+
# the extent that dedication to the public domain is not available,
|
| 8 |
+
# everyone is granted a worldwide, perpetual, royalty-free,
|
| 9 |
+
# non-exclusive license to exercise all rights associated with the
|
| 10 |
+
# contents of this file for any purpose whatsoever.
|
| 11 |
+
# No rights are reserved.
|
| 12 |
+
#
|
| 13 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 14 |
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 15 |
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
| 16 |
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
| 17 |
+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
| 18 |
+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 19 |
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 20 |
+
# SOFTWARE.
|
| 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 |
+
# Size of a data block (in bytes)
|
| 185 |
+
block_size = 8
|
| 186 |
+
# Size of a key (in bytes)
|
| 187 |
+
key_size = (16, 24)
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/PKCS1_OAEP.py
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Cipher/PKCS1_OAEP.py : PKCS#1 OAEP
|
| 4 |
+
#
|
| 5 |
+
# ===================================================================
|
| 6 |
+
# The contents of this file are dedicated to the public domain. To
|
| 7 |
+
# the extent that dedication to the public domain is not available,
|
| 8 |
+
# everyone is granted a worldwide, perpetual, royalty-free,
|
| 9 |
+
# non-exclusive license to exercise all rights associated with the
|
| 10 |
+
# contents of this file for any purpose whatsoever.
|
| 11 |
+
# No rights are reserved.
|
| 12 |
+
#
|
| 13 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 14 |
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 15 |
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
| 16 |
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
| 17 |
+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
| 18 |
+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 19 |
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 20 |
+
# SOFTWARE.
|
| 21 |
+
# ===================================================================
|
| 22 |
+
|
| 23 |
+
from Crypto.Signature.pss import MGF1
|
| 24 |
+
import Crypto.Hash.SHA1
|
| 25 |
+
|
| 26 |
+
from Crypto.Util.py3compat import _copy_bytes
|
| 27 |
+
import Crypto.Util.number
|
| 28 |
+
from Crypto.Util.number import ceil_div, bytes_to_long, long_to_bytes
|
| 29 |
+
from Crypto.Util.strxor import strxor
|
| 30 |
+
from Crypto import Random
|
| 31 |
+
from ._pkcs1_oaep_decode import oaep_decode
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class PKCS1OAEP_Cipher:
|
| 35 |
+
"""Cipher object for PKCS#1 v1.5 OAEP.
|
| 36 |
+
Do not create directly: use :func:`new` instead."""
|
| 37 |
+
|
| 38 |
+
def __init__(self, key, hashAlgo, mgfunc, label, randfunc):
|
| 39 |
+
"""Initialize this PKCS#1 OAEP cipher object.
|
| 40 |
+
|
| 41 |
+
:Parameters:
|
| 42 |
+
key : an RSA key object
|
| 43 |
+
If a private half is given, both encryption and decryption are possible.
|
| 44 |
+
If a public half is given, only encryption is possible.
|
| 45 |
+
hashAlgo : hash object
|
| 46 |
+
The hash function to use. This can be a module under `Crypto.Hash`
|
| 47 |
+
or an existing hash object created from any of such modules. If not specified,
|
| 48 |
+
`Crypto.Hash.SHA1` is used.
|
| 49 |
+
mgfunc : callable
|
| 50 |
+
A mask generation function that accepts two parameters: a string to
|
| 51 |
+
use as seed, and the lenth of the mask to generate, in bytes.
|
| 52 |
+
If not specified, the standard MGF1 consistent with ``hashAlgo`` is used (a safe choice).
|
| 53 |
+
label : bytes/bytearray/memoryview
|
| 54 |
+
A label to apply to this particular encryption. If not specified,
|
| 55 |
+
an empty string is used. Specifying a label does not improve
|
| 56 |
+
security.
|
| 57 |
+
randfunc : callable
|
| 58 |
+
A function that returns random bytes.
|
| 59 |
+
|
| 60 |
+
:attention: Modify the mask generation function only if you know what you are doing.
|
| 61 |
+
Sender and receiver must use the same one.
|
| 62 |
+
"""
|
| 63 |
+
self._key = key
|
| 64 |
+
|
| 65 |
+
if hashAlgo:
|
| 66 |
+
self._hashObj = hashAlgo
|
| 67 |
+
else:
|
| 68 |
+
self._hashObj = Crypto.Hash.SHA1
|
| 69 |
+
|
| 70 |
+
if mgfunc:
|
| 71 |
+
self._mgf = mgfunc
|
| 72 |
+
else:
|
| 73 |
+
self._mgf = lambda x, y: MGF1(x, y, self._hashObj)
|
| 74 |
+
|
| 75 |
+
self._label = _copy_bytes(None, None, label)
|
| 76 |
+
self._randfunc = randfunc
|
| 77 |
+
|
| 78 |
+
def can_encrypt(self):
|
| 79 |
+
"""Legacy function to check if you can call :meth:`encrypt`.
|
| 80 |
+
|
| 81 |
+
.. deprecated:: 3.0"""
|
| 82 |
+
return self._key.can_encrypt()
|
| 83 |
+
|
| 84 |
+
def can_decrypt(self):
|
| 85 |
+
"""Legacy function to check if you can call :meth:`decrypt`.
|
| 86 |
+
|
| 87 |
+
.. deprecated:: 3.0"""
|
| 88 |
+
return self._key.can_decrypt()
|
| 89 |
+
|
| 90 |
+
def encrypt(self, message):
|
| 91 |
+
"""Encrypt a message with PKCS#1 OAEP.
|
| 92 |
+
|
| 93 |
+
:param message:
|
| 94 |
+
The message to encrypt, also known as plaintext. It can be of
|
| 95 |
+
variable length, but not longer than the RSA modulus (in bytes)
|
| 96 |
+
minus 2, minus twice the hash output size.
|
| 97 |
+
For instance, if you use RSA 2048 and SHA-256, the longest message
|
| 98 |
+
you can encrypt is 190 byte long.
|
| 99 |
+
:type message: bytes/bytearray/memoryview
|
| 100 |
+
|
| 101 |
+
:returns: The ciphertext, as large as the RSA modulus.
|
| 102 |
+
:rtype: bytes
|
| 103 |
+
|
| 104 |
+
:raises ValueError:
|
| 105 |
+
if the message is too long.
|
| 106 |
+
"""
|
| 107 |
+
|
| 108 |
+
# See 7.1.1 in RFC3447
|
| 109 |
+
modBits = Crypto.Util.number.size(self._key.n)
|
| 110 |
+
k = ceil_div(modBits, 8) # Convert from bits to bytes
|
| 111 |
+
hLen = self._hashObj.digest_size
|
| 112 |
+
mLen = len(message)
|
| 113 |
+
|
| 114 |
+
# Step 1b
|
| 115 |
+
ps_len = k - mLen - 2 * hLen - 2
|
| 116 |
+
if ps_len < 0:
|
| 117 |
+
raise ValueError("Plaintext is too long.")
|
| 118 |
+
# Step 2a
|
| 119 |
+
lHash = self._hashObj.new(self._label).digest()
|
| 120 |
+
# Step 2b
|
| 121 |
+
ps = b'\x00' * ps_len
|
| 122 |
+
# Step 2c
|
| 123 |
+
db = lHash + ps + b'\x01' + _copy_bytes(None, None, message)
|
| 124 |
+
# Step 2d
|
| 125 |
+
ros = self._randfunc(hLen)
|
| 126 |
+
# Step 2e
|
| 127 |
+
dbMask = self._mgf(ros, k-hLen-1)
|
| 128 |
+
# Step 2f
|
| 129 |
+
maskedDB = strxor(db, dbMask)
|
| 130 |
+
# Step 2g
|
| 131 |
+
seedMask = self._mgf(maskedDB, hLen)
|
| 132 |
+
# Step 2h
|
| 133 |
+
maskedSeed = strxor(ros, seedMask)
|
| 134 |
+
# Step 2i
|
| 135 |
+
em = b'\x00' + maskedSeed + maskedDB
|
| 136 |
+
# Step 3a (OS2IP)
|
| 137 |
+
em_int = bytes_to_long(em)
|
| 138 |
+
# Step 3b (RSAEP)
|
| 139 |
+
m_int = self._key._encrypt(em_int)
|
| 140 |
+
# Step 3c (I2OSP)
|
| 141 |
+
c = long_to_bytes(m_int, k)
|
| 142 |
+
return c
|
| 143 |
+
|
| 144 |
+
def decrypt(self, ciphertext):
|
| 145 |
+
"""Decrypt a message with PKCS#1 OAEP.
|
| 146 |
+
|
| 147 |
+
:param ciphertext: The encrypted message.
|
| 148 |
+
:type ciphertext: bytes/bytearray/memoryview
|
| 149 |
+
|
| 150 |
+
:returns: The original message (plaintext).
|
| 151 |
+
:rtype: bytes
|
| 152 |
+
|
| 153 |
+
:raises ValueError:
|
| 154 |
+
if the ciphertext has the wrong length, or if decryption
|
| 155 |
+
fails the integrity check (in which case, the decryption
|
| 156 |
+
key is probably wrong).
|
| 157 |
+
:raises TypeError:
|
| 158 |
+
if the RSA key has no private half (i.e. you are trying
|
| 159 |
+
to decrypt using a public key).
|
| 160 |
+
"""
|
| 161 |
+
|
| 162 |
+
# See 7.1.2 in RFC3447
|
| 163 |
+
modBits = Crypto.Util.number.size(self._key.n)
|
| 164 |
+
k = ceil_div(modBits, 8) # Convert from bits to bytes
|
| 165 |
+
hLen = self._hashObj.digest_size
|
| 166 |
+
|
| 167 |
+
# Step 1b and 1c
|
| 168 |
+
if len(ciphertext) != k or k < hLen+2:
|
| 169 |
+
raise ValueError("Ciphertext with incorrect length.")
|
| 170 |
+
# Step 2a (O2SIP)
|
| 171 |
+
ct_int = bytes_to_long(ciphertext)
|
| 172 |
+
# Step 2b (RSADP) and step 2c (I2OSP)
|
| 173 |
+
em = self._key._decrypt_to_bytes(ct_int)
|
| 174 |
+
# Step 3a
|
| 175 |
+
lHash = self._hashObj.new(self._label).digest()
|
| 176 |
+
# y must be 0, but we MUST NOT check it here in order not to
|
| 177 |
+
# allow attacks like Manger's (http://dl.acm.org/citation.cfm?id=704143)
|
| 178 |
+
maskedSeed = em[1:hLen+1]
|
| 179 |
+
maskedDB = em[hLen+1:]
|
| 180 |
+
# Step 3c
|
| 181 |
+
seedMask = self._mgf(maskedDB, hLen)
|
| 182 |
+
# Step 3d
|
| 183 |
+
seed = strxor(maskedSeed, seedMask)
|
| 184 |
+
# Step 3e
|
| 185 |
+
dbMask = self._mgf(seed, k-hLen-1)
|
| 186 |
+
# Step 3f
|
| 187 |
+
db = strxor(maskedDB, dbMask)
|
| 188 |
+
# Step 3b + 3g
|
| 189 |
+
res = oaep_decode(em, lHash, db)
|
| 190 |
+
if res <= 0:
|
| 191 |
+
raise ValueError("Incorrect decryption.")
|
| 192 |
+
# Step 4
|
| 193 |
+
return db[res:]
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def new(key, hashAlgo=None, mgfunc=None, label=b'', randfunc=None):
|
| 197 |
+
"""Return a cipher object :class:`PKCS1OAEP_Cipher`
|
| 198 |
+
that can be used to perform PKCS#1 OAEP encryption or decryption.
|
| 199 |
+
|
| 200 |
+
:param key:
|
| 201 |
+
The key object to use to encrypt or decrypt the message.
|
| 202 |
+
Decryption is only possible with a private RSA key.
|
| 203 |
+
:type key: RSA key object
|
| 204 |
+
|
| 205 |
+
:param hashAlgo:
|
| 206 |
+
The hash function to use. This can be a module under `Crypto.Hash`
|
| 207 |
+
or an existing hash object created from any of such modules.
|
| 208 |
+
If not specified, `Crypto.Hash.SHA1` is used.
|
| 209 |
+
:type hashAlgo: hash object
|
| 210 |
+
|
| 211 |
+
:param mgfunc:
|
| 212 |
+
A mask generation function that accepts two parameters: a string to
|
| 213 |
+
use as seed, and the lenth of the mask to generate, in bytes.
|
| 214 |
+
If not specified, the standard MGF1 consistent with ``hashAlgo`` is used (a safe choice).
|
| 215 |
+
:type mgfunc: callable
|
| 216 |
+
|
| 217 |
+
:param label:
|
| 218 |
+
A label to apply to this particular encryption. If not specified,
|
| 219 |
+
an empty string is used. Specifying a label does not improve
|
| 220 |
+
security.
|
| 221 |
+
:type label: bytes/bytearray/memoryview
|
| 222 |
+
|
| 223 |
+
:param randfunc:
|
| 224 |
+
A function that returns random bytes.
|
| 225 |
+
The default is `Random.get_random_bytes`.
|
| 226 |
+
:type randfunc: callable
|
| 227 |
+
"""
|
| 228 |
+
|
| 229 |
+
if randfunc is None:
|
| 230 |
+
randfunc = Random.get_random_bytes
|
| 231 |
+
return PKCS1OAEP_Cipher(key, hashAlgo, mgfunc, label, randfunc)
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/PKCS1_v1_5.pyi
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Callable, Union, Any, Optional, TypeVar
|
| 2 |
+
|
| 3 |
+
from Crypto.PublicKey.RSA import RsaKey
|
| 4 |
+
|
| 5 |
+
Buffer = Union[bytes, bytearray, memoryview]
|
| 6 |
+
T = TypeVar('T')
|
| 7 |
+
|
| 8 |
+
class PKCS115_Cipher:
|
| 9 |
+
def __init__(self,
|
| 10 |
+
key: RsaKey,
|
| 11 |
+
randfunc: Callable[[int], bytes]) -> None: ...
|
| 12 |
+
def can_encrypt(self) -> bool: ...
|
| 13 |
+
def can_decrypt(self) -> bool: ...
|
| 14 |
+
def encrypt(self, message: Buffer) -> bytes: ...
|
| 15 |
+
def decrypt(self, ciphertext: Buffer,
|
| 16 |
+
sentinel: T,
|
| 17 |
+
expected_pt_len: Optional[int] = ...) -> Union[bytes, T]: ...
|
| 18 |
+
|
| 19 |
+
def new(key: RsaKey,
|
| 20 |
+
randfunc: Optional[Callable[[int], bytes]] = ...) -> PKCS115_Cipher: ...
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/Salsa20.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Cipher/Salsa20.py : Salsa20 stream cipher (http://cr.yp.to/snuffle.html)
|
| 4 |
+
#
|
| 5 |
+
# Contributed by Fabrizio Tarizzo <fabrizio@fabriziotarizzo.org>.
|
| 6 |
+
#
|
| 7 |
+
# ===================================================================
|
| 8 |
+
# The contents of this file are dedicated to the public domain. To
|
| 9 |
+
# the extent that dedication to the public domain is not available,
|
| 10 |
+
# everyone is granted a worldwide, perpetual, royalty-free,
|
| 11 |
+
# non-exclusive license to exercise all rights associated with the
|
| 12 |
+
# contents of this file for any purpose whatsoever.
|
| 13 |
+
# No rights are reserved.
|
| 14 |
+
#
|
| 15 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 16 |
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 17 |
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
| 18 |
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
| 19 |
+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
| 20 |
+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 21 |
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 22 |
+
# SOFTWARE.
|
| 23 |
+
# ===================================================================
|
| 24 |
+
|
| 25 |
+
from Crypto.Util.py3compat import _copy_bytes
|
| 26 |
+
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
|
| 27 |
+
create_string_buffer,
|
| 28 |
+
get_raw_buffer, VoidPointer,
|
| 29 |
+
SmartPointer, c_size_t,
|
| 30 |
+
c_uint8_ptr, is_writeable_buffer)
|
| 31 |
+
|
| 32 |
+
from Crypto.Random import get_random_bytes
|
| 33 |
+
|
| 34 |
+
_raw_salsa20_lib = load_pycryptodome_raw_lib("Crypto.Cipher._Salsa20",
|
| 35 |
+
"""
|
| 36 |
+
int Salsa20_stream_init(uint8_t *key, size_t keylen,
|
| 37 |
+
uint8_t *nonce, size_t nonce_len,
|
| 38 |
+
void **pSalsaState);
|
| 39 |
+
int Salsa20_stream_destroy(void *salsaState);
|
| 40 |
+
int Salsa20_stream_encrypt(void *salsaState,
|
| 41 |
+
const uint8_t in[],
|
| 42 |
+
uint8_t out[], size_t len);
|
| 43 |
+
""")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class Salsa20Cipher:
|
| 47 |
+
"""Salsa20 cipher object. Do not create it directly. Use :py:func:`new`
|
| 48 |
+
instead.
|
| 49 |
+
|
| 50 |
+
:var nonce: The nonce with length 8
|
| 51 |
+
:vartype nonce: byte string
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
def __init__(self, key, nonce):
|
| 55 |
+
"""Initialize a Salsa20 cipher object
|
| 56 |
+
|
| 57 |
+
See also `new()` at the module level."""
|
| 58 |
+
|
| 59 |
+
if len(key) not in key_size:
|
| 60 |
+
raise ValueError("Incorrect key length for Salsa20 (%d bytes)" % len(key))
|
| 61 |
+
|
| 62 |
+
if len(nonce) != 8:
|
| 63 |
+
raise ValueError("Incorrect nonce length for Salsa20 (%d bytes)" %
|
| 64 |
+
len(nonce))
|
| 65 |
+
|
| 66 |
+
self.nonce = _copy_bytes(None, None, nonce)
|
| 67 |
+
|
| 68 |
+
self._state = VoidPointer()
|
| 69 |
+
result = _raw_salsa20_lib.Salsa20_stream_init(
|
| 70 |
+
c_uint8_ptr(key),
|
| 71 |
+
c_size_t(len(key)),
|
| 72 |
+
c_uint8_ptr(nonce),
|
| 73 |
+
c_size_t(len(nonce)),
|
| 74 |
+
self._state.address_of())
|
| 75 |
+
if result:
|
| 76 |
+
raise ValueError("Error %d instantiating a Salsa20 cipher")
|
| 77 |
+
self._state = SmartPointer(self._state.get(),
|
| 78 |
+
_raw_salsa20_lib.Salsa20_stream_destroy)
|
| 79 |
+
|
| 80 |
+
self.block_size = 1
|
| 81 |
+
self.key_size = len(key)
|
| 82 |
+
|
| 83 |
+
def encrypt(self, plaintext, output=None):
|
| 84 |
+
"""Encrypt a piece of data.
|
| 85 |
+
|
| 86 |
+
Args:
|
| 87 |
+
plaintext(bytes/bytearray/memoryview): The data to encrypt, of any size.
|
| 88 |
+
Keyword Args:
|
| 89 |
+
output(bytes/bytearray/memoryview): The location where the ciphertext
|
| 90 |
+
is written to. If ``None``, the ciphertext is returned.
|
| 91 |
+
Returns:
|
| 92 |
+
If ``output`` is ``None``, the ciphertext is returned as ``bytes``.
|
| 93 |
+
Otherwise, ``None``.
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
if output is None:
|
| 97 |
+
ciphertext = create_string_buffer(len(plaintext))
|
| 98 |
+
else:
|
| 99 |
+
ciphertext = output
|
| 100 |
+
|
| 101 |
+
if not is_writeable_buffer(output):
|
| 102 |
+
raise TypeError("output must be a bytearray or a writeable memoryview")
|
| 103 |
+
|
| 104 |
+
if len(plaintext) != len(output):
|
| 105 |
+
raise ValueError("output must have the same length as the input"
|
| 106 |
+
" (%d bytes)" % len(plaintext))
|
| 107 |
+
|
| 108 |
+
result = _raw_salsa20_lib.Salsa20_stream_encrypt(
|
| 109 |
+
self._state.get(),
|
| 110 |
+
c_uint8_ptr(plaintext),
|
| 111 |
+
c_uint8_ptr(ciphertext),
|
| 112 |
+
c_size_t(len(plaintext)))
|
| 113 |
+
if result:
|
| 114 |
+
raise ValueError("Error %d while encrypting with Salsa20" % result)
|
| 115 |
+
|
| 116 |
+
if output is None:
|
| 117 |
+
return get_raw_buffer(ciphertext)
|
| 118 |
+
else:
|
| 119 |
+
return None
|
| 120 |
+
|
| 121 |
+
def decrypt(self, ciphertext, output=None):
|
| 122 |
+
"""Decrypt a piece of data.
|
| 123 |
+
|
| 124 |
+
Args:
|
| 125 |
+
ciphertext(bytes/bytearray/memoryview): The data to decrypt, of any size.
|
| 126 |
+
Keyword Args:
|
| 127 |
+
output(bytes/bytearray/memoryview): The location where the plaintext
|
| 128 |
+
is written to. If ``None``, the plaintext is returned.
|
| 129 |
+
Returns:
|
| 130 |
+
If ``output`` is ``None``, the plaintext is returned as ``bytes``.
|
| 131 |
+
Otherwise, ``None``.
|
| 132 |
+
"""
|
| 133 |
+
|
| 134 |
+
try:
|
| 135 |
+
return self.encrypt(ciphertext, output=output)
|
| 136 |
+
except ValueError as e:
|
| 137 |
+
raise ValueError(str(e).replace("enc", "dec"))
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def new(key, nonce=None):
|
| 141 |
+
"""Create a new Salsa20 cipher
|
| 142 |
+
|
| 143 |
+
:keyword key: The secret key to use. It must be 16 or 32 bytes long.
|
| 144 |
+
:type key: bytes/bytearray/memoryview
|
| 145 |
+
|
| 146 |
+
:keyword nonce:
|
| 147 |
+
A value that must never be reused for any other encryption
|
| 148 |
+
done with this key. It must be 8 bytes long.
|
| 149 |
+
|
| 150 |
+
If not provided, a random byte string will be generated (you can read
|
| 151 |
+
it back via the ``nonce`` attribute of the returned object).
|
| 152 |
+
:type nonce: bytes/bytearray/memoryview
|
| 153 |
+
|
| 154 |
+
:Return: a :class:`Crypto.Cipher.Salsa20.Salsa20Cipher` object
|
| 155 |
+
"""
|
| 156 |
+
|
| 157 |
+
if nonce is None:
|
| 158 |
+
nonce = get_random_bytes(8)
|
| 159 |
+
|
| 160 |
+
return Salsa20Cipher(key, nonce)
|
| 161 |
+
|
| 162 |
+
# Size of a data block (in bytes)
|
| 163 |
+
block_size = 1
|
| 164 |
+
|
| 165 |
+
# Size of a key (in bytes)
|
| 166 |
+
key_size = (16, 32)
|
| 167 |
+
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/Salsa20.pyi
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Union, Tuple, Optional, overload, Optional
|
| 2 |
+
|
| 3 |
+
Buffer = bytes|bytearray|memoryview
|
| 4 |
+
|
| 5 |
+
class Salsa20Cipher:
|
| 6 |
+
nonce: bytes
|
| 7 |
+
block_size: int
|
| 8 |
+
key_size: int
|
| 9 |
+
|
| 10 |
+
def __init__(self,
|
| 11 |
+
key: Buffer,
|
| 12 |
+
nonce: Buffer) -> None: ...
|
| 13 |
+
@overload
|
| 14 |
+
def encrypt(self, plaintext: Buffer) -> bytes: ...
|
| 15 |
+
@overload
|
| 16 |
+
def encrypt(self, plaintext: Buffer, output: Union[bytearray, memoryview]) -> None: ...
|
| 17 |
+
@overload
|
| 18 |
+
def decrypt(self, plaintext: Buffer) -> bytes: ...
|
| 19 |
+
@overload
|
| 20 |
+
def decrypt(self, plaintext: Buffer, output: Union[bytearray, memoryview]) -> None: ...
|
| 21 |
+
|
| 22 |
+
def new(key: Buffer, nonce: Optional[Buffer] = ...) -> Salsa20Cipher: ...
|
| 23 |
+
|
| 24 |
+
block_size: int
|
| 25 |
+
key_size: Tuple[int, int]
|
| 26 |
+
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__init__.pyi
ADDED
|
File without changes
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/AES.cpython-310.pyc
ADDED
|
Binary file (7.05 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/ARC2.cpython-310.pyc
ADDED
|
Binary file (5.84 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/ARC4.cpython-310.pyc
ADDED
|
Binary file (3.68 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/Blowfish.cpython-310.pyc
ADDED
|
Binary file (4.98 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/CAST.cpython-310.pyc
ADDED
|
Binary file (5.09 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/ChaCha20.cpython-310.pyc
ADDED
|
Binary file (7.4 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/ChaCha20_Poly1305.cpython-310.pyc
ADDED
|
Binary file (9.87 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/DES.cpython-310.pyc
ADDED
|
Binary file (4.97 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/DES3.cpython-310.pyc
ADDED
|
Binary file (6.19 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/PKCS1_OAEP.cpython-310.pyc
ADDED
|
Binary file (6.95 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/PKCS1_v1_5.cpython-310.pyc
ADDED
|
Binary file (5.88 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/Salsa20.cpython-310.pyc
ADDED
|
Binary file (4.59 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_EKSBlowfish.cpython-310.pyc
ADDED
|
Binary file (3.46 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (1.69 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_cbc.cpython-310.pyc
ADDED
|
Binary file (7.47 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_ccm.cpython-310.pyc
ADDED
|
Binary file (17.9 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_cfb.cpython-310.pyc
ADDED
|
Binary file (7.79 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_ctr.cpython-310.pyc
ADDED
|
Binary file (11 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_eax.cpython-310.pyc
ADDED
|
Binary file (12.3 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_ecb.cpython-310.pyc
ADDED
|
Binary file (5.82 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_gcm.cpython-310.pyc
ADDED
|
Binary file (17.1 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_ocb.cpython-310.pyc
ADDED
|
Binary file (14.7 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_ofb.cpython-310.pyc
ADDED
|
Binary file (7.36 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_openpgp.cpython-310.pyc
ADDED
|
Binary file (4.82 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_mode_siv.cpython-310.pyc
ADDED
|
Binary file (11.8 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/__pycache__/_pkcs1_oaep_decode.cpython-310.pyc
ADDED
|
Binary file (1.53 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_chacha20.abi3.so
ADDED
|
Binary file (30.6 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_cbc.py
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ===================================================================
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2014, Legrandin <helderijs@gmail.com>
|
| 4 |
+
# All rights reserved.
|
| 5 |
+
#
|
| 6 |
+
# Redistribution and use in source and binary forms, with or without
|
| 7 |
+
# modification, are permitted provided that the following conditions
|
| 8 |
+
# are met:
|
| 9 |
+
#
|
| 10 |
+
# 1. Redistributions of source code must retain the above copyright
|
| 11 |
+
# notice, this list of conditions and the following disclaimer.
|
| 12 |
+
# 2. Redistributions in binary form must reproduce the above copyright
|
| 13 |
+
# notice, this list of conditions and the following disclaimer in
|
| 14 |
+
# the documentation and/or other materials provided with the
|
| 15 |
+
# distribution.
|
| 16 |
+
#
|
| 17 |
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 18 |
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 19 |
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
| 20 |
+
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
| 21 |
+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
| 22 |
+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
| 23 |
+
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
| 24 |
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 25 |
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
| 26 |
+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
| 27 |
+
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 28 |
+
# POSSIBILITY OF SUCH DAMAGE.
|
| 29 |
+
# ===================================================================
|
| 30 |
+
|
| 31 |
+
"""
|
| 32 |
+
Ciphertext Block Chaining (CBC) mode.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
__all__ = ['CbcMode']
|
| 36 |
+
|
| 37 |
+
from Crypto.Util.py3compat import _copy_bytes
|
| 38 |
+
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, VoidPointer,
|
| 39 |
+
create_string_buffer, get_raw_buffer,
|
| 40 |
+
SmartPointer, c_size_t, c_uint8_ptr,
|
| 41 |
+
is_writeable_buffer)
|
| 42 |
+
|
| 43 |
+
from Crypto.Random import get_random_bytes
|
| 44 |
+
|
| 45 |
+
raw_cbc_lib = load_pycryptodome_raw_lib("Crypto.Cipher._raw_cbc", """
|
| 46 |
+
int CBC_start_operation(void *cipher,
|
| 47 |
+
const uint8_t iv[],
|
| 48 |
+
size_t iv_len,
|
| 49 |
+
void **pResult);
|
| 50 |
+
int CBC_encrypt(void *cbcState,
|
| 51 |
+
const uint8_t *in,
|
| 52 |
+
uint8_t *out,
|
| 53 |
+
size_t data_len);
|
| 54 |
+
int CBC_decrypt(void *cbcState,
|
| 55 |
+
const uint8_t *in,
|
| 56 |
+
uint8_t *out,
|
| 57 |
+
size_t data_len);
|
| 58 |
+
int CBC_stop_operation(void *state);
|
| 59 |
+
"""
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class CbcMode(object):
|
| 64 |
+
"""*Cipher-Block Chaining (CBC)*.
|
| 65 |
+
|
| 66 |
+
Each of the ciphertext blocks depends on the current
|
| 67 |
+
and all previous plaintext blocks.
|
| 68 |
+
|
| 69 |
+
An Initialization Vector (*IV*) is required.
|
| 70 |
+
|
| 71 |
+
See `NIST SP800-38A`_ , Section 6.2 .
|
| 72 |
+
|
| 73 |
+
.. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
|
| 74 |
+
|
| 75 |
+
:undocumented: __init__
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
def __init__(self, block_cipher, iv):
|
| 79 |
+
"""Create a new block cipher, configured in CBC mode.
|
| 80 |
+
|
| 81 |
+
:Parameters:
|
| 82 |
+
block_cipher : C pointer
|
| 83 |
+
A smart pointer to the low-level block cipher instance.
|
| 84 |
+
|
| 85 |
+
iv : bytes/bytearray/memoryview
|
| 86 |
+
The initialization vector to use for encryption or decryption.
|
| 87 |
+
It is as long as the cipher block.
|
| 88 |
+
|
| 89 |
+
**The IV must be unpredictable**. Ideally it is picked randomly.
|
| 90 |
+
|
| 91 |
+
Reusing the *IV* for encryptions performed with the same key
|
| 92 |
+
compromises confidentiality.
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
self._state = VoidPointer()
|
| 96 |
+
result = raw_cbc_lib.CBC_start_operation(block_cipher.get(),
|
| 97 |
+
c_uint8_ptr(iv),
|
| 98 |
+
c_size_t(len(iv)),
|
| 99 |
+
self._state.address_of())
|
| 100 |
+
if result:
|
| 101 |
+
raise ValueError("Error %d while instantiating the CBC mode"
|
| 102 |
+
% result)
|
| 103 |
+
|
| 104 |
+
# Ensure that object disposal of this Python object will (eventually)
|
| 105 |
+
# free the memory allocated by the raw library for the cipher mode
|
| 106 |
+
self._state = SmartPointer(self._state.get(),
|
| 107 |
+
raw_cbc_lib.CBC_stop_operation)
|
| 108 |
+
|
| 109 |
+
# Memory allocated for the underlying block cipher is now owed
|
| 110 |
+
# by the cipher mode
|
| 111 |
+
block_cipher.release()
|
| 112 |
+
|
| 113 |
+
self.block_size = len(iv)
|
| 114 |
+
"""The block size of the underlying cipher, in bytes."""
|
| 115 |
+
|
| 116 |
+
self.iv = _copy_bytes(None, None, iv)
|
| 117 |
+
"""The Initialization Vector originally used to create the object.
|
| 118 |
+
The value does not change."""
|
| 119 |
+
|
| 120 |
+
self.IV = self.iv
|
| 121 |
+
"""Alias for `iv`"""
|
| 122 |
+
|
| 123 |
+
self._next = ["encrypt", "decrypt"]
|
| 124 |
+
|
| 125 |
+
def encrypt(self, plaintext, output=None):
|
| 126 |
+
"""Encrypt data with the key and the parameters set at initialization.
|
| 127 |
+
|
| 128 |
+
A cipher object is stateful: once you have encrypted a message
|
| 129 |
+
you cannot encrypt (or decrypt) another message using the same
|
| 130 |
+
object.
|
| 131 |
+
|
| 132 |
+
The data to encrypt can be broken up in two or
|
| 133 |
+
more pieces and `encrypt` can be called multiple times.
|
| 134 |
+
|
| 135 |
+
That is, the statement:
|
| 136 |
+
|
| 137 |
+
>>> c.encrypt(a) + c.encrypt(b)
|
| 138 |
+
|
| 139 |
+
is equivalent to:
|
| 140 |
+
|
| 141 |
+
>>> c.encrypt(a+b)
|
| 142 |
+
|
| 143 |
+
That also means that you cannot reuse an object for encrypting
|
| 144 |
+
or decrypting other data with the same key.
|
| 145 |
+
|
| 146 |
+
This function does not add any padding to the plaintext.
|
| 147 |
+
|
| 148 |
+
:Parameters:
|
| 149 |
+
plaintext : bytes/bytearray/memoryview
|
| 150 |
+
The piece of data to encrypt.
|
| 151 |
+
Its lenght must be multiple of the cipher block size.
|
| 152 |
+
:Keywords:
|
| 153 |
+
output : bytearray/memoryview
|
| 154 |
+
The location where the ciphertext must be written to.
|
| 155 |
+
If ``None``, the ciphertext is returned.
|
| 156 |
+
:Return:
|
| 157 |
+
If ``output`` is ``None``, the ciphertext is returned as ``bytes``.
|
| 158 |
+
Otherwise, ``None``.
|
| 159 |
+
"""
|
| 160 |
+
|
| 161 |
+
if "encrypt" not in self._next:
|
| 162 |
+
raise TypeError("encrypt() cannot be called after decrypt()")
|
| 163 |
+
self._next = ["encrypt"]
|
| 164 |
+
|
| 165 |
+
if output is None:
|
| 166 |
+
ciphertext = create_string_buffer(len(plaintext))
|
| 167 |
+
else:
|
| 168 |
+
ciphertext = output
|
| 169 |
+
|
| 170 |
+
if not is_writeable_buffer(output):
|
| 171 |
+
raise TypeError("output must be a bytearray or a writeable memoryview")
|
| 172 |
+
|
| 173 |
+
if len(plaintext) != len(output):
|
| 174 |
+
raise ValueError("output must have the same length as the input"
|
| 175 |
+
" (%d bytes)" % len(plaintext))
|
| 176 |
+
|
| 177 |
+
result = raw_cbc_lib.CBC_encrypt(self._state.get(),
|
| 178 |
+
c_uint8_ptr(plaintext),
|
| 179 |
+
c_uint8_ptr(ciphertext),
|
| 180 |
+
c_size_t(len(plaintext)))
|
| 181 |
+
if result:
|
| 182 |
+
if result == 3:
|
| 183 |
+
raise ValueError("Data must be padded to %d byte boundary in CBC mode" % self.block_size)
|
| 184 |
+
raise ValueError("Error %d while encrypting in CBC mode" % result)
|
| 185 |
+
|
| 186 |
+
if output is None:
|
| 187 |
+
return get_raw_buffer(ciphertext)
|
| 188 |
+
else:
|
| 189 |
+
return None
|
| 190 |
+
|
| 191 |
+
def decrypt(self, ciphertext, output=None):
|
| 192 |
+
"""Decrypt data with the key and the parameters set at initialization.
|
| 193 |
+
|
| 194 |
+
A cipher object is stateful: once you have decrypted a message
|
| 195 |
+
you cannot decrypt (or encrypt) another message with the same
|
| 196 |
+
object.
|
| 197 |
+
|
| 198 |
+
The data to decrypt can be broken up in two or
|
| 199 |
+
more pieces and `decrypt` can be called multiple times.
|
| 200 |
+
|
| 201 |
+
That is, the statement:
|
| 202 |
+
|
| 203 |
+
>>> c.decrypt(a) + c.decrypt(b)
|
| 204 |
+
|
| 205 |
+
is equivalent to:
|
| 206 |
+
|
| 207 |
+
>>> c.decrypt(a+b)
|
| 208 |
+
|
| 209 |
+
This function does not remove any padding from the plaintext.
|
| 210 |
+
|
| 211 |
+
:Parameters:
|
| 212 |
+
ciphertext : bytes/bytearray/memoryview
|
| 213 |
+
The piece of data to decrypt.
|
| 214 |
+
Its length must be multiple of the cipher block size.
|
| 215 |
+
:Keywords:
|
| 216 |
+
output : bytearray/memoryview
|
| 217 |
+
The location where the plaintext must be written to.
|
| 218 |
+
If ``None``, the plaintext is returned.
|
| 219 |
+
:Return:
|
| 220 |
+
If ``output`` is ``None``, the plaintext is returned as ``bytes``.
|
| 221 |
+
Otherwise, ``None``.
|
| 222 |
+
"""
|
| 223 |
+
|
| 224 |
+
if "decrypt" not in self._next:
|
| 225 |
+
raise TypeError("decrypt() cannot be called after encrypt()")
|
| 226 |
+
self._next = ["decrypt"]
|
| 227 |
+
|
| 228 |
+
if output is None:
|
| 229 |
+
plaintext = create_string_buffer(len(ciphertext))
|
| 230 |
+
else:
|
| 231 |
+
plaintext = output
|
| 232 |
+
|
| 233 |
+
if not is_writeable_buffer(output):
|
| 234 |
+
raise TypeError("output must be a bytearray or a writeable memoryview")
|
| 235 |
+
|
| 236 |
+
if len(ciphertext) != len(output):
|
| 237 |
+
raise ValueError("output must have the same length as the input"
|
| 238 |
+
" (%d bytes)" % len(plaintext))
|
| 239 |
+
|
| 240 |
+
result = raw_cbc_lib.CBC_decrypt(self._state.get(),
|
| 241 |
+
c_uint8_ptr(ciphertext),
|
| 242 |
+
c_uint8_ptr(plaintext),
|
| 243 |
+
c_size_t(len(ciphertext)))
|
| 244 |
+
if result:
|
| 245 |
+
if result == 3:
|
| 246 |
+
raise ValueError("Data must be padded to %d byte boundary in CBC mode" % self.block_size)
|
| 247 |
+
raise ValueError("Error %d while decrypting in CBC mode" % result)
|
| 248 |
+
|
| 249 |
+
if output is None:
|
| 250 |
+
return get_raw_buffer(plaintext)
|
| 251 |
+
else:
|
| 252 |
+
return None
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def _create_cbc_cipher(factory, **kwargs):
|
| 256 |
+
"""Instantiate a cipher object that performs CBC encryption/decryption.
|
| 257 |
+
|
| 258 |
+
:Parameters:
|
| 259 |
+
factory : module
|
| 260 |
+
The underlying block cipher, a module from ``Crypto.Cipher``.
|
| 261 |
+
|
| 262 |
+
:Keywords:
|
| 263 |
+
iv : bytes/bytearray/memoryview
|
| 264 |
+
The IV to use for CBC.
|
| 265 |
+
|
| 266 |
+
IV : bytes/bytearray/memoryview
|
| 267 |
+
Alias for ``iv``.
|
| 268 |
+
|
| 269 |
+
Any other keyword will be passed to the underlying block cipher.
|
| 270 |
+
See the relevant documentation for details (at least ``key`` will need
|
| 271 |
+
to be present).
|
| 272 |
+
"""
|
| 273 |
+
|
| 274 |
+
cipher_state = factory._create_base_cipher(kwargs)
|
| 275 |
+
iv = kwargs.pop("IV", None)
|
| 276 |
+
IV = kwargs.pop("iv", None)
|
| 277 |
+
|
| 278 |
+
if (None, None) == (iv, IV):
|
| 279 |
+
iv = get_random_bytes(factory.block_size)
|
| 280 |
+
if iv is not None:
|
| 281 |
+
if IV is not None:
|
| 282 |
+
raise TypeError("You must either use 'iv' or 'IV', not both")
|
| 283 |
+
else:
|
| 284 |
+
iv = IV
|
| 285 |
+
|
| 286 |
+
if len(iv) != factory.block_size:
|
| 287 |
+
raise ValueError("Incorrect IV length (it must be %d bytes long)" %
|
| 288 |
+
factory.block_size)
|
| 289 |
+
|
| 290 |
+
if kwargs:
|
| 291 |
+
raise TypeError("Unknown parameters for CBC: %s" % str(kwargs))
|
| 292 |
+
|
| 293 |
+
return CbcMode(cipher_state, iv)
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_cfb.py
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Cipher/mode_cfb.py : CFB mode
|
| 4 |
+
#
|
| 5 |
+
# ===================================================================
|
| 6 |
+
# The contents of this file are dedicated to the public domain. To
|
| 7 |
+
# the extent that dedication to the public domain is not available,
|
| 8 |
+
# everyone is granted a worldwide, perpetual, royalty-free,
|
| 9 |
+
# non-exclusive license to exercise all rights associated with the
|
| 10 |
+
# contents of this file for any purpose whatsoever.
|
| 11 |
+
# No rights are reserved.
|
| 12 |
+
#
|
| 13 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 14 |
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 15 |
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
| 16 |
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
| 17 |
+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
| 18 |
+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 19 |
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 20 |
+
# SOFTWARE.
|
| 21 |
+
# ===================================================================
|
| 22 |
+
|
| 23 |
+
"""
|
| 24 |
+
Counter Feedback (CFB) mode.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
__all__ = ['CfbMode']
|
| 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_cfb_lib = load_pycryptodome_raw_lib("Crypto.Cipher._raw_cfb","""
|
| 38 |
+
int CFB_start_operation(void *cipher,
|
| 39 |
+
const uint8_t iv[],
|
| 40 |
+
size_t iv_len,
|
| 41 |
+
size_t segment_len, /* In bytes */
|
| 42 |
+
void **pResult);
|
| 43 |
+
int CFB_encrypt(void *cfbState,
|
| 44 |
+
const uint8_t *in,
|
| 45 |
+
uint8_t *out,
|
| 46 |
+
size_t data_len);
|
| 47 |
+
int CFB_decrypt(void *cfbState,
|
| 48 |
+
const uint8_t *in,
|
| 49 |
+
uint8_t *out,
|
| 50 |
+
size_t data_len);
|
| 51 |
+
int CFB_stop_operation(void *state);"""
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class CfbMode(object):
|
| 56 |
+
"""*Cipher FeedBack (CFB)*.
|
| 57 |
+
|
| 58 |
+
This mode is similar to CFB, but it transforms
|
| 59 |
+
the underlying block cipher into a stream cipher.
|
| 60 |
+
|
| 61 |
+
Plaintext and ciphertext are processed in *segments*
|
| 62 |
+
of **s** bits. The mode is therefore sometimes
|
| 63 |
+
labelled **s**-bit CFB.
|
| 64 |
+
|
| 65 |
+
An Initialization Vector (*IV*) is required.
|
| 66 |
+
|
| 67 |
+
See `NIST SP800-38A`_ , Section 6.3.
|
| 68 |
+
|
| 69 |
+
.. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
|
| 70 |
+
|
| 71 |
+
:undocumented: __init__
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
def __init__(self, block_cipher, iv, segment_size):
|
| 75 |
+
"""Create a new block cipher, configured in CFB mode.
|
| 76 |
+
|
| 77 |
+
:Parameters:
|
| 78 |
+
block_cipher : C pointer
|
| 79 |
+
A smart pointer to the low-level block cipher instance.
|
| 80 |
+
|
| 81 |
+
iv : bytes/bytearray/memoryview
|
| 82 |
+
The initialization vector to use for encryption or decryption.
|
| 83 |
+
It is as long as the cipher block.
|
| 84 |
+
|
| 85 |
+
**The IV must be unpredictable**. Ideally it is picked randomly.
|
| 86 |
+
|
| 87 |
+
Reusing the *IV* for encryptions performed with the same key
|
| 88 |
+
compromises confidentiality.
|
| 89 |
+
|
| 90 |
+
segment_size : integer
|
| 91 |
+
The number of bytes the plaintext and ciphertext are segmented in.
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
self._state = VoidPointer()
|
| 95 |
+
result = raw_cfb_lib.CFB_start_operation(block_cipher.get(),
|
| 96 |
+
c_uint8_ptr(iv),
|
| 97 |
+
c_size_t(len(iv)),
|
| 98 |
+
c_size_t(segment_size),
|
| 99 |
+
self._state.address_of())
|
| 100 |
+
if result:
|
| 101 |
+
raise ValueError("Error %d while instantiating the CFB mode" % result)
|
| 102 |
+
|
| 103 |
+
# Ensure that object disposal of this Python object will (eventually)
|
| 104 |
+
# free the memory allocated by the raw library for the cipher mode
|
| 105 |
+
self._state = SmartPointer(self._state.get(),
|
| 106 |
+
raw_cfb_lib.CFB_stop_operation)
|
| 107 |
+
|
| 108 |
+
# Memory allocated for the underlying block cipher is now owed
|
| 109 |
+
# by the cipher mode
|
| 110 |
+
block_cipher.release()
|
| 111 |
+
|
| 112 |
+
self.block_size = len(iv)
|
| 113 |
+
"""The block size of the underlying cipher, in bytes."""
|
| 114 |
+
|
| 115 |
+
self.iv = _copy_bytes(None, None, iv)
|
| 116 |
+
"""The Initialization Vector originally used to create the object.
|
| 117 |
+
The value does not change."""
|
| 118 |
+
|
| 119 |
+
self.IV = self.iv
|
| 120 |
+
"""Alias for `iv`"""
|
| 121 |
+
|
| 122 |
+
self._next = ["encrypt", "decrypt"]
|
| 123 |
+
|
| 124 |
+
def encrypt(self, plaintext, output=None):
|
| 125 |
+
"""Encrypt data with the key and the parameters set at initialization.
|
| 126 |
+
|
| 127 |
+
A cipher object is stateful: once you have encrypted a message
|
| 128 |
+
you cannot encrypt (or decrypt) another message using the same
|
| 129 |
+
object.
|
| 130 |
+
|
| 131 |
+
The data to encrypt can be broken up in two or
|
| 132 |
+
more pieces and `encrypt` can be called multiple times.
|
| 133 |
+
|
| 134 |
+
That is, the statement:
|
| 135 |
+
|
| 136 |
+
>>> c.encrypt(a) + c.encrypt(b)
|
| 137 |
+
|
| 138 |
+
is equivalent to:
|
| 139 |
+
|
| 140 |
+
>>> c.encrypt(a+b)
|
| 141 |
+
|
| 142 |
+
This function does not add any padding to the plaintext.
|
| 143 |
+
|
| 144 |
+
:Parameters:
|
| 145 |
+
plaintext : bytes/bytearray/memoryview
|
| 146 |
+
The piece of data to encrypt.
|
| 147 |
+
It can be of any length.
|
| 148 |
+
:Keywords:
|
| 149 |
+
output : bytearray/memoryview
|
| 150 |
+
The location where the ciphertext must be written to.
|
| 151 |
+
If ``None``, the ciphertext is returned.
|
| 152 |
+
:Return:
|
| 153 |
+
If ``output`` is ``None``, the ciphertext is returned as ``bytes``.
|
| 154 |
+
Otherwise, ``None``.
|
| 155 |
+
"""
|
| 156 |
+
|
| 157 |
+
if "encrypt" not in self._next:
|
| 158 |
+
raise TypeError("encrypt() cannot be called after decrypt()")
|
| 159 |
+
self._next = ["encrypt"]
|
| 160 |
+
|
| 161 |
+
if output is None:
|
| 162 |
+
ciphertext = create_string_buffer(len(plaintext))
|
| 163 |
+
else:
|
| 164 |
+
ciphertext = output
|
| 165 |
+
|
| 166 |
+
if not is_writeable_buffer(output):
|
| 167 |
+
raise TypeError("output must be a bytearray or a writeable memoryview")
|
| 168 |
+
|
| 169 |
+
if len(plaintext) != len(output):
|
| 170 |
+
raise ValueError("output must have the same length as the input"
|
| 171 |
+
" (%d bytes)" % len(plaintext))
|
| 172 |
+
|
| 173 |
+
result = raw_cfb_lib.CFB_encrypt(self._state.get(),
|
| 174 |
+
c_uint8_ptr(plaintext),
|
| 175 |
+
c_uint8_ptr(ciphertext),
|
| 176 |
+
c_size_t(len(plaintext)))
|
| 177 |
+
if result:
|
| 178 |
+
raise ValueError("Error %d while encrypting in CFB mode" % result)
|
| 179 |
+
|
| 180 |
+
if output is None:
|
| 181 |
+
return get_raw_buffer(ciphertext)
|
| 182 |
+
else:
|
| 183 |
+
return None
|
| 184 |
+
|
| 185 |
+
def decrypt(self, ciphertext, output=None):
|
| 186 |
+
"""Decrypt data with the key and the parameters set at initialization.
|
| 187 |
+
|
| 188 |
+
A cipher object is stateful: once you have decrypted a message
|
| 189 |
+
you cannot decrypt (or encrypt) another message with the same
|
| 190 |
+
object.
|
| 191 |
+
|
| 192 |
+
The data to decrypt can be broken up in two or
|
| 193 |
+
more pieces and `decrypt` can be called multiple times.
|
| 194 |
+
|
| 195 |
+
That is, the statement:
|
| 196 |
+
|
| 197 |
+
>>> c.decrypt(a) + c.decrypt(b)
|
| 198 |
+
|
| 199 |
+
is equivalent to:
|
| 200 |
+
|
| 201 |
+
>>> c.decrypt(a+b)
|
| 202 |
+
|
| 203 |
+
This function does not remove any padding from the plaintext.
|
| 204 |
+
|
| 205 |
+
:Parameters:
|
| 206 |
+
ciphertext : bytes/bytearray/memoryview
|
| 207 |
+
The piece of data to decrypt.
|
| 208 |
+
It can be of any length.
|
| 209 |
+
:Keywords:
|
| 210 |
+
output : bytearray/memoryview
|
| 211 |
+
The location where the plaintext must be written to.
|
| 212 |
+
If ``None``, the plaintext is returned.
|
| 213 |
+
:Return:
|
| 214 |
+
If ``output`` is ``None``, the plaintext is returned as ``bytes``.
|
| 215 |
+
Otherwise, ``None``.
|
| 216 |
+
"""
|
| 217 |
+
|
| 218 |
+
if "decrypt" not in self._next:
|
| 219 |
+
raise TypeError("decrypt() cannot be called after encrypt()")
|
| 220 |
+
self._next = ["decrypt"]
|
| 221 |
+
|
| 222 |
+
if output is None:
|
| 223 |
+
plaintext = create_string_buffer(len(ciphertext))
|
| 224 |
+
else:
|
| 225 |
+
plaintext = output
|
| 226 |
+
|
| 227 |
+
if not is_writeable_buffer(output):
|
| 228 |
+
raise TypeError("output must be a bytearray or a writeable memoryview")
|
| 229 |
+
|
| 230 |
+
if len(ciphertext) != len(output):
|
| 231 |
+
raise ValueError("output must have the same length as the input"
|
| 232 |
+
" (%d bytes)" % len(plaintext))
|
| 233 |
+
|
| 234 |
+
result = raw_cfb_lib.CFB_decrypt(self._state.get(),
|
| 235 |
+
c_uint8_ptr(ciphertext),
|
| 236 |
+
c_uint8_ptr(plaintext),
|
| 237 |
+
c_size_t(len(ciphertext)))
|
| 238 |
+
if result:
|
| 239 |
+
raise ValueError("Error %d while decrypting in CFB mode" % result)
|
| 240 |
+
|
| 241 |
+
if output is None:
|
| 242 |
+
return get_raw_buffer(plaintext)
|
| 243 |
+
else:
|
| 244 |
+
return None
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def _create_cfb_cipher(factory, **kwargs):
|
| 248 |
+
"""Instantiate a cipher object that performs CFB encryption/decryption.
|
| 249 |
+
|
| 250 |
+
:Parameters:
|
| 251 |
+
factory : module
|
| 252 |
+
The underlying block cipher, a module from ``Crypto.Cipher``.
|
| 253 |
+
|
| 254 |
+
:Keywords:
|
| 255 |
+
iv : bytes/bytearray/memoryview
|
| 256 |
+
The IV to use for CFB.
|
| 257 |
+
|
| 258 |
+
IV : bytes/bytearray/memoryview
|
| 259 |
+
Alias for ``iv``.
|
| 260 |
+
|
| 261 |
+
segment_size : integer
|
| 262 |
+
The number of bit the plaintext and ciphertext are segmented in.
|
| 263 |
+
If not present, the default is 8.
|
| 264 |
+
|
| 265 |
+
Any other keyword will be passed to the underlying block cipher.
|
| 266 |
+
See the relevant documentation for details (at least ``key`` will need
|
| 267 |
+
to be present).
|
| 268 |
+
"""
|
| 269 |
+
|
| 270 |
+
cipher_state = factory._create_base_cipher(kwargs)
|
| 271 |
+
|
| 272 |
+
iv = kwargs.pop("IV", None)
|
| 273 |
+
IV = kwargs.pop("iv", None)
|
| 274 |
+
|
| 275 |
+
if (None, None) == (iv, IV):
|
| 276 |
+
iv = get_random_bytes(factory.block_size)
|
| 277 |
+
if iv is not None:
|
| 278 |
+
if IV is not None:
|
| 279 |
+
raise TypeError("You must either use 'iv' or 'IV', not both")
|
| 280 |
+
else:
|
| 281 |
+
iv = IV
|
| 282 |
+
|
| 283 |
+
if len(iv) != factory.block_size:
|
| 284 |
+
raise ValueError("Incorrect IV length (it must be %d bytes long)" %
|
| 285 |
+
factory.block_size)
|
| 286 |
+
|
| 287 |
+
segment_size_bytes, rem = divmod(kwargs.pop("segment_size", 8), 8)
|
| 288 |
+
if segment_size_bytes == 0 or rem != 0:
|
| 289 |
+
raise ValueError("'segment_size' must be positive and multiple of 8 bits")
|
| 290 |
+
|
| 291 |
+
if kwargs:
|
| 292 |
+
raise TypeError("Unknown parameters for CFB: %s" % str(kwargs))
|
| 293 |
+
return CfbMode(cipher_state, iv, segment_size_bytes)
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_eax.py
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ===================================================================
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2014, Legrandin <helderijs@gmail.com>
|
| 4 |
+
# All rights reserved.
|
| 5 |
+
#
|
| 6 |
+
# Redistribution and use in source and binary forms, with or without
|
| 7 |
+
# modification, are permitted provided that the following conditions
|
| 8 |
+
# are met:
|
| 9 |
+
#
|
| 10 |
+
# 1. Redistributions of source code must retain the above copyright
|
| 11 |
+
# notice, this list of conditions and the following disclaimer.
|
| 12 |
+
# 2. Redistributions in binary form must reproduce the above copyright
|
| 13 |
+
# notice, this list of conditions and the following disclaimer in
|
| 14 |
+
# the documentation and/or other materials provided with the
|
| 15 |
+
# distribution.
|
| 16 |
+
#
|
| 17 |
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 18 |
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 19 |
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
| 20 |
+
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
| 21 |
+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
| 22 |
+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
| 23 |
+
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
| 24 |
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 25 |
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
| 26 |
+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
| 27 |
+
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 28 |
+
# POSSIBILITY OF SUCH DAMAGE.
|
| 29 |
+
# ===================================================================
|
| 30 |
+
|
| 31 |
+
"""
|
| 32 |
+
EAX mode.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
__all__ = ['EaxMode']
|
| 36 |
+
|
| 37 |
+
import struct
|
| 38 |
+
from binascii import unhexlify
|
| 39 |
+
|
| 40 |
+
from Crypto.Util.py3compat import byte_string, bord, _copy_bytes
|
| 41 |
+
|
| 42 |
+
from Crypto.Util._raw_api import is_buffer
|
| 43 |
+
|
| 44 |
+
from Crypto.Util.strxor import strxor
|
| 45 |
+
from Crypto.Util.number import long_to_bytes, bytes_to_long
|
| 46 |
+
|
| 47 |
+
from Crypto.Hash import CMAC, BLAKE2s
|
| 48 |
+
from Crypto.Random import get_random_bytes
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class EaxMode(object):
|
| 52 |
+
"""*EAX* mode.
|
| 53 |
+
|
| 54 |
+
This is an Authenticated Encryption with Associated Data
|
| 55 |
+
(`AEAD`_) mode. It provides both confidentiality and authenticity.
|
| 56 |
+
|
| 57 |
+
The header of the message may be left in the clear, if needed,
|
| 58 |
+
and it will still be subject to authentication.
|
| 59 |
+
|
| 60 |
+
The decryption step tells the receiver if the message comes
|
| 61 |
+
from a source that really knowns the secret key.
|
| 62 |
+
Additionally, decryption detects if any part of the message -
|
| 63 |
+
including the header - has been modified or corrupted.
|
| 64 |
+
|
| 65 |
+
This mode requires a *nonce*.
|
| 66 |
+
|
| 67 |
+
This mode is only available for ciphers that operate on 64 or
|
| 68 |
+
128 bits blocks.
|
| 69 |
+
|
| 70 |
+
There are no official standards defining EAX.
|
| 71 |
+
The implementation is based on `a proposal`__ that
|
| 72 |
+
was presented to NIST.
|
| 73 |
+
|
| 74 |
+
.. _AEAD: http://blog.cryptographyengineering.com/2012/05/how-to-choose-authenticated-encryption.html
|
| 75 |
+
.. __: http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/eax/eax-spec.pdf
|
| 76 |
+
|
| 77 |
+
:undocumented: __init__
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
def __init__(self, factory, key, nonce, mac_len, cipher_params):
|
| 81 |
+
"""EAX cipher mode"""
|
| 82 |
+
|
| 83 |
+
self.block_size = factory.block_size
|
| 84 |
+
"""The block size of the underlying cipher, in bytes."""
|
| 85 |
+
|
| 86 |
+
self.nonce = _copy_bytes(None, None, nonce)
|
| 87 |
+
"""The nonce originally used to create the object."""
|
| 88 |
+
|
| 89 |
+
self._mac_len = mac_len
|
| 90 |
+
self._mac_tag = None # Cache for MAC tag
|
| 91 |
+
|
| 92 |
+
# Allowed transitions after initialization
|
| 93 |
+
self._next = ["update", "encrypt", "decrypt",
|
| 94 |
+
"digest", "verify"]
|
| 95 |
+
|
| 96 |
+
# MAC tag length
|
| 97 |
+
if not (2 <= self._mac_len <= self.block_size):
|
| 98 |
+
raise ValueError("'mac_len' must be at least 2 and not larger than %d"
|
| 99 |
+
% self.block_size)
|
| 100 |
+
|
| 101 |
+
# Nonce cannot be empty and must be a byte string
|
| 102 |
+
if len(self.nonce) == 0:
|
| 103 |
+
raise ValueError("Nonce cannot be empty in EAX mode")
|
| 104 |
+
if not is_buffer(nonce):
|
| 105 |
+
raise TypeError("nonce must be bytes, bytearray or memoryview")
|
| 106 |
+
|
| 107 |
+
self._omac = [
|
| 108 |
+
CMAC.new(key,
|
| 109 |
+
b'\x00' * (self.block_size - 1) + struct.pack('B', i),
|
| 110 |
+
ciphermod=factory,
|
| 111 |
+
cipher_params=cipher_params)
|
| 112 |
+
for i in range(0, 3)
|
| 113 |
+
]
|
| 114 |
+
|
| 115 |
+
# Compute MAC of nonce
|
| 116 |
+
self._omac[0].update(self.nonce)
|
| 117 |
+
self._signer = self._omac[1]
|
| 118 |
+
|
| 119 |
+
# MAC of the nonce is also the initial counter for CTR encryption
|
| 120 |
+
counter_int = bytes_to_long(self._omac[0].digest())
|
| 121 |
+
self._cipher = factory.new(key,
|
| 122 |
+
factory.MODE_CTR,
|
| 123 |
+
initial_value=counter_int,
|
| 124 |
+
nonce=b"",
|
| 125 |
+
**cipher_params)
|
| 126 |
+
|
| 127 |
+
def update(self, assoc_data):
|
| 128 |
+
"""Protect associated data
|
| 129 |
+
|
| 130 |
+
If there is any associated data, the caller has to invoke
|
| 131 |
+
this function one or more times, before using
|
| 132 |
+
``decrypt`` or ``encrypt``.
|
| 133 |
+
|
| 134 |
+
By *associated data* it is meant any data (e.g. packet headers) that
|
| 135 |
+
will not be encrypted and will be transmitted in the clear.
|
| 136 |
+
However, the receiver is still able to detect any modification to it.
|
| 137 |
+
|
| 138 |
+
If there is no associated data, this method must not be called.
|
| 139 |
+
|
| 140 |
+
The caller may split associated data in segments of any size, and
|
| 141 |
+
invoke this method multiple times, each time with the next segment.
|
| 142 |
+
|
| 143 |
+
:Parameters:
|
| 144 |
+
assoc_data : bytes/bytearray/memoryview
|
| 145 |
+
A piece of associated data. There are no restrictions on its size.
|
| 146 |
+
"""
|
| 147 |
+
|
| 148 |
+
if "update" not in self._next:
|
| 149 |
+
raise TypeError("update() can only be called"
|
| 150 |
+
" immediately after initialization")
|
| 151 |
+
|
| 152 |
+
self._next = ["update", "encrypt", "decrypt",
|
| 153 |
+
"digest", "verify"]
|
| 154 |
+
|
| 155 |
+
self._signer.update(assoc_data)
|
| 156 |
+
return self
|
| 157 |
+
|
| 158 |
+
def encrypt(self, plaintext, output=None):
|
| 159 |
+
"""Encrypt data with the key and the parameters set at initialization.
|
| 160 |
+
|
| 161 |
+
A cipher object is stateful: once you have encrypted a message
|
| 162 |
+
you cannot encrypt (or decrypt) another message using the same
|
| 163 |
+
object.
|
| 164 |
+
|
| 165 |
+
The data to encrypt can be broken up in two or
|
| 166 |
+
more pieces and `encrypt` can be called multiple times.
|
| 167 |
+
|
| 168 |
+
That is, the statement:
|
| 169 |
+
|
| 170 |
+
>>> c.encrypt(a) + c.encrypt(b)
|
| 171 |
+
|
| 172 |
+
is equivalent to:
|
| 173 |
+
|
| 174 |
+
>>> c.encrypt(a+b)
|
| 175 |
+
|
| 176 |
+
This function does not add any padding to the plaintext.
|
| 177 |
+
|
| 178 |
+
:Parameters:
|
| 179 |
+
plaintext : bytes/bytearray/memoryview
|
| 180 |
+
The piece of data to encrypt.
|
| 181 |
+
It can be of any length.
|
| 182 |
+
:Keywords:
|
| 183 |
+
output : bytearray/memoryview
|
| 184 |
+
The location where the ciphertext must be written to.
|
| 185 |
+
If ``None``, the ciphertext is returned.
|
| 186 |
+
:Return:
|
| 187 |
+
If ``output`` is ``None``, the ciphertext as ``bytes``.
|
| 188 |
+
Otherwise, ``None``.
|
| 189 |
+
"""
|
| 190 |
+
|
| 191 |
+
if "encrypt" not in self._next:
|
| 192 |
+
raise TypeError("encrypt() can only be called after"
|
| 193 |
+
" initialization or an update()")
|
| 194 |
+
self._next = ["encrypt", "digest"]
|
| 195 |
+
ct = self._cipher.encrypt(plaintext, output=output)
|
| 196 |
+
if output is None:
|
| 197 |
+
self._omac[2].update(ct)
|
| 198 |
+
else:
|
| 199 |
+
self._omac[2].update(output)
|
| 200 |
+
return ct
|
| 201 |
+
|
| 202 |
+
def decrypt(self, ciphertext, output=None):
|
| 203 |
+
"""Decrypt data with the key and the parameters set at initialization.
|
| 204 |
+
|
| 205 |
+
A cipher object is stateful: once you have decrypted a message
|
| 206 |
+
you cannot decrypt (or encrypt) another message with the same
|
| 207 |
+
object.
|
| 208 |
+
|
| 209 |
+
The data to decrypt can be broken up in two or
|
| 210 |
+
more pieces and `decrypt` can be called multiple times.
|
| 211 |
+
|
| 212 |
+
That is, the statement:
|
| 213 |
+
|
| 214 |
+
>>> c.decrypt(a) + c.decrypt(b)
|
| 215 |
+
|
| 216 |
+
is equivalent to:
|
| 217 |
+
|
| 218 |
+
>>> c.decrypt(a+b)
|
| 219 |
+
|
| 220 |
+
This function does not remove any padding from the plaintext.
|
| 221 |
+
|
| 222 |
+
:Parameters:
|
| 223 |
+
ciphertext : bytes/bytearray/memoryview
|
| 224 |
+
The piece of data to decrypt.
|
| 225 |
+
It can be of any length.
|
| 226 |
+
:Keywords:
|
| 227 |
+
output : bytearray/memoryview
|
| 228 |
+
The location where the plaintext must be written to.
|
| 229 |
+
If ``None``, the plaintext is returned.
|
| 230 |
+
:Return:
|
| 231 |
+
If ``output`` is ``None``, the plaintext as ``bytes``.
|
| 232 |
+
Otherwise, ``None``.
|
| 233 |
+
"""
|
| 234 |
+
|
| 235 |
+
if "decrypt" not in self._next:
|
| 236 |
+
raise TypeError("decrypt() can only be called"
|
| 237 |
+
" after initialization or an update()")
|
| 238 |
+
self._next = ["decrypt", "verify"]
|
| 239 |
+
self._omac[2].update(ciphertext)
|
| 240 |
+
return self._cipher.decrypt(ciphertext, output=output)
|
| 241 |
+
|
| 242 |
+
def digest(self):
|
| 243 |
+
"""Compute the *binary* MAC tag.
|
| 244 |
+
|
| 245 |
+
The caller invokes this function at the very end.
|
| 246 |
+
|
| 247 |
+
This method returns the MAC that shall be sent to the receiver,
|
| 248 |
+
together with the ciphertext.
|
| 249 |
+
|
| 250 |
+
:Return: the MAC, as a byte string.
|
| 251 |
+
"""
|
| 252 |
+
|
| 253 |
+
if "digest" not in self._next:
|
| 254 |
+
raise TypeError("digest() cannot be called when decrypting"
|
| 255 |
+
" or validating a message")
|
| 256 |
+
self._next = ["digest"]
|
| 257 |
+
|
| 258 |
+
if not self._mac_tag:
|
| 259 |
+
tag = b'\x00' * self.block_size
|
| 260 |
+
for i in range(3):
|
| 261 |
+
tag = strxor(tag, self._omac[i].digest())
|
| 262 |
+
self._mac_tag = tag[:self._mac_len]
|
| 263 |
+
|
| 264 |
+
return self._mac_tag
|
| 265 |
+
|
| 266 |
+
def hexdigest(self):
|
| 267 |
+
"""Compute the *printable* MAC tag.
|
| 268 |
+
|
| 269 |
+
This method is like `digest`.
|
| 270 |
+
|
| 271 |
+
:Return: the MAC, as a hexadecimal string.
|
| 272 |
+
"""
|
| 273 |
+
return "".join(["%02x" % bord(x) for x in self.digest()])
|
| 274 |
+
|
| 275 |
+
def verify(self, received_mac_tag):
|
| 276 |
+
"""Validate the *binary* MAC tag.
|
| 277 |
+
|
| 278 |
+
The caller invokes this function at the very end.
|
| 279 |
+
|
| 280 |
+
This method checks if the decrypted message is indeed valid
|
| 281 |
+
(that is, if the key is correct) and it has not been
|
| 282 |
+
tampered with while in transit.
|
| 283 |
+
|
| 284 |
+
:Parameters:
|
| 285 |
+
received_mac_tag : bytes/bytearray/memoryview
|
| 286 |
+
This is the *binary* MAC, as received from the sender.
|
| 287 |
+
:Raises MacMismatchError:
|
| 288 |
+
if the MAC does not match. The message has been tampered with
|
| 289 |
+
or the key is incorrect.
|
| 290 |
+
"""
|
| 291 |
+
|
| 292 |
+
if "verify" not in self._next:
|
| 293 |
+
raise TypeError("verify() cannot be called"
|
| 294 |
+
" when encrypting a message")
|
| 295 |
+
self._next = ["verify"]
|
| 296 |
+
|
| 297 |
+
if not self._mac_tag:
|
| 298 |
+
tag = b'\x00' * self.block_size
|
| 299 |
+
for i in range(3):
|
| 300 |
+
tag = strxor(tag, self._omac[i].digest())
|
| 301 |
+
self._mac_tag = tag[:self._mac_len]
|
| 302 |
+
|
| 303 |
+
secret = get_random_bytes(16)
|
| 304 |
+
|
| 305 |
+
mac1 = BLAKE2s.new(digest_bits=160, key=secret, data=self._mac_tag)
|
| 306 |
+
mac2 = BLAKE2s.new(digest_bits=160, key=secret, data=received_mac_tag)
|
| 307 |
+
|
| 308 |
+
if mac1.digest() != mac2.digest():
|
| 309 |
+
raise ValueError("MAC check failed")
|
| 310 |
+
|
| 311 |
+
def hexverify(self, hex_mac_tag):
|
| 312 |
+
"""Validate the *printable* MAC tag.
|
| 313 |
+
|
| 314 |
+
This method is like `verify`.
|
| 315 |
+
|
| 316 |
+
:Parameters:
|
| 317 |
+
hex_mac_tag : string
|
| 318 |
+
This is the *printable* MAC, as received from the sender.
|
| 319 |
+
:Raises MacMismatchError:
|
| 320 |
+
if the MAC does not match. The message has been tampered with
|
| 321 |
+
or the key is incorrect.
|
| 322 |
+
"""
|
| 323 |
+
|
| 324 |
+
self.verify(unhexlify(hex_mac_tag))
|
| 325 |
+
|
| 326 |
+
def encrypt_and_digest(self, plaintext, output=None):
|
| 327 |
+
"""Perform encrypt() and digest() in one step.
|
| 328 |
+
|
| 329 |
+
:Parameters:
|
| 330 |
+
plaintext : bytes/bytearray/memoryview
|
| 331 |
+
The piece of data to encrypt.
|
| 332 |
+
:Keywords:
|
| 333 |
+
output : bytearray/memoryview
|
| 334 |
+
The location where the ciphertext must be written to.
|
| 335 |
+
If ``None``, the ciphertext is returned.
|
| 336 |
+
:Return:
|
| 337 |
+
a tuple with two items:
|
| 338 |
+
|
| 339 |
+
- the ciphertext, as ``bytes``
|
| 340 |
+
- the MAC tag, as ``bytes``
|
| 341 |
+
|
| 342 |
+
The first item becomes ``None`` when the ``output`` parameter
|
| 343 |
+
specified a location for the result.
|
| 344 |
+
"""
|
| 345 |
+
|
| 346 |
+
return self.encrypt(plaintext, output=output), self.digest()
|
| 347 |
+
|
| 348 |
+
def decrypt_and_verify(self, ciphertext, received_mac_tag, output=None):
|
| 349 |
+
"""Perform decrypt() and verify() in one step.
|
| 350 |
+
|
| 351 |
+
:Parameters:
|
| 352 |
+
ciphertext : bytes/bytearray/memoryview
|
| 353 |
+
The piece of data to decrypt.
|
| 354 |
+
received_mac_tag : bytes/bytearray/memoryview
|
| 355 |
+
This is the *binary* MAC, as received from the sender.
|
| 356 |
+
:Keywords:
|
| 357 |
+
output : bytearray/memoryview
|
| 358 |
+
The location where the plaintext must be written to.
|
| 359 |
+
If ``None``, the plaintext is returned.
|
| 360 |
+
:Return: the plaintext as ``bytes`` or ``None`` when the ``output``
|
| 361 |
+
parameter specified a location for the result.
|
| 362 |
+
:Raises MacMismatchError:
|
| 363 |
+
if the MAC does not match. The message has been tampered with
|
| 364 |
+
or the key is incorrect.
|
| 365 |
+
"""
|
| 366 |
+
|
| 367 |
+
pt = self.decrypt(ciphertext, output=output)
|
| 368 |
+
self.verify(received_mac_tag)
|
| 369 |
+
return pt
|
| 370 |
+
|
| 371 |
+
|
| 372 |
+
def _create_eax_cipher(factory, **kwargs):
|
| 373 |
+
"""Create a new block cipher, configured in EAX mode.
|
| 374 |
+
|
| 375 |
+
:Parameters:
|
| 376 |
+
factory : module
|
| 377 |
+
A symmetric cipher module from `Crypto.Cipher` (like
|
| 378 |
+
`Crypto.Cipher.AES`).
|
| 379 |
+
|
| 380 |
+
:Keywords:
|
| 381 |
+
key : bytes/bytearray/memoryview
|
| 382 |
+
The secret key to use in the symmetric cipher.
|
| 383 |
+
|
| 384 |
+
nonce : bytes/bytearray/memoryview
|
| 385 |
+
A value that must never be reused for any other encryption.
|
| 386 |
+
There are no restrictions on its length, but it is recommended to use
|
| 387 |
+
at least 16 bytes.
|
| 388 |
+
|
| 389 |
+
The nonce shall never repeat for two different messages encrypted with
|
| 390 |
+
the same key, but it does not need to be random.
|
| 391 |
+
|
| 392 |
+
If not specified, a 16 byte long random string is used.
|
| 393 |
+
|
| 394 |
+
mac_len : integer
|
| 395 |
+
Length of the MAC, in bytes. It must be no larger than the cipher
|
| 396 |
+
block bytes (which is the default).
|
| 397 |
+
"""
|
| 398 |
+
|
| 399 |
+
try:
|
| 400 |
+
key = kwargs.pop("key")
|
| 401 |
+
nonce = kwargs.pop("nonce", None)
|
| 402 |
+
if nonce is None:
|
| 403 |
+
nonce = get_random_bytes(16)
|
| 404 |
+
mac_len = kwargs.pop("mac_len", factory.block_size)
|
| 405 |
+
except KeyError as e:
|
| 406 |
+
raise TypeError("Missing parameter: " + str(e))
|
| 407 |
+
|
| 408 |
+
return EaxMode(factory, key, nonce, mac_len, kwargs)
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_ecb.py
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Cipher/mode_ecb.py : ECB mode
|
| 4 |
+
#
|
| 5 |
+
# ===================================================================
|
| 6 |
+
# The contents of this file are dedicated to the public domain. To
|
| 7 |
+
# the extent that dedication to the public domain is not available,
|
| 8 |
+
# everyone is granted a worldwide, perpetual, royalty-free,
|
| 9 |
+
# non-exclusive license to exercise all rights associated with the
|
| 10 |
+
# contents of this file for any purpose whatsoever.
|
| 11 |
+
# No rights are reserved.
|
| 12 |
+
#
|
| 13 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 14 |
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 15 |
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
| 16 |
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
| 17 |
+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
| 18 |
+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 19 |
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 20 |
+
# SOFTWARE.
|
| 21 |
+
# ===================================================================
|
| 22 |
+
|
| 23 |
+
"""
|
| 24 |
+
Electronic Code Book (ECB) mode.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
__all__ = [ 'EcbMode' ]
|
| 28 |
+
|
| 29 |
+
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
|
| 30 |
+
VoidPointer, create_string_buffer,
|
| 31 |
+
get_raw_buffer, SmartPointer,
|
| 32 |
+
c_size_t, c_uint8_ptr,
|
| 33 |
+
is_writeable_buffer)
|
| 34 |
+
|
| 35 |
+
raw_ecb_lib = load_pycryptodome_raw_lib("Crypto.Cipher._raw_ecb", """
|
| 36 |
+
int ECB_start_operation(void *cipher,
|
| 37 |
+
void **pResult);
|
| 38 |
+
int ECB_encrypt(void *ecbState,
|
| 39 |
+
const uint8_t *in,
|
| 40 |
+
uint8_t *out,
|
| 41 |
+
size_t data_len);
|
| 42 |
+
int ECB_decrypt(void *ecbState,
|
| 43 |
+
const uint8_t *in,
|
| 44 |
+
uint8_t *out,
|
| 45 |
+
size_t data_len);
|
| 46 |
+
int ECB_stop_operation(void *state);
|
| 47 |
+
"""
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class EcbMode(object):
|
| 52 |
+
"""*Electronic Code Book (ECB)*.
|
| 53 |
+
|
| 54 |
+
This is the simplest encryption mode. Each of the plaintext blocks
|
| 55 |
+
is directly encrypted into a ciphertext block, independently of
|
| 56 |
+
any other block.
|
| 57 |
+
|
| 58 |
+
This mode is dangerous because it exposes frequency of symbols
|
| 59 |
+
in your plaintext. Other modes (e.g. *CBC*) should be used instead.
|
| 60 |
+
|
| 61 |
+
See `NIST SP800-38A`_ , Section 6.1.
|
| 62 |
+
|
| 63 |
+
.. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
|
| 64 |
+
|
| 65 |
+
:undocumented: __init__
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
def __init__(self, block_cipher):
|
| 69 |
+
"""Create a new block cipher, configured in ECB mode.
|
| 70 |
+
|
| 71 |
+
:Parameters:
|
| 72 |
+
block_cipher : C pointer
|
| 73 |
+
A smart pointer to the low-level block cipher instance.
|
| 74 |
+
"""
|
| 75 |
+
self.block_size = block_cipher.block_size
|
| 76 |
+
|
| 77 |
+
self._state = VoidPointer()
|
| 78 |
+
result = raw_ecb_lib.ECB_start_operation(block_cipher.get(),
|
| 79 |
+
self._state.address_of())
|
| 80 |
+
if result:
|
| 81 |
+
raise ValueError("Error %d while instantiating the ECB mode"
|
| 82 |
+
% result)
|
| 83 |
+
|
| 84 |
+
# Ensure that object disposal of this Python object will (eventually)
|
| 85 |
+
# free the memory allocated by the raw library for the cipher
|
| 86 |
+
# mode
|
| 87 |
+
self._state = SmartPointer(self._state.get(),
|
| 88 |
+
raw_ecb_lib.ECB_stop_operation)
|
| 89 |
+
|
| 90 |
+
# Memory allocated for the underlying block cipher is now owned
|
| 91 |
+
# by the cipher mode
|
| 92 |
+
block_cipher.release()
|
| 93 |
+
|
| 94 |
+
def encrypt(self, plaintext, output=None):
|
| 95 |
+
"""Encrypt data with the key set at initialization.
|
| 96 |
+
|
| 97 |
+
The data to encrypt can be broken up in two or
|
| 98 |
+
more pieces and `encrypt` can be called multiple times.
|
| 99 |
+
|
| 100 |
+
That is, the statement:
|
| 101 |
+
|
| 102 |
+
>>> c.encrypt(a) + c.encrypt(b)
|
| 103 |
+
|
| 104 |
+
is equivalent to:
|
| 105 |
+
|
| 106 |
+
>>> c.encrypt(a+b)
|
| 107 |
+
|
| 108 |
+
This function does not add any padding to the plaintext.
|
| 109 |
+
|
| 110 |
+
:Parameters:
|
| 111 |
+
plaintext : bytes/bytearray/memoryview
|
| 112 |
+
The piece of data to encrypt.
|
| 113 |
+
The length must be multiple of the cipher block length.
|
| 114 |
+
:Keywords:
|
| 115 |
+
output : bytearray/memoryview
|
| 116 |
+
The location where the ciphertext must be written to.
|
| 117 |
+
If ``None``, the ciphertext is returned.
|
| 118 |
+
:Return:
|
| 119 |
+
If ``output`` is ``None``, the ciphertext is returned as ``bytes``.
|
| 120 |
+
Otherwise, ``None``.
|
| 121 |
+
"""
|
| 122 |
+
|
| 123 |
+
if output is None:
|
| 124 |
+
ciphertext = create_string_buffer(len(plaintext))
|
| 125 |
+
else:
|
| 126 |
+
ciphertext = output
|
| 127 |
+
|
| 128 |
+
if not is_writeable_buffer(output):
|
| 129 |
+
raise TypeError("output must be a bytearray or a writeable memoryview")
|
| 130 |
+
|
| 131 |
+
if len(plaintext) != len(output):
|
| 132 |
+
raise ValueError("output must have the same length as the input"
|
| 133 |
+
" (%d bytes)" % len(plaintext))
|
| 134 |
+
|
| 135 |
+
result = raw_ecb_lib.ECB_encrypt(self._state.get(),
|
| 136 |
+
c_uint8_ptr(plaintext),
|
| 137 |
+
c_uint8_ptr(ciphertext),
|
| 138 |
+
c_size_t(len(plaintext)))
|
| 139 |
+
if result:
|
| 140 |
+
if result == 3:
|
| 141 |
+
raise ValueError("Data must be aligned to block boundary in ECB mode")
|
| 142 |
+
raise ValueError("Error %d while encrypting in ECB mode" % result)
|
| 143 |
+
|
| 144 |
+
if output is None:
|
| 145 |
+
return get_raw_buffer(ciphertext)
|
| 146 |
+
else:
|
| 147 |
+
return None
|
| 148 |
+
|
| 149 |
+
def decrypt(self, ciphertext, output=None):
|
| 150 |
+
"""Decrypt data with the key set at initialization.
|
| 151 |
+
|
| 152 |
+
The data to decrypt can be broken up in two or
|
| 153 |
+
more pieces and `decrypt` can be called multiple times.
|
| 154 |
+
|
| 155 |
+
That is, the statement:
|
| 156 |
+
|
| 157 |
+
>>> c.decrypt(a) + c.decrypt(b)
|
| 158 |
+
|
| 159 |
+
is equivalent to:
|
| 160 |
+
|
| 161 |
+
>>> c.decrypt(a+b)
|
| 162 |
+
|
| 163 |
+
This function does not remove any padding from the plaintext.
|
| 164 |
+
|
| 165 |
+
:Parameters:
|
| 166 |
+
ciphertext : bytes/bytearray/memoryview
|
| 167 |
+
The piece of data to decrypt.
|
| 168 |
+
The length must be multiple of the cipher block length.
|
| 169 |
+
:Keywords:
|
| 170 |
+
output : bytearray/memoryview
|
| 171 |
+
The location where the plaintext must be written to.
|
| 172 |
+
If ``None``, the plaintext is returned.
|
| 173 |
+
:Return:
|
| 174 |
+
If ``output`` is ``None``, the plaintext is returned as ``bytes``.
|
| 175 |
+
Otherwise, ``None``.
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
if output is None:
|
| 179 |
+
plaintext = create_string_buffer(len(ciphertext))
|
| 180 |
+
else:
|
| 181 |
+
plaintext = output
|
| 182 |
+
|
| 183 |
+
if not is_writeable_buffer(output):
|
| 184 |
+
raise TypeError("output must be a bytearray or a writeable memoryview")
|
| 185 |
+
|
| 186 |
+
if len(ciphertext) != len(output):
|
| 187 |
+
raise ValueError("output must have the same length as the input"
|
| 188 |
+
" (%d bytes)" % len(plaintext))
|
| 189 |
+
|
| 190 |
+
result = raw_ecb_lib.ECB_decrypt(self._state.get(),
|
| 191 |
+
c_uint8_ptr(ciphertext),
|
| 192 |
+
c_uint8_ptr(plaintext),
|
| 193 |
+
c_size_t(len(ciphertext)))
|
| 194 |
+
if result:
|
| 195 |
+
if result == 3:
|
| 196 |
+
raise ValueError("Data must be aligned to block boundary in ECB mode")
|
| 197 |
+
raise ValueError("Error %d while decrypting in ECB mode" % result)
|
| 198 |
+
|
| 199 |
+
if output is None:
|
| 200 |
+
return get_raw_buffer(plaintext)
|
| 201 |
+
else:
|
| 202 |
+
return None
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def _create_ecb_cipher(factory, **kwargs):
|
| 206 |
+
"""Instantiate a cipher object that performs ECB encryption/decryption.
|
| 207 |
+
|
| 208 |
+
:Parameters:
|
| 209 |
+
factory : module
|
| 210 |
+
The underlying block cipher, a module from ``Crypto.Cipher``.
|
| 211 |
+
|
| 212 |
+
All keywords are passed to the underlying block cipher.
|
| 213 |
+
See the relevant documentation for details (at least ``key`` will need
|
| 214 |
+
to be present"""
|
| 215 |
+
|
| 216 |
+
cipher_state = factory._create_base_cipher(kwargs)
|
| 217 |
+
cipher_state.block_size = factory.block_size
|
| 218 |
+
if kwargs:
|
| 219 |
+
raise TypeError("Unknown parameters for ECB: %s" % str(kwargs))
|
| 220 |
+
return EcbMode(cipher_state)
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_gcm.pyi
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from types import ModuleType
|
| 2 |
+
from typing import Union, Tuple, Dict, overload, Optional
|
| 3 |
+
|
| 4 |
+
__all__ = ['GcmMode']
|
| 5 |
+
|
| 6 |
+
Buffer = Union[bytes, bytearray, memoryview]
|
| 7 |
+
|
| 8 |
+
class GcmMode(object):
|
| 9 |
+
block_size: int
|
| 10 |
+
nonce: Buffer
|
| 11 |
+
|
| 12 |
+
def __init__(self,
|
| 13 |
+
factory: ModuleType,
|
| 14 |
+
key: Buffer,
|
| 15 |
+
nonce: Buffer,
|
| 16 |
+
mac_len: int,
|
| 17 |
+
cipher_params: Dict) -> None: ...
|
| 18 |
+
|
| 19 |
+
def update(self, assoc_data: Buffer) -> GcmMode: ...
|
| 20 |
+
|
| 21 |
+
@overload
|
| 22 |
+
def encrypt(self, plaintext: Buffer) -> bytes: ...
|
| 23 |
+
@overload
|
| 24 |
+
def encrypt(self, plaintext: Buffer, output: Union[bytearray, memoryview]) -> None: ...
|
| 25 |
+
@overload
|
| 26 |
+
def decrypt(self, plaintext: Buffer) -> bytes: ...
|
| 27 |
+
@overload
|
| 28 |
+
def decrypt(self, plaintext: Buffer, output: Union[bytearray, memoryview]) -> None: ...
|
| 29 |
+
|
| 30 |
+
def digest(self) -> bytes: ...
|
| 31 |
+
def hexdigest(self) -> str: ...
|
| 32 |
+
def verify(self, received_mac_tag: Buffer) -> None: ...
|
| 33 |
+
def hexverify(self, hex_mac_tag: str) -> None: ...
|
| 34 |
+
|
| 35 |
+
@overload
|
| 36 |
+
def encrypt_and_digest(self,
|
| 37 |
+
plaintext: Buffer) -> Tuple[bytes, bytes]: ...
|
| 38 |
+
@overload
|
| 39 |
+
def encrypt_and_digest(self,
|
| 40 |
+
plaintext: Buffer,
|
| 41 |
+
output: Buffer) -> Tuple[None, bytes]: ...
|
| 42 |
+
def decrypt_and_verify(self,
|
| 43 |
+
ciphertext: Buffer,
|
| 44 |
+
received_mac_tag: Buffer,
|
| 45 |
+
output: Optional[Union[bytearray, memoryview]] = ...) -> bytes: ...
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_ocb.py
ADDED
|
@@ -0,0 +1,532 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ===================================================================
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2014, Legrandin <helderijs@gmail.com>
|
| 4 |
+
# All rights reserved.
|
| 5 |
+
#
|
| 6 |
+
# Redistribution and use in source and binary forms, with or without
|
| 7 |
+
# modification, are permitted provided that the following conditions
|
| 8 |
+
# are met:
|
| 9 |
+
#
|
| 10 |
+
# 1. Redistributions of source code must retain the above copyright
|
| 11 |
+
# notice, this list of conditions and the following disclaimer.
|
| 12 |
+
# 2. Redistributions in binary form must reproduce the above copyright
|
| 13 |
+
# notice, this list of conditions and the following disclaimer in
|
| 14 |
+
# the documentation and/or other materials provided with the
|
| 15 |
+
# distribution.
|
| 16 |
+
#
|
| 17 |
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 18 |
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 19 |
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
| 20 |
+
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
| 21 |
+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
| 22 |
+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
| 23 |
+
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
| 24 |
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 25 |
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
| 26 |
+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
| 27 |
+
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 28 |
+
# POSSIBILITY OF SUCH DAMAGE.
|
| 29 |
+
# ===================================================================
|
| 30 |
+
|
| 31 |
+
"""
|
| 32 |
+
Offset Codebook (OCB) mode.
|
| 33 |
+
|
| 34 |
+
OCB is Authenticated Encryption with Associated Data (AEAD) cipher mode
|
| 35 |
+
designed by Prof. Phillip Rogaway and specified in `RFC7253`_.
|
| 36 |
+
|
| 37 |
+
The algorithm provides both authenticity and privacy, it is very efficient,
|
| 38 |
+
it uses only one key and it can be used in online mode (so that encryption
|
| 39 |
+
or decryption can start before the end of the message is available).
|
| 40 |
+
|
| 41 |
+
This module implements the third and last variant of OCB (OCB3) and it only
|
| 42 |
+
works in combination with a 128-bit block symmetric cipher, like AES.
|
| 43 |
+
|
| 44 |
+
OCB is patented in US but `free licenses`_ exist for software implementations
|
| 45 |
+
meant for non-military purposes.
|
| 46 |
+
|
| 47 |
+
Example:
|
| 48 |
+
>>> from Crypto.Cipher import AES
|
| 49 |
+
>>> from Crypto.Random import get_random_bytes
|
| 50 |
+
>>>
|
| 51 |
+
>>> key = get_random_bytes(32)
|
| 52 |
+
>>> cipher = AES.new(key, AES.MODE_OCB)
|
| 53 |
+
>>> plaintext = b"Attack at dawn"
|
| 54 |
+
>>> ciphertext, mac = cipher.encrypt_and_digest(plaintext)
|
| 55 |
+
>>> # Deliver cipher.nonce, ciphertext and mac
|
| 56 |
+
...
|
| 57 |
+
>>> cipher = AES.new(key, AES.MODE_OCB, nonce=nonce)
|
| 58 |
+
>>> try:
|
| 59 |
+
>>> plaintext = cipher.decrypt_and_verify(ciphertext, mac)
|
| 60 |
+
>>> except ValueError:
|
| 61 |
+
>>> print "Invalid message"
|
| 62 |
+
>>> else:
|
| 63 |
+
>>> print plaintext
|
| 64 |
+
|
| 65 |
+
:undocumented: __package__
|
| 66 |
+
|
| 67 |
+
.. _RFC7253: http://www.rfc-editor.org/info/rfc7253
|
| 68 |
+
.. _free licenses: http://web.cs.ucdavis.edu/~rogaway/ocb/license.htm
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
import struct
|
| 72 |
+
from binascii import unhexlify
|
| 73 |
+
|
| 74 |
+
from Crypto.Util.py3compat import bord, _copy_bytes, bchr
|
| 75 |
+
from Crypto.Util.number import long_to_bytes, bytes_to_long
|
| 76 |
+
from Crypto.Util.strxor import strxor
|
| 77 |
+
|
| 78 |
+
from Crypto.Hash import BLAKE2s
|
| 79 |
+
from Crypto.Random import get_random_bytes
|
| 80 |
+
|
| 81 |
+
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, VoidPointer,
|
| 82 |
+
create_string_buffer, get_raw_buffer,
|
| 83 |
+
SmartPointer, c_size_t, c_uint8_ptr,
|
| 84 |
+
is_buffer)
|
| 85 |
+
|
| 86 |
+
_raw_ocb_lib = load_pycryptodome_raw_lib("Crypto.Cipher._raw_ocb", """
|
| 87 |
+
int OCB_start_operation(void *cipher,
|
| 88 |
+
const uint8_t *offset_0,
|
| 89 |
+
size_t offset_0_len,
|
| 90 |
+
void **pState);
|
| 91 |
+
int OCB_encrypt(void *state,
|
| 92 |
+
const uint8_t *in,
|
| 93 |
+
uint8_t *out,
|
| 94 |
+
size_t data_len);
|
| 95 |
+
int OCB_decrypt(void *state,
|
| 96 |
+
const uint8_t *in,
|
| 97 |
+
uint8_t *out,
|
| 98 |
+
size_t data_len);
|
| 99 |
+
int OCB_update(void *state,
|
| 100 |
+
const uint8_t *in,
|
| 101 |
+
size_t data_len);
|
| 102 |
+
int OCB_digest(void *state,
|
| 103 |
+
uint8_t *tag,
|
| 104 |
+
size_t tag_len);
|
| 105 |
+
int OCB_stop_operation(void *state);
|
| 106 |
+
""")
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class OcbMode(object):
|
| 110 |
+
"""Offset Codebook (OCB) mode.
|
| 111 |
+
|
| 112 |
+
:undocumented: __init__
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
def __init__(self, factory, nonce, mac_len, cipher_params):
|
| 116 |
+
|
| 117 |
+
if factory.block_size != 16:
|
| 118 |
+
raise ValueError("OCB mode is only available for ciphers"
|
| 119 |
+
" that operate on 128 bits blocks")
|
| 120 |
+
|
| 121 |
+
self.block_size = 16
|
| 122 |
+
"""The block size of the underlying cipher, in bytes."""
|
| 123 |
+
|
| 124 |
+
self.nonce = _copy_bytes(None, None, nonce)
|
| 125 |
+
"""Nonce used for this session."""
|
| 126 |
+
if len(nonce) not in range(1, 16):
|
| 127 |
+
raise ValueError("Nonce must be at most 15 bytes long")
|
| 128 |
+
if not is_buffer(nonce):
|
| 129 |
+
raise TypeError("Nonce must be bytes, bytearray or memoryview")
|
| 130 |
+
|
| 131 |
+
self._mac_len = mac_len
|
| 132 |
+
if not 8 <= mac_len <= 16:
|
| 133 |
+
raise ValueError("MAC tag must be between 8 and 16 bytes long")
|
| 134 |
+
|
| 135 |
+
# Cache for MAC tag
|
| 136 |
+
self._mac_tag = None
|
| 137 |
+
|
| 138 |
+
# Cache for unaligned associated data
|
| 139 |
+
self._cache_A = b""
|
| 140 |
+
|
| 141 |
+
# Cache for unaligned ciphertext/plaintext
|
| 142 |
+
self._cache_P = b""
|
| 143 |
+
|
| 144 |
+
# Allowed transitions after initialization
|
| 145 |
+
self._next = ["update", "encrypt", "decrypt",
|
| 146 |
+
"digest", "verify"]
|
| 147 |
+
|
| 148 |
+
# Compute Offset_0
|
| 149 |
+
params_without_key = dict(cipher_params)
|
| 150 |
+
key = params_without_key.pop("key")
|
| 151 |
+
|
| 152 |
+
taglen_mod128 = (self._mac_len * 8) % 128
|
| 153 |
+
if len(self.nonce) < 15:
|
| 154 |
+
nonce = bchr(taglen_mod128 << 1) +\
|
| 155 |
+
b'\x00' * (14 - len(nonce)) +\
|
| 156 |
+
b'\x01' +\
|
| 157 |
+
self.nonce
|
| 158 |
+
else:
|
| 159 |
+
nonce = bchr((taglen_mod128 << 1) | 0x01) +\
|
| 160 |
+
self.nonce
|
| 161 |
+
|
| 162 |
+
bottom_bits = bord(nonce[15]) & 0x3F # 6 bits, 0..63
|
| 163 |
+
top_bits = bord(nonce[15]) & 0xC0 # 2 bits
|
| 164 |
+
|
| 165 |
+
ktop_cipher = factory.new(key,
|
| 166 |
+
factory.MODE_ECB,
|
| 167 |
+
**params_without_key)
|
| 168 |
+
ktop = ktop_cipher.encrypt(struct.pack('15sB',
|
| 169 |
+
nonce[:15],
|
| 170 |
+
top_bits))
|
| 171 |
+
|
| 172 |
+
stretch = ktop + strxor(ktop[:8], ktop[1:9]) # 192 bits
|
| 173 |
+
offset_0 = long_to_bytes(bytes_to_long(stretch) >>
|
| 174 |
+
(64 - bottom_bits), 24)[8:]
|
| 175 |
+
|
| 176 |
+
# Create low-level cipher instance
|
| 177 |
+
raw_cipher = factory._create_base_cipher(cipher_params)
|
| 178 |
+
if cipher_params:
|
| 179 |
+
raise TypeError("Unknown keywords: " + str(cipher_params))
|
| 180 |
+
|
| 181 |
+
self._state = VoidPointer()
|
| 182 |
+
result = _raw_ocb_lib.OCB_start_operation(raw_cipher.get(),
|
| 183 |
+
offset_0,
|
| 184 |
+
c_size_t(len(offset_0)),
|
| 185 |
+
self._state.address_of())
|
| 186 |
+
if result:
|
| 187 |
+
raise ValueError("Error %d while instantiating the OCB mode"
|
| 188 |
+
% result)
|
| 189 |
+
|
| 190 |
+
# Ensure that object disposal of this Python object will (eventually)
|
| 191 |
+
# free the memory allocated by the raw library for the cipher mode
|
| 192 |
+
self._state = SmartPointer(self._state.get(),
|
| 193 |
+
_raw_ocb_lib.OCB_stop_operation)
|
| 194 |
+
|
| 195 |
+
# Memory allocated for the underlying block cipher is now owed
|
| 196 |
+
# by the cipher mode
|
| 197 |
+
raw_cipher.release()
|
| 198 |
+
|
| 199 |
+
def _update(self, assoc_data, assoc_data_len):
|
| 200 |
+
result = _raw_ocb_lib.OCB_update(self._state.get(),
|
| 201 |
+
c_uint8_ptr(assoc_data),
|
| 202 |
+
c_size_t(assoc_data_len))
|
| 203 |
+
if result:
|
| 204 |
+
raise ValueError("Error %d while computing MAC in OCB mode" % result)
|
| 205 |
+
|
| 206 |
+
def update(self, assoc_data):
|
| 207 |
+
"""Process the associated data.
|
| 208 |
+
|
| 209 |
+
If there is any associated data, the caller has to invoke
|
| 210 |
+
this method one or more times, before using
|
| 211 |
+
``decrypt`` or ``encrypt``.
|
| 212 |
+
|
| 213 |
+
By *associated data* it is meant any data (e.g. packet headers) that
|
| 214 |
+
will not be encrypted and will be transmitted in the clear.
|
| 215 |
+
However, the receiver shall still able to detect modifications.
|
| 216 |
+
|
| 217 |
+
If there is no associated data, this method must not be called.
|
| 218 |
+
|
| 219 |
+
The caller may split associated data in segments of any size, and
|
| 220 |
+
invoke this method multiple times, each time with the next segment.
|
| 221 |
+
|
| 222 |
+
:Parameters:
|
| 223 |
+
assoc_data : bytes/bytearray/memoryview
|
| 224 |
+
A piece of associated data.
|
| 225 |
+
"""
|
| 226 |
+
|
| 227 |
+
if "update" not in self._next:
|
| 228 |
+
raise TypeError("update() can only be called"
|
| 229 |
+
" immediately after initialization")
|
| 230 |
+
|
| 231 |
+
self._next = ["encrypt", "decrypt", "digest",
|
| 232 |
+
"verify", "update"]
|
| 233 |
+
|
| 234 |
+
if len(self._cache_A) > 0:
|
| 235 |
+
filler = min(16 - len(self._cache_A), len(assoc_data))
|
| 236 |
+
self._cache_A += _copy_bytes(None, filler, assoc_data)
|
| 237 |
+
assoc_data = assoc_data[filler:]
|
| 238 |
+
|
| 239 |
+
if len(self._cache_A) < 16:
|
| 240 |
+
return self
|
| 241 |
+
|
| 242 |
+
# Clear the cache, and proceeding with any other aligned data
|
| 243 |
+
self._cache_A, seg = b"", self._cache_A
|
| 244 |
+
self.update(seg)
|
| 245 |
+
|
| 246 |
+
update_len = len(assoc_data) // 16 * 16
|
| 247 |
+
self._cache_A = _copy_bytes(update_len, None, assoc_data)
|
| 248 |
+
self._update(assoc_data, update_len)
|
| 249 |
+
return self
|
| 250 |
+
|
| 251 |
+
def _transcrypt_aligned(self, in_data, in_data_len,
|
| 252 |
+
trans_func, trans_desc):
|
| 253 |
+
|
| 254 |
+
out_data = create_string_buffer(in_data_len)
|
| 255 |
+
result = trans_func(self._state.get(),
|
| 256 |
+
in_data,
|
| 257 |
+
out_data,
|
| 258 |
+
c_size_t(in_data_len))
|
| 259 |
+
if result:
|
| 260 |
+
raise ValueError("Error %d while %sing in OCB mode"
|
| 261 |
+
% (result, trans_desc))
|
| 262 |
+
return get_raw_buffer(out_data)
|
| 263 |
+
|
| 264 |
+
def _transcrypt(self, in_data, trans_func, trans_desc):
|
| 265 |
+
# Last piece to encrypt/decrypt
|
| 266 |
+
if in_data is None:
|
| 267 |
+
out_data = self._transcrypt_aligned(self._cache_P,
|
| 268 |
+
len(self._cache_P),
|
| 269 |
+
trans_func,
|
| 270 |
+
trans_desc)
|
| 271 |
+
self._cache_P = b""
|
| 272 |
+
return out_data
|
| 273 |
+
|
| 274 |
+
# Try to fill up the cache, if it already contains something
|
| 275 |
+
prefix = b""
|
| 276 |
+
if len(self._cache_P) > 0:
|
| 277 |
+
filler = min(16 - len(self._cache_P), len(in_data))
|
| 278 |
+
self._cache_P += _copy_bytes(None, filler, in_data)
|
| 279 |
+
in_data = in_data[filler:]
|
| 280 |
+
|
| 281 |
+
if len(self._cache_P) < 16:
|
| 282 |
+
# We could not manage to fill the cache, so there is certainly
|
| 283 |
+
# no output yet.
|
| 284 |
+
return b""
|
| 285 |
+
|
| 286 |
+
# Clear the cache, and proceeding with any other aligned data
|
| 287 |
+
prefix = self._transcrypt_aligned(self._cache_P,
|
| 288 |
+
len(self._cache_P),
|
| 289 |
+
trans_func,
|
| 290 |
+
trans_desc)
|
| 291 |
+
self._cache_P = b""
|
| 292 |
+
|
| 293 |
+
# Process data in multiples of the block size
|
| 294 |
+
trans_len = len(in_data) // 16 * 16
|
| 295 |
+
result = self._transcrypt_aligned(c_uint8_ptr(in_data),
|
| 296 |
+
trans_len,
|
| 297 |
+
trans_func,
|
| 298 |
+
trans_desc)
|
| 299 |
+
if prefix:
|
| 300 |
+
result = prefix + result
|
| 301 |
+
|
| 302 |
+
# Left-over
|
| 303 |
+
self._cache_P = _copy_bytes(trans_len, None, in_data)
|
| 304 |
+
|
| 305 |
+
return result
|
| 306 |
+
|
| 307 |
+
def encrypt(self, plaintext=None):
|
| 308 |
+
"""Encrypt the next piece of plaintext.
|
| 309 |
+
|
| 310 |
+
After the entire plaintext has been passed (but before `digest`),
|
| 311 |
+
you **must** call this method one last time with no arguments to collect
|
| 312 |
+
the final piece of ciphertext.
|
| 313 |
+
|
| 314 |
+
If possible, use the method `encrypt_and_digest` instead.
|
| 315 |
+
|
| 316 |
+
:Parameters:
|
| 317 |
+
plaintext : bytes/bytearray/memoryview
|
| 318 |
+
The next piece of data to encrypt or ``None`` to signify
|
| 319 |
+
that encryption has finished and that any remaining ciphertext
|
| 320 |
+
has to be produced.
|
| 321 |
+
:Return:
|
| 322 |
+
the ciphertext, as a byte string.
|
| 323 |
+
Its length may not match the length of the *plaintext*.
|
| 324 |
+
"""
|
| 325 |
+
|
| 326 |
+
if "encrypt" not in self._next:
|
| 327 |
+
raise TypeError("encrypt() can only be called after"
|
| 328 |
+
" initialization or an update()")
|
| 329 |
+
|
| 330 |
+
if plaintext is None:
|
| 331 |
+
self._next = ["digest"]
|
| 332 |
+
else:
|
| 333 |
+
self._next = ["encrypt"]
|
| 334 |
+
return self._transcrypt(plaintext, _raw_ocb_lib.OCB_encrypt, "encrypt")
|
| 335 |
+
|
| 336 |
+
def decrypt(self, ciphertext=None):
|
| 337 |
+
"""Decrypt the next piece of ciphertext.
|
| 338 |
+
|
| 339 |
+
After the entire ciphertext has been passed (but before `verify`),
|
| 340 |
+
you **must** call this method one last time with no arguments to collect
|
| 341 |
+
the remaining piece of plaintext.
|
| 342 |
+
|
| 343 |
+
If possible, use the method `decrypt_and_verify` instead.
|
| 344 |
+
|
| 345 |
+
:Parameters:
|
| 346 |
+
ciphertext : bytes/bytearray/memoryview
|
| 347 |
+
The next piece of data to decrypt or ``None`` to signify
|
| 348 |
+
that decryption has finished and that any remaining plaintext
|
| 349 |
+
has to be produced.
|
| 350 |
+
:Return:
|
| 351 |
+
the plaintext, as a byte string.
|
| 352 |
+
Its length may not match the length of the *ciphertext*.
|
| 353 |
+
"""
|
| 354 |
+
|
| 355 |
+
if "decrypt" not in self._next:
|
| 356 |
+
raise TypeError("decrypt() can only be called after"
|
| 357 |
+
" initialization or an update()")
|
| 358 |
+
|
| 359 |
+
if ciphertext is None:
|
| 360 |
+
self._next = ["verify"]
|
| 361 |
+
else:
|
| 362 |
+
self._next = ["decrypt"]
|
| 363 |
+
return self._transcrypt(ciphertext,
|
| 364 |
+
_raw_ocb_lib.OCB_decrypt,
|
| 365 |
+
"decrypt")
|
| 366 |
+
|
| 367 |
+
def _compute_mac_tag(self):
|
| 368 |
+
|
| 369 |
+
if self._mac_tag is not None:
|
| 370 |
+
return
|
| 371 |
+
|
| 372 |
+
if self._cache_A:
|
| 373 |
+
self._update(self._cache_A, len(self._cache_A))
|
| 374 |
+
self._cache_A = b""
|
| 375 |
+
|
| 376 |
+
mac_tag = create_string_buffer(16)
|
| 377 |
+
result = _raw_ocb_lib.OCB_digest(self._state.get(),
|
| 378 |
+
mac_tag,
|
| 379 |
+
c_size_t(len(mac_tag))
|
| 380 |
+
)
|
| 381 |
+
if result:
|
| 382 |
+
raise ValueError("Error %d while computing digest in OCB mode"
|
| 383 |
+
% result)
|
| 384 |
+
self._mac_tag = get_raw_buffer(mac_tag)[:self._mac_len]
|
| 385 |
+
|
| 386 |
+
def digest(self):
|
| 387 |
+
"""Compute the *binary* MAC tag.
|
| 388 |
+
|
| 389 |
+
Call this method after the final `encrypt` (the one with no arguments)
|
| 390 |
+
to obtain the MAC tag.
|
| 391 |
+
|
| 392 |
+
The MAC tag is needed by the receiver to determine authenticity
|
| 393 |
+
of the message.
|
| 394 |
+
|
| 395 |
+
:Return: the MAC, as a byte string.
|
| 396 |
+
"""
|
| 397 |
+
|
| 398 |
+
if "digest" not in self._next:
|
| 399 |
+
raise TypeError("digest() cannot be called now for this cipher")
|
| 400 |
+
|
| 401 |
+
assert(len(self._cache_P) == 0)
|
| 402 |
+
|
| 403 |
+
self._next = ["digest"]
|
| 404 |
+
|
| 405 |
+
if self._mac_tag is None:
|
| 406 |
+
self._compute_mac_tag()
|
| 407 |
+
|
| 408 |
+
return self._mac_tag
|
| 409 |
+
|
| 410 |
+
def hexdigest(self):
|
| 411 |
+
"""Compute the *printable* MAC tag.
|
| 412 |
+
|
| 413 |
+
This method is like `digest`.
|
| 414 |
+
|
| 415 |
+
:Return: the MAC, as a hexadecimal string.
|
| 416 |
+
"""
|
| 417 |
+
return "".join(["%02x" % bord(x) for x in self.digest()])
|
| 418 |
+
|
| 419 |
+
def verify(self, received_mac_tag):
|
| 420 |
+
"""Validate the *binary* MAC tag.
|
| 421 |
+
|
| 422 |
+
Call this method after the final `decrypt` (the one with no arguments)
|
| 423 |
+
to check if the message is authentic and valid.
|
| 424 |
+
|
| 425 |
+
:Parameters:
|
| 426 |
+
received_mac_tag : bytes/bytearray/memoryview
|
| 427 |
+
This is the *binary* MAC, as received from the sender.
|
| 428 |
+
:Raises ValueError:
|
| 429 |
+
if the MAC does not match. The message has been tampered with
|
| 430 |
+
or the key is incorrect.
|
| 431 |
+
"""
|
| 432 |
+
|
| 433 |
+
if "verify" not in self._next:
|
| 434 |
+
raise TypeError("verify() cannot be called now for this cipher")
|
| 435 |
+
|
| 436 |
+
assert(len(self._cache_P) == 0)
|
| 437 |
+
|
| 438 |
+
self._next = ["verify"]
|
| 439 |
+
|
| 440 |
+
if self._mac_tag is None:
|
| 441 |
+
self._compute_mac_tag()
|
| 442 |
+
|
| 443 |
+
secret = get_random_bytes(16)
|
| 444 |
+
mac1 = BLAKE2s.new(digest_bits=160, key=secret, data=self._mac_tag)
|
| 445 |
+
mac2 = BLAKE2s.new(digest_bits=160, key=secret, data=received_mac_tag)
|
| 446 |
+
|
| 447 |
+
if mac1.digest() != mac2.digest():
|
| 448 |
+
raise ValueError("MAC check failed")
|
| 449 |
+
|
| 450 |
+
def hexverify(self, hex_mac_tag):
|
| 451 |
+
"""Validate the *printable* MAC tag.
|
| 452 |
+
|
| 453 |
+
This method is like `verify`.
|
| 454 |
+
|
| 455 |
+
:Parameters:
|
| 456 |
+
hex_mac_tag : string
|
| 457 |
+
This is the *printable* MAC, as received from the sender.
|
| 458 |
+
:Raises ValueError:
|
| 459 |
+
if the MAC does not match. The message has been tampered with
|
| 460 |
+
or the key is incorrect.
|
| 461 |
+
"""
|
| 462 |
+
|
| 463 |
+
self.verify(unhexlify(hex_mac_tag))
|
| 464 |
+
|
| 465 |
+
def encrypt_and_digest(self, plaintext):
|
| 466 |
+
"""Encrypt the message and create the MAC tag in one step.
|
| 467 |
+
|
| 468 |
+
:Parameters:
|
| 469 |
+
plaintext : bytes/bytearray/memoryview
|
| 470 |
+
The entire message to encrypt.
|
| 471 |
+
:Return:
|
| 472 |
+
a tuple with two byte strings:
|
| 473 |
+
|
| 474 |
+
- the encrypted data
|
| 475 |
+
- the MAC
|
| 476 |
+
"""
|
| 477 |
+
|
| 478 |
+
return self.encrypt(plaintext) + self.encrypt(), self.digest()
|
| 479 |
+
|
| 480 |
+
def decrypt_and_verify(self, ciphertext, received_mac_tag):
|
| 481 |
+
"""Decrypted the message and verify its authenticity in one step.
|
| 482 |
+
|
| 483 |
+
:Parameters:
|
| 484 |
+
ciphertext : bytes/bytearray/memoryview
|
| 485 |
+
The entire message to decrypt.
|
| 486 |
+
received_mac_tag : byte string
|
| 487 |
+
This is the *binary* MAC, as received from the sender.
|
| 488 |
+
|
| 489 |
+
:Return: the decrypted data (byte string).
|
| 490 |
+
:Raises ValueError:
|
| 491 |
+
if the MAC does not match. The message has been tampered with
|
| 492 |
+
or the key is incorrect.
|
| 493 |
+
"""
|
| 494 |
+
|
| 495 |
+
plaintext = self.decrypt(ciphertext) + self.decrypt()
|
| 496 |
+
self.verify(received_mac_tag)
|
| 497 |
+
return plaintext
|
| 498 |
+
|
| 499 |
+
|
| 500 |
+
def _create_ocb_cipher(factory, **kwargs):
|
| 501 |
+
"""Create a new block cipher, configured in OCB mode.
|
| 502 |
+
|
| 503 |
+
:Parameters:
|
| 504 |
+
factory : module
|
| 505 |
+
A symmetric cipher module from `Crypto.Cipher`
|
| 506 |
+
(like `Crypto.Cipher.AES`).
|
| 507 |
+
|
| 508 |
+
:Keywords:
|
| 509 |
+
nonce : bytes/bytearray/memoryview
|
| 510 |
+
A value that must never be reused for any other encryption.
|
| 511 |
+
Its length can vary from 1 to 15 bytes.
|
| 512 |
+
If not specified, a random 15 bytes long nonce is generated.
|
| 513 |
+
|
| 514 |
+
mac_len : integer
|
| 515 |
+
Length of the MAC, in bytes.
|
| 516 |
+
It must be in the range ``[8..16]``.
|
| 517 |
+
The default is 16 (128 bits).
|
| 518 |
+
|
| 519 |
+
Any other keyword will be passed to the underlying block cipher.
|
| 520 |
+
See the relevant documentation for details (at least ``key`` will need
|
| 521 |
+
to be present).
|
| 522 |
+
"""
|
| 523 |
+
|
| 524 |
+
try:
|
| 525 |
+
nonce = kwargs.pop("nonce", None)
|
| 526 |
+
if nonce is None:
|
| 527 |
+
nonce = get_random_bytes(15)
|
| 528 |
+
mac_len = kwargs.pop("mac_len", 16)
|
| 529 |
+
except KeyError as e:
|
| 530 |
+
raise TypeError("Keyword missing: " + str(e))
|
| 531 |
+
|
| 532 |
+
return OcbMode(factory, nonce, mac_len, kwargs)
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_mode_siv.pyi
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from types import ModuleType
|
| 2 |
+
from typing import Union, Tuple, Dict, Optional, overload
|
| 3 |
+
|
| 4 |
+
Buffer = Union[bytes, bytearray, memoryview]
|
| 5 |
+
|
| 6 |
+
__all__ = ['SivMode']
|
| 7 |
+
|
| 8 |
+
class SivMode(object):
|
| 9 |
+
block_size: int
|
| 10 |
+
nonce: bytes
|
| 11 |
+
|
| 12 |
+
def __init__(self,
|
| 13 |
+
factory: ModuleType,
|
| 14 |
+
key: Buffer,
|
| 15 |
+
nonce: Buffer,
|
| 16 |
+
kwargs: Dict) -> None: ...
|
| 17 |
+
|
| 18 |
+
def update(self, component: Buffer) -> SivMode: ...
|
| 19 |
+
|
| 20 |
+
def encrypt(self, plaintext: Buffer) -> bytes: ...
|
| 21 |
+
def decrypt(self, plaintext: Buffer) -> bytes: ...
|
| 22 |
+
|
| 23 |
+
def digest(self) -> bytes: ...
|
| 24 |
+
def hexdigest(self) -> str: ...
|
| 25 |
+
def verify(self, received_mac_tag: Buffer) -> None: ...
|
| 26 |
+
def hexverify(self, hex_mac_tag: str) -> None: ...
|
| 27 |
+
|
| 28 |
+
@overload
|
| 29 |
+
def encrypt_and_digest(self,
|
| 30 |
+
plaintext: Buffer) -> Tuple[bytes, bytes]: ...
|
| 31 |
+
@overload
|
| 32 |
+
def encrypt_and_digest(self,
|
| 33 |
+
plaintext: Buffer,
|
| 34 |
+
output: Buffer) -> Tuple[None, bytes]: ...
|
| 35 |
+
def decrypt_and_verify(self,
|
| 36 |
+
ciphertext: Buffer,
|
| 37 |
+
received_mac_tag: Buffer,
|
| 38 |
+
output: Optional[Union[bytearray, memoryview]] = ...) -> bytes: ...
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_raw_aes.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f4265907fce6174d7e043039af0f244d650861a8b1c50d135b738602dde37578
|
| 3 |
+
size 106808
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_raw_blowfish.abi3.so
ADDED
|
Binary file (78.6 kB). View file
|
|
|
minigpt2/lib/python3.10/site-packages/Crypto/Cipher/_raw_cast.abi3.so
ADDED
|
Binary file (57.4 kB). View file
|
|
|