xref: /openbmc/u-boot/drivers/crypto/fsl/fsl_hash.c (revision f13606b7)
1 /*
2  * Copyright 2014 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  *
6  */
7 
8 #include <common.h>
9 #include <malloc.h>
10 #include "jobdesc.h"
11 #include "desc.h"
12 #include "jr.h"
13 
14 #define CRYPTO_MAX_ALG_NAME	80
15 #define SHA1_DIGEST_SIZE        20
16 #define SHA256_DIGEST_SIZE      32
17 
18 struct caam_hash_template {
19 	char name[CRYPTO_MAX_ALG_NAME];
20 	unsigned int digestsize;
21 	u32 alg_type;
22 };
23 
24 enum caam_hash_algos {
25 	SHA1 = 0,
26 	SHA256
27 };
28 
29 static struct caam_hash_template driver_hash[] = {
30 	{
31 		.name = "sha1",
32 		.digestsize = SHA1_DIGEST_SIZE,
33 		.alg_type = OP_ALG_ALGSEL_SHA1,
34 	},
35 	{
36 		.name = "sha256",
37 		.digestsize = SHA256_DIGEST_SIZE,
38 		.alg_type = OP_ALG_ALGSEL_SHA256,
39 	},
40 };
41 
42 int caam_hash(const unsigned char *pbuf, unsigned int buf_len,
43 	      unsigned char *pout, enum caam_hash_algos algo)
44 {
45 	int ret = 0;
46 	uint32_t *desc;
47 
48 	desc = malloc(sizeof(int) * MAX_CAAM_DESCSIZE);
49 	if (!desc) {
50 		debug("Not enough memory for descriptor allocation\n");
51 		return -1;
52 	}
53 
54 	inline_cnstr_jobdesc_hash(desc, pbuf, buf_len, pout,
55 				  driver_hash[algo].alg_type,
56 				  driver_hash[algo].digestsize,
57 				  0);
58 
59 	ret = run_descriptor_jr(desc);
60 
61 	free(desc);
62 	return ret;
63 }
64 
65 void hw_sha256(const unsigned char *pbuf, unsigned int buf_len,
66 			unsigned char *pout, unsigned int chunk_size)
67 {
68 	if (caam_hash(pbuf, buf_len, pout, SHA256))
69 		printf("CAAM was not setup properly or it is faulty\n");
70 }
71 
72 void hw_sha1(const unsigned char *pbuf, unsigned int buf_len,
73 			unsigned char *pout, unsigned int chunk_size)
74 {
75 	if (caam_hash(pbuf, buf_len, pout, SHA1))
76 		printf("CAAM was not setup properly or it is faulty\n");
77 }
78