1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (C) Foundries Ltd. 2020 - All Rights Reserved
4  * Author: Jorge Ramirez <jorge@foundries.io>
5  */
6 
7 #ifndef SE050_UTILS_H_
8 #define SE050_UTILS_H_
9 
10 #include <se050.h>
11 #include <tee_api_types.h>
12 
13 #define SE050_SCP03_KEY_SZ 16
14 
15 struct se050_scp_key {
16 	uint8_t enc[SE050_SCP03_KEY_SZ];
17 	uint8_t mac[SE050_SCP03_KEY_SZ];
18 	uint8_t dek[SE050_SCP03_KEY_SZ];
19 };
20 
21 struct s050_scp_rotate_cmd {
22 	uint8_t cmd[128];
23 	size_t cmd_len;
24 	uint8_t kcv[16];
25 	size_t kcv_len;
26 };
27 
28 #define OID_MIN			((uint32_t)(0x00000001))
29 #define OID_MAX			((uint32_t)(OID_MIN + 0x7BFFFFFE))
30 
31 #define SE050_KEY_WATERMARK	0x57721566
32 #define WATERMARKED(x)	\
33 	((uint64_t)(((uint64_t)SE050_KEY_WATERMARK) << 32) + (x))
34 
35 sss_status_t se050_get_oid(uint32_t *val);
36 
37 struct rsa_keypair;
38 struct ecc_keypair;
39 
40 uint32_t se050_rsa_keypair_from_nvm(struct rsa_keypair *key);
41 uint32_t se050_ecc_keypair_from_nvm(struct ecc_keypair *key);
42 uint64_t se050_generate_private_key(uint32_t oid);
43 
44 void se050_refcount_init_ctx(uint8_t **cnt);
45 int se050_refcount_final_ctx(uint8_t *cnt);
46 
47 sss_status_t se050_get_se_info(sss_se05x_session_t *session, bool display);
48 
49 enum se050_scp03_ksrc { SCP03_CFG, SCP03_DERIVED, SCP03_OFID };
50 void se050_scp03_set_enable(enum se050_scp03_ksrc ksrc);
51 void se050_scp03_set_disable(void);
52 bool se050_scp03_enabled(void);
53 sss_status_t se050_scp03_get_current_keys(struct se050_scp_key *keys);
54 sss_status_t se050_scp03_get_keys(struct se050_scp_key *keys,
55 				  enum se050_scp03_ksrc);
56 sss_status_t se050_scp03_subkey_derive(struct se050_scp_key *keys);
57 sss_status_t se050_scp03_prepare_rotate_cmd(struct sss_se05x_ctx *ctx,
58 					    struct s050_scp_rotate_cmd *cmd,
59 					    struct se050_scp_key *keys);
60 #endif /* SE050_UTILS_H_ */
61