From 6faad239766accdcc007540f0b8b38d4709cd435 Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Wed, 4 Jul 2012 17:26:29 +0200 Subject: crypto: Allow aes_cfb_128_{en|de}crypt to accept unaligned data Remove the need for padding up to 16-byte multiple. --- lib/crypto/c_src/crypto.c | 3 +-- lib/crypto/doc/src/crypto.xml | 42 +++++++++++++++++++++++++++++++++------- lib/crypto/test/crypto_SUITE.erl | 17 ++++++++++++---- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/lib/crypto/c_src/crypto.c b/lib/crypto/c_src/crypto.c index 4be593e208..a6a81d6fe2 100644 --- a/lib/crypto/c_src/crypto.c +++ b/lib/crypto/c_src/crypto.c @@ -954,8 +954,7 @@ static ERL_NIF_TERM aes_cfb_128_crypt(ErlNifEnv* env, int argc, const ERL_NIF_TE if (!enif_inspect_iolist_as_binary(env, argv[0], &key) || key.size != 16 || !enif_inspect_binary(env, argv[1], &ivec) || ivec.size != 16 - || !enif_inspect_iolist_as_binary(env, argv[2], &text) - || text.size % 16 != 0) { + || !enif_inspect_iolist_as_binary(env, argv[2], &text)) { return enif_make_badarg(env); } diff --git a/lib/crypto/doc/src/crypto.xml b/lib/crypto/doc/src/crypto.xml index 19db6c9dd4..2868fe05f0 100644 --- a/lib/crypto/doc/src/crypto.xml +++ b/lib/crypto/doc/src/crypto.xml @@ -643,16 +643,14 @@ Mpint() = >]]> aes_cfb_128_encrypt(Key, IVec, Text) -> Cipher - aes_cbc_128_encrypt(Key, IVec, Text) -> Cipher - Encrypt Textaccording to AES in Cipher Feedback mode or Cipher Block Chaining mode + Encrypt Textaccording to AES in Cipher Feedback mode Key = Text = iolist() | binary() IVec = Cipher = binary()

Encrypts Text according to AES in Cipher Feedback - mode (CFB) or Cipher Block Chaining mode (CBC). Text - must be a multiple of 128 bits (16 bytes). Key is the + mode (CFB). Key is the AES key, and IVec is an arbitrary initializing vector. The lengths of Key and IVec must be 128 bits (16 bytes).

@@ -660,15 +658,45 @@ Mpint() = >]]>
aes_cfb_128_decrypt(Key, IVec, Cipher) -> Text + Decrypt Cipheraccording to AES in Cipher Feedback mode + + Key = Cipher = iolist() | binary() + IVec = Text = binary() + + +

Decrypts Cipher according to AES in Cipher Feedback Mode (CFB). + Key is the AES key, and IVec is an arbitrary + initializing vector. Key and IVec must have + the same values as those used when encrypting. The lengths of + Key and IVec must be 128 bits (16 bytes).

+
+
+ + aes_cbc_128_encrypt(Key, IVec, Text) -> Cipher + Encrypt Textaccording to AES in Cipher Block Chaining mode + + Key = Text = iolist() | binary() + IVec = Cipher = binary() + + +

Encrypts Text according to AES in Cipher Block Chaining + mode (CBC). Text + must be a multiple of 128 bits (16 bytes). Key is the + AES key, and IVec is an arbitrary initializing vector. + The lengths of Key and IVec must be 128 bits + (16 bytes).

+
+
+ aes_cbc_128_decrypt(Key, IVec, Cipher) -> Text - Decrypt Cipheraccording to AES in Cipher Feedback mode or Cipher Block Chaining mode + Decrypt Cipheraccording to AES in Cipher Block Chaining mode Key = Cipher = iolist() | binary() IVec = Text = binary() -

Decrypts Cipher according to Cipher Feedback Mode (CFB) - or Cipher Block Chaining mode (CBC). +

Decrypts Cipher according to AES in Cipher Block + Chaining mode (CBC). Key is the AES key, and IVec is an arbitrary initializing vector. Key and IVec must have the same values as those used when encrypting. Cipher diff --git a/lib/crypto/test/crypto_SUITE.erl b/lib/crypto/test/crypto_SUITE.erl index 627c966dfb..196f00da5d 100644 --- a/lib/crypto/test/crypto_SUITE.erl +++ b/lib/crypto/test/crypto_SUITE.erl @@ -717,10 +717,19 @@ aes_cfb(Config) when is_list(Config) -> ?line Key = hexstr2bin("2b7e151628aed2a6abf7158809cf4f3c"), ?line IVec = hexstr2bin("000102030405060708090a0b0c0d0e0f"), ?line Plain = hexstr2bin("6bc1bee22e409f96e93d7e117393172a"), - ?line Cipher = crypto:aes_cfb_128_encrypt(Key, IVec, Plain), - ?line m(Cipher, hexstr2bin("3b3fd92eb72dad20333449f8e83cfb4a")), - ?line m(Plain, - crypto:aes_cfb_128_decrypt(Key, IVec, Cipher)). + ?line Cipher = hexstr2bin("3b3fd92eb72dad20333449f8e83cfb4a"), + + %% Try all prefixes of plain and cipher. + aes_cfb_do(byte_size(Plain), Plain, Cipher, Key, IVec). + +aes_cfb_do(N, Plain, Cipher, Key, IVec) when N >= 0 -> + <> = Plain, + <> = Cipher, + ?line C = crypto:aes_cfb_128_encrypt(Key, IVec, P), + ?line P = crypto:aes_cfb_128_decrypt(Key, IVec, C), + aes_cfb_do(N-1, Plain, Cipher, Key, IVec); +aes_cfb_do(_, _, _, _, _) -> ok. + %% %% -- cgit v1.2.3