blob: 5d9f1e0d59bd6c21b41069ac7aa655943ae5b06b [file] [log] [blame]
Chia-Wei Wang6e844742021-07-30 09:08:03 +08001// 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
Chia-Wei Wang6e844742021-07-30 09:08:03 +08009#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
18struct hash_info {
19 char *name;
20 uint32_t digest_size;
21};
22
23static 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
33enum 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
47ssize_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
55const 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
63int 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
75int 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
87int 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
97int 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
107int 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
117UCLASS_DRIVER(hash) = {
118 .id = UCLASS_HASH,
119 .name = "hash",
120};