From 5214220fa27086ebd8f5e60949fb08532e29154e Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 15 Feb 2014 23:02:19 -0600 Subject: add CAST5 CTR vectors (built from AES CTR vectors) --- .../primitives/vectors/ciphers/CAST5/cast5-ctr.txt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/hazmat/primitives/vectors/ciphers/CAST5/cast5-ctr.txt diff --git a/tests/hazmat/primitives/vectors/ciphers/CAST5/cast5-ctr.txt b/tests/hazmat/primitives/vectors/ciphers/CAST5/cast5-ctr.txt new file mode 100644 index 00000000..1e345403 --- /dev/null +++ b/tests/hazmat/primitives/vectors/ciphers/CAST5/cast5-ctr.txt @@ -0,0 +1,18 @@ + +COUNT = 0 +KEY = AE6852F8121067CC4BF7A5765577F39E +IV = 0000003000000000 +PLAINTEXT = 53696E676C6520626C6F636B206D7367 +CIPHERTEXT = 1f5483baa07a68abccfed68179e84ac0 + +COUNT = 1 +KEY = 7E24067817FAE0D743D6CE1F32539163 +IV = 006CB6DBC0543B59 +PLAINTEXT = 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F +CIPHERTEXT = 07c52bc5f858c5b1a21ee4e27f7b1ac90c17087d1e6ae0fd81c72e248cce329b + +COUNT = 2 +KEY = 7691BE035E5020A8AC6E618529F9A0DC +IV = 00E0017B27777F3F +PLAINTEXT = 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20212223 +CIPHERTEXT = 612aec9151e753cc1f8b086ece5343737697cda52122e1d9d2294cb43e7547bb81b50939 \ No newline at end of file -- cgit v1.2.3 From 432b4cb67e11b5e9ea9fb22b8a39443e11ab7bc1 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 15 Feb 2014 23:02:47 -0600 Subject: update generation script and verification script to support CTR gen --- .../custom-vectors/cast5/generate_cast5.py | 6 ++++-- .../custom-vectors/cast5/verify_cast5.go | 23 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/development/custom-vectors/cast5/generate_cast5.py b/docs/development/custom-vectors/cast5/generate_cast5.py index c3f579e7..32ef3b43 100644 --- a/docs/development/custom-vectors/cast5/generate_cast5.py +++ b/docs/development/custom-vectors/cast5/generate_cast5.py @@ -1,6 +1,6 @@ import binascii -from cryptography.hazmat.backends.openssl.backend import backend +from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.ciphers import base, algorithms, modes @@ -8,7 +8,7 @@ def encrypt(mode, key, iv, plaintext): cipher = base.Cipher( algorithms.CAST5(binascii.unhexlify(key)), mode(binascii.unhexlify(iv)), - backend + default_backend() ) encryptor = cipher.encryptor() ct = encryptor.update(binascii.unhexlify(plaintext)) @@ -57,3 +57,5 @@ ofb_path = "tests/hazmat/primitives/vectors/ciphers/AES/OFB/OFBMMT128.rsp" write_file(build_vectors(modes.OFB, ofb_path), "cast5-ofb.txt") cfb_path = "tests/hazmat/primitives/vectors/ciphers/AES/CFB/CFB128MMT128.rsp" write_file(build_vectors(modes.CFB, cfb_path), "cast5-cfb.txt") +ctr_path = "tests/hazmat/primitives/vectors/ciphers/AES/CTR/aes-128-ctr.txt" +write_file(build_vectors(modes.CTR, ctr_path), "cast5-ctr.txt") diff --git a/docs/development/custom-vectors/cast5/verify_cast5.go b/docs/development/custom-vectors/cast5/verify_cast5.go index 49e1023d..f735d989 100644 --- a/docs/development/custom-vectors/cast5/verify_cast5.go +++ b/docs/development/custom-vectors/cast5/verify_cast5.go @@ -91,6 +91,26 @@ func (o cfbVerifier) validate(count string, key, iv, plaintext, expected_ciphert } } +type ctrVerifier struct{} + +func (o ctrVerifier) validate(count string, key, iv, plaintext, expected_ciphertext []byte) { + block, err := cast5.NewCipher(key) + if err != nil { + panic(err) + } + + ciphertext := make([]byte, len(plaintext)) + stream := cipher.NewCTR(block, iv) + stream.XORKeyStream(ciphertext, plaintext) + + if !bytes.Equal(ciphertext, expected_ciphertext) { + panic(fmt.Errorf("vector mismatch @ COUNT = %s:\n %s != %s\n", + count, + hex.EncodeToString(expected_ciphertext), + hex.EncodeToString(ciphertext))) + } +} + func validateVectors(verifier VectorVerifier, filename string) { vectors, err := os.Open(filename) if err != nil { @@ -138,4 +158,7 @@ func main() { validateVectors(cbcVerifier{}, "tests/hazmat/primitives/vectors/ciphers/CAST5/cast5-cbc.txt") fmt.Println("CBC OK.") + validateVectors(ctrVerifier{}, + "tests/hazmat/primitives/vectors/ciphers/CAST5/cast5-ctr.txt") + fmt.Println("CTR OK.") } -- cgit v1.2.3 From aee319fc9a384e3b75dad304ecebf5ca6aad7730 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 15 Feb 2014 23:03:51 -0600 Subject: update docs to explain CTR generation source --- docs/development/custom-vectors/cast5.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/development/custom-vectors/cast5.rst b/docs/development/custom-vectors/cast5.rst index 09b3bdb1..5d448dfa 100644 --- a/docs/development/custom-vectors/cast5.rst +++ b/docs/development/custom-vectors/cast5.rst @@ -1,10 +1,11 @@ CAST5 Vector Creation ===================== -This page documents the code that was used to generate the CAST5 CBC, CFB, and -OFB test vectors as well as the code used to verify them against another -implementation. For CAST5 the vectors were generated using OpenSSL and verified -with Go. +This page documents the code that was used to generate the CAST5 CBC, CFB, OFB, +and CTR test vectors as well as the code used to verify them against another +implementation. For CAST5 the CBC, CFB, and OFB vectors were generated using +OpenSSL and the CTR vectors were generated using Apple's CommonCrypto. All of +the modes were verified with Go. Creation -------- -- cgit v1.2.3 From 9104dd2b2c5cd717dc7c0459d911263a8b6f7058 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 15 Feb 2014 23:06:57 -0600 Subject: clearer prose --- docs/development/custom-vectors/cast5.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/development/custom-vectors/cast5.rst b/docs/development/custom-vectors/cast5.rst index 5d448dfa..f5400270 100644 --- a/docs/development/custom-vectors/cast5.rst +++ b/docs/development/custom-vectors/cast5.rst @@ -4,8 +4,8 @@ CAST5 Vector Creation This page documents the code that was used to generate the CAST5 CBC, CFB, OFB, and CTR test vectors as well as the code used to verify them against another implementation. For CAST5 the CBC, CFB, and OFB vectors were generated using -OpenSSL and the CTR vectors were generated using Apple's CommonCrypto. All of -the modes were verified with Go. +OpenSSL and the CTR vectors were generated using Apple's CommonCrypto. All the +generated vectors were verified with Go. Creation -------- -- cgit v1.2.3 From e1dbd3a3ca82d5f6aa3c5da6f7e29d0d487a9c61 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 16 Feb 2014 21:37:22 -0600 Subject: add origin header to cast5-ctr test vectors --- tests/hazmat/primitives/vectors/ciphers/CAST5/cast5-ctr.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/hazmat/primitives/vectors/ciphers/CAST5/cast5-ctr.txt b/tests/hazmat/primitives/vectors/ciphers/CAST5/cast5-ctr.txt index 1e345403..2bcdb4d6 100644 --- a/tests/hazmat/primitives/vectors/ciphers/CAST5/cast5-ctr.txt +++ b/tests/hazmat/primitives/vectors/ciphers/CAST5/cast5-ctr.txt @@ -1,3 +1,7 @@ +# CAST5 CTR vectors built for https://github.com/pyca/cryptography +# Derived from the RFC 3686 test vectors for AES CTR +# Verified against the Go crypto packages +# Key Length : 128 COUNT = 0 KEY = AE6852F8121067CC4BF7A5765577F39E @@ -15,4 +19,4 @@ COUNT = 2 KEY = 7691BE035E5020A8AC6E618529F9A0DC IV = 00E0017B27777F3F PLAINTEXT = 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20212223 -CIPHERTEXT = 612aec9151e753cc1f8b086ece5343737697cda52122e1d9d2294cb43e7547bb81b50939 \ No newline at end of file +CIPHERTEXT = 612aec9151e753cc1f8b086ece5343737697cda52122e1d9d2294cb43e7547bb81b50939 -- cgit v1.2.3