2 * Copyright 2014 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm-generic/errno.h>
17 #define CRYPTO_MAX_ALG_NAME 80
18 #define SHA1_DIGEST_SIZE 20
19 #define SHA256_DIGEST_SIZE 32
21 struct caam_hash_template {
22 char name[CRYPTO_MAX_ALG_NAME];
23 unsigned int digestsize;
27 enum caam_hash_algos {
32 static struct caam_hash_template driver_hash[] = {
35 .digestsize = SHA1_DIGEST_SIZE,
36 .alg_type = OP_ALG_ALGSEL_SHA1,
40 .digestsize = SHA256_DIGEST_SIZE,
41 .alg_type = OP_ALG_ALGSEL_SHA256,
45 static enum caam_hash_algos get_hash_type(struct hash_algo *algo)
47 if (!strcmp(algo->name, driver_hash[SHA1].name))
53 /* Create the context for progressive hashing using h/w acceleration.
55 * @ctxp: Pointer to the pointer of the context for hashing
56 * @caam_algo: Enum for SHA1 or SHA256
57 * @return 0 if ok, -ENOMEM on error
59 static int caam_hash_init(void **ctxp, enum caam_hash_algos caam_algo)
61 *ctxp = calloc(1, sizeof(struct sha_ctx));
63 debug("Cannot allocate memory for context\n");
70 * Update sg table for progressive hashing using h/w acceleration
72 * The context is freed by this function if an error occurs.
73 * We support at most 32 Scatter/Gather Entries.
75 * @hash_ctx: Pointer to the context for hashing
76 * @buf: Pointer to the buffer being hashed
77 * @size: Size of the buffer being hashed
78 * @is_last: 1 if this is the last update; 0 otherwise
79 * @caam_algo: Enum for SHA1 or SHA256
80 * @return 0 if ok, -EINVAL on error
82 static int caam_hash_update(void *hash_ctx, const void *buf,
83 unsigned int size, int is_last,
84 enum caam_hash_algos caam_algo)
87 dma_addr_t addr = virt_to_phys((void *)buf);
88 struct sha_ctx *ctx = hash_ctx;
90 if (ctx->sg_num >= MAX_SG_32) {
95 #ifdef CONFIG_PHYS_64BIT
96 ctx->sg_tbl[ctx->sg_num].addr_hi = addr >> 32;
98 ctx->sg_tbl[ctx->sg_num].addr_hi = 0x0;
100 ctx->sg_tbl[ctx->sg_num].addr_lo = addr;
102 sec_out32(&ctx->sg_tbl[ctx->sg_num].len_flag,
103 (size & SG_ENTRY_LENGTH_MASK));
108 final = sec_in32(&ctx->sg_tbl[ctx->sg_num - 1].len_flag) |
110 sec_out32(&ctx->sg_tbl[ctx->sg_num - 1].len_flag, final);
117 * Perform progressive hashing on the given buffer and copy hash at
120 * The context is freed after completion of hash operation.
122 * @hash_ctx: Pointer to the context for hashing
123 * @dest_buf: Pointer to the destination buffer where hash is to be copied
124 * @size: Size of the buffer being hashed
125 * @caam_algo: Enum for SHA1 or SHA256
126 * @return 0 if ok, -EINVAL on error
128 static int caam_hash_finish(void *hash_ctx, void *dest_buf,
129 int size, enum caam_hash_algos caam_algo)
132 struct sha_ctx *ctx = hash_ctx;
135 if (size < driver_hash[caam_algo].digestsize) {
140 for (i = 0; i < ctx->sg_num; i++)
141 len += (sec_in32(&ctx->sg_tbl[i].len_flag) &
142 SG_ENTRY_LENGTH_MASK);
144 inline_cnstr_jobdesc_hash(ctx->sha_desc, (uint8_t *)ctx->sg_tbl, len,
146 driver_hash[caam_algo].alg_type,
147 driver_hash[caam_algo].digestsize,
150 ret = run_descriptor_jr(ctx->sha_desc);
153 debug("Error %x\n", ret);
155 memcpy(dest_buf, ctx->hash, sizeof(ctx->hash));
161 int caam_hash(const unsigned char *pbuf, unsigned int buf_len,
162 unsigned char *pout, enum caam_hash_algos algo)
167 desc = malloc(sizeof(int) * MAX_CAAM_DESCSIZE);
169 debug("Not enough memory for descriptor allocation\n");
173 inline_cnstr_jobdesc_hash(desc, pbuf, buf_len, pout,
174 driver_hash[algo].alg_type,
175 driver_hash[algo].digestsize,
178 ret = run_descriptor_jr(desc);
184 void hw_sha256(const unsigned char *pbuf, unsigned int buf_len,
185 unsigned char *pout, unsigned int chunk_size)
187 if (caam_hash(pbuf, buf_len, pout, SHA256))
188 printf("CAAM was not setup properly or it is faulty\n");
191 void hw_sha1(const unsigned char *pbuf, unsigned int buf_len,
192 unsigned char *pout, unsigned int chunk_size)
194 if (caam_hash(pbuf, buf_len, pout, SHA1))
195 printf("CAAM was not setup properly or it is faulty\n");
198 int hw_sha_init(struct hash_algo *algo, void **ctxp)
200 return caam_hash_init(ctxp, get_hash_type(algo));
203 int hw_sha_update(struct hash_algo *algo, void *ctx, const void *buf,
204 unsigned int size, int is_last)
206 return caam_hash_update(ctx, buf, size, is_last, get_hash_type(algo));
209 int hw_sha_finish(struct hash_algo *algo, void *ctx, void *dest_buf,
212 return caam_hash_finish(ctx, dest_buf, size, get_hash_type(algo));