aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_pbkdf.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-08-03 13:54:48 -0400
committerJack Lloyd <[email protected]>2017-08-03 13:54:48 -0400
commit147eb1c26c236a362c4fb42a3d119ef10175092d (patch)
tree7a7d6843045ee6090e5d85b6a83ddae8f0bf2181 /src/tests/test_pbkdf.cpp
parent95c618bd340637aa0a101a57ccb15ba6a2ebe2b4 (diff)
Add a test of PGP S2K iteration encoding
Diffstat (limited to 'src/tests/test_pbkdf.cpp')
-rw-r--r--src/tests/test_pbkdf.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/tests/test_pbkdf.cpp b/src/tests/test_pbkdf.cpp
index 32144bd9b..c45cc45de 100644
--- a/src/tests/test_pbkdf.cpp
+++ b/src/tests/test_pbkdf.cpp
@@ -10,6 +10,10 @@
#include <botan/pbkdf.h>
#endif
+#if defined(BOTAN_HAS_PGP_S2K)
+ #include <botan/pgp_s2k.h>
+#endif
+
namespace Botan_Tests {
namespace {
@@ -53,6 +57,55 @@ BOTAN_REGISTER_TEST("pbkdf", PBKDF_KAT_Tests);
#endif
+#if defined(BOTAN_HAS_PGP_S2K)
+
+class PGP_S2K_Iter_Test : public Test
+ {
+ public:
+ std::vector<Test::Result> run() override
+ {
+ Test::Result result("PGP_S2K iteration encoding");
+
+ // The maximum representable iteration count
+ const size_t max_iter = 65011712;
+
+ result.test_eq("Encoding of large value accepted",
+ Botan::OpenPGP_S2K::encode_count(max_iter * 2), size_t(255));
+ result.test_eq("Encoding of small value accepted",
+ Botan::OpenPGP_S2K::encode_count(0), size_t(0));
+
+ for(size_t c = 0; c != 256; ++c)
+ {
+ const size_t dec = Botan::OpenPGP_S2K::decode_count(c);
+ const size_t comp_dec = (16 + (c & 0x0F)) << ((c >> 4) + 6);
+ result.test_eq("Decoded value matches PGP formula", dec, comp_dec);
+ }
+
+ uint8_t last_enc = 0;
+
+ for(size_t i = 0; i <= max_iter; i += 64)
+ {
+ const uint8_t enc = Botan::OpenPGP_S2K::encode_count(i);
+ result.test_lte("Encoded value non-decreasing", last_enc, enc);
+
+ /*
+ The iteration count as encoded may not be exactly the
+ value requested, but should never be less
+ */
+ const size_t dec = Botan::OpenPGP_S2K::decode_count(enc);
+ result.test_gte("Decoded value is >= requested", dec, i);
+
+ last_enc = enc;
+ }
+
+ return std::vector<Test::Result>{result};
+ }
+ };
+
+BOTAN_REGISTER_TEST("pgp_s2k_iter", PGP_S2K_Iter_Test);
+
+#endif
+
}
}