/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Copyright (C) 2020 Pengutronix, Ahmad Fatoum <[email protected]>3* Copyright 2024-2025 NXP4*/56#ifndef __CAAM_BLOB_GEN7#define __CAAM_BLOB_GEN89#include <linux/types.h>10#include <linux/errno.h>1112#define CAAM_BLOB_KEYMOD_LENGTH 1613#define CAAM_BLOB_OVERHEAD (32 + 16)14#define CAAM_BLOB_MAX_LEN 409615#define CAAM_ENC_ALGO_CCM 0x116#define CAAM_ENC_ALGO_ECB 0x217#define CAAM_NONCE_SIZE 618#define CAAM_ICV_SIZE 619#define CAAM_CCM_OVERHEAD (CAAM_NONCE_SIZE + CAAM_ICV_SIZE)2021struct caam_blob_priv;2223/**24* struct caam_pkey_info - information for CAAM protected key25* @is_pkey: flag to identify, if the key is protected.26* @key_enc_algo: identifies the algorithm, ccm or ecb27* @plain_key_sz: size of plain key.28* @key_buf: contains key data29*/30struct caam_pkey_info {31u8 is_pkey;32u8 key_enc_algo;33u16 plain_key_sz;34u8 key_buf[];35} __packed;3637/* sizeof struct caam_pkey_info */38#define CAAM_PKEY_HEADER 43940/**41* struct caam_blob_info - information for CAAM blobbing42* @pkey_info: pointer to keep protected key information43* @input: pointer to input buffer (must be DMAable)44* @input_len: length of @input buffer in bytes.45* @output: pointer to output buffer (must be DMAable)46* @output_len: length of @output buffer in bytes.47* @key_mod: key modifier48* @key_mod_len: length of @key_mod in bytes.49* May not exceed %CAAM_BLOB_KEYMOD_LENGTH50*/51struct caam_blob_info {52struct caam_pkey_info pkey_info;5354void *input;55size_t input_len;5657void *output;58size_t output_len;5960const void *key_mod;61size_t key_mod_len;62};6364/**65* caam_blob_gen_init - initialize blob generation66* Return: pointer to new &struct caam_blob_priv instance on success67* and ``ERR_PTR(-ENODEV)`` if CAAM has no hardware blobbing support68* or no job ring could be allocated.69*/70struct caam_blob_priv *caam_blob_gen_init(void);7172/**73* caam_blob_gen_exit - free blob generation resources74* @priv: instance returned by caam_blob_gen_init()75*/76void caam_blob_gen_exit(struct caam_blob_priv *priv);7778/**79* caam_process_blob - encapsulate or decapsulate blob80* @priv: instance returned by caam_blob_gen_init()81* @info: pointer to blobbing info describing key, blob and82* key modifier buffers.83* @encap: true for encapsulation, false for decapsulation84*85* Return: %0 and sets ``info->output_len`` on success and a negative86* error code otherwise.87*/88int caam_process_blob(struct caam_blob_priv *priv,89struct caam_blob_info *info, bool encap);9091/**92* caam_encap_blob - encapsulate blob93* @priv: instance returned by caam_blob_gen_init()94* @info: pointer to blobbing info describing input key,95* output blob and key modifier buffers.96*97* Return: %0 and sets ``info->output_len`` on success and98* a negative error code otherwise.99*/100static inline int caam_encap_blob(struct caam_blob_priv *priv,101struct caam_blob_info *info)102{103if (info->output_len < info->input_len + CAAM_BLOB_OVERHEAD)104return -EINVAL;105106return caam_process_blob(priv, info, true);107}108109/**110* caam_decap_blob - decapsulate blob111* @priv: instance returned by caam_blob_gen_init()112* @info: pointer to blobbing info describing output key,113* input blob and key modifier buffers.114*115* Return: %0 and sets ``info->output_len`` on success and116* a negative error code otherwise.117*/118static inline int caam_decap_blob(struct caam_blob_priv *priv,119struct caam_blob_info *info)120{121if (info->input_len < CAAM_BLOB_OVERHEAD ||122info->output_len < info->input_len - CAAM_BLOB_OVERHEAD)123return -EINVAL;124125return caam_process_blob(priv, info, false);126}127128#endif129130131