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 #include <common.h> 10 #include <dm.h> 11 #include <image.h> 12 #include <u-boot/sha1.h> 13 14 /* Algorithms/capability of crypto, works together with crypto_algo_nbits() */ 15 #define CRYPTO_MD5 BIT(0) 16 #define CRYPTO_SHA1 BIT(1) 17 #define CRYPTO_SHA256 BIT(2) 18 #define CRYPTO_SHA512 BIT(3) 19 #define CRYPTO_SM3 BIT(4) 20 21 #define CRYPTO_RSA512 BIT(10) 22 #define CRYPTO_RSA1024 BIT(11) 23 #define CRYPTO_RSA2048 BIT(12) 24 #define CRYPTO_RSA3072 BIT(13) 25 #define CRYPTO_RSA4096 BIT(14) 26 27 #define CRYPTO_DES BIT(20) 28 #define CRYPTO_AES BIT(21) 29 #define CRYPTO_SM4 BIT(22) 30 31 #define CRYPTO_HMAC_MD5 BIT(25) 32 #define CRYPTO_HMAC_SHA1 BIT(26) 33 #define CRYPTO_HMAC_SHA256 BIT(27) 34 #define CRYPTO_HMAC_SHA512 BIT(28) 35 #define CRYPTO_HMAC_SM3 BIT(29) 36 37 #define BYTE2WORD(bytes) ((bytes) / 4) 38 #define BITS2BYTE(nbits) ((nbits) / 8) 39 #define BITS2WORD(nbits) ((nbits) / 32) 40 41 enum RK_CRYPTO_MODE { 42 RK_MODE_ECB = 0, 43 RK_MODE_CBC, 44 RK_MODE_CTS, 45 RK_MODE_CTR, 46 RK_MODE_CFB, 47 RK_MODE_OFB, 48 RK_MODE_XTS, 49 RK_MODE_MAX 50 }; 51 52 typedef struct { 53 u32 algo; /* Algorithm: CRYPTO_MD5/CRYPTO_SHA1/CRYPTO_RSA2048... */ 54 u32 length; /* Data total length */ 55 56 } sha_context; 57 58 typedef struct { 59 u32 algo; /* Algorithm: CRYPTO_MD5/CRYPTO_SHA1/CRYPTO_RSA2048... */ 60 u32 *n; /* Public key factor N */ 61 u32 *e; /* Public key factor E */ 62 u32 *c; /* Optional, a accelerate factor for some crypto */ 63 } rsa_key; 64 65 typedef struct { 66 u32 algo; 67 u32 mode; 68 const u8 *key; 69 const u8 *twk_key; 70 u32 key_len; 71 const u8 *iv; 72 u32 iv_len; 73 } cipher_context; 74 75 struct dm_crypto_ops { 76 /* Hardware algorithm capability */ 77 u32 (*capability)(struct udevice *dev); 78 79 /* SHA init/update/final */ 80 int (*sha_init)(struct udevice *dev, sha_context *ctx); 81 int (*sha_update)(struct udevice *dev, u32 *input, u32 len); 82 int (*sha_final)(struct udevice *dev, sha_context *ctx, u8 *output); 83 84 /* RSA verify */ 85 int (*rsa_verify)(struct udevice *dev, rsa_key *ctx, 86 u8 *sign, u8 *output); 87 /* HMAC init/update/final */ 88 int (*hmac_init)(struct udevice *dev, sha_context *ctx, 89 u8 *key, u32 key_len); 90 int (*hmac_update)(struct udevice *dev, u32 *input, u32 len); 91 int (*hmac_final)(struct udevice *dev, sha_context *ctx, u8 *output); 92 93 /* cipher encryption and decryption */ 94 int (*cipher_crypt)(struct udevice *dev, cipher_context *ctx, 95 const u8 *in, u8 *out, u32 len, bool enc); 96 }; 97 98 /** 99 * crypto_algo_nbits() - Get algorithm bits accroding to algorithm 100 * @capability: expected algorithm capability, eg. CRYPTO_MD5/RSA2048... 101 * 102 * @return algorithm bits 103 */ 104 u32 crypto_algo_nbits(u32 algo); 105 106 /** 107 * crypto_get_device() - Get crypto device by capability 108 * @capability: expected algorithm capability, eg. CRYPTO_MD5/RSA2048... 109 * 110 * @return dev on success, otherwise NULL 111 */ 112 struct udevice *crypto_get_device(u32 capability); 113 114 /** 115 * crypto_sha_init() - Crypto sha init 116 * 117 * @dev: crypto device 118 * @ctx: sha context 119 * 120 * @return 0 on success, otherwise failed 121 */ 122 int crypto_sha_init(struct udevice *dev, sha_context *ctx); 123 124 /** 125 * crypto_sha_update() - Crypto sha update 126 * 127 * @dev: crypto device 128 * @input: input data buffer 129 * @len: input data length 130 * 131 * @return 0 on success, otherwise failed 132 */ 133 int crypto_sha_update(struct udevice *dev, u32 *input, u32 len); 134 135 /** 136 * crypto_sha_final() - Crypto sha finish and get result 137 * 138 * @dev: crypto device 139 * @ctx: sha context 140 * @output: output hash data 141 * 142 * @return 0 on success, otherwise failed 143 */ 144 int crypto_sha_final(struct udevice *dev, sha_context *ctx, u8 *output); 145 146 /** 147 * crypto_sha_csum() - Crypto sha hash for one data block only 148 * 149 * @dev: crypto device 150 * @ctx: sha context 151 * @input: input data buffer 152 * @input_len: input data length 153 * @output: output hash data 154 * 155 * @return 0 on success, otherwise failed 156 */ 157 int crypto_sha_csum(struct udevice *dev, sha_context *ctx, 158 char *input, u32 input_len, u8 *output); 159 160 /** 161 * crypto_sha_regions_csum() - Crypto sha hash for multi data blocks 162 * 163 * @dev: crypto device 164 * @ctx: sha context 165 * @region: regions buffer 166 * @region_count: regions count 167 * @output: output hash data 168 * 169 * @return 0 on success, otherwise failed 170 */ 171 int crypto_sha_regions_csum(struct udevice *dev, sha_context *ctx, 172 const struct image_region region[], 173 int region_count, u8 *output); 174 175 /** 176 * crypto_rsa_verify() - Crypto rsa verify 177 * 178 * @dev: crypto device 179 * @ctx: rsa key context 180 * @sign: signature 181 * @output: output hash data buffer 182 * 183 * @return 0 on success, otherwise failed 184 */ 185 int crypto_rsa_verify(struct udevice *dev, rsa_key *ctx, u8 *sign, u8 *output); 186 187 /** 188 * crypto_hmac_init() - Crypto hmac init 189 * 190 * @dev: crypto device 191 * @ctx: sha context 192 * 193 * @return 0 on success, otherwise failed 194 */ 195 int crypto_hmac_init(struct udevice *dev, sha_context *ctx, 196 u8 *key, u32 key_len); 197 198 /** 199 * crypto_hmac_update() - Crypto hmac update 200 * 201 * @dev: crypto device 202 * @input: input data buffer 203 * @len: input data length 204 * 205 * @return 0 on success, otherwise failed 206 */ 207 int crypto_hmac_update(struct udevice *dev, u32 *input, u32 len); 208 209 /** 210 * crypto_sha_final() - Crypto hmac finish and get result 211 * 212 * @dev: crypto device 213 * @ctx: sha context 214 * @output: output hash data 215 * 216 * @return 0 on success, otherwise failed 217 */ 218 int crypto_hmac_final(struct udevice *dev, sha_context *ctx, u8 *output); 219 220 /** 221 * crypto_cipher() - Crypto cipher crypt 222 * 223 * @dev: crypto device 224 * @ctx: cipher context 225 * @in: input data buffer 226 * @out: output data buffer 227 * @len: input data length 228 * @enc: true for encrypt, false for decrypt 229 * @return 0 on success, otherwise failed 230 */ 231 int crypto_cipher(struct udevice *dev, cipher_context *ctx, 232 const u8 *in, u8 *out, u32 len, bool enc); 233 234 #endif 235