xref: /rk3399_rockchip-uboot/include/crypto.h (revision 87e4c6020eff05133e40ab8b7b0e37e6a2be37e4)
1 /* SPDX-License-Identifier:     GPL-2.0+ */
2 /*
3  * (C) Copyright 2019 Rockchip Electronics Co., Ltd
4  */
5 
6 #ifndef _CORE_CRYPTO_H_
7 #define _CORE_CRYPTO_H_
8 
9 /* Algorithms/capability of crypto, works together with crypto_algo_nbits() */
10 #define CRYPTO_MD5		BIT(0)
11 #define CRYPTO_SHA1		BIT(1)
12 #define CRYPTO_SHA256		BIT(2)
13 #define CRYPTO_RSA512		BIT(3)
14 #define CRYPTO_RSA1024		BIT(4)
15 #define CRYPTO_RSA2048		BIT(5)
16 #define CRYPTO_RSA3072		BIT(6)
17 #define CRYPTO_RSA4096		BIT(7)
18 
19 #define BYTE2WORD(bytes)	((bytes) / 4)
20 #define BITS2BYTE(nbits)	((nbits) / 8)
21 #define BITS2WORD(nbits)	((nbits) / 32)
22 
23 typedef struct {
24 	u32 algo;	/* Algorithm: CRYPTO_MD5/CRYPTO_SHA1/CRYPTO_RSA2048... */
25 	u32 length;	/* Data total length */
26 
27 } sha_context;
28 
29 typedef struct {
30 	u32 algo;	/* Algorithm: CRYPTO_MD5/CRYPTO_SHA1/CRYPTO_RSA2048... */
31 	u32 *n;		/* Public key factor N */
32 	u32 *e;		/* Public key factor E */
33 	u32 *c;		/* Optional, a accelerate factor for some crypto */
34 } rsa_key;
35 
36 struct dm_crypto_ops {
37 	/* Hardware algorithm capability */
38 	u32 (*capability)(struct udevice *dev);
39 
40 	/* SHA init/update/final */
41 	int (*sha_init)(struct udevice *dev, sha_context *ctx);
42 	int (*sha_update)(struct udevice *dev, u32 *input, u32 len);
43 	int (*sha_final)(struct udevice *dev, sha_context *ctx, u8 *output);
44 
45 	/* RSA verify */
46 	int (*rsa_verify)(struct udevice *dev, rsa_key *ctx,
47 			  u8 *sign, u8 *output);
48 };
49 
50 /**
51  * crypto_algo_nbits() - Get algorithm bits accroding to algorithm
52  * @capability: expected algorithm capability, eg. CRYPTO_MD5/RSA2048...
53  *
54  * @return algorithm bits
55  */
56 u32 crypto_algo_nbits(u32 algo);
57 
58 /**
59  * crypto_get_device() - Get crypto device by capability
60  * @capability: expected algorithm capability, eg. CRYPTO_MD5/RSA2048...
61  *
62  * @return dev on success, otherwise NULL
63  */
64 struct udevice *crypto_get_device(u32 capability);
65 
66 /**
67  * crypto_sha_init() - Crypto sha init
68  *
69  * @dev: crypto device
70  * @ctx: sha context
71  *
72  * @return 0 on success, otherwise failed
73  */
74 int crypto_sha_init(struct udevice *dev, sha_context *ctx);
75 
76 /**
77  * crypto_sha_update() - Crypto sha update
78  *
79  * @dev: crypto device
80  * @input: input data buffer
81  * @len: input data length
82  *
83  * @return 0 on success, otherwise failed
84  */
85 int crypto_sha_update(struct udevice *dev, u32 *input, u32 len);
86 
87 /**
88  * crypto_sha_final() - Crypto sha finish and get result
89  *
90  * @dev: crypto device
91  * @ctx: sha context
92  * @output: output hash data
93  *
94  * @return 0 on success, otherwise failed
95  */
96 int crypto_sha_final(struct udevice *dev, sha_context *ctx, u8 *output);
97 
98 /**
99  * crypto_sha_csum() - Crypto sha hash for one data block only
100  *
101  * @dev: crypto device
102  * @ctx: sha context
103  * @input: input data buffer
104  * @input_len: input data length
105  * @output: output hash data
106  *
107  * @return 0 on success, otherwise failed
108  */
109 int crypto_sha_csum(struct udevice *dev, sha_context *ctx,
110 		    char *input, u32 input_len, u8 *output);
111 
112 /**
113  * crypto_rsa_verify() - Crypto rsa verify
114  *
115  * @dev: crypto device
116  * @ctx: rsa key context
117  * @sign: signature
118  * @output: output hash data buffer
119  *
120  * @return 0 on success, otherwise failed
121  */
122 int crypto_rsa_verify(struct udevice *dev, rsa_key *ctx, u8 *sign, u8 *output);
123 
124 #endif
125