1 /* 2 * SEC Descriptor Construction Library 3 * Basic job descriptor construction 4 * 5 * Copyright 2014 Freescale Semiconductor, Inc. 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 * 9 */ 10 11 #include <common.h> 12 #include "desc_constr.h" 13 #include "jobdesc.h" 14 15 #define KEY_BLOB_SIZE 32 16 #define MAC_SIZE 16 17 18 void inline_cnstr_jobdesc_hash(uint32_t *desc, 19 const uint8_t *msg, uint32_t msgsz, uint8_t *digest, 20 u32 alg_type, uint32_t alg_size, int sg_tbl) 21 { 22 /* SHA 256 , output is of length 32 words */ 23 uint32_t storelen = alg_size; 24 u32 options; 25 dma_addr_t dma_addr_in, dma_addr_out; 26 27 dma_addr_in = virt_to_phys((void *)msg); 28 dma_addr_out = virt_to_phys((void *)digest); 29 30 init_job_desc(desc, 0); 31 append_operation(desc, OP_TYPE_CLASS2_ALG | 32 OP_ALG_AAI_HASH | OP_ALG_AS_INITFINAL | 33 OP_ALG_ENCRYPT | OP_ALG_ICV_OFF | alg_type); 34 35 options = LDST_CLASS_2_CCB | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2; 36 if (sg_tbl) 37 options |= FIFOLDST_SGF; 38 if (msgsz > 0xffff) { 39 options |= FIFOLDST_EXT; 40 append_fifo_load(desc, dma_addr_in, 0, options); 41 append_cmd(desc, msgsz); 42 } else { 43 append_fifo_load(desc, dma_addr_in, msgsz, options); 44 } 45 46 append_store(desc, dma_addr_out, storelen, 47 LDST_CLASS_2_CCB | LDST_SRCDST_BYTE_CONTEXT); 48 } 49 50 void inline_cnstr_jobdesc_blob_encap(uint32_t *desc, uint8_t *key_idnfr, 51 uint8_t *plain_txt, uint8_t *enc_blob, 52 uint32_t in_sz) 53 { 54 dma_addr_t dma_addr_key_idnfr, dma_addr_in, dma_addr_out; 55 uint32_t key_sz = KEY_IDNFR_SZ_BYTES; 56 /* output blob will have 32 bytes key blob in beginning and 57 * 16 byte HMAC identifier at end of data blob */ 58 uint32_t out_sz = in_sz + KEY_BLOB_SIZE + MAC_SIZE; 59 60 dma_addr_key_idnfr = virt_to_phys((void *)key_idnfr); 61 dma_addr_in = virt_to_phys((void *)plain_txt); 62 dma_addr_out = virt_to_phys((void *)enc_blob); 63 64 init_job_desc(desc, 0); 65 66 append_key(desc, dma_addr_key_idnfr, key_sz, CLASS_2); 67 68 append_seq_in_ptr(desc, dma_addr_in, in_sz, 0); 69 70 append_seq_out_ptr(desc, dma_addr_out, out_sz, 0); 71 72 append_operation(desc, OP_TYPE_ENCAP_PROTOCOL | OP_PCLID_BLOB); 73 } 74 75 void inline_cnstr_jobdesc_blob_decap(uint32_t *desc, uint8_t *key_idnfr, 76 uint8_t *enc_blob, uint8_t *plain_txt, 77 uint32_t out_sz) 78 { 79 dma_addr_t dma_addr_key_idnfr, dma_addr_in, dma_addr_out; 80 uint32_t key_sz = KEY_IDNFR_SZ_BYTES; 81 uint32_t in_sz = out_sz + KEY_BLOB_SIZE + MAC_SIZE; 82 83 dma_addr_key_idnfr = virt_to_phys((void *)key_idnfr); 84 dma_addr_in = virt_to_phys((void *)enc_blob); 85 dma_addr_out = virt_to_phys((void *)plain_txt); 86 87 init_job_desc(desc, 0); 88 89 append_key(desc, dma_addr_key_idnfr, key_sz, CLASS_2); 90 91 append_seq_in_ptr(desc, dma_addr_in, in_sz, 0); 92 93 append_seq_out_ptr(desc, dma_addr_out, out_sz, 0); 94 95 append_operation(desc, OP_TYPE_DECAP_PROTOCOL | OP_PCLID_BLOB); 96 } 97 98 /* 99 * Descriptor to instantiate RNG State Handle 0 in normal mode and 100 * load the JDKEK, TDKEK and TDSK registers 101 */ 102 void inline_cnstr_jobdesc_rng_instantiation(uint32_t *desc) 103 { 104 u32 *jump_cmd; 105 106 init_job_desc(desc, 0); 107 108 /* INIT RNG in non-test mode */ 109 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG | 110 OP_ALG_AS_INIT); 111 112 /* wait for done */ 113 jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1); 114 set_jump_tgt_here(desc, jump_cmd); 115 116 /* 117 * load 1 to clear written reg: 118 * resets the done interrrupt and returns the RNG to idle. 119 */ 120 append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW); 121 122 /* generate secure keys (non-test) */ 123 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG | 124 OP_ALG_RNG4_SK); 125 } 126