aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/arch/aarch64/odp_random.h
blob: 023e6c45568e519c364b885b038b865c62b70195 (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
/* Copyright (c) 2021, ARM Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#ifndef ODP_AARCH64_RANDOM_H_
#define ODP_AARCH64_RANDOM_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <odp/api/spec/random.h>
#include <odp/autoheader_internal.h>

#include <stdint.h>

odp_random_kind_t _odp_random_max_kind_generic(void);
int32_t _odp_random_true_data_generic(uint8_t *buf, uint32_t len);
int32_t _odp_random_crypto_data_generic(uint8_t *buf, uint32_t len);

#ifdef __ARM_FEATURE_RNG

#if __ARM_FEATURE_UNALIGNED != 1
#error This implementation requires unaligned access
#endif

static inline int _odp_random_max_kind(void)
{
	return ODP_RANDOM_TRUE;
}

static inline int _odp_rndr(uint64_t *v)
{
	int pass;

	/* Return a 64-bit random number which is reseeded from the True Random
	 * Number source. If the hardware returns a genuine random number,
	 * PSTATE.NZCV is set to 0b0000. The NZCV condition flag is checked via
	 * the CSET instruction. If the hardware cannot return a genuine random
	 * number in a reasonable period of time, PSTATE.NZCV is set to 0b0100
	 * and the data value returned is 0. */
	__asm__ volatile("mrs   %0, s3_3_c2_c4_0\n\t"
			 "cset  %w[pass], ne"
			 : "=r" (*v), [pass] "=r" (pass)
			 :
			 : "cc");

	return pass;
}

static inline int _odp_rndrrs(uint64_t *v)
{
	int pass;

	/* Return a 64-bit random number which is reseeded from the True Random
	 * Number source immediately before the read of the random number.
	 * If the hardware returns a genuine random number, PSTATE.NZCV is
	 * set to 0b0000. The NZCV condition flag is checked via the CSET
	 * instruction. If the hardware cannot return a genuine random number
	 * in a reasonable period of time, PSTATE.NZCV is set to 0b0100 and the
	 * data value returned is 0. */
	__asm__ volatile("mrs   %0, s3_3_c2_c4_1\n\t"
			 "cset  %w[pass], ne"
			 : "=r" (*v), [pass] "=r" (pass)
			 :
			 : "cc");

	return pass;
}

static inline int32_t _odp_random_crypto_data(uint8_t *buf, uint32_t len)
{
	uint64_t temp;

	for (uint32_t i = 0; i < len / 8; i++) {
		while (!_odp_rndr(&temp))
			;

		*(uint64_t *)(uintptr_t)buf = temp;
		buf += 8;
	}

	if (len & 7) {
		while (!_odp_rndr(&temp))
			;

		if (len & 4) {
			*(uint32_t *)(uintptr_t)buf = temp & 0xffffffff;
			temp >>= 32;
			buf += 4;
		}

		if (len & 2) {
			*(uint16_t *)(uintptr_t)buf = temp & 0xffff;
			temp >>= 16;
			buf += 2;
		}

		if (len & 1)
			*buf = temp & 0xff;
	}

	return len;
}

static inline int32_t _odp_random_true_data(uint8_t *buf, uint32_t len)
{
	uint64_t temp;

	for (uint32_t i = 0; i < len / 8; i++) {
		while (!_odp_rndrrs(&temp))
			;

		*(uint64_t *)(uintptr_t)buf = temp;
		buf += 8;
	}

	if (len & 7) {
		while (!_odp_rndrrs(&temp))
			;

		if (len & 4) {
			*(uint32_t *)(uintptr_t)buf = temp & 0xffffffff;
			temp >>= 32;
			buf += 4;
		}

		if (len & 2) {
			*(uint16_t *)(uintptr_t)buf = temp & 0xffff;
			temp >>= 16;
			buf += 2;
		}

		if (len & 1)
			*buf = temp & 0xff;
	}

	return len;
}

#else

static inline int _odp_random_max_kind(void)
{
	return _odp_random_max_kind_generic();
}

static inline int32_t _odp_random_crypto_data(uint8_t *buf, uint32_t len)
{
	return _odp_random_crypto_data_generic(buf, len);
}

static inline int32_t _odp_random_true_data(uint8_t *buf, uint32_t len)
{
	return _odp_random_true_data_generic(buf, len);
}

#endif

#ifdef __cplusplus
}
#endif

#endif