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