OOP API wrapper class for BufferedBlockCipher
test if T is a block cipher
test buffered AES/CTR encryption test vectors: http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
1 t { 2 import dcrypt.blockcipher.aes; 3 import dcrypt.blockcipher.modes.ctr; 4 import dcrypt.blockcipher.modes.cbc; 5 import std.range; 6 import std.conv: text; 7 8 BufferedBlockCipher!(CTR!AES) cipher; 9 10 const ubyte[] key = cast(const ubyte[])x"2b7e151628aed2a6abf7158809cf4f3c"; 11 const ubyte[] iv = cast(const ubyte[])x"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"; 12 13 const ubyte[] plain = cast(const ubyte[])x" 14 6bc1bee22e409f96e93d7e117393172a 15 ae2d8a571e03ac9c9eb76fac45af8e51 16 30c81c46a35ce411e5fbc1191a0a52ef 17 f69f2445df4f9b17ad2b417be66c3710 18 "; 19 20 const ubyte[] expected_ciphertext = cast(const ubyte[])x" 21 874d6191b620e3261bef6864990db6ce 22 9806f66b7970fdff8617187bb9fffdff 23 5ae4df3edbd5d35e5b4f09020db03eab 24 1e031dda2fbe03d1792170a0f3009cee 25 "; 26 27 28 // encryption mode 29 cipher.start(true, key, iv); 30 31 ubyte[plain.length] buf; 32 33 size_t len; 34 len = cipher.processBytes(plain, buf); 35 len += cipher.doFinal(buf[len..$]); 36 assert(len == plain.length); 37 38 assert(buf == expected_ciphertext, text(cipher.name,": encryption failed")); 39 40 // decryption mode 41 cipher.start(false, key, iv); 42 43 len = cipher.processBytes(buf, buf); 44 len += cipher.doFinal(buf[len..$]); 45 assert(len == plain.length); 46 47 assert(buf == plain, text(cipher.name,": decryption failed"));
test buffered AES/CTR encryption with incomplete last block test vectors: http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
1 t { 2 import dcrypt.blockcipher.aes; 3 import dcrypt.blockcipher.modes.ctr; 4 import dcrypt.blockcipher.modes.cbc; 5 import std.range; 6 import std.conv: text; 7 8 BufferedBlockCipher!(CTR!AES, true) cipher; // true: allow partial block 9 10 const ubyte[] key = cast(const ubyte[])x"2b7e151628aed2a6abf7158809cf4f3c"; 11 const ubyte[] iv = cast(const ubyte[])x"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"; 12 13 const ubyte[] plain = cast(const ubyte[])x" 14 6bc1bee22e409f96e93d7e117393172a 15 ae2d8a571e03ac9c9eb76fac45af8e51 16 30c81c46a35ce411e5fbc1191a0a52ef 17 f69f2445df4f9b17 18 "; 19 20 const ubyte[] expected_ciphertext = cast(const ubyte[])x" 21 874d6191b620e3261bef6864990db6ce 22 9806f66b7970fdff8617187bb9fffdff 23 5ae4df3edbd5d35e5b4f09020db03eab 24 1e031dda2fbe03d1 25 "; 26 27 28 // encryption mode 29 cipher.start(true, key, iv); 30 31 ubyte[plain.length] buf; 32 33 size_t len; 34 len = cipher.processBytes(plain, buf); 35 len += cipher.doFinal(buf[len..$]); 36 assert(len == plain.length); 37 38 assert(buf == expected_ciphertext, text(cipher.name,": encryption failed")); 39 40 // decryption mode 41 cipher.start(false, key, iv); 42 43 len = cipher.processBytes(buf, buf); 44 len += cipher.doFinal(buf[len..$]); 45 assert(len == plain.length); 46 47 assert(buf == plain, text(cipher.name,": decryption failed"));