1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2021 ASPEED Technology Inc.
4 * Author: ChiaWei Wang <chiawei_wang@aspeedtech.com>
5 */
6
7 #define LOG_CATEGORY UCLASS_HASH
8
9 #include <common.h>
10 #include <dm.h>
11 #include <asm/global_data.h>
12 #include <u-boot/hash.h>
13 #include <errno.h>
14 #include <fdtdec.h>
15 #include <malloc.h>
16 #include <asm/io.h>
17 #include <linux/list.h>
18
19 struct hash_info {
20 char *name;
21 uint32_t digest_size;
22 };
23
24 static const struct hash_info hash_info[HASH_ALGO_NUM] = {
25 [HASH_ALGO_CRC16_CCITT] = { "crc16-ccitt", 2 },
26 [HASH_ALGO_CRC32] = { "crc32", 4 },
27 [HASH_ALGO_MD5] = { "md5", 16 },
28 [HASH_ALGO_SHA1] = { "sha1", 20 },
29 [HASH_ALGO_SHA256] = { "sha256", 32 },
30 [HASH_ALGO_SHA384] = { "sha384", 48 },
31 [HASH_ALGO_SHA512] = { "sha512", 64},
32 };
33
hash_algo_lookup_by_name(const char * name)34 enum HASH_ALGO hash_algo_lookup_by_name(const char *name)
35 {
36 int i;
37
38 if (!name)
39 return HASH_ALGO_INVALID;
40
41 for (i = 0; i < HASH_ALGO_NUM; ++i)
42 if (!strcmp(name, hash_info[i].name))
43 return i;
44
45 return HASH_ALGO_INVALID;
46 }
47
hash_algo_digest_size(enum HASH_ALGO algo)48 ssize_t hash_algo_digest_size(enum HASH_ALGO algo)
49 {
50 if (algo >= HASH_ALGO_NUM)
51 return -EINVAL;
52
53 return hash_info[algo].digest_size;
54 }
55
hash_algo_name(enum HASH_ALGO algo)56 const char *hash_algo_name(enum HASH_ALGO algo)
57 {
58 if (algo >= HASH_ALGO_NUM)
59 return NULL;
60
61 return hash_info[algo].name;
62 }
63
hash_digest(struct udevice * dev,enum HASH_ALGO algo,const void * ibuf,const uint32_t ilen,void * obuf)64 int hash_digest(struct udevice *dev, enum HASH_ALGO algo,
65 const void *ibuf, const uint32_t ilen,
66 void *obuf)
67 {
68 struct hash_ops *ops = (struct hash_ops *)device_get_ops(dev);
69
70 if (!ops->hash_digest)
71 return -ENOSYS;
72
73 return ops->hash_digest(dev, algo, ibuf, ilen, obuf);
74 }
75
hash_digest_wd(struct udevice * dev,enum HASH_ALGO algo,const void * ibuf,const uint32_t ilen,void * obuf,uint32_t chunk_sz)76 int hash_digest_wd(struct udevice *dev, enum HASH_ALGO algo,
77 const void *ibuf, const uint32_t ilen,
78 void *obuf, uint32_t chunk_sz)
79 {
80 struct hash_ops *ops = (struct hash_ops *)device_get_ops(dev);
81
82 if (!ops->hash_digest_wd)
83 return -ENOSYS;
84
85 return ops->hash_digest_wd(dev, algo, ibuf, ilen, obuf, chunk_sz);
86 }
87
hash_init(struct udevice * dev,enum HASH_ALGO algo,void ** ctxp)88 int hash_init(struct udevice *dev, enum HASH_ALGO algo, void **ctxp)
89 {
90 struct hash_ops *ops = (struct hash_ops *)device_get_ops(dev);
91
92 if (!ops->hash_init)
93 return -ENOSYS;
94
95 return ops->hash_init(dev, algo, ctxp);
96 }
97
hash_update(struct udevice * dev,void * ctx,const void * ibuf,const uint32_t ilen)98 int hash_update(struct udevice *dev, void *ctx, const void *ibuf, const uint32_t ilen)
99 {
100 struct hash_ops *ops = (struct hash_ops *)device_get_ops(dev);
101
102 if (!ops->hash_update)
103 return -ENOSYS;
104
105 return ops->hash_update(dev, ctx, ibuf, ilen);
106 }
107
hash_finish(struct udevice * dev,void * ctx,void * obuf)108 int hash_finish(struct udevice *dev, void *ctx, void *obuf)
109 {
110 struct hash_ops *ops = (struct hash_ops *)device_get_ops(dev);
111
112 if (!ops->hash_finish)
113 return -ENOSYS;
114
115 return ops->hash_finish(dev, ctx, obuf);
116 }
117
118 UCLASS_DRIVER(hash) = {
119 .id = UCLASS_HASH,
120 .name = "hash",
121 };
122