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> |
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 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 67 | ret = run_descriptor_jr(desc); |
| 68 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 69 | if (ret) { |
| 70 | printf("Error in blob decapsulation: %d\n", ret); |
| 71 | } else { |
| 72 | size = ALIGN(len, ARCH_DMA_MINALIGN); |
| 73 | invalidate_dcache_range((unsigned long)dst, |
| 74 | (unsigned long)dst + size); |
| 75 | |
| 76 | puts("Blob decapsulation successful.\n"); |
| 77 | } |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 78 | |
| 79 | free(desc); |
| 80 | return ret; |
| 81 | } |
| 82 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 83 | /** |
| 84 | * blob_encap() - Encapsulate the data as a blob |
| 85 | * @key_mod: - Key modifier address |
| 86 | * @src: - Source address (data) |
| 87 | * @dst: - Destination address (blob) |
| 88 | * @len: - Size of data to be encapsulated |
| 89 | * |
| 90 | * Note: Start and end of the key_mod, src and dst buffers have to be aligned to |
| 91 | * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed. |
| 92 | * |
| 93 | * Returns zero on success, negative on error. |
| 94 | */ |
gaurav rana | b64308e | 2015-02-25 09:37:09 +0530 | [diff] [blame] | 95 | int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 96 | { |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 97 | int ret, size, i = 0; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 98 | u32 *desc; |
| 99 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 100 | if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) || |
| 101 | !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) || |
| 102 | !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) { |
| 103 | puts("Error: blob_encap: Address arguments are not aligned!\n"); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 107 | printf("\nEncapsulating data to form blob\n"); |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 108 | desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 109 | if (!desc) { |
| 110 | debug("Not enough memory for descriptor allocation\n"); |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 111 | return -ENOMEM; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 112 | } |
| 113 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 114 | size = ALIGN(16, ARCH_DMA_MINALIGN); |
| 115 | flush_dcache_range((unsigned long)key_mod, |
| 116 | (unsigned long)key_mod + size); |
| 117 | |
| 118 | size = ALIGN(len, ARCH_DMA_MINALIGN); |
| 119 | flush_dcache_range((unsigned long)src, |
| 120 | (unsigned long)src + size); |
| 121 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 122 | inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len); |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 123 | |
| 124 | debug("Descriptor dump:\n"); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 125 | for (i = 0; i < 14; i++) |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 126 | debug("Word[%d]: %08x\n", i, *(desc + i)); |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 127 | |
| 128 | size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN); |
| 129 | flush_dcache_range((unsigned long)desc, |
| 130 | (unsigned long)desc + size); |
| 131 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 132 | ret = run_descriptor_jr(desc); |
| 133 | |
Clemens Gruber | 58b1396 | 2018-01-07 20:26:29 +0100 | [diff] [blame] | 134 | if (ret) { |
| 135 | printf("Error in blob encapsulation: %d\n", ret); |
| 136 | } else { |
| 137 | size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN); |
| 138 | invalidate_dcache_range((unsigned long)dst, |
| 139 | (unsigned long)dst + size); |
| 140 | |
| 141 | puts("Blob encapsulation successful.\n"); |
| 142 | } |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 143 | |
| 144 | free(desc); |
| 145 | return ret; |
| 146 | } |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 147 | |
| 148 | #ifdef CONFIG_CMD_DEKBLOB |
| 149 | int blob_dek(const u8 *src, u8 *dst, u8 len) |
| 150 | { |
| 151 | int ret, size, i = 0; |
| 152 | u32 *desc; |
| 153 | |
| 154 | int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE; |
| 155 | |
| 156 | puts("\nEncapsulating provided DEK to form blob\n"); |
| 157 | desc = memalign(ARCH_DMA_MINALIGN, |
| 158 | sizeof(uint32_t) * DEK_BLOB_DESCSIZE); |
| 159 | if (!desc) { |
| 160 | debug("Not enough memory for descriptor allocation\n"); |
| 161 | return -ENOMEM; |
| 162 | } |
| 163 | |
| 164 | ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len); |
| 165 | if (ret) { |
| 166 | debug("Error in Job Descriptor Construction: %d\n", ret); |
| 167 | } else { |
| 168 | size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE, |
| 169 | ARCH_DMA_MINALIGN); |
| 170 | flush_dcache_range((unsigned long)desc, |
| 171 | (unsigned long)desc + size); |
| 172 | size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN); |
| 173 | flush_dcache_range((unsigned long)dst, |
| 174 | (unsigned long)dst + size); |
| 175 | |
| 176 | ret = run_descriptor_jr(desc); |
| 177 | } |
| 178 | |
| 179 | if (ret) { |
| 180 | debug("Error in Encapsulation %d\n", ret); |
| 181 | goto err; |
| 182 | } |
| 183 | |
| 184 | size = roundup(out_sz, ARCH_DMA_MINALIGN); |
| 185 | invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size); |
| 186 | |
| 187 | puts("DEK Blob\n"); |
| 188 | for (i = 0; i < out_sz; i++) |
| 189 | printf("%02X", ((uint8_t *)dst)[i]); |
| 190 | printf("\n"); |
| 191 | |
| 192 | err: |
| 193 | free(desc); |
| 194 | return ret; |
| 195 | } |
| 196 | #endif |