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