blob: c71e932a0f32432890e2a7550d9b45d49d54139e [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
Gilad Ben-Yosseffbb88a32019-05-14 10:48:18 +03002 * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
Dan Handley9df48042015-03-19 18:58:55 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Dan Handley9df48042015-03-19 18:58:55 +00005 */
6
Juan Castillo31a68f02015-04-14 12:49:03 +01007#include <assert.h>
Juan Castillo31a68f02015-04-14 12:49:03 +01008#include <stdint.h>
9#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
11#include <lib/cassert.h>
12#include <plat/common/platform.h>
13#include <tools_share/tbbr_oid.h>
Antonio Nino Diaza320ecd2019-01-15 14:19:50 +000014#include <platform_def.h>
Juan Castillo31a68f02015-04-14 12:49:03 +010015
Juan Castillo31a68f02015-04-14 12:49:03 +010016/* SHA256 algorithm */
17#define SHA256_BYTES 32
18
19/* ROTPK locations */
20#define ARM_ROTPK_REGS_ID 1
21#define ARM_ROTPK_DEVEL_RSA_ID 2
Qixiang Xu1c2aef12017-08-24 15:12:20 +080022#define ARM_ROTPK_DEVEL_ECDSA_ID 3
Juan Castillo31a68f02015-04-14 12:49:03 +010023
Juan Castillo31a68f02015-04-14 12:49:03 +010024static const unsigned char rotpk_hash_hdr[] = \
25 "\x30\x31\x30\x0D\x06\x09\x60\x86\x48" \
26 "\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20";
27static const unsigned int rotpk_hash_hdr_len = sizeof(rotpk_hash_hdr) - 1;
28static unsigned char rotpk_hash_der[sizeof(rotpk_hash_hdr) - 1 + SHA256_BYTES];
29
Soby Mathew3e6bbda2017-06-02 17:44:07 +010030/* Use the cryptocell variants if Cryptocell is present */
31#if !ARM_CRYPTOCELL_INTEG
32#if !ARM_ROTPK_LOCATION_ID
33 #error "ARM_ROTPK_LOCATION_ID not defined"
34#endif
35
36/* Weak definition may be overridden in specific platform */
37#pragma weak plat_get_nv_ctr
38#pragma weak plat_set_nv_ctr
39
Juan Castillo31a68f02015-04-14 12:49:03 +010040#if (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_RSA_ID)
41static const unsigned char arm_devel_rotpk_hash[] = \
42 "\xB0\xF3\x82\x09\x12\x97\xD8\x3A" \
43 "\x37\x7A\x72\x47\x1B\xEC\x32\x73" \
44 "\xE9\x92\x32\xE2\x49\x59\xF6\x5E" \
45 "\x8B\x4A\x4A\x46\xD8\x22\x9A\xDA";
Qixiang Xu1c2aef12017-08-24 15:12:20 +080046#elif (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_ECDSA_ID)
47static const unsigned char arm_devel_rotpk_hash[] = \
48 "\x2E\x40\xBF\x6E\xF9\x12\xBB\x98" \
49 "\x31\x71\x09\x0E\x1E\x15\x3D\x0B" \
50 "\xFD\xD1\xCC\x69\x4A\x98\xEB\x8B" \
51 "\xA0\xB0\x20\x86\x4E\x6C\x07\x17";
Juan Castillo31a68f02015-04-14 12:49:03 +010052#endif
53
Dan Handley9df48042015-03-19 18:58:55 +000054/*
Juan Castillo31a68f02015-04-14 12:49:03 +010055 * Return the ROTPK hash in the following ASN.1 structure in DER format:
56 *
57 * AlgorithmIdentifier ::= SEQUENCE {
58 * algorithm OBJECT IDENTIFIER,
59 * parameters ANY DEFINED BY algorithm OPTIONAL
60 * }
61 *
62 * DigestInfo ::= SEQUENCE {
63 * digestAlgorithm AlgorithmIdentifier,
64 * digest OCTET STRING
65 * }
66 */
67int plat_get_rotpk_info(void *cookie, void **key_ptr, unsigned int *key_len,
68 unsigned int *flags)
69{
70 uint8_t *dst;
71
72 assert(key_ptr != NULL);
73 assert(key_len != NULL);
74 assert(flags != NULL);
75
76 /* Copy the DER header */
77 memcpy(rotpk_hash_der, rotpk_hash_hdr, rotpk_hash_hdr_len);
78 dst = (uint8_t *)&rotpk_hash_der[rotpk_hash_hdr_len];
79
Qixiang Xu1c2aef12017-08-24 15:12:20 +080080#if (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_RSA_ID) \
81 || (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_ECDSA_ID)
Juan Castillo31a68f02015-04-14 12:49:03 +010082 memcpy(dst, arm_devel_rotpk_hash, SHA256_BYTES);
83#elif (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_REGS_ID)
84 uint32_t *src, tmp;
85 unsigned int words, i;
86
87 /*
88 * Append the hash from Trusted Root-Key Storage registers. The hash has
89 * not been written linearly into the registers, so we have to do a bit
90 * of byte swapping:
91 *
92 * 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C
93 * +---------------------------------------------------------------+
94 * | Reg0 | Reg1 | Reg2 | Reg3 | Reg4 | Reg5 | Reg6 | Reg7 |
95 * +---------------------------------------------------------------+
96 * | ... ... | | ... ... |
97 * | +--------------------+ | +-------+
98 * | | | |
99 * +----------------------------+ +----------------------------+
100 * | | | |
101 * +-------+ | +--------------------+ |
102 * | | | |
103 * v v v v
104 * +---------------------------------------------------------------+
105 * | | |
106 * +---------------------------------------------------------------+
107 * 0 15 16 31
108 *
109 * Additionally, we have to access the registers in 32-bit words
110 */
111 words = SHA256_BYTES >> 3;
112
113 /* Swap bytes 0-15 (first four registers) */
114 src = (uint32_t *)TZ_PUB_KEY_HASH_BASE;
115 for (i = 0 ; i < words ; i++) {
116 tmp = src[words - 1 - i];
117 /* Words are read in little endian */
118 *dst++ = (uint8_t)((tmp >> 24) & 0xFF);
119 *dst++ = (uint8_t)((tmp >> 16) & 0xFF);
120 *dst++ = (uint8_t)((tmp >> 8) & 0xFF);
121 *dst++ = (uint8_t)(tmp & 0xFF);
122 }
123
124 /* Swap bytes 16-31 (last four registers) */
125 src = (uint32_t *)(TZ_PUB_KEY_HASH_BASE + SHA256_BYTES / 2);
126 for (i = 0 ; i < words ; i++) {
127 tmp = src[words - 1 - i];
128 *dst++ = (uint8_t)((tmp >> 24) & 0xFF);
129 *dst++ = (uint8_t)((tmp >> 16) & 0xFF);
130 *dst++ = (uint8_t)((tmp >> 8) & 0xFF);
131 *dst++ = (uint8_t)(tmp & 0xFF);
132 }
Qixiang Xu1c2aef12017-08-24 15:12:20 +0800133#endif /* (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_RSA_ID) \
134 || (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_ECDSA_ID) */
Juan Castillo31a68f02015-04-14 12:49:03 +0100135
136 *key_ptr = (void *)rotpk_hash_der;
137 *key_len = (unsigned int)sizeof(rotpk_hash_der);
138 *flags = ROTPK_IS_HASH;
139 return 0;
140}
141
Juan Castillobfb7fa62016-01-22 11:05:57 +0000142/*
143 * Return the non-volatile counter value stored in the platform. The cookie
144 * will contain the OID of the counter in the certificate.
145 *
146 * Return: 0 = success, Otherwise = error
147 */
148int plat_get_nv_ctr(void *cookie, unsigned int *nv_ctr)
149{
150 const char *oid;
151 uint32_t *nv_ctr_addr;
152
153 assert(cookie != NULL);
154 assert(nv_ctr != NULL);
155
156 oid = (const char *)cookie;
157 if (strcmp(oid, TRUSTED_FW_NVCOUNTER_OID) == 0) {
158 nv_ctr_addr = (uint32_t *)TFW_NVCTR_BASE;
159 } else if (strcmp(oid, NON_TRUSTED_FW_NVCOUNTER_OID) == 0) {
160 nv_ctr_addr = (uint32_t *)NTFW_CTR_BASE;
161 } else {
162 return 1;
163 }
164
165 *nv_ctr = (unsigned int)(*nv_ctr_addr);
166
167 return 0;
168}
169
170/*
Antonio Nino Diaz9d602fe2016-05-20 14:14:16 +0100171 * Store a new non-volatile counter value. By default on ARM development
172 * platforms, the non-volatile counters are RO and cannot be modified. We expect
173 * the values in the certificates to always match the RO values so that this
174 * function is never called.
Juan Castillobfb7fa62016-01-22 11:05:57 +0000175 *
176 * Return: 0 = success, Otherwise = error
177 */
178int plat_set_nv_ctr(void *cookie, unsigned int nv_ctr)
179{
180 return 1;
181}
Soby Mathew3e6bbda2017-06-02 17:44:07 +0100182#else /* ARM_CRYPTOCELL_INTEG */
183
Gilad Ben-Yosseffbb88a32019-05-14 10:48:18 +0300184#include <drivers/arm/cryptocell/cc_rotpk.h>
Soby Mathew3e6bbda2017-06-02 17:44:07 +0100185
186/*
187 * Return the ROTPK hash in the following ASN.1 structure in DER format:
188 *
189 * AlgorithmIdentifier ::= SEQUENCE {
190 * algorithm OBJECT IDENTIFIER,
191 * parameters ANY DEFINED BY algorithm OPTIONAL
192 * }
193 *
194 * DigestInfo ::= SEQUENCE {
195 * digestAlgorithm AlgorithmIdentifier,
196 * digest OCTET STRING
197 * }
198 */
199int plat_get_rotpk_info(void *cookie, void **key_ptr, unsigned int *key_len,
200 unsigned int *flags)
201{
202 unsigned char *dst;
Soby Mathew3e6bbda2017-06-02 17:44:07 +0100203
204 assert(key_ptr != NULL);
205 assert(key_len != NULL);
206 assert(flags != NULL);
207
Soby Mathew3e6bbda2017-06-02 17:44:07 +0100208 /* Copy the DER header */
209 memcpy(rotpk_hash_der, rotpk_hash_hdr, rotpk_hash_hdr_len);
210 dst = &rotpk_hash_der[rotpk_hash_hdr_len];
Soby Mathew3e6bbda2017-06-02 17:44:07 +0100211 *key_ptr = rotpk_hash_der;
212 *key_len = sizeof(rotpk_hash_der);
Gilad Ben-Yosseffbb88a32019-05-14 10:48:18 +0300213 return cc_get_rotpk_hash(dst, SHA256_BYTES, flags);
Soby Mathew3e6bbda2017-06-02 17:44:07 +0100214}
Soby Mathew3e6bbda2017-06-02 17:44:07 +0100215#endif /* ARM_CRYPTOCELL_INTEG */