src/pypdf/_crypt_providers/_fallback.py
1# Copyright (c) 2023, exiledkingcc
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright notice,
9# this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above copyright notice,
11# this list of conditions and the following disclaimer in the documentation
12# and/or other materials provided with the distribution.
13# * The name of the author may not be used to endorse or promote products
14# derived from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27
28from pypdf._crypt_providers._base import CryptBase
29from pypdf.errors import DependencyError
30
31_DEPENDENCY_ERROR_STR = "cryptography>=3.1 is required for AES algorithm"
32
33
34crypt_provider = ("local_crypt_fallback", "0.0.0")
35
36
37class CryptRC4(CryptBase):
38 def __init__(self, key: bytes) -> None:
39 s = bytearray(range(256))
40 key_length = len(key)
41 j = 0
42 for i in range(256):
43 s_i = s[i]
44 j = (j + s_i + key[i % key_length]) & 0xFF
45 s[i] = s[j]
46 s[j] = s_i
47 self.s = s
48
49 def encrypt(self, data: bytes) -> bytes:
50 s = bytearray(self.s)
51 keystream = bytearray(len(data))
52 i = j = 0
53 for k in range(len(data)):
54 i = (i + 1) & 0xFF
55 s_i = s[i]
56 j = (j + s_i) & 0xFF
57 s_j = s[j]
58 s[i] = s_j
59 s[j] = s_i
60 keystream[k] = s[(s_i + s_j) & 0xFF]
61 # XOR the whole keystream with the data at once instead of byte by byte.
62 xored = int.from_bytes(data, "big") ^ int.from_bytes(keystream, "big")
63 return xored.to_bytes(len(data), "big")
64
65 def decrypt(self, data: bytes, *, strict: bool = True) -> bytes:
66 return self.encrypt(data)
67
68
69class CryptAES(CryptBase):
70 def __init__(self, key: bytes) -> None:
71 pass
72
73 def encrypt(self, data: bytes) -> bytes:
74 raise DependencyError(_DEPENDENCY_ERROR_STR)
75
76 def decrypt(self, data: bytes, *, strict: bool = True) -> bytes:
77 raise DependencyError(_DEPENDENCY_ERROR_STR)
78
79
80def rc4_encrypt(key: bytes, data: bytes) -> bytes:
81 return CryptRC4(key).encrypt(data)
82
83
84def rc4_decrypt(key: bytes, data: bytes) -> bytes:
85 return CryptRC4(key).decrypt(data)
86
87
88def aes_ecb_encrypt(key: bytes, data: bytes) -> bytes:
89 raise DependencyError(_DEPENDENCY_ERROR_STR)
90
91
92def aes_ecb_decrypt(key: bytes, data: bytes) -> bytes:
93 raise DependencyError(_DEPENDENCY_ERROR_STR)
94
95
96def aes_cbc_encrypt(key: bytes, iv: bytes, data: bytes) -> bytes:
97 raise DependencyError(_DEPENDENCY_ERROR_STR)
98
99
100def aes_cbc_decrypt(key: bytes, iv: bytes, data: bytes) -> bytes:
101 raise DependencyError(_DEPENDENCY_ERROR_STR)