rsa padding done
This commit is contained in:
parent
d087abd7e6
commit
d454bde3c3
|
@ -6,7 +6,7 @@ Since this (auth) hash did not have a name before, I gave it the name 'authur1'
|
|||
|
||||
@author: Christoph J. Scherr <software@cscherr.de>
|
||||
@license: MIT
|
||||
@source: https://git.cscherr.de/PlexSheep/plexcryptool/src/branch/master/plexcryptool/authur1.py
|
||||
@source: https://git.cscherr.de/PlexSheep/plexcryptool/
|
||||
"""
|
||||
import argparse
|
||||
import random
|
||||
|
|
|
@ -5,7 +5,7 @@ you also need the chiffrat.txt file in the same directory.
|
|||
|
||||
@license: MIT
|
||||
@author: Christoph J. Scherr <software@cscherr.de>
|
||||
@source: https://git.cscherr.de/PlexSheep/plexcryptool/src/branch/master/plexcryptool/basic-decrypt.py
|
||||
@source: https://git.cscherr.de/PlexSheep/plexcryptool/
|
||||
"""
|
||||
MAX_IN_LINE = 16
|
||||
DE_MOST_COMMON = ['E', 'N', 'I', 'R', 'A']
|
||||
|
|
|
@ -7,6 +7,7 @@ Common tools that may be used by many
|
|||
"""
|
||||
|
||||
from math import floor
|
||||
from typing import Literal
|
||||
|
||||
def byte_xor(ba0: bytearray, ba1: bytearray) -> bytearray:
|
||||
"""
|
||||
|
@ -27,3 +28,19 @@ def calc_byte_len(n: int) -> int:
|
|||
return 1 + floor(len)
|
||||
else:
|
||||
return floor(len)
|
||||
|
||||
def int_to_bytearray(n: int, size: int|None = None, endianness = 'big') -> bytearray:
|
||||
"""
|
||||
convert an integer to a bytearray
|
||||
|
||||
you can specify a size if you want, if the number is too big an Exception will be raised.
|
||||
size is in bytes
|
||||
"""
|
||||
brepr: bytes = n.to_bytes(calc_byte_len(n), endianness)
|
||||
ba: bytearray = bytearray(brepr)
|
||||
if size is not None and len(ba) > size:
|
||||
raise Exception("Number is larger than requested")
|
||||
while size is not None and size > len(ba):
|
||||
ba: bytearray = bytearray(1) + ba
|
||||
|
||||
return ba
|
||||
|
|
|
@ -4,8 +4,7 @@ A small script to help analyze the md5 hash function.
|
|||
|
||||
@author: Christoph J. Scherr <software@cscherr.de>
|
||||
@license: MIT
|
||||
@source: https://git.cscherr.de/PlexSheep/plexcryptool/src/branch/master/plexcryptool/md5-analyzer.py
|
||||
TODO do I need to implement md5 by myself for the assignment?
|
||||
@source: https://git.cscherr.de/PlexSheep/plexcryptool/
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
0976f89569d94414fed4f7714cd85c840dbf338781926f5d1f6284688c97eaeef7f42b3d844c70468cfb960092235855da5d5c66107dfee4204c1a78512ee00306da957a856a085affe40b2ab32fc90a8c8e059feea9a83e753618aeae04884aa1658afe59a69c30ec5c014a5da06ff484143ca1ea884b44a3f92d063db8503f
|
|
@ -4,7 +4,7 @@ Perform RSA-OAEP
|
|||
|
||||
@author: Christoph J. Scherr <software@cscherr.de>
|
||||
@license: MIT
|
||||
@source: https://git.cscherr.de/PlexSheep/plexcryptool/src/branch/master/plexcryptool/trash-hash.py
|
||||
@source: https://git.cscherr.de/PlexSheep/plexcryptool/
|
||||
"""
|
||||
|
||||
import argparse
|
||||
|
|
|
@ -0,0 +1,104 @@
|
|||
"""
|
||||
Rsa padding script for croptology lectures
|
||||
|
||||
@author: Christoph J. Scherr <software@cscherr.de>
|
||||
@license: MIT
|
||||
@source: https://git.cscherr.de/PlexSheep/plexcryptool/
|
||||
"""
|
||||
|
||||
import argparse
|
||||
|
||||
import plexcryptool
|
||||
import common
|
||||
|
||||
# predefined constants
|
||||
P = 2**206 - 5
|
||||
Q = 2**226 -5
|
||||
N = P*Q
|
||||
D = 0xaffe0815
|
||||
CIPHERTEXTS: list[int] = [
|
||||
0x78766a52455329b486aaa414c3a029834a7e4b6ed87019dce4056f4d8999b137404d9ec4df28da201c9b0bc142deb1d86ff94d83becc,
|
||||
0x670b865216dfd0aacd5f7fa8802e704fa82f3fb9c7dbe3eb5a9ec308a1a2288648b15d5cc8ba2f54b245a972aea977932c9c84cf6422,
|
||||
0x61d5f2a4298bff3d6ebcd78830fb9181d97235623819eb7c60b92dcdf836a6cf731c60187e72f471c05d1c6eab216c3f6032af3c5370,
|
||||
0x3651009d02a0c72b9bc206c57d12277594d9eaad28bb3de5d661670b42f1cfafe688b9674e34d4ad79db898205417086e7e1877b9ef1,
|
||||
0x96e51d4675c6be5b14ec0cf2a9e9a9610a99d632723b3f1fcfc6b36806f5d74045f47622817cc35f6ffe9afe29f0aa236cbe12371651,
|
||||
]
|
||||
|
||||
def padding_check(cihpertext: int, private_key: tuple[int, int], verbose: bool = False) -> bool:
|
||||
"""
|
||||
Check the padding of a given cihpertext
|
||||
"""
|
||||
decrypted: int = pow(cihpertext, private_key[0], private_key[1])
|
||||
# the ciphertexts seem to all be 54B long, so I have just hardcoded that
|
||||
cleartext: bytearray = common.int_to_bytearray(decrypted, 54)
|
||||
if verbose:
|
||||
print(f"original:\t{cihpertext:054x}")
|
||||
print(f"decrypted:\t{cleartext.hex()}")
|
||||
|
||||
index: int = 0
|
||||
padding_active: bool = False
|
||||
for b in cleartext:
|
||||
|
||||
if index == 0 and b != 0:
|
||||
# the first byte has to be 0x00
|
||||
if verbose:
|
||||
print(f"error:\t\tThe first byte is not 00: {b:02x}")
|
||||
return False
|
||||
elif index == 1:
|
||||
# the second byte is the padding type, we only accept 01 and 02 type padding
|
||||
if b == 1:
|
||||
padding_active = True
|
||||
pad_type = b
|
||||
if verbose:
|
||||
print(f"type:\t\t01 deterministic padding")
|
||||
elif b == 2:
|
||||
padding_active = True
|
||||
pad_type = b
|
||||
if verbose:
|
||||
print(f"type:\t\t02 nondeterministic padding")
|
||||
else:
|
||||
if verbose:
|
||||
print(f"error:\t\tThe second byte is not 01 or 02 (block type): {b:02x}")
|
||||
return False
|
||||
elif padding_active:
|
||||
if b == 0:
|
||||
# padding has ended
|
||||
padding_active = False
|
||||
continue
|
||||
if pad_type == 1 and b != 0xff:
|
||||
if verbose:
|
||||
print(f"error:\t\tPadding of type 01 contains non ff byte: {b:02x}")
|
||||
return False
|
||||
elif pad_type == 2:
|
||||
if b == 0:
|
||||
# must be random non 00
|
||||
if verbose:
|
||||
print(f"error:\t\tPadding of type 02 contains 00 byte: {b:02x}")
|
||||
return False
|
||||
|
||||
|
||||
|
||||
index += 1
|
||||
|
||||
if padding_active:
|
||||
if verbose:
|
||||
print(f"error:\t\tPadding has not been ended with 00 byte")
|
||||
|
||||
|
||||
return True
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(prog="rsa-padding", description='validates a rsa padding')
|
||||
parser.add_argument('-v', '--verbose', action="store_true",
|
||||
help='append some auth hashed stuff')
|
||||
args = parser.parse_args()
|
||||
|
||||
results: list[bool] = []
|
||||
for c in CIPHERTEXTS:
|
||||
b = padding_check(c, (D, N), args.verbose)
|
||||
print(f"Passed:\t\t{b}")
|
||||
plexcryptool.cplex.printing.seperator()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -4,7 +4,7 @@ Dirty hash we covered in an excercise for Week 16 of 2023 in cryptography
|
|||
|
||||
@author: Christoph J. Scherr <software@cscherr.de>
|
||||
@license: MIT
|
||||
@source: https://git.cscherr.de/PlexSheep/plexcryptool/src/branch/master/plexcryptool/trash-hash.py
|
||||
@source: https://git.cscherr.de/PlexSheep/plexcryptool/
|
||||
"""
|
||||
import math
|
||||
import argparse
|
||||
|
|
Loading…
Reference in New Issue