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