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