Skip to content

Commit c02c0b8

Browse files
committed
Remove dead code (encryption functions in crypto.py, all of binary.py)
1 parent 42b160b commit c02c0b8

File tree

10 files changed

+8
-309
lines changed

10 files changed

+8
-309
lines changed

cms/server/admin/authentication.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from werkzeug.wrappers import Request, Response
2626

2727
from cms import config
28-
from cmscommon.binary import hex_to_bin
2928
from cmscommon.datetime import make_timestamp
3029

3130

@@ -130,7 +129,7 @@ def wsgi_app(self, environ: dict, start_response: Callable):
130129
self._local.request = Request(environ)
131130
self._local.cookie = JSONSecureCookie.load_cookie(
132131
self._request, AWSAuthMiddleware.COOKIE,
133-
hex_to_bin(config.web_server.secret_key))
132+
bytes.fromhex(config.web_server.secret_key))
134133
self._verify_cookie()
135134

136135
def my_start_response(status, headers, exc_info=None):

cms/server/admin/server.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from cms.db import SessionGen, Dataset, Submission, SubmissionResult, Task
3535
from cms.io import WebService, rpc_method
3636
from cms.service import EvaluationService
37-
from cmscommon.binary import hex_to_bin
3837
from .authentication import AWSAuthMiddleware
3938
from .handlers import HANDLERS
4039
from .jinja2_toolbox import AWS_ENVIRONMENT
@@ -52,7 +51,7 @@ def __init__(self, shard: int):
5251
parameters = {
5352
"static_files": [("cms.server", "static"),
5453
("cms.server.admin", "static")],
55-
"cookie_secret": hex_to_bin(config.web_server.secret_key),
54+
"cookie_secret": bytes.fromhex(config.web_server.secret_key),
5655
"debug": config.web_server.tornado_debug,
5756
"num_proxies_used": config.admin_web_server.num_proxies_used,
5857
"auth_middleware": AWSAuthMiddleware,

cms/server/contest/server.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
from cms.io import WebService
4747
from cms.locale import get_translations
4848
from cms.server.contest.jinja2_toolbox import CWS_ENVIRONMENT
49-
from cmscommon.binary import hex_to_bin
5049
from .handlers import HANDLERS
5150
from .handlers.base import ContestListHandler
5251
from .handlers.main import MainHandler
@@ -66,7 +65,7 @@ def __init__(self, shard: int, contest_id: int | None = None):
6665
parameters = {
6766
"static_files": [("cms.server", "static"),
6867
("cms.server.contest", "static")],
69-
"cookie_secret": hex_to_bin(config.web_server.secret_key),
68+
"cookie_secret": bytes.fromhex(config.web_server.secret_key),
7069
"debug": config.web_server.tornado_debug,
7170
"is_proxy_used": None,
7271
"num_proxies_used": config.contest_web_server.num_proxies_used,

cmscommon/binary.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

cmscommon/crypto.py

Lines changed: 4 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,22 @@
2222

2323
"""Utilities dealing with encryption and randomness."""
2424

25-
import binascii
26-
import random
25+
import secrets
2726
from string import ascii_lowercase
2827

2928
import bcrypt
30-
from Cryptodome import Random
31-
from Cryptodome.Cipher import AES
32-
33-
from cmscommon.binary import bin_to_hex, hex_to_bin, bin_to_b64, b64_to_bin
3429

3530

3631
__all__ = [
3732
"get_random_key", "get_hex_random_key",
3833

39-
"encrypt_binary", "decrypt_binary",
40-
"encrypt_number", "decrypt_number",
41-
4234
"generate_random_password",
4335

4436
"validate_password", "build_password", "hash_password",
4537
"parse_authentication",
4638
]
4739

4840

49-
_RANDOM = Random.new()
50-
5141
# bcrypt difficulty parameter. This is here so that it can be set to a lower
5242
# value when running unit tests. It seems that the lowest accepted value is 4.
5343
BCRYPT_ROUNDS = 12
@@ -56,96 +46,15 @@ def get_random_key() -> bytes:
5646
"""Generate 16 random bytes, safe to be used as AES key.
5747
5848
"""
59-
return _RANDOM.read(16)
49+
return secrets.token_bytes(16)
6050

6151

6252
def get_hex_random_key() -> str:
6353
"""Generate 16 random bytes, safe to be used as AES key.
6454
Return it encoded in hexadecimal.
6555
6656
"""
67-
return bin_to_hex(get_random_key())
68-
69-
70-
def encrypt_binary(pt: bytes, key_hex: str) -> str:
71-
"""Encrypt the plaintext with the 16-bytes key.
72-
73-
A random salt is added to avoid having the same input being
74-
encrypted to the same output.
75-
76-
pt: the "plaintext" to encode.
77-
key_hex: a 16-bytes key in hex (a string of 32 hex chars).
78-
79-
return: pt encrypted using the key, in a format URL-safe
80-
(more precisely, base64-encoded with alphabet "a-zA-Z0-9.-_").
81-
82-
"""
83-
key = hex_to_bin(key_hex)
84-
# Pad the plaintext to make its length become a multiple of the block size
85-
# (that is, for AES, 16 bytes), using a byte 0x01 followed by as many bytes
86-
# 0x00 as needed. If the length of the message is already a multiple of 16
87-
# bytes, add a new block.
88-
pt_pad = pt + b'\01' + b'\00' * (16 - (len(pt) + 1) % 16)
89-
# The IV is a random block used to differentiate messages encrypted with
90-
# the same key. An IV should never be used more than once in the lifetime
91-
# of the key. In this way encrypting the same plaintext twice will produce
92-
# different ciphertexts.
93-
iv = get_random_key()
94-
# Initialize the AES cipher with the given key and IV.
95-
aes = AES.new(key, AES.MODE_CBC, iv)
96-
ct = aes.encrypt(pt_pad)
97-
# Convert the ciphertext in a URL-safe base64 encoding
98-
ct_b64 = bin_to_b64(iv + ct)\
99-
.replace('+', '-').replace('/', '_').replace('=', '.')
100-
return ct_b64
101-
102-
103-
def decrypt_binary(ct_b64: str, key_hex: str) -> bytes:
104-
"""Decrypt a ciphertext generated by encrypt_binary.
105-
106-
ct_b64: the ciphertext as produced by encrypt_binary.
107-
key_hex: the 16-bytes key in hex format used to encrypt.
108-
109-
return: the plaintext.
110-
111-
raise (ValueError): if the ciphertext is invalid.
112-
113-
"""
114-
key = hex_to_bin(key_hex)
115-
try:
116-
# Convert the ciphertext from a URL-safe base64 encoding to a
117-
# bytestring, which contains both the IV (the first 16 bytes) as well
118-
# as the encrypted padded plaintext.
119-
iv_ct = b64_to_bin(
120-
ct_b64.replace('-', '+').replace('_', '/').replace('.', '='))
121-
aes = AES.new(key, AES.MODE_CBC, iv_ct[:16])
122-
# Get the padded plaintext.
123-
pt_pad = aes.decrypt(iv_ct[16:])
124-
# Remove the padding.
125-
# TODO check that the padding is correct, i.e. that it contains at most
126-
# 15 bytes 0x00 preceded by a byte 0x01.
127-
pt = pt_pad.rstrip(b'\x00')[:-1]
128-
return pt
129-
except (TypeError, binascii.Error):
130-
raise ValueError('Could not decode from base64.')
131-
except ValueError:
132-
raise ValueError('Wrong AES cryptogram length.')
133-
134-
135-
def encrypt_number(num: int, key_hex: str) -> str:
136-
"""Encrypt an integer number, with the same properties as
137-
encrypt_binary().
138-
139-
"""
140-
hexnum = b"%x" % num
141-
return encrypt_binary(hexnum, key_hex)
142-
143-
144-
def decrypt_number(enc: str, key_hex: str) -> int:
145-
"""Decrypt an integer number encrypted with encrypt_number().
146-
147-
"""
148-
return int(decrypt_binary(enc, key_hex), 16)
57+
return get_random_key().hex()
14958

15059

15160
def generate_random_password() -> str:
@@ -154,7 +63,7 @@ def generate_random_password() -> str:
15463
return: a random string.
15564
15665
"""
157-
return "".join((random.choice(ascii_lowercase) for _ in range(6)))
66+
return "".join((secrets.choice(ascii_lowercase) for _ in range(6)))
15867

15968

16069
def parse_authentication(authentication: str) -> tuple[str, str]:

cmscommon/digest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import hashlib
2020
import io
2121

22-
from cmscommon.binary import bin_to_hex
23-
2422

2523
__all__ = [
2624
"Digester", "bytes_digest", "path_digest"
@@ -39,7 +37,7 @@ def update(self, b: bytes):
3937

4038
def digest(self) -> str:
4139
"""Return the digest as an hex string."""
42-
return bin_to_hex(self._hasher.digest())
40+
return self._hasher.digest().hex()
4341

4442

4543
def bytes_digest(b: bytes) -> str:

cmstestsuite/unit_tests/cmscommon/binary_test.py

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)