xref: /rk3399_ARM-atf/drivers/nxp/crypto/caam/src/hw_key_blob.c (revision 4274b5265576c1c27680970978066d05b7b2b96b)
1 /*
2  * Copyright 2021 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <errno.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "caam.h"
16 #include <common/debug.h>
17 #include "jobdesc.h"
18 #include "sec_hw_specific.h"
19 
20 
21 /* Callback function after Instantiation descriptor is submitted to SEC
22  */
23 static void blob_done(uint32_t *desc, uint32_t status, void *arg,
24 		      void *job_ring)
25 {
26 	INFO("Blob Desc SUCCESS with status %x\n", status);
27 }
28 
29 /* @brief Submit descriptor to create blob
30  * @retval 0 on success
31  * @retval -1 on error
32  */
33 int get_hw_unq_key_blob_hw(uint8_t *hw_key, int size)
34 {
35 	int ret = 0;
36 	int i = 0;
37 
38 	uint32_t key_sz = KEY_IDNFR_SZ_BYTES;
39 	uint8_t key_data[KEY_IDNFR_SZ_BYTES];
40 	uint8_t in_data[16];
41 	uint8_t out_data[16 + KEY_BLOB_SIZE + MAC_SIZE];
42 	struct job_descriptor desc __aligned(CACHE_WRITEBACK_GRANULE);
43 	struct job_descriptor *jobdesc = &desc;
44 	uint32_t in_sz = 16U;
45 
46 	if (size <= 0 ||  size > 16) {
47 		ERROR("Error: Requested invalid length of HUK.\n");
48 		return -1;
49 	}
50 	/* Output blob will have 32 bytes key blob in beginning and
51 	 * 16 byte HMAC identifier at end of data blob
52 	 */
53 	uint32_t out_sz = in_sz + KEY_BLOB_SIZE + MAC_SIZE;
54 
55 	uint32_t operation = CMD_OPERATION | OP_TYPE_ENCAP_PROTOCOL |
56 	    OP_PCLID_BLOB | BLOB_PROTO_INFO;
57 
58 	memset(key_data, 0xff, KEY_IDNFR_SZ_BYTES);
59 	memset(in_data, 0x00, in_sz);
60 	memset(out_data, 0x00, in_sz);
61 
62 	jobdesc->arg = NULL;
63 	jobdesc->callback = blob_done;
64 
65 	INFO("\nGenerating Master Key Verification Blob.\n");
66 
67 	/* Create the hw_rng descriptor */
68 	ret = cnstr_hw_encap_blob_jobdesc(jobdesc->desc, key_data, key_sz,
69 					  CLASS_2, in_data, in_sz, out_data,
70 					  out_sz, operation);
71 
72 	/* Finally, generate the blob. */
73 	ret = run_descriptor_jr(jobdesc);
74 	if (ret != 0) {
75 		ERROR("Error in running hw unq key blob descriptor\n");
76 		return -1;
77 	}
78 	/* Copying alternate bytes of the Master Key Verification Blob.
79 	 */
80 	for (i = 0; i < size; i++) {
81 		hw_key[i] = out_data[2 * i];
82 	}
83 
84 	return ret;
85 }
86