Esempio n. 1
0
 def __init__(self, cipher: EncryptionAlg):
     """
     Parameters:
         cipher (EncryptionAlg): Instantiated encryption algorithm.
     """
     Primitive.__init__(self)
     self.underlying_mode = ECB(cipher)
Esempio n. 2
0
 def __init__(self, cipher: EncryptionAlg):
     """
     Parameters:
         cipher (EncryptionAlg): Instantiated encryption algorithm.
     """
     Primitive.__init__(self)
     self.cipher = cipher
Esempio n. 3
0
    def __init__(self,
                 curve: TwistedEdwardsCurve = EdwardsCurve25519,
                 hash_obj: object = SHA512(),
                 d: int = None,
                 A: TwistedEdwardsPoint = None,
                 a: int = None,
                 h: bytes = None,
                 clamp: bool = True):
        """
        Parameters:
            curve (TwistedEdwardsCurve): Curve used for calculations.
            hash_obj           (object): Instantiated object with compatible hash interface.
            d                     (int): Private key.
            A     (TwistedEdwardsPoint): (Optional) Public point.
            a                     (int): (Optional) Public scalar.
            h                   (bytes): (Optional) Hashed private key.
            clamp                (bool): Whether or not to clamp the public scalar.
        """
        Primitive.__init__(self)

        self.B = curve.B
        self.curve = curve
        self.d = Bytes.wrap(d or max(1,
                                     Bytes.random(hash_obj.digest_size).int()))
        self.H = hash_obj

        self.h = h or hash_obj.hash(self.d)

        a = a or self.h[:self.curve.b // 8].int()
        self.a = curve.clamp_to_curve(a, True) if clamp else a

        self.A = A or self.B * self.a
Esempio n. 4
0
 def __init__(self, digest_bit_length: int):
     """
     Parameters:
         digest_bit_length (int): Desired digest length in bits.
     """
     super().__init__(r=1088, c=512, bits=digest_bit_length, padding=0x1F)
     Primitive.__init__(self)
Esempio n. 5
0
 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)
Esempio n. 6
0
 def __init__(self, cipher: EncryptionAlg):
     """
     Parameters:
         cipher (EncryptionAlg): Instantiated encryption algorithm.
     """
     Primitive.__init__(self)
     self.cipher = cipher
     self.padder = PKCS7(self.cipher.block_size)
Esempio n. 7
0
 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)
Esempio n. 8
0
 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
Esempio n. 9
0
 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
Esempio n. 10
0
 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
Esempio n. 11
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)
Esempio n. 12
0
 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
Esempio n. 13
0
 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
Esempio n. 14
0
 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()
Esempio n. 15
0
 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
Esempio n. 16
0
 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)
Esempio n. 17
0
    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)
Esempio n. 18
0
    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)
Esempio n. 19
0
    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()
Esempio n. 20
0
    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)
Esempio n. 21
0
 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
Esempio n. 22
0
    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)
Esempio n. 23
0
    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
Esempio n. 24
0
    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()
Esempio n. 25
0
 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
Esempio n. 26
0
 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
Esempio n. 27
0
    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()
Esempio n. 28
0
    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)
Esempio n. 29
0
 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
Esempio n. 30
0
    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()