blob: 80394730121f4c45641381578fa531741849e2ca [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ruchika Guptaac1b2692014-10-15 11:35:30 +05302/*
3 * Copyright 2014 Freescale Semiconductor, Inc.
Kshitiz Varshneyc4686322021-09-19 17:09:53 +02004 * Copyright 2021 NXP
Ruchika Guptaac1b2692014-10-15 11:35:30 +05305 */
6
7#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Ruchika Guptaac1b2692014-10-15 11:35:30 +053010#include <malloc.h>
Breno Lima45e65122018-01-17 10:03:45 -020011#include <memalign.h>
Ruchika Guptaac1b2692014-10-15 11:35:30 +053012#include "jobdesc.h"
13#include "desc.h"
14#include "jr.h"
gaurav ranaef201592015-02-20 12:51:46 +053015#include "fsl_hash.h"
16#include <hw_sha.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <asm/cache.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090018#include <linux/errno.h>
Ruchika Guptaac1b2692014-10-15 11:35:30 +053019
20#define CRYPTO_MAX_ALG_NAME 80
21#define SHA1_DIGEST_SIZE 20
22#define SHA256_DIGEST_SIZE 32
23
24struct caam_hash_template {
25 char name[CRYPTO_MAX_ALG_NAME];
26 unsigned int digestsize;
27 u32 alg_type;
28};
29
30enum caam_hash_algos {
31 SHA1 = 0,
32 SHA256
33};
34
35static struct caam_hash_template driver_hash[] = {
36 {
37 .name = "sha1",
38 .digestsize = SHA1_DIGEST_SIZE,
39 .alg_type = OP_ALG_ALGSEL_SHA1,
40 },
41 {
42 .name = "sha256",
43 .digestsize = SHA256_DIGEST_SIZE,
44 .alg_type = OP_ALG_ALGSEL_SHA256,
45 },
46};
47
gaurav ranaef201592015-02-20 12:51:46 +053048static enum caam_hash_algos get_hash_type(struct hash_algo *algo)
49{
50 if (!strcmp(algo->name, driver_hash[SHA1].name))
51 return SHA1;
52 else
53 return SHA256;
54}
55
56/* Create the context for progressive hashing using h/w acceleration.
57 *
58 * @ctxp: Pointer to the pointer of the context for hashing
59 * @caam_algo: Enum for SHA1 or SHA256
60 * @return 0 if ok, -ENOMEM on error
61 */
62static int caam_hash_init(void **ctxp, enum caam_hash_algos caam_algo)
63{
64 *ctxp = calloc(1, sizeof(struct sha_ctx));
65 if (*ctxp == NULL) {
66 debug("Cannot allocate memory for context\n");
67 return -ENOMEM;
68 }
69 return 0;
70}
71
72/*
73 * Update sg table for progressive hashing using h/w acceleration
74 *
75 * The context is freed by this function if an error occurs.
76 * We support at most 32 Scatter/Gather Entries.
77 *
78 * @hash_ctx: Pointer to the context for hashing
79 * @buf: Pointer to the buffer being hashed
80 * @size: Size of the buffer being hashed
81 * @is_last: 1 if this is the last update; 0 otherwise
82 * @caam_algo: Enum for SHA1 or SHA256
83 * @return 0 if ok, -EINVAL on error
84 */
85static int caam_hash_update(void *hash_ctx, const void *buf,
86 unsigned int size, int is_last,
87 enum caam_hash_algos caam_algo)
88{
Heinrich Schuchardt0dd1d842020-06-27 10:13:55 +020089 uint32_t final;
Ye Li3c3e9a12021-03-25 17:30:36 +080090 caam_dma_addr_t addr = virt_to_phys((void *)buf);
gaurav ranaef201592015-02-20 12:51:46 +053091 struct sha_ctx *ctx = hash_ctx;
92
93 if (ctx->sg_num >= MAX_SG_32) {
94 free(ctx);
95 return -EINVAL;
96 }
97
Ye Li3c3e9a12021-03-25 17:30:36 +080098#ifdef CONFIG_CAAM_64BIT
Aneesh Bansal43421822015-10-29 22:58:03 +053099 sec_out32(&ctx->sg_tbl[ctx->sg_num].addr_hi, (uint32_t)(addr >> 32));
gaurav ranaef201592015-02-20 12:51:46 +0530100#else
Aneesh Bansal43421822015-10-29 22:58:03 +0530101 sec_out32(&ctx->sg_tbl[ctx->sg_num].addr_hi, 0x0);
gaurav ranaef201592015-02-20 12:51:46 +0530102#endif
Ye Li3c3e9a12021-03-25 17:30:36 +0800103 sec_out32(&ctx->sg_tbl[ctx->sg_num].addr_lo, (caam_dma_addr_t)addr);
gaurav ranaef201592015-02-20 12:51:46 +0530104
105 sec_out32(&ctx->sg_tbl[ctx->sg_num].len_flag,
106 (size & SG_ENTRY_LENGTH_MASK));
107
108 ctx->sg_num++;
109
110 if (is_last) {
111 final = sec_in32(&ctx->sg_tbl[ctx->sg_num - 1].len_flag) |
112 SG_ENTRY_FINAL_BIT;
113 sec_out32(&ctx->sg_tbl[ctx->sg_num - 1].len_flag, final);
114 }
115
116 return 0;
117}
118
119/*
120 * Perform progressive hashing on the given buffer and copy hash at
121 * destination buffer
122 *
Kshitiz Varshneyc4686322021-09-19 17:09:53 +0200123 * The context is freed after successful completion of hash operation.
124 * In case of failure, context is not freed.
gaurav ranaef201592015-02-20 12:51:46 +0530125 * @hash_ctx: Pointer to the context for hashing
126 * @dest_buf: Pointer to the destination buffer where hash is to be copied
127 * @size: Size of the buffer being hashed
128 * @caam_algo: Enum for SHA1 or SHA256
129 * @return 0 if ok, -EINVAL on error
130 */
131static int caam_hash_finish(void *hash_ctx, void *dest_buf,
132 int size, enum caam_hash_algos caam_algo)
133{
134 uint32_t len = 0;
135 struct sha_ctx *ctx = hash_ctx;
136 int i = 0, ret = 0;
137
138 if (size < driver_hash[caam_algo].digestsize) {
gaurav ranaef201592015-02-20 12:51:46 +0530139 return -EINVAL;
140 }
141
142 for (i = 0; i < ctx->sg_num; i++)
143 len += (sec_in32(&ctx->sg_tbl[i].len_flag) &
144 SG_ENTRY_LENGTH_MASK);
145
146 inline_cnstr_jobdesc_hash(ctx->sha_desc, (uint8_t *)ctx->sg_tbl, len,
147 ctx->hash,
148 driver_hash[caam_algo].alg_type,
149 driver_hash[caam_algo].digestsize,
150 1);
151
152 ret = run_descriptor_jr(ctx->sha_desc);
153
Kshitiz Varshneyc4686322021-09-19 17:09:53 +0200154 if (ret) {
gaurav ranaef201592015-02-20 12:51:46 +0530155 debug("Error %x\n", ret);
Kshitiz Varshneyc4686322021-09-19 17:09:53 +0200156 return ret;
157 } else {
gaurav ranaef201592015-02-20 12:51:46 +0530158 memcpy(dest_buf, ctx->hash, sizeof(ctx->hash));
Kshitiz Varshneyc4686322021-09-19 17:09:53 +0200159 }
gaurav ranaef201592015-02-20 12:51:46 +0530160 free(ctx);
161 return ret;
162}
163
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530164int caam_hash(const unsigned char *pbuf, unsigned int buf_len,
165 unsigned char *pout, enum caam_hash_algos algo)
166{
167 int ret = 0;
168 uint32_t *desc;
Breno Lima45e65122018-01-17 10:03:45 -0200169 unsigned int size;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530170
Breno Lima45e65122018-01-17 10:03:45 -0200171 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530172 if (!desc) {
173 debug("Not enough memory for descriptor allocation\n");
gaurav ranaef201592015-02-20 12:51:46 +0530174 return -ENOMEM;
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530175 }
176
Breno Lima45e65122018-01-17 10:03:45 -0200177 if (!IS_ALIGNED((uintptr_t)pbuf, ARCH_DMA_MINALIGN) ||
178 !IS_ALIGNED((uintptr_t)pout, ARCH_DMA_MINALIGN)) {
179 puts("Error: Address arguments are not aligned\n");
180 return -EINVAL;
181 }
182
183 size = ALIGN(buf_len, ARCH_DMA_MINALIGN);
184 flush_dcache_range((unsigned long)pbuf, (unsigned long)pbuf + size);
185
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530186 inline_cnstr_jobdesc_hash(desc, pbuf, buf_len, pout,
187 driver_hash[algo].alg_type,
188 driver_hash[algo].digestsize,
189 0);
190
Breno Lima45e65122018-01-17 10:03:45 -0200191 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
192 flush_dcache_range((unsigned long)desc, (unsigned long)desc + size);
193
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530194 ret = run_descriptor_jr(desc);
195
Breno Lima45e65122018-01-17 10:03:45 -0200196 size = ALIGN(driver_hash[algo].digestsize, ARCH_DMA_MINALIGN);
197 invalidate_dcache_range((unsigned long)pout,
198 (unsigned long)pout + size);
199
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530200 free(desc);
201 return ret;
202}
203
204void hw_sha256(const unsigned char *pbuf, unsigned int buf_len,
205 unsigned char *pout, unsigned int chunk_size)
206{
207 if (caam_hash(pbuf, buf_len, pout, SHA256))
208 printf("CAAM was not setup properly or it is faulty\n");
209}
210
211void hw_sha1(const unsigned char *pbuf, unsigned int buf_len,
212 unsigned char *pout, unsigned int chunk_size)
213{
214 if (caam_hash(pbuf, buf_len, pout, SHA1))
215 printf("CAAM was not setup properly or it is faulty\n");
216}
gaurav ranaef201592015-02-20 12:51:46 +0530217
218int hw_sha_init(struct hash_algo *algo, void **ctxp)
219{
220 return caam_hash_init(ctxp, get_hash_type(algo));
221}
222
223int hw_sha_update(struct hash_algo *algo, void *ctx, const void *buf,
224 unsigned int size, int is_last)
225{
226 return caam_hash_update(ctx, buf, size, is_last, get_hash_type(algo));
227}
228
229int hw_sha_finish(struct hash_algo *algo, void *ctx, void *dest_buf,
230 int size)
231{
232 return caam_hash_finish(ctx, dest_buf, size, get_hash_type(algo));
233}