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