blob: 3c19230bd616860b091c58dd280f8c44247c4e07 [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
Max Shvetsov06dba292019-12-06 11:50:12 +00002 * Copyright (c) 2015-2020, 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
Max Shvetsov06dba292019-12-06 11:50:12 +000011#include <common/debug.h>
12#include <drivers/arm/cryptocell/cc_rotpk.h>
13#include <drivers/delay_timer.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <lib/cassert.h>
Max Shvetsov06dba292019-12-06 11:50:12 +000015#include <plat/arm/common/plat_arm.h>
16#include <plat/common/common_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <plat/common/platform.h>
Antonio Nino Diaza320ecd2019-01-15 14:19:50 +000018#include <platform_def.h>
Max Shvetsov06dba292019-12-06 11:50:12 +000019#include <tools_share/tbbr_oid.h>
Juan Castillo31a68f02015-04-14 12:49:03 +010020
Juan Castillo31a68f02015-04-14 12:49:03 +010021
Soby Mathew3e6bbda2017-06-02 17:44:07 +010022#if !ARM_CRYPTOCELL_INTEG
23#if !ARM_ROTPK_LOCATION_ID
24 #error "ARM_ROTPK_LOCATION_ID not defined"
25#endif
Max Shvetsov06dba292019-12-06 11:50:12 +000026#endif
Soby Mathew3e6bbda2017-06-02 17:44:07 +010027
28/* Weak definition may be overridden in specific platform */
29#pragma weak plat_get_nv_ctr
30#pragma weak plat_set_nv_ctr
31
Max Shvetsov06dba292019-12-06 11:50:12 +000032extern unsigned char arm_rotpk_header[], arm_rotpk_hash_end[];
33
34static unsigned char rotpk_hash_der[ARM_ROTPK_HEADER_LEN + ARM_ROTPK_HASH_LEN];
Juan Castillo31a68f02015-04-14 12:49:03 +010035
Dan Handley9df48042015-03-19 18:58:55 +000036/*
Max Shvetsov06dba292019-12-06 11:50:12 +000037 * Return the ROTPK hash stored in dedicated registers.
Juan Castillo31a68f02015-04-14 12:49:03 +010038 */
Max Shvetsov06dba292019-12-06 11:50:12 +000039int arm_get_rotpk_info_regs(void **key_ptr, unsigned int *key_len,
Juan Castillo31a68f02015-04-14 12:49:03 +010040 unsigned int *flags)
41{
42 uint8_t *dst;
Max Shvetsov06dba292019-12-06 11:50:12 +000043 uint32_t *src, tmp;
44 unsigned int words, i;
Juan Castillo31a68f02015-04-14 12:49:03 +010045
46 assert(key_ptr != NULL);
47 assert(key_len != NULL);
48 assert(flags != NULL);
49
50 /* Copy the DER header */
Juan Castillo31a68f02015-04-14 12:49:03 +010051
Max Shvetsov06dba292019-12-06 11:50:12 +000052 memcpy(rotpk_hash_der, arm_rotpk_header, ARM_ROTPK_HEADER_LEN);
53 dst = (uint8_t *)&rotpk_hash_der[ARM_ROTPK_HEADER_LEN];
Juan Castillo31a68f02015-04-14 12:49:03 +010054
Max Shvetsov06dba292019-12-06 11:50:12 +000055 words = ARM_ROTPK_HASH_LEN >> 2;
Juan Castillo31a68f02015-04-14 12:49:03 +010056
Juan Castillo31a68f02015-04-14 12:49:03 +010057 src = (uint32_t *)TZ_PUB_KEY_HASH_BASE;
58 for (i = 0 ; i < words ; i++) {
59 tmp = src[words - 1 - i];
60 /* Words are read in little endian */
Juan Castillo31a68f02015-04-14 12:49:03 +010061 *dst++ = (uint8_t)(tmp & 0xFF);
Juan Castillo31a68f02015-04-14 12:49:03 +010062 *dst++ = (uint8_t)((tmp >> 8) & 0xFF);
Max Shvetsov06dba292019-12-06 11:50:12 +000063 *dst++ = (uint8_t)((tmp >> 16) & 0xFF);
64 *dst++ = (uint8_t)((tmp >> 24) & 0xFF);
Juan Castillo31a68f02015-04-14 12:49:03 +010065 }
Juan Castillo31a68f02015-04-14 12:49:03 +010066
67 *key_ptr = (void *)rotpk_hash_der;
68 *key_len = (unsigned int)sizeof(rotpk_hash_der);
69 *flags = ROTPK_IS_HASH;
70 return 0;
71}
72
Max Shvetsov06dba292019-12-06 11:50:12 +000073#if (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_RSA_ID) || \
74 (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_ECDSA_ID)
75/*
76 * Return development ROTPK hash generated from ROT_KEY.
77 */
78int arm_get_rotpk_info_dev(void **key_ptr, unsigned int *key_len,
79 unsigned int *flags)
80{
81 *key_ptr = arm_rotpk_header;
82 *key_len = arm_rotpk_hash_end - arm_rotpk_header;
83 *flags = ROTPK_IS_HASH;
84 return 0;
85}
86#endif
87
88#if ARM_CRYPTOCELL_INTEG
89/*
90 * Return ROTPK hash from CryptoCell.
91 */
92int arm_get_rotpk_info_cc(void **key_ptr, unsigned int *key_len,
93 unsigned int *flags)
94{
95 unsigned char *dst;
96
97 assert(key_ptr != NULL);
98 assert(key_len != NULL);
99 assert(flags != NULL);
100
101 /* Copy the DER header */
102 memcpy(rotpk_hash_der, arm_rotpk_header, ARM_ROTPK_HEADER_LEN);
103 dst = &rotpk_hash_der[ARM_ROTPK_HEADER_LEN];
104 *key_ptr = rotpk_hash_der;
105 *key_len = sizeof(rotpk_hash_der);
106 return cc_get_rotpk_hash(dst, ARM_ROTPK_HASH_LEN, flags);
107}
108#endif
109
110/*
111 * Wraper function for most Arm platforms to get ROTPK hash.
112 */
113int arm_get_rotpk_info(void **key_ptr, unsigned int *key_len,
114 unsigned int *flags)
115{
116#if ARM_CRYPTOCELL_INTEG
117 return arm_get_rotpk_info_cc(key_ptr, key_len, flags);
118#else
119
120#if (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_RSA_ID) || \
121 (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_ECDSA_ID)
122 return arm_get_rotpk_info_dev(key_ptr, key_len, flags);
123#elif (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_REGS_ID)
124 return arm_get_rotpk_info_regs(key_ptr, key_len, flags);
125#else
126 return 1;
127#endif
128
129#endif /* ARM_CRYPTOCELL_INTEG */
130}
131
Juan Castillobfb7fa62016-01-22 11:05:57 +0000132/*
133 * Return the non-volatile counter value stored in the platform. The cookie
134 * will contain the OID of the counter in the certificate.
135 *
136 * Return: 0 = success, Otherwise = error
137 */
138int plat_get_nv_ctr(void *cookie, unsigned int *nv_ctr)
139{
140 const char *oid;
141 uint32_t *nv_ctr_addr;
142
143 assert(cookie != NULL);
144 assert(nv_ctr != NULL);
145
146 oid = (const char *)cookie;
147 if (strcmp(oid, TRUSTED_FW_NVCOUNTER_OID) == 0) {
148 nv_ctr_addr = (uint32_t *)TFW_NVCTR_BASE;
149 } else if (strcmp(oid, NON_TRUSTED_FW_NVCOUNTER_OID) == 0) {
150 nv_ctr_addr = (uint32_t *)NTFW_CTR_BASE;
151 } else {
152 return 1;
153 }
154
155 *nv_ctr = (unsigned int)(*nv_ctr_addr);
156
157 return 0;
158}
159
160/*
Antonio Nino Diaz9d602fe2016-05-20 14:14:16 +0100161 * Store a new non-volatile counter value. By default on ARM development
162 * platforms, the non-volatile counters are RO and cannot be modified. We expect
163 * the values in the certificates to always match the RO values so that this
164 * function is never called.
Juan Castillobfb7fa62016-01-22 11:05:57 +0000165 *
166 * Return: 0 = success, Otherwise = error
167 */
168int plat_set_nv_ctr(void *cookie, unsigned int nv_ctr)
169{
170 return 1;
171}