1//
2// PBKDF2EngineTest.cpp
3//
4// Copyright (c) 2014, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "PBKDF2EngineTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/PBKDF2Engine.h"
15#include "Poco/HMACEngine.h"
16#include "Poco/SHA1Engine.h"
17
18
19using Poco::PBKDF2Engine;
20using Poco::HMACEngine;
21using Poco::SHA1Engine;
22using Poco::DigestEngine;
23
24
25PBKDF2EngineTest::PBKDF2EngineTest(const std::string& rName): CppUnit::TestCase(rName)
26{
27}
28
29
30PBKDF2EngineTest::~PBKDF2EngineTest()
31{
32}
33
34
35void PBKDF2EngineTest::testPBKDF2a()
36{
37 // test vector 1 from RFC 6070
38
39 std::string p("password");
40 std::string s("salt");
41 PBKDF2Engine<HMACEngine<SHA1Engine> > pbkdf2(s, 1, 20);
42 pbkdf2.update(p);
43 std::string dk = DigestEngine::digestToHex(pbkdf2.digest());
44 assertTrue (dk == "0c60c80f961f0e71f3a9b524af6012062fe037a6");
45}
46
47
48void PBKDF2EngineTest::testPBKDF2b()
49{
50 // test vector 2 from RFC 6070
51
52 std::string p("password");
53 std::string s("salt");
54 PBKDF2Engine<HMACEngine<SHA1Engine> > pbkdf2(s, 2, 20);
55 pbkdf2.update(p);
56 std::string dk = DigestEngine::digestToHex(pbkdf2.digest());
57 assertTrue (dk == "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957");
58}
59
60
61void PBKDF2EngineTest::testPBKDF2c()
62{
63 // test vector 3 from RFC 6070
64
65 std::string p("password");
66 std::string s("salt");
67 PBKDF2Engine<HMACEngine<SHA1Engine> > pbkdf2(s, 4096, 20);
68 pbkdf2.update(p);
69 std::string dk = DigestEngine::digestToHex(pbkdf2.digest());
70 assertTrue (dk == "4b007901b765489abead49d926f721d065a429c1");
71}
72
73
74void PBKDF2EngineTest::testPBKDF2d()
75{
76 // test vector 4 from RFC 6070
77#if defined(ENABLE_LONG_RUNNING_TESTS)
78 std::string p("password");
79 std::string s("salt");
80 PBKDF2Engine<HMACEngine<SHA1Engine> > pbkdf2(s, 16777216, 20);
81 pbkdf2.update(p);
82 std::string dk = DigestEngine::digestToHex(pbkdf2.digest());
83 assertTrue (dk == "eefe3d61cd4da4e4e9945b3d6ba2158c2634e984");
84#endif // defined(ENABLE_LONG_RUNNING_TESTS)
85}
86
87
88void PBKDF2EngineTest::testPBKDF2e()
89{
90 // test vector 5 from RFC 6070
91
92 std::string p("passwordPASSWORDpassword");
93 std::string s("saltSALTsaltSALTsaltSALTsaltSALTsalt");
94 PBKDF2Engine<HMACEngine<SHA1Engine> > pbkdf2(s, 4096, 25);
95 pbkdf2.update(p);
96 std::string dk = DigestEngine::digestToHex(pbkdf2.digest());
97 assertTrue (dk == "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038");
98}
99
100
101void PBKDF2EngineTest::testPBKDF2f()
102{
103 // test vector 6 from RFC 6070
104
105 std::string p("pass\0word", 9);
106 std::string s("sa\0lt", 5);
107 PBKDF2Engine<HMACEngine<SHA1Engine> > pbkdf2(s, 4096, 16);
108 pbkdf2.update(p);
109 std::string dk = DigestEngine::digestToHex(pbkdf2.digest());
110 assertTrue (dk == "56fa6aa75548099dcc37d7f03425e0c3");
111}
112
113
114void PBKDF2EngineTest::setUp()
115{
116}
117
118
119void PBKDF2EngineTest::tearDown()
120{
121}
122
123
124CppUnit::Test* PBKDF2EngineTest::suite()
125{
126 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("PBKDF2EngineTest");
127
128 CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2a);
129 CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2b);
130 CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2c);
131 CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2d);
132 CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2e);
133 CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2f);
134
135 return pSuite;
136}
137