Push current state to Gitea

This commit is contained in:
Martin Asprusten
2025-04-17 19:25:23 +02:00
commit 889b6546ff
16 changed files with 746 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
import math
import hashlib
# According to NIST Special Publication 800-90A, Revision 1, this should be a cryptographically secure pseudo-random
# number generator, provided I've implemented it properly, which is of course very possible I haven't
class CSPRNG:
def __init__(self, entropy: bytes, nonce: bytes=b'', personalization_string: bytes=b''):
self.V = hash_df(entropy + nonce + personalization_string, 888).to_bytes(111)
self.C = hash_df(int(0).to_bytes(0) + self.V, 888).to_bytes(111)
self.reseed_counter = 1
def hash_gen(self, requested_number_of_bits: int):
m = int(math.ceil(requested_number_of_bits / 512))
data = self.V
w = b''
for i in range(m):
hasher = hashlib.sha512()
hasher.update(data)
w += hasher.digest()
data = int.from_bytes(data)
data = (data + 1) % 2 ** 888
data = data.to_bytes(111)
w = int.from_bytes(w)
w = w >> (512 * m - requested_number_of_bits)
return w
def get_random_bytes(self, number_of_bytes: int):
return_bytes = self.hash_gen(number_of_bytes * 8).to_bytes(number_of_bytes)
hasher = hashlib.sha512()
hasher.update(int(3).to_bytes(1) + self.V)
h = hasher.digest()
new_v = (int.from_bytes(self.V) + int.from_bytes(h) + int.from_bytes(self.C) + self.reseed_counter) % 2 ** 888
self.V = new_v.to_bytes(111)
self.reseed_counter += 1
return return_bytes
# Hash derivation function as specified in section 10.3.1 of NIST Special Publication 800-90A, Revision 1
def hash_df(input_string: bytes, number_of_bits: int):
temp = b''
length = int(math.ceil(number_of_bits / 512))
for i in range(length):
hash_input = (i + 1).to_bytes(1) + number_of_bits.to_bytes(4) + input_string
m = hashlib.sha512()
m.update(hash_input)
temp += m.digest()
number = int.from_bytes(temp)
number = number >> (512 * length - number_of_bits)
return number
+92
View File
@@ -0,0 +1,92 @@
import base64
import secrets
import math
import Crypto.Util
# This commutative cipher is based on the SRA cryptographical system, which is just a modification of RSA where the
# modulus n is known, but both the encryption and decryption exponents are kept secret. As long as both keys use the
# same modulus, this cryptography system is commutative, i.e. Ea(Eb(x)) = Eb(Ea(x)) if encryption with key a is denoted
# as Ea() and encryption with key b is denoted as Eb.
class CommutativeCipher:
def __init__(self, p, q):
self.n = p*q
carmichael_function = (p-1) * (q-1)
# Make the exponent have almost as many bits as the modulus
number_of_bits = int(math.ceil(math.log(self.n) / math.log(2)))
self.e = Crypto.Util.number.getPrime(number_of_bits-10, randfunc=secrets.token_bytes)
self.d = pow(self.e, -1, carmichael_function)
def encode(self, message):
message_was_base64 = False
message_was_bytes = False
if isinstance(message, str):
message_bytes = base64.b64decode(message)
message = message_bytes
message_was_base64 = True
try:
message_int = int.from_bytes(message)
message = message_int
message_was_bytes = True
except TypeError:
# Assume message is already an integer
pass
if not isinstance(message, int):
raise Exception(
'The message to encrypt was not of the correct type (base64 string, bytes-like object, or integer'
)
if message >= self.n:
raise Exception(
'The message is equal to or larger than the modulus'
)
encrypted = pow(message, self.e, self.n)
if message_was_bytes:
# Find number of bits
number_of_bits = int(math.ceil(math.log(encrypted) / math.log(2)))
number_of_bytes = int(math.ceil(number_of_bits / 8))
encrypted = encrypted.to_bytes(number_of_bytes)
if message_was_base64:
encrypted = base64.b64encode(encrypted)
return encrypted
def decode(self, cipher):
cipher_was_base64 = False
cipher_was_bytes = False
if isinstance(cipher, str):
cipher_was_base64 = True
cipher = base64.b64decode(cipher)
try:
cipher_int = int.from_bytes(cipher)
cipher = cipher_int
cipher_was_bytes = True
except TypeError:
pass
if not isinstance(cipher, int):
raise Exception('The passed cipher was not a valid type (base64 string, bytes object or integer)')
if cipher >= self.n:
raise Exception('The passed cipher is equal to or larger than the modulus')
decrypted = pow(cipher, self.d, self.n)
if cipher_was_bytes:
number_of_bits = int(math.ceil(math.log(decrypted)/math.log(2)))
number_of_bytes = int(math.ceil(number_of_bits / 8))
decrypted = decrypted.to_bytes(number_of_bytes)
if cipher_was_base64:
decrypted = base64.b64encode(decrypted)
return decrypted
View File