aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-09-14 12:39:05 -0400
committerJack Lloyd <[email protected]>2016-09-15 09:23:22 -0400
commitdc5aa5be4d254f9f7e9b68cd265058011462cdaf (patch)
tree5f883c1fd95d00ec2c9a5cacc974f43149cc9505 /src/tests
parentbe4655148cfc8cb048fd53de0965cc5e939c4cbc (diff)
Add cpuid overload to test framework
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/data/aead/gcm.vec3
-rw-r--r--src/tests/data/block/aes.vec3
-rw-r--r--src/tests/data/block/idea.vec3
-rw-r--r--src/tests/data/block/noekeon.vec3
-rw-r--r--src/tests/data/block/serpent.vec3
-rw-r--r--src/tests/data/block/threefish.vec3
-rw-r--r--src/tests/data/hash/sha1.vec2
-rw-r--r--src/tests/data/stream/chacha.vec3
-rw-r--r--src/tests/tests.cpp113
-rw-r--r--src/tests/tests.h3
10 files changed, 124 insertions, 15 deletions
diff --git a/src/tests/data/aead/gcm.vec b/src/tests/data/aead/gcm.vec
index 516e828ec..be8250792 100644
--- a/src/tests/data/aead/gcm.vec
+++ b/src/tests/data/aead/gcm.vec
@@ -1,3 +1,6 @@
+
+#test cpuid aesni ssse3 clmul
+
[AES-128/GCM]
# Nist | Test Case 1
Key = 00000000000000000000000000000000
diff --git a/src/tests/data/block/aes.vec b/src/tests/data/block/aes.vec
index 4e0b7399c..50d9cc469 100644
--- a/src/tests/data/block/aes.vec
+++ b/src/tests/data/block/aes.vec
@@ -1,3 +1,6 @@
+
+#test cpuid aesni ssse3
+
[AES-128]
Key = 000102030405060708090A0B0C0D0E0F
In = 00112233445566778899AABBCCDDEEFF
diff --git a/src/tests/data/block/idea.vec b/src/tests/data/block/idea.vec
index 705d02d68..eee5ef5e9 100644
--- a/src/tests/data/block/idea.vec
+++ b/src/tests/data/block/idea.vec
@@ -1,3 +1,6 @@
+
+#test cpuid sse2
+
[IDEA]
Key = ED1BCC9E9267925F3132BA3A8CF9B764
In = 7409000000000000
diff --git a/src/tests/data/block/noekeon.vec b/src/tests/data/block/noekeon.vec
index 2921301c1..08e7cabf3 100644
--- a/src/tests/data/block/noekeon.vec
+++ b/src/tests/data/block/noekeon.vec
@@ -1,3 +1,6 @@
+
+#cpuid simd32
+
[Noekeon]
Key = 00000000000000000000000000000000
In = 00000000000000000000000000000000
diff --git a/src/tests/data/block/serpent.vec b/src/tests/data/block/serpent.vec
index 623ee6be8..8a40c54b7 100644
--- a/src/tests/data/block/serpent.vec
+++ b/src/tests/data/block/serpent.vec
@@ -1,3 +1,6 @@
+
+#test cpuid simd
+
[Serpent]
Key = 00000000000000000000000000000000
In = D29D576FCEA3A3A7ED9099F29273D78E2D62A890CEA3A3A7ED9099F29273D78ED29D576F315C5C58ED9099F29273D78E2D62A890315C5C58ED9099F29273D78ED29D576FCEA3A3A7126F660D9273D78E2D62A890CEA3A3A7126F660D9273D78ED29D576F315C5C58126F660D9273D78E2D62A890315C5C58126F660D9273D78ED29D576FCEA3A3A7ED9099F26D8C28712D62A890CEA3A3A7ED9099F26D8C2871D29D576F315C5C58ED9099F26D8C28712D62A890315C5C58ED9099F26D8C2871D29D576FCEA3A3A7126F660D6D8C28712D62A890CEA3A3A7126F660D6D8C2871D29D576F315C5C58126F660D6D8C2871
diff --git a/src/tests/data/block/threefish.vec b/src/tests/data/block/threefish.vec
index b4e9d8229..dfaccb7ff 100644
--- a/src/tests/data/block/threefish.vec
+++ b/src/tests/data/block/threefish.vec
@@ -1,3 +1,6 @@
+
+#test cpuid avx2
+
[Threefish-512]
Key = 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In = 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/src/tests/data/hash/sha1.vec b/src/tests/data/hash/sha1.vec
index f5fd82340..e86650f30 100644
--- a/src/tests/data/hash/sha1.vec
+++ b/src/tests/data/hash/sha1.vec
@@ -1,3 +1,5 @@
+#test cpuid sse2
+
[SHA-160]
In =
Out = DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
diff --git a/src/tests/data/stream/chacha.vec b/src/tests/data/stream/chacha.vec
index 47d3aa7fe..f7ba701ba 100644
--- a/src/tests/data/stream/chacha.vec
+++ b/src/tests/data/stream/chacha.vec
@@ -1,3 +1,6 @@
+
+#test cpuid sse2
+
[ChaCha(8)]
# Tests got from the original implementation of Daniel J. Bernstein
diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp
index a6f96144c..2913e5445 100644
--- a/src/tests/tests.cpp
+++ b/src/tests/tests.cpp
@@ -527,21 +527,21 @@ Text_Based_Test::Text_Based_Test(const std::string& algo,
std::vector<uint8_t> Text_Based_Test::get_req_bin(const VarMap& vars,
const std::string& key) const
- {
- auto i = vars.find(key);
- if(i == vars.end())
- throw Test_Error("Test missing variable " + key);
+ {
+ auto i = vars.find(key);
+ if(i == vars.end())
+ throw Test_Error("Test missing variable " + key);
- try
- {
- return Botan::hex_decode(i->second);
- }
- catch(std::exception&)
- {
- throw Test_Error("Test invalid hex input '" + i->second + "'" +
- + " for key " + key);
- }
+ try
+ {
+ return Botan::hex_decode(i->second);
+ }
+ catch(std::exception&)
+ {
+ throw Test_Error("Test invalid hex input '" + i->second + "'" +
+ + " for key " + key);
}
+ }
std::string Text_Based_Test::get_opt_str(const VarMap& vars,
const std::string& key, const std::string& def_value) const
@@ -646,9 +646,17 @@ std::string Text_Based_Test::get_next_line()
}
m_cur.reset(new std::ifstream(m_srcs[0]));
+ m_cur_src_name = m_srcs[0];
+
+ // Reinit cpuid on new file if needed
+ if(m_cpu_flags.empty() == false)
+ {
+ m_cpu_flags.clear();
+ Botan::CPUID::initialize();
+ }
if(!m_cur->good())
- throw Test_Error("Could not open input file '" + m_srcs[0]);
+ throw Test_Error("Could not open input file '" + m_cur_src_name);
m_srcs.pop_front();
}
@@ -662,7 +670,12 @@ std::string Text_Based_Test::get_next_line()
continue;
if(line[0] == '#')
- continue;
+ {
+ if(line.compare(0, 6, "#test ") == 0)
+ return line;
+ else
+ continue;
+ }
return line;
}
@@ -685,6 +698,42 @@ std::string strip_ws(const std::string& in)
return in.substr(first_c, last_c - first_c + 1);
}
+std::vector<Botan::CPUID::CPUID_bits> map_cpuid_string(const std::string& tok)
+ {
+#if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
+ if(tok == "sse2" || tok == "simd")
+ return {Botan::CPUID::CPUID_SSE2_BIT};
+ if(tok == "ssse3")
+ return {Botan::CPUID::CPUID_SSSE3_BIT};
+ if(tok == "aesni")
+ return {Botan::CPUID::CPUID_AESNI_BIT};
+ if(tok == "clmul")
+ return {Botan::CPUID::CPUID_CLMUL_BIT};
+ if(tok == "avx2")
+ return {Botan::CPUID::CPUID_AVX2_BIT};
+#endif
+
+#if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
+ if(tok == "altivec" || tok == "simd")
+ return {Botan::CPUID::CPUID_ALITVEC_BIT};
+#endif
+
+ return {};
+ }
+
+std::vector<Botan::CPUID::CPUID_bits>
+parse_cpuid_bits(const std::vector<std::string>& tok)
+ {
+ std::vector<Botan::CPUID::CPUID_bits> bits;
+ for(size_t i = 1; i < tok.size(); ++i)
+ {
+ const std::vector<Botan::CPUID::CPUID_bits> more = map_cpuid_string(tok[i]);
+ bits.insert(bits.end(), more.begin(), more.end());
+ }
+
+ return bits;
+ }
+
}
std::vector<Test::Result> Text_Based_Test::run()
@@ -701,6 +750,26 @@ std::vector<Test::Result> Text_Based_Test::run()
if(line.empty()) // EOF
break;
+ if(line.compare(0, 6, "#test ") == 0)
+ {
+ std::vector<std::string> pragma_tokens = Botan::split_on(line.substr(6), ' ');
+
+ if(pragma_tokens.empty())
+ throw Test_Error("Empty pragma found in " + m_cur_src_name);
+
+ if(pragma_tokens[0] != "cpuid")
+ throw Test_Error("Unknown test pragma '" + line + "' in " + m_cur_src_name);
+
+ m_cpu_flags = parse_cpuid_bits(pragma_tokens);
+
+ if(m_cpu_flags.empty())
+ throw Test_Error("Empty cpuid pragma in " + m_cur_src_name);
+
+ continue;
+ }
+ else if(line[0] == '#')
+ throw Test_Error("Unknown test pragma '" + line + "' in " + m_cur_src_name);
+
if(line[0] == '[' && line[line.size()-1] == ']')
{
header = line.substr(1, line.size() - 2);
@@ -736,7 +805,21 @@ std::vector<Test::Result> Text_Based_Test::run()
++test_cnt;
uint64_t start = Test::timestamp();
+
Test::Result result = run_one_test(header, vars);
+ if(m_cpu_flags.size() > 0)
+ {
+ for(auto&& cpuid_bit : m_cpu_flags)
+ {
+ if(Botan::CPUID::has_cpuid_bit(cpuid_bit))
+ {
+ Botan::CPUID::clear_cpuid_bit(cpuid_bit);
+ // now re-run the test
+ result.merge(run_one_test(header, vars));
+ }
+ }
+ Botan::CPUID::initialize();
+ }
result.set_ns_consumed(Test::timestamp() - start);
if(result.tests_failed())
diff --git a/src/tests/tests.h b/src/tests/tests.h
index 39aaf67cd..43b9e0d3a 100644
--- a/src/tests/tests.h
+++ b/src/tests/tests.h
@@ -12,6 +12,7 @@
#include <botan/rng.h>
#include <botan/hex.h>
#include <botan/symkey.h>
+#include <botan/cpuid.h>
#if defined(BOTAN_HAS_BIGINT)
#include <botan/bigint.h>
@@ -442,7 +443,9 @@ class Text_Based_Test : public Test
bool m_first = true;
std::unique_ptr<std::ifstream> m_cur;
+ std::string m_cur_src_name;
std::deque<std::string> m_srcs;
+ std::vector<Botan::CPUID::CPUID_bits> m_cpu_flags;
};
}