2 * Copyright (c) 2013-2015 TRUSTONIC LIMITED
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef __KM_SHARED_UTIL_H__
33 #define __KM_SHARED_UTIL_H__
35 #include "hardware/keymaster_defs.h"
37 #define BITS_TO_BYTES(n) (((n)+7)/8)
40 #define MIN(a,b) ((a) < (b) ? (a) : (b))
44 * Read a serialized little-endian encoding of a uint32_t.
46 * @param pos position to read from
53 * Read a serialized little-endian encoding of a uint64_t.
55 * @param pos position to read from
62 * Write a serialized little-endian encoding of a uint32_t.
64 * @param pos position to write to
72 * Write a serialized little-endian encoding of a uint64_t.
74 * @param pos position to write to
82 * Write a serialized little-endian encoding of a uint32_t and increment the
83 * position by 4 bytes.
85 * @param pos pointer to position to write to
88 void set_u32_increment_pos(
93 * Write a serialized little-endian encoding of a uint64_t and increment the
94 * position by 8 bytes.
96 * @param pos pointer to position to write to
99 void set_u64_increment_pos(
104 * Write data and increment position by length of data.
106 * @param pos pointer to position to write to
107 * @param src buffer to write
108 * @param len length of buffer
110 void set_data_increment_pos(
116 * Set a pointer and increment position.
118 * @param ptr pointer to pointer to set
119 * @param src pointer to position to set it to
120 * @param len length by which to increment \p *src
122 void set_ptr_increment_src(
128 * Check consistency of parameters.
129 * @param algorithm key type
130 * @param purpose operation purpose
131 * @return whether \p algorithm and \p purpose are consistent
133 bool check_algorithm_purpose(
134 keymaster_algorithm_t algorithm
,
135 keymaster_purpose_t purpose
);
138 * Memory needed to store a set of (HW- or SW-enforced) characteristics, n32 of
139 * which are uint32_t (enum, uint, bool) and n64 of which are uint64_t (ulong,
142 #define KM_W_CHARACTERISTICS_SIZE(n32,n64) (4 + (4 + 4)*(n32) + (4 + 8)*(n64))
144 /* Hardware-enforced characteristics:
145 * KM_TAG_PURPOSE, // enum, uint32_t, up to 4 of these
146 * KM_TAG_ALGORITHM, // enum, uint32_t
147 * KM_TAG_KEY_SIZE, // uint, uint32_t
148 * KM_TAG_BLOCK_MODE, // enum, uint32_t, up to 4 of these
149 * KM_TAG_DIGEST, // enum, uint32_t, up to 7 of these
150 * KM_TAG_PADDING, // enum, uint32_t, up to 5 of these
151 * KM_TAG_RSA_PUBLIC_EXPONENT, // ulong, uint64_t
152 * KM_TAG_BLOB_USAGE_REQUIREMENTS, // enum, uint32_t
153 * KM_TAG_BOOTLOADER_ONLY, // bool, uint32_t
154 * KM_TAG_ORIGIN, // enum, uint32_t
155 * KM_TAG_ROLLBACK_RESISTANT, // bool, uint32_t
156 * KM_TAG_USER_SECURE_ID, // uint64_t, up to ? of these
157 * KM_TAG_NO_AUTH_REQUIRED, // bool, uint32_t
158 * KM_TAG_USER_AUTH_TYPE, // enum, uint32_t
159 * KM_TAG_AUTH_TIMEOUT, // uint, uint32_t
160 * KM_TAG_CALLER_NONCE, // bool, uint32_t
161 * KM_TAG_MIN_MAC_LENGTH, // uint, uint32_t
163 #define KM_MAX_N_USER_SECURE_ID 8 // arbitrary
164 #define KM_N_HW_32 31
165 #define KM_N_HW_64 (1 + KM_MAX_N_USER_SECURE_ID)
167 /* Software-enforced characteristics: (set and enforced by keystore)
168 * KM_TAG_ACTIVE_DATETIME, // date, uint64_t
169 * KM_TAG_CREATION_DATETIME, // date, uint64_t
170 * KM_TAG_MAX_USES_PER_BOOT, // uint, uint32_t
171 * KM_TAG_MIN_SECONDS_BETWEEN_OPS, // uint, uint32_t
172 * KM_TAG_ORIGINATION_EXPIRE_DATETIME, // date, uint64_t
173 * KM_TAG_USAGE_EXPIRE_DATETIME, // date, uint64_t
178 #define KM_N_HW_CHARACTERISTICS (KM_N_HW_32 + KM_N_HW_64)
179 #define KM_N_SW_CHARACTERISTICS (KM_N_SW_32 + KM_N_SW_64)
180 #define KM_HW_CHARACTERISTICS_SIZE \
181 KM_W_CHARACTERISTICS_SIZE(KM_N_HW_32, KM_N_HW_64)
182 #define KM_SW_CHARACTERISTICS_SIZE \
183 KM_W_CHARACTERISTICS_SIZE(KM_N_SW_32, KM_N_SW_64)
186 * Maximum amount of memory needed for serialized key characteristics.
188 #define KM_CHARACTERISTICS_SIZE \
189 (KM_HW_CHARACTERISTICS_SIZE + KM_SW_CHARACTERISTICS_SIZE)
191 /* Nuber of parameters added by default on key import or key generation */
192 #define OWN_PARAMS_NB 3
194 /* Size of an array storing default parameters */
195 #define OWN_PARAMS_SIZE ( OWN_PARAMS_NB * (4+4) ) // OWN_PARAMS_NB x (tag + (enum or bool))
198 * Size of out_params buffer when required for begin() operation.
200 * This is enough to hold a 16-byte IV field, serialized
201 * (param_count | tag | blob_length | blob_data).
203 #define TEE_BEGIN_OUT_PARAMS_SIZE (4 + 4 + 4 + 16)
205 #endif /* __KM_SHARED_UTIL_H__ */