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