def __init__(self, cipher: EncryptionAlg=None, iv: bytes=b'\x00' * 16): """ Parameters: cipher (EncryptionAlg): Instantiated encryption algorithm. iv (bytes): Initialization vector for CBC mode. """ Primitive.__init__(self) self.cbc = CBC(cipher or Rijndael(Bytes.random(32)), iv)
def __init__(self, cipher: EncryptionAlg, iv: bytes): """ Parameters: cipher (EncryptionAlg): Instantiated encryption algorithm. iv (bytes): Bytes-like initialization vector. """ Primitive.__init__(self) self.underlying_mode = CBC(cipher, iv)
def __init__(self, cipher: EncryptionAlg, sector_encryptor: FunctionType): """ Parameters: cipher (EncryptionAlg): Instantiated encryption algorithm. sector_encryptor (func): Function that takes in a plaintext and returns a ciphertext. """ Primitive.__init__(self) self.cipher = cipher self.sector_encryptor = sector_encryptor
def __init__(self, key: bytes): """ Parameters: key (bytes): Bytes-like object to key the cipher. """ Primitive.__init__(self) self.key = Bytes.wrap(key) self.key_schedule = key_schedule self.round_func = round_func
def __init__(self, cipher: EncryptionAlg, iv: bytes=RFC3394_IV): """ Parameters: cipher (EncryptionAlg): Instantiated encryption algorithm. iv (bytes): Bytes-like initialization vector. """ Primitive.__init__(self) self.cipher = cipher self.iv = iv
def __init__(self, key: bytes): """ Parameters: key (bytes): Key (40-2040 bits). """ Primitive.__init__(self) self.key = key self.S = self.key_schedule(key) self.i = 0 self.j = 0
def __init__(self, cipher: EncryptionAlg, iv: bytes): """ Parameters: cipher (EncryptionAlg): Instantiated encryption algorithm. iv (bytes): Bytes-like initialization vector. """ Primitive.__init__(self) self.cipher = cipher self.iv = iv self.padder = PKCS7(self.cipher.block_size)
def __init__(self, key: bytes, desired_hash_len: int): """ Parameters: key (bytes): (Optional) Bytes-like object to key the hash. desired_hash_len (int): Desired output length. """ Primitive.__init__(self) self.key = key self.digest_size = desired_hash_len self.block_size = self.IMPL_BLOCK_SIZE
def __init__(self, key: bytes): """ Parameters: key (bytes): Bytes-like object to key the cipher. """ Primitive.__init__(self) self.key = Bytes.wrap(key) self.S = [] self.K = None self.k = 0 self._key_schedule()
def __init__(self, cipher: EncryptionAlg, nonce: bytes): """ Parameters: cipher (EncryptionAlg): Instantiated encryption algorithm. nonce (bytes): Bytes-like nonce. """ Primitive.__init__(self) self.cipher = cipher self.nonce = Bytes.wrap(nonce) self.counter = 0 self.byteorder = self.nonce.byteorder
def __init__(self, cipher: EncryptionAlg, nonce: bytes): """ Parameters: cipher (EncryptionAlg): Instantiated encryption algorithm. nonce (bytes): Bytes-like nonce. """ Primitive.__init__(self) self.cipher = cipher self.nonce = nonce self.ctr = CTR(self.cipher, b'') self.cmac = CMAC(self.cipher)
def __init__(self, initial_state: bytes = state_to_bytes(iv)): """ Parameters: initial_state (bytes): (Optional) Initial internal state. """ super().__init__(initial_state=initial_state, compression_func=compression_func, digest_size=16, endianness='little') Primitive.__init__(self)
def __init__(self, initial_state: bytes = INIT_STATE): """ Parameters: initial_state (bytes): (Optional) Initial internal state. """ super().__init__(initial_state=initial_state, compression_func=COMPRESS, digest_size=20, endianness='little') Primitive.__init__(self)
def __init__(self, cipher: EncryptionAlg, mac_len: int): """ Parameters: cipher (EncryptionAlg): Instantiated encryption algorithm. mac_len (int): Length of MAC to generate. """ Primitive.__init__(self) self.cipher = cipher self.cmac = CBCMAC(self.cipher) self.mac_len = mac_len self.ctr = CTR(self.cipher, b'\x00' * 16)
def __init__(self, key: bytes): """ Parameters: key (bytes): Bytes-like object to key the cipher. """ Primitive.__init__(self) self.key = Bytes(key, byteorder='big') self._stretch_key() self.key = Bytes(self.key.int(), 'little').zfill(32) self.K, self.K_hat = self.make_subkeys()
def __init__(self, initial_state: bytes=state_to_bytes([0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476])): """ Parameters: initial_state (bytes): (Optional) Initial internal state. """ super().__init__( initial_state=initial_state, compression_func=compression_func, digest_size=16, endianness='little' ) Primitive.__init__(self)
def __init__(self, key: bytes, nonce: bytes, r: bytes, cipher=Rijndael): """ Parameters: key (bytes): Bytes-like object to key the underlying cipher. nonce (bytes): Bytes-like nonce. r (bytes): Bytes-like polynomial. cipher (class): Instantiable class representing a block cipher. """ Primitive.__init__(self) self.key = key self.nonce = nonce self.r = Bytes.wrap(r, byteorder='little').to_int() self.cipher = cipher
def __init__(self, key: bytes): """ Parameters: key (bytes): Bytes-like object to key the cipher. """ Primitive.__init__(self) key = Bytes.wrap(key) if not len(key) in [8, 16, 24]: raise ValueError('`key` size must be in [8, 16, 24]') self.key = key self.des_arr = [DES(subkey.zfill(8)) for subkey in key.chunk(8)] self.block_size = 8
def __init__(self, key: bytes, run_key_schedule: bool=True): """ Parameters: key (bytes): Bytes-like object to key the cipher. run_key_schedule (bool): Whether or not to run the key schedule. Useful when extending Blowfish (i.e. bcrypt). """ Primitive.__init__(self) self.key = Bytes.wrap(key) self.P = deepcopy(P) self.S = [deepcopy(S1), deepcopy(S2), deepcopy(S3), deepcopy(S4)] self.block_size = 8 if run_key_schedule: self.key_schedule()
def __init__(self, key: bytes = None, H: FunctionType = SHA256().hash, q: int = DiffieHellman.MODP_2048): """ Parameters: key (bytes): Bytes-like object shared by both parties to authenticate each other. H (func): Cryptographic hash function. Takes in bytes and returns the hash digest. q (int): Modulus. """ Primitive.__init__(self) self.key = key or Bytes(random_int_between(1, q)) self.q = q self.A = random_int_between(1, q) self.a = random_int_between(1, q) self.H = H
def __init__(self, d: int = None, pub: WeierstrassPoint = None, G: WeierstrassPoint = P256.G): """ Parameters: d (int): Secret key. G (WeierstrassPoint): Generator point on an elliptical curve. """ Primitive.__init__(self) self.d = d or random_int(G.ring.cardinality()) self.G = G self.pub = pub if not pub: self.recompute_pub()
def __init__(self, g: int = 2, p: int = DiffieHellman.MODP_2048, key: int = None): """ Parameters: g (int): Generator. p (int): Prime modulus. key (int): Key. """ Primitive.__init__(self) self.key = key or random_int_between(1, p) self.g = g self.p = p self.pub = pow(self.g, self.key, self.p)
def __init__(self, G: WeierstrassCurve, hash_obj: object = SHA256(), d: int = None): """ Parameters: G (WeierstrassCurve): Generator point for a curve. hash_obj (object): Instantiated object with compatible hash interface. d (int): (Optional) Private key. """ Primitive.__init__(self) self.G = G self.q = self.G.curve.q self.d = Bytes.wrap(d).int() if d else random_int_between(1, self.q) self.Q = self.d * self.G self.hash_obj = hash_obj
def __init__(self, key: bytes): """ Parameters: key (bytes): Bytes-like object to key the cipher. """ Primitive.__init__(self) key = Bytes.wrap(key) if not len(key) in [16, 24, 32]: raise ValueError("`key` must be 128, 192, or 256 bits long.") self.key = key self.k = [] self.ke = [] self.kw = [None] * 4 self.key_schedule()
def __init__(self, key: bytes, iv: bytes): """ Parameters: key (bytes): Key (128 bits). iv (bytes): Initialization vector (16 bytes). """ Primitive.__init__(self) self.key = Bytes.wrap(key) self.iv = iv self.lfsr_states = [0] * 16 self.R = [0] * 2 self.X = [0] * 4 self.initialize() self.reorganize_bits() self.F() self.run_lfsr()
def __init__(self, g: int = 2, p: int = MODP_2048, q: int = None, key: int = None): """ Parameters: key (int): Secret key. g (int): Exponent base. p (int): Modulus. q (int): Order. """ Primitive.__init__(self) self.key = key or random_int_between(2, q or p) self.g = g self.p = p self.q = q
def __init__(self, initial_state: bytes = state_to_bytes([ 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xC3D2E1F0 ])): """ Parameters: initial_state (bytes): (Optional) Initial internal state. """ if type(initial_state) is list: initial_state = state_to_bytes(initial_state) super().__init__( initial_state=initial_state, compression_func=compression_func, digest_size=20, ) Primitive.__init__(self)
def __init__(self, cipher: EncryptionAlg): """ Parameters: cipher (EncryptionAlg): Instantiated encryption algorithm. """ Primitive.__init__(self) self.cipher = cipher self.H = self.cipher.encrypt(b'\x00' * 16).int() self.ctr = CTR(self.cipher, b'\x00' * 8) # Precompute the product table self.product_table = [0] * 16 self.product_table[reverse_bits(1)] = self.H for i in range(2, 16, 2): self.product_table[reverse_bits(i)] = self.gcm_shift( self.product_table[reverse_bits(i // 2)]) self.product_table[reverse_bits( i + 1)] = self.product_table[reverse_bits(i)] ^ self.H
def __init__(self, hash_obj: object=SHA256(), p: int=None, q: int=None, g: int=None, x: int=None, L: int=2048, N: int=256): """ Parameters: hash_obj (object): Instantiated object with compatible hash interface. p (int): (Optional) Prime modulus. q (int): (Optional) Prime modulus. g (int): (Optional) Generator. x (int): (Optional) Private key. L (int): (Optional) Bit length of `p`. N (int): (Optional) Bit length of `q`. """ Primitive.__init__(self) # Parameter generation # https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf if not q: q = find_prime(N) # Start somewhere in 2**(L-1); ensure it's even i = Bytes.random((L-1) // 8).int() // 2 * 2 # Construct the base as an even multiple of `q` base = 2**(L-1) // (2*q) * 2 while not is_prime((base + i) * q + 1): i += 2 p = (base + i) * q + 1 assert (p-1) % q == 0 # Construct `g` while True: h = Bytes.random(N // 8).int() % (p-1) g = pow(h, (p-1) // q, p) if h > 1 and h < (p-1) and g > 1: break self.p = p self.q = q self.g = g self.x = x or random_int_between(1, self.q) self.y = pow(self.g, self.x, self.p) self.hash_obj = hash_obj
def __init__(self, r: int, c: int, digest_bit_size: int, auto_reset_state: bool = True): """ Parameters: r (int): Bit-size of the sponge function. c (int): Sponge capacity. digest_bit_size (int): Desired size of output. auto_reset_state (bool): Whether or not to reset the internal state before hashing. """ super().__init__(self.keccak_f, self.pad, r, c) Primitive.__init__(self) self.w = (r + c) // 25 self.n = int(log(self.w, 2) * 2 + 12) self.digest_size = (digest_bit_size // 8) self.auto_reset_state = auto_reset_state