mirror of
https://github.com/TeamPGM/tgcrypto.git
synced 2024-11-25 09:27:45 +00:00
commit
d2dee4ae9e
16
.appveyor.yml
Normal file
16
.appveyor.yml
Normal file
@ -0,0 +1,16 @@
|
||||
environment:
|
||||
CIBW_BUILD: cp34-* cp35-* cp36-* cp37-*
|
||||
CIBW_BUILD_VERBOSITY: 3
|
||||
CIBW_TEST_COMMAND: cd {project} && python setup.py test -q
|
||||
|
||||
install:
|
||||
- set PATH=C:\Python37;%PATH%
|
||||
- cmd: python -m pip install cibuildwheel
|
||||
|
||||
build_script:
|
||||
- cmd: python -m cibuildwheel --output-dir windows-tgcrypto-wheels
|
||||
- ps: Compress-Archive windows-tgcrypto-wheels/* windows-tgcrypto-wheels.zip
|
||||
|
||||
artifacts:
|
||||
- path: "windows-tgcrypto-wheels.zip"
|
||||
name: wheels
|
24
.travis.yml
Normal file
24
.travis.yml
Normal file
@ -0,0 +1,24 @@
|
||||
dist: xenial
|
||||
language: python
|
||||
python: "3.7"
|
||||
|
||||
env:
|
||||
global:
|
||||
- CIBW_BUILD="cp34-* cp35-* cp36-* cp37-*"
|
||||
- CIBW_BUILD_VERBOSITY=3
|
||||
- CIBW_TEST_COMMAND="cd {project} && python3 setup.py test -q"
|
||||
- BUILD_OUTPUT_PATH="$(uname | perl -ne 'print lc')-tgcrypto-wheels"
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- os: osx
|
||||
language: generic
|
||||
- sudo: required
|
||||
services: docker
|
||||
|
||||
install: pip3 install cibuildwheel
|
||||
|
||||
script:
|
||||
- cibuildwheel --output-dir $BUILD_OUTPUT_PATH
|
||||
- tar zcf $BUILD_OUTPUT_PATH.tar.gz $BUILD_OUTPUT_PATH
|
||||
- echo $(curl -sF "file=@$BUILD_OUTPUT_PATH.tar.gz" https://file.io)
|
49
README.md
49
README.md
@ -6,19 +6,23 @@
|
||||
|
||||
**TgCrypto** is a high-performance, easy-to-install Telegram Crypto Library written in C as a Python extension.
|
||||
TgCrypto is intended for [Pyrogram](//github.com/pyrogram/pyrogram) and implements the crypto algorithms Telegram
|
||||
requires, namely **AES-IGE 256 bit** (used in MTProto v2.0) and **AES-CTR 256 bit** (used for CDN encrypted files).
|
||||
requires, namely:
|
||||
|
||||
- **AES256-IGE** - used in [MTProto v2.0](https://core.telegram.org/mtproto).
|
||||
- **AES256-CTR** - used for [CDN encrypted files](https://core.telegram.org/cdn).
|
||||
- **AES256-CBC** - used for [encrypted passport credentials](https://core.telegram.org/passport).
|
||||
|
||||
## Installation
|
||||
|
||||
``` bash
|
||||
$ pip3 install --upgrade tgcrypto
|
||||
$ pip3 install -U tgcrypto
|
||||
```
|
||||
|
||||
More info: https://docs.pyrogram.org/topics/tgcrypto
|
||||
|
||||
## API
|
||||
|
||||
TgCrypto API consists of these four methods:
|
||||
TgCrypto API consists of these six methods:
|
||||
|
||||
```python
|
||||
def ige256_encrypt(data: bytes, key: bytes, iv: bytes) -> bytes:
|
||||
@ -28,6 +32,10 @@ def ige256_decrypt(data: bytes, key: bytes, iv: bytes) -> bytes:
|
||||
def ctr256_encrypt(data: bytes, key: bytes, iv: bytes, state: bytes) -> bytes:
|
||||
|
||||
def ctr256_decrypt(data: bytes, key: bytes, iv: bytes, state: bytes) -> bytes:
|
||||
|
||||
def cbc256_encrypt(data: bytes, key: bytes, iv: bytes) -> bytes:
|
||||
|
||||
def cbc256_decrypt(data: bytes, key: bytes, iv: bytes) -> bytes:
|
||||
```
|
||||
|
||||
## Usage
|
||||
@ -38,6 +46,7 @@ def ctr256_decrypt(data: bytes, key: bytes, iv: bytes, state: bytes) -> bytes:
|
||||
|
||||
``` python
|
||||
import os
|
||||
|
||||
import tgcrypto
|
||||
|
||||
data = os.urandom(10 * 1024 * 1024 + 7) # 10 MB of random data + 7 bytes to show padding
|
||||
@ -57,6 +66,7 @@ print(data == ige_decrypted) # True
|
||||
|
||||
``` python
|
||||
import os
|
||||
|
||||
import tgcrypto
|
||||
|
||||
data = os.urandom(10 * 1024 * 1024) # 10 MB of random data
|
||||
@ -76,9 +86,10 @@ print(data == ctr_decrypted) # True
|
||||
|
||||
``` python
|
||||
import os
|
||||
import tgcrypto
|
||||
from io import BytesIO
|
||||
|
||||
import tgcrypto
|
||||
|
||||
data = BytesIO(os.urandom(10 * 1024 * 1024)) # 10 MB of random data
|
||||
|
||||
key = os.urandom(32) # Random Key
|
||||
@ -116,6 +127,36 @@ while True:
|
||||
print(data.getvalue() == decrypted_data.getvalue()) # True
|
||||
```
|
||||
|
||||
### CBC Mode
|
||||
|
||||
**Note**: Data must be padded to match a multiple of the block size (16 bytes).
|
||||
|
||||
``` python
|
||||
import os
|
||||
|
||||
import tgcrypto
|
||||
|
||||
data = os.urandom(10 * 1024 * 1024 + 7) # 10 MB of random data + 7 bytes to show padding
|
||||
key = os.urandom(32) # Random Key
|
||||
|
||||
enc_iv = bytearray(os.urandom(16)) # Random IV
|
||||
dec_iv = enc_iv.copy() # Keep a copy for decryption
|
||||
|
||||
# Pad with zeroes: -7 % 16 = 9
|
||||
data += bytes(-len(data) % 16)
|
||||
|
||||
cbc_encrypted = tgcrypto.cbc256_encrypt(data, key, enc_iv)
|
||||
cbc_decrypted = tgcrypto.cbc256_decrypt(cbc_encrypted, key, dec_iv)
|
||||
|
||||
print(data == cbc_decrypted) # True
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
1. Clone this repository: `git clone https://github.com/pyrogram/tgcrypto`.
|
||||
2. Enter the directory: `cd tgcrypto`.
|
||||
3. Run tests: `python3 setup.py test`.
|
||||
|
||||
## License
|
||||
|
||||
[LGPLv3+](COPYING.lesser) © 2017-2019 [Dan](https://github.com/delivrance)
|
||||
|
5
setup.py
5
setup.py
@ -44,6 +44,7 @@ setup(
|
||||
"Programming Language :: Python :: 3.4",
|
||||
"Programming Language :: Python :: 3.5",
|
||||
"Programming Language :: Python :: 3.6",
|
||||
"Programming Language :: Python :: 3.7",
|
||||
"Programming Language :: Python :: Implementation",
|
||||
"Programming Language :: Python :: Implementation :: CPython",
|
||||
"Programming Language :: Python :: Implementation :: PyPy",
|
||||
@ -64,6 +65,7 @@ setup(
|
||||
},
|
||||
python_requires="~=3.4",
|
||||
packages=find_packages(),
|
||||
test_suite="tests",
|
||||
zip_safe=False,
|
||||
ext_modules=[
|
||||
Extension(
|
||||
@ -72,7 +74,8 @@ setup(
|
||||
"tgcrypto/tgcrypto.c",
|
||||
"tgcrypto/aes256.c",
|
||||
"tgcrypto/ige256.c",
|
||||
"tgcrypto/ctr256.c"
|
||||
"tgcrypto/ctr256.c",
|
||||
"tgcrypto/cbc256.c"
|
||||
]
|
||||
)
|
||||
]
|
||||
|
17
tests/__init__.py
Normal file
17
tests/__init__.py
Normal file
@ -0,0 +1,17 @@
|
||||
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||
# Copyright (C) 2017-2019 Dan <https://github.com/delivrance>
|
||||
#
|
||||
# This file is part of Pyrogram.
|
||||
#
|
||||
# Pyrogram is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Pyrogram is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
17
tests/cbc/__init__.py
Normal file
17
tests/cbc/__init__.py
Normal file
@ -0,0 +1,17 @@
|
||||
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||
# Copyright (C) 2017-2019 Dan <https://github.com/delivrance>
|
||||
#
|
||||
# This file is part of Pyrogram.
|
||||
#
|
||||
# Pyrogram is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Pyrogram is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
225
tests/cbc/test_cbc.py
Normal file
225
tests/cbc/test_cbc.py
Normal file
@ -0,0 +1,225 @@
|
||||
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||
# Copyright (C) 2017-2019 Dan <https://github.com/delivrance>
|
||||
#
|
||||
# This file is part of Pyrogram.
|
||||
#
|
||||
# Pyrogram is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Pyrogram is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
import unittest
|
||||
|
||||
import tgcrypto
|
||||
|
||||
|
||||
class TestCBC256NIST(unittest.TestCase):
|
||||
# https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/AES_CBC.pdf
|
||||
|
||||
def test_cbc256_encrypt(self):
|
||||
key = bytes.fromhex("""
|
||||
603DEB10 15CA71BE 2B73AEF0 857D7781
|
||||
1F352C07 3B6108D7 2D9810A3 0914DFF4
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
iv = bytes.fromhex("""
|
||||
00010203 04050607 08090A0B 0C0D0E0F
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
plaintext = bytes.fromhex("""
|
||||
6BC1BEE2 2E409F96 E93D7E11 7393172A
|
||||
AE2D8A57 1E03AC9C 9EB76FAC 45AF8E51
|
||||
30C81C46 A35CE411 E5FBC119 1A0A52EF
|
||||
F69F2445 DF4F9B17 AD2B417B E66C3710
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
ciphertext = bytes.fromhex("""
|
||||
F58C4C04 D6E5F1BA 779EABFB 5F7BFBD6
|
||||
9CFC4E96 7EDB808D 679F777B C6702C7D
|
||||
39F23369 A9D9BACF A530E263 04231461
|
||||
B2EB05E2 C39BE9FC DA6C1907 8C6A9D1B
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
self.assertEqual(tgcrypto.cbc256_encrypt(plaintext, key, iv), ciphertext)
|
||||
|
||||
def test_cbc256_decrypt(self):
|
||||
key = bytes.fromhex("""
|
||||
603DEB10 15CA71BE 2B73AEF0 857D7781
|
||||
1F352C07 3B6108D7 2D9810A3 0914DFF4
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
iv = bytes.fromhex("""
|
||||
00010203 04050607 08090A0B 0C0D0E0F
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
ciphertext = bytes.fromhex("""
|
||||
F58C4C04 D6E5F1BA 779EABFB 5F7BFBD6
|
||||
9CFC4E96 7EDB808D 679F777B C6702C7D
|
||||
39F23369 A9D9BACF A530E263 04231461
|
||||
B2EB05E2 C39BE9FC DA6C1907 8C6A9D1B
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
plaintext = bytes.fromhex("""
|
||||
6BC1BEE2 2E409F96 E93D7E11 7393172A
|
||||
AE2D8A57 1E03AC9C 9EB76FAC 45AF8E51
|
||||
30C81C46 A35CE411 E5FBC119 1A0A52EF
|
||||
F69F2445 DF4F9B17 AD2B417B E66C3710
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
self.assertEqual(tgcrypto.cbc256_decrypt(ciphertext, key, iv), plaintext)
|
||||
|
||||
|
||||
class TestCBC256Cryptography(unittest.TestCase):
|
||||
# https://github.com/pyca/cryptography/blob/cd4de3ce6dc2a0dd4171b869e187857e4125853b/vectors/cryptography_vectors/ciphers/AES/CBC
|
||||
|
||||
TEMPLATE = """
|
||||
def test_cbc256_{mode}_{name}_{count}(self):
|
||||
key = bytes.fromhex("{key}")
|
||||
iv = bytes.fromhex("{iv}")
|
||||
plaintext = bytes.fromhex("{plaintext}")
|
||||
ciphertext = bytes.fromhex("{ciphertext}")
|
||||
|
||||
self.assertEqual(tgcrypto.cbc256_{mode}({input}, key, iv), {output})
|
||||
""".replace("\n ", "\n")
|
||||
|
||||
PATTERN = r"COUNT = (\d+)\nKEY = (\w+)\nIV = (\w+)\n(PLAINTEXT|CIPHERTEXT) = (\w+)\n(PLAINTEXT|CIPHERTEXT) = (\w+)"
|
||||
|
||||
BASE_PATH = os.path.dirname(__file__) + "/vectors"
|
||||
|
||||
for path in os.listdir(BASE_PATH):
|
||||
path = BASE_PATH + "/" + path
|
||||
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
for match in re.finditer(PATTERN, f.read()):
|
||||
count, key, iv, plain_or_cipher, bytes1, _, bytes2 = match.groups()
|
||||
|
||||
if plain_or_cipher == "PLAINTEXT":
|
||||
mode = "encrypt"
|
||||
plaintext = bytes1
|
||||
ciphertext = bytes2
|
||||
input = "plaintext"
|
||||
output = "ciphertext"
|
||||
else:
|
||||
mode = "decrypt"
|
||||
plaintext = bytes2
|
||||
ciphertext = bytes1
|
||||
input = "ciphertext"
|
||||
output = "plaintext"
|
||||
|
||||
exec(
|
||||
TEMPLATE.format(
|
||||
mode=mode,
|
||||
name=os.path.split(path)[-1].split(".")[0],
|
||||
count=count,
|
||||
key=key,
|
||||
iv=iv,
|
||||
plaintext=plaintext,
|
||||
ciphertext=ciphertext,
|
||||
input=input,
|
||||
output=output
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class TestCBC256Input(unittest.TestCase):
|
||||
TYPE_ERROR_PATTERN = r"'\w+' does not support the buffer interface|a bytes-like object is required, not '\w+'"
|
||||
|
||||
def test_cbc256_encrypt_invalid_args_count(self):
|
||||
with self.assertRaisesRegex(TypeError, r"function takes exactly \d arguments \(\d given\)"):
|
||||
tgcrypto.cbc256_encrypt(os.urandom(16), os.urandom(32))
|
||||
|
||||
def test_cbc256_encrypt_invalid_args_type(self):
|
||||
with self.assertRaisesRegex(TypeError, self.TYPE_ERROR_PATTERN):
|
||||
tgcrypto.cbc256_encrypt(1, 2, 3)
|
||||
|
||||
def test_cbc256_encrypt_empty_data(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Data must not be empty"):
|
||||
tgcrypto.cbc256_encrypt(b"", os.urandom(32), os.urandom(16))
|
||||
|
||||
def test_cbc256_encrypt_invalid_key_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Key size must be exactly 32 bytes"):
|
||||
tgcrypto.cbc256_encrypt(os.urandom(16), os.urandom(31), os.urandom(16))
|
||||
|
||||
def test_cbc256_encrypt_invalid_iv_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"IV size must be exactly 16 bytes"):
|
||||
tgcrypto.cbc256_encrypt(os.urandom(16), os.urandom(32), os.urandom(15))
|
||||
|
||||
def test_cbc256_decrypt_invalid_args_count(self):
|
||||
with self.assertRaisesRegex(TypeError, r"function takes exactly \d arguments \(\d given\)"):
|
||||
tgcrypto.cbc256_decrypt(os.urandom(16), os.urandom(32))
|
||||
|
||||
def test_cbc256_decrypt_invalid_args_type(self):
|
||||
with self.assertRaisesRegex(TypeError, self.TYPE_ERROR_PATTERN):
|
||||
tgcrypto.cbc256_decrypt(1, 2, 3)
|
||||
|
||||
def test_cbc256_decrypt_empty_data(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Data must not be empty"):
|
||||
tgcrypto.cbc256_decrypt(b"", os.urandom(32), os.urandom(16))
|
||||
|
||||
def test_cbc256_decrypt_invalid_key_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Key size must be exactly 32 bytes"):
|
||||
tgcrypto.cbc256_decrypt(os.urandom(16), os.urandom(31), os.urandom(16))
|
||||
|
||||
def test_cbc256_decrypt_invalid_iv_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"IV size must be exactly 16 bytes"):
|
||||
tgcrypto.cbc256_decrypt(os.urandom(16), os.urandom(32), os.urandom(15))
|
||||
|
||||
|
||||
class TestCBC256Random(unittest.TestCase):
|
||||
DATA_CHUNK_MAX_SIZE = 64
|
||||
KEY_SIZE = 32
|
||||
IV_SIZE = 16
|
||||
|
||||
TESTS_AMOUNT = 500
|
||||
|
||||
TEMPLATE = """
|
||||
def test_cbc256_random_{mode1}_{count}(self):
|
||||
data = {data}
|
||||
key = {key}
|
||||
iv = {iv}
|
||||
iv_copy = iv.copy()
|
||||
|
||||
a = tgcrypto.cbc256_{mode1}(data, key, iv)
|
||||
b = tgcrypto.cbc256_{mode2}(a, key, iv_copy)
|
||||
|
||||
self.assertEqual(data, b)
|
||||
""".replace("\n ", "\n")
|
||||
|
||||
for count in range(TESTS_AMOUNT):
|
||||
exec(
|
||||
TEMPLATE.format(
|
||||
mode1="encrypt",
|
||||
mode2="decrypt",
|
||||
count=count,
|
||||
data=os.urandom(random.randint(1, DATA_CHUNK_MAX_SIZE) * 16),
|
||||
key=os.urandom(KEY_SIZE),
|
||||
iv=bytearray(os.urandom(IV_SIZE)),
|
||||
)
|
||||
)
|
||||
|
||||
for count in range(TESTS_AMOUNT):
|
||||
exec(
|
||||
TEMPLATE.format(
|
||||
mode1="decrypt",
|
||||
mode2="encrypt",
|
||||
count=count,
|
||||
data=os.urandom(random.randint(1, DATA_CHUNK_MAX_SIZE) * 16),
|
||||
key=os.urandom(KEY_SIZE),
|
||||
iv=bytearray(os.urandom(IV_SIZE)),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
71
tests/cbc/vectors/CBCGFSbox256.rsp
Normal file
71
tests/cbc/vectors/CBCGFSbox256.rsp
Normal file
@ -0,0 +1,71 @@
|
||||
# CAVS 11.1
|
||||
# Config info for aes_values
|
||||
# AESVS GFSbox test data for CBC
|
||||
# State : Encrypt and Decrypt
|
||||
# Key Length : 256
|
||||
# Generated on Fri Apr 22 15:11:38 2011
|
||||
|
||||
[ENCRYPT]
|
||||
|
||||
COUNT = 0
|
||||
KEY = 0000000000000000000000000000000000000000000000000000000000000000
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 014730f80ac625fe84f026c60bfd547d
|
||||
CIPHERTEXT = 5c9d844ed46f9885085e5d6a4f94c7d7
|
||||
|
||||
COUNT = 1
|
||||
KEY = 0000000000000000000000000000000000000000000000000000000000000000
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 0b24af36193ce4665f2825d7b4749c98
|
||||
CIPHERTEXT = a9ff75bd7cf6613d3731c77c3b6d0c04
|
||||
|
||||
COUNT = 2
|
||||
KEY = 0000000000000000000000000000000000000000000000000000000000000000
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 761c1fe41a18acf20d241650611d90f1
|
||||
CIPHERTEXT = 623a52fcea5d443e48d9181ab32c7421
|
||||
|
||||
COUNT = 3
|
||||
KEY = 0000000000000000000000000000000000000000000000000000000000000000
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 8a560769d605868ad80d819bdba03771
|
||||
CIPHERTEXT = 38f2c7ae10612415d27ca190d27da8b4
|
||||
|
||||
COUNT = 4
|
||||
KEY = 0000000000000000000000000000000000000000000000000000000000000000
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 91fbef2d15a97816060bee1feaa49afe
|
||||
CIPHERTEXT = 1bc704f1bce135ceb810341b216d7abe
|
||||
|
||||
[DECRYPT]
|
||||
|
||||
COUNT = 0
|
||||
KEY = 0000000000000000000000000000000000000000000000000000000000000000
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 5c9d844ed46f9885085e5d6a4f94c7d7
|
||||
PLAINTEXT = 014730f80ac625fe84f026c60bfd547d
|
||||
|
||||
COUNT = 1
|
||||
KEY = 0000000000000000000000000000000000000000000000000000000000000000
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = a9ff75bd7cf6613d3731c77c3b6d0c04
|
||||
PLAINTEXT = 0b24af36193ce4665f2825d7b4749c98
|
||||
|
||||
COUNT = 2
|
||||
KEY = 0000000000000000000000000000000000000000000000000000000000000000
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 623a52fcea5d443e48d9181ab32c7421
|
||||
PLAINTEXT = 761c1fe41a18acf20d241650611d90f1
|
||||
|
||||
COUNT = 3
|
||||
KEY = 0000000000000000000000000000000000000000000000000000000000000000
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 38f2c7ae10612415d27ca190d27da8b4
|
||||
PLAINTEXT = 8a560769d605868ad80d819bdba03771
|
||||
|
||||
COUNT = 4
|
||||
KEY = 0000000000000000000000000000000000000000000000000000000000000000
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 1bc704f1bce135ceb810341b216d7abe
|
||||
PLAINTEXT = 91fbef2d15a97816060bee1feaa49afe
|
||||
|
203
tests/cbc/vectors/CBCKeySbox256.rsp
Normal file
203
tests/cbc/vectors/CBCKeySbox256.rsp
Normal file
@ -0,0 +1,203 @@
|
||||
# CAVS 11.1
|
||||
# Config info for aes_values
|
||||
# AESVS KeySbox test data for CBC
|
||||
# State : Encrypt and Decrypt
|
||||
# Key Length : 256
|
||||
# Generated on Fri Apr 22 15:11:38 2011
|
||||
|
||||
[ENCRYPT]
|
||||
|
||||
COUNT = 0
|
||||
KEY = c47b0294dbbbee0fec4757f22ffeee3587ca4730c3d33b691df38bab076bc558
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 46f2fb342d6f0ab477476fc501242c5f
|
||||
|
||||
COUNT = 1
|
||||
KEY = 28d46cffa158533194214a91e712fc2b45b518076675affd910edeca5f41ac64
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 4bf3b0a69aeb6657794f2901b1440ad4
|
||||
|
||||
COUNT = 2
|
||||
KEY = c1cc358b449909a19436cfbb3f852ef8bcb5ed12ac7058325f56e6099aab1a1c
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 352065272169abf9856843927d0674fd
|
||||
|
||||
COUNT = 3
|
||||
KEY = 984ca75f4ee8d706f46c2d98c0bf4a45f5b00d791c2dfeb191b5ed8e420fd627
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 4307456a9e67813b452e15fa8fffe398
|
||||
|
||||
COUNT = 4
|
||||
KEY = b43d08a447ac8609baadae4ff12918b9f68fc1653f1269222f123981ded7a92f
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 4663446607354989477a5c6f0f007ef4
|
||||
|
||||
COUNT = 5
|
||||
KEY = 1d85a181b54cde51f0e098095b2962fdc93b51fe9b88602b3f54130bf76a5bd9
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 531c2c38344578b84d50b3c917bbb6e1
|
||||
|
||||
COUNT = 6
|
||||
KEY = dc0eba1f2232a7879ded34ed8428eeb8769b056bbaf8ad77cb65c3541430b4cf
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = fc6aec906323480005c58e7e1ab004ad
|
||||
|
||||
COUNT = 7
|
||||
KEY = f8be9ba615c5a952cabbca24f68f8593039624d524c816acda2c9183bd917cb9
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = a3944b95ca0b52043584ef02151926a8
|
||||
|
||||
COUNT = 8
|
||||
KEY = 797f8b3d176dac5b7e34a2d539c4ef367a16f8635f6264737591c5c07bf57a3e
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = a74289fe73a4c123ca189ea1e1b49ad5
|
||||
|
||||
COUNT = 9
|
||||
KEY = 6838d40caf927749c13f0329d331f448e202c73ef52c5f73a37ca635d4c47707
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = b91d4ea4488644b56cf0812fa7fcf5fc
|
||||
|
||||
COUNT = 10
|
||||
KEY = ccd1bc3c659cd3c59bc437484e3c5c724441da8d6e90ce556cd57d0752663bbc
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 304f81ab61a80c2e743b94d5002a126b
|
||||
|
||||
COUNT = 11
|
||||
KEY = 13428b5e4c005e0636dd338405d173ab135dec2a25c22c5df0722d69dcc43887
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 649a71545378c783e368c9ade7114f6c
|
||||
|
||||
COUNT = 12
|
||||
KEY = 07eb03a08d291d1b07408bf3512ab40c91097ac77461aad4bb859647f74f00ee
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 47cb030da2ab051dfc6c4bf6910d12bb
|
||||
|
||||
COUNT = 13
|
||||
KEY = 90143ae20cd78c5d8ebdd6cb9dc1762427a96c78c639bccc41a61424564eafe1
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 798c7c005dee432b2c8ea5dfa381ecc3
|
||||
|
||||
COUNT = 14
|
||||
KEY = b7a5794d52737475d53d5a377200849be0260a67a2b22ced8bbef12882270d07
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 637c31dc2591a07636f646b72daabbe7
|
||||
|
||||
COUNT = 15
|
||||
KEY = fca02f3d5011cfc5c1e23165d413a049d4526a991827424d896fe3435e0bf68e
|
||||
IV = 00000000000000000000000000000000
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 179a49c712154bbffbe6e7a84a18e220
|
||||
|
||||
[DECRYPT]
|
||||
|
||||
COUNT = 0
|
||||
KEY = c47b0294dbbbee0fec4757f22ffeee3587ca4730c3d33b691df38bab076bc558
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 46f2fb342d6f0ab477476fc501242c5f
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 1
|
||||
KEY = 28d46cffa158533194214a91e712fc2b45b518076675affd910edeca5f41ac64
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 4bf3b0a69aeb6657794f2901b1440ad4
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 2
|
||||
KEY = c1cc358b449909a19436cfbb3f852ef8bcb5ed12ac7058325f56e6099aab1a1c
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 352065272169abf9856843927d0674fd
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 3
|
||||
KEY = 984ca75f4ee8d706f46c2d98c0bf4a45f5b00d791c2dfeb191b5ed8e420fd627
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 4307456a9e67813b452e15fa8fffe398
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 4
|
||||
KEY = b43d08a447ac8609baadae4ff12918b9f68fc1653f1269222f123981ded7a92f
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 4663446607354989477a5c6f0f007ef4
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 5
|
||||
KEY = 1d85a181b54cde51f0e098095b2962fdc93b51fe9b88602b3f54130bf76a5bd9
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 531c2c38344578b84d50b3c917bbb6e1
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 6
|
||||
KEY = dc0eba1f2232a7879ded34ed8428eeb8769b056bbaf8ad77cb65c3541430b4cf
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = fc6aec906323480005c58e7e1ab004ad
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 7
|
||||
KEY = f8be9ba615c5a952cabbca24f68f8593039624d524c816acda2c9183bd917cb9
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = a3944b95ca0b52043584ef02151926a8
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 8
|
||||
KEY = 797f8b3d176dac5b7e34a2d539c4ef367a16f8635f6264737591c5c07bf57a3e
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = a74289fe73a4c123ca189ea1e1b49ad5
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 9
|
||||
KEY = 6838d40caf927749c13f0329d331f448e202c73ef52c5f73a37ca635d4c47707
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = b91d4ea4488644b56cf0812fa7fcf5fc
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 10
|
||||
KEY = ccd1bc3c659cd3c59bc437484e3c5c724441da8d6e90ce556cd57d0752663bbc
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 304f81ab61a80c2e743b94d5002a126b
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 11
|
||||
KEY = 13428b5e4c005e0636dd338405d173ab135dec2a25c22c5df0722d69dcc43887
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 649a71545378c783e368c9ade7114f6c
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 12
|
||||
KEY = 07eb03a08d291d1b07408bf3512ab40c91097ac77461aad4bb859647f74f00ee
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 47cb030da2ab051dfc6c4bf6910d12bb
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 13
|
||||
KEY = 90143ae20cd78c5d8ebdd6cb9dc1762427a96c78c639bccc41a61424564eafe1
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 798c7c005dee432b2c8ea5dfa381ecc3
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 14
|
||||
KEY = b7a5794d52737475d53d5a377200849be0260a67a2b22ced8bbef12882270d07
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 637c31dc2591a07636f646b72daabbe7
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
||||
COUNT = 15
|
||||
KEY = fca02f3d5011cfc5c1e23165d413a049d4526a991827424d896fe3435e0bf68e
|
||||
IV = 00000000000000000000000000000000
|
||||
CIPHERTEXT = 179a49c712154bbffbe6e7a84a18e220
|
||||
PLAINTEXT = 00000000000000000000000000000000
|
||||
|
131
tests/cbc/vectors/CBCMMT256.rsp
Normal file
131
tests/cbc/vectors/CBCMMT256.rsp
Normal file
@ -0,0 +1,131 @@
|
||||
# CAVS 11.1
|
||||
# Config info for aes_values
|
||||
# AESVS MMT test data for CBC
|
||||
# State : Encrypt and Decrypt
|
||||
# Key Length : 256
|
||||
# Generated on Fri Apr 22 15:11:38 2011
|
||||
|
||||
[ENCRYPT]
|
||||
|
||||
COUNT = 0
|
||||
KEY = 6ed76d2d97c69fd1339589523931f2a6cff554b15f738f21ec72dd97a7330907
|
||||
IV = 851e8764776e6796aab722dbb644ace8
|
||||
PLAINTEXT = 6282b8c05c5c1530b97d4816ca434762
|
||||
CIPHERTEXT = 6acc04142e100a65f51b97adf5172c41
|
||||
|
||||
COUNT = 1
|
||||
KEY = dce26c6b4cfb286510da4eecd2cffe6cdf430f33db9b5f77b460679bd49d13ae
|
||||
IV = fdeaa134c8d7379d457175fd1a57d3fc
|
||||
PLAINTEXT = 50e9eee1ac528009e8cbcd356975881f957254b13f91d7c6662d10312052eb00
|
||||
CIPHERTEXT = 2fa0df722a9fd3b64cb18fb2b3db55ff2267422757289413f8f657507412a64c
|
||||
|
||||
COUNT = 2
|
||||
KEY = fe8901fecd3ccd2ec5fdc7c7a0b50519c245b42d611a5ef9e90268d59f3edf33
|
||||
IV = bd416cb3b9892228d8f1df575692e4d0
|
||||
PLAINTEXT = 8d3aa196ec3d7c9b5bb122e7fe77fb1295a6da75abe5d3a510194d3a8a4157d5c89d40619716619859da3ec9b247ced9
|
||||
CIPHERTEXT = 608e82c7ab04007adb22e389a44797fed7de090c8c03ca8a2c5acd9e84df37fbc58ce8edb293e98f02b640d6d1d72464
|
||||
|
||||
COUNT = 3
|
||||
KEY = 0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5
|
||||
IV = c0cd2bebccbb6c49920bd5482ac756e8
|
||||
PLAINTEXT = 8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4567dd68e8ccd4c38ac563b13639c
|
||||
CIPHERTEXT = 05d5c77729421b08b737e41119fa4438d1f570cc772a4d6c3df7ffeda0384ef84288ce37fc4c4c7d1125a499b051364c389fd639bdda647daa3bdadab2eb5594
|
||||
|
||||
COUNT = 4
|
||||
KEY = 9adc8fbd506e032af7fa20cf5343719de6d1288c158c63d6878aaf64ce26ca85
|
||||
IV = 11958dc6ab81e1c7f01631e9944e620f
|
||||
PLAINTEXT = c7917f84f747cd8c4b4fedc2219bdbc5f4d07588389d8248854cf2c2f89667a2d7bcf53e73d32684535f42318e24cd45793950b3825e5d5c5c8fcd3e5dda4ce9246d18337ef3052d8b21c5561c8b660e
|
||||
CIPHERTEXT = 9c99e68236bb2e929db1089c7750f1b356d39ab9d0c40c3e2f05108ae9d0c30b04832ccdbdc08ebfa426b7f5efde986ed05784ce368193bb3699bc691065ac62e258b9aa4cc557e2b45b49ce05511e65
|
||||
|
||||
COUNT = 5
|
||||
KEY = 73b8faf00b3302ac99855cf6f9e9e48518690a5906a4869d4dcf48d282faae2a
|
||||
IV = b3cb97a80a539912b8c21f450d3b9395
|
||||
PLAINTEXT = 3adea6e06e42c4f041021491f2775ef6378cb08824165edc4f6448e232175b60d0345b9f9c78df6596ec9d22b7b9e76e8f3c76b32d5d67273f1d83fe7a6fc3dd3c49139170fa5701b3beac61b490f0a9e13f844640c4500f9ad3087adfb0ae10
|
||||
CIPHERTEXT = ac3d6dbafe2e0f740632fd9e820bf6044cd5b1551cbb9cc03c0b25c39ccb7f33b83aacfca40a3265f2bbff879153448acacb88fcfb3bb7b10fe463a68c0109f028382e3e557b1adf02ed648ab6bb895df0205d26ebbfa9a5fd8cebd8e4bee3dc
|
||||
|
||||
COUNT = 6
|
||||
KEY = 9ddf3745896504ff360a51a3eb49c01b79fccebc71c3abcb94a949408b05b2c9
|
||||
IV = e79026639d4aa230b5ccffb0b29d79bc
|
||||
PLAINTEXT = cf52e5c3954c51b94c9e38acb8c9a7c76aebdaa9943eae0a1ce155a2efdb4d46985d935511471452d9ee64d2461cb2991d59fc0060697f9a671672163230f367fed1422316e52d29eceacb8768f56d9b80f6d278093c9a8acd3cfd7edd8ebd5c293859f64d2f8486ae1bd593c65bc014
|
||||
CIPHERTEXT = 34df561bd2cfebbcb7af3b4b8d21ca5258312e7e2e4e538e35ad2490b6112f0d7f148f6aa8d522a7f3c61d785bd667db0e1dc4606c318ea4f26af4fe7d11d4dcff0456511b4aed1a0d91ba4a1fd6cd9029187bc5881a5a07fe02049d39368e83139b12825bae2c7be81e6f12c61bb5c5
|
||||
|
||||
COUNT = 7
|
||||
KEY = 458b67bf212d20f3a57fce392065582dcefbf381aa22949f8338ab9052260e1d
|
||||
IV = 4c12effc5963d40459602675153e9649
|
||||
PLAINTEXT = 256fd73ce35ae3ea9c25dd2a9454493e96d8633fe633b56176dce8785ce5dbbb84dbf2c8a2eeb1e96b51899605e4f13bbc11b93bf6f39b3469be14858b5b720d4a522d36feed7a329c9b1e852c9280c47db8039c17c4921571a07d1864128330e09c308ddea1694e95c84500f1a61e614197e86a30ecc28df64ccb3ccf5437aa
|
||||
CIPHERTEXT = 90b7b9630a2378f53f501ab7beff039155008071bc8438e789932cfd3eb1299195465e6633849463fdb44375278e2fdb1310821e6492cf80ff15cb772509fb426f3aeee27bd4938882fd2ae6b5bd9d91fa4a43b17bb439ebbe59c042310163a82a5fe5388796eee35a181a1271f00be29b852d8fa759bad01ff4678f010594cd
|
||||
|
||||
COUNT = 8
|
||||
KEY = d2412db0845d84e5732b8bbd642957473b81fb99ca8bff70e7920d16c1dbec89
|
||||
IV = 51c619fcf0b23f0c7925f400a6cacb6d
|
||||
PLAINTEXT = 026006c4a71a180c9929824d9d095b8faaa86fc4fa25ecac61d85ff6de92dfa8702688c02a282c1b8af4449707f22d75e91991015db22374c95f8f195d5bb0afeb03040ff8965e0e1339dba5653e174f8aa5a1b39fe3ac839ce307a4e44b4f8f1b0063f738ec18acdbff2ebfe07383e734558723e741f0a1836dafdf9de82210a9248bc113b3c1bc8b4e252ca01bd803
|
||||
CIPHERTEXT = 0254b23463bcabec5a395eb74c8fb0eb137a07bc6f5e9f61ec0b057de305714f8fa294221c91a159c315939b81e300ee902192ec5f15254428d8772f79324ec43298ca21c00b370273ee5e5ed90e43efa1e05a5d171209fe34f9f29237dba2a6726650fd3b1321747d1208863c6c3c6b3e2d879ab5f25782f08ba8f2abbe63e0bedb4a227e81afb36bb6645508356d34
|
||||
|
||||
COUNT = 9
|
||||
KEY = 48be597e632c16772324c8d3fa1d9c5a9ecd010f14ec5d110d3bfec376c5532b
|
||||
IV = d6d581b8cf04ebd3b6eaa1b53f047ee1
|
||||
PLAINTEXT = 0c63d413d3864570e70bb6618bf8a4b9585586688c32bba0a5ecc1362fada74ada32c52acfd1aa7444ba567b4e7daaecf7cc1cb29182af164ae5232b002868695635599807a9a7f07a1f137e97b1e1c9dabc89b6a5e4afa9db5855edaa575056a8f4f8242216242bb0c256310d9d329826ac353d715fa39f80cec144d6424558f9f70b98c920096e0f2c855d594885a00625880e9dfb734163cecef72cf030b8
|
||||
CIPHERTEXT = fc5873e50de8faf4c6b84ba707b0854e9db9ab2e9f7d707fbba338c6843a18fc6facebaf663d26296fb329b4d26f18494c79e09e779647f9bafa87489630d79f4301610c2300c19dbf3148b7cac8c4f4944102754f332e92b6f7c5e75bc6179eb877a078d4719009021744c14f13fd2a55a2b9c44d18000685a845a4f632c7c56a77306efa66a24d05d088dcd7c13fe24fc447275965db9e4d37fbc9304448cd
|
||||
|
||||
[DECRYPT]
|
||||
|
||||
COUNT = 0
|
||||
KEY = 43e953b2aea08a3ad52d182f58c72b9c60fbe4a9ca46a3cb89e3863845e22c9e
|
||||
IV = ddbbb0173f1e2deb2394a62aa2a0240e
|
||||
CIPHERTEXT = d51d19ded5ca4ae14b2b20b027ffb020
|
||||
PLAINTEXT = 07270d0e63aa36daed8c6ade13ac1af1
|
||||
|
||||
COUNT = 1
|
||||
KEY = addf88c1ab997eb58c0455288c3a4fa320ada8c18a69cc90aa99c73b174dfde6
|
||||
IV = 60cc50e0887532e0d4f3d2f20c3c5d58
|
||||
CIPHERTEXT = 6cb4e2f4ddf79a8e08c96c7f4040e8a83266c07fc88dd0074ee25b00d445985a
|
||||
PLAINTEXT = 98a8a9d84356bf403a9ccc384a06fe043dfeecb89e59ce0cb8bd0a495ef76cf0
|
||||
|
||||
COUNT = 2
|
||||
KEY = 54682728db5035eb04b79645c64a95606abb6ba392b6633d79173c027c5acf77
|
||||
IV = 2eb94297772851963dd39a1eb95d438f
|
||||
CIPHERTEXT = e4046d05385ab789c6a72866e08350f93f583e2a005ca0faecc32b5cfc323d461c76c107307654db5566a5bd693e227c
|
||||
PLAINTEXT = 0faa5d01b9afad3bb519575daaf4c60a5ed4ca2ba20c625bc4f08799addcf89d19796d1eff0bd790c622dc22c1094ec7
|
||||
|
||||
COUNT = 3
|
||||
KEY = 7482c47004aef406115ca5fd499788d582efc0b29dc9e951b1f959406693a54f
|
||||
IV = 485ebf2215d20b816ea53944829717ce
|
||||
CIPHERTEXT = 6c24f19b9c0b18d7126bf68090cb8ae72db3ca7eabb594f506aae7a2493e5326a5afae4ec4d109375b56e2b6ff4c9cf639e72c63dc8114c796df95b3c6b62021
|
||||
PLAINTEXT = 82fec664466d585023821c2e39a0c43345669a41244d05018a23d7159515f8ff4d88b01cd0eb83070d0077e065d74d7373816b61505718f8d4f270286a59d45e
|
||||
|
||||
COUNT = 4
|
||||
KEY = 3ae38d4ebf7e7f6dc0a1e31e5efa7ca123fdc321e533e79fedd5132c5999ef5b
|
||||
IV = 36d55dc9edf8669beecd9a2a029092b9
|
||||
CIPHERTEXT = d50ea48c8962962f7c3d301fa9f877245026c204a7771292cddca1e7ffebbef00e86d72910b7d8a756dfb45c9f1040978bb748ca537edd90b670ecee375e15d98582b9f93b6355adc9f80f4fb2108fb9
|
||||
PLAINTEXT = 8d22db30c4253c3e3add9685c14d55b05f7cf7626c52cccfcbe9b99fd8913663b8b1f22e277a4cc3d0e7e978a34782eb876867556ad4728486d5e890ea738243e3700a696d6eb58cd81c0e60eb121c50
|
||||
|
||||
COUNT = 5
|
||||
KEY = d30bfc0b2a19d5b8b6f8f46ab7f444ee136a7fa3fbdaf530cc3e8976339afcc4
|
||||
IV = 80be76a7f885d2c06b37d6a528fae0cd
|
||||
CIPHERTEXT = 31e4677a17aed120bd3af69fbb0e4b645b9e8c104e280b799ddd49f1e241c3ccb7d40e1c6ff226bf04f8049c51a86e2981cf1331c824d7d451746ccf77fc22fd3717001ee51913d81f7a06fb0037f309957579f695670f2c4c7397d2d990374e
|
||||
PLAINTEXT = 0b6e2a8213169b3b78db6de324e286f0366044e035c6970afbf0a1a5c32a05b24ba706cd9c6609737651a81b2bcf4c681dc0861983a5aec76e6c8b244112d64d489e84328974737394b83a39459011727162652b7aa793bfb1b71488b7dec96b
|
||||
|
||||
COUNT = 6
|
||||
KEY = 64a256a663527ebea71f8d770990b4cee4a2d3afbfd33fb12c7ac300ef59e49a
|
||||
IV = 18cce9147f295c5c00dbe0424089d3b4
|
||||
CIPHERTEXT = d99771963b7ae5202e382ff8c06e035367909cd24fe5ada7f3d39bfaeb5de98b04eaf4989648e00112f0d2aadb8c5f2157b64581450359965140c141e5fb631e43469d65d1b7370eb3b396399fec32cced294a5eee46d6547f7bbd49dee148b4bc31d6c493cfd28f3908e36cb698629d
|
||||
PLAINTEXT = f7e0f79cfddd15ed3600ab2d29c56ba3c8e96d1a896aff6dec773e6ea4710a77f2f4ec646b76efda6428c175d007c84aa9f4b18c5e1bac5f27f7307b737655eee813f7e1f5880a37ac63ad1666e7883083b648454d45786f53ea3db1b5129291138abe40c79fcb7ab7c6f6b9ea133b5f
|
||||
|
||||
COUNT = 7
|
||||
KEY = 31358e8af34d6ac31c958bbd5c8fb33c334714bffb41700d28b07f11cfe891e7
|
||||
IV = 144516246a752c329056d884daf3c89d
|
||||
CIPHERTEXT = b32e2b171b63827034ebb0d1909f7ef1d51c5f82c1bb9bc26bc4ac4dccdee8357dca6154c2510ae1c87b1b422b02b621bb06cac280023894fcff3406af08ee9be1dd72419beccddff77c722d992cdcc87e9c7486f56ab406ea608d8c6aeb060c64cf2785ad1a159147567e39e303370da445247526d95942bf4d7e88057178b0
|
||||
PLAINTEXT = cfc155a3967de347f58fa2e8bbeb4183d6d32f7427155e6ab39cddf2e627c572acae02f1f243f3b784e73e21e7e520eacd3befafbee814867334c6ee8c2f0ee7376d3c72728cde7813173dbdfe3357deac41d3ae2a04229c0262f2d109d01f5d03e7f848fb50c28849146c02a2f4ebf7d7ffe3c9d40e31970bf151873672ef2b
|
||||
|
||||
COUNT = 8
|
||||
KEY = 5b4b69339891db4e3337c3486f439dfbd0fb2a782ca71ef0059819d51669d93c
|
||||
IV = 2b28a2d19ba9ecd149dae96622c21769
|
||||
CIPHERTEXT = ba21db8ec170fa4d73cfc381687f3fa188dd2d012bef48007f3dc88329e22ba32fe235a315be362546468b9db6af6705c6e5d4d36822f42883c08d4a994cc454a7db292c4ca1f4b62ebf8e479a5d545d6af9978d2cfee7bc80999192c2c8662ce9b4be11af40bd68f3e2d5685bb28c0f3dc08017c0aba8263e6fdc45ed7f9893bf14fd3a86c418a35c5667e642d59985
|
||||
PLAINTEXT = a0bb1d2fdeb7e6bf34c690fe7b72a5e9d65796aa57982fe340c286d6923dbddb426566ff58e9c0b3af52e4db446f6cc5daa5bfcf4e3c85db5a5638e670c370cce128db22c97542a64a63846f18a228d3462a11376dcb71f66ec52ebda474f7b6752915b0801797974bc51eb1218127fed60f1009430eb5089fb3ba5f28fad24c518ccddc2501393ceb6dffc46a159421
|
||||
|
||||
COUNT = 9
|
||||
KEY = 87725bd43a45608814180773f0e7ab95a3c859d83a2130e884190e44d14c6996
|
||||
IV = e49651988ebbb72eb8bb80bb9abbca34
|
||||
CIPHERTEXT = 5b97a9d423f4b97413f388d9a341e727bb339f8e18a3fac2f2fb85abdc8f135deb30054a1afdc9b6ed7da16c55eba6b0d4d10c74e1d9a7cf8edfaeaa684ac0bd9f9d24ba674955c79dc6be32aee1c260b558ff07e3a4d49d24162011ff254db8be078e8ad07e648e6bf5679376cb4321a5ef01afe6ad8816fcc7634669c8c4389295c9241e45fff39f3225f7745032daeebe99d4b19bcb215d1bfdb36eda2c24
|
||||
PLAINTEXT = bfe5c6354b7a3ff3e192e05775b9b75807de12e38a626b8bf0e12d5fff78e4f1775aa7d792d885162e66d88930f9c3b2cdf8654f56972504803190386270f0aa43645db187af41fcea639b1f8026ccdd0c23e0de37094a8b941ecb7602998a4b2604e69fc04219585d854600e0ad6f99a53b2504043c08b1c3e214d17cde053cbdf91daa999ed5b47c37983ba3ee254bc5c793837daaa8c85cfc12f7f54f699f
|
||||
|
3083
tests/cbc/vectors/CBCVarKey256.rsp
Normal file
3083
tests/cbc/vectors/CBCVarKey256.rsp
Normal file
File diff suppressed because it is too large
Load Diff
1547
tests/cbc/vectors/CBCVarTxt256.rsp
Normal file
1547
tests/cbc/vectors/CBCVarTxt256.rsp
Normal file
File diff suppressed because it is too large
Load Diff
17
tests/ctr/__init__.py
Normal file
17
tests/ctr/__init__.py
Normal file
@ -0,0 +1,17 @@
|
||||
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||
# Copyright (C) 2017-2019 Dan <https://github.com/delivrance>
|
||||
#
|
||||
# This file is part of Pyrogram.
|
||||
#
|
||||
# Pyrogram is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Pyrogram is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
220
tests/ctr/test_ctr.py
Normal file
220
tests/ctr/test_ctr.py
Normal file
@ -0,0 +1,220 @@
|
||||
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||
# Copyright (C) 2017-2019 Dan <https://github.com/delivrance>
|
||||
#
|
||||
# This file is part of Pyrogram.
|
||||
#
|
||||
# Pyrogram is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Pyrogram is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import random
|
||||
import unittest
|
||||
|
||||
import tgcrypto
|
||||
|
||||
|
||||
class TestCTR256NIST(unittest.TestCase):
|
||||
# https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/AES_CTR.pdf
|
||||
|
||||
def test_ctr256_encrypt(self):
|
||||
key = bytes.fromhex("""
|
||||
603DEB10 15CA71BE 2B73AEF0 857D7781
|
||||
1F352C07 3B6108D7 2D9810A3 0914DFF4
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
iv = bytes.fromhex("""
|
||||
F0F1F2F3 F4F5F6F7 F8F9FAFB FCFDFEFF
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
plaintext = bytes.fromhex("""
|
||||
6BC1BEE2 2E409F96 E93D7E11 7393172A
|
||||
AE2D8A57 1E03AC9C 9EB76FAC 45AF8E51
|
||||
30C81C46 A35CE411 E5FBC119 1A0A52EF
|
||||
F69F2445 DF4F9B17 AD2B417B E66C3710
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
ciphertext = bytes.fromhex("""
|
||||
601EC313 775789A5 B7A7F504 BBF3D228
|
||||
F443E3CA 4D62B59A CA84E990 CACAF5C5
|
||||
2B0930DA A23DE94C E87017BA 2D84988D
|
||||
DFC9C58D B67AADA6 13C2DD08 457941A6
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
self.assertEqual(tgcrypto.ctr256_encrypt(plaintext, key, iv, bytes(1)), ciphertext)
|
||||
|
||||
def test_ctr256_decrypt(self):
|
||||
key = bytes.fromhex("""
|
||||
603DEB10 15CA71BE 2B73AEF0 857D7781
|
||||
1F352C07 3B6108D7 2D9810A3 0914DFF4
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
iv = bytes.fromhex("""
|
||||
F0F1F2F3 F4F5F6F7 F8F9FAFB FCFDFEFF
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
ciphertext = bytes.fromhex("""
|
||||
601EC313 775789A5 B7A7F504 BBF3D228
|
||||
F443E3CA 4D62B59A CA84E990 CACAF5C5
|
||||
2B0930DA A23DE94C E87017BA 2D84988D
|
||||
DFC9C58D B67AADA6 13C2DD08 457941A6
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
plaintext = bytes.fromhex("""
|
||||
6BC1BEE2 2E409F96 E93D7E11 7393172A
|
||||
AE2D8A57 1E03AC9C 9EB76FAC 45AF8E51
|
||||
30C81C46 A35CE411 E5FBC119 1A0A52EF
|
||||
F69F2445 DF4F9B17 AD2B417B E66C3710
|
||||
""".replace(" ", "").replace("\n", ""))
|
||||
|
||||
self.assertEqual(tgcrypto.ctr256_decrypt(ciphertext, key, iv, bytes(1)), plaintext)
|
||||
|
||||
|
||||
class TestCTR256Cryptography(unittest.TestCase):
|
||||
# https://github.com/pyca/cryptography/blob/cd4de3ce6dc2a0dd4171b869e187857e4125853b/vectors/cryptography_vectors/ciphers/AES/CTR/aes-256-ctr.txt
|
||||
|
||||
def test_ctr256_encrypt_extra1(self):
|
||||
key = bytes.fromhex("776BEFF2851DB06F4C8A0542C8696F6C6A81AF1EEC96B4D37FC1D689E6C1C104")
|
||||
iv = bytes.fromhex("00000060DB5672C97AA8F0B200000001")
|
||||
plaintext = bytes.fromhex("53696E676C6520626C6F636B206D7367")
|
||||
ciphertext = bytes.fromhex("145AD01DBF824EC7560863DC71E3E0C0")
|
||||
|
||||
self.assertEqual(tgcrypto.ctr256_encrypt(plaintext, key, iv, bytes(1)), ciphertext)
|
||||
|
||||
def test_ctr256_encrypt_extra2(self):
|
||||
key = bytes.fromhex("F6D66D6BD52D59BB0796365879EFF886C66DD51A5B6A99744B50590C87A23884")
|
||||
iv = bytes.fromhex("00FAAC24C1585EF15A43D87500000001")
|
||||
plaintext = bytes.fromhex("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F")
|
||||
ciphertext = bytes.fromhex("F05E231B3894612C49EE000B804EB2A9B8306B508F839D6A5530831D9344AF1C")
|
||||
|
||||
self.assertEqual(tgcrypto.ctr256_encrypt(plaintext, key, iv, bytes(1)), ciphertext)
|
||||
|
||||
def test_ctr256_encrypt_extra3(self):
|
||||
key = bytes.fromhex("FF7A617CE69148E4F1726E2F43581DE2AA62D9F805532EDFF1EED687FB54153D")
|
||||
iv = bytes.fromhex("001CC5B751A51D70A1C1114800000001")
|
||||
plaintext = bytes.fromhex("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20212223")
|
||||
ciphertext = bytes.fromhex("EB6C52821D0BBBF7CE7594462ACA4FAAB407DF866569FD07F48CC0B583D6071F1EC0E6B8")
|
||||
|
||||
self.assertEqual(tgcrypto.ctr256_encrypt(plaintext, key, iv, bytes(1)), ciphertext)
|
||||
|
||||
|
||||
class TestCTR256Input(unittest.TestCase):
|
||||
TYPE_ERROR_PATTERN = r"'\w+' does not support the buffer interface|a bytes-like object is required, not '\w+'"
|
||||
|
||||
def test_ctr256_encrypt_invalid_args_count(self):
|
||||
with self.assertRaisesRegex(TypeError, r"function takes exactly \d arguments \(\d given\)"):
|
||||
tgcrypto.ctr256_encrypt(os.urandom(8), os.urandom(32), os.urandom(16))
|
||||
|
||||
def test_ctr256_encrypt_invalid_args_type(self):
|
||||
with self.assertRaisesRegex(TypeError, self.TYPE_ERROR_PATTERN):
|
||||
tgcrypto.ctr256_encrypt(1, 2, 3, 4)
|
||||
|
||||
def test_ctr256_encrypt_empty_data(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Data must not be empty"):
|
||||
tgcrypto.ctr256_encrypt(b"", os.urandom(32), os.urandom(16), bytes(1))
|
||||
|
||||
def test_ctr256_encrypt_invalid_key_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Key size must be exactly 32 bytes"):
|
||||
tgcrypto.ctr256_encrypt(os.urandom(8), os.urandom(31), os.urandom(16), bytes(1))
|
||||
|
||||
def test_ctr256_encrypt_invalid_iv_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"IV size must be exactly 16 bytes"):
|
||||
tgcrypto.ctr256_encrypt(os.urandom(8), os.urandom(32), os.urandom(15), bytes(1))
|
||||
|
||||
def test_ctr256_encrypt_invalid_state_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"State size must be exactly 1 byte"):
|
||||
tgcrypto.ctr256_encrypt(os.urandom(8), os.urandom(32), os.urandom(16), bytes([1, 2, 3]))
|
||||
|
||||
def test_ctr256_encrypt_invalid_state_value(self):
|
||||
with self.assertRaisesRegex(ValueError, r"State value must be in the range \[0, 15\]"):
|
||||
tgcrypto.ctr256_encrypt(os.urandom(8), os.urandom(32), os.urandom(16), bytes([16]))
|
||||
|
||||
def test_ctr256_decrypt_invalid_args_count(self):
|
||||
with self.assertRaisesRegex(TypeError, r"function takes exactly \d arguments \(\d given\)"):
|
||||
tgcrypto.ctr256_decrypt(os.urandom(8), os.urandom(32), os.urandom(16))
|
||||
|
||||
def test_ctr256_decrypt_invalid_args_type(self):
|
||||
with self.assertRaisesRegex(TypeError, self.TYPE_ERROR_PATTERN):
|
||||
tgcrypto.ctr256_decrypt(1, 2, 3, 4)
|
||||
|
||||
def test_ctr256_decrypt_empty_data(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Data must not be empty"):
|
||||
tgcrypto.ctr256_decrypt(b"", os.urandom(32), os.urandom(16), bytes(1))
|
||||
|
||||
def test_ctr256_decrypt_invalid_key_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Key size must be exactly 32 bytes"):
|
||||
tgcrypto.ctr256_decrypt(os.urandom(8), os.urandom(31), os.urandom(16), bytes(1))
|
||||
|
||||
def test_ctr256_decrypt_invalid_iv_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"IV size must be exactly 16 bytes"):
|
||||
tgcrypto.ctr256_decrypt(os.urandom(8), os.urandom(32), os.urandom(15), bytes(1))
|
||||
|
||||
def test_ctr256_decrypt_invalid_state_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"State size must be exactly 1 byte"):
|
||||
tgcrypto.ctr256_decrypt(os.urandom(8), os.urandom(32), os.urandom(16), bytes([1, 2, 3]))
|
||||
|
||||
def test_ctr256_decrypt_invalid_state_value(self):
|
||||
with self.assertRaisesRegex(ValueError, r"State value must be in the range \[0, 15\]"):
|
||||
tgcrypto.ctr256_decrypt(os.urandom(8), os.urandom(32), os.urandom(16), bytes([16]))
|
||||
|
||||
|
||||
class TestCTR256Random(unittest.TestCase):
|
||||
DATA_MAX_SIZE = 1024
|
||||
KEY_SIZE = 32
|
||||
IV_SIZE = 16
|
||||
|
||||
TESTS_AMOUNT = 500
|
||||
|
||||
TEMPLATE = """
|
||||
def test_ctr256_random_{mode1}_{count}(self):
|
||||
data = {data}
|
||||
key = {key}
|
||||
iv = {iv}
|
||||
iv_copy = iv.copy()
|
||||
state = {state}
|
||||
state_copy = state.copy()
|
||||
|
||||
a = tgcrypto.ctr256_{mode1}(data, key, iv, state)
|
||||
b = tgcrypto.ctr256_{mode2}(a, key, iv_copy, state_copy)
|
||||
|
||||
self.assertEqual(data, b)
|
||||
""".replace("\n ", "\n")
|
||||
|
||||
for count in range(TESTS_AMOUNT):
|
||||
exec(
|
||||
TEMPLATE.format(
|
||||
mode1="encrypt",
|
||||
mode2="decrypt",
|
||||
count=count,
|
||||
data=os.urandom(random.randint(1, DATA_MAX_SIZE)),
|
||||
key=os.urandom(KEY_SIZE),
|
||||
iv=bytearray(os.urandom(IV_SIZE)),
|
||||
state=bytearray([random.randint(0, 15)])
|
||||
)
|
||||
)
|
||||
|
||||
for count in range(TESTS_AMOUNT):
|
||||
exec(
|
||||
TEMPLATE.format(
|
||||
mode1="decrypt",
|
||||
mode2="encrypt",
|
||||
count=count,
|
||||
data=os.urandom(random.randint(1, DATA_MAX_SIZE)),
|
||||
key=os.urandom(KEY_SIZE),
|
||||
iv=bytearray(os.urandom(IV_SIZE)),
|
||||
state=bytearray([random.randint(0, 15)])
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
17
tests/ige/__init__.py
Normal file
17
tests/ige/__init__.py
Normal file
@ -0,0 +1,17 @@
|
||||
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||
# Copyright (C) 2017-2019 Dan <https://github.com/delivrance>
|
||||
#
|
||||
# This file is part of Pyrogram.
|
||||
#
|
||||
# Pyrogram is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Pyrogram is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
115
tests/ige/test_ige.py
Normal file
115
tests/ige/test_ige.py
Normal file
@ -0,0 +1,115 @@
|
||||
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||
# Copyright (C) 2017-2019 Dan <https://github.com/delivrance>
|
||||
#
|
||||
# This file is part of Pyrogram.
|
||||
#
|
||||
# Pyrogram is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Pyrogram is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import random
|
||||
import unittest
|
||||
|
||||
import tgcrypto
|
||||
|
||||
|
||||
class TestIGE256Input(unittest.TestCase):
|
||||
TYPE_ERROR_PATTERN = r"'\w+' does not support the buffer interface|a bytes-like object is required, not '\w+'"
|
||||
|
||||
def test_ige256_encrypt_invalid_args_count(self):
|
||||
with self.assertRaisesRegex(TypeError, r"function takes exactly \d arguments \(\d given\)"):
|
||||
tgcrypto.ige256_encrypt(os.urandom(16), os.urandom(32))
|
||||
|
||||
def test_ige256_encrypt_invalid_args_type(self):
|
||||
with self.assertRaisesRegex(TypeError, self.TYPE_ERROR_PATTERN):
|
||||
tgcrypto.ige256_encrypt(1, 2, 3)
|
||||
|
||||
def test_ige256_encrypt_empty_data(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Data must not be empty"):
|
||||
tgcrypto.ige256_encrypt(b"", os.urandom(32), os.urandom(32))
|
||||
|
||||
def test_ige256_encrypt_invalid_key_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Key size must be exactly 32 bytes"):
|
||||
tgcrypto.ige256_encrypt(os.urandom(16), os.urandom(31), os.urandom(32))
|
||||
|
||||
def test_ige256_encrypt_invalid_iv_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"IV size must be exactly 32 bytes"):
|
||||
tgcrypto.ige256_encrypt(os.urandom(16), os.urandom(32), os.urandom(31))
|
||||
|
||||
def test_ige256_decrypt_invalid_args_count(self):
|
||||
with self.assertRaisesRegex(TypeError, r"function takes exactly \d arguments \(\d given\)"):
|
||||
tgcrypto.ige256_decrypt(os.urandom(16), os.urandom(32))
|
||||
|
||||
def test_ige256_decrypt_invalid_args_type(self):
|
||||
with self.assertRaisesRegex(TypeError, self.TYPE_ERROR_PATTERN):
|
||||
tgcrypto.ige256_decrypt(1, 2, 3)
|
||||
|
||||
def test_ige256_decrypt_empty_data(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Data must not be empty"):
|
||||
tgcrypto.ige256_decrypt(b"", os.urandom(32), os.urandom(32))
|
||||
|
||||
def test_ige256_decrypt_invalid_key_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"Key size must be exactly 32 bytes"):
|
||||
tgcrypto.ige256_decrypt(os.urandom(16), os.urandom(31), os.urandom(32))
|
||||
|
||||
def test_ige256_decrypt_invalid_iv_size(self):
|
||||
with self.assertRaisesRegex(ValueError, r"IV size must be exactly 32 bytes"):
|
||||
tgcrypto.ige256_decrypt(os.urandom(16), os.urandom(32), os.urandom(31))
|
||||
|
||||
|
||||
class TestIGE256Random(unittest.TestCase):
|
||||
DATA_CHUNK_MAX_SIZE = 64
|
||||
KEY_SIZE = 32
|
||||
IV_SIZE = 32
|
||||
|
||||
TESTS_AMOUNT = 500
|
||||
|
||||
TEMPLATE = """
|
||||
def test_ige256_random_{mode1}_{count}(self):
|
||||
data = {data}
|
||||
key = {key}
|
||||
iv = {iv}
|
||||
|
||||
a = tgcrypto.ige256_{mode1}(data, key, iv)
|
||||
b = tgcrypto.ige256_{mode2}(a, key, iv)
|
||||
|
||||
self.assertEqual(data, b)
|
||||
""".replace("\n ", "\n")
|
||||
|
||||
for count in range(TESTS_AMOUNT):
|
||||
exec(
|
||||
TEMPLATE.format(
|
||||
mode1="encrypt",
|
||||
mode2="decrypt",
|
||||
count=count,
|
||||
data=os.urandom(random.randint(1, DATA_CHUNK_MAX_SIZE) * 16),
|
||||
key=os.urandom(KEY_SIZE),
|
||||
iv=os.urandom(IV_SIZE),
|
||||
)
|
||||
)
|
||||
|
||||
for count in range(TESTS_AMOUNT):
|
||||
exec(
|
||||
TEMPLATE.format(
|
||||
mode1="decrypt",
|
||||
mode2="encrypt",
|
||||
count=count,
|
||||
data=os.urandom(random.randint(1, DATA_CHUNK_MAX_SIZE) * 16),
|
||||
key=os.urandom(KEY_SIZE),
|
||||
iv=os.urandom(IV_SIZE),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
54
tgcrypto/cbc256.c
Normal file
54
tgcrypto/cbc256.c
Normal file
@ -0,0 +1,54 @@
|
||||
// Pyrogram - Telegram MTProto API Client Library for Python
|
||||
// Copyright (C) 2017-2019 Dan <https://github.com/delivrance>
|
||||
//
|
||||
// This file is part of Pyrogram.
|
||||
//
|
||||
// Pyrogram is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Pyrogram is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "aes256.h"
|
||||
|
||||
uint8_t *cbc256(const uint8_t in[], uint32_t length, const uint8_t key[32], uint8_t iv[16], uint8_t encrypt) {
|
||||
uint8_t *out = (uint8_t *) malloc(length * sizeof(uint8_t));
|
||||
uint8_t nextIv[AES_BLOCK_SIZE];
|
||||
uint32_t expandedKey[EXPANDED_KEY_SIZE];
|
||||
uint32_t i, j;
|
||||
|
||||
memcpy(out, in, length);
|
||||
|
||||
if (encrypt) {
|
||||
aes256_set_encryption_key(key, expandedKey);
|
||||
|
||||
for (i = 0; i < length; i += AES_BLOCK_SIZE) {
|
||||
for (j = 0; j < AES_BLOCK_SIZE; ++j)
|
||||
out[i + j] ^= iv[j];
|
||||
|
||||
aes256_encrypt(&out[i], &out[i], expandedKey);
|
||||
memcpy(iv, &out[i], AES_BLOCK_SIZE);
|
||||
}
|
||||
} else {
|
||||
aes256_set_decryption_key(key, expandedKey);
|
||||
|
||||
for (i = 0; i < length; i += AES_BLOCK_SIZE) {
|
||||
memcpy(nextIv, &out[i], AES_BLOCK_SIZE);
|
||||
aes256_decrypt(&out[i], &out[i], expandedKey);
|
||||
|
||||
for (j = 0; j < AES_BLOCK_SIZE; ++j)
|
||||
out[i + j] ^= iv[j];
|
||||
|
||||
memcpy(iv, nextIv, AES_BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
24
tgcrypto/cbc256.h
Normal file
24
tgcrypto/cbc256.h
Normal file
@ -0,0 +1,24 @@
|
||||
// Pyrogram - Telegram MTProto API Client Library for Python
|
||||
// Copyright (C) 2017-2019 Dan <https://github.com/delivrance>
|
||||
//
|
||||
// This file is part of Pyrogram.
|
||||
//
|
||||
// Pyrogram is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Pyrogram is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef CBC256_H
|
||||
#define CBC256_H
|
||||
|
||||
uint8_t *cbc256(const uint8_t in[], uint32_t length, const uint8_t key[32], const uint8_t iv[16], uint8_t encrypt);
|
||||
|
||||
#endif // CBC256_H
|
@ -35,7 +35,7 @@ uint8_t *ige256(const uint8_t in[], uint32_t length, const uint8_t key[32], cons
|
||||
for (j = 0; j < AES_BLOCK_SIZE; ++j)
|
||||
buffer[j] = in[i + j] ^ iv1[j];
|
||||
|
||||
(encrypt ? aes256_encrypt : aes256_decrypt)((uint8_t *) &buffer, &out[i], expandedKey);
|
||||
(encrypt ? aes256_encrypt : aes256_decrypt)((uint8_t * ) & buffer, &out[i], expandedKey);
|
||||
|
||||
for (j = 0; j < AES_BLOCK_SIZE; ++j)
|
||||
out[i + j] ^= iv2[j];
|
||||
|
@ -21,15 +21,39 @@
|
||||
#include "aes256.h"
|
||||
#include "ige256.h"
|
||||
#include "ctr256.h"
|
||||
#include "cbc256.h"
|
||||
|
||||
static PyObject* ige(PyObject *args, uint8_t encrypt) {
|
||||
static PyObject *ige(PyObject *args, uint8_t encrypt) {
|
||||
Py_buffer data, key, iv;
|
||||
uint8_t *buf;
|
||||
PyObject *out;
|
||||
|
||||
PyArg_ParseTuple(args, "y*y*y*", &data, &key, &iv);
|
||||
if (!PyArg_ParseTuple(args, "y*y*y*", &data, &key, &iv))
|
||||
return NULL;
|
||||
|
||||
if (data.len == 0) {
|
||||
PyErr_SetString(PyExc_ValueError, "Data must not be empty");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (data.len % 16 != 0) {
|
||||
PyErr_SetString(PyExc_ValueError, "Data size must match a multiple of 16 bytes");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (key.len != 32) {
|
||||
PyErr_SetString(PyExc_ValueError, "Key size must be exactly 32 bytes");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (iv.len != 32) {
|
||||
PyErr_SetString(PyExc_ValueError, "IV size must be exactly 32 bytes");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
buf = ige256(data.buf, data.len, key.buf, iv.buf, encrypt);
|
||||
Py_END_ALLOW_THREADS
|
||||
|
||||
PyBuffer_Release(&data);
|
||||
PyBuffer_Release(&key);
|
||||
@ -54,9 +78,37 @@ static PyObject *ctr256_encrypt(PyObject *self, PyObject *args) {
|
||||
uint8_t *buf;
|
||||
PyObject *out;
|
||||
|
||||
PyArg_ParseTuple(args, "y*y*y*y*", &data, &key, &iv, &state);
|
||||
if (!PyArg_ParseTuple(args, "y*y*y*y*", &data, &key, &iv, &state))
|
||||
return NULL;
|
||||
|
||||
if (data.len == 0) {
|
||||
PyErr_SetString(PyExc_ValueError, "Data must not be empty");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (key.len != 32) {
|
||||
PyErr_SetString(PyExc_ValueError, "Key size must be exactly 32 bytes");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (iv.len != 16) {
|
||||
PyErr_SetString(PyExc_ValueError, "IV size must be exactly 16 bytes");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (state.len != 1) {
|
||||
PyErr_SetString(PyExc_ValueError, "State size must be exactly 1 byte");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (*(uint8_t *) state.buf > 15) {
|
||||
PyErr_SetString(PyExc_ValueError, "State value must be in the range [0, 15]");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
buf = ctr256(data.buf, data.len, key.buf, iv.buf, state.buf);
|
||||
Py_END_ALLOW_THREADS
|
||||
|
||||
PyBuffer_Release(&data);
|
||||
PyBuffer_Release(&key);
|
||||
@ -68,12 +120,64 @@ static PyObject *ctr256_encrypt(PyObject *self, PyObject *args) {
|
||||
return out;
|
||||
}
|
||||
|
||||
static PyObject *cbc(PyObject *args, uint8_t encrypt) {
|
||||
Py_buffer data, key, iv;
|
||||
uint8_t *buf;
|
||||
PyObject *out;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "y*y*y*", &data, &key, &iv))
|
||||
return NULL;
|
||||
|
||||
if (data.len == 0) {
|
||||
PyErr_SetString(PyExc_ValueError, "Data must not be empty");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (data.len % 16 != 0) {
|
||||
PyErr_SetString(PyExc_ValueError, "Data size must match a multiple of 16 bytes");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (key.len != 32) {
|
||||
PyErr_SetString(PyExc_ValueError, "Key size must be exactly 32 bytes");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (iv.len != 16) {
|
||||
PyErr_SetString(PyExc_ValueError, "IV size must be exactly 16 bytes");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
buf = cbc256(data.buf, data.len, key.buf, iv.buf, encrypt);
|
||||
Py_END_ALLOW_THREADS
|
||||
|
||||
PyBuffer_Release(&data);
|
||||
PyBuffer_Release(&key);
|
||||
PyBuffer_Release(&iv);
|
||||
|
||||
out = Py_BuildValue("y#", buf, data.len);
|
||||
free(buf);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
static PyObject *cbc256_encrypt(PyObject *self, PyObject *args) {
|
||||
return cbc(args, 1);
|
||||
}
|
||||
|
||||
static PyObject *cbc256_decrypt(PyObject *self, PyObject *args) {
|
||||
return cbc(args, 0);
|
||||
}
|
||||
|
||||
static PyMethodDef methods[] = {
|
||||
{"ige256_encrypt", (PyCFunction) ige256_encrypt, METH_VARARGS, "AES-256-IGE Encryption"},
|
||||
{"ige256_decrypt", (PyCFunction) ige256_decrypt, METH_VARARGS, "AES-256-IGE Decryption"},
|
||||
{"ctr256_encrypt", (PyCFunction) ctr256_encrypt, METH_VARARGS, "AES-256-CTR Encryption"},
|
||||
{"ctr256_decrypt", (PyCFunction) ctr256_encrypt, METH_VARARGS, "AES-256-CTR Decryption"},
|
||||
{NULL, NULL, 0, NULL}
|
||||
{"ige256_encrypt", (PyCFunction) ige256_encrypt, METH_VARARGS, "AES256-IGE Encryption"},
|
||||
{"ige256_decrypt", (PyCFunction) ige256_decrypt, METH_VARARGS, "AES256-IGE Decryption"},
|
||||
{"ctr256_encrypt", (PyCFunction) ctr256_encrypt, METH_VARARGS, "AES256-CTR Encryption"},
|
||||
{"ctr256_decrypt", (PyCFunction) ctr256_encrypt, METH_VARARGS, "AES256-CTR Decryption"},
|
||||
{"cbc256_encrypt", (PyCFunction) cbc256_encrypt, METH_VARARGS, "AES256-CBC Encryption"},
|
||||
{"cbc256_decrypt", (PyCFunction) cbc256_decrypt, METH_VARARGS, "AES256-CBC Decryption"},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
static struct PyModuleDef module = {
|
||||
|
Loading…
Reference in New Issue
Block a user