Example #1
0
    def _createContext(self, encrypt):
        context = m2.cipher_ctx_new()
        keybits = len(self.key) * 8
        mode = [None, 'ecb', 'cbc', 'cfb1', 'cfb8', 'cfb64', 'cfb128', 'ofb', 'ctr'][self.mode]
        if mode is None:
            raise AssertionError

        ciph_type_name = 'aes_%d_%s' % (keybits, mode)
        cipherType = getattr(m2, ciph_type_name)()

        m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
        return context
Example #2
0
    def _createContext(self, encrypt):
        context = m2.cipher_ctx_new()
        if len(self.key)==16:
            cipherType = m2.aes_128_cbc()
        elif len(self.key)==24:
            cipherType = m2.aes_192_cbc()
        elif len(self.key)==32:
            cipherType = m2.aes_256_cbc()
        else:
            raise AssertionError("Key is bad size: %s" % len(self.key))

        m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
        return context
Example #3
0
    def _createContext(self, encrypt):
        context = m2.cipher_ctx_new()
        keybits = len(self.key) * 8
        mode = [
            None, 'ecb', 'cbc', 'cfb1', 'cfb8', 'cfb64', 'cfb128', 'ofb', 'ctr'
        ][self.mode]
        if mode is None:
            raise AssertionError

        ciph_type_name = 'aes_%d_%s' % (keybits, mode)
        cipherType = getattr(m2, ciph_type_name)()

        m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
        return context
Example #4
0
 def __init__(self, alg, key, iv, op, key_as_bytes=0, d='md5',
              salt='12345678', i=1, padding=1):
     cipher = getattr(m2, alg, None)
     if cipher is None:
         raise ValueError('unknown cipher', alg)
     self.cipher = cipher()
     if key_as_bytes:
         kmd = getattr(m2, d, None)
         if kmd is None:
             raise ValueError('unknown message digest', d)
         key = m2.bytes_to_key(self.cipher, kmd(), key, salt, iv, i)
     self.ctx = m2.cipher_ctx_new()
     m2.cipher_init(self.ctx, self.cipher, key, iv, op)
     self.set_padding(padding)
     del key
Example #5
0
 def test_cipher_init_reinit(self):
     ctx = m2.cipher_ctx_new()
     m2.cipher_init(ctx, m2.aes_128_cbc(), b'\x01' * (128//8), b'\x02' * (128//8), 1)
     m2.cipher_init(ctx, m2.aes_128_cbc(), None, None, 1)
Example #6
0
 def _createContext(self, encrypt):
     context = m2.cipher_ctx_new()
     cipherType = m2.des_ede3_cbc()
     m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
     return context
Example #7
0
 def _createContext(self, encrypt):
     context = m2.cipher_ctx_new()
     cipherType = m2.des_ede3_cbc()
     m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
     return context