1 /* 2 * Copyright (C) 2022 Huawei Technologies Co., Ltd. 3 * Licensed under the Mulan PSL v2. 4 * You can use this software according to the terms and conditions of the Mulan PSL v2. 5 * You may obtain a copy of Mulan PSL v2 at: 6 * http://license.coscl.org.cn/MulanPSL2 7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR 8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR 9 * PURPOSE. 10 * See the Mulan PSL v2 for more details. 11 */ 12 #ifndef CRYPTO_DRIVER_ADAPTOR_OPS_H 13 #define CRYPTO_DRIVER_ADAPTOR_OPS_H 14 15 #include <stdio.h> 16 #include <stdint.h> 17 #include <stdbool.h> 18 #include "crypto_driver_adaptor.h" 19 20 struct crypto_drv_ops_t { 21 int32_t (*init)(void); 22 bool (*is_alg_support)(uint32_t alg_type); 23 int32_t (*power_on)(void); 24 int32_t (*power_off)(void); 25 int32_t (*get_ctx_size)(uint32_t alg_type); 26 int32_t (*ctx_copy)(uint32_t alg_type, const void *src_ctx, uint32_t src_size, void *dest_ctx, uint32_t dest_size); 27 int32_t (*get_driver_ability)(void); 28 int32_t (*hash_init)(void *ctx, uint32_t alg_type); 29 int32_t (*hash_update)(void *ctx, const struct memref_t *data_in); 30 int32_t (*hash_dofinal)(void *ctx, const struct memref_t *data_in, struct memref_t *data_out); 31 int32_t (*hash)(uint32_t alg_type, const struct memref_t *data_in, struct memref_t *data_out); 32 int32_t (*hmac_init)(uint32_t alg_type, void *ctx, const struct symmerit_key_t *key); 33 int32_t (*hmac_update)(void *ctx, const struct memref_t *data_in); 34 int32_t (*hmac_dofinal)(void *ctx, const struct memref_t *data_in, struct memref_t *data_out); 35 int32_t (*hmac)(uint32_t alg_type, const struct symmerit_key_t *key, 36 const struct memref_t *data_in, struct memref_t *data_out); 37 int32_t (*cipher_init)(uint32_t alg_type, void *ctx, uint32_t direction, 38 const struct symmerit_key_t *key, const struct memref_t *iv); 39 int32_t (*cipher_update)(void *ctx, const struct memref_t *data_in, struct memref_t *data_out); 40 int32_t (*cipher_dofinal)(void *ctx, const struct memref_t *data_in, struct memref_t *data_out); 41 int32_t (*cipher)(uint32_t alg_type, uint32_t direction, const struct symmerit_key_t *key, 42 const struct memref_t *iv, const struct memref_t *data_in, struct memref_t *data_out); 43 int32_t (*ae_init)(uint32_t alg_type, void *ctx, uint32_t direction, 44 const struct symmerit_key_t *key, const struct ae_init_data *ae_init_param); 45 int32_t (*ae_update_aad)(void *ctx, const struct memref_t *aad_data); 46 int32_t (*ae_update)(void *ctx, const struct memref_t *data_in, struct memref_t *data_out); 47 int32_t (*ae_enc_final)(void *ctx, const struct memref_t *data_in, 48 struct memref_t *data_out, struct memref_t *tag_out); 49 int32_t (*ae_dec_final)(void *ctx, const struct memref_t *data_in, const struct memref_t *tag_in, 50 struct memref_t *data_out); 51 int32_t (*rsa_generate_keypair)(uint32_t key_size, const struct memref_t *e_value, bool crt_mode, 52 struct rsa_priv_key_t *key_pair); 53 int32_t (*rsa_encrypt)(uint32_t alg_type, const struct rsa_pub_key_t *public_key, 54 const struct asymmetric_params_t *rsa_params, 55 const struct memref_t *data_in, struct memref_t *data_out); 56 int32_t (*rsa_decrypt)(uint32_t alg_type, const struct rsa_priv_key_t *private_key, 57 const struct asymmetric_params_t *rsa_params, 58 const struct memref_t *data_in, struct memref_t *data_out); 59 int32_t (*rsa_sign_digest)(uint32_t alg_type, const struct rsa_priv_key_t *private_key, 60 const struct asymmetric_params_t *rsa_params, 61 const struct memref_t *digest, struct memref_t *signature); 62 int32_t (*rsa_verify_digest)(uint32_t alg_type, const struct rsa_pub_key_t *public_key, 63 const struct asymmetric_params_t *rsa_params, 64 const struct memref_t *digest, const struct memref_t *signature); 65 int32_t (*ecc_generate_keypair)(uint32_t keysize, uint32_t curve, 66 struct ecc_pub_key_t *public_key, struct ecc_priv_key_t *private_key); 67 int32_t (*ecc_encrypt)(uint32_t alg_type, const struct ecc_pub_key_t *public_key, 68 const struct asymmetric_params_t *ec_params, 69 const struct memref_t *data_in, struct memref_t *data_out); 70 int32_t (*ecc_decrypt)(uint32_t alg_type, const struct ecc_priv_key_t *private_key, 71 const struct asymmetric_params_t *ec_params, 72 const struct memref_t *data_in, struct memref_t *data_out); 73 int32_t (*ecc_sign_digest)(uint32_t alg_type, const struct ecc_priv_key_t *private_key, 74 const struct asymmetric_params_t *ec_params, 75 const struct memref_t *digest, struct memref_t *signature); 76 int32_t (*ecc_verify_digest)(uint32_t alg_type, const struct ecc_pub_key_t *public_key, 77 const struct asymmetric_params_t *ec_params, 78 const struct memref_t *digest, const struct memref_t *signature); 79 int32_t (*ecdh_derive_key)(uint32_t alg_type, 80 const struct ecc_pub_key_t *client_key, const struct ecc_priv_key_t *server_key, 81 const struct asymmetric_params_t *ec_params, struct memref_t *secret); 82 int32_t (*dh_generate_key)(const struct dh_key_t *dh_generate_key_data, 83 struct memref_t *pub_key, struct memref_t *priv_key); 84 int32_t (*dh_derive_key)(const struct dh_key_t *dh_derive_key_data, struct memref_t *secret); 85 int32_t (*generate_random)(void *buffer, size_t size); 86 int32_t (*get_entropy)(void *buffer, size_t size); 87 int32_t (*derive_root_key)(uint32_t derive_type, const struct memref_t *data_in, 88 struct memref_t *data_out); 89 int32_t (*pbkdf2)(const struct memref_t *password, const struct memref_t *salt, uint32_t iterations, 90 uint32_t digest_type, struct memref_t *data_out); 91 int32_t (*get_oemkey)(void *buffer, size_t size); 92 int32_t (*suspend)(void); 93 int32_t (*resume)(void); 94 }; 95 96 #define crypto_driver_declare(init, is_alg_support, power_on, power_off, get_ctx_size, ctx_copy, get_driver_ability, \ 97 hash_init, hash_update, hash_dofinal, hash, hmac_init, hmac_update, hmac_dofinal, hmac, cipher_init, \ 98 cipher_update, cipher_dofinal, cipher, ae_init, ae_update_aad, ae_update, ae_enc_final, ae_dec_final, \ 99 rsa_generate_keypair, rsa_encrypt, rsa_decrypt, rsa_sign_digest, rsa_verify_digest, ecc_generate_keypair, \ 100 ecc_encrypt, ecc_decrypt, ecc_sign_digest, ecc_verify_digest, ecdh_derive_key, dh_generate_key, dh_derive_key, \ 101 generate_random, get_entropy, derive_root_key, pbkdf2, get_oemkey, suspend, resume) \ 102 __attribute__((visibility("default"))) const struct crypto_drv_ops_t g_crypto_drv_ops = { \ 103 init, is_alg_support, power_on, power_off, get_ctx_size, ctx_copy, get_driver_ability, \ 104 hash_init, hash_update, hash_dofinal, hash, hmac_init, hmac_update, hmac_dofinal, hmac, cipher_init, \ 105 cipher_update, cipher_dofinal, cipher, ae_init, ae_update_aad, ae_update, ae_enc_final, ae_dec_final, \ 106 rsa_generate_keypair, rsa_encrypt, rsa_decrypt, rsa_sign_digest, rsa_verify_digest, ecc_generate_keypair, \ 107 ecc_encrypt, ecc_decrypt, ecc_sign_digest, ecc_verify_digest, ecdh_derive_key, dh_generate_key, dh_derive_key, \ 108 generate_random, get_entropy, derive_root_key, pbkdf2, get_oemkey, suspend, resume} 109 110 #endif 111