xref: /openbmc/u-boot/common/hash.c (revision 45e7dfa9)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2460408efSSimon Glass /*
3460408efSSimon Glass  * Copyright (c) 2012 The Chromium OS Authors.
4460408efSSimon Glass  *
5460408efSSimon Glass  * (C) Copyright 2011
6460408efSSimon Glass  * Joe Hershberger, National Instruments, joe.hershberger@ni.com
7460408efSSimon Glass  *
8460408efSSimon Glass  * (C) Copyright 2000
9460408efSSimon Glass  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10460408efSSimon Glass  */
11460408efSSimon Glass 
122dd90027SRuchika Gupta #ifndef USE_HOSTCC
13460408efSSimon Glass #include <common.h>
14460408efSSimon Glass #include <command.h>
15bf007ebbSHung-ying Tyan #include <malloc.h>
160eb25b61SJoe Hershberger #include <mapmem.h>
171f9c9280SAkshay Saraswat #include <hw_sha.h>
18bd091b67SSimon Glass #include <asm/io.h>
191221ce45SMasahiro Yamada #include <linux/errno.h>
202dd90027SRuchika Gupta #else
212dd90027SRuchika Gupta #include "mkimage.h"
222dd90027SRuchika Gupta #include <time.h>
232dd90027SRuchika Gupta #include <image.h>
242dd90027SRuchika Gupta #endif /* !USE_HOSTCC*/
252dd90027SRuchika Gupta 
262dd90027SRuchika Gupta #include <hash.h>
272dd90027SRuchika Gupta #include <u-boot/crc.h>
282dd90027SRuchika Gupta #include <u-boot/sha1.h>
292dd90027SRuchika Gupta #include <u-boot/sha256.h>
30e9221d03SReuben Dowle #include <u-boot/sha512.h>
312dd90027SRuchika Gupta #include <u-boot/md5.h>
32460408efSSimon Glass 
3378eda89eSTom Rini #if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
hash_init_sha1(struct hash_algo * algo,void ** ctxp)34bf007ebbSHung-ying Tyan static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
35bf007ebbSHung-ying Tyan {
36bf007ebbSHung-ying Tyan 	sha1_context *ctx = malloc(sizeof(sha1_context));
37bf007ebbSHung-ying Tyan 	sha1_starts(ctx);
38bf007ebbSHung-ying Tyan 	*ctxp = ctx;
39bf007ebbSHung-ying Tyan 	return 0;
40bf007ebbSHung-ying Tyan }
41bf007ebbSHung-ying Tyan 
hash_update_sha1(struct hash_algo * algo,void * ctx,const void * buf,unsigned int size,int is_last)42bf007ebbSHung-ying Tyan static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
43bf007ebbSHung-ying Tyan 			    unsigned int size, int is_last)
44bf007ebbSHung-ying Tyan {
45bf007ebbSHung-ying Tyan 	sha1_update((sha1_context *)ctx, buf, size);
46bf007ebbSHung-ying Tyan 	return 0;
47bf007ebbSHung-ying Tyan }
48bf007ebbSHung-ying Tyan 
hash_finish_sha1(struct hash_algo * algo,void * ctx,void * dest_buf,int size)49bf007ebbSHung-ying Tyan static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
50bf007ebbSHung-ying Tyan 			    int size)
51bf007ebbSHung-ying Tyan {
52bf007ebbSHung-ying Tyan 	if (size < algo->digest_size)
53bf007ebbSHung-ying Tyan 		return -1;
54bf007ebbSHung-ying Tyan 
55bf007ebbSHung-ying Tyan 	sha1_finish((sha1_context *)ctx, dest_buf);
56bf007ebbSHung-ying Tyan 	free(ctx);
57bf007ebbSHung-ying Tyan 	return 0;
58bf007ebbSHung-ying Tyan }
59bf007ebbSHung-ying Tyan #endif
60bf007ebbSHung-ying Tyan 
6178eda89eSTom Rini #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
hash_init_sha256(struct hash_algo * algo,void ** ctxp)62bf007ebbSHung-ying Tyan static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
63bf007ebbSHung-ying Tyan {
64bf007ebbSHung-ying Tyan 	sha256_context *ctx = malloc(sizeof(sha256_context));
65bf007ebbSHung-ying Tyan 	sha256_starts(ctx);
66bf007ebbSHung-ying Tyan 	*ctxp = ctx;
67bf007ebbSHung-ying Tyan 	return 0;
68bf007ebbSHung-ying Tyan }
69bf007ebbSHung-ying Tyan 
hash_update_sha256(struct hash_algo * algo,void * ctx,const void * buf,unsigned int size,int is_last)70bf007ebbSHung-ying Tyan static int hash_update_sha256(struct hash_algo *algo, void *ctx,
71bf007ebbSHung-ying Tyan 			      const void *buf, unsigned int size, int is_last)
72bf007ebbSHung-ying Tyan {
73bf007ebbSHung-ying Tyan 	sha256_update((sha256_context *)ctx, buf, size);
74bf007ebbSHung-ying Tyan 	return 0;
75bf007ebbSHung-ying Tyan }
76bf007ebbSHung-ying Tyan 
hash_finish_sha256(struct hash_algo * algo,void * ctx,void * dest_buf,int size)77bf007ebbSHung-ying Tyan static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
78bf007ebbSHung-ying Tyan 			      *dest_buf, int size)
79bf007ebbSHung-ying Tyan {
80bf007ebbSHung-ying Tyan 	if (size < algo->digest_size)
81bf007ebbSHung-ying Tyan 		return -1;
82bf007ebbSHung-ying Tyan 
83bf007ebbSHung-ying Tyan 	sha256_finish((sha256_context *)ctx, dest_buf);
84bf007ebbSHung-ying Tyan 	free(ctx);
85bf007ebbSHung-ying Tyan 	return 0;
86bf007ebbSHung-ying Tyan }
87bf007ebbSHung-ying Tyan #endif
88bf007ebbSHung-ying Tyan 
89*45e7dfa9SJoel Stanley #if defined(CONFIG_SHA384) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
hash_init_sha384(struct hash_algo * algo,void ** ctxp)90e9221d03SReuben Dowle static int hash_init_sha384(struct hash_algo *algo, void **ctxp)
91e9221d03SReuben Dowle {
92e9221d03SReuben Dowle 	sha512_context *ctx = malloc(sizeof(sha512_context));
93e9221d03SReuben Dowle 	sha384_starts(ctx);
94e9221d03SReuben Dowle 	*ctxp = ctx;
95e9221d03SReuben Dowle 	return 0;
96e9221d03SReuben Dowle }
97e9221d03SReuben Dowle 
hash_update_sha384(struct hash_algo * algo,void * ctx,const void * buf,unsigned int size,int is_last)98e9221d03SReuben Dowle static int hash_update_sha384(struct hash_algo *algo, void *ctx,
99e9221d03SReuben Dowle 			      const void *buf, unsigned int size, int is_last)
100e9221d03SReuben Dowle {
101e9221d03SReuben Dowle 	sha384_update((sha512_context *)ctx, buf, size);
102e9221d03SReuben Dowle 	return 0;
103e9221d03SReuben Dowle }
104e9221d03SReuben Dowle 
hash_finish_sha384(struct hash_algo * algo,void * ctx,void * dest_buf,int size)105e9221d03SReuben Dowle static int hash_finish_sha384(struct hash_algo *algo, void *ctx, void
106e9221d03SReuben Dowle 			      *dest_buf, int size)
107e9221d03SReuben Dowle {
108e9221d03SReuben Dowle 	if (size < algo->digest_size)
109e9221d03SReuben Dowle 		return -1;
110e9221d03SReuben Dowle 
111e9221d03SReuben Dowle 	sha384_finish((sha512_context *)ctx, dest_buf);
112e9221d03SReuben Dowle 	free(ctx);
113e9221d03SReuben Dowle 	return 0;
114e9221d03SReuben Dowle }
115e9221d03SReuben Dowle #endif
116e9221d03SReuben Dowle 
117*45e7dfa9SJoel Stanley #if defined(CONFIG_SHA512) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
hash_init_sha512(struct hash_algo * algo,void ** ctxp)118e9221d03SReuben Dowle static int hash_init_sha512(struct hash_algo *algo, void **ctxp)
119e9221d03SReuben Dowle {
120e9221d03SReuben Dowle 	sha512_context *ctx = malloc(sizeof(sha512_context));
121e9221d03SReuben Dowle 	sha512_starts(ctx);
122e9221d03SReuben Dowle 	*ctxp = ctx;
123e9221d03SReuben Dowle 	return 0;
124e9221d03SReuben Dowle }
125e9221d03SReuben Dowle 
hash_update_sha512(struct hash_algo * algo,void * ctx,const void * buf,unsigned int size,int is_last)126e9221d03SReuben Dowle static int hash_update_sha512(struct hash_algo *algo, void *ctx,
127e9221d03SReuben Dowle 			      const void *buf, unsigned int size, int is_last)
128e9221d03SReuben Dowle {
129e9221d03SReuben Dowle 	sha512_update((sha512_context *)ctx, buf, size);
130e9221d03SReuben Dowle 	return 0;
131e9221d03SReuben Dowle }
132e9221d03SReuben Dowle 
hash_finish_sha512(struct hash_algo * algo,void * ctx,void * dest_buf,int size)133e9221d03SReuben Dowle static int hash_finish_sha512(struct hash_algo *algo, void *ctx, void
134e9221d03SReuben Dowle 			      *dest_buf, int size)
135e9221d03SReuben Dowle {
136e9221d03SReuben Dowle 	if (size < algo->digest_size)
137e9221d03SReuben Dowle 		return -1;
138e9221d03SReuben Dowle 
139e9221d03SReuben Dowle 	sha512_finish((sha512_context *)ctx, dest_buf);
140e9221d03SReuben Dowle 	free(ctx);
141e9221d03SReuben Dowle 	return 0;
142e9221d03SReuben Dowle }
143e9221d03SReuben Dowle #endif
144e9221d03SReuben Dowle 
145e9221d03SReuben Dowle 
hash_init_crc16_ccitt(struct hash_algo * algo,void ** ctxp)14651c2345bSPhilipp Tomsich static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
14751c2345bSPhilipp Tomsich {
14851c2345bSPhilipp Tomsich 	uint16_t *ctx = malloc(sizeof(uint16_t));
14951c2345bSPhilipp Tomsich 	*ctx = 0;
15051c2345bSPhilipp Tomsich 	*ctxp = ctx;
15151c2345bSPhilipp Tomsich 	return 0;
15251c2345bSPhilipp Tomsich }
15351c2345bSPhilipp Tomsich 
hash_update_crc16_ccitt(struct hash_algo * algo,void * ctx,const void * buf,unsigned int size,int is_last)15451c2345bSPhilipp Tomsich static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
15551c2345bSPhilipp Tomsich 				   const void *buf, unsigned int size,
15651c2345bSPhilipp Tomsich 				   int is_last)
15751c2345bSPhilipp Tomsich {
15851c2345bSPhilipp Tomsich 	*((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
15951c2345bSPhilipp Tomsich 	return 0;
16051c2345bSPhilipp Tomsich }
16151c2345bSPhilipp Tomsich 
hash_finish_crc16_ccitt(struct hash_algo * algo,void * ctx,void * dest_buf,int size)16251c2345bSPhilipp Tomsich static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
16351c2345bSPhilipp Tomsich 				   void *dest_buf, int size)
16451c2345bSPhilipp Tomsich {
16551c2345bSPhilipp Tomsich 	if (size < algo->digest_size)
16651c2345bSPhilipp Tomsich 		return -1;
16751c2345bSPhilipp Tomsich 
16851c2345bSPhilipp Tomsich 	*((uint16_t *)dest_buf) = *((uint16_t *)ctx);
16951c2345bSPhilipp Tomsich 	free(ctx);
17051c2345bSPhilipp Tomsich 	return 0;
17151c2345bSPhilipp Tomsich }
17251c2345bSPhilipp Tomsich 
hash_init_crc32(struct hash_algo * algo,void ** ctxp)173bf007ebbSHung-ying Tyan static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
174bf007ebbSHung-ying Tyan {
175bf007ebbSHung-ying Tyan 	uint32_t *ctx = malloc(sizeof(uint32_t));
176bf007ebbSHung-ying Tyan 	*ctx = 0;
177bf007ebbSHung-ying Tyan 	*ctxp = ctx;
178bf007ebbSHung-ying Tyan 	return 0;
179bf007ebbSHung-ying Tyan }
180bf007ebbSHung-ying Tyan 
hash_update_crc32(struct hash_algo * algo,void * ctx,const void * buf,unsigned int size,int is_last)181bf007ebbSHung-ying Tyan static int hash_update_crc32(struct hash_algo *algo, void *ctx,
182bf007ebbSHung-ying Tyan 			     const void *buf, unsigned int size, int is_last)
183bf007ebbSHung-ying Tyan {
184bf007ebbSHung-ying Tyan 	*((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
185bf007ebbSHung-ying Tyan 	return 0;
186bf007ebbSHung-ying Tyan }
187bf007ebbSHung-ying Tyan 
hash_finish_crc32(struct hash_algo * algo,void * ctx,void * dest_buf,int size)188bf007ebbSHung-ying Tyan static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
189bf007ebbSHung-ying Tyan 			     int size)
190bf007ebbSHung-ying Tyan {
191bf007ebbSHung-ying Tyan 	if (size < algo->digest_size)
192bf007ebbSHung-ying Tyan 		return -1;
193bf007ebbSHung-ying Tyan 
194bf007ebbSHung-ying Tyan 	*((uint32_t *)dest_buf) = *((uint32_t *)ctx);
195bf007ebbSHung-ying Tyan 	free(ctx);
196bf007ebbSHung-ying Tyan 	return 0;
197bf007ebbSHung-ying Tyan }
198bf007ebbSHung-ying Tyan 
199460408efSSimon Glass /*
20078eda89eSTom Rini  * These are the hash algorithms we support.  If we have hardware acceleration
20178eda89eSTom Rini  * is enable we will use that, otherwise a software version of the algorithm.
20278eda89eSTom Rini  * Note that algorithm names must be in lower case.
203460408efSSimon Glass  */
204460408efSSimon Glass static struct hash_algo hash_algo[] = {
20546fe2c04SRuchika Gupta #ifdef CONFIG_SHA1
206460408efSSimon Glass 	{
20778eda89eSTom Rini 		.name 		= "sha1",
20878eda89eSTom Rini 		.digest_size	= SHA1_SUM_LEN,
20978eda89eSTom Rini 		.chunk_size	= CHUNKSZ_SHA1,
21078eda89eSTom Rini #ifdef CONFIG_SHA_HW_ACCEL
21178eda89eSTom Rini 		.hash_func_ws	= hw_sha1,
21278eda89eSTom Rini #else
21378eda89eSTom Rini 		.hash_func_ws	= sha1_csum_wd,
21478eda89eSTom Rini #endif
21578eda89eSTom Rini #ifdef CONFIG_SHA_PROG_HW_ACCEL
21678eda89eSTom Rini 		.hash_init	= hw_sha_init,
21778eda89eSTom Rini 		.hash_update	= hw_sha_update,
21878eda89eSTom Rini 		.hash_finish	= hw_sha_finish,
21978eda89eSTom Rini #else
22078eda89eSTom Rini 		.hash_init	= hash_init_sha1,
22178eda89eSTom Rini 		.hash_update	= hash_update_sha1,
22278eda89eSTom Rini 		.hash_finish	= hash_finish_sha1,
22378eda89eSTom Rini #endif
224460408efSSimon Glass 	},
225460408efSSimon Glass #endif
226460408efSSimon Glass #ifdef CONFIG_SHA256
227460408efSSimon Glass 	{
22878eda89eSTom Rini 		.name		= "sha256",
22978eda89eSTom Rini 		.digest_size	= SHA256_SUM_LEN,
23078eda89eSTom Rini 		.chunk_size	= CHUNKSZ_SHA256,
23178eda89eSTom Rini #ifdef CONFIG_SHA_HW_ACCEL
23278eda89eSTom Rini 		.hash_func_ws	= hw_sha256,
23378eda89eSTom Rini #else
23478eda89eSTom Rini 		.hash_func_ws	= sha256_csum_wd,
23578eda89eSTom Rini #endif
23678eda89eSTom Rini #ifdef CONFIG_SHA_PROG_HW_ACCEL
23778eda89eSTom Rini 		.hash_init	= hw_sha_init,
23878eda89eSTom Rini 		.hash_update	= hw_sha_update,
23978eda89eSTom Rini 		.hash_finish	= hw_sha_finish,
24078eda89eSTom Rini #else
24178eda89eSTom Rini 		.hash_init	= hash_init_sha256,
24278eda89eSTom Rini 		.hash_update	= hash_update_sha256,
24378eda89eSTom Rini 		.hash_finish	= hash_finish_sha256,
24478eda89eSTom Rini #endif
245460408efSSimon Glass 	},
246460408efSSimon Glass #endif
247e9221d03SReuben Dowle #ifdef CONFIG_SHA384
248e9221d03SReuben Dowle 	{
249e9221d03SReuben Dowle 		.name		= "sha384",
250e9221d03SReuben Dowle 		.digest_size	= SHA384_SUM_LEN,
251e9221d03SReuben Dowle 		.chunk_size	= CHUNKSZ_SHA384,
252*45e7dfa9SJoel Stanley #ifdef CONFIG_SHA_HW_ACCEL
253*45e7dfa9SJoel Stanley 		.hash_func_ws	= hw_sha384,
254*45e7dfa9SJoel Stanley #else
255e9221d03SReuben Dowle 		.hash_func_ws	= sha384_csum_wd,
256*45e7dfa9SJoel Stanley #endif
257*45e7dfa9SJoel Stanley #ifdef CONFIG_SHA_PROG_HW_ACCEL
258*45e7dfa9SJoel Stanley 		.hash_init	= hw_sha_init,
259*45e7dfa9SJoel Stanley 		.hash_update	= hw_sha_update,
260*45e7dfa9SJoel Stanley 		.hash_finish	= hw_sha_finish,
261*45e7dfa9SJoel Stanley #else
262e9221d03SReuben Dowle 		.hash_init	= hash_init_sha384,
263e9221d03SReuben Dowle 		.hash_update	= hash_update_sha384,
264e9221d03SReuben Dowle 		.hash_finish	= hash_finish_sha384,
265*45e7dfa9SJoel Stanley #endif
266e9221d03SReuben Dowle 	},
267e9221d03SReuben Dowle #endif
268e9221d03SReuben Dowle #ifdef CONFIG_SHA512
269e9221d03SReuben Dowle 	{
270e9221d03SReuben Dowle 		.name		= "sha512",
271e9221d03SReuben Dowle 		.digest_size	= SHA512_SUM_LEN,
272e9221d03SReuben Dowle 		.chunk_size	= CHUNKSZ_SHA512,
273*45e7dfa9SJoel Stanley #ifdef CONFIG_SHA_HW_ACCEL
274*45e7dfa9SJoel Stanley 		.hash_func_ws	= hw_sha512,
275*45e7dfa9SJoel Stanley #else
276e9221d03SReuben Dowle 		.hash_func_ws	= sha512_csum_wd,
277*45e7dfa9SJoel Stanley #endif
278*45e7dfa9SJoel Stanley #ifdef CONFIG_SHA_PROG_HW_ACCEL
279*45e7dfa9SJoel Stanley 		.hash_init	= hw_sha_init,
280*45e7dfa9SJoel Stanley 		.hash_update	= hw_sha_update,
281*45e7dfa9SJoel Stanley 		.hash_finish	= hw_sha_finish,
282*45e7dfa9SJoel Stanley #else
283e9221d03SReuben Dowle 		.hash_init	= hash_init_sha512,
284e9221d03SReuben Dowle 		.hash_update	= hash_update_sha512,
285e9221d03SReuben Dowle 		.hash_finish	= hash_finish_sha512,
286*45e7dfa9SJoel Stanley #endif
287e9221d03SReuben Dowle 	},
288e9221d03SReuben Dowle #endif
289d20a40deSSimon Glass 	{
29051c2345bSPhilipp Tomsich 		.name		= "crc16-ccitt",
29151c2345bSPhilipp Tomsich 		.digest_size	= 2,
29251c2345bSPhilipp Tomsich 		.chunk_size	= CHUNKSZ,
29351c2345bSPhilipp Tomsich 		.hash_func_ws	= crc16_ccitt_wd_buf,
29451c2345bSPhilipp Tomsich 		.hash_init	= hash_init_crc16_ccitt,
29551c2345bSPhilipp Tomsich 		.hash_update	= hash_update_crc16_ccitt,
29651c2345bSPhilipp Tomsich 		.hash_finish	= hash_finish_crc16_ccitt,
29751c2345bSPhilipp Tomsich 	},
29851c2345bSPhilipp Tomsich 	{
29978eda89eSTom Rini 		.name		= "crc32",
30078eda89eSTom Rini 		.digest_size	= 4,
30178eda89eSTom Rini 		.chunk_size	= CHUNKSZ_CRC32,
30278eda89eSTom Rini 		.hash_func_ws	= crc32_wd_buf,
30378eda89eSTom Rini 		.hash_init	= hash_init_crc32,
30478eda89eSTom Rini 		.hash_update	= hash_update_crc32,
30578eda89eSTom Rini 		.hash_finish	= hash_finish_crc32,
306d20a40deSSimon Glass 	},
307460408efSSimon Glass };
308460408efSSimon Glass 
309d20a40deSSimon Glass /* Try to minimize code size for boards that don't want much hashing */
310221a949eSDaniel Thompson #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
311e9221d03SReuben Dowle 	defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH) || \
312e9221d03SReuben Dowle 	defined(CONFIG_SHA384) || defined(CONFIG_SHA512)
313d20a40deSSimon Glass #define multi_hash()	1
314d20a40deSSimon Glass #else
315d20a40deSSimon Glass #define multi_hash()	0
316d20a40deSSimon Glass #endif
317d20a40deSSimon Glass 
hash_lookup_algo(const char * algo_name,struct hash_algo ** algop)3182dd90027SRuchika Gupta int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
3192dd90027SRuchika Gupta {
3202dd90027SRuchika Gupta 	int i;
3212dd90027SRuchika Gupta 
3222dd90027SRuchika Gupta 	for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
3232dd90027SRuchika Gupta 		if (!strcmp(algo_name, hash_algo[i].name)) {
3242dd90027SRuchika Gupta 			*algop = &hash_algo[i];
3252dd90027SRuchika Gupta 			return 0;
3262dd90027SRuchika Gupta 		}
3272dd90027SRuchika Gupta 	}
3282dd90027SRuchika Gupta 
3292dd90027SRuchika Gupta 	debug("Unknown hash algorithm '%s'\n", algo_name);
3302dd90027SRuchika Gupta 	return -EPROTONOSUPPORT;
3312dd90027SRuchika Gupta }
3322dd90027SRuchika Gupta 
hash_progressive_lookup_algo(const char * algo_name,struct hash_algo ** algop)3332dd90027SRuchika Gupta int hash_progressive_lookup_algo(const char *algo_name,
3342dd90027SRuchika Gupta 				 struct hash_algo **algop)
3352dd90027SRuchika Gupta {
3362dd90027SRuchika Gupta 	int i;
3372dd90027SRuchika Gupta 
3382dd90027SRuchika Gupta 	for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
3392dd90027SRuchika Gupta 		if (!strcmp(algo_name, hash_algo[i].name)) {
3402dd90027SRuchika Gupta 			if (hash_algo[i].hash_init) {
3412dd90027SRuchika Gupta 				*algop = &hash_algo[i];
3422dd90027SRuchika Gupta 				return 0;
3432dd90027SRuchika Gupta 			}
3442dd90027SRuchika Gupta 		}
3452dd90027SRuchika Gupta 	}
3462dd90027SRuchika Gupta 
3472dd90027SRuchika Gupta 	debug("Unknown hash algorithm '%s'\n", algo_name);
3482dd90027SRuchika Gupta 	return -EPROTONOSUPPORT;
3492dd90027SRuchika Gupta }
3502dd90027SRuchika Gupta 
3512dd90027SRuchika Gupta #ifndef USE_HOSTCC
hash_parse_string(const char * algo_name,const char * str,uint8_t * result)3528f0b1e24SStefan Roese int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
3538f0b1e24SStefan Roese {
3548f0b1e24SStefan Roese 	struct hash_algo *algo;
3558f0b1e24SStefan Roese 	int ret;
3568f0b1e24SStefan Roese 	int i;
3578f0b1e24SStefan Roese 
3588f0b1e24SStefan Roese 	ret = hash_lookup_algo(algo_name, &algo);
3598f0b1e24SStefan Roese 	if (ret)
3608f0b1e24SStefan Roese 		return ret;
3618f0b1e24SStefan Roese 
3628f0b1e24SStefan Roese 	for (i = 0; i < algo->digest_size; i++) {
3638f0b1e24SStefan Roese 		char chr[3];
3648f0b1e24SStefan Roese 
3658f0b1e24SStefan Roese 		strncpy(chr, &str[i * 2], 2);
3668f0b1e24SStefan Roese 		result[i] = simple_strtoul(chr, NULL, 16);
3678f0b1e24SStefan Roese 	}
3688f0b1e24SStefan Roese 
3698f0b1e24SStefan Roese 	return 0;
3708f0b1e24SStefan Roese }
3718f0b1e24SStefan Roese 
hash_block(const char * algo_name,const void * data,unsigned int len,uint8_t * output,int * output_size)37248ad68deSTom Rini int hash_block(const char *algo_name, const void *data, unsigned int len,
37348ad68deSTom Rini 	       uint8_t *output, int *output_size)
37448ad68deSTom Rini {
37548ad68deSTom Rini 	struct hash_algo *algo;
37648ad68deSTom Rini 	int ret;
37748ad68deSTom Rini 
37848ad68deSTom Rini 	ret = hash_lookup_algo(algo_name, &algo);
37948ad68deSTom Rini 	if (ret)
38048ad68deSTom Rini 		return ret;
38148ad68deSTom Rini 
38248ad68deSTom Rini 	if (output_size && *output_size < algo->digest_size) {
38348ad68deSTom Rini 		debug("Output buffer size %d too small (need %d bytes)",
38448ad68deSTom Rini 		      *output_size, algo->digest_size);
38548ad68deSTom Rini 		return -ENOSPC;
38648ad68deSTom Rini 	}
38748ad68deSTom Rini 	if (output_size)
38848ad68deSTom Rini 		*output_size = algo->digest_size;
38948ad68deSTom Rini 	algo->hash_func_ws(data, len, output, algo->chunk_size);
39048ad68deSTom Rini 
39148ad68deSTom Rini 	return 0;
39248ad68deSTom Rini }
39348ad68deSTom Rini 
39448ad68deSTom Rini #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
395460408efSSimon Glass /**
396460408efSSimon Glass  * store_result: Store the resulting sum to an address or variable
397460408efSSimon Glass  *
398460408efSSimon Glass  * @algo:		Hash algorithm being used
399460408efSSimon Glass  * @sum:		Hash digest (algo->digest_size bytes)
400460408efSSimon Glass  * @dest:		Destination, interpreted as a hex address if it starts
401d5b76673SSimon Glass  *			with * (or allow_env_vars is 0) or otherwise as an
402d5b76673SSimon Glass  *			environment variable.
403d5b76673SSimon Glass  * @allow_env_vars:	non-zero to permit storing the result to an
404d5b76673SSimon Glass  *			variable environment
405460408efSSimon Glass  */
store_result(struct hash_algo * algo,const uint8_t * sum,const char * dest,int allow_env_vars)40604819a4fSSimon Glass static void store_result(struct hash_algo *algo, const uint8_t *sum,
407d5b76673SSimon Glass 			 const char *dest, int allow_env_vars)
408460408efSSimon Glass {
409460408efSSimon Glass 	unsigned int i;
410d5b76673SSimon Glass 	int env_var = 0;
411460408efSSimon Glass 
412d5b76673SSimon Glass 	/*
413d5b76673SSimon Glass 	 * If environment variables are allowed, then we assume that 'dest'
414d5b76673SSimon Glass 	 * is an environment variable, unless it starts with *, in which
415d5b76673SSimon Glass 	 * case we assume it is an address. If not allowed, it is always an
416d5b76673SSimon Glass 	 * address. This is to support the crc32 command.
417d5b76673SSimon Glass 	 */
418d5b76673SSimon Glass 	if (allow_env_vars) {
419d5b76673SSimon Glass 		if (*dest == '*')
420d5b76673SSimon Glass 			dest++;
421d5b76673SSimon Glass 		else
422d5b76673SSimon Glass 			env_var = 1;
423d5b76673SSimon Glass 	}
424460408efSSimon Glass 
425d5b76673SSimon Glass 	if (env_var) {
426460408efSSimon Glass 		char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
427460408efSSimon Glass 		char *str_ptr = str_output;
428460408efSSimon Glass 
429460408efSSimon Glass 		for (i = 0; i < algo->digest_size; i++) {
430460408efSSimon Glass 			sprintf(str_ptr, "%02x", sum[i]);
431460408efSSimon Glass 			str_ptr += 2;
432460408efSSimon Glass 		}
4338b9cc866SJeroen Hofstee 		*str_ptr = '\0';
434382bee57SSimon Glass 		env_set(dest, str_output);
435d5b76673SSimon Glass 	} else {
436bd091b67SSimon Glass 		ulong addr;
437bd091b67SSimon Glass 		void *buf;
438d5b76673SSimon Glass 
439bd091b67SSimon Glass 		addr = simple_strtoul(dest, NULL, 16);
440bd091b67SSimon Glass 		buf = map_sysmem(addr, algo->digest_size);
441bd091b67SSimon Glass 		memcpy(buf, sum, algo->digest_size);
442bd091b67SSimon Glass 		unmap_sysmem(buf);
443460408efSSimon Glass 	}
444460408efSSimon Glass }
445460408efSSimon Glass 
446460408efSSimon Glass /**
447460408efSSimon Glass  * parse_verify_sum: Parse a hash verification parameter
448460408efSSimon Glass  *
449460408efSSimon Glass  * @algo:		Hash algorithm being used
450460408efSSimon Glass  * @verify_str:		Argument to parse. If it starts with * then it is
451460408efSSimon Glass  *			interpreted as a hex address containing the hash.
452460408efSSimon Glass  *			If the length is exactly the right number of hex digits
453460408efSSimon Glass  *			for the digest size, then we assume it is a hex digest.
454460408efSSimon Glass  *			Otherwise we assume it is an environment variable, and
455460408efSSimon Glass  *			look up its value (it must contain a hex digest).
456460408efSSimon Glass  * @vsum:		Returns binary digest value (algo->digest_size bytes)
457d5b76673SSimon Glass  * @allow_env_vars:	non-zero to permit storing the result to an environment
458d5b76673SSimon Glass  *			variable. If 0 then verify_str is assumed to be an
459d5b76673SSimon Glass  *			address, and the * prefix is not expected.
460460408efSSimon Glass  * @return 0 if ok, non-zero on error
461460408efSSimon Glass  */
parse_verify_sum(struct hash_algo * algo,char * verify_str,uint8_t * vsum,int allow_env_vars)46204819a4fSSimon Glass static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
46304819a4fSSimon Glass 			    uint8_t *vsum, int allow_env_vars)
464460408efSSimon Glass {
465d5b76673SSimon Glass 	int env_var = 0;
466d5b76673SSimon Glass 
467d5b76673SSimon Glass 	/* See comment above in store_result() */
468d5b76673SSimon Glass 	if (allow_env_vars) {
469d5b76673SSimon Glass 		if (*verify_str == '*')
470d5b76673SSimon Glass 			verify_str++;
471d5b76673SSimon Glass 		else
472d5b76673SSimon Glass 			env_var = 1;
473d5b76673SSimon Glass 	}
474d5b76673SSimon Glass 
4753ef46a99SNikolay Dimitrov 	if (!env_var) {
476bd091b67SSimon Glass 		ulong addr;
477bd091b67SSimon Glass 		void *buf;
478460408efSSimon Glass 
479bd091b67SSimon Glass 		addr = simple_strtoul(verify_str, NULL, 16);
480bd091b67SSimon Glass 		buf = map_sysmem(addr, algo->digest_size);
481bd091b67SSimon Glass 		memcpy(vsum, buf, algo->digest_size);
482460408efSSimon Glass 	} else {
483460408efSSimon Glass 		char *vsum_str;
484460408efSSimon Glass 		int digits = algo->digest_size * 2;
485460408efSSimon Glass 
486460408efSSimon Glass 		/*
487460408efSSimon Glass 		 * As with the original code from sha1sum.c, we assume that a
488460408efSSimon Glass 		 * string which matches the digest size exactly is a hex
489460408efSSimon Glass 		 * string and not an environment variable.
490460408efSSimon Glass 		 */
491460408efSSimon Glass 		if (strlen(verify_str) == digits)
492460408efSSimon Glass 			vsum_str = verify_str;
493460408efSSimon Glass 		else {
49400caae6dSSimon Glass 			vsum_str = env_get(verify_str);
495460408efSSimon Glass 			if (vsum_str == NULL || strlen(vsum_str) != digits) {
496460408efSSimon Glass 				printf("Expected %d hex digits in env var\n",
497460408efSSimon Glass 				       digits);
498460408efSSimon Glass 				return 1;
499460408efSSimon Glass 			}
500460408efSSimon Glass 		}
501460408efSSimon Glass 
5028f0b1e24SStefan Roese 		hash_parse_string(algo->name, vsum_str, vsum);
503460408efSSimon Glass 	}
504460408efSSimon Glass 	return 0;
505460408efSSimon Glass }
506460408efSSimon Glass 
hash_show(struct hash_algo * algo,ulong addr,ulong len,uint8_t * output)50748ad68deSTom Rini static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
508460408efSSimon Glass {
509460408efSSimon Glass 	int i;
510460408efSSimon Glass 
511460408efSSimon Glass 	printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
512460408efSSimon Glass 	for (i = 0; i < algo->digest_size; i++)
513460408efSSimon Glass 		printf("%02x", output[i]);
514460408efSSimon Glass }
515460408efSSimon Glass 
hash_command(const char * algo_name,int flags,cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])516d5b76673SSimon Glass int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
517460408efSSimon Glass 		 int argc, char * const argv[])
518460408efSSimon Glass {
519460408efSSimon Glass 	ulong addr, len;
520460408efSSimon Glass 
5213ef46a99SNikolay Dimitrov 	if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
522460408efSSimon Glass 		return CMD_RET_USAGE;
523460408efSSimon Glass 
524d5b76673SSimon Glass 	addr = simple_strtoul(*argv++, NULL, 16);
525d5b76673SSimon Glass 	len = simple_strtoul(*argv++, NULL, 16);
526d5b76673SSimon Glass 
527d20a40deSSimon Glass 	if (multi_hash()) {
528d20a40deSSimon Glass 		struct hash_algo *algo;
529d7af2baaSBreno Lima 		u8 *output;
53004819a4fSSimon Glass 		uint8_t vsum[HASH_MAX_DIGEST_SIZE];
531bd091b67SSimon Glass 		void *buf;
532d20a40deSSimon Glass 
533bf007ebbSHung-ying Tyan 		if (hash_lookup_algo(algo_name, &algo)) {
534460408efSSimon Glass 			printf("Unknown hash algorithm '%s'\n", algo_name);
535460408efSSimon Glass 			return CMD_RET_USAGE;
536460408efSSimon Glass 		}
537460408efSSimon Glass 		argc -= 2;
538460408efSSimon Glass 
539460408efSSimon Glass 		if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
540460408efSSimon Glass 			puts("HASH_MAX_DIGEST_SIZE exceeded\n");
541460408efSSimon Glass 			return 1;
542460408efSSimon Glass 		}
543460408efSSimon Glass 
544d7af2baaSBreno Lima 		output = memalign(ARCH_DMA_MINALIGN,
545d7af2baaSBreno Lima 				  sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
546d7af2baaSBreno Lima 
547bd091b67SSimon Glass 		buf = map_sysmem(addr, len);
548bd091b67SSimon Glass 		algo->hash_func_ws(buf, len, output, algo->chunk_size);
549bd091b67SSimon Glass 		unmap_sysmem(buf);
550460408efSSimon Glass 
551460408efSSimon Glass 		/* Try to avoid code bloat when verify is not needed */
552221a949eSDaniel Thompson #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
553221a949eSDaniel Thompson 	defined(CONFIG_HASH_VERIFY)
554d5b76673SSimon Glass 		if (flags & HASH_FLAG_VERIFY) {
555460408efSSimon Glass #else
556460408efSSimon Glass 		if (0) {
557460408efSSimon Glass #endif
558d5b76673SSimon Glass 			if (parse_verify_sum(algo, *argv, vsum,
559d5b76673SSimon Glass 					flags & HASH_FLAG_ENV)) {
560d20a40deSSimon Glass 				printf("ERROR: %s does not contain a valid "
561d20a40deSSimon Glass 					"%s sum\n", *argv, algo->name);
562460408efSSimon Glass 				return 1;
563460408efSSimon Glass 			}
564460408efSSimon Glass 			if (memcmp(output, vsum, algo->digest_size) != 0) {
565460408efSSimon Glass 				int i;
566460408efSSimon Glass 
56731890ae2SSimon Glass 				hash_show(algo, addr, len, output);
568460408efSSimon Glass 				printf(" != ");
569460408efSSimon Glass 				for (i = 0; i < algo->digest_size; i++)
570460408efSSimon Glass 					printf("%02x", vsum[i]);
571460408efSSimon Glass 				puts(" ** ERROR **\n");
572460408efSSimon Glass 				return 1;
573460408efSSimon Glass 			}
574460408efSSimon Glass 		} else {
57531890ae2SSimon Glass 			hash_show(algo, addr, len, output);
576460408efSSimon Glass 			printf("\n");
577460408efSSimon Glass 
578d5b76673SSimon Glass 			if (argc) {
579d5b76673SSimon Glass 				store_result(algo, output, *argv,
580d5b76673SSimon Glass 					flags & HASH_FLAG_ENV);
581d5b76673SSimon Glass 			}
582d7af2baaSBreno Lima 		unmap_sysmem(output);
583d7af2baaSBreno Lima 
584460408efSSimon Glass 		}
585460408efSSimon Glass 
586d20a40deSSimon Glass 	/* Horrible code size hack for boards that just want crc32 */
587d20a40deSSimon Glass 	} else {
588d20a40deSSimon Glass 		ulong crc;
589d20a40deSSimon Glass 		ulong *ptr;
590d20a40deSSimon Glass 
591d20a40deSSimon Glass 		crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
592d20a40deSSimon Glass 
593d20a40deSSimon Glass 		printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
594d20a40deSSimon Glass 				addr, addr + len - 1, crc);
595d20a40deSSimon Glass 
5964b756b01STom Rini 		if (argc >= 3) {
5974b756b01STom Rini 			ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
598d20a40deSSimon Glass 			*ptr = crc;
599d20a40deSSimon Glass 		}
600d20a40deSSimon Glass 	}
601d20a40deSSimon Glass 
602460408efSSimon Glass 	return 0;
603460408efSSimon Glass }
604d70f919eSSimon Glass #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
605d70f919eSSimon Glass #endif /* !USE_HOSTCC */
606