1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2019-04-23     tyx          the first version
9  */
10 
11 #include <rtthread.h>
12 #include <rtdevice.h>
13 #include <hw_hash.h>
14 
15 /**
16  * @brief           Creating hash Context
17  *
18  * @param device    Hardware crypto device
19  * @param type      Type of hash context
20  *
21  * @return          Hash context
22  */
rt_hwcrypto_hash_create(struct rt_hwcrypto_device * device,hwcrypto_type type)23 struct rt_hwcrypto_ctx *rt_hwcrypto_hash_create(struct rt_hwcrypto_device *device, hwcrypto_type type)
24 {
25     struct rt_hwcrypto_ctx *ctx;
26 
27     ctx = rt_hwcrypto_ctx_create(device, type, sizeof(struct hwcrypto_hash));
28     return ctx;
29 }
30 
31 /**
32  * @brief           Destroy hash Context
33  *
34  * @param ctx       Hash context
35  */
rt_hwcrypto_hash_destroy(struct rt_hwcrypto_ctx * ctx)36 void rt_hwcrypto_hash_destroy(struct rt_hwcrypto_ctx *ctx)
37 {
38     rt_hwcrypto_ctx_destroy(ctx);
39 }
40 
41 /**
42  * @brief           Get the final hash value
43  *
44  * @param ctx       Hash context
45  * @param output    Hash value buffer
46  * @param length    Hash value buffer length
47  *
48  * @return          RT_EOK on success.
49  */
rt_hwcrypto_hash_finish(struct rt_hwcrypto_ctx * ctx,rt_uint8_t * output,rt_size_t length)50 rt_err_t rt_hwcrypto_hash_finish(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *output, rt_size_t length)
51 {
52     if (ctx && ((struct hwcrypto_hash *)ctx)->ops->finish)
53     {
54         return ((struct hwcrypto_hash *)ctx)->ops->finish((struct hwcrypto_hash *)ctx, output, length);
55     }
56     return -RT_ERROR;
57 }
58 
59 /**
60  * @brief           Processing a packet of data
61  *
62  * @param ctx       Hash context
63  * @param input     Data buffer to be Processed
64  * @param length    Data Buffer length
65  *
66  * @return          RT_EOK on success.
67  */
rt_hwcrypto_hash_update(struct rt_hwcrypto_ctx * ctx,const rt_uint8_t * input,rt_size_t length)68 rt_err_t rt_hwcrypto_hash_update(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *input, rt_size_t length)
69 {
70     if (ctx && ((struct hwcrypto_hash *)ctx)->ops->update)
71     {
72         return ((struct hwcrypto_hash *)ctx)->ops->update((struct hwcrypto_hash *)ctx, input, length);
73     }
74     return -RT_ERROR;
75 }
76 
77 /**
78  * @brief           This function copy hash context
79  *
80  * @param des       The destination hash context
81  * @param src       The hash context to be copy
82  *
83  * @return          RT_EOK on success.
84  */
rt_hwcrypto_hash_cpy(struct rt_hwcrypto_ctx * des,const struct rt_hwcrypto_ctx * src)85 rt_err_t rt_hwcrypto_hash_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
86 {
87     return rt_hwcrypto_ctx_cpy(des, src);
88 }
89 
90 /**
91  * @brief           Reset hash context
92  *
93  * @param ctx       Hash context
94  */
rt_hwcrypto_hash_reset(struct rt_hwcrypto_ctx * ctx)95 void rt_hwcrypto_hash_reset(struct rt_hwcrypto_ctx *ctx)
96 {
97     rt_hwcrypto_ctx_reset(ctx);
98 }
99 
100 /**
101  * @brief           Setting hash context type
102  *
103  * @param ctx       Hash context
104  * @param type      Types of settings
105  *
106  * @return          RT_EOK on success.
107  */
rt_hwcrypto_hash_set_type(struct rt_hwcrypto_ctx * ctx,hwcrypto_type type)108 rt_err_t rt_hwcrypto_hash_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type)
109 {
110     return rt_hwcrypto_set_type(ctx, type);
111 }
112