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 decsriptor is submitted to SEC
22  */
blob_done(uint32_t * desc,uint32_t status,void * arg,void * job_ring)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  */
get_hw_unq_key_blob_hw(uint8_t * hw_key,int size)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 	/* Output blob will have 32 bytes key blob in beginning and
47 	 * 16 byte HMAC identifier at end of data blob
48 	 */
49 	uint32_t out_sz = in_sz + KEY_BLOB_SIZE + MAC_SIZE;
50 
51 	uint32_t operation = CMD_OPERATION | OP_TYPE_ENCAP_PROTOCOL |
52 	    OP_PCLID_BLOB | BLOB_PROTO_INFO;
53 
54 	memset(key_data, 0xff, KEY_IDNFR_SZ_BYTES);
55 	memset(in_data, 0x00, in_sz);
56 	memset(out_data, 0x00, in_sz);
57 
58 	jobdesc->arg = NULL;
59 	jobdesc->callback = blob_done;
60 
61 	INFO("\nGenerating Master Key Verification Blob.\n");
62 
63 	/* Create the hw_rng descriptor */
64 	ret = cnstr_hw_encap_blob_jobdesc(jobdesc->desc, key_data, key_sz,
65 					  CLASS_2, in_data, in_sz, out_data,
66 					  out_sz, operation);
67 
68 	/* Finally, generate the blob. */
69 	ret = run_descriptor_jr(jobdesc);
70 	if (ret != 0) {
71 		ERROR("Error in running hw unq key blob descriptor\n");
72 		return -1;
73 	}
74 	/* Copying alternate bytes of the Master Key Verification Blob.
75 	 */
76 	for (i = 0; i < size; i++) {
77 		hw_key[i] = out_data[2 * i];
78 	}
79 
80 	return ret;
81 }
82