Debian Codegraph @

src/lib/data_mgr/test/RFC4880Tests.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 RFC4880Tests.cpp
29
30 Contains test cases to test the RFC4880 implementation
31 *****************************************************************************/
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <cppunit/extensions/HelperMacros.h>
36 #include "RFC4880Tests.h"
37 #include "RFC4880.h"
38 #include "ByteString.h"
39 #include "CryptoFactory.h"
40 #include "AESKey.h"
41
42 CPPUNIT_TEST_SUITE_REGISTRATION(RFC4880Tests);
43
44 void RFC4880Tests::setUp()
45 {
46 CPPUNIT_ASSERT((rng = CryptoFactory::i()->getRNG()) != NULL);
47 }
48
49 void RFC4880Tests::tearDown()
50 {
51 }
52
53 void RFC4880Tests::testRFC4880()
54 {
55 const unsigned char* pwd1String = (const unsigned char*) "monkey";
56 const unsigned char* pwd2String = (const unsigned char*) "bicycle";
57 ByteString pwd1(pwd1String, strlen("monkey"));
58 ByteString pwd2(pwd2String, strlen("bicycle"));
59
60 // Generate salt and make sure that two different salt values are generated and
61 // that the last byte is also different (resulting in a different iteration jitter
62 // when computing a PBE key using both salt values)
63 ByteString salt1, salt2;
64
65 do
66 {
67 CPPUNIT_ASSERT(rng->generateRandom(salt1, 8) && rng->generateRandom(salt2, 8));
68 }
69 while ((salt1 == salt2) || (salt1[salt1.size() - 1] == salt2[salt2.size() - 1]));
70
71 // Create a password-based encryption key from the first and second password
72 AESKey* key1;
73 AESKey* key2;
74
75 CPPUNIT_ASSERT(RFC4880::PBEDeriveKey(pwd1, salt1, &key1));
76 CPPUNIT_ASSERT(RFC4880::PBEDeriveKey(pwd2, salt2, &key2));
77
78 // Check that the output keys differ and have the correct length
79 CPPUNIT_ASSERT(key1->getKeyBits().size() == 32);
80 CPPUNIT_ASSERT(key2->getKeyBits().size() == 32);
81 CPPUNIT_ASSERT(key1->getKeyBits() != key2->getKeyBits());
82
83 // Rederive the keys to check that the same output is generated every time
84 AESKey* key1_;
85 AESKey* key2_;
86
87 CPPUNIT_ASSERT(RFC4880::PBEDeriveKey(pwd1, salt1, &key1_));
88 CPPUNIT_ASSERT(RFC4880::PBEDeriveKey(pwd2, salt2, &key2_));
89
90 CPPUNIT_ASSERT(key1->getKeyBits() == key1_->getKeyBits());
91 CPPUNIT_ASSERT(key2->getKeyBits() == key2_->getKeyBits());
92
93 // Now reverse the salts and derive new keys
94 AESKey* key3;
95 AESKey* key4;
96
97 CPPUNIT_ASSERT(RFC4880::PBEDeriveKey(pwd1, salt2, &key3));
98 CPPUNIT_ASSERT(RFC4880::PBEDeriveKey(pwd2, salt1, &key4));
99
100 // Check that the keys are different and that they differ from the
101 // original keys (because different salts were used)
102 CPPUNIT_ASSERT(key3->getKeyBits() != key4->getKeyBits());
103 CPPUNIT_ASSERT(key1->getKeyBits() != key3->getKeyBits());
104 CPPUNIT_ASSERT(key1->getKeyBits() != key4->getKeyBits());
105 CPPUNIT_ASSERT(key2->getKeyBits() != key3->getKeyBits());
106 CPPUNIT_ASSERT(key2->getKeyBits() != key4->getKeyBits());
107
108 // Clean up
109 delete key1;
110 delete key2;
111 delete key1_;
112 delete key2_;
113 delete key3;
114 delete key4;
115 }
116