<s> from __future__ import absolute_import , division , print_function <EOL> import binascii <EOL> import pytest <EOL> from cryptography . exceptions import ( <EOL> AlreadyFinalized , InvalidKey , _Reasons <EOL> ) <EOL> from cryptography . hazmat . backends . interfaces import HMACBackend <EOL> from cryptography . hazmat . backends . interfaces import HashBackend <EOL> from cryptography . hazmat . primitives import hashes <EOL> from cryptography . hazmat . primitives . kdf . concatkdf import ConcatKDFHMAC <EOL> from cryptography . hazmat . primitives . kdf . concatkdf import ConcatKDFHash <EOL> from ... utils import raises_unsupported_algorithm <EOL> @ pytest . mark . requires_backend_interface ( interface = HashBackend ) <EOL> class TestConcatKDFHash ( object ) : <EOL> def test_length_limit ( self , backend ) : <EOL> big_length = hashes . SHA256 ( ) . digest_size * ( <NUM_LIT:2> ** <NUM_LIT:32> - <NUM_LIT:1> ) + <NUM_LIT:1> <EOL> with pytest . raises ( ValueError ) : <EOL> ConcatKDFHash ( hashes . SHA256 ( ) , big_length , None , backend ) <EOL> def test_already_finalized ( self , backend ) : <EOL> ckdf = ConcatKDFHash ( hashes . SHA256 ( ) , <NUM_LIT:16> , None , backend ) <EOL> ckdf . derive ( b"<STR_LIT>" * <NUM_LIT:16> ) <EOL> with pytest . raises ( AlreadyFinalized ) : <EOL> ckdf . derive ( b"<STR_LIT>" * <NUM_LIT:16> ) <EOL> def test_derive ( self , backend ) : <EOL> prk = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> ) <EOL> okm = binascii . unhexlify ( b"<STR_LIT>" ) <EOL> oinfo = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> ckdf = ConcatKDFHash ( hashes . SHA256 ( ) , <NUM_LIT:16> , oinfo , backend ) <EOL> assert ckdf . derive ( prk ) == okm <EOL> def test_verify ( self , backend ) : <EOL> prk = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> ) <EOL> okm = binascii . unhexlify ( b"<STR_LIT>" ) <EOL> oinfo = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> ckdf = ConcatKDFHash ( hashes . SHA256 ( ) , <NUM_LIT:16> , oinfo , backend ) <EOL> assert ckdf . verify ( prk , okm ) is None <EOL> def test_invalid_verify ( self , backend ) : <EOL> prk = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> ) <EOL> oinfo = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> ckdf = ConcatKDFHash ( hashes . SHA256 ( ) , <NUM_LIT:16> , oinfo , backend ) <EOL> with pytest . raises ( InvalidKey ) : <EOL> ckdf . verify ( prk , b"<STR_LIT>" ) <EOL> def test_unicode_typeerror ( self , backend ) : <EOL> with pytest . raises ( TypeError ) : <EOL> ConcatKDFHash ( <EOL> hashes . SHA256 ( ) , <EOL> <NUM_LIT:16> , <EOL> otherinfo = u"<STR_LIT:foo>" , <EOL> backend = backend <EOL> ) <EOL> with pytest . raises ( TypeError ) : <EOL> ckdf = ConcatKDFHash ( <EOL> hashes . SHA256 ( ) , <EOL> <NUM_LIT:16> , <EOL> otherinfo = None , <EOL> backend = backend <EOL> ) <EOL> ckdf . derive ( u"<STR_LIT:foo>" ) <EOL> with pytest . raises ( TypeError ) : <EOL> ckdf = ConcatKDFHash ( <EOL> hashes . SHA256 ( ) , <EOL> <NUM_LIT:16> , <EOL> otherinfo = None , <EOL> backend = backend <EOL> ) <EOL> ckdf . verify ( u"<STR_LIT:foo>" , b"<STR_LIT:bar>" ) <EOL> with pytest . raises ( TypeError ) : <EOL> ckdf = ConcatKDFHash ( <EOL> hashes . SHA256 ( ) , <EOL> <NUM_LIT:16> , <EOL> otherinfo = None , <EOL> backend = backend <EOL> ) <EOL> ckdf . verify ( b"<STR_LIT:foo>" , u"<STR_LIT:bar>" ) <EOL> @ pytest . mark . requires_backend_interface ( interface = HMACBackend ) <EOL> class TestConcatKDFHMAC ( object ) : <EOL> def test_length_limit ( self , backend ) : <EOL> big_length = hashes . SHA256 ( ) . digest_size * ( <NUM_LIT:2> ** <NUM_LIT:32> - <NUM_LIT:1> ) + <NUM_LIT:1> <EOL> with pytest . raises ( ValueError ) : <EOL> ConcatKDFHMAC ( hashes . SHA256 ( ) , big_length , None , None , backend ) <EOL> def test_already_finalized ( self , backend ) : <EOL> ckdf = ConcatKDFHMAC ( hashes . SHA256 ( ) , <NUM_LIT:16> , None , None , backend ) <EOL> ckdf . derive ( b"<STR_LIT>" * <NUM_LIT:16> ) <EOL> with pytest . raises ( AlreadyFinalized ) : <EOL> ckdf . derive ( b"<STR_LIT>" * <NUM_LIT:16> ) <EOL> def test_derive ( self , backend ) : <EOL> prk = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> okm = binascii . unhexlify ( b"<STR_LIT>" <EOL> b"<STR_LIT>" ) <EOL> oinfo = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> ckdf = ConcatKDFHMAC ( hashes . SHA512 ( ) , <NUM_LIT:32> , None , oinfo , backend ) <EOL> assert ckdf . derive ( prk ) == okm <EOL> def test_derive_explicit_salt ( self , backend ) : <EOL> prk = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> okm = binascii . unhexlify ( b"<STR_LIT>" <EOL> b"<STR_LIT>" ) <EOL> oinfo = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> ckdf = ConcatKDFHMAC ( <EOL> hashes . SHA512 ( ) , <NUM_LIT:32> , b"<STR_LIT:\x00>" * <NUM_LIT> , oinfo , backend <EOL> ) <EOL> assert ckdf . derive ( prk ) == okm <EOL> def test_verify ( self , backend ) : <EOL> prk = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> okm = binascii . unhexlify ( b"<STR_LIT>" <EOL> b"<STR_LIT>" ) <EOL> oinfo = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> ckdf = ConcatKDFHMAC ( hashes . SHA512 ( ) , <NUM_LIT:32> , None , oinfo , backend ) <EOL> assert ckdf . verify ( prk , okm ) is None <EOL> def test_invalid_verify ( self , backend ) : <EOL> prk = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> oinfo = binascii . unhexlify ( <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> ckdf = ConcatKDFHMAC ( hashes . SHA512 ( ) , <NUM_LIT:32> , None , oinfo , backend ) <EOL> with pytest . raises ( InvalidKey ) : <EOL> ckdf . verify ( prk , b"<STR_LIT>" ) <EOL> def test_unicode_typeerror ( self , backend ) : <EOL> with pytest . raises ( TypeError ) : <EOL> ConcatKDFHMAC ( <EOL> hashes . SHA256 ( ) , <EOL> <NUM_LIT:16> , salt = u"<STR_LIT:foo>" , <EOL> otherinfo = None , <EOL> backend = backend <EOL> ) <EOL> with pytest . raises ( TypeError ) : <EOL> ConcatKDFHMAC ( <EOL> hashes . SHA256 ( ) , <EOL> <NUM_LIT:16> , salt = None , <EOL> otherinfo = u"<STR_LIT:foo>" , <EOL> backend = backend <EOL> ) <EOL> with pytest . raises ( TypeError ) : <EOL> ckdf = ConcatKDFHMAC ( <EOL> hashes . SHA256 ( ) , <EOL> <NUM_LIT:16> , salt = None , <EOL> otherinfo = None , <EOL> backend = backend <EOL> ) <EOL> ckdf . derive ( u"<STR_LIT:foo>" ) <EOL> with pytest . raises ( TypeError ) : <EOL> ckdf = ConcatKDFHMAC ( <EOL> hashes . SHA256 ( ) , <EOL> <NUM_LIT:16> , salt = None , <EOL> otherinfo = None , <EOL> backend = backend <EOL> ) <EOL> ckdf . verify ( u"<STR_LIT:foo>" , b"<STR_LIT:bar>" ) <EOL> with pytest . raises ( TypeError ) : <EOL> ckdf = ConcatKDFHMAC ( <EOL> hashes . SHA256 ( ) , <EOL> <NUM_LIT:16> , salt = None , <EOL> otherinfo = None , <EOL> backend = backend <EOL> ) <EOL> ckdf . verify ( b"<STR_LIT:foo>" , u"<STR_LIT:bar>" ) <EOL> def test_invalid_backend ( ) : <EOL> pretend_backend = object ( ) <EOL> with raises_unsupported_algorithm ( _Reasons . BACKEND_MISSING_INTERFACE ) : <EOL> ConcatKDFHash ( hashes . SHA256 ( ) , <NUM_LIT:16> , None , pretend_backend ) <EOL> with raises_unsupported_algorithm ( _Reasons . BACKEND_MISSING_INTERFACE ) : <EOL> ConcatKDFHMAC ( hashes . SHA256 ( ) , <NUM_LIT:16> , None , None , pretend_backend ) </s>
