aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2013-03-14 19:07:38 +0000
committerlloyd <[email protected]>2013-03-14 19:07:38 +0000
commit3d4daa8f11c1ba664a2854d0c372bef4cbd54e19 (patch)
treee6f4049b8d1bf958c752c05ae061eb634e0af738
parent261a665b1be34f07d5012eed5f6090fd85dd8479 (diff)
OCB decryption
-rw-r--r--checks/bench.cpp7
-rw-r--r--checks/ocb.cpp124
-rw-r--r--checks/validate.cpp3
-rw-r--r--checks/validate.dat17
-rw-r--r--src/engine/core_engine/core_modes.cpp7
-rw-r--r--src/filters/modes/ocb/ocb.cpp113
-rw-r--r--src/filters/modes/ocb/ocb.h51
7 files changed, 246 insertions, 76 deletions
diff --git a/checks/bench.cpp b/checks/bench.cpp
index 4e6f15fb6..f7ddc5650 100644
--- a/checks/bench.cpp
+++ b/checks/bench.cpp
@@ -203,10 +203,13 @@ bool bench_algo(const std::string& algo,
}
size_t cipher_keylen = proto_cipher->maximum_keylength();
- const size_t cipher_ivlen = proto_cipher->block_size();
+ size_t cipher_ivlen = proto_cipher->block_size();
+ // hacks!
if(algo_parts[1] == "XTS")
- cipher_keylen *= 2; // hack!
+ cipher_keylen *= 2;
+ if(algo_parts[1] == "OCB")
+ cipher_ivlen -= 1;
std::vector<byte> buf(16 * 1024);
rng.randomize(&buf[0], buf.size());
diff --git a/checks/ocb.cpp b/checks/ocb.cpp
index b45b6d2b8..78be74bef 100644
--- a/checks/ocb.cpp
+++ b/checks/ocb.cpp
@@ -35,9 +35,6 @@ std::vector<byte> ocb_decrypt(const SymmetricKey& key,
const byte pt[], size_t pt_len,
const byte ad[], size_t ad_len)
{
- throw std::runtime_error("Not implemented");
-
-#if 0
OCB_Decryption* ocb = new OCB_Decryption(new AES_128);
ocb->set_key(key);
@@ -46,8 +43,7 @@ std::vector<byte> ocb_decrypt(const SymmetricKey& key,
Pipe pipe(ocb);
pipe.process_msg(pt, pt_len);
- return pipe.read_all_unlocked();
-#endif
+ return unlock(pipe.read_all());
}
template<typename Alloc, typename Alloc2>
@@ -59,6 +55,82 @@ std::vector<byte> ocb_encrypt(const SymmetricKey& key,
return ocb_encrypt(key, nonce, &pt[0], pt.size(), &ad[0], ad.size());
}
+template<typename Alloc, typename Alloc2>
+std::vector<byte> ocb_decrypt(const SymmetricKey& key,
+ const std::vector<byte>& nonce,
+ const std::vector<byte, Alloc>& pt,
+ const std::vector<byte, Alloc2>& ad)
+ {
+ return ocb_decrypt(key, nonce, &pt[0], pt.size(), &ad[0], ad.size());
+ }
+
+std::vector<byte> ocb_encrypt(OCB_Encryption& ocb,
+ Pipe& pipe,
+ const std::vector<byte>& nonce,
+ const std::vector<byte>& pt,
+ const std::vector<byte>& ad)
+ {
+ ocb.set_nonce(&nonce[0], nonce.size());
+ ocb.set_associated_data(&ad[0], ad.size());
+
+ pipe.process_msg(pt);
+ return unlock(pipe.read_all(Pipe::LAST_MESSAGE));
+ }
+
+void test_ocb_long_filters()
+ {
+ SymmetricKey key("00000000000000000000000000000000");
+
+ OCB_Encryption* ocb = new OCB_Encryption(new AES_128);
+
+ ocb->set_key(key);
+ Pipe pipe(ocb);
+
+ const std::vector<byte> empty;
+ std::vector<byte> N(12);
+ std::vector<byte> C;
+
+ for(size_t i = 0; i != 128; ++i)
+ {
+ const std::vector<byte> S(i);
+ N[11] = i;
+
+ const std::vector<byte> C1 = ocb_encrypt(*ocb, pipe, N, S, S);
+ const std::vector<byte> C2 = ocb_encrypt(*ocb, pipe, N, S, empty);
+ const std::vector<byte> C3 = ocb_encrypt(*ocb, pipe, N, empty, S);
+
+ //std::cout << "C_" << i << " = " << hex_encode(C1) << " " << hex_encode(C2) << " " << hex_encode(C3) << "\n";
+
+ C += C1;
+ C += C2;
+ C += C3;
+ }
+
+ SHA_256 sha256;
+ sha256.update(C);
+ const std::string C_hash = hex_encode(sha256.final());
+ const std::string expected_C_hash = "C4E5158067F49356042296B13B050DE00A120EA846073E5E0DACFD0C9F43CC65";
+
+ if(C_hash != expected_C_hash)
+ {
+ std::cout << "OCB-128 long test, C hashes differ\n";
+ std::cout << C_hash << " !=\n" << expected_C_hash << "\n";
+ }
+
+ //std::cout << "SHA-256(C) = " << C_hash << "\n";
+
+ N[11] = 0;
+ const std::vector<byte> cipher = ocb_encrypt(*ocb, pipe, N, empty, C);
+
+ const std::string expected = "B2B41CBF9B05037DA7F16C24A35C1C94";
+
+ const std::string cipher_hex = hex_encode(cipher);
+
+ if(cipher_hex != expected)
+ std::cout << "OCB AES-128 long test mistmatch " << cipher_hex << " != " << expected << "\n";
+ else
+ std::cout << "OCB AES-128 long test OK\n";
+ }
void test_ocb_long()
{
@@ -114,6 +186,29 @@ void test_ocb_long()
std::cout << "OCB AES-128 long test mistmatch " << cipher_hex << " != " << expected << "\n";
else
std::cout << "OCB AES-128 long test OK\n";
+
+ try
+ {
+ const std::vector<byte> p = ocb_decrypt(key, N, cipher, C);
+
+ BOTAN_ASSERT(p.empty(), "return plaintext is empty");
+ }
+ catch(std::exception& e)
+ {
+ std::cout << "Error in OCB decrypt - " << e.what() << "\n";
+ }
+
+ try
+ {
+ C[0] ^= 1;
+ ocb_decrypt(key, N, cipher, C);
+ std::cout << "OCB failed to reject bad message\n";
+ }
+ catch(std::exception& e)
+ {
+ }
+
+
}
void test_ocb()
@@ -129,20 +224,27 @@ void test_ocb()
std::vector<byte> ctext = ocb_encrypt(key, nonce, pt, ad);
- std::string ctext_hex = hex_encode(ctext);
+ const std::string ctext_hex = hex_encode(ctext);
if(ctext_hex != expected)
std::cout << "OCB/AES-128 encrypt test failure\n" << ctext_hex << " !=\n" << expected << "\n";
else
std::cout << "OCB/AES-128 encrypt OK\n";
-#if 0
- std::vector<byte> dec = ocb_decrypt(key, nonce, ctext, ad);
+ try
+ {
+ std::vector<byte> dec = ocb_decrypt(key, nonce, ctext, ad);
- std::cout << hex_encode(dec) << "\n";
-#endif
+ if(dec == pt) { std::cout << "OCB decrypts OK\n"; }
+ else { std::cout << "OCB fails to decrypt\n"; }
+ }
+ catch(std::exception& e)
+ {
+ std::cout << "Correct OCB message rejected - " << e.what() << "\n";
+ }
- test_ocb_long();
+ //test_ocb_long();
+ test_ocb_long_filters();
}
diff --git a/checks/validate.cpp b/checks/validate.cpp
index 55af920cd..2126f8a10 100644
--- a/checks/validate.cpp
+++ b/checks/validate.cpp
@@ -416,7 +416,8 @@ u32bit do_validation_tests(const std::string& filename,
errors++;
}
- test_ocb();
+ if(should_pass)
+ test_ocb();
return errors;
}
diff --git a/checks/validate.dat b/checks/validate.dat
index f50b63547..2e748dfc7 100644
--- a/checks/validate.dat
+++ b/checks/validate.dat
@@ -25969,6 +25969,23 @@ F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF
603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A30914DFF4:\
F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF
+[AES-128/OCB]
+:197B9C3C441D3C83EAFB2BEF633B9182:\
+000102030405060708090A0B0C0D0E0F:000102030405060708090A0B
+
+0001020304050607:\
+92B657130A74B85A971EFFCAE19AD4716F88E87B871FBEED:\
+000102030405060708090A0B0C0D0E0F:000102030405060708090A0B
+
+000102030405060708090A0B0C0D0E0F:\
+BEA5E8798DBE7110031C144DA0B2612213CC8B747807121A4CBB3E4BD6B456AF:\
+000102030405060708090A0B0C0D0E0F:000102030405060708090A0B
+
+000102030405060708090A0B0C0D0E0F1011121314151617:\
+BEA5E8798DBE7110031C144DA0B26122FCFCEE7A\
+2A8D4D486EF2F52587FDA0ED97DC7EEDE241DF68:\
+000102030405060708090A0B0C0D0E0F:000102030405060708090A0B
+
[AES-128/EAX]
:32E55CE0C3FAEA48164B122C1BE22D85:\
C61A0851AB4E515D11525B92E2B9D850:C825FC7C4D539DC74887CECC70884F37
diff --git a/src/engine/core_engine/core_modes.cpp b/src/engine/core_engine/core_modes.cpp
index 039a60c78..08124a3a0 100644
--- a/src/engine/core_engine/core_modes.cpp
+++ b/src/engine/core_engine/core_modes.cpp
@@ -136,10 +136,8 @@ Keyed_Filter* get_cipher_mode(const BlockCipher* block_cipher,
{
if(direction == ENCRYPTION)
return new OCB_Encryption(block_cipher->clone(), 16);
- /*
else
return new OCB_Decryption(block_cipher->clone(), 16);
- */
}
#endif
@@ -237,7 +235,10 @@ Keyed_Filter* Core_Engine::get_cipher(const std::string& algo_spec,
if(filt)
return filt;
- throw Algorithm_Not_Found(cipher_name + "/" + mode + "/" + padding);
+ if(padding != "NoPadding")
+ throw Algorithm_Not_Found(cipher_name + "/" + mode + "/" + padding);
+ else
+ throw Algorithm_Not_Found(cipher_name + "/" + mode);
}
}
diff --git a/src/filters/modes/ocb/ocb.cpp b/src/filters/modes/ocb/ocb.cpp
index 05b1019b5..20cf8b37c 100644
--- a/src/filters/modes/ocb/ocb.cpp
+++ b/src/filters/modes/ocb/ocb.cpp
@@ -11,25 +11,13 @@
#include <botan/internal/bit_ops.h>
#include <algorithm>
+#include <botan/hex.h>
#include <iostream>
namespace Botan {
namespace ShouldNotBeHere {
-inline void xor_mem(byte out[], const byte in[], size_t length)
- {
- for(size_t i = 0; i != length; ++i)
- out[i] ^= in[i];
- }
-
-inline void xor_mem(byte out[], const byte in[], const byte in2[], size_t length)
- {
- for(size_t i = 0; i != length; ++i)
- out[i] = in[i] ^ in2[i];
- }
-
-
template<typename T, typename Alloc, typename Alloc2>
std::vector<T, Alloc>&
operator^=(std::vector<T, Alloc>& out,
@@ -38,7 +26,7 @@ operator^=(std::vector<T, Alloc>& out,
if(out.size() < in.size())
out.resize(in.size());
- xor_mem(&out[0], &in[0], in.size());
+ xor_buf(&out[0], &in[0], in.size());
return out;
}
@@ -109,7 +97,7 @@ secure_vector<byte> ocb_hash(const L_computer& L,
offset ^= L(ctz(i+1));
buf = offset;
- xor_mem(&buf[0], &ad[16*i], 16);
+ xor_buf(&buf[0], &ad[16*i], 16);
cipher.encrypt(buf);
@@ -121,7 +109,7 @@ secure_vector<byte> ocb_hash(const L_computer& L,
offset ^= L.star();
buf = offset;
- xor_mem(&buf[0], &ad[16*ad_blocks], ad_remainder);
+ xor_buf(&buf[0], &ad[16*ad_blocks], ad_remainder);
buf[ad_len % 16] ^= 0x80;
cipher.encrypt(buf);
@@ -134,14 +122,19 @@ secure_vector<byte> ocb_hash(const L_computer& L,
}
-OCB_Mode::OCB_Mode(BlockCipher* cipher, size_t tag_size) :
- Buffered_Filter(16, 0),
+OCB_Mode::OCB_Mode(BlockCipher* cipher, size_t tag_size, bool decrypting) :
+ Buffered_Filter(16, decrypting ? tag_size : 0),
m_cipher(cipher), m_tag_size(tag_size),
m_ad_hash(16), m_offset(16), m_checksum(16)
{
if(m_cipher->block_size() != 16)
throw std::invalid_argument("OCB requires a 128 bit cipher so cannot be used with " +
m_cipher->name());
+
+ if(m_tag_size != 16) // 64, 96 bits also supported
+ throw std::invalid_argument("OCB cannot produce a " + std::to_string(m_tag_size) +
+ " byte tag");
+
}
OCB_Mode::~OCB_Mode() { /* for unique_ptr destructor */ }
@@ -164,7 +157,7 @@ void OCB_Mode::set_key(const SymmetricKey& key)
void OCB_Mode::set_nonce(const byte nonce[], size_t nonce_len)
{
- if(nonce_len > 15) // OCB supports 127 bits, we support 120
+ if(!valid_iv_length(nonce_len))
throw Invalid_IV_Length(name(), nonce_len);
byte bottom;
@@ -236,12 +229,12 @@ void OCB_Encryption::buffered_block(const byte input[], size_t input_length)
{
// could run in parallel
- xor_mem(&m_checksum[0], &input[16*i], 16);
+ xor_buf(&m_checksum[0], &input[16*i], 16);
m_offset ^= L(ctz(++m_block_index));
ctext_buf = m_offset;
- xor_mem(&ctext_buf[0], &input[16*i], 16);
+ xor_buf(&ctext_buf[0], &input[16*i], 16);
m_cipher->encrypt(ctext_buf);
ctext_buf ^= m_offset;
@@ -261,14 +254,14 @@ void OCB_Encryption::buffered_final(const byte input[], size_t input_length)
{
BOTAN_ASSERT(input_length < 16, "Only a partial block left");
- xor_mem(&m_checksum[0], &input[0], input_length);
+ xor_buf(&m_checksum[0], &input[0], input_length);
m_checksum[input_length] ^= 0x80;
m_offset ^= m_L->star(); // Offset_*
secure_vector<byte> buf(16);
m_cipher->encrypt(m_offset, buf);
- xor_mem(&buf[0], &input[0], input_length);
+ xor_buf(&buf[0], &input[0], input_length);
send(buf, input_length); // final ciphertext
}
@@ -286,6 +279,80 @@ void OCB_Encryption::buffered_final(const byte input[], size_t input_length)
zeroise(m_checksum);
zeroise(m_offset);
+ m_block_index = 0;
+ }
+
+void OCB_Decryption::buffered_block(const byte input[], size_t input_length)
+ {
+ BOTAN_ASSERT(input_length % 16 == 0, "Input length is an even number of blocks");
+
+ const size_t blocks = input_length / 16;
+
+ const L_computer& L = *m_L;
+
+ secure_vector<byte> ptext_buf(16);
+
+ for(size_t i = 0; i != blocks; ++i)
+ {
+ // could run in parallel
+
+ m_offset ^= L(ctz(++m_block_index));
+
+ ptext_buf = m_offset;
+ xor_buf(&ptext_buf[0], &input[16*i], 16);
+ m_cipher->decrypt(ptext_buf);
+ ptext_buf ^= m_offset;
+
+ send(ptext_buf);
+ m_checksum ^= ptext_buf;
+ }
+ }
+
+void OCB_Decryption::buffered_final(const byte input[], size_t input_length)
+ {
+ /*
+ todo - might have multiple blocks here if buffering up multiple
+ blocks for bitslice mode, run those first by calling buffered_write
+ directly
+ */
+
+ BOTAN_ASSERT(input_length >= m_tag_size, "We have the tag");
+
+ const byte* included_tag = &input[input_length-m_tag_size];
+ input_length -= m_tag_size;
+
+ if(input_length)
+ {
+ BOTAN_ASSERT(input_length < 16, "Only a partial block left");
+
+ m_offset ^= m_L->star(); // Offset_*
+
+ secure_vector<byte> buf(16);
+ m_cipher->encrypt(m_offset, buf); // P_*
+
+ xor_buf(&buf[0], &input[0], input_length);
+
+ xor_buf(&m_checksum[0], &buf[0], input_length);
+ m_checksum[input_length] ^= 0x80;
+
+ send(buf, input_length); // final plaintext
+ }
+
+ // now compute the tag
+ secure_vector<byte> mac = m_offset;
+ mac ^= m_checksum;
+ mac ^= m_L->dollar();
+
+ m_cipher->encrypt(mac);
+
+ mac ^= m_ad_hash;
+
+ zeroise(m_checksum);
+ zeroise(m_offset);
+ m_block_index = 0;
+
+ if(!same_mem(&mac[0], included_tag, m_tag_size))
+ throw Integrity_Failure("OCB tag check failed");
}
}
diff --git a/src/filters/modes/ocb/ocb.h b/src/filters/modes/ocb/ocb.h
index 2c0795fe1..8179f84a3 100644
--- a/src/filters/modes/ocb/ocb.h
+++ b/src/filters/modes/ocb/ocb.h
@@ -15,6 +15,8 @@
namespace Botan {
+class L_computer;
+
/**
* OCB Mode (base class for OCB_Encryption and OCB_Decryption). Note
* that OCB is patented, but is freely licensed in some circumstances.
@@ -32,8 +34,9 @@ class BOTAN_DLL OCB_Mode : public AEAD_Mode,
/**
* @param cipher the 128-bit block cipher to use
* @param tag_size is how big the auth tag will be
+ * @param decrypting true if decrypting
*/
- OCB_Mode(BlockCipher* cipher, size_t tag_size = 16);
+ OCB_Mode(BlockCipher* cipher, size_t tag_size, bool decrypting);
~OCB_Mode();
@@ -46,10 +49,15 @@ class BOTAN_DLL OCB_Mode : public AEAD_Mode,
std::string name() const override;
+ bool valid_iv_length(size_t length) const override
+ {
+ return (length > 0 && length < 16);
+ }
+
protected:
// fixme make these private
std::unique_ptr<BlockCipher> m_cipher;
- std::unique_ptr<class L_computer> m_L;
+ std::unique_ptr<L_computer> m_L;
size_t m_tag_size = 0;
size_t m_block_index = 0;
@@ -71,56 +79,27 @@ class BOTAN_DLL OCB_Encryption : public OCB_Mode
* @param tag_size is how big the auth tag will be
*/
OCB_Encryption(BlockCipher* cipher, size_t tag_size = 16) :
- OCB_Mode(cipher, tag_size) {}
+ OCB_Mode(cipher, tag_size, false) {}
private:
void buffered_block(const byte input[], size_t input_length) override;
void buffered_final(const byte input[], size_t input_length) override;
};
-#if 0
-/**
-* OCB Decryption
-*/
-class BOTAN_DLL OCB_Decryption : public AEAD_Mode,
- private Buffered_Filter
+class BOTAN_DLL OCB_Decryption : public OCB_Mode
{
public:
/**
- * @param cipher the cipher to use
+ * @param cipher the 128-bit block cipher to use
* @param tag_size is how big the auth tag will be
*/
- OCB_Decryption(BlockCipher* cipher, size_t tag_size = 16);
-
- ~OCB_Decryption();
-
- void set_key(const SymmetricKey& key) override;
- void set_nonce(const byte nonce[], size_t nonce_len) override;
-
- void set_associated_data(const byte ad[], size_t ad_len) override;
-
- bool valid_keylength(size_t n) const override;
-
- std::string name() const override;
+ OCB_Decryption(BlockCipher* cipher, size_t tag_size = 16) :
+ OCB_Mode(cipher, tag_size, true) {}
private:
void buffered_block(const byte input[], size_t input_length) override;
void buffered_final(const byte input[], size_t input_length) override;
-
- void write(const byte input[], size_t input_length) override;
- void start_msg() override;
- void end_msg() override;
-
- std::unique_ptr<BlockCipher> m_cipher;
- std::unique_ptr<class L_computer> m_L;
- size_t m_tag_size = 0;
- size_t m_block_index = 0;
-
- secure_vector<byte> m_ad_hash;
- secure_vector<byte> m_offset;
- secure_vector<byte> m_checksum;
};
-#endif
}