aboutsummaryrefslogtreecommitdiff
path: root/tftf/tests/runtime_services/standard_service/trng/api_tests/test_trng.c
blob: 64b8db78fe77b9a06119c8795a9024a07a0d8cab (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
/*
 * Copyright (c) 2021, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <arch_helpers.h>
#include <assert.h>
#include <debug.h>
#include <platform.h>
#include <psci.h>
#include <smccc.h>
#include <string.h>
#include <errno.h>
#include <tftf_lib.h>
#include <trng.h>

/*
 * @Test_Aim@ exercise TRNG Version SMC.
 *
 * This test should exercise the trng version call. Versions before 1.0 don't
 * exsit, so should fail. This should also skip if TRNG is not implemented.
 */
test_result_t test_trng_version(void)
{
	int32_t version = tftf_trng_version();

	if (version == TRNG_E_NOT_SUPPORTED) {
		return TEST_RESULT_SKIPPED;
	}


	if (version < TRNG_VERSION(1, 0)) {
		return TEST_RESULT_FAIL;
	}

	return TEST_RESULT_SUCCESS;
}

/*
 * @Test_Aim@ Verify that TRNG reports implemented functions.
 *
 * Check that TRNG Features reports that all TRNG functions
 * are implemented.
 */
test_result_t test_trng_features(void)
{
	int32_t version = tftf_trng_version();

	if (version == TRNG_E_NOT_SUPPORTED) {
		return TEST_RESULT_SKIPPED;
	}

	if (!(tftf_trng_feature_implemented(SMC_TRNG_VERSION) &&
	      tftf_trng_feature_implemented(SMC_TRNG_FEATURES) &&
	      tftf_trng_feature_implemented(SMC_TRNG_UUID) &&
	      tftf_trng_feature_implemented(SMC_TRNG_RND))) {
		return TEST_RESULT_FAIL;
	}

	return TEST_RESULT_SUCCESS;
}

/*
 * @Test_Aim@ TRNG_RND Meets the Zero-fill requirements of the spec
 */
test_result_t test_trng_rnd(void)
{
	smc_ret_values rnd_out;
	size_t msb_shift = (TRNG_MAX_BITS/3) - U(1);

	int32_t version = tftf_trng_version();

	if (version == TRNG_E_NOT_SUPPORTED) {
		return TEST_RESULT_SKIPPED;
	}

	/* Test invalid entropy sizes */
	rnd_out = tftf_trng_rnd(U(0));
	if (rnd_out.ret0 != TRNG_E_INVALID_PARAMS) {
		ERROR("RND 0 returned 0x%lx\n", rnd_out.ret0);
		return TEST_RESULT_FAIL;
	}

	rnd_out = tftf_trng_rnd(TRNG_MAX_BITS + U(1));
	if (rnd_out.ret0 != TRNG_E_INVALID_PARAMS) {
		ERROR("RND 0x%x returned 0x%lx\n",  TRNG_MAX_BITS + U(1),
				rnd_out.ret0);
		return TEST_RESULT_FAIL;
	}

	/* Test valid corner cases.
	 * Here we expect the bits in Entropy[MAX_BITS-1:N]
	 * to be 0, where N is the requested number of bits
	 * of entropy */

	/* For N = 1, all returned entropy bits should be 0
	 * except the least significant bit */
	rnd_out = tftf_trng_rnd(U(1));
	if (rnd_out.ret0 == TRNG_E_NO_ENTOPY) {
		WARN("There is not a single bit of entropy\n");
		return TEST_RESULT_SKIPPED;
	}
	if ((rnd_out.ret1 & TRNG_ENTROPY_MASK) != 0) {
		ERROR("non-zero r1 value 0x%lx\n", rnd_out.ret1);
		return TEST_RESULT_FAIL;
	}
	if ((rnd_out.ret2 & TRNG_ENTROPY_MASK) != 0) {
		ERROR("non-zero r2 value 0x%lx\n", rnd_out.ret2);
		return TEST_RESULT_FAIL;
	}
	if ((rnd_out.ret3 & (TRNG_ENTROPY_MASK - U(1))) != 0) {
		ERROR("Unexpected r3 value 0x%lx\n", rnd_out.ret3);
		return TEST_RESULT_FAIL;
	}

	/* For N = MAX_BITS-1, the most significant bit should be 0 */
	rnd_out = tftf_trng_rnd(TRNG_MAX_BITS - U(1));
	if (rnd_out.ret0 == TRNG_E_NO_ENTOPY) {
		WARN("There is not a single bit of entropy\n");
		return TEST_RESULT_SKIPPED;
	}
	if ((rnd_out.ret1 & (1 << msb_shift)) != 0) {
		ERROR("Unexpected r1 value 0x%lx\n", rnd_out.ret1);
		return TEST_RESULT_FAIL;
	}
	return TEST_RESULT_SUCCESS;
}