summaryrefslogtreecommitdiff
path: root/tests/test_crypto-logic.cpp
blob: d13f6af319da57ac7a45e65ed42040ebca23eb2a (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 *  Copyright (c) 2017 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
#include <crypto-logic.h>
#include <platform/decider.h>
#include <generic-backend/gstore.h>
#include <db-row.h>

#include <utility>
#include <cstdlib>
#include <time.h>
#include <boost/test/unit_test.hpp>

#include "test_common.h"

using namespace CKM;

namespace {

Password createRandomPass(size_t size)
{
	static unsigned int seed = ::time(nullptr);

	Password buf(size, 0x00);
	for (size_t i = 0; i < size; ++i)
		buf[i] = static_cast<Password::value_type>(::rand_r(&seed) % 256);

	return buf;
}

} // namespace anonymous

BOOST_AUTO_TEST_SUITE(CRYPTO_LOGIC_TEST)

BOOST_AUTO_TEST_CASE(move_semantics)
{
	CryptoLogic logic;

	const ClientId client = "test_client";
	BOOST_REQUIRE_NO_THROW(logic.pushKey(client, createRandom(10)));

	CryptoLogic moved(std::move(logic));
	BOOST_REQUIRE(!logic.haveKey(client));
	BOOST_REQUIRE(moved.haveKey(client));

	CryptoLogic moveAssigned = std::move(moved);
	BOOST_REQUIRE(!moved.haveKey(client));
	BOOST_REQUIRE(moveAssigned.haveKey(client));

	moveAssigned = std::move(moveAssigned);
	BOOST_REQUIRE(moveAssigned.haveKey(client));
}

BOOST_AUTO_TEST_CASE(push_key)
{
	CryptoLogic logic;

	const ClientId client = "test_client";
	BOOST_REQUIRE_THROW(logic.pushKey(std::string(), createRandom(10)),
						Exc::InternalError);
	BOOST_REQUIRE_THROW(logic.pushKey(client, RawBuffer()),
						Exc::InternalError);

	BOOST_REQUIRE_NO_THROW(logic.pushKey(client, createRandom(10)));
	BOOST_REQUIRE_THROW(logic.pushKey(client, createRandom(10)),
						Exc::InternalError);

	ClientId increasingOwner = "a";
	for (size_t i = 0; i < 20; ++i, increasingOwner.push_back('a')) {
		BOOST_REQUIRE_NO_THROW(logic.pushKey(increasingOwner, createRandom(10)));
		BOOST_REQUIRE_THROW(logic.pushKey(increasingOwner, createRandom(10)),
							Exc::InternalError);
	}
}

BOOST_AUTO_TEST_CASE(row_encryption)
{
	Policy policy(Password(), true);
	Crypto::Data data(DataType(DataType::Type::BINARY_DATA), createRandom(10));
	Crypto::Decider decider;
	Crypto::GStore &store = decider.getStore(data.type, policy);
	Token token = store.import(data, policy.password, Crypto::EncryptionParams());

	Name name = "test_data";
	ClientId owner = "test_owner";
	DB::Row row(token, name, owner, static_cast<int>(policy.extractable));

	CryptoLogic logic;

	BOOST_REQUIRE_THROW(logic.encryptRow(row), Exc::InternalError);

	auto key = createRandom(32);
	BOOST_REQUIRE_NO_THROW(logic.pushKey(owner, key));
	BOOST_REQUIRE_NO_THROW(logic.encryptRow(row));
	BOOST_REQUIRE_NO_THROW(logic.decryptRow(policy.password, row));
}

BOOST_AUTO_TEST_CASE(row_encryption_negatives)
{
	Policy policy(Password(), true);
	Crypto::Data data(DataType(DataType::Type::BINARY_DATA), createRandom(10));
	Crypto::Decider decider;
	Crypto::GStore &store = decider.getStore(data.type, policy);
	Token token = store.import(data, policy.password, Crypto::EncryptionParams());

	Name name = "test_data";
	ClientId owner = "test_owner";
	DB::Row row(token, name, owner, static_cast<int>(policy.extractable));

	CryptoLogic logic;

	auto key = createRandom(32);
	BOOST_REQUIRE_NO_THROW(logic.pushKey(owner, key));
	BOOST_REQUIRE_NO_THROW(logic.encryptRow(row));

	BOOST_REQUIRE_THROW(logic.decryptRow(createRandomPass(10), row),
						Exc::AuthenticationFailed);

	BOOST_REQUIRE_NO_THROW(logic.removeKey(owner));
	BOOST_REQUIRE_THROW(logic.decryptRow(Password(), row),
						Exc::AuthenticationFailed);
	BOOST_REQUIRE_NO_THROW(logic.pushKey(owner, key));

	row.algorithmType = DBCMAlgType::NONE;
	BOOST_REQUIRE_THROW(logic.decryptRow(Password(), row),
						Exc::AuthenticationFailed);
}

BOOST_AUTO_TEST_SUITE_END() // CRYPTO_LOGIC_TEST