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