Path: blob/master/drivers/firmware/xilinx/zynqmp-crypto.c
121833 views
// SPDX-License-Identifier: GPL-2.01/*2* Firmware layer for XilSecure APIs.3*4* Copyright (C) 2014-2022 Xilinx, Inc.5* Copyright (C) 2022-2025 Advanced Micro Devices, Inc.6*/78#include <linux/firmware/xlnx-zynqmp.h>9#include <linux/module.h>1011/**12* zynqmp_pm_aes_engine - Access AES hardware to encrypt/decrypt the data using13* AES-GCM core.14* @address: Address of the AesParams structure.15* @out: Returned output value16*17* Return: Returns status, either success or error code.18*/19int zynqmp_pm_aes_engine(const u64 address, u32 *out)20{21u32 ret_payload[PAYLOAD_ARG_CNT];22int ret;2324if (!out)25return -EINVAL;2627ret = zynqmp_pm_invoke_fn(PM_SECURE_AES, ret_payload, 2, upper_32_bits(address),28lower_32_bits(address));29*out = ret_payload[1];3031return ret;32}33EXPORT_SYMBOL_GPL(zynqmp_pm_aes_engine);3435/**36* zynqmp_pm_sha_hash - Access the SHA engine to calculate the hash37* @address: Address of the data/ Address of output buffer where38* hash should be stored.39* @size: Size of the data.40* @flags:41* BIT(0) - for initializing csudma driver and SHA3(Here address42* and size inputs can be NULL).43* BIT(1) - to call Sha3_Update API which can be called multiple44* times when data is not contiguous.45* BIT(2) - to get final hash of the whole updated data.46* Hash will be overwritten at provided address with47* 48 bytes.48*49* Return: Returns status, either success or error code.50*/51int zynqmp_pm_sha_hash(const u64 address, const u32 size, const u32 flags)52{53u32 lower_addr = lower_32_bits(address);54u32 upper_addr = upper_32_bits(address);5556return zynqmp_pm_invoke_fn(PM_SECURE_SHA, NULL, 4, upper_addr, lower_addr, size, flags);57}58EXPORT_SYMBOL_GPL(zynqmp_pm_sha_hash);5960/**61* xlnx_get_crypto_dev_data() - Get crypto dev data of platform62* @feature_map: List of available feature map of all platform63*64* Return: Returns crypto dev data, either address crypto dev or ERR PTR65*/66void *xlnx_get_crypto_dev_data(struct xlnx_feature *feature_map)67{68struct xlnx_feature *feature;69u32 pm_family_code;70int ret;7172/* Get the Family code and sub family code of platform */73ret = zynqmp_pm_get_family_info(&pm_family_code);74if (ret < 0)75return ERR_PTR(ret);7677feature = feature_map;78for (; feature->family; feature++) {79if (feature->family == pm_family_code) {80ret = zynqmp_pm_feature(feature->feature_id);81if (ret < 0)82return ERR_PTR(ret);8384return feature->data;85}86}87return ERR_PTR(-ENODEV);88}89EXPORT_SYMBOL_GPL(xlnx_get_crypto_dev_data);9091/**92* versal_pm_aes_key_write - Write AES key registers93* @keylen: Size of the input key to be written94* @keysrc: Key Source to be selected to which provided95* key should be updated96* @keyaddr: Address of a buffer which should contain the key97* to be written98*99* This function provides support to write AES volatile user keys.100*101* Return: Returns status, either success or error+reason102*/103int versal_pm_aes_key_write(const u32 keylen,104const u32 keysrc, const u64 keyaddr)105{106return zynqmp_pm_invoke_fn(XSECURE_API_AES_WRITE_KEY, NULL, 4,107keylen, keysrc,108lower_32_bits(keyaddr),109upper_32_bits(keyaddr));110}111EXPORT_SYMBOL_GPL(versal_pm_aes_key_write);112113/**114* versal_pm_aes_key_zero - Zeroise AES User key registers115* @keysrc: Key Source to be selected to which provided116* key should be updated117*118* This function provides support to zeroise AES volatile user keys.119*120* Return: Returns status, either success or error+reason121*/122int versal_pm_aes_key_zero(const u32 keysrc)123{124return zynqmp_pm_invoke_fn(XSECURE_API_AES_KEY_ZERO, NULL, 1, keysrc);125}126EXPORT_SYMBOL_GPL(versal_pm_aes_key_zero);127128/**129* versal_pm_aes_op_init - Init AES operation130* @hw_req: AES op init structure address131*132* This function provides support to init AES operation.133*134* Return: Returns status, either success or error+reason135*/136int versal_pm_aes_op_init(const u64 hw_req)137{138return zynqmp_pm_invoke_fn(XSECURE_API_AES_OP_INIT, NULL, 2,139lower_32_bits(hw_req),140upper_32_bits(hw_req));141}142EXPORT_SYMBOL_GPL(versal_pm_aes_op_init);143144/**145* versal_pm_aes_update_aad - AES update aad146* @aad_addr: AES aad address147* @aad_len: AES aad data length148*149* This function provides support to update AAD data.150*151* Return: Returns status, either success or error+reason152*/153int versal_pm_aes_update_aad(const u64 aad_addr, const u32 aad_len)154{155return zynqmp_pm_invoke_fn(XSECURE_API_AES_UPDATE_AAD, NULL, 3,156lower_32_bits(aad_addr),157upper_32_bits(aad_addr),158aad_len);159}160EXPORT_SYMBOL_GPL(versal_pm_aes_update_aad);161162/**163* versal_pm_aes_enc_update - Access AES hardware to encrypt the data using164* AES-GCM core.165* @in_params: Address of the AesParams structure166* @in_addr: Address of input buffer167*168* Return: Returns status, either success or error code.169*/170int versal_pm_aes_enc_update(const u64 in_params, const u64 in_addr)171{172return zynqmp_pm_invoke_fn(XSECURE_API_AES_ENCRYPT_UPDATE, NULL, 4,173lower_32_bits(in_params),174upper_32_bits(in_params),175lower_32_bits(in_addr),176upper_32_bits(in_addr));177}178EXPORT_SYMBOL_GPL(versal_pm_aes_enc_update);179180/**181* versal_pm_aes_enc_final - Access AES hardware to store the GCM tag182* @gcm_addr: Address of the gcm tag183*184* Return: Returns status, either success or error code.185*/186int versal_pm_aes_enc_final(const u64 gcm_addr)187{188return zynqmp_pm_invoke_fn(XSECURE_API_AES_ENCRYPT_FINAL, NULL, 2,189lower_32_bits(gcm_addr),190upper_32_bits(gcm_addr));191}192EXPORT_SYMBOL_GPL(versal_pm_aes_enc_final);193194/**195* versal_pm_aes_dec_update - Access AES hardware to decrypt the data using196* AES-GCM core.197* @in_params: Address of the AesParams structure198* @in_addr: Address of input buffer199*200* Return: Returns status, either success or error code.201*/202int versal_pm_aes_dec_update(const u64 in_params, const u64 in_addr)203{204return zynqmp_pm_invoke_fn(XSECURE_API_AES_DECRYPT_UPDATE, NULL, 4,205lower_32_bits(in_params),206upper_32_bits(in_params),207lower_32_bits(in_addr),208upper_32_bits(in_addr));209}210EXPORT_SYMBOL_GPL(versal_pm_aes_dec_update);211212/**213* versal_pm_aes_dec_final - Access AES hardware to get the GCM tag214* @gcm_addr: Address of the gcm tag215*216* Return: Returns status, either success or error code.217*/218int versal_pm_aes_dec_final(const u64 gcm_addr)219{220return zynqmp_pm_invoke_fn(XSECURE_API_AES_DECRYPT_FINAL, NULL, 2,221lower_32_bits(gcm_addr),222upper_32_bits(gcm_addr));223}224EXPORT_SYMBOL_GPL(versal_pm_aes_dec_final);225226/**227* versal_pm_aes_init - Init AES block228*229* This function initialise AES block.230*231* Return: Returns status, either success or error+reason232*/233int versal_pm_aes_init(void)234{235return zynqmp_pm_invoke_fn(XSECURE_API_AES_INIT, NULL, 0);236}237EXPORT_SYMBOL_GPL(versal_pm_aes_init);238239240