forked from chrysn/aiocoap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_crypto.py
36 lines (27 loc) · 1.5 KB
/
test_crypto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# This file is part of the Python aiocoap library project.
#
# Copyright (c) 2012-2014 Maciej Wasilak <http://sixpinetrees.blogspot.com/>,
# 2013-2014 Christian Amsüss <[email protected]>
#
# aiocoap is free software, this file is published under the MIT license as
# described in the accompanying LICENSE file.
"""Tests for the util.crypto module"""
import unittest
import aiocoap.util.crypto
class TestUtilCrypto(unittest.TestCase):
def test_reproduced_output(self):
"""Reproducibility of encryption results, decryption and detecting
tampered AAD
These values were not verified by themselves with another
implementation, but the function that produced them produced valid key
material in OSCOAP."""
key = b"0123456789----------0123456789--"
iv = b"1234567"
message = b"Hello Bob, this is Alice."
aad = b"The envelope said that this is from Alice to Bob."
ciphertext, tag = aiocoap.util.crypto.encrypt_ccm(message, aad, key, iv, 14)
self.assertEqual(ciphertext, b'g^\xc9X\xednx\xd5\xc6\xab\x8c\x85\xaa\xed\\f\xee\xebk\xbc]SCZ\x85')
self.assertEqual(tag, b'P\xce0{K\xaf*\xf9\xb1\xb4\xbc\x1c\x84_')
decrypted = aiocoap.util.crypto.decrypt_ccm(ciphertext, aad, tag, key, iv)
self.assertEqual(decrypted, message)
self.assertRaises(aiocoap.util.crypto.InvalidAEAD, aiocoap.util.crypto.decrypt_ccm, ciphertext, b"The envelope said this is from Michelle to Bob.", tag, key, iv)