/
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_v1_5.py
(7979B)
# -*- coding: utf-8 -*- # # Cipher/PKCS1-v1_5.py : PKCS#1 v1.5 # # =================================================================== # 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. # =================================================================== __all__ = [ 'new', 'PKCS115_Cipher' ] from Cryptodome.Util.number import ceil_div, bytes_to_long, long_to_bytes from Cryptodome.Util.py3compat import bord, _copy_bytes import Cryptodome.Util.number from Cryptodome import Random class PKCS115_Cipher: """This cipher can perform PKCS#1 v1.5 RSA encryption or decryption. Do not instantiate directly. Use :func:`Cryptodome.Cipher.PKCS1_v1_5.new` instead.""" def __init__(self, key, randfunc): """Initialize this PKCS#1 v1.5 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. randfunc : callable Function that returns random bytes. """ self._key = key self._randfunc = randfunc def can_encrypt(self): """Return True if this cipher object can be used for encryption.""" return self._key.can_encrypt() def can_decrypt(self): """Return True if this cipher object can be used for decryption.""" return self._key.can_decrypt() def encrypt(self, message): """Produce the PKCS#1 v1.5 encryption of a message. This function is named ``RSAES-PKCS1-V1_5-ENCRYPT``, and it is specified in `section 7.2.1 of RFC8017 <https://tools.ietf.org/html/rfc8017#page-28>`_. :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 11. :type message: bytes/bytearray/memoryview :Returns: A byte string, the ciphertext in which the message is encrypted. It is as long as the RSA modulus (in bytes). :Raises ValueError: If the RSA key length is not sufficiently long to deal with the given message. """ # See 7.2.1 in RFC8017 modBits = Cryptodome.Util.number.size(self._key.n) k = ceil_div(modBits,8) # Convert from bits to bytes mLen = len(message) # Step 1 if mLen > k - 11: raise ValueError("Plaintext is too long.") # Step 2a ps = [] while len(ps) != k - mLen - 3: new_byte = self._randfunc(1) if bord(new_byte[0]) == 0x00: continue ps.append(new_byte) ps = b"".join(ps) assert(len(ps) == k - mLen - 3) # Step 2b em = b'\x00\x02' + ps + b'\x00' + _copy_bytes(None, None, message) # 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, sentinel): r"""Decrypt a PKCS#1 v1.5 ciphertext. This function is named ``RSAES-PKCS1-V1_5-DECRYPT``, and is specified in `section 7.2.2 of RFC8017 <https://tools.ietf.org/html/rfc8017#page-29>`_. :param ciphertext: The ciphertext that contains the message to recover. :type ciphertext: bytes/bytearray/memoryview :param sentinel: The object to return whenever an error is detected. :type sentinel: any type :Returns: A byte string. It is either the original message or the ``sentinel`` (in case of an error). :Raises ValueError: If the ciphertext length is incorrect :Raises TypeError: If the RSA key has no private half (i.e. it cannot be used for decyption). .. warning:: You should **never** let the party who submitted the ciphertext know that this function returned the ``sentinel`` value. Armed with such knowledge (for a fair amount of carefully crafted but invalid ciphertexts), an attacker is able to recontruct the plaintext of any other encryption that were carried out with the same RSA public key (see `Bleichenbacher's`__ attack). In general, it should not be possible for the other party to distinguish whether processing at the server side failed because the value returned was a ``sentinel`` as opposed to a random, invalid message. In fact, the second option is not that unlikely: encryption done according to PKCS#1 v1.5 embeds no good integrity check. There is roughly one chance in 2\ :sup:`16` for a random ciphertext to be returned as a valid message (although random looking). It is therefore advisabled to: 1. Select as ``sentinel`` a value that resembles a plausable random, invalid message. 2. Not report back an error as soon as you detect a ``sentinel`` value. Put differently, you should not explicitly check if the returned value is the ``sentinel`` or not. 3. Cover all possible errors with a single, generic error indicator. 4. Embed into the definition of ``message`` (at the protocol level) a digest (e.g. ``SHA-1``). It is recommended for it to be the rightmost part ``message``. 5. Where possible, monitor the number of errors due to ciphertexts originating from the same party, and slow down the rate of the requests from such party (or even blacklist it altogether). **If you are designing a new protocol, consider using the more robust PKCS#1 OAEP.** .. __: http://www.bell-labs.com/user/bleichen/papers/pkcs.ps """ # See 7.2.1 in RFC3447 modBits = Cryptodome.Util.number.size(self._key.n) k = ceil_div(modBits,8) # Convert from bits to bytes # Step 1 if len(ciphertext) != k: 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 3 sep = em.find(b'\x00', 2) if not em.startswith(b'\x00\x02') or sep < 10: return sentinel # Step 4 return em[sep + 1:] def new(key, randfunc=None): """Create a cipher for performing PKCS#1 v1.5 encryption or decryption. :param key: The key to use to encrypt or decrypt the message. This is a `Cryptodome.PublicKey.RSA` object. Decryption is only possible if *key* is a private RSA key. :type key: RSA key object :param randfunc: Function that return random bytes. The default is :func:`Cryptodome.Random.get_random_bytes`. :type randfunc: callable :returns: A cipher object `PKCS115_Cipher`. """ if randfunc is None: randfunc = Random.get_random_bytes return PKCS115_Cipher(key, randfunc)
Save
cmd:
run