#NEWLINE# def aesCCM( key , key_handle , nonce , data , decrypt = False ) : #NEWLINE# #INDENT# '\n    Function implementing YubiHSM AEAD encrypt/decrypt in software.\n    ' #NEWLINE# if decrypt : #NEWLINE# #INDENT# ( data , saved_mac ) = _split_data ( data , ( len ( data ) - pyhsm . defines . YSM_AEAD_MAC_SIZE ) ) #NEWLINE# #UNINDENT# nonce = pyhsm . util . input_validate_nonce ( out , pad = True ) #NEWLINE# mac = _cbc_mac ( key , key_handle , nonce , len ( data ) ) #NEWLINE# counter = _ctr_counter ( key_handle , nonce , value = 0 ) #NEWLINE# ctr_aes = AES . new ( key , AES . MODE_CTR , counter = counter . next ) #NEWLINE# out = [ ] #NEWLINE# while data : #NEWLINE# #INDENT# ( thisblock , data ) = _split_data ( data , pyhsm . defines . YSM_BLOCK_SIZE ) #NEWLINE# if decrypt : #NEWLINE# #INDENT# aes_out = ctr_aes . decrypt ( thisblock ) #NEWLINE# mac . update ( aes_out ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# mac . update ( thisblock ) #NEWLINE# aes_out = ctr_aes . encrypt ( thisblock ) #NEWLINE# #UNINDENT# out . append ( aes_out ) #NEWLINE# #UNINDENT# counter . value = 0 #NEWLINE# mac . finalize ( counter . pack ( ) ) #NEWLINE# if decrypt : #NEWLINE# #INDENT# if ( mac . get ( ) != saved_mac ) : #NEWLINE# #INDENT# raise pyhsm . exception . YHSM_Error ( 'AEAD integrity check failed' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# out . append ( mac . get ( ) ) #NEWLINE# #UNINDENT# return '' . join ( out )