/
opt
/
imh-python
/
lib
/
python3.9
/
site-packages
/
Cryptodome
/
Cipher
/
/opt/imh-python/lib/python3.9/site-packages/Cryptodome/Cipher
mkdir
upload
Name
Size
Mode
Actions
__pycache__/
-
0755
rm
AES.py
9569
0644
edit
dl
rm
AES.pyi
1387
0644
edit
dl
rm
ARC2.py
7026
0644
edit
dl
rm
ARC2.pyi
982
0644
edit
dl
rm
ARC4.py
5168
0644
edit
dl
rm
ARC4.pyi
431
0644
edit
dl
rm
Blowfish.py
5976
0644
edit
dl
rm
Blowfish.pyi
1018
0644
edit
dl
rm
CAST.py
6087
0644
edit
dl
rm
CAST.pyi
983
0644
edit
dl
rm
ChaCha20.py
10754
0644
edit
dl
rm
ChaCha20.pyi
762
0644
edit
dl
rm
ChaCha20_Poly1305.py
11561
0644
edit
dl
rm
ChaCha20_Poly1305.pyi
1068
0644
edit
dl
rm
DES.py
5963
0644
edit
dl
rm
DES.pyi
963
0644
edit
dl
rm
DES3.py
6941
0644
edit
dl
rm
DES3.pyi
1033
0644
edit
dl
rm
PKCS1_OAEP.py
8880
0644
edit
dl
rm
PKCS1_OAEP.pyi
1183
0644
edit
dl
rm
PKCS1_v1_5.py
7979
0644
edit
dl
rm
PKCS1_v1_5.pyi
571
0644
edit
dl
rm
Salsa20.py
6369
0644
edit
dl
rm
Salsa20.pyi
744
0644
edit
dl
rm
_ARC4.cpython-39-x86_64-linux-gnu.so
8072
0755
edit
dl
rm
_chacha20.cpython-39-x86_64-linux-gnu.so
11024
0755
edit
dl
rm
_EKSBlowfish.py
5217
0644
edit
dl
rm
_EKSBlowfish.pyi
270
0644
edit
dl
rm
_mode_cbc.py
10971
0644
edit
dl
rm
_mode_cbc.pyi
691
0644
edit
dl
rm
_mode_ccm.py
24476
0644
edit
dl
rm
_mode_ccm.pyi
1600
0644
edit
dl
rm
_mode_cfb.py
10821
0644
edit
dl
rm
_mode_cfb.pyi
731
0644
edit
dl
rm
_mode_ctr.py
15920
0644
edit
dl
rm
_mode_ctr.pyi
804
0644
edit
dl
rm
_mode_eax.py
14543
0644
edit
dl
rm
_mode_eax.pyi
1545
0644
edit
dl
rm
_mode_ecb.py
8222
0644
edit
dl
rm
_mode_ecb.pyi
596
0644
edit
dl
rm
_mode_gcm.py
21402
0644
edit
dl
rm
_mode_gcm.pyi
1541
0644
edit
dl
rm
_mode_ocb.py
19838
0644
edit
dl
rm
_mode_ocb.pyi
1231
0644
edit
dl
rm
_mode_ofb.py
10301
0644
edit
dl
rm
_mode_ofb.pyi
695
0644
edit
dl
rm
_mode_openpgp.py
7061
0644
edit
dl
rm
_mode_openpgp.pyi
556
0644
edit
dl
rm
_mode_siv.py
14094
0644
edit
dl
rm
_mode_siv.pyi
1261
0644
edit
dl
rm
_raw_aes.cpython-39-x86_64-linux-gnu.so
26816
0755
edit
dl
rm
_raw_aesni.cpython-39-x86_64-linux-gnu.so
11648
0755
edit
dl
rm
_raw_arc2.cpython-39-x86_64-linux-gnu.so
10528
0755
edit
dl
rm
_raw_blowfish.cpython-39-x86_64-linux-gnu.so
13568
0755
edit
dl
rm
_raw_cast.cpython-39-x86_64-linux-gnu.so
19864
0755
edit
dl
rm
_raw_cbc.cpython-39-x86_64-linux-gnu.so
8672
0755
edit
dl
rm
_raw_cfb.cpython-39-x86_64-linux-gnu.so
8928
0755
edit
dl
rm
_raw_ctr.cpython-39-x86_64-linux-gnu.so
9200
0755
edit
dl
rm
_raw_des.cpython-39-x86_64-linux-gnu.so
45120
0755
edit
dl
rm
_raw_des3.cpython-39-x86_64-linux-gnu.so
45408
0755
edit
dl
rm
_raw_ecb.cpython-39-x86_64-linux-gnu.so
6528
0755
edit
dl
rm
_raw_eksblowfish.cpython-39-x86_64-linux-gnu.so
18888
0755
edit
dl
rm
_raw_ocb.cpython-39-x86_64-linux-gnu.so
11456
0755
edit
dl
rm
_raw_ofb.cpython-39-x86_64-linux-gnu.so
7496
0755
edit
dl
rm
_Salsa20.cpython-39-x86_64-linux-gnu.so
8824
0755
edit
dl
rm
__init__.py
2888
0644
edit
dl
rm
__init__.pyi
0
0644
edit
dl
rm
Edit:
/opt/imh-python/lib/python3.9/site-packages/Cryptodome/Cipher/PKCS1_OAEP.py
(8880B)
# -*- coding: utf-8 -*- # # Cipher/PKCS1_OAEP.py : PKCS#1 OAEP # # =================================================================== # The contents of this file are dedicated to the public domain. To # the extent that dedication to the public domain is not available, # everyone is granted a worldwide, perpetual, royalty-free, # non-exclusive license to exercise all rights associated with the # contents of this file for any purpose whatsoever. # No rights are reserved. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # =================================================================== from Cryptodome.Signature.pss import MGF1 import Cryptodome.Hash.SHA1 from Cryptodome.Util.py3compat import bord, _copy_bytes import Cryptodome.Util.number from Cryptodome.Util.number import ceil_div, bytes_to_long, long_to_bytes from Cryptodome.Util.strxor import strxor from Cryptodome import Random class PKCS1OAEP_Cipher: """Cipher object for PKCS#1 v1.5 OAEP. Do not create directly: use :func:`new` instead.""" def __init__(self, key, hashAlgo, mgfunc, label, randfunc): """Initialize this PKCS#1 OAEP cipher object. :Parameters: key : an RSA key object If a private half is given, both encryption and decryption are possible. If a public half is given, only encryption is possible. hashAlgo : hash object The hash function to use. This can be a module under `Cryptodome.Hash` or an existing hash object created from any of such modules. If not specified, `Cryptodome.Hash.SHA1` is used. mgfunc : callable A mask generation function that accepts two parameters: a string to use as seed, and the lenth of the mask to generate, in bytes. If not specified, the standard MGF1 consistent with ``hashAlgo`` is used (a safe choice). label : bytes/bytearray/memoryview A label to apply to this particular encryption. If not specified, an empty string is used. Specifying a label does not improve security. randfunc : callable A function that returns random bytes. :attention: Modify the mask generation function only if you know what you are doing. Sender and receiver must use the same one. """ self._key = key if hashAlgo: self._hashObj = hashAlgo else: self._hashObj = Cryptodome.Hash.SHA1 if mgfunc: self._mgf = mgfunc else: self._mgf = lambda x,y: MGF1(x,y,self._hashObj) self._label = _copy_bytes(None, None, label) self._randfunc = randfunc def can_encrypt(self): """Legacy function to check if you can call :meth:`encrypt`. .. deprecated:: 3.0""" return self._key.can_encrypt() def can_decrypt(self): """Legacy function to check if you can call :meth:`decrypt`. .. deprecated:: 3.0""" return self._key.can_decrypt() def encrypt(self, message): """Encrypt a message with PKCS#1 OAEP. :param message: The message to encrypt, also known as plaintext. It can be of variable length, but not longer than the RSA modulus (in bytes) minus 2, minus twice the hash output size. For instance, if you use RSA 2048 and SHA-256, the longest message you can encrypt is 190 byte long. :type message: bytes/bytearray/memoryview :returns: The ciphertext, as large as the RSA modulus. :rtype: bytes :raises ValueError: if the message is too long. """ # See 7.1.1 in RFC3447 modBits = Cryptodome.Util.number.size(self._key.n) k = ceil_div(modBits, 8) # Convert from bits to bytes hLen = self._hashObj.digest_size mLen = len(message) # Step 1b ps_len = k - mLen - 2 * hLen - 2 if ps_len < 0: raise ValueError("Plaintext is too long.") # Step 2a lHash = self._hashObj.new(self._label).digest() # Step 2b ps = b'\x00' * ps_len # Step 2c db = lHash + ps + b'\x01' + _copy_bytes(None, None, message) # Step 2d ros = self._randfunc(hLen) # Step 2e dbMask = self._mgf(ros, k-hLen-1) # Step 2f maskedDB = strxor(db, dbMask) # Step 2g seedMask = self._mgf(maskedDB, hLen) # Step 2h maskedSeed = strxor(ros, seedMask) # Step 2i em = b'\x00' + maskedSeed + maskedDB # Step 3a (OS2IP) em_int = bytes_to_long(em) # Step 3b (RSAEP) m_int = self._key._encrypt(em_int) # Step 3c (I2OSP) c = long_to_bytes(m_int, k) return c def decrypt(self, ciphertext): """Decrypt a message with PKCS#1 OAEP. :param ciphertext: The encrypted message. :type ciphertext: bytes/bytearray/memoryview :returns: The original message (plaintext). :rtype: bytes :raises ValueError: if the ciphertext has the wrong length, or if decryption fails the integrity check (in which case, the decryption key is probably wrong). :raises TypeError: if the RSA key has no private half (i.e. you are trying to decrypt using a public key). """ # See 7.1.2 in RFC3447 modBits = Cryptodome.Util.number.size(self._key.n) k = ceil_div(modBits,8) # Convert from bits to bytes hLen = self._hashObj.digest_size # Step 1b and 1c if len(ciphertext) != k or k<hLen+2: raise ValueError("Ciphertext with incorrect length.") # Step 2a (O2SIP) ct_int = bytes_to_long(ciphertext) # Step 2b (RSADP) m_int = self._key._decrypt(ct_int) # Complete step 2c (I2OSP) em = long_to_bytes(m_int, k) # Step 3a lHash = self._hashObj.new(self._label).digest() # Step 3b y = em[0] # y must be 0, but we MUST NOT check it here in order not to # allow attacks like Manger's (http://dl.acm.org/citation.cfm?id=704143) maskedSeed = em[1:hLen+1] maskedDB = em[hLen+1:] # Step 3c seedMask = self._mgf(maskedDB, hLen) # Step 3d seed = strxor(maskedSeed, seedMask) # Step 3e dbMask = self._mgf(seed, k-hLen-1) # Step 3f db = strxor(maskedDB, dbMask) # Step 3g one_pos = db[hLen:].find(b'\x01') lHash1 = db[:hLen] invalid = bord(y) | int(one_pos < 0) hash_compare = strxor(lHash1, lHash) for x in hash_compare: invalid |= bord(x) for x in db[hLen:one_pos]: invalid |= bord(x) if invalid != 0: raise ValueError("Incorrect decryption.") # Step 4 return db[hLen + one_pos + 1:] def new(key, hashAlgo=None, mgfunc=None, label=b'', randfunc=None): """Return a cipher object :class:`PKCS1OAEP_Cipher` that can be used to perform PKCS#1 OAEP encryption or decryption. :param key: The key object to use to encrypt or decrypt the message. Decryption is only possible with a private RSA key. :type key: RSA key object :param hashAlgo: The hash function to use. This can be a module under `Cryptodome.Hash` or an existing hash object created from any of such modules. If not specified, `Cryptodome.Hash.SHA1` is used. :type hashAlgo: hash object :param mgfunc: A mask generation function that accepts two parameters: a string to use as seed, and the lenth of the mask to generate, in bytes. If not specified, the standard MGF1 consistent with ``hashAlgo`` is used (a safe choice). :type mgfunc: callable :param label: A label to apply to this particular encryption. If not specified, an empty string is used. Specifying a label does not improve security. :type label: bytes/bytearray/memoryview :param randfunc: A function that returns random bytes. The default is `Random.get_random_bytes`. :type randfunc: callable """ if randfunc is None: randfunc = Random.get_random_bytes return PKCS1OAEP_Cipher(key, hashAlgo, mgfunc, label, randfunc)
Save
cmd:
run