def test_nonexistent_aead_cipher(self): from cryptography.hazmat.backends.commoncrypto.backend import Backend b = Backend() cipher = Cipher( DummyCipher(), GCM(b"fake_iv_here"), backend=b, ) with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER): cipher.encryptor()
def test_nonexistent_aead_cipher(self): from cryptography.hazmat.backends.commoncrypto.backend import Backend b = Backend() cipher = Cipher( DummyCipher(), GCM(b"fake_iv_here"), backend=b, ) with pytest.raises(UnsupportedAlgorithm): cipher.encryptor()
def aes_cbc_enc(aesKey, sPlainMsg): ''' AuthenticatedEncryption - Do a encrypt then mac operation on given message. a random iv is generated but not transmitted, instead a random block is prepended to the message and encrypted. Inturn the same can be discarded during decrypting. Each CBC block depends on the previous encrypted block so the 0th block acts as a inplace iv at one level. Encryption uses AES in CBC mode with PKCS7 padding where required. MAC uses HMAC with SHA256 As encryption and mac using independent algorithms so the key is shared wrt encryption and mac operations. With aes length of key passed is independent of the block size used, and user can use any valid aes key size. This is similar to the age old encrypt as well as sign messages to ensure confidentiality as well as authenticity. But then instead of using asymm logic for signing, use a hash with hidden key based logic. Also make sure that one checks the authenticity before trying to use it or work on it, so that new oracles arent opened up, beyond the minimal unavoidable oracle. ''' ### Prepare for encryption blockLen = 16 iv = os.urandom(blockLen) aes = AES(aesKey) cbc = CBC(iv) aesCbc = Cipher(aes, cbc, default_backend()) aesCbcEnc = aesCbc.encryptor() random0thBlock = os.urandom(blockLen) # This allows iv to be discarded # so also while decrypting discard the 0th block plainMsg = random0thBlock + sPlainMsg.encode('utf-8') # do PKCS7 padding if bInternalPadder: padLen = blockLen - (len(plainMsg) % blockLen) for i in range(padLen): plainMsg += int.to_bytes(padLen, 1, 'little') else: pad = PKCS7(blockLen * 8).padder() plainMsg = pad.update(plainMsg) plainMsg += pad.finalize() ### do encrypt encMsg = aesCbcEnc.update(plainMsg) encFina = aesCbcEnc.finalize() encMsg = encMsg + encFina ### Prepare for mac sha256 = SHA256() hmac = HMAC(aesKey, sha256, default_backend()) ### do mac hmac.update(encMsg) macMsg = hmac.finalize() return encMsg, macMsg