aboutsummaryrefslogtreecommitdiff
path: root/components/service/crypto/test/service/extension/hash/hash_service_scenarios.cpp
blob: f76d59e16a8a6b76071704a923675125eb68d02b (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
/*
 * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <string>
#include <cstring>
#include <cstdint>
#include <vector>
#include <CppUTest/TestHarness.h>
#include "hash_service_scenarios.h"
#include "hash_test_vectors.h"

hash_service_scenarios::hash_service_scenarios(crypto_client *crypto_client) :
	m_crypto_client(crypto_client),
	m_ref_input(NULL)
{

}

hash_service_scenarios::~hash_service_scenarios()
{
	delete m_crypto_client;
	m_crypto_client = NULL;

	delete [] m_ref_input;
	m_ref_input = NULL;
}

void hash_service_scenarios::create_ref_input(size_t size)
{
	m_ref_input = new uint8_t[size];
	memset(m_ref_input, 0x51, size);
}

void hash_service_scenarios::calculateHash()
{
	/* Calculates a hash and compares result against test vector */
	psa_status_t status;
	std::vector<uint8_t> input;
	std::vector<uint8_t> expected_output;
	uint8_t output[PSA_HASH_MAX_SIZE];
	size_t output_len;

	hash_test_vectors::plaintext_1_len_610(input);
	hash_test_vectors::sha256_1(expected_output);

	uint32_t op_handle = 0;

	status = m_crypto_client->hash_setup(&op_handle, PSA_ALG_SHA_256);
	CHECK_EQUAL(PSA_SUCCESS, status);

	status = m_crypto_client->hash_update(op_handle, &input[0], input.size());
	CHECK_EQUAL(PSA_SUCCESS, status);

	status = m_crypto_client->hash_finish(op_handle, output, sizeof(output), &output_len);
	CHECK_EQUAL(PSA_SUCCESS, status);

	UNSIGNED_LONGS_EQUAL(expected_output.size(), output_len);
	MEMCMP_EQUAL(&expected_output[0], &output[0], output_len);
}

void hash_service_scenarios::hashAndVerify()
{
	/* Calculates and verifies hash over a large reference input */
	size_t max_payload = m_crypto_client->hash_max_update_size();
	size_t input_size = 9999;
	size_t byte_count = 0;

	create_ref_input(input_size);

	uint8_t hash[PSA_HASH_MAX_SIZE];
	size_t hash_len;

	uint32_t op_handle = 0;
	psa_status_t status;

	status = m_crypto_client->hash_setup(&op_handle, PSA_ALG_SHA_256);
	CHECK_EQUAL(PSA_SUCCESS, status);

	while (byte_count < input_size) {

		size_t bytes_left = input_size - byte_count;
		size_t update_size = (bytes_left < max_payload) ?
			bytes_left :
			max_payload;

		status = m_crypto_client->hash_update(op_handle, &m_ref_input[byte_count], update_size);
		CHECK_EQUAL(PSA_SUCCESS, status);

		byte_count += update_size;
	}

	uint32_t clone_op_handle = 0;

	status = m_crypto_client->hash_clone(op_handle, &clone_op_handle);
	CHECK_EQUAL(PSA_SUCCESS, status);

	status = m_crypto_client->hash_finish(op_handle, hash, sizeof(hash), &hash_len);
	CHECK_EQUAL(PSA_SUCCESS, status);

	status = m_crypto_client->hash_verify(clone_op_handle, hash, hash_len);
	CHECK_EQUAL(PSA_SUCCESS, status);
}

void hash_service_scenarios::hashAbort()
{
	/* Aborts a hash operation after the first update */
	size_t max_payload = m_crypto_client->hash_max_update_size();
	size_t input_size = 15999;

	create_ref_input(input_size);

	uint8_t hash[PSA_HASH_MAX_SIZE];
	size_t hash_len;

	uint32_t op_handle = 0;
	psa_status_t status;

	status = m_crypto_client->hash_setup(&op_handle, PSA_ALG_SHA_256);
	CHECK_EQUAL(PSA_SUCCESS, status);

	size_t update_size = (input_size < max_payload) ?
			input_size :
			max_payload;

	status = m_crypto_client->hash_update(op_handle, &m_ref_input[0], update_size);
	CHECK_EQUAL(PSA_SUCCESS, status);

	status = m_crypto_client->hash_abort(op_handle);
	CHECK_EQUAL(PSA_SUCCESS, status);

	status = m_crypto_client->hash_update(op_handle, &m_ref_input[0], update_size);
	CHECK_EQUAL(PSA_ERROR_BAD_STATE, status);
}