Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Freescale Semiconductor, Inc. |
| 4 | * |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 9 | #include <malloc.h> |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 10 | #include <memalign.h> |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 11 | #include <fsl_sec.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 12 | #include <asm/cache.h> |
Masahiro Yamada | 64e4f7f | 2016-09-21 11:28:57 +0900 | [diff] [blame] | 13 | #include <linux/errno.h> |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 14 | #include "jobdesc.h" |
| 15 | #include "desc.h" |
| 16 | #include "jr.h" |
| 17 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 18 | /** |
| 19 | * blob_decap() - Decapsulate the data from a blob |
| 20 | * @key_mod: - Key modifier address |
| 21 | * @src: - Source address (blob) |
| 22 | * @dst: - Destination address (data) |
| 23 | * @len: - Size of decapsulated data |
| 24 | * |
| 25 | * Note: Start and end of the key_mod, src and dst buffers have to be aligned to |
| 26 | * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed. |
| 27 | * |
| 28 | * Returns zero on success, negative on error. |
| 29 | */ |
gaurav rana | b64308e | 2015-02-25 09:37:09 +0530 | [diff] [blame] | 30 | int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 31 | { |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 32 | int ret, size, i = 0; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 33 | u32 *desc; |
| 34 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 35 | if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) || |
| 36 | !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) || |
| 37 | !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) { |
| 38 | puts("Error: blob_decap: Address arguments are not aligned!\n"); |
| 39 | return -EINVAL; |
| 40 | } |
| 41 | |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 42 | printf("\nDecapsulating blob to get data\n"); |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 43 | desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 44 | if (!desc) { |
| 45 | debug("Not enough memory for descriptor allocation\n"); |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 46 | return -ENOMEM; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 47 | } |
| 48 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 49 | size = ALIGN(16, ARCH_DMA_MINALIGN); |
| 50 | flush_dcache_range((unsigned long)key_mod, |
| 51 | (unsigned long)key_mod + size); |
| 52 | |
| 53 | size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN); |
| 54 | flush_dcache_range((unsigned long)src, |
| 55 | (unsigned long)src + size); |
| 56 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 57 | inline_cnstr_jobdesc_blob_decap(desc, key_mod, src, dst, len); |
| 58 | |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 59 | debug("Descriptor dump:\n"); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 60 | for (i = 0; i < 14; i++) |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 61 | debug("Word[%d]: %08x\n", i, *(desc + i)); |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 62 | |
| 63 | size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN); |
| 64 | flush_dcache_range((unsigned long)desc, |
| 65 | (unsigned long)desc + size); |
| 66 | |
Breno Lima | a422c21 | 2021-03-25 17:30:16 +0800 | [diff] [blame] | 67 | flush_dcache_range((unsigned long)dst, |
| 68 | (unsigned long)dst + size); |
| 69 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 70 | ret = run_descriptor_jr(desc); |
| 71 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 72 | if (ret) { |
Gaurav Jain | 2b0f059 | 2022-04-15 16:52:34 +0530 | [diff] [blame] | 73 | /* clear the blob data output buffer */ |
| 74 | memset(dst, 0x00, len); |
| 75 | size = ALIGN(len, ARCH_DMA_MINALIGN); |
| 76 | flush_dcache_range((unsigned long)dst, (unsigned long)dst + size); |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 77 | printf("Error in blob decapsulation: %d\n", ret); |
| 78 | } else { |
| 79 | size = ALIGN(len, ARCH_DMA_MINALIGN); |
| 80 | invalidate_dcache_range((unsigned long)dst, |
| 81 | (unsigned long)dst + size); |
| 82 | |
| 83 | puts("Blob decapsulation successful.\n"); |
| 84 | } |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 85 | |
| 86 | free(desc); |
| 87 | return ret; |
| 88 | } |
| 89 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 90 | /** |
| 91 | * blob_encap() - Encapsulate the data as a blob |
| 92 | * @key_mod: - Key modifier address |
| 93 | * @src: - Source address (data) |
| 94 | * @dst: - Destination address (blob) |
| 95 | * @len: - Size of data to be encapsulated |
| 96 | * |
| 97 | * Note: Start and end of the key_mod, src and dst buffers have to be aligned to |
| 98 | * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed. |
| 99 | * |
| 100 | * Returns zero on success, negative on error. |
| 101 | */ |
gaurav rana | b64308e | 2015-02-25 09:37:09 +0530 | [diff] [blame] | 102 | int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 103 | { |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 104 | int ret, size, i = 0; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 105 | u32 *desc; |
| 106 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 107 | if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) || |
| 108 | !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) || |
| 109 | !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) { |
| 110 | puts("Error: blob_encap: Address arguments are not aligned!\n"); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 114 | printf("\nEncapsulating data to form blob\n"); |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 115 | desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 116 | if (!desc) { |
| 117 | debug("Not enough memory for descriptor allocation\n"); |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 118 | return -ENOMEM; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 119 | } |
| 120 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 121 | size = ALIGN(16, ARCH_DMA_MINALIGN); |
| 122 | flush_dcache_range((unsigned long)key_mod, |
| 123 | (unsigned long)key_mod + size); |
| 124 | |
| 125 | size = ALIGN(len, ARCH_DMA_MINALIGN); |
| 126 | flush_dcache_range((unsigned long)src, |
| 127 | (unsigned long)src + size); |
| 128 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 129 | inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len); |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 130 | |
| 131 | debug("Descriptor dump:\n"); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 132 | for (i = 0; i < 14; i++) |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 133 | debug("Word[%d]: %08x\n", i, *(desc + i)); |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 134 | |
| 135 | size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN); |
| 136 | flush_dcache_range((unsigned long)desc, |
| 137 | (unsigned long)desc + size); |
| 138 | |
Breno Lima | a422c21 | 2021-03-25 17:30:16 +0800 | [diff] [blame] | 139 | flush_dcache_range((unsigned long)dst, |
| 140 | (unsigned long)dst + size); |
| 141 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 142 | ret = run_descriptor_jr(desc); |
| 143 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 144 | if (ret) { |
| 145 | printf("Error in blob encapsulation: %d\n", ret); |
| 146 | } else { |
| 147 | size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN); |
| 148 | invalidate_dcache_range((unsigned long)dst, |
| 149 | (unsigned long)dst + size); |
| 150 | |
| 151 | puts("Blob encapsulation successful.\n"); |
| 152 | } |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 153 | |
| 154 | free(desc); |
| 155 | return ret; |
| 156 | } |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 157 | |
| 158 | #ifdef CONFIG_CMD_DEKBLOB |
| 159 | int blob_dek(const u8 *src, u8 *dst, u8 len) |
| 160 | { |
| 161 | int ret, size, i = 0; |
| 162 | u32 *desc; |
| 163 | |
| 164 | int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE; |
| 165 | |
| 166 | puts("\nEncapsulating provided DEK to form blob\n"); |
| 167 | desc = memalign(ARCH_DMA_MINALIGN, |
| 168 | sizeof(uint32_t) * DEK_BLOB_DESCSIZE); |
| 169 | if (!desc) { |
| 170 | debug("Not enough memory for descriptor allocation\n"); |
| 171 | return -ENOMEM; |
| 172 | } |
| 173 | |
| 174 | ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len); |
| 175 | if (ret) { |
| 176 | debug("Error in Job Descriptor Construction: %d\n", ret); |
| 177 | } else { |
| 178 | size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE, |
| 179 | ARCH_DMA_MINALIGN); |
| 180 | flush_dcache_range((unsigned long)desc, |
| 181 | (unsigned long)desc + size); |
| 182 | size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN); |
| 183 | flush_dcache_range((unsigned long)dst, |
| 184 | (unsigned long)dst + size); |
| 185 | |
| 186 | ret = run_descriptor_jr(desc); |
| 187 | } |
| 188 | |
| 189 | if (ret) { |
| 190 | debug("Error in Encapsulation %d\n", ret); |
| 191 | goto err; |
| 192 | } |
| 193 | |
| 194 | size = roundup(out_sz, ARCH_DMA_MINALIGN); |
| 195 | invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size); |
| 196 | |
| 197 | puts("DEK Blob\n"); |
| 198 | for (i = 0; i < out_sz; i++) |
| 199 | printf("%02X", ((uint8_t *)dst)[i]); |
| 200 | printf("\n"); |
| 201 | |
| 202 | err: |
| 203 | free(desc); |
| 204 | return ret; |
| 205 | } |
| 206 | #endif |