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