File: key256.cc

package info (click to toggle)
yapet 2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,920 kB
  • sloc: cpp: 32,397; sh: 5,032; makefile: 880; ansic: 36; sed: 16
file content (77 lines) | stat: -rw-r--r-- 2,445 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

#include <cstring>

#include "consts.h"
#include "key256.hh"

constexpr std::uint8_t expected_key[] = {
    0x85, 0x9c, 0xb0, 0x9e, 0xa2, 0x17, 0xb7, 0xb6, 0x6b, 0xd6, 0x2f,
    0x08, 0xa1, 0xcd, 0x44, 0xcb, 0xb6, 0x8a, 0xdd, 0x4a, 0x55, 0x17,
    0x1e, 0xbb, 0xa6, 0xe1, 0xe9, 0xbe, 0x06, 0x9a, 0x46, 0xab,
};

constexpr char password[] = "JustATestPasswordForKeepingSecret";
auto passwordLength =
    static_cast<yapet::SecureArray::size_type>(std::strlen(password));

class Key256Test : public CppUnit::TestFixture {
   private:
    yapet::SecureArray passwordArray{0};
    yapet::MetaData parameters;

   public:
    static CppUnit::TestSuite *suite() {
        CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite("Key 256");

        suiteOfTests->addTest(new CppUnit::TestCaller<Key256Test>(
            "should create proper key", &Key256Test::testKey));
        suiteOfTests->addTest(new CppUnit::TestCaller<Key256Test>(
            "should create no IV", &Key256Test::testIV));

        return suiteOfTests;
    }

    void setUp() {
        passwordArray = yapet::toSecureArray(password);
        yapet::MetaData p;
        p.setValue(YAPET::Consts::ARGON2_MEMORY_COST_KEY, 262144);
        p.setValue(YAPET::Consts::ARGON2_PARALLELISM_KEY, 16);
        p.setValue(YAPET::Consts::ARGON2_TIME_COST_KEY, 5);
        p.setValue(YAPET::Consts::ARGON2_SALT1_KEY, 0x61626364);
        p.setValue(YAPET::Consts::ARGON2_SALT2_KEY, 0x65666768);
        p.setValue(YAPET::Consts::ARGON2_SALT3_KEY, 0x696a6b6c);
        p.setValue(YAPET::Consts::ARGON2_SALT4_KEY, 0x6d6e6f70);

        parameters = p;
    }

    void testKey() {
        yapet::Key256 key{};
        key.keyingParameters(parameters);
        key.password(passwordArray);

        for (int i = 0; i < key.keySize(); i++) {
            CPPUNIT_ASSERT((*key.key())[i] == expected_key[i]);
        }
    }

    void testIV() {
        yapet::Key256 key{};
        key.keyingParameters(parameters);
        key.password(passwordArray);

        CPPUNIT_ASSERT(key.ivecSize() == 0);
        CPPUNIT_ASSERT(key.ivec() == yapet::SecureArray{});
    }
};

int main() {
    CppUnit::TextUi::TestRunner runner;
    runner.addTest(Key256Test::suite());
    return runner.run() ? 0 : 1;
}