test_rfc8103.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley
  5. # Copyright (c) 2019, Vigil Security, LLC
  6. # License: http://snmplabs.com/pyasn1/license.html
  7. #
  8. import sys
  9. import unittest
  10. from pyasn1.codec.der import decoder as der_decoder
  11. from pyasn1.codec.der import encoder as der_encoder
  12. from pyasn1_modules import pem
  13. from pyasn1_modules import rfc5280
  14. from pyasn1_modules import rfc8103
  15. class CAEADChaCha20Poly1305TestCase(unittest.TestCase):
  16. alg_id_pem_text = "MBsGCyqGSIb3DQEJEAMSBAzK/rq++s7brd7K+Ig="
  17. def setUp(self):
  18. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  19. def testDerCodec(self):
  20. substrate = pem.readBase64fromText(self.alg_id_pem_text)
  21. asn1Object, rest = der_decoder.decode(
  22. substrate, asn1Spec=self.asn1Spec)
  23. self.assertFalse(rest)
  24. self.assertTrue(asn1Object.prettyPrint())
  25. self.assertEqual(rfc8103.id_alg_AEADChaCha20Poly1305, asn1Object[0])
  26. param, rest = der_decoder.decode(
  27. asn1Object[1], rfc8103.AEADChaCha20Poly1305Nonce())
  28. self.assertFalse(rest)
  29. self.assertTrue(param.prettyPrint())
  30. self.assertEqual(
  31. rfc8103.AEADChaCha20Poly1305Nonce(value='\xca\xfe\xba\xbe\xfa'
  32. '\xce\xdb\xad\xde\xca'
  33. '\xf8\x88'),
  34. param)
  35. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  36. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  37. if __name__ == '__main__':
  38. result = unittest.TextTestRunner(verbosity=2).run(suite)
  39. sys.exit(not result.wasSuccessful())