Path: blob/master/drivers/crypto/intel/keembay/keembay-ocs-hcu-core.c
29524 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Intel Keem Bay OCS HCU Crypto Driver.3*4* Copyright (C) 2018-2020 Intel Corporation5*/67#include <crypto/engine.h>8#include <crypto/hmac.h>9#include <crypto/internal/hash.h>10#include <crypto/scatterwalk.h>11#include <crypto/sha2.h>12#include <crypto/sm3.h>13#include <linux/completion.h>14#include <linux/dma-mapping.h>15#include <linux/err.h>16#include <linux/interrupt.h>17#include <linux/kernel.h>18#include <linux/mod_devicetable.h>19#include <linux/module.h>20#include <linux/platform_device.h>21#include <linux/string.h>2223#include "ocs-hcu.h"2425#define DRV_NAME "keembay-ocs-hcu"2627/* Flag marking a final request. */28#define REQ_FINAL BIT(0)29/* Flag marking a HMAC request. */30#define REQ_FLAGS_HMAC BIT(1)31/* Flag set when HW HMAC is being used. */32#define REQ_FLAGS_HMAC_HW BIT(2)33/* Flag set when SW HMAC is being used. */34#define REQ_FLAGS_HMAC_SW BIT(3)3536/**37* struct ocs_hcu_ctx: OCS HCU Transform context.38* @hcu_dev: The OCS HCU device used by the transformation.39* @key: The key (used only for HMAC transformations).40* @key_len: The length of the key.41* @is_sm3_tfm: Whether or not this is an SM3 transformation.42* @is_hmac_tfm: Whether or not this is a HMAC transformation.43*/44struct ocs_hcu_ctx {45struct ocs_hcu_dev *hcu_dev;46u8 key[SHA512_BLOCK_SIZE];47size_t key_len;48bool is_sm3_tfm;49bool is_hmac_tfm;50};5152/**53* struct ocs_hcu_rctx - Context for the request.54* @hcu_dev: OCS HCU device to be used to service the request.55* @flags: Flags tracking request status.56* @algo: Algorithm to use for the request.57* @blk_sz: Block size of the transformation / request.58* @dig_sz: Digest size of the transformation / request.59* @dma_list: OCS DMA linked list.60* @hash_ctx: OCS HCU hashing context.61* @buffer: Buffer to store: partial block of data and SW HMAC62* artifacts (ipad, opad, etc.).63* @buf_cnt: Number of bytes currently stored in the buffer.64* @buf_dma_addr: The DMA address of @buffer (when mapped).65* @buf_dma_count: The number of bytes in @buffer currently DMA-mapped.66* @sg: Head of the scatterlist entries containing data.67* @sg_data_total: Total data in the SG list at any time.68* @sg_data_offset: Offset into the data of the current individual SG node.69* @sg_dma_nents: Number of sg entries mapped in dma_list.70* @nents: Number of entries in the scatterlist.71*/72struct ocs_hcu_rctx {73struct ocs_hcu_dev *hcu_dev;74u32 flags;75enum ocs_hcu_algo algo;76size_t blk_sz;77size_t dig_sz;78struct ocs_hcu_dma_list *dma_list;79struct ocs_hcu_hash_ctx hash_ctx;80/*81* Buffer is double the block size because we need space for SW HMAC82* artifacts, i.e:83* - ipad (1 block) + a possible partial block of data.84* - opad (1 block) + digest of H(k ^ ipad || m)85*/86u8 buffer[2 * SHA512_BLOCK_SIZE];87size_t buf_cnt;88dma_addr_t buf_dma_addr;89size_t buf_dma_count;90struct scatterlist *sg;91unsigned int sg_data_total;92unsigned int sg_data_offset;93unsigned int sg_dma_nents;94unsigned int nents;95};9697/**98* struct ocs_hcu_drv - Driver data99* @dev_list: The list of HCU devices.100* @lock: The lock protecting dev_list.101*/102struct ocs_hcu_drv {103struct list_head dev_list;104spinlock_t lock; /* Protects dev_list. */105};106107static struct ocs_hcu_drv ocs_hcu = {108.dev_list = LIST_HEAD_INIT(ocs_hcu.dev_list),109.lock = __SPIN_LOCK_UNLOCKED(ocs_hcu.lock),110};111112/*113* Return the total amount of data in the request; that is: the data in the114* request buffer + the data in the sg list.115*/116static inline unsigned int kmb_get_total_data(struct ocs_hcu_rctx *rctx)117{118return rctx->sg_data_total + rctx->buf_cnt;119}120121/* Move remaining content of scatter-gather list to context buffer. */122static int flush_sg_to_ocs_buffer(struct ocs_hcu_rctx *rctx)123{124size_t count;125126if (rctx->sg_data_total > (sizeof(rctx->buffer) - rctx->buf_cnt)) {127WARN(1, "%s: sg data does not fit in buffer\n", __func__);128return -EINVAL;129}130131while (rctx->sg_data_total) {132if (!rctx->sg) {133WARN(1, "%s: unexpected NULL sg\n", __func__);134return -EINVAL;135}136/*137* If current sg has been fully processed, skip to the next138* one.139*/140if (rctx->sg_data_offset == rctx->sg->length) {141rctx->sg = sg_next(rctx->sg);142rctx->sg_data_offset = 0;143continue;144}145/*146* Determine the maximum data available to copy from the node.147* Minimum of the length left in the sg node, or the total data148* in the request.149*/150count = min(rctx->sg->length - rctx->sg_data_offset,151rctx->sg_data_total);152/* Copy from scatter-list entry to context buffer. */153scatterwalk_map_and_copy(&rctx->buffer[rctx->buf_cnt],154rctx->sg, rctx->sg_data_offset,155count, 0);156157rctx->sg_data_offset += count;158rctx->sg_data_total -= count;159rctx->buf_cnt += count;160}161162return 0;163}164165static struct ocs_hcu_dev *kmb_ocs_hcu_find_dev(struct ahash_request *req)166{167struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);168struct ocs_hcu_ctx *tctx = crypto_ahash_ctx(tfm);169170/* If the HCU device for the request was previously set, return it. */171if (tctx->hcu_dev)172return tctx->hcu_dev;173174/*175* Otherwise, get the first HCU device available (there should be one176* and only one device).177*/178spin_lock_bh(&ocs_hcu.lock);179tctx->hcu_dev = list_first_entry_or_null(&ocs_hcu.dev_list,180struct ocs_hcu_dev,181list);182spin_unlock_bh(&ocs_hcu.lock);183184return tctx->hcu_dev;185}186187/* Free OCS DMA linked list and DMA-able context buffer. */188static void kmb_ocs_hcu_dma_cleanup(struct ahash_request *req,189struct ocs_hcu_rctx *rctx)190{191struct ocs_hcu_dev *hcu_dev = rctx->hcu_dev;192struct device *dev = hcu_dev->dev;193194/* Unmap rctx->buffer (if mapped). */195if (rctx->buf_dma_count) {196dma_unmap_single(dev, rctx->buf_dma_addr, rctx->buf_dma_count,197DMA_TO_DEVICE);198rctx->buf_dma_count = 0;199}200201/* Unmap req->src (if mapped). */202if (rctx->sg_dma_nents) {203dma_unmap_sg(dev, req->src, rctx->nents, DMA_TO_DEVICE);204rctx->sg_dma_nents = 0;205}206207/* Free dma_list (if allocated). */208if (rctx->dma_list) {209ocs_hcu_dma_list_free(hcu_dev, rctx->dma_list);210rctx->dma_list = NULL;211}212}213214/*215* Prepare for DMA operation:216* - DMA-map request context buffer (if needed)217* - DMA-map SG list (only the entries to be processed, see note below)218* - Allocate OCS HCU DMA linked list (number of elements = SG entries to219* process + context buffer (if not empty)).220* - Add DMA-mapped request context buffer to OCS HCU DMA list.221* - Add SG entries to DMA list.222*223* Note: if this is a final request, we process all the data in the SG list,224* otherwise we can only process up to the maximum amount of block-aligned data225* (the remainder will be put into the context buffer and processed in the next226* request).227*/228static int kmb_ocs_dma_prepare(struct ahash_request *req)229{230struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);231struct device *dev = rctx->hcu_dev->dev;232unsigned int remainder = 0;233unsigned int total;234int nents;235size_t count;236int rc;237int i;238239/* This function should be called only when there is data to process. */240total = kmb_get_total_data(rctx);241if (!total)242return -EINVAL;243244/*245* If this is not a final DMA (terminated DMA), the data passed to the246* HCU must be aligned to the block size; compute the remainder data to247* be processed in the next request.248*/249if (!(rctx->flags & REQ_FINAL))250remainder = total % rctx->blk_sz;251252/* Determine the number of scatter gather list entries to process. */253nents = sg_nents_for_len(req->src, rctx->sg_data_total - remainder);254255if (nents < 0)256return nents;257258/* If there are entries to process, map them. */259if (nents) {260rctx->sg_dma_nents = dma_map_sg(dev, req->src, nents,261DMA_TO_DEVICE);262if (!rctx->sg_dma_nents) {263dev_err(dev, "Failed to MAP SG\n");264rc = -ENOMEM;265goto cleanup;266}267268/* Save the value of nents to pass to dma_unmap_sg. */269rctx->nents = nents;270271/*272* The value returned by dma_map_sg() can be < nents; so update273* nents accordingly.274*/275nents = rctx->sg_dma_nents;276}277278/*279* If context buffer is not empty, map it and add extra DMA entry for280* it.281*/282if (rctx->buf_cnt) {283rctx->buf_dma_addr = dma_map_single(dev, rctx->buffer,284rctx->buf_cnt,285DMA_TO_DEVICE);286if (dma_mapping_error(dev, rctx->buf_dma_addr)) {287dev_err(dev, "Failed to map request context buffer\n");288rc = -ENOMEM;289goto cleanup;290}291rctx->buf_dma_count = rctx->buf_cnt;292/* Increase number of dma entries. */293nents++;294}295296/* Allocate OCS HCU DMA list. */297rctx->dma_list = ocs_hcu_dma_list_alloc(rctx->hcu_dev, nents);298if (!rctx->dma_list) {299rc = -ENOMEM;300goto cleanup;301}302303/* Add request context buffer (if previously DMA-mapped) */304if (rctx->buf_dma_count) {305rc = ocs_hcu_dma_list_add_tail(rctx->hcu_dev, rctx->dma_list,306rctx->buf_dma_addr,307rctx->buf_dma_count);308if (rc)309goto cleanup;310}311312/* Add the SG nodes to be processed to the DMA linked list. */313for_each_sg(req->src, rctx->sg, rctx->sg_dma_nents, i) {314/*315* The number of bytes to add to the list entry is the minimum316* between:317* - The DMA length of the SG entry.318* - The data left to be processed.319*/320count = min(rctx->sg_data_total - remainder,321sg_dma_len(rctx->sg) - rctx->sg_data_offset);322/*323* Do not create a zero length DMA descriptor. Check in case of324* zero length SG node.325*/326if (count == 0)327continue;328/* Add sg to HCU DMA list. */329rc = ocs_hcu_dma_list_add_tail(rctx->hcu_dev,330rctx->dma_list,331rctx->sg->dma_address,332count);333if (rc)334goto cleanup;335336/* Update amount of data remaining in SG list. */337rctx->sg_data_total -= count;338339/*340* If remaining data is equal to remainder (note: 'less than'341* case should never happen in practice), we are done: update342* offset and exit the loop.343*/344if (rctx->sg_data_total <= remainder) {345WARN_ON(rctx->sg_data_total < remainder);346rctx->sg_data_offset += count;347break;348}349350/*351* If we get here is because we need to process the next sg in352* the list; set offset within the sg to 0.353*/354rctx->sg_data_offset = 0;355}356357return 0;358cleanup:359dev_err(dev, "Failed to prepare DMA.\n");360kmb_ocs_hcu_dma_cleanup(req, rctx);361362return rc;363}364365static void kmb_ocs_hcu_secure_cleanup(struct ahash_request *req)366{367struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);368369/* Clear buffer of any data. */370memzero_explicit(rctx->buffer, sizeof(rctx->buffer));371}372373static int kmb_ocs_hcu_handle_queue(struct ahash_request *req)374{375struct ocs_hcu_dev *hcu_dev = kmb_ocs_hcu_find_dev(req);376377if (!hcu_dev)378return -ENOENT;379380return crypto_transfer_hash_request_to_engine(hcu_dev->engine, req);381}382383static int prepare_ipad(struct ahash_request *req)384{385struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);386struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);387struct ocs_hcu_ctx *ctx = crypto_ahash_ctx(tfm);388int i;389390WARN(rctx->buf_cnt, "%s: Context buffer is not empty\n", __func__);391WARN(!(rctx->flags & REQ_FLAGS_HMAC_SW),392"%s: HMAC_SW flag is not set\n", __func__);393/*394* Key length must be equal to block size. If key is shorter,395* we pad it with zero (note: key cannot be longer, since396* longer keys are hashed by kmb_ocs_hcu_setkey()).397*/398if (ctx->key_len > rctx->blk_sz) {399WARN(1, "%s: Invalid key length in tfm context\n", __func__);400return -EINVAL;401}402memzero_explicit(&ctx->key[ctx->key_len],403rctx->blk_sz - ctx->key_len);404ctx->key_len = rctx->blk_sz;405/*406* Prepare IPAD for HMAC. Only done for first block.407* HMAC(k,m) = H(k ^ opad || H(k ^ ipad || m))408* k ^ ipad will be first hashed block.409* k ^ opad will be calculated in the final request.410* Only needed if not using HW HMAC.411*/412for (i = 0; i < rctx->blk_sz; i++)413rctx->buffer[i] = ctx->key[i] ^ HMAC_IPAD_VALUE;414rctx->buf_cnt = rctx->blk_sz;415416return 0;417}418419static int kmb_ocs_hcu_do_one_request(struct crypto_engine *engine, void *areq)420{421struct ahash_request *req = container_of(areq, struct ahash_request,422base);423struct ocs_hcu_dev *hcu_dev = kmb_ocs_hcu_find_dev(req);424struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);425struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);426struct ocs_hcu_ctx *tctx = crypto_ahash_ctx(tfm);427int rc;428int i;429430if (!hcu_dev) {431rc = -ENOENT;432goto error;433}434435/*436* If hardware HMAC flag is set, perform HMAC in hardware.437*438* NOTE: this flag implies REQ_FINAL && kmb_get_total_data(rctx)439*/440if (rctx->flags & REQ_FLAGS_HMAC_HW) {441/* Map input data into the HCU DMA linked list. */442rc = kmb_ocs_dma_prepare(req);443if (rc)444goto error;445446rc = ocs_hcu_hmac(hcu_dev, rctx->algo, tctx->key, tctx->key_len,447rctx->dma_list, req->result, rctx->dig_sz);448449/* Unmap data and free DMA list regardless of return code. */450kmb_ocs_hcu_dma_cleanup(req, rctx);451452/* Process previous return code. */453if (rc)454goto error;455456goto done;457}458459/* Handle update request case. */460if (!(rctx->flags & REQ_FINAL)) {461/* Update should always have input data. */462if (!kmb_get_total_data(rctx))463return -EINVAL;464465/* Map input data into the HCU DMA linked list. */466rc = kmb_ocs_dma_prepare(req);467if (rc)468goto error;469470/* Do hashing step. */471rc = ocs_hcu_hash_update(hcu_dev, &rctx->hash_ctx,472rctx->dma_list);473474/* Unmap data and free DMA list regardless of return code. */475kmb_ocs_hcu_dma_cleanup(req, rctx);476477/* Process previous return code. */478if (rc)479goto error;480481/*482* Reset request buffer count (data in the buffer was just483* processed).484*/485rctx->buf_cnt = 0;486/*487* Move remaining sg data into the request buffer, so that it488* will be processed during the next request.489*490* NOTE: we have remaining data if kmb_get_total_data() was not491* a multiple of block size.492*/493rc = flush_sg_to_ocs_buffer(rctx);494if (rc)495goto error;496497goto done;498}499500/* If we get here, this is a final request. */501502/* If there is data to process, use finup. */503if (kmb_get_total_data(rctx)) {504/* Map input data into the HCU DMA linked list. */505rc = kmb_ocs_dma_prepare(req);506if (rc)507goto error;508509/* Do hashing step. */510rc = ocs_hcu_hash_finup(hcu_dev, &rctx->hash_ctx,511rctx->dma_list,512req->result, rctx->dig_sz);513/* Free DMA list regardless of return code. */514kmb_ocs_hcu_dma_cleanup(req, rctx);515516/* Process previous return code. */517if (rc)518goto error;519520} else { /* Otherwise (if we have no data), use final. */521rc = ocs_hcu_hash_final(hcu_dev, &rctx->hash_ctx, req->result,522rctx->dig_sz);523if (rc)524goto error;525}526527/*528* If we are finalizing a SW HMAC request, we just computed the result529* of: H(k ^ ipad || m).530*531* We now need to complete the HMAC calculation with the OPAD step,532* that is, we need to compute H(k ^ opad || digest), where digest is533* the digest we just obtained, i.e., H(k ^ ipad || m).534*/535if (rctx->flags & REQ_FLAGS_HMAC_SW) {536/*537* Compute k ^ opad and store it in the request buffer (which538* is not used anymore at this point).539* Note: key has been padded / hashed already (so keylen ==540* blksz) .541*/542WARN_ON(tctx->key_len != rctx->blk_sz);543for (i = 0; i < rctx->blk_sz; i++)544rctx->buffer[i] = tctx->key[i] ^ HMAC_OPAD_VALUE;545/* Now append the digest to the rest of the buffer. */546for (i = 0; (i < rctx->dig_sz); i++)547rctx->buffer[rctx->blk_sz + i] = req->result[i];548549/* Now hash the buffer to obtain the final HMAC. */550rc = ocs_hcu_digest(hcu_dev, rctx->algo, rctx->buffer,551rctx->blk_sz + rctx->dig_sz, req->result,552rctx->dig_sz);553if (rc)554goto error;555}556557/* Perform secure clean-up. */558kmb_ocs_hcu_secure_cleanup(req);559done:560crypto_finalize_hash_request(hcu_dev->engine, req, 0);561562return 0;563564error:565kmb_ocs_hcu_secure_cleanup(req);566return rc;567}568569static int kmb_ocs_hcu_init(struct ahash_request *req)570{571struct ocs_hcu_dev *hcu_dev = kmb_ocs_hcu_find_dev(req);572struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);573struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);574struct ocs_hcu_ctx *ctx = crypto_ahash_ctx(tfm);575576if (!hcu_dev)577return -ENOENT;578579/* Initialize entire request context to zero. */580memset(rctx, 0, sizeof(*rctx));581582rctx->hcu_dev = hcu_dev;583rctx->dig_sz = crypto_ahash_digestsize(tfm);584585switch (rctx->dig_sz) {586#ifdef CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224587case SHA224_DIGEST_SIZE:588rctx->blk_sz = SHA224_BLOCK_SIZE;589rctx->algo = OCS_HCU_ALGO_SHA224;590break;591#endif /* CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224 */592case SHA256_DIGEST_SIZE:593rctx->blk_sz = SHA256_BLOCK_SIZE;594/*595* SHA256 and SM3 have the same digest size: use info from tfm596* context to find out which one we should use.597*/598rctx->algo = ctx->is_sm3_tfm ? OCS_HCU_ALGO_SM3 :599OCS_HCU_ALGO_SHA256;600break;601case SHA384_DIGEST_SIZE:602rctx->blk_sz = SHA384_BLOCK_SIZE;603rctx->algo = OCS_HCU_ALGO_SHA384;604break;605case SHA512_DIGEST_SIZE:606rctx->blk_sz = SHA512_BLOCK_SIZE;607rctx->algo = OCS_HCU_ALGO_SHA512;608break;609default:610return -EINVAL;611}612613/* Initialize intermediate data. */614ocs_hcu_hash_init(&rctx->hash_ctx, rctx->algo);615616/* If this a HMAC request, set HMAC flag. */617if (ctx->is_hmac_tfm)618rctx->flags |= REQ_FLAGS_HMAC;619620return 0;621}622623static int kmb_ocs_hcu_update(struct ahash_request *req)624{625struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);626int rc;627628if (!req->nbytes)629return 0;630631rctx->sg_data_total = req->nbytes;632rctx->sg_data_offset = 0;633rctx->sg = req->src;634635/*636* If we are doing HMAC, then we must use SW-assisted HMAC, since HW637* HMAC does not support context switching (there it can only be used638* with finup() or digest()).639*/640if (rctx->flags & REQ_FLAGS_HMAC &&641!(rctx->flags & REQ_FLAGS_HMAC_SW)) {642rctx->flags |= REQ_FLAGS_HMAC_SW;643rc = prepare_ipad(req);644if (rc)645return rc;646}647648/*649* If remaining sg_data fits into ctx buffer, just copy it there; we'll650* process it at the next update() or final().651*/652if (rctx->sg_data_total <= (sizeof(rctx->buffer) - rctx->buf_cnt))653return flush_sg_to_ocs_buffer(rctx);654655return kmb_ocs_hcu_handle_queue(req);656}657658/* Common logic for kmb_ocs_hcu_final() and kmb_ocs_hcu_finup(). */659static int kmb_ocs_hcu_fin_common(struct ahash_request *req)660{661struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);662struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);663struct ocs_hcu_ctx *ctx = crypto_ahash_ctx(tfm);664int rc;665666rctx->flags |= REQ_FINAL;667668/*669* If this is a HMAC request and, so far, we didn't have to switch to670* SW HMAC, check if we can use HW HMAC.671*/672if (rctx->flags & REQ_FLAGS_HMAC &&673!(rctx->flags & REQ_FLAGS_HMAC_SW)) {674/*675* If we are here, it means we never processed any data so far,676* so we can use HW HMAC, but only if there is some data to677* process (since OCS HW MAC does not support zero-length678* messages) and the key length is supported by the hardware679* (OCS HCU HW only supports length <= 64); if HW HMAC cannot680* be used, fall back to SW-assisted HMAC.681*/682if (kmb_get_total_data(rctx) &&683ctx->key_len <= OCS_HCU_HW_KEY_LEN) {684rctx->flags |= REQ_FLAGS_HMAC_HW;685} else {686rctx->flags |= REQ_FLAGS_HMAC_SW;687rc = prepare_ipad(req);688if (rc)689return rc;690}691}692693return kmb_ocs_hcu_handle_queue(req);694}695696static int kmb_ocs_hcu_final(struct ahash_request *req)697{698struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);699700rctx->sg_data_total = 0;701rctx->sg_data_offset = 0;702rctx->sg = NULL;703704return kmb_ocs_hcu_fin_common(req);705}706707static int kmb_ocs_hcu_finup(struct ahash_request *req)708{709struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);710711rctx->sg_data_total = req->nbytes;712rctx->sg_data_offset = 0;713rctx->sg = req->src;714715return kmb_ocs_hcu_fin_common(req);716}717718static int kmb_ocs_hcu_digest(struct ahash_request *req)719{720int rc = 0;721struct ocs_hcu_dev *hcu_dev = kmb_ocs_hcu_find_dev(req);722723if (!hcu_dev)724return -ENOENT;725726rc = kmb_ocs_hcu_init(req);727if (rc)728return rc;729730rc = kmb_ocs_hcu_finup(req);731732return rc;733}734735static int kmb_ocs_hcu_export(struct ahash_request *req, void *out)736{737struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);738739/* Intermediate data is always stored and applied per request. */740memcpy(out, rctx, sizeof(*rctx));741742return 0;743}744745static int kmb_ocs_hcu_import(struct ahash_request *req, const void *in)746{747struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);748749/* Intermediate data is always stored and applied per request. */750memcpy(rctx, in, sizeof(*rctx));751752return 0;753}754755static int kmb_ocs_hcu_setkey(struct crypto_ahash *tfm, const u8 *key,756unsigned int keylen)757{758unsigned int digestsize = crypto_ahash_digestsize(tfm);759struct ocs_hcu_ctx *ctx = crypto_ahash_ctx(tfm);760size_t blk_sz = crypto_ahash_blocksize(tfm);761struct crypto_ahash *ahash_tfm;762struct ahash_request *req;763struct crypto_wait wait;764struct scatterlist sg;765const char *alg_name;766int rc;767768/*769* Key length must be equal to block size:770* - If key is shorter, we are done for now (the key will be padded771* later on); this is to maximize the use of HW HMAC (which works772* only for keys <= 64 bytes).773* - If key is longer, we hash it.774*/775if (keylen <= blk_sz) {776memcpy(ctx->key, key, keylen);777ctx->key_len = keylen;778return 0;779}780781switch (digestsize) {782#ifdef CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224783case SHA224_DIGEST_SIZE:784alg_name = "sha224-keembay-ocs";785break;786#endif /* CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224 */787case SHA256_DIGEST_SIZE:788alg_name = ctx->is_sm3_tfm ? "sm3-keembay-ocs" :789"sha256-keembay-ocs";790break;791case SHA384_DIGEST_SIZE:792alg_name = "sha384-keembay-ocs";793break;794case SHA512_DIGEST_SIZE:795alg_name = "sha512-keembay-ocs";796break;797default:798return -EINVAL;799}800801ahash_tfm = crypto_alloc_ahash(alg_name, 0, 0);802if (IS_ERR(ahash_tfm))803return PTR_ERR(ahash_tfm);804805req = ahash_request_alloc(ahash_tfm, GFP_KERNEL);806if (!req) {807rc = -ENOMEM;808goto err_free_ahash;809}810811crypto_init_wait(&wait);812ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,813crypto_req_done, &wait);814crypto_ahash_clear_flags(ahash_tfm, ~0);815816sg_init_one(&sg, key, keylen);817ahash_request_set_crypt(req, &sg, ctx->key, keylen);818819rc = crypto_wait_req(crypto_ahash_digest(req), &wait);820if (rc == 0)821ctx->key_len = digestsize;822823ahash_request_free(req);824err_free_ahash:825crypto_free_ahash(ahash_tfm);826827return rc;828}829830/* Set request size and initialize tfm context. */831static void __cra_init(struct crypto_tfm *tfm, struct ocs_hcu_ctx *ctx)832{833crypto_ahash_set_reqsize_dma(__crypto_ahash_cast(tfm),834sizeof(struct ocs_hcu_rctx));835}836837static int kmb_ocs_hcu_sha_cra_init(struct crypto_tfm *tfm)838{839struct ocs_hcu_ctx *ctx = crypto_tfm_ctx(tfm);840841__cra_init(tfm, ctx);842843return 0;844}845846static int kmb_ocs_hcu_sm3_cra_init(struct crypto_tfm *tfm)847{848struct ocs_hcu_ctx *ctx = crypto_tfm_ctx(tfm);849850__cra_init(tfm, ctx);851852ctx->is_sm3_tfm = true;853854return 0;855}856857static int kmb_ocs_hcu_hmac_sm3_cra_init(struct crypto_tfm *tfm)858{859struct ocs_hcu_ctx *ctx = crypto_tfm_ctx(tfm);860861__cra_init(tfm, ctx);862863ctx->is_sm3_tfm = true;864ctx->is_hmac_tfm = true;865866return 0;867}868869static int kmb_ocs_hcu_hmac_cra_init(struct crypto_tfm *tfm)870{871struct ocs_hcu_ctx *ctx = crypto_tfm_ctx(tfm);872873__cra_init(tfm, ctx);874875ctx->is_hmac_tfm = true;876877return 0;878}879880/* Function called when 'tfm' is de-initialized. */881static void kmb_ocs_hcu_hmac_cra_exit(struct crypto_tfm *tfm)882{883struct ocs_hcu_ctx *ctx = crypto_tfm_ctx(tfm);884885/* Clear the key. */886memzero_explicit(ctx->key, sizeof(ctx->key));887}888889static struct ahash_engine_alg ocs_hcu_algs[] = {890#ifdef CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224891{892.base.init = kmb_ocs_hcu_init,893.base.update = kmb_ocs_hcu_update,894.base.final = kmb_ocs_hcu_final,895.base.finup = kmb_ocs_hcu_finup,896.base.digest = kmb_ocs_hcu_digest,897.base.export = kmb_ocs_hcu_export,898.base.import = kmb_ocs_hcu_import,899.base.halg = {900.digestsize = SHA224_DIGEST_SIZE,901.statesize = sizeof(struct ocs_hcu_rctx),902.base = {903.cra_name = "sha224",904.cra_driver_name = "sha224-keembay-ocs",905.cra_priority = 255,906.cra_flags = CRYPTO_ALG_ASYNC,907.cra_blocksize = SHA224_BLOCK_SIZE,908.cra_ctxsize = sizeof(struct ocs_hcu_ctx),909.cra_alignmask = 0,910.cra_module = THIS_MODULE,911.cra_init = kmb_ocs_hcu_sha_cra_init,912}913},914.op.do_one_request = kmb_ocs_hcu_do_one_request,915},916{917.base.init = kmb_ocs_hcu_init,918.base.update = kmb_ocs_hcu_update,919.base.final = kmb_ocs_hcu_final,920.base.finup = kmb_ocs_hcu_finup,921.base.digest = kmb_ocs_hcu_digest,922.base.export = kmb_ocs_hcu_export,923.base.import = kmb_ocs_hcu_import,924.base.setkey = kmb_ocs_hcu_setkey,925.base.halg = {926.digestsize = SHA224_DIGEST_SIZE,927.statesize = sizeof(struct ocs_hcu_rctx),928.base = {929.cra_name = "hmac(sha224)",930.cra_driver_name = "hmac-sha224-keembay-ocs",931.cra_priority = 255,932.cra_flags = CRYPTO_ALG_ASYNC,933.cra_blocksize = SHA224_BLOCK_SIZE,934.cra_ctxsize = sizeof(struct ocs_hcu_ctx),935.cra_alignmask = 0,936.cra_module = THIS_MODULE,937.cra_init = kmb_ocs_hcu_hmac_cra_init,938.cra_exit = kmb_ocs_hcu_hmac_cra_exit,939}940},941.op.do_one_request = kmb_ocs_hcu_do_one_request,942},943#endif /* CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224 */944{945.base.init = kmb_ocs_hcu_init,946.base.update = kmb_ocs_hcu_update,947.base.final = kmb_ocs_hcu_final,948.base.finup = kmb_ocs_hcu_finup,949.base.digest = kmb_ocs_hcu_digest,950.base.export = kmb_ocs_hcu_export,951.base.import = kmb_ocs_hcu_import,952.base.halg = {953.digestsize = SHA256_DIGEST_SIZE,954.statesize = sizeof(struct ocs_hcu_rctx),955.base = {956.cra_name = "sha256",957.cra_driver_name = "sha256-keembay-ocs",958.cra_priority = 255,959.cra_flags = CRYPTO_ALG_ASYNC,960.cra_blocksize = SHA256_BLOCK_SIZE,961.cra_ctxsize = sizeof(struct ocs_hcu_ctx),962.cra_alignmask = 0,963.cra_module = THIS_MODULE,964.cra_init = kmb_ocs_hcu_sha_cra_init,965}966},967.op.do_one_request = kmb_ocs_hcu_do_one_request,968},969{970.base.init = kmb_ocs_hcu_init,971.base.update = kmb_ocs_hcu_update,972.base.final = kmb_ocs_hcu_final,973.base.finup = kmb_ocs_hcu_finup,974.base.digest = kmb_ocs_hcu_digest,975.base.export = kmb_ocs_hcu_export,976.base.import = kmb_ocs_hcu_import,977.base.setkey = kmb_ocs_hcu_setkey,978.base.halg = {979.digestsize = SHA256_DIGEST_SIZE,980.statesize = sizeof(struct ocs_hcu_rctx),981.base = {982.cra_name = "hmac(sha256)",983.cra_driver_name = "hmac-sha256-keembay-ocs",984.cra_priority = 255,985.cra_flags = CRYPTO_ALG_ASYNC,986.cra_blocksize = SHA256_BLOCK_SIZE,987.cra_ctxsize = sizeof(struct ocs_hcu_ctx),988.cra_alignmask = 0,989.cra_module = THIS_MODULE,990.cra_init = kmb_ocs_hcu_hmac_cra_init,991.cra_exit = kmb_ocs_hcu_hmac_cra_exit,992}993},994.op.do_one_request = kmb_ocs_hcu_do_one_request,995},996{997.base.init = kmb_ocs_hcu_init,998.base.update = kmb_ocs_hcu_update,999.base.final = kmb_ocs_hcu_final,1000.base.finup = kmb_ocs_hcu_finup,1001.base.digest = kmb_ocs_hcu_digest,1002.base.export = kmb_ocs_hcu_export,1003.base.import = kmb_ocs_hcu_import,1004.base.halg = {1005.digestsize = SM3_DIGEST_SIZE,1006.statesize = sizeof(struct ocs_hcu_rctx),1007.base = {1008.cra_name = "sm3",1009.cra_driver_name = "sm3-keembay-ocs",1010.cra_priority = 255,1011.cra_flags = CRYPTO_ALG_ASYNC,1012.cra_blocksize = SM3_BLOCK_SIZE,1013.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1014.cra_alignmask = 0,1015.cra_module = THIS_MODULE,1016.cra_init = kmb_ocs_hcu_sm3_cra_init,1017}1018},1019.op.do_one_request = kmb_ocs_hcu_do_one_request,1020},1021{1022.base.init = kmb_ocs_hcu_init,1023.base.update = kmb_ocs_hcu_update,1024.base.final = kmb_ocs_hcu_final,1025.base.finup = kmb_ocs_hcu_finup,1026.base.digest = kmb_ocs_hcu_digest,1027.base.export = kmb_ocs_hcu_export,1028.base.import = kmb_ocs_hcu_import,1029.base.setkey = kmb_ocs_hcu_setkey,1030.base.halg = {1031.digestsize = SM3_DIGEST_SIZE,1032.statesize = sizeof(struct ocs_hcu_rctx),1033.base = {1034.cra_name = "hmac(sm3)",1035.cra_driver_name = "hmac-sm3-keembay-ocs",1036.cra_priority = 255,1037.cra_flags = CRYPTO_ALG_ASYNC,1038.cra_blocksize = SM3_BLOCK_SIZE,1039.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1040.cra_alignmask = 0,1041.cra_module = THIS_MODULE,1042.cra_init = kmb_ocs_hcu_hmac_sm3_cra_init,1043.cra_exit = kmb_ocs_hcu_hmac_cra_exit,1044}1045},1046.op.do_one_request = kmb_ocs_hcu_do_one_request,1047},1048{1049.base.init = kmb_ocs_hcu_init,1050.base.update = kmb_ocs_hcu_update,1051.base.final = kmb_ocs_hcu_final,1052.base.finup = kmb_ocs_hcu_finup,1053.base.digest = kmb_ocs_hcu_digest,1054.base.export = kmb_ocs_hcu_export,1055.base.import = kmb_ocs_hcu_import,1056.base.halg = {1057.digestsize = SHA384_DIGEST_SIZE,1058.statesize = sizeof(struct ocs_hcu_rctx),1059.base = {1060.cra_name = "sha384",1061.cra_driver_name = "sha384-keembay-ocs",1062.cra_priority = 255,1063.cra_flags = CRYPTO_ALG_ASYNC,1064.cra_blocksize = SHA384_BLOCK_SIZE,1065.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1066.cra_alignmask = 0,1067.cra_module = THIS_MODULE,1068.cra_init = kmb_ocs_hcu_sha_cra_init,1069}1070},1071.op.do_one_request = kmb_ocs_hcu_do_one_request,1072},1073{1074.base.init = kmb_ocs_hcu_init,1075.base.update = kmb_ocs_hcu_update,1076.base.final = kmb_ocs_hcu_final,1077.base.finup = kmb_ocs_hcu_finup,1078.base.digest = kmb_ocs_hcu_digest,1079.base.export = kmb_ocs_hcu_export,1080.base.import = kmb_ocs_hcu_import,1081.base.setkey = kmb_ocs_hcu_setkey,1082.base.halg = {1083.digestsize = SHA384_DIGEST_SIZE,1084.statesize = sizeof(struct ocs_hcu_rctx),1085.base = {1086.cra_name = "hmac(sha384)",1087.cra_driver_name = "hmac-sha384-keembay-ocs",1088.cra_priority = 255,1089.cra_flags = CRYPTO_ALG_ASYNC,1090.cra_blocksize = SHA384_BLOCK_SIZE,1091.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1092.cra_alignmask = 0,1093.cra_module = THIS_MODULE,1094.cra_init = kmb_ocs_hcu_hmac_cra_init,1095.cra_exit = kmb_ocs_hcu_hmac_cra_exit,1096}1097},1098.op.do_one_request = kmb_ocs_hcu_do_one_request,1099},1100{1101.base.init = kmb_ocs_hcu_init,1102.base.update = kmb_ocs_hcu_update,1103.base.final = kmb_ocs_hcu_final,1104.base.finup = kmb_ocs_hcu_finup,1105.base.digest = kmb_ocs_hcu_digest,1106.base.export = kmb_ocs_hcu_export,1107.base.import = kmb_ocs_hcu_import,1108.base.halg = {1109.digestsize = SHA512_DIGEST_SIZE,1110.statesize = sizeof(struct ocs_hcu_rctx),1111.base = {1112.cra_name = "sha512",1113.cra_driver_name = "sha512-keembay-ocs",1114.cra_priority = 255,1115.cra_flags = CRYPTO_ALG_ASYNC,1116.cra_blocksize = SHA512_BLOCK_SIZE,1117.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1118.cra_alignmask = 0,1119.cra_module = THIS_MODULE,1120.cra_init = kmb_ocs_hcu_sha_cra_init,1121}1122},1123.op.do_one_request = kmb_ocs_hcu_do_one_request,1124},1125{1126.base.init = kmb_ocs_hcu_init,1127.base.update = kmb_ocs_hcu_update,1128.base.final = kmb_ocs_hcu_final,1129.base.finup = kmb_ocs_hcu_finup,1130.base.digest = kmb_ocs_hcu_digest,1131.base.export = kmb_ocs_hcu_export,1132.base.import = kmb_ocs_hcu_import,1133.base.setkey = kmb_ocs_hcu_setkey,1134.base.halg = {1135.digestsize = SHA512_DIGEST_SIZE,1136.statesize = sizeof(struct ocs_hcu_rctx),1137.base = {1138.cra_name = "hmac(sha512)",1139.cra_driver_name = "hmac-sha512-keembay-ocs",1140.cra_priority = 255,1141.cra_flags = CRYPTO_ALG_ASYNC,1142.cra_blocksize = SHA512_BLOCK_SIZE,1143.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1144.cra_alignmask = 0,1145.cra_module = THIS_MODULE,1146.cra_init = kmb_ocs_hcu_hmac_cra_init,1147.cra_exit = kmb_ocs_hcu_hmac_cra_exit,1148}1149},1150.op.do_one_request = kmb_ocs_hcu_do_one_request,1151},1152};11531154/* Device tree driver match. */1155static const struct of_device_id kmb_ocs_hcu_of_match[] = {1156{1157.compatible = "intel,keembay-ocs-hcu",1158},1159{}1160};1161MODULE_DEVICE_TABLE(of, kmb_ocs_hcu_of_match);11621163static void kmb_ocs_hcu_remove(struct platform_device *pdev)1164{1165struct ocs_hcu_dev *hcu_dev = platform_get_drvdata(pdev);11661167crypto_engine_unregister_ahashes(ocs_hcu_algs, ARRAY_SIZE(ocs_hcu_algs));11681169crypto_engine_exit(hcu_dev->engine);11701171spin_lock_bh(&ocs_hcu.lock);1172list_del(&hcu_dev->list);1173spin_unlock_bh(&ocs_hcu.lock);1174}11751176static int kmb_ocs_hcu_probe(struct platform_device *pdev)1177{1178struct device *dev = &pdev->dev;1179struct ocs_hcu_dev *hcu_dev;1180int rc;11811182hcu_dev = devm_kzalloc(dev, sizeof(*hcu_dev), GFP_KERNEL);1183if (!hcu_dev)1184return -ENOMEM;11851186hcu_dev->dev = dev;11871188platform_set_drvdata(pdev, hcu_dev);1189rc = dma_set_mask_and_coherent(&pdev->dev, OCS_HCU_DMA_BIT_MASK);1190if (rc)1191return rc;11921193hcu_dev->io_base = devm_platform_ioremap_resource(pdev, 0);1194if (IS_ERR(hcu_dev->io_base))1195return PTR_ERR(hcu_dev->io_base);11961197init_completion(&hcu_dev->irq_done);11981199/* Get and request IRQ. */1200hcu_dev->irq = platform_get_irq(pdev, 0);1201if (hcu_dev->irq < 0)1202return hcu_dev->irq;12031204rc = devm_request_threaded_irq(&pdev->dev, hcu_dev->irq,1205ocs_hcu_irq_handler, NULL, 0,1206"keembay-ocs-hcu", hcu_dev);1207if (rc < 0) {1208dev_err(dev, "Could not request IRQ.\n");1209return rc;1210}12111212INIT_LIST_HEAD(&hcu_dev->list);12131214spin_lock_bh(&ocs_hcu.lock);1215list_add_tail(&hcu_dev->list, &ocs_hcu.dev_list);1216spin_unlock_bh(&ocs_hcu.lock);12171218/* Initialize crypto engine */1219hcu_dev->engine = crypto_engine_alloc_init(dev, 1);1220if (!hcu_dev->engine) {1221rc = -ENOMEM;1222goto list_del;1223}12241225rc = crypto_engine_start(hcu_dev->engine);1226if (rc) {1227dev_err(dev, "Could not start engine.\n");1228goto cleanup;1229}12301231/* Security infrastructure guarantees OCS clock is enabled. */12321233rc = crypto_engine_register_ahashes(ocs_hcu_algs, ARRAY_SIZE(ocs_hcu_algs));1234if (rc) {1235dev_err(dev, "Could not register algorithms.\n");1236goto cleanup;1237}12381239return 0;12401241cleanup:1242crypto_engine_exit(hcu_dev->engine);1243list_del:1244spin_lock_bh(&ocs_hcu.lock);1245list_del(&hcu_dev->list);1246spin_unlock_bh(&ocs_hcu.lock);12471248return rc;1249}12501251/* The OCS driver is a platform device. */1252static struct platform_driver kmb_ocs_hcu_driver = {1253.probe = kmb_ocs_hcu_probe,1254.remove = kmb_ocs_hcu_remove,1255.driver = {1256.name = DRV_NAME,1257.of_match_table = kmb_ocs_hcu_of_match,1258},1259};12601261module_platform_driver(kmb_ocs_hcu_driver);12621263MODULE_LICENSE("GPL");126412651266