blob: 7ce3b69f7d06396756c0e093d7051d842e18e7d8 [file] [log] [blame]
Andrey Pronin511fc2c2021-04-14 00:48:19 -07001
Howard Yang19fa7b12023-01-11 13:36:06 +08002/* Copyright 2021 The ChromiumOS Authors
Andrey Pronin511fc2c2021-04-14 00:48:19 -07003 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#ifndef __PINWEAVER_EAL_H
8#define __PINWEAVER_EAL_H
9
10#include <stddef.h>
Andrey Pronin04db55a2021-04-26 22:09:34 -070011
12#include "pinweaver.h"
13#include "pinweaver_eal_types.h"
Andrey Pronin511fc2c2021-04-14 00:48:19 -070014
15#ifndef PINWEAVER_EAL_INFO
16#define PINWEAVER_EAL_INFO(...)
17#endif
18
Andrey Pronin623f9062021-04-22 01:20:58 -070019#ifdef __cplusplus
20extern "C" {
21#endif
22
Howard Yangfad8b972022-08-05 16:58:33 +080023#ifndef BIOMETRICS_DEV
24#error BIOMETRICS_DEV needs to be defined.
25#endif
26
Andrey Pronin4d459372021-08-10 16:56:33 -070027/* Implements memcpy_s on all platforms
28 */
29int pinweaver_eal_memcpy_s(
30 void * dest,
31 size_t destsz,
32 const void * src,
33 size_t count
34);
35
Andrey Pronin511fc2c2021-04-14 00:48:19 -070036/*
Andrey Proninc2343a32021-05-15 04:17:06 -070037 * Derives wrap_key and hmac_key based on key_derivation_nonce.
38 * Returns 0 on success.
Andrey Pronin511fc2c2021-04-14 00:48:19 -070039 */
40int pinweaver_eal_derive_keys(struct merkle_tree_t *merkle_tree);
41
42/*
43 * Functions for calculating SHA-256.
Andrey Proninc2343a32021-05-15 04:17:06 -070044 * Returns 0 on success.
Andrey Pronin511fc2c2021-04-14 00:48:19 -070045 */
46int pinweaver_eal_sha256_init(pinweaver_eal_sha256_ctx_t *ctx);
47int pinweaver_eal_sha256_update(pinweaver_eal_sha256_ctx_t *ctx,
48 const void *data,
49 size_t size);
50int pinweaver_eal_sha256_final(pinweaver_eal_sha256_ctx_t *ctx,
51 void *res);
52
53/*
54 * Functions for calculating HMAC SHA-256.
55 * Only 256 bit key size is used.
Andrey Proninc2343a32021-05-15 04:17:06 -070056 * Returns 0 on success.
Andrey Pronin511fc2c2021-04-14 00:48:19 -070057 */
58int pinweaver_eal_hmac_sha256_init(pinweaver_eal_hmac_sha256_ctx_t *ctx,
59 const void *key,
60 size_t key_size /* in bytes */);
61int pinweaver_eal_hmac_sha256_update(pinweaver_eal_hmac_sha256_ctx_t *ctx,
62 const void *data,
63 size_t size);
64int pinweaver_eal_hmac_sha256_final(pinweaver_eal_hmac_sha256_ctx_t *ctx,
Andrey Pronin04db55a2021-04-26 22:09:34 -070065 void *res);
Andrey Pronin511fc2c2021-04-14 00:48:19 -070066
67/*
68 * Perform AES-256 CTR.
69 * Only 256 bit key size is used.
Andrey Proninc2343a32021-05-15 04:17:06 -070070 * Returns 0 on success.
Andrey Pronin511fc2c2021-04-14 00:48:19 -070071 */
72int pinweaver_eal_aes256_ctr(const void *key,
73 size_t key_size, /* in bytes */
74 const void *iv,
75 const void *data,
76 size_t size,
77 void *res);
78
79
80/*
81 * Constant time implementation of memcmp to avoid timing side channels.
82 */
83int pinweaver_eal_safe_memcmp(const void *s1, const void *s2, size_t len);
84
85/*
86 * Get random bytes.
Andrey Proninc2343a32021-05-15 04:17:06 -070087 * Returns 0 on success.
Andrey Pronin511fc2c2021-04-14 00:48:19 -070088 */
89int pinweaver_eal_rand_bytes(void *buf, size_t size);
90
91/*
92 * Get number of seconds since cold boot.
93 */
Edward Hillcb73fa72022-05-12 21:33:05 +000094uint64_t pinweaver_eal_seconds_since_boot(void);
Andrey Pronin511fc2c2021-04-14 00:48:19 -070095
96/*
Andrey Proninc2343a32021-05-15 04:17:06 -070097 * Functions for calculating SHA256 of the values of the selected PCRs.
98 * Returns 0 on success.
Andrey Pronin511fc2c2021-04-14 00:48:19 -070099 */
100uint8_t pinweaver_eal_get_current_pcr_digest(
101 const uint8_t bitmask[2],
102 uint8_t sha256_of_selected_pcr[32]);
103
104/*
105 * Storage functions.
Andrey Proninc2343a32021-05-15 04:17:06 -0700106 * Return 0 on success.
Andrey Pronin511fc2c2021-04-14 00:48:19 -0700107 */
Edward Hillcb73fa72022-05-12 21:33:05 +0000108int pinweaver_eal_storage_start(void);
Leo Lai7f598a52021-08-11 00:28:52 +0800109int pinweaver_eal_storage_init_state(uint8_t root_hash[PW_HASH_SIZE],
Andrey Pronin511fc2c2021-04-14 00:48:19 -0700110 uint32_t *restart_count);
111
112int pinweaver_eal_storage_get_log(struct pw_log_storage_t *dest);
113int pinweaver_eal_storage_set_log(const struct pw_log_storage_t *log);
114
115int pinweaver_eal_storage_get_tree_data(struct pw_long_term_storage_t *dest);
116int pinweaver_eal_storage_set_tree_data(
117 const struct pw_long_term_storage_t *data);
118
Howard Yangfad8b972022-08-05 16:58:33 +0800119#if BIOMETRICS_DEV
120
121/* Biometrics vendor functions. */
122
123/*
124 * Load the Pk of the specified auth channel.
Howard Yang19fa7b12023-01-11 13:36:06 +0800125 * The pk should be valid when status is return code is 0.
Howard Yangfad8b972022-08-05 16:58:33 +0800126 * Returns 0 on success.
Howard Yang19fa7b12023-01-11 13:36:06 +0800127 * Returns PW_ERR_BIO_AUTH_PK_NOT_ESTABLISHED when the Pk is not established.
Howard Yangfad8b972022-08-05 16:58:33 +0800128 */
Howard Yang19fa7b12023-01-11 13:36:06 +0800129int pinweaver_eal_storage_get_ba_pk(uint8_t auth_channel,
Howard Yangfad8b972022-08-05 16:58:33 +0800130 struct pw_ba_pk_t *pk);
131
132/*
133 * Set the Pk of the specified auth channel.
134 * Returns 0 on success.
135 */
Howard Yang19fa7b12023-01-11 13:36:06 +0800136int pinweaver_eal_storage_set_ba_pk(uint8_t auth_channel,
Howard Yangfad8b972022-08-05 16:58:33 +0800137 const struct pw_ba_pk_t *pk);
138
139/*
140 * Derive a ECC key pair, perform ECDH exchange with the |ecc_pt_in| public
141 * point, set |secret| as the shared secret, and set |ecc_pt_out| as the
142 * derived ECC key pair's public point.
143 */
144int pinweaver_eal_ecdh_derive(const struct pw_ba_ecc_pt_t *ecc_pt_in,
145 void *secret, size_t *secret_size,
146 struct pw_ba_ecc_pt_t *ecc_pt_out);
147
148#endif
149
Andrey Pronin623f9062021-04-22 01:20:58 -0700150#ifdef __cplusplus
151}
152#endif
153
Andrey Pronin511fc2c2021-04-14 00:48:19 -0700154#endif /* __PINWEAVER_EAL_H */