Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Freescale Semiconductor, Inc. |
Kshitiz Varshney | c468632 | 2021-09-19 17:09:53 +0200 | [diff] [blame] | 4 | * Copyright 2021 NXP |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +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 | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 9 | #include <malloc.h> |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 10 | #include <memalign.h> |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 11 | #include "jobdesc.h" |
| 12 | #include "desc.h" |
| 13 | #include "jr.h" |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 14 | #include "fsl_hash.h" |
| 15 | #include <hw_sha.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 16 | #include <asm/cache.h> |
Masahiro Yamada | 64e4f7f | 2016-09-21 11:28:57 +0900 | [diff] [blame] | 17 | #include <linux/errno.h> |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 18 | |
| 19 | #define CRYPTO_MAX_ALG_NAME 80 |
| 20 | #define SHA1_DIGEST_SIZE 20 |
| 21 | #define SHA256_DIGEST_SIZE 32 |
| 22 | |
| 23 | struct caam_hash_template { |
| 24 | char name[CRYPTO_MAX_ALG_NAME]; |
| 25 | unsigned int digestsize; |
| 26 | u32 alg_type; |
| 27 | }; |
| 28 | |
| 29 | enum caam_hash_algos { |
| 30 | SHA1 = 0, |
| 31 | SHA256 |
| 32 | }; |
| 33 | |
| 34 | static struct caam_hash_template driver_hash[] = { |
| 35 | { |
| 36 | .name = "sha1", |
| 37 | .digestsize = SHA1_DIGEST_SIZE, |
| 38 | .alg_type = OP_ALG_ALGSEL_SHA1, |
| 39 | }, |
| 40 | { |
| 41 | .name = "sha256", |
| 42 | .digestsize = SHA256_DIGEST_SIZE, |
| 43 | .alg_type = OP_ALG_ALGSEL_SHA256, |
| 44 | }, |
| 45 | }; |
| 46 | |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 47 | static enum caam_hash_algos get_hash_type(struct hash_algo *algo) |
| 48 | { |
| 49 | if (!strcmp(algo->name, driver_hash[SHA1].name)) |
| 50 | return SHA1; |
| 51 | else |
| 52 | return SHA256; |
| 53 | } |
| 54 | |
| 55 | /* Create the context for progressive hashing using h/w acceleration. |
| 56 | * |
| 57 | * @ctxp: Pointer to the pointer of the context for hashing |
| 58 | * @caam_algo: Enum for SHA1 or SHA256 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 59 | * Return: 0 if ok, -ENOMEM on error |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 60 | */ |
| 61 | static int caam_hash_init(void **ctxp, enum caam_hash_algos caam_algo) |
| 62 | { |
| 63 | *ctxp = calloc(1, sizeof(struct sha_ctx)); |
| 64 | if (*ctxp == NULL) { |
| 65 | debug("Cannot allocate memory for context\n"); |
| 66 | return -ENOMEM; |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Update sg table for progressive hashing using h/w acceleration |
| 73 | * |
| 74 | * The context is freed by this function if an error occurs. |
| 75 | * We support at most 32 Scatter/Gather Entries. |
| 76 | * |
| 77 | * @hash_ctx: Pointer to the context for hashing |
| 78 | * @buf: Pointer to the buffer being hashed |
| 79 | * @size: Size of the buffer being hashed |
| 80 | * @is_last: 1 if this is the last update; 0 otherwise |
| 81 | * @caam_algo: Enum for SHA1 or SHA256 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 82 | * Return: 0 if ok, -EINVAL on error |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 83 | */ |
| 84 | static int caam_hash_update(void *hash_ctx, const void *buf, |
| 85 | unsigned int size, int is_last, |
| 86 | enum caam_hash_algos caam_algo) |
| 87 | { |
Heinrich Schuchardt | 0dd1d84 | 2020-06-27 10:13:55 +0200 | [diff] [blame] | 88 | uint32_t final; |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 89 | caam_dma_addr_t addr = virt_to_phys((void *)buf); |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 90 | struct sha_ctx *ctx = hash_ctx; |
| 91 | |
| 92 | if (ctx->sg_num >= MAX_SG_32) { |
| 93 | free(ctx); |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 97 | #ifdef CONFIG_CAAM_64BIT |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 98 | sec_out32(&ctx->sg_tbl[ctx->sg_num].addr_hi, (uint32_t)(addr >> 32)); |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 99 | #else |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 100 | sec_out32(&ctx->sg_tbl[ctx->sg_num].addr_hi, 0x0); |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 101 | #endif |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 102 | sec_out32(&ctx->sg_tbl[ctx->sg_num].addr_lo, (caam_dma_addr_t)addr); |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 103 | |
| 104 | sec_out32(&ctx->sg_tbl[ctx->sg_num].len_flag, |
| 105 | (size & SG_ENTRY_LENGTH_MASK)); |
| 106 | |
| 107 | ctx->sg_num++; |
| 108 | |
| 109 | if (is_last) { |
| 110 | final = sec_in32(&ctx->sg_tbl[ctx->sg_num - 1].len_flag) | |
| 111 | SG_ENTRY_FINAL_BIT; |
| 112 | sec_out32(&ctx->sg_tbl[ctx->sg_num - 1].len_flag, final); |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Perform progressive hashing on the given buffer and copy hash at |
| 120 | * destination buffer |
| 121 | * |
Kshitiz Varshney | c468632 | 2021-09-19 17:09:53 +0200 | [diff] [blame] | 122 | * The context is freed after successful completion of hash operation. |
| 123 | * In case of failure, context is not freed. |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 124 | * @hash_ctx: Pointer to the context for hashing |
| 125 | * @dest_buf: Pointer to the destination buffer where hash is to be copied |
| 126 | * @size: Size of the buffer being hashed |
| 127 | * @caam_algo: Enum for SHA1 or SHA256 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 128 | * Return: 0 if ok, -EINVAL on error |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 129 | */ |
| 130 | static int caam_hash_finish(void *hash_ctx, void *dest_buf, |
| 131 | int size, enum caam_hash_algos caam_algo) |
| 132 | { |
Gaurav Jain | 609892f | 2022-07-29 15:32:23 +0530 | [diff] [blame] | 133 | uint32_t len = 0, sg_entry_len; |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 134 | struct sha_ctx *ctx = hash_ctx; |
| 135 | int i = 0, ret = 0; |
Gaurav Jain | 609892f | 2022-07-29 15:32:23 +0530 | [diff] [blame] | 136 | caam_dma_addr_t addr; |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 137 | |
| 138 | if (size < driver_hash[caam_algo].digestsize) { |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 139 | return -EINVAL; |
| 140 | } |
| 141 | |
Gaurav Jain | 609892f | 2022-07-29 15:32:23 +0530 | [diff] [blame] | 142 | flush_dcache_range((ulong)ctx->sg_tbl, |
| 143 | (ulong)(ctx->sg_tbl) + (ctx->sg_num * sizeof(struct sg_entry))); |
| 144 | for (i = 0; i < ctx->sg_num; i++) { |
| 145 | sg_entry_len = (sec_in32(&ctx->sg_tbl[i].len_flag) & |
| 146 | SG_ENTRY_LENGTH_MASK); |
| 147 | len += sg_entry_len; |
| 148 | #ifdef CONFIG_CAAM_64BIT |
| 149 | addr = sec_in32(&ctx->sg_tbl[i].addr_hi); |
| 150 | addr = (addr << 32) | sec_in32(&ctx->sg_tbl[i].addr_lo); |
| 151 | #else |
| 152 | addr = sec_in32(&ctx->sg_tbl[i].addr_lo); |
| 153 | #endif |
| 154 | flush_dcache_range(addr, addr + sg_entry_len); |
| 155 | } |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 156 | inline_cnstr_jobdesc_hash(ctx->sha_desc, (uint8_t *)ctx->sg_tbl, len, |
| 157 | ctx->hash, |
| 158 | driver_hash[caam_algo].alg_type, |
| 159 | driver_hash[caam_algo].digestsize, |
| 160 | 1); |
| 161 | |
Gaurav Jain | 5c2c406 | 2022-05-11 14:23:19 +0530 | [diff] [blame] | 162 | flush_dcache_range((ulong)ctx->sha_desc, |
| 163 | (ulong)(ctx->sha_desc) + (sizeof(uint32_t) * MAX_CAAM_DESCSIZE)); |
| 164 | flush_dcache_range((ulong)ctx->hash, |
| 165 | (ulong)(ctx->hash) + driver_hash[caam_algo].digestsize); |
| 166 | |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 167 | ret = run_descriptor_jr(ctx->sha_desc); |
| 168 | |
Kshitiz Varshney | c468632 | 2021-09-19 17:09:53 +0200 | [diff] [blame] | 169 | if (ret) { |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 170 | debug("Error %x\n", ret); |
Kshitiz Varshney | c468632 | 2021-09-19 17:09:53 +0200 | [diff] [blame] | 171 | return ret; |
| 172 | } else { |
Gaurav Jain | 5c2c406 | 2022-05-11 14:23:19 +0530 | [diff] [blame] | 173 | invalidate_dcache_range((ulong)ctx->hash, |
| 174 | (ulong)(ctx->hash) + driver_hash[caam_algo].digestsize); |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 175 | memcpy(dest_buf, ctx->hash, sizeof(ctx->hash)); |
Kshitiz Varshney | c468632 | 2021-09-19 17:09:53 +0200 | [diff] [blame] | 176 | } |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 177 | free(ctx); |
| 178 | return ret; |
| 179 | } |
| 180 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 181 | int caam_hash(const unsigned char *pbuf, unsigned int buf_len, |
| 182 | unsigned char *pout, enum caam_hash_algos algo) |
| 183 | { |
| 184 | int ret = 0; |
| 185 | uint32_t *desc; |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 186 | unsigned int size; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 187 | |
Gaurav Jain | 95fa794 | 2022-04-19 10:52:28 +0530 | [diff] [blame] | 188 | desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE); |
| 189 | if (!desc) { |
| 190 | debug("Not enough memory for descriptor allocation\n"); |
| 191 | return -ENOMEM; |
| 192 | } |
| 193 | |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 194 | size = ALIGN(buf_len, ARCH_DMA_MINALIGN); |
| 195 | flush_dcache_range((unsigned long)pbuf, (unsigned long)pbuf + size); |
| 196 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 197 | inline_cnstr_jobdesc_hash(desc, pbuf, buf_len, pout, |
| 198 | driver_hash[algo].alg_type, |
| 199 | driver_hash[algo].digestsize, |
| 200 | 0); |
| 201 | |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 202 | size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN); |
| 203 | flush_dcache_range((unsigned long)desc, (unsigned long)desc + size); |
Gaurav Jain | 95fa794 | 2022-04-19 10:52:28 +0530 | [diff] [blame] | 204 | size = ALIGN(driver_hash[algo].digestsize, ARCH_DMA_MINALIGN); |
| 205 | invalidate_dcache_range((unsigned long)pout, (unsigned long)pout + size); |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 206 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 207 | ret = run_descriptor_jr(desc); |
| 208 | |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 209 | size = ALIGN(driver_hash[algo].digestsize, ARCH_DMA_MINALIGN); |
| 210 | invalidate_dcache_range((unsigned long)pout, |
| 211 | (unsigned long)pout + size); |
| 212 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 213 | free(desc); |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | void hw_sha256(const unsigned char *pbuf, unsigned int buf_len, |
| 218 | unsigned char *pout, unsigned int chunk_size) |
| 219 | { |
| 220 | if (caam_hash(pbuf, buf_len, pout, SHA256)) |
| 221 | printf("CAAM was not setup properly or it is faulty\n"); |
| 222 | } |
| 223 | |
| 224 | void hw_sha1(const unsigned char *pbuf, unsigned int buf_len, |
| 225 | unsigned char *pout, unsigned int chunk_size) |
| 226 | { |
| 227 | if (caam_hash(pbuf, buf_len, pout, SHA1)) |
| 228 | printf("CAAM was not setup properly or it is faulty\n"); |
| 229 | } |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 230 | |
| 231 | int hw_sha_init(struct hash_algo *algo, void **ctxp) |
| 232 | { |
| 233 | return caam_hash_init(ctxp, get_hash_type(algo)); |
| 234 | } |
| 235 | |
| 236 | int hw_sha_update(struct hash_algo *algo, void *ctx, const void *buf, |
| 237 | unsigned int size, int is_last) |
| 238 | { |
| 239 | return caam_hash_update(ctx, buf, size, is_last, get_hash_type(algo)); |
| 240 | } |
| 241 | |
| 242 | int hw_sha_finish(struct hash_algo *algo, void *ctx, void *dest_buf, |
| 243 | int size) |
| 244 | { |
| 245 | return caam_hash_finish(ctx, dest_buf, size, get_hash_type(algo)); |
| 246 | } |