aboutsummaryrefslogtreecommitdiff
path: root/components/service/crypto/test/unit/crypto_msg_encode_decode.cpp
blob: ea8e388268675c0c6fc2c36720cee0489e72c241 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <CppUTest/TestHarness.h>
#include <service/crypto/protobuf/generate_key.pb.h>
#include <service/crypto/protobuf/export_public_key.pb.h>
#include <service/crypto/protobuf/sign_hash.pb.h>
#include <pb.h>
#include <pb_encode.h>
#include <pb_decode.h>


TEST_GROUP(CryptoMsgTests) {

	void setup() {
        print_info = false;
    }

	/* Nanopb encode/decode methods */
	static bool encode_byte_array(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {

		const pb_bytes_array_t *byte_array = (const pb_bytes_array_t *)*arg;
		if (!pb_encode_tag_for_field(stream, field)) return false;

		return pb_encode_string(stream, byte_array->bytes, byte_array->size);
	}

	static bool decode_byte_array(pb_istream_t *stream, const pb_field_t *field, void **arg) {

		(void)field;
		pb_bytes_array_t *byte_array = (pb_bytes_array_t *)*arg;
		if (stream->bytes_left > byte_array->size) return false;

		return pb_read(stream, byte_array->bytes, stream->bytes_left);
	}

	static pb_callback_t out_byte_array(const pb_bytes_array_t *byte_array) {

		pb_callback_t callback;
		callback.funcs.encode = encode_byte_array;
		callback.arg = (void*)byte_array;

		return callback;
	}

	static pb_callback_t in_byte_array(pb_bytes_array_t *byte_array) {

		pb_callback_t callback;
		callback.funcs.decode = decode_byte_array;
		callback.arg = (void*)byte_array;
		return callback;
	}

	bool print_info;
};

TEST(CryptoMsgTests, GenerateKeyInMsgTest) {
	/* Sender - set values and serialize */
	ts_crypto_GenerateKeyIn sent_msg = ts_crypto_GenerateKeyIn_init_default;
	ts_crypto_KeyAttributes sent_key_attributes = ts_crypto_KeyAttributes_init_default;

	sent_key_attributes.type =
		ts_crypto_KeyType_KEY_TYPE_ECC_PUBLIC_KEY_BASE |
		ts_crypto_EccCurve_ECC_CURVE_SECP_R1;

	sent_key_attributes.key_bits = 256;
	sent_key_attributes.lifetime = ts_crypto_KeyLifetime_KEY_LIFETIME_PERSISTENT;
	sent_key_attributes.id = 3;

	sent_key_attributes.has_policy = true;
	sent_key_attributes.policy.usage = ts_crypto_KeyUsage_KEY_USAGE_SIGN_HASH;
	sent_key_attributes.policy.alg =
		ts_crypto_Alg_ALG_DETERMINISTIC_ECDSA_BASE |
		ts_crypto_Alg_ALG_SHA_256;

	sent_msg.attributes = sent_key_attributes;
	sent_msg.has_attributes = true;

	size_t sent_msg_len;
	CHECK(pb_get_encoded_size(&sent_msg_len, ts_crypto_GenerateKeyIn_fields, &sent_msg));
	uint8_t *sent_msg_buf = (uint8_t*)malloc(sent_msg_len);
	CHECK(sent_msg_len > 0);
	CHECK(sent_msg_buf);

	pb_ostream_t ostream = pb_ostream_from_buffer(sent_msg_buf, sent_msg_len);
	CHECK(pb_encode(&ostream, ts_crypto_GenerateKeyIn_fields, &sent_msg));
	CHECK_EQUAL(sent_msg_len, ostream.bytes_written);

	/* Receiver - deserialize and use values */
	ts_crypto_GenerateKeyIn recv_msg = ts_crypto_GenerateKeyIn_init_default;

	pb_istream_t istream = pb_istream_from_buffer(sent_msg_buf, sent_msg_len);
	CHECK_EQUAL(sent_msg_len, istream.bytes_left);
	CHECK(pb_decode(&istream, ts_crypto_GenerateKeyIn_fields, &recv_msg));
	CHECK_EQUAL(0, istream.bytes_left);

	free(sent_msg_buf);

	CHECK(recv_msg.has_attributes);
	CHECK_EQUAL(sent_key_attributes.type, recv_msg.attributes.type);
	CHECK_EQUAL(sent_key_attributes.key_bits, recv_msg.attributes.key_bits);
	CHECK_EQUAL(sent_key_attributes.lifetime, recv_msg.attributes.lifetime);
	CHECK_EQUAL(sent_key_attributes.id, recv_msg.attributes.id);

	CHECK(recv_msg.attributes.has_policy);
	CHECK_EQUAL(sent_key_attributes.policy.usage, recv_msg.attributes.policy.usage);
	CHECK_EQUAL(sent_key_attributes.policy.alg, recv_msg.attributes.policy.alg);

	if (print_info) {
		printf("Serialized op_export_public_key len: %ld\n", sent_msg_len);
	}
}

TEST(CryptoMsgTests, ExportPublicKeyInMsgTest) {
	/* Sender - set values and serialize */
	ts_crypto_ExportPublicKeyIn sent_msg = ts_crypto_ExportPublicKeyIn_init_default;

	sent_msg.handle = 55;

	size_t sent_msg_len;
	CHECK(pb_get_encoded_size(&sent_msg_len, ts_crypto_ExportPublicKeyIn_fields, &sent_msg));
	uint8_t *sent_msg_buf = (uint8_t*)malloc(sent_msg_len);
	CHECK(sent_msg_len > 0);
	CHECK(sent_msg_buf);

	pb_ostream_t ostream = pb_ostream_from_buffer(sent_msg_buf, sent_msg_len);
	CHECK(pb_encode(&ostream, ts_crypto_ExportPublicKeyIn_fields, &sent_msg));
	CHECK_EQUAL(sent_msg_len, ostream.bytes_written);

	/* Receiver - deserialize and use values */
	ts_crypto_ExportPublicKeyIn recv_msg = ts_crypto_ExportPublicKeyIn_init_default;

	pb_istream_t istream = pb_istream_from_buffer(sent_msg_buf, sent_msg_len);
	CHECK_EQUAL(sent_msg_len, istream.bytes_left);
	CHECK(pb_decode(&istream, ts_crypto_ExportPublicKeyIn_fields, &recv_msg));
	CHECK_EQUAL(0, istream.bytes_left);

	CHECK_EQUAL(sent_msg.handle, recv_msg.handle);

	free(sent_msg_buf);

	if (print_info) {
		printf("Serialized op_export_public_key len: %ld\n", sent_msg_len);
	}
}

TEST(CryptoMsgTests, ExportPublicKeyOutMsgTest) {
	/* Sender - set values and serialize */
	ts_crypto_ExportPublicKeyOut sent_msg = ts_crypto_ExportPublicKeyOut_init_default;

	PB_BYTES_ARRAY_T(5) example_key = {5, {0x31, 0x32, 0x33, 0x34, 0x35}};
	sent_msg.data = out_byte_array((const pb_bytes_array_t *)&example_key);

	size_t sent_msg_len;
	CHECK(pb_get_encoded_size(&sent_msg_len, ts_crypto_ExportPublicKeyOut_fields, &sent_msg));
	uint8_t *sent_msg_buf = (uint8_t*)malloc(sent_msg_len);
	CHECK(sent_msg_len > 0);
	CHECK(sent_msg_buf);

	pb_ostream_t ostream = pb_ostream_from_buffer(sent_msg_buf, sent_msg_len);
	CHECK(pb_encode(&ostream, ts_crypto_ExportPublicKeyOut_fields, &sent_msg));
	CHECK_EQUAL(sent_msg_len, ostream.bytes_written);

	/* Receiver - deserialize and use values */
	PB_BYTES_ARRAY_T(5) recv_key = {5, {0}};
	ts_crypto_ExportPublicKeyOut recv_msg = ts_crypto_ExportPublicKeyOut_init_default;
	recv_msg.data = in_byte_array((pb_bytes_array_t *)&recv_key);

	pb_istream_t istream = pb_istream_from_buffer(sent_msg_buf, sent_msg_len);
	CHECK_EQUAL(sent_msg_len, istream.bytes_left);
	CHECK(pb_decode(&istream, ts_crypto_ExportPublicKeyOut_fields, &recv_msg));
	CHECK_EQUAL(0, istream.bytes_left);

	free(sent_msg_buf);

	CHECK_EQUAL(example_key.size, recv_key.size);
	CHECK(memcmp(example_key.bytes, recv_key.bytes, example_key.size) == 0);

	if (print_info) {
		printf("Serialized result_export_public_key len: %ld\n", sent_msg_len);
	}
}

TEST(CryptoMsgTests, SignHashInMsgTest) {
	/* Sender - set values and serialize */
	ts_crypto_SignHashIn sent_msg = ts_crypto_SignHashIn_init_default;

	PB_BYTES_ARRAY_T(6) msg_to_sign = {6, {0x34, 0x32, 0x33, 0x34, 0x35, 0x36}};
	sent_msg.hash = out_byte_array((const pb_bytes_array_t *)&msg_to_sign);

	sent_msg.handle = 71;
    sent_msg.alg =
		ts_crypto_Alg_ALG_DETERMINISTIC_ECDSA_BASE |
		ts_crypto_Alg_ALG_SHA_256;

	size_t sent_msg_len;
	CHECK(pb_get_encoded_size(&sent_msg_len, ts_crypto_SignHashIn_fields, &sent_msg));
	uint8_t *sent_msg_buf = (uint8_t*)malloc(sent_msg_len);
	CHECK(sent_msg_len > 0);
	CHECK(sent_msg_buf);

	pb_ostream_t ostream = pb_ostream_from_buffer(sent_msg_buf, sent_msg_len);
	CHECK(pb_encode(&ostream, ts_crypto_SignHashIn_fields, &sent_msg));
	CHECK_EQUAL(sent_msg_len, ostream.bytes_written);

	/* Receiver - deserialize and use values */
	PB_BYTES_ARRAY_T(6) recv_msg_to_sign = {6, {0}};
	ts_crypto_SignHashIn recv_msg = ts_crypto_SignHashIn_init_default;
	recv_msg.hash = in_byte_array((pb_bytes_array_t *)&recv_msg_to_sign);

	pb_istream_t istream = pb_istream_from_buffer(sent_msg_buf, sent_msg_len);
	CHECK_EQUAL(sent_msg_len, istream.bytes_left);
	CHECK(pb_decode(&istream, ts_crypto_SignHashIn_fields, &recv_msg));
	CHECK_EQUAL(0, istream.bytes_left);

	free(sent_msg_buf);

	CHECK_EQUAL(sent_msg.handle, recv_msg.handle);
	CHECK_EQUAL(sent_msg.alg, recv_msg.alg);

	CHECK_EQUAL(msg_to_sign.size, recv_msg_to_sign.size);
	CHECK(memcmp(msg_to_sign.bytes, recv_msg_to_sign.bytes, msg_to_sign.size) == 0);

	if (print_info) {
		printf("Serialized op_asym_sign len: %ld\n", sent_msg_len);
	}
}

TEST(CryptoMsgTests, SignHashOutMsgTest) {
	/* Sender - set values and serialize */
	ts_crypto_SignHashOut sent_msg = ts_crypto_SignHashOut_init_default;

	PB_BYTES_ARRAY_T(10) example_signature = {10, {0x01, 0x02, 0x5a, 0x7c, 0x35, 0x01, 0x02, 0x5a, 0x7c, 0x35}};
	sent_msg.signature = out_byte_array((const pb_bytes_array_t *)&example_signature);

	size_t sent_msg_len;
	CHECK(pb_get_encoded_size(&sent_msg_len, ts_crypto_SignHashOut_fields, &sent_msg));
	uint8_t *sent_msg_buf = (uint8_t*)malloc(sent_msg_len);
	CHECK(sent_msg_len > 0);
	CHECK(sent_msg_buf);

	pb_ostream_t ostream = pb_ostream_from_buffer(sent_msg_buf, sent_msg_len);
	CHECK(pb_encode(&ostream, ts_crypto_SignHashOut_fields, &sent_msg));
	CHECK_EQUAL(sent_msg_len, ostream.bytes_written);

	/* Receiver - deserialize and use values */
	PB_BYTES_ARRAY_T(10) recv_signature = {10, {0}};
	ts_crypto_SignHashOut recv_msg = ts_crypto_SignHashOut_init_default;
	recv_msg.signature = in_byte_array((pb_bytes_array_t *)&recv_signature);

	pb_istream_t istream = pb_istream_from_buffer(sent_msg_buf, sent_msg_len);
	CHECK_EQUAL(sent_msg_len, istream.bytes_left);
	CHECK(pb_decode(&istream, ts_crypto_SignHashOut_fields, &recv_msg));
	CHECK_EQUAL(0, istream.bytes_left);

	free(sent_msg_buf);

	CHECK_EQUAL(example_signature.size, recv_signature.size);
	CHECK(memcmp(example_signature.bytes, recv_signature.bytes, example_signature.size) == 0);

	if (print_info) {
		printf("Serialized result_asym_sign len: %ld\n", sent_msg_len);
	}
}