11b44c5a6SAntoine Ténart /* 21b44c5a6SAntoine Ténart * Copyright (C) 2017 Marvell 31b44c5a6SAntoine Ténart * 41b44c5a6SAntoine Ténart * Antoine Tenart <antoine.tenart@free-electrons.com> 51b44c5a6SAntoine Ténart * 61b44c5a6SAntoine Ténart * This file is licensed under the terms of the GNU General Public 71b44c5a6SAntoine Ténart * License version 2. This program is licensed "as is" without any 81b44c5a6SAntoine Ténart * warranty of any kind, whether express or implied. 91b44c5a6SAntoine Ténart */ 101b44c5a6SAntoine Ténart 11aed3731eSAntoine Ténart #include <crypto/hmac.h> 121b44c5a6SAntoine Ténart #include <crypto/sha.h> 131b44c5a6SAntoine Ténart #include <linux/device.h> 141b44c5a6SAntoine Ténart #include <linux/dma-mapping.h> 151b44c5a6SAntoine Ténart #include <linux/dmapool.h> 161b44c5a6SAntoine Ténart 171b44c5a6SAntoine Ténart #include "safexcel.h" 181b44c5a6SAntoine Ténart 191b44c5a6SAntoine Ténart struct safexcel_ahash_ctx { 201b44c5a6SAntoine Ténart struct safexcel_context base; 211b44c5a6SAntoine Ténart struct safexcel_crypto_priv *priv; 221b44c5a6SAntoine Ténart 231b44c5a6SAntoine Ténart u32 alg; 241b44c5a6SAntoine Ténart u32 digest; 251b44c5a6SAntoine Ténart 261b44c5a6SAntoine Ténart u32 ipad[SHA1_DIGEST_SIZE / sizeof(u32)]; 271b44c5a6SAntoine Ténart u32 opad[SHA1_DIGEST_SIZE / sizeof(u32)]; 281b44c5a6SAntoine Ténart }; 291b44c5a6SAntoine Ténart 301b44c5a6SAntoine Ténart struct safexcel_ahash_req { 311b44c5a6SAntoine Ténart bool last_req; 321b44c5a6SAntoine Ténart bool finish; 331b44c5a6SAntoine Ténart bool hmac; 341eb7b403SOfer Heifetz bool needs_inv; 351b44c5a6SAntoine Ténart 36c957f8b3SAntoine Ténart int nents; 37c957f8b3SAntoine Ténart 381b44c5a6SAntoine Ténart u8 state_sz; /* expected sate size, only set once */ 392973633eSAntoine Ténart u32 state[SHA256_DIGEST_SIZE / sizeof(u32)] __aligned(sizeof(u32)); 401b44c5a6SAntoine Ténart 411b44c5a6SAntoine Ténart u64 len; 421b44c5a6SAntoine Ténart u64 processed; 431b44c5a6SAntoine Ténart 441b44c5a6SAntoine Ténart u8 cache[SHA256_BLOCK_SIZE] __aligned(sizeof(u32)); 451b44c5a6SAntoine Ténart u8 cache_next[SHA256_BLOCK_SIZE] __aligned(sizeof(u32)); 461b44c5a6SAntoine Ténart }; 471b44c5a6SAntoine Ténart 481b44c5a6SAntoine Ténart struct safexcel_ahash_export_state { 491b44c5a6SAntoine Ténart u64 len; 501b44c5a6SAntoine Ténart u64 processed; 511b44c5a6SAntoine Ténart 521b44c5a6SAntoine Ténart u32 state[SHA256_DIGEST_SIZE / sizeof(u32)]; 531b44c5a6SAntoine Ténart u8 cache[SHA256_BLOCK_SIZE]; 541b44c5a6SAntoine Ténart }; 551b44c5a6SAntoine Ténart 561b44c5a6SAntoine Ténart static void safexcel_hash_token(struct safexcel_command_desc *cdesc, 571b44c5a6SAntoine Ténart u32 input_length, u32 result_length) 581b44c5a6SAntoine Ténart { 591b44c5a6SAntoine Ténart struct safexcel_token *token = 601b44c5a6SAntoine Ténart (struct safexcel_token *)cdesc->control_data.token; 611b44c5a6SAntoine Ténart 621b44c5a6SAntoine Ténart token[0].opcode = EIP197_TOKEN_OPCODE_DIRECTION; 631b44c5a6SAntoine Ténart token[0].packet_length = input_length; 641b44c5a6SAntoine Ténart token[0].stat = EIP197_TOKEN_STAT_LAST_HASH; 651b44c5a6SAntoine Ténart token[0].instructions = EIP197_TOKEN_INS_TYPE_HASH; 661b44c5a6SAntoine Ténart 671b44c5a6SAntoine Ténart token[1].opcode = EIP197_TOKEN_OPCODE_INSERT; 681b44c5a6SAntoine Ténart token[1].packet_length = result_length; 691b44c5a6SAntoine Ténart token[1].stat = EIP197_TOKEN_STAT_LAST_HASH | 701b44c5a6SAntoine Ténart EIP197_TOKEN_STAT_LAST_PACKET; 711b44c5a6SAntoine Ténart token[1].instructions = EIP197_TOKEN_INS_TYPE_OUTPUT | 721b44c5a6SAntoine Ténart EIP197_TOKEN_INS_INSERT_HASH_DIGEST; 731b44c5a6SAntoine Ténart } 741b44c5a6SAntoine Ténart 751b44c5a6SAntoine Ténart static void safexcel_context_control(struct safexcel_ahash_ctx *ctx, 761b44c5a6SAntoine Ténart struct safexcel_ahash_req *req, 771b44c5a6SAntoine Ténart struct safexcel_command_desc *cdesc, 781b44c5a6SAntoine Ténart unsigned int digestsize, 791b44c5a6SAntoine Ténart unsigned int blocksize) 801b44c5a6SAntoine Ténart { 811b44c5a6SAntoine Ténart int i; 821b44c5a6SAntoine Ténart 831b44c5a6SAntoine Ténart cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_HASH_OUT; 841b44c5a6SAntoine Ténart cdesc->control_data.control0 |= ctx->alg; 851b44c5a6SAntoine Ténart cdesc->control_data.control0 |= ctx->digest; 861b44c5a6SAntoine Ténart 871b44c5a6SAntoine Ténart if (ctx->digest == CONTEXT_CONTROL_DIGEST_PRECOMPUTED) { 881b44c5a6SAntoine Ténart if (req->processed) { 891b44c5a6SAntoine Ténart if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA1) 901b44c5a6SAntoine Ténart cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(6); 911b44c5a6SAntoine Ténart else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA224 || 921b44c5a6SAntoine Ténart ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA256) 931b44c5a6SAntoine Ténart cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(9); 941b44c5a6SAntoine Ténart 951b44c5a6SAntoine Ténart cdesc->control_data.control1 |= CONTEXT_CONTROL_DIGEST_CNT; 961b44c5a6SAntoine Ténart } else { 971b44c5a6SAntoine Ténart cdesc->control_data.control0 |= CONTEXT_CONTROL_RESTART_HASH; 981b44c5a6SAntoine Ténart } 991b44c5a6SAntoine Ténart 1001b44c5a6SAntoine Ténart if (!req->finish) 1011b44c5a6SAntoine Ténart cdesc->control_data.control0 |= CONTEXT_CONTROL_NO_FINISH_HASH; 1021b44c5a6SAntoine Ténart 1031b44c5a6SAntoine Ténart /* 1041b44c5a6SAntoine Ténart * Copy the input digest if needed, and setup the context 1051b44c5a6SAntoine Ténart * fields. Do this now as we need it to setup the first command 1061b44c5a6SAntoine Ténart * descriptor. 1071b44c5a6SAntoine Ténart */ 1081b44c5a6SAntoine Ténart if (req->processed) { 1091b44c5a6SAntoine Ténart for (i = 0; i < digestsize / sizeof(u32); i++) 1101b44c5a6SAntoine Ténart ctx->base.ctxr->data[i] = cpu_to_le32(req->state[i]); 1111b44c5a6SAntoine Ténart 1121b44c5a6SAntoine Ténart if (req->finish) 1131b44c5a6SAntoine Ténart ctx->base.ctxr->data[i] = cpu_to_le32(req->processed / blocksize); 1141b44c5a6SAntoine Ténart } 1151b44c5a6SAntoine Ténart } else if (ctx->digest == CONTEXT_CONTROL_DIGEST_HMAC) { 1161b44c5a6SAntoine Ténart cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(10); 1171b44c5a6SAntoine Ténart 1181b44c5a6SAntoine Ténart memcpy(ctx->base.ctxr->data, ctx->ipad, digestsize); 1191b44c5a6SAntoine Ténart memcpy(ctx->base.ctxr->data + digestsize / sizeof(u32), 1201b44c5a6SAntoine Ténart ctx->opad, digestsize); 1211b44c5a6SAntoine Ténart } 1221b44c5a6SAntoine Ténart } 1231b44c5a6SAntoine Ténart 1241eb7b403SOfer Heifetz static int safexcel_handle_req_result(struct safexcel_crypto_priv *priv, int ring, 1251b44c5a6SAntoine Ténart struct crypto_async_request *async, 1261b44c5a6SAntoine Ténart bool *should_complete, int *ret) 1271b44c5a6SAntoine Ténart { 1281b44c5a6SAntoine Ténart struct safexcel_result_desc *rdesc; 1291b44c5a6SAntoine Ténart struct ahash_request *areq = ahash_request_cast(async); 1301b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 1311b44c5a6SAntoine Ténart struct safexcel_ahash_req *sreq = ahash_request_ctx(areq); 1322973633eSAntoine Ténart int cache_len; 1331b44c5a6SAntoine Ténart 1341b44c5a6SAntoine Ténart *ret = 0; 1351b44c5a6SAntoine Ténart 1361b44c5a6SAntoine Ténart spin_lock_bh(&priv->ring[ring].egress_lock); 1371b44c5a6SAntoine Ténart rdesc = safexcel_ring_next_rptr(priv, &priv->ring[ring].rdr); 1381b44c5a6SAntoine Ténart if (IS_ERR(rdesc)) { 1391b44c5a6SAntoine Ténart dev_err(priv->dev, 1401b44c5a6SAntoine Ténart "hash: result: could not retrieve the result descriptor\n"); 1411b44c5a6SAntoine Ténart *ret = PTR_ERR(rdesc); 1421b44c5a6SAntoine Ténart } else if (rdesc->result_data.error_code) { 1431b44c5a6SAntoine Ténart dev_err(priv->dev, 1441b44c5a6SAntoine Ténart "hash: result: result descriptor error (%d)\n", 1451b44c5a6SAntoine Ténart rdesc->result_data.error_code); 1461b44c5a6SAntoine Ténart *ret = -EINVAL; 1471b44c5a6SAntoine Ténart } 1481b44c5a6SAntoine Ténart 1491b44c5a6SAntoine Ténart safexcel_complete(priv, ring); 1501b44c5a6SAntoine Ténart spin_unlock_bh(&priv->ring[ring].egress_lock); 1511b44c5a6SAntoine Ténart 1521b44c5a6SAntoine Ténart if (sreq->finish) 1532973633eSAntoine Ténart memcpy(areq->result, sreq->state, 1542973633eSAntoine Ténart crypto_ahash_digestsize(ahash)); 1551b44c5a6SAntoine Ténart 156c957f8b3SAntoine Ténart if (sreq->nents) { 157c957f8b3SAntoine Ténart dma_unmap_sg(priv->dev, areq->src, sreq->nents, DMA_TO_DEVICE); 158c957f8b3SAntoine Ténart sreq->nents = 0; 159c957f8b3SAntoine Ténart } 1601b44c5a6SAntoine Ténart 1611b44c5a6SAntoine Ténart safexcel_free_context(priv, async, sreq->state_sz); 1621b44c5a6SAntoine Ténart 1631b44c5a6SAntoine Ténart cache_len = sreq->len - sreq->processed; 1641b44c5a6SAntoine Ténart if (cache_len) 1651b44c5a6SAntoine Ténart memcpy(sreq->cache, sreq->cache_next, cache_len); 1661b44c5a6SAntoine Ténart 1671b44c5a6SAntoine Ténart *should_complete = true; 1681b44c5a6SAntoine Ténart 1691b44c5a6SAntoine Ténart return 1; 1701b44c5a6SAntoine Ténart } 1711b44c5a6SAntoine Ténart 1721eb7b403SOfer Heifetz static int safexcel_ahash_send_req(struct crypto_async_request *async, int ring, 1731eb7b403SOfer Heifetz struct safexcel_request *request, 1741eb7b403SOfer Heifetz int *commands, int *results) 1751b44c5a6SAntoine Ténart { 1761b44c5a6SAntoine Ténart struct ahash_request *areq = ahash_request_cast(async); 1771b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 1781b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 1791b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 1801b44c5a6SAntoine Ténart struct safexcel_crypto_priv *priv = ctx->priv; 1811b44c5a6SAntoine Ténart struct safexcel_command_desc *cdesc, *first_cdesc = NULL; 1821b44c5a6SAntoine Ténart struct safexcel_result_desc *rdesc; 1831b44c5a6SAntoine Ténart struct scatterlist *sg; 184c957f8b3SAntoine Ténart int i, queued, len, cache_len, extra, n_cdesc = 0, ret = 0; 1851b44c5a6SAntoine Ténart 1861b44c5a6SAntoine Ténart queued = len = req->len - req->processed; 1871b44c5a6SAntoine Ténart if (queued < crypto_ahash_blocksize(ahash)) 1881b44c5a6SAntoine Ténart cache_len = queued; 1891b44c5a6SAntoine Ténart else 1901b44c5a6SAntoine Ténart cache_len = queued - areq->nbytes; 1911b44c5a6SAntoine Ténart 1921b44c5a6SAntoine Ténart /* 1931b44c5a6SAntoine Ténart * If this is not the last request and the queued data does not fit 1941b44c5a6SAntoine Ténart * into full blocks, cache it for the next send() call. 1951b44c5a6SAntoine Ténart */ 1961b44c5a6SAntoine Ténart extra = queued & (crypto_ahash_blocksize(ahash) - 1); 1971b44c5a6SAntoine Ténart if (!req->last_req && extra) { 1981b44c5a6SAntoine Ténart sg_pcopy_to_buffer(areq->src, sg_nents(areq->src), 1991b44c5a6SAntoine Ténart req->cache_next, extra, areq->nbytes - extra); 2001b44c5a6SAntoine Ténart 2011b44c5a6SAntoine Ténart queued -= extra; 2021b44c5a6SAntoine Ténart len -= extra; 2031b44c5a6SAntoine Ténart } 2041b44c5a6SAntoine Ténart 2051b44c5a6SAntoine Ténart spin_lock_bh(&priv->ring[ring].egress_lock); 2061b44c5a6SAntoine Ténart 2071b44c5a6SAntoine Ténart /* Add a command descriptor for the cached data, if any */ 2081b44c5a6SAntoine Ténart if (cache_len) { 2091b44c5a6SAntoine Ténart ctx->base.cache = kzalloc(cache_len, EIP197_GFP_FLAGS(*async)); 2101b44c5a6SAntoine Ténart if (!ctx->base.cache) { 2111b44c5a6SAntoine Ténart ret = -ENOMEM; 2121b44c5a6SAntoine Ténart goto unlock; 2131b44c5a6SAntoine Ténart } 2141b44c5a6SAntoine Ténart memcpy(ctx->base.cache, req->cache, cache_len); 2151b44c5a6SAntoine Ténart ctx->base.cache_dma = dma_map_single(priv->dev, ctx->base.cache, 2161b44c5a6SAntoine Ténart cache_len, DMA_TO_DEVICE); 2171b44c5a6SAntoine Ténart if (dma_mapping_error(priv->dev, ctx->base.cache_dma)) { 2181b44c5a6SAntoine Ténart ret = -EINVAL; 2191b44c5a6SAntoine Ténart goto free_cache; 2201b44c5a6SAntoine Ténart } 2211b44c5a6SAntoine Ténart 2221b44c5a6SAntoine Ténart ctx->base.cache_sz = cache_len; 2231b44c5a6SAntoine Ténart first_cdesc = safexcel_add_cdesc(priv, ring, 1, 2241b44c5a6SAntoine Ténart (cache_len == len), 2251b44c5a6SAntoine Ténart ctx->base.cache_dma, 2261b44c5a6SAntoine Ténart cache_len, len, 2271b44c5a6SAntoine Ténart ctx->base.ctxr_dma); 2281b44c5a6SAntoine Ténart if (IS_ERR(first_cdesc)) { 2291b44c5a6SAntoine Ténart ret = PTR_ERR(first_cdesc); 2301b44c5a6SAntoine Ténart goto unmap_cache; 2311b44c5a6SAntoine Ténart } 2321b44c5a6SAntoine Ténart n_cdesc++; 2331b44c5a6SAntoine Ténart 2341b44c5a6SAntoine Ténart queued -= cache_len; 2351b44c5a6SAntoine Ténart if (!queued) 2361b44c5a6SAntoine Ténart goto send_command; 2371b44c5a6SAntoine Ténart } 2381b44c5a6SAntoine Ténart 2391b44c5a6SAntoine Ténart /* Now handle the current ahash request buffer(s) */ 240c957f8b3SAntoine Ténart req->nents = dma_map_sg(priv->dev, areq->src, 2411b44c5a6SAntoine Ténart sg_nents_for_len(areq->src, areq->nbytes), 2421b44c5a6SAntoine Ténart DMA_TO_DEVICE); 243c957f8b3SAntoine Ténart if (!req->nents) { 2441b44c5a6SAntoine Ténart ret = -ENOMEM; 2451b44c5a6SAntoine Ténart goto cdesc_rollback; 2461b44c5a6SAntoine Ténart } 2471b44c5a6SAntoine Ténart 248c957f8b3SAntoine Ténart for_each_sg(areq->src, sg, req->nents, i) { 2491b44c5a6SAntoine Ténart int sglen = sg_dma_len(sg); 2501b44c5a6SAntoine Ténart 2511b44c5a6SAntoine Ténart /* Do not overflow the request */ 2521b44c5a6SAntoine Ténart if (queued - sglen < 0) 2531b44c5a6SAntoine Ténart sglen = queued; 2541b44c5a6SAntoine Ténart 2551b44c5a6SAntoine Ténart cdesc = safexcel_add_cdesc(priv, ring, !n_cdesc, 2561b44c5a6SAntoine Ténart !(queued - sglen), sg_dma_address(sg), 2571b44c5a6SAntoine Ténart sglen, len, ctx->base.ctxr_dma); 2581b44c5a6SAntoine Ténart if (IS_ERR(cdesc)) { 2591b44c5a6SAntoine Ténart ret = PTR_ERR(cdesc); 2601b44c5a6SAntoine Ténart goto cdesc_rollback; 2611b44c5a6SAntoine Ténart } 2621b44c5a6SAntoine Ténart n_cdesc++; 2631b44c5a6SAntoine Ténart 2641b44c5a6SAntoine Ténart if (n_cdesc == 1) 2651b44c5a6SAntoine Ténart first_cdesc = cdesc; 2661b44c5a6SAntoine Ténart 2671b44c5a6SAntoine Ténart queued -= sglen; 2681b44c5a6SAntoine Ténart if (!queued) 2691b44c5a6SAntoine Ténart break; 2701b44c5a6SAntoine Ténart } 2711b44c5a6SAntoine Ténart 2721b44c5a6SAntoine Ténart send_command: 2731b44c5a6SAntoine Ténart /* Setup the context options */ 2741b44c5a6SAntoine Ténart safexcel_context_control(ctx, req, first_cdesc, req->state_sz, 2751b44c5a6SAntoine Ténart crypto_ahash_blocksize(ahash)); 2761b44c5a6SAntoine Ténart 2771b44c5a6SAntoine Ténart /* Add the token */ 2781b44c5a6SAntoine Ténart safexcel_hash_token(first_cdesc, len, req->state_sz); 2791b44c5a6SAntoine Ténart 2802973633eSAntoine Ténart ctx->base.result_dma = dma_map_single(priv->dev, req->state, 2811b44c5a6SAntoine Ténart req->state_sz, DMA_FROM_DEVICE); 2821b44c5a6SAntoine Ténart if (dma_mapping_error(priv->dev, ctx->base.result_dma)) { 2831b44c5a6SAntoine Ténart ret = -EINVAL; 2841b44c5a6SAntoine Ténart goto cdesc_rollback; 2851b44c5a6SAntoine Ténart } 2861b44c5a6SAntoine Ténart 2871b44c5a6SAntoine Ténart /* Add a result descriptor */ 2881b44c5a6SAntoine Ténart rdesc = safexcel_add_rdesc(priv, ring, 1, 1, ctx->base.result_dma, 2891b44c5a6SAntoine Ténart req->state_sz); 2901b44c5a6SAntoine Ténart if (IS_ERR(rdesc)) { 2911b44c5a6SAntoine Ténart ret = PTR_ERR(rdesc); 2921b44c5a6SAntoine Ténart goto cdesc_rollback; 2931b44c5a6SAntoine Ténart } 2941b44c5a6SAntoine Ténart 2951b44c5a6SAntoine Ténart spin_unlock_bh(&priv->ring[ring].egress_lock); 2961b44c5a6SAntoine Ténart 29797858434SAntoine Ténart req->processed += len; 29897858434SAntoine Ténart request->req = &areq->base; 29997858434SAntoine Ténart 3001b44c5a6SAntoine Ténart *commands = n_cdesc; 3011b44c5a6SAntoine Ténart *results = 1; 3021b44c5a6SAntoine Ténart return 0; 3031b44c5a6SAntoine Ténart 3041b44c5a6SAntoine Ténart cdesc_rollback: 3051b44c5a6SAntoine Ténart for (i = 0; i < n_cdesc; i++) 3061b44c5a6SAntoine Ténart safexcel_ring_rollback_wptr(priv, &priv->ring[ring].cdr); 3071b44c5a6SAntoine Ténart unmap_cache: 3081b44c5a6SAntoine Ténart if (ctx->base.cache_dma) { 3091b44c5a6SAntoine Ténart dma_unmap_single(priv->dev, ctx->base.cache_dma, 3101b44c5a6SAntoine Ténart ctx->base.cache_sz, DMA_TO_DEVICE); 3111b44c5a6SAntoine Ténart ctx->base.cache_sz = 0; 3121b44c5a6SAntoine Ténart } 3131b44c5a6SAntoine Ténart free_cache: 3141b44c5a6SAntoine Ténart kfree(ctx->base.cache); 3151b44c5a6SAntoine Ténart ctx->base.cache = NULL; 3161b44c5a6SAntoine Ténart 3171b44c5a6SAntoine Ténart unlock: 3181b44c5a6SAntoine Ténart spin_unlock_bh(&priv->ring[ring].egress_lock); 3191b44c5a6SAntoine Ténart return ret; 3201b44c5a6SAntoine Ténart } 3211b44c5a6SAntoine Ténart 3221b44c5a6SAntoine Ténart static inline bool safexcel_ahash_needs_inv_get(struct ahash_request *areq) 3231b44c5a6SAntoine Ténart { 3241b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 3251b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 3261b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 3271b44c5a6SAntoine Ténart unsigned int state_w_sz = req->state_sz / sizeof(u32); 3281b44c5a6SAntoine Ténart int i; 3291b44c5a6SAntoine Ténart 3301b44c5a6SAntoine Ténart for (i = 0; i < state_w_sz; i++) 3311b44c5a6SAntoine Ténart if (ctx->base.ctxr->data[i] != cpu_to_le32(req->state[i])) 3321b44c5a6SAntoine Ténart return true; 3331b44c5a6SAntoine Ténart 3341b44c5a6SAntoine Ténart if (ctx->base.ctxr->data[state_w_sz] != 3351b44c5a6SAntoine Ténart cpu_to_le32(req->processed / crypto_ahash_blocksize(ahash))) 3361b44c5a6SAntoine Ténart return true; 3371b44c5a6SAntoine Ténart 3381b44c5a6SAntoine Ténart return false; 3391b44c5a6SAntoine Ténart } 3401b44c5a6SAntoine Ténart 3411b44c5a6SAntoine Ténart static int safexcel_handle_inv_result(struct safexcel_crypto_priv *priv, 3421b44c5a6SAntoine Ténart int ring, 3431b44c5a6SAntoine Ténart struct crypto_async_request *async, 3441b44c5a6SAntoine Ténart bool *should_complete, int *ret) 3451b44c5a6SAntoine Ténart { 3461b44c5a6SAntoine Ténart struct safexcel_result_desc *rdesc; 3471b44c5a6SAntoine Ténart struct ahash_request *areq = ahash_request_cast(async); 3481b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 3491b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(ahash); 3501b44c5a6SAntoine Ténart int enq_ret; 3511b44c5a6SAntoine Ténart 3521b44c5a6SAntoine Ténart *ret = 0; 3531b44c5a6SAntoine Ténart 3541b44c5a6SAntoine Ténart spin_lock_bh(&priv->ring[ring].egress_lock); 3551b44c5a6SAntoine Ténart rdesc = safexcel_ring_next_rptr(priv, &priv->ring[ring].rdr); 3561b44c5a6SAntoine Ténart if (IS_ERR(rdesc)) { 3571b44c5a6SAntoine Ténart dev_err(priv->dev, 3581b44c5a6SAntoine Ténart "hash: invalidate: could not retrieve the result descriptor\n"); 3591b44c5a6SAntoine Ténart *ret = PTR_ERR(rdesc); 3601b44c5a6SAntoine Ténart } else if (rdesc->result_data.error_code) { 3611b44c5a6SAntoine Ténart dev_err(priv->dev, 3621b44c5a6SAntoine Ténart "hash: invalidate: result descriptor error (%d)\n", 3631b44c5a6SAntoine Ténart rdesc->result_data.error_code); 3641b44c5a6SAntoine Ténart *ret = -EINVAL; 3651b44c5a6SAntoine Ténart } 3661b44c5a6SAntoine Ténart 3671b44c5a6SAntoine Ténart safexcel_complete(priv, ring); 3681b44c5a6SAntoine Ténart spin_unlock_bh(&priv->ring[ring].egress_lock); 3691b44c5a6SAntoine Ténart 3701b44c5a6SAntoine Ténart if (ctx->base.exit_inv) { 3711b44c5a6SAntoine Ténart dma_pool_free(priv->context_pool, ctx->base.ctxr, 3721b44c5a6SAntoine Ténart ctx->base.ctxr_dma); 3731b44c5a6SAntoine Ténart 3741b44c5a6SAntoine Ténart *should_complete = true; 3751b44c5a6SAntoine Ténart return 1; 3761b44c5a6SAntoine Ténart } 3771b44c5a6SAntoine Ténart 37886671abbSAntoine Ténart ring = safexcel_select_ring(priv); 37986671abbSAntoine Ténart ctx->base.ring = ring; 3801b44c5a6SAntoine Ténart 38186671abbSAntoine Ténart spin_lock_bh(&priv->ring[ring].queue_lock); 38286671abbSAntoine Ténart enq_ret = crypto_enqueue_request(&priv->ring[ring].queue, async); 38386671abbSAntoine Ténart spin_unlock_bh(&priv->ring[ring].queue_lock); 3841b44c5a6SAntoine Ténart 3851b44c5a6SAntoine Ténart if (enq_ret != -EINPROGRESS) 3861b44c5a6SAntoine Ténart *ret = enq_ret; 3871b44c5a6SAntoine Ténart 3888472e778SAntoine Ténart queue_work(priv->ring[ring].workqueue, 3898472e778SAntoine Ténart &priv->ring[ring].work_data.work); 39086671abbSAntoine Ténart 3911b44c5a6SAntoine Ténart *should_complete = false; 3921b44c5a6SAntoine Ténart 3931b44c5a6SAntoine Ténart return 1; 3941b44c5a6SAntoine Ténart } 3951b44c5a6SAntoine Ténart 3961eb7b403SOfer Heifetz static int safexcel_handle_result(struct safexcel_crypto_priv *priv, int ring, 3971eb7b403SOfer Heifetz struct crypto_async_request *async, 3981eb7b403SOfer Heifetz bool *should_complete, int *ret) 3991eb7b403SOfer Heifetz { 4001eb7b403SOfer Heifetz struct ahash_request *areq = ahash_request_cast(async); 4011eb7b403SOfer Heifetz struct safexcel_ahash_req *req = ahash_request_ctx(areq); 4021eb7b403SOfer Heifetz int err; 4031eb7b403SOfer Heifetz 404871df319SAntoine Ténart BUG_ON(priv->version == EIP97 && req->needs_inv); 405871df319SAntoine Ténart 4061eb7b403SOfer Heifetz if (req->needs_inv) { 4071eb7b403SOfer Heifetz req->needs_inv = false; 4081eb7b403SOfer Heifetz err = safexcel_handle_inv_result(priv, ring, async, 4091eb7b403SOfer Heifetz should_complete, ret); 4101eb7b403SOfer Heifetz } else { 4111eb7b403SOfer Heifetz err = safexcel_handle_req_result(priv, ring, async, 4121eb7b403SOfer Heifetz should_complete, ret); 4131eb7b403SOfer Heifetz } 4141eb7b403SOfer Heifetz 4151eb7b403SOfer Heifetz return err; 4161eb7b403SOfer Heifetz } 4171eb7b403SOfer Heifetz 4181b44c5a6SAntoine Ténart static int safexcel_ahash_send_inv(struct crypto_async_request *async, 4191b44c5a6SAntoine Ténart int ring, struct safexcel_request *request, 4201b44c5a6SAntoine Ténart int *commands, int *results) 4211b44c5a6SAntoine Ténart { 4221b44c5a6SAntoine Ténart struct ahash_request *areq = ahash_request_cast(async); 4231b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 4241b44c5a6SAntoine Ténart int ret; 4251b44c5a6SAntoine Ténart 4265290ad6eSOfer Heifetz ret = safexcel_invalidate_cache(async, ctx->priv, 4271b44c5a6SAntoine Ténart ctx->base.ctxr_dma, ring, request); 4281b44c5a6SAntoine Ténart if (unlikely(ret)) 4291b44c5a6SAntoine Ténart return ret; 4301b44c5a6SAntoine Ténart 4311b44c5a6SAntoine Ténart *commands = 1; 4321b44c5a6SAntoine Ténart *results = 1; 4331b44c5a6SAntoine Ténart 4341b44c5a6SAntoine Ténart return 0; 4351b44c5a6SAntoine Ténart } 4361b44c5a6SAntoine Ténart 4371eb7b403SOfer Heifetz static int safexcel_ahash_send(struct crypto_async_request *async, 4381eb7b403SOfer Heifetz int ring, struct safexcel_request *request, 4391eb7b403SOfer Heifetz int *commands, int *results) 4401eb7b403SOfer Heifetz { 4411eb7b403SOfer Heifetz struct ahash_request *areq = ahash_request_cast(async); 4421eb7b403SOfer Heifetz struct safexcel_ahash_req *req = ahash_request_ctx(areq); 4431eb7b403SOfer Heifetz int ret; 4441eb7b403SOfer Heifetz 4451eb7b403SOfer Heifetz if (req->needs_inv) 4461eb7b403SOfer Heifetz ret = safexcel_ahash_send_inv(async, ring, request, 4471eb7b403SOfer Heifetz commands, results); 4481eb7b403SOfer Heifetz else 4491eb7b403SOfer Heifetz ret = safexcel_ahash_send_req(async, ring, request, 4501eb7b403SOfer Heifetz commands, results); 4511eb7b403SOfer Heifetz return ret; 4521eb7b403SOfer Heifetz } 4531eb7b403SOfer Heifetz 4541b44c5a6SAntoine Ténart static int safexcel_ahash_exit_inv(struct crypto_tfm *tfm) 4551b44c5a6SAntoine Ténart { 4561b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm); 4571b44c5a6SAntoine Ténart struct safexcel_crypto_priv *priv = ctx->priv; 4587cad2fabSAntoine Ténart AHASH_REQUEST_ON_STACK(req, __crypto_ahash_cast(tfm)); 4597cad2fabSAntoine Ténart struct safexcel_ahash_req *rctx = ahash_request_ctx(req); 4603e1166b9SArnd Bergmann struct safexcel_inv_result result = {}; 46186671abbSAntoine Ténart int ring = ctx->base.ring; 4621b44c5a6SAntoine Ténart 4637cad2fabSAntoine Ténart memset(req, 0, sizeof(struct ahash_request)); 4641b44c5a6SAntoine Ténart 4651b44c5a6SAntoine Ténart /* create invalidation request */ 4661b44c5a6SAntoine Ténart init_completion(&result.completion); 4677cad2fabSAntoine Ténart ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 4681b44c5a6SAntoine Ténart safexcel_inv_complete, &result); 4691b44c5a6SAntoine Ténart 4707cad2fabSAntoine Ténart ahash_request_set_tfm(req, __crypto_ahash_cast(tfm)); 4717cad2fabSAntoine Ténart ctx = crypto_tfm_ctx(req->base.tfm); 4721b44c5a6SAntoine Ténart ctx->base.exit_inv = true; 4731eb7b403SOfer Heifetz rctx->needs_inv = true; 4741b44c5a6SAntoine Ténart 47586671abbSAntoine Ténart spin_lock_bh(&priv->ring[ring].queue_lock); 4767cad2fabSAntoine Ténart crypto_enqueue_request(&priv->ring[ring].queue, &req->base); 47786671abbSAntoine Ténart spin_unlock_bh(&priv->ring[ring].queue_lock); 4781b44c5a6SAntoine Ténart 4798472e778SAntoine Ténart queue_work(priv->ring[ring].workqueue, 4808472e778SAntoine Ténart &priv->ring[ring].work_data.work); 4811b44c5a6SAntoine Ténart 4821b44c5a6SAntoine Ténart wait_for_completion_interruptible(&result.completion); 4831b44c5a6SAntoine Ténart 4841b44c5a6SAntoine Ténart if (result.error) { 4851b44c5a6SAntoine Ténart dev_warn(priv->dev, "hash: completion error (%d)\n", 4861b44c5a6SAntoine Ténart result.error); 4871b44c5a6SAntoine Ténart return result.error; 4881b44c5a6SAntoine Ténart } 4891b44c5a6SAntoine Ténart 4901b44c5a6SAntoine Ténart return 0; 4911b44c5a6SAntoine Ténart } 4921b44c5a6SAntoine Ténart 493cc75f5ceSAntoine Ténart /* safexcel_ahash_cache: cache data until at least one request can be sent to 494cc75f5ceSAntoine Ténart * the engine, aka. when there is at least 1 block size in the pipe. 495cc75f5ceSAntoine Ténart */ 4961b44c5a6SAntoine Ténart static int safexcel_ahash_cache(struct ahash_request *areq) 4971b44c5a6SAntoine Ténart { 4981b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 4991b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 5001b44c5a6SAntoine Ténart int queued, cache_len; 5011b44c5a6SAntoine Ténart 502cc75f5ceSAntoine Ténart /* cache_len: everyting accepted by the driver but not sent yet, 503cc75f5ceSAntoine Ténart * tot sz handled by update() - last req sz - tot sz handled by send() 504cc75f5ceSAntoine Ténart */ 5051b44c5a6SAntoine Ténart cache_len = req->len - areq->nbytes - req->processed; 506cc75f5ceSAntoine Ténart /* queued: everything accepted by the driver which will be handled by 507cc75f5ceSAntoine Ténart * the next send() calls. 508cc75f5ceSAntoine Ténart * tot sz handled by update() - tot sz handled by send() 509cc75f5ceSAntoine Ténart */ 5101b44c5a6SAntoine Ténart queued = req->len - req->processed; 5111b44c5a6SAntoine Ténart 5121b44c5a6SAntoine Ténart /* 5131b44c5a6SAntoine Ténart * In case there isn't enough bytes to proceed (less than a 5141b44c5a6SAntoine Ténart * block size), cache the data until we have enough. 5151b44c5a6SAntoine Ténart */ 5161b44c5a6SAntoine Ténart if (cache_len + areq->nbytes <= crypto_ahash_blocksize(ahash)) { 5171b44c5a6SAntoine Ténart sg_pcopy_to_buffer(areq->src, sg_nents(areq->src), 5181b44c5a6SAntoine Ténart req->cache + cache_len, 5191b44c5a6SAntoine Ténart areq->nbytes, 0); 5201b44c5a6SAntoine Ténart return areq->nbytes; 5211b44c5a6SAntoine Ténart } 5221b44c5a6SAntoine Ténart 523dfbcc08fSAntoine Ténart /* We couldn't cache all the data */ 5241b44c5a6SAntoine Ténart return -E2BIG; 5251b44c5a6SAntoine Ténart } 5261b44c5a6SAntoine Ténart 5271b44c5a6SAntoine Ténart static int safexcel_ahash_enqueue(struct ahash_request *areq) 5281b44c5a6SAntoine Ténart { 5291b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 5301b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 5311b44c5a6SAntoine Ténart struct safexcel_crypto_priv *priv = ctx->priv; 53286671abbSAntoine Ténart int ret, ring; 5331b44c5a6SAntoine Ténart 5341eb7b403SOfer Heifetz req->needs_inv = false; 5351b44c5a6SAntoine Ténart 536c4daf4ccSOfer Heifetz if (ctx->base.ctxr) { 537871df319SAntoine Ténart if (priv->version == EIP197 && 538871df319SAntoine Ténart !ctx->base.needs_inv && req->processed && 539c4daf4ccSOfer Heifetz ctx->digest == CONTEXT_CONTROL_DIGEST_PRECOMPUTED) 540c4daf4ccSOfer Heifetz /* We're still setting needs_inv here, even though it is 541c4daf4ccSOfer Heifetz * cleared right away, because the needs_inv flag can be 542c4daf4ccSOfer Heifetz * set in other functions and we want to keep the same 543c4daf4ccSOfer Heifetz * logic. 544c4daf4ccSOfer Heifetz */ 5451b44c5a6SAntoine Ténart ctx->base.needs_inv = safexcel_ahash_needs_inv_get(areq); 5461b44c5a6SAntoine Ténart 5471eb7b403SOfer Heifetz if (ctx->base.needs_inv) { 5481eb7b403SOfer Heifetz ctx->base.needs_inv = false; 5491eb7b403SOfer Heifetz req->needs_inv = true; 5501eb7b403SOfer Heifetz } 5511b44c5a6SAntoine Ténart } else { 5521b44c5a6SAntoine Ténart ctx->base.ring = safexcel_select_ring(priv); 5531b44c5a6SAntoine Ténart ctx->base.ctxr = dma_pool_zalloc(priv->context_pool, 5541b44c5a6SAntoine Ténart EIP197_GFP_FLAGS(areq->base), 5551b44c5a6SAntoine Ténart &ctx->base.ctxr_dma); 5561b44c5a6SAntoine Ténart if (!ctx->base.ctxr) 5571b44c5a6SAntoine Ténart return -ENOMEM; 5581b44c5a6SAntoine Ténart } 5591b44c5a6SAntoine Ténart 56086671abbSAntoine Ténart ring = ctx->base.ring; 5611b44c5a6SAntoine Ténart 56286671abbSAntoine Ténart spin_lock_bh(&priv->ring[ring].queue_lock); 56386671abbSAntoine Ténart ret = crypto_enqueue_request(&priv->ring[ring].queue, &areq->base); 56486671abbSAntoine Ténart spin_unlock_bh(&priv->ring[ring].queue_lock); 56586671abbSAntoine Ténart 5668472e778SAntoine Ténart queue_work(priv->ring[ring].workqueue, 5678472e778SAntoine Ténart &priv->ring[ring].work_data.work); 5681b44c5a6SAntoine Ténart 5691b44c5a6SAntoine Ténart return ret; 5701b44c5a6SAntoine Ténart } 5711b44c5a6SAntoine Ténart 5721b44c5a6SAntoine Ténart static int safexcel_ahash_update(struct ahash_request *areq) 5731b44c5a6SAntoine Ténart { 5741b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 5751b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 5761b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 5771b44c5a6SAntoine Ténart 5781b44c5a6SAntoine Ténart /* If the request is 0 length, do nothing */ 5791b44c5a6SAntoine Ténart if (!areq->nbytes) 5801b44c5a6SAntoine Ténart return 0; 5811b44c5a6SAntoine Ténart 5821b44c5a6SAntoine Ténart req->len += areq->nbytes; 5831b44c5a6SAntoine Ténart 5841b44c5a6SAntoine Ténart safexcel_ahash_cache(areq); 5851b44c5a6SAntoine Ténart 5861b44c5a6SAntoine Ténart /* 5871b44c5a6SAntoine Ténart * We're not doing partial updates when performing an hmac request. 5881b44c5a6SAntoine Ténart * Everything will be handled by the final() call. 5891b44c5a6SAntoine Ténart */ 5901b44c5a6SAntoine Ténart if (ctx->digest == CONTEXT_CONTROL_DIGEST_HMAC) 5911b44c5a6SAntoine Ténart return 0; 5921b44c5a6SAntoine Ténart 5931b44c5a6SAntoine Ténart if (req->hmac) 5941b44c5a6SAntoine Ténart return safexcel_ahash_enqueue(areq); 5951b44c5a6SAntoine Ténart 5961b44c5a6SAntoine Ténart if (!req->last_req && 5971b44c5a6SAntoine Ténart req->len - req->processed > crypto_ahash_blocksize(ahash)) 5981b44c5a6SAntoine Ténart return safexcel_ahash_enqueue(areq); 5991b44c5a6SAntoine Ténart 6001b44c5a6SAntoine Ténart return 0; 6011b44c5a6SAntoine Ténart } 6021b44c5a6SAntoine Ténart 6031b44c5a6SAntoine Ténart static int safexcel_ahash_final(struct ahash_request *areq) 6041b44c5a6SAntoine Ténart { 6051b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 6061b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 6071b44c5a6SAntoine Ténart 6081b44c5a6SAntoine Ténart req->last_req = true; 6091b44c5a6SAntoine Ténart req->finish = true; 6101b44c5a6SAntoine Ténart 6111b44c5a6SAntoine Ténart /* If we have an overall 0 length request */ 6121b44c5a6SAntoine Ténart if (!(req->len + areq->nbytes)) { 6131b44c5a6SAntoine Ténart if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA1) 6141b44c5a6SAntoine Ténart memcpy(areq->result, sha1_zero_message_hash, 6151b44c5a6SAntoine Ténart SHA1_DIGEST_SIZE); 6161b44c5a6SAntoine Ténart else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA224) 6171b44c5a6SAntoine Ténart memcpy(areq->result, sha224_zero_message_hash, 6181b44c5a6SAntoine Ténart SHA224_DIGEST_SIZE); 6191b44c5a6SAntoine Ténart else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA256) 6201b44c5a6SAntoine Ténart memcpy(areq->result, sha256_zero_message_hash, 6211b44c5a6SAntoine Ténart SHA256_DIGEST_SIZE); 6221b44c5a6SAntoine Ténart 6231b44c5a6SAntoine Ténart return 0; 6241b44c5a6SAntoine Ténart } 6251b44c5a6SAntoine Ténart 6261b44c5a6SAntoine Ténart return safexcel_ahash_enqueue(areq); 6271b44c5a6SAntoine Ténart } 6281b44c5a6SAntoine Ténart 6291b44c5a6SAntoine Ténart static int safexcel_ahash_finup(struct ahash_request *areq) 6301b44c5a6SAntoine Ténart { 6311b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 6321b44c5a6SAntoine Ténart 6331b44c5a6SAntoine Ténart req->last_req = true; 6341b44c5a6SAntoine Ténart req->finish = true; 6351b44c5a6SAntoine Ténart 6361b44c5a6SAntoine Ténart safexcel_ahash_update(areq); 6371b44c5a6SAntoine Ténart return safexcel_ahash_final(areq); 6381b44c5a6SAntoine Ténart } 6391b44c5a6SAntoine Ténart 6401b44c5a6SAntoine Ténart static int safexcel_ahash_export(struct ahash_request *areq, void *out) 6411b44c5a6SAntoine Ténart { 6421b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 6431b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 6441b44c5a6SAntoine Ténart struct safexcel_ahash_export_state *export = out; 6451b44c5a6SAntoine Ténart 6461b44c5a6SAntoine Ténart export->len = req->len; 6471b44c5a6SAntoine Ténart export->processed = req->processed; 6481b44c5a6SAntoine Ténart 6491b44c5a6SAntoine Ténart memcpy(export->state, req->state, req->state_sz); 6501b44c5a6SAntoine Ténart memcpy(export->cache, req->cache, crypto_ahash_blocksize(ahash)); 6511b44c5a6SAntoine Ténart 6521b44c5a6SAntoine Ténart return 0; 6531b44c5a6SAntoine Ténart } 6541b44c5a6SAntoine Ténart 6551b44c5a6SAntoine Ténart static int safexcel_ahash_import(struct ahash_request *areq, const void *in) 6561b44c5a6SAntoine Ténart { 6571b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 6581b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 6591b44c5a6SAntoine Ténart const struct safexcel_ahash_export_state *export = in; 6601b44c5a6SAntoine Ténart int ret; 6611b44c5a6SAntoine Ténart 6621b44c5a6SAntoine Ténart ret = crypto_ahash_init(areq); 6631b44c5a6SAntoine Ténart if (ret) 6641b44c5a6SAntoine Ténart return ret; 6651b44c5a6SAntoine Ténart 6661b44c5a6SAntoine Ténart req->len = export->len; 6671b44c5a6SAntoine Ténart req->processed = export->processed; 6681b44c5a6SAntoine Ténart 6691b44c5a6SAntoine Ténart memcpy(req->cache, export->cache, crypto_ahash_blocksize(ahash)); 6701b44c5a6SAntoine Ténart memcpy(req->state, export->state, req->state_sz); 6711b44c5a6SAntoine Ténart 6721b44c5a6SAntoine Ténart return 0; 6731b44c5a6SAntoine Ténart } 6741b44c5a6SAntoine Ténart 6751b44c5a6SAntoine Ténart static int safexcel_ahash_cra_init(struct crypto_tfm *tfm) 6761b44c5a6SAntoine Ténart { 6771b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm); 6781b44c5a6SAntoine Ténart struct safexcel_alg_template *tmpl = 6791b44c5a6SAntoine Ténart container_of(__crypto_ahash_alg(tfm->__crt_alg), 6801b44c5a6SAntoine Ténart struct safexcel_alg_template, alg.ahash); 6811b44c5a6SAntoine Ténart 6821b44c5a6SAntoine Ténart ctx->priv = tmpl->priv; 6831eb7b403SOfer Heifetz ctx->base.send = safexcel_ahash_send; 6841eb7b403SOfer Heifetz ctx->base.handle_result = safexcel_handle_result; 6851b44c5a6SAntoine Ténart 6861b44c5a6SAntoine Ténart crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 6871b44c5a6SAntoine Ténart sizeof(struct safexcel_ahash_req)); 6881b44c5a6SAntoine Ténart return 0; 6891b44c5a6SAntoine Ténart } 6901b44c5a6SAntoine Ténart 6911b44c5a6SAntoine Ténart static int safexcel_sha1_init(struct ahash_request *areq) 6921b44c5a6SAntoine Ténart { 6931b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 6941b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 6951b44c5a6SAntoine Ténart 6961b44c5a6SAntoine Ténart memset(req, 0, sizeof(*req)); 6971b44c5a6SAntoine Ténart 6981b44c5a6SAntoine Ténart req->state[0] = SHA1_H0; 6991b44c5a6SAntoine Ténart req->state[1] = SHA1_H1; 7001b44c5a6SAntoine Ténart req->state[2] = SHA1_H2; 7011b44c5a6SAntoine Ténart req->state[3] = SHA1_H3; 7021b44c5a6SAntoine Ténart req->state[4] = SHA1_H4; 7031b44c5a6SAntoine Ténart 7041b44c5a6SAntoine Ténart ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA1; 7051b44c5a6SAntoine Ténart ctx->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED; 7061b44c5a6SAntoine Ténart req->state_sz = SHA1_DIGEST_SIZE; 7071b44c5a6SAntoine Ténart 7081b44c5a6SAntoine Ténart return 0; 7091b44c5a6SAntoine Ténart } 7101b44c5a6SAntoine Ténart 7111b44c5a6SAntoine Ténart static int safexcel_sha1_digest(struct ahash_request *areq) 7121b44c5a6SAntoine Ténart { 7131b44c5a6SAntoine Ténart int ret = safexcel_sha1_init(areq); 7141b44c5a6SAntoine Ténart 7151b44c5a6SAntoine Ténart if (ret) 7161b44c5a6SAntoine Ténart return ret; 7171b44c5a6SAntoine Ténart 7181b44c5a6SAntoine Ténart return safexcel_ahash_finup(areq); 7191b44c5a6SAntoine Ténart } 7201b44c5a6SAntoine Ténart 7211b44c5a6SAntoine Ténart static void safexcel_ahash_cra_exit(struct crypto_tfm *tfm) 7221b44c5a6SAntoine Ténart { 7231b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm); 7241b44c5a6SAntoine Ténart struct safexcel_crypto_priv *priv = ctx->priv; 7251b44c5a6SAntoine Ténart int ret; 7261b44c5a6SAntoine Ténart 7271b44c5a6SAntoine Ténart /* context not allocated, skip invalidation */ 7281b44c5a6SAntoine Ténart if (!ctx->base.ctxr) 7291b44c5a6SAntoine Ténart return; 7301b44c5a6SAntoine Ténart 731871df319SAntoine Ténart if (priv->version == EIP197) { 7321b44c5a6SAntoine Ténart ret = safexcel_ahash_exit_inv(tfm); 7331b44c5a6SAntoine Ténart if (ret) 7341b44c5a6SAntoine Ténart dev_warn(priv->dev, "hash: invalidation error %d\n", ret); 735871df319SAntoine Ténart } else { 736871df319SAntoine Ténart dma_pool_free(priv->context_pool, ctx->base.ctxr, 737871df319SAntoine Ténart ctx->base.ctxr_dma); 738871df319SAntoine Ténart } 7391b44c5a6SAntoine Ténart } 7401b44c5a6SAntoine Ténart 7411b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_sha1 = { 7421b44c5a6SAntoine Ténart .type = SAFEXCEL_ALG_TYPE_AHASH, 7431b44c5a6SAntoine Ténart .alg.ahash = { 7441b44c5a6SAntoine Ténart .init = safexcel_sha1_init, 7451b44c5a6SAntoine Ténart .update = safexcel_ahash_update, 7461b44c5a6SAntoine Ténart .final = safexcel_ahash_final, 7471b44c5a6SAntoine Ténart .finup = safexcel_ahash_finup, 7481b44c5a6SAntoine Ténart .digest = safexcel_sha1_digest, 7491b44c5a6SAntoine Ténart .export = safexcel_ahash_export, 7501b44c5a6SAntoine Ténart .import = safexcel_ahash_import, 7511b44c5a6SAntoine Ténart .halg = { 7521b44c5a6SAntoine Ténart .digestsize = SHA1_DIGEST_SIZE, 7531b44c5a6SAntoine Ténart .statesize = sizeof(struct safexcel_ahash_export_state), 7541b44c5a6SAntoine Ténart .base = { 7551b44c5a6SAntoine Ténart .cra_name = "sha1", 7561b44c5a6SAntoine Ténart .cra_driver_name = "safexcel-sha1", 7571b44c5a6SAntoine Ténart .cra_priority = 300, 7581b44c5a6SAntoine Ténart .cra_flags = CRYPTO_ALG_ASYNC | 7591b44c5a6SAntoine Ténart CRYPTO_ALG_KERN_DRIVER_ONLY, 7601b44c5a6SAntoine Ténart .cra_blocksize = SHA1_BLOCK_SIZE, 7611b44c5a6SAntoine Ténart .cra_ctxsize = sizeof(struct safexcel_ahash_ctx), 7621b44c5a6SAntoine Ténart .cra_init = safexcel_ahash_cra_init, 7631b44c5a6SAntoine Ténart .cra_exit = safexcel_ahash_cra_exit, 7641b44c5a6SAntoine Ténart .cra_module = THIS_MODULE, 7651b44c5a6SAntoine Ténart }, 7661b44c5a6SAntoine Ténart }, 7671b44c5a6SAntoine Ténart }, 7681b44c5a6SAntoine Ténart }; 7691b44c5a6SAntoine Ténart 7701b44c5a6SAntoine Ténart static int safexcel_hmac_sha1_init(struct ahash_request *areq) 7711b44c5a6SAntoine Ténart { 7721b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 7731b44c5a6SAntoine Ténart 7741b44c5a6SAntoine Ténart safexcel_sha1_init(areq); 7751b44c5a6SAntoine Ténart ctx->digest = CONTEXT_CONTROL_DIGEST_HMAC; 7761b44c5a6SAntoine Ténart return 0; 7771b44c5a6SAntoine Ténart } 7781b44c5a6SAntoine Ténart 7791b44c5a6SAntoine Ténart static int safexcel_hmac_sha1_digest(struct ahash_request *areq) 7801b44c5a6SAntoine Ténart { 7811b44c5a6SAntoine Ténart int ret = safexcel_hmac_sha1_init(areq); 7821b44c5a6SAntoine Ténart 7831b44c5a6SAntoine Ténart if (ret) 7841b44c5a6SAntoine Ténart return ret; 7851b44c5a6SAntoine Ténart 7861b44c5a6SAntoine Ténart return safexcel_ahash_finup(areq); 7871b44c5a6SAntoine Ténart } 7881b44c5a6SAntoine Ténart 7891b44c5a6SAntoine Ténart struct safexcel_ahash_result { 7901b44c5a6SAntoine Ténart struct completion completion; 7911b44c5a6SAntoine Ténart int error; 7921b44c5a6SAntoine Ténart }; 7931b44c5a6SAntoine Ténart 7941b44c5a6SAntoine Ténart static void safexcel_ahash_complete(struct crypto_async_request *req, int error) 7951b44c5a6SAntoine Ténart { 7961b44c5a6SAntoine Ténart struct safexcel_ahash_result *result = req->data; 7971b44c5a6SAntoine Ténart 7981b44c5a6SAntoine Ténart if (error == -EINPROGRESS) 7991b44c5a6SAntoine Ténart return; 8001b44c5a6SAntoine Ténart 8011b44c5a6SAntoine Ténart result->error = error; 8021b44c5a6SAntoine Ténart complete(&result->completion); 8031b44c5a6SAntoine Ténart } 8041b44c5a6SAntoine Ténart 8051b44c5a6SAntoine Ténart static int safexcel_hmac_init_pad(struct ahash_request *areq, 8061b44c5a6SAntoine Ténart unsigned int blocksize, const u8 *key, 8071b44c5a6SAntoine Ténart unsigned int keylen, u8 *ipad, u8 *opad) 8081b44c5a6SAntoine Ténart { 8091b44c5a6SAntoine Ténart struct safexcel_ahash_result result; 8101b44c5a6SAntoine Ténart struct scatterlist sg; 8111b44c5a6SAntoine Ténart int ret, i; 8121b44c5a6SAntoine Ténart u8 *keydup; 8131b44c5a6SAntoine Ténart 8141b44c5a6SAntoine Ténart if (keylen <= blocksize) { 8151b44c5a6SAntoine Ténart memcpy(ipad, key, keylen); 8161b44c5a6SAntoine Ténart } else { 8171b44c5a6SAntoine Ténart keydup = kmemdup(key, keylen, GFP_KERNEL); 8181b44c5a6SAntoine Ténart if (!keydup) 8191b44c5a6SAntoine Ténart return -ENOMEM; 8201b44c5a6SAntoine Ténart 8211b44c5a6SAntoine Ténart ahash_request_set_callback(areq, CRYPTO_TFM_REQ_MAY_BACKLOG, 8221b44c5a6SAntoine Ténart safexcel_ahash_complete, &result); 8231b44c5a6SAntoine Ténart sg_init_one(&sg, keydup, keylen); 8241b44c5a6SAntoine Ténart ahash_request_set_crypt(areq, &sg, ipad, keylen); 8251b44c5a6SAntoine Ténart init_completion(&result.completion); 8261b44c5a6SAntoine Ténart 8271b44c5a6SAntoine Ténart ret = crypto_ahash_digest(areq); 8281b44c5a6SAntoine Ténart if (ret == -EINPROGRESS) { 8291b44c5a6SAntoine Ténart wait_for_completion_interruptible(&result.completion); 8301b44c5a6SAntoine Ténart ret = result.error; 8311b44c5a6SAntoine Ténart } 8321b44c5a6SAntoine Ténart 8331b44c5a6SAntoine Ténart /* Avoid leaking */ 8341b44c5a6SAntoine Ténart memzero_explicit(keydup, keylen); 8351b44c5a6SAntoine Ténart kfree(keydup); 8361b44c5a6SAntoine Ténart 8371b44c5a6SAntoine Ténart if (ret) 8381b44c5a6SAntoine Ténart return ret; 8391b44c5a6SAntoine Ténart 8401b44c5a6SAntoine Ténart keylen = crypto_ahash_digestsize(crypto_ahash_reqtfm(areq)); 8411b44c5a6SAntoine Ténart } 8421b44c5a6SAntoine Ténart 8431b44c5a6SAntoine Ténart memset(ipad + keylen, 0, blocksize - keylen); 8441b44c5a6SAntoine Ténart memcpy(opad, ipad, blocksize); 8451b44c5a6SAntoine Ténart 8461b44c5a6SAntoine Ténart for (i = 0; i < blocksize; i++) { 847aed3731eSAntoine Ténart ipad[i] ^= HMAC_IPAD_VALUE; 848aed3731eSAntoine Ténart opad[i] ^= HMAC_OPAD_VALUE; 8491b44c5a6SAntoine Ténart } 8501b44c5a6SAntoine Ténart 8511b44c5a6SAntoine Ténart return 0; 8521b44c5a6SAntoine Ténart } 8531b44c5a6SAntoine Ténart 8541b44c5a6SAntoine Ténart static int safexcel_hmac_init_iv(struct ahash_request *areq, 8551b44c5a6SAntoine Ténart unsigned int blocksize, u8 *pad, void *state) 8561b44c5a6SAntoine Ténart { 8571b44c5a6SAntoine Ténart struct safexcel_ahash_result result; 8581b44c5a6SAntoine Ténart struct safexcel_ahash_req *req; 8591b44c5a6SAntoine Ténart struct scatterlist sg; 8601b44c5a6SAntoine Ténart int ret; 8611b44c5a6SAntoine Ténart 8621b44c5a6SAntoine Ténart ahash_request_set_callback(areq, CRYPTO_TFM_REQ_MAY_BACKLOG, 8631b44c5a6SAntoine Ténart safexcel_ahash_complete, &result); 8641b44c5a6SAntoine Ténart sg_init_one(&sg, pad, blocksize); 8651b44c5a6SAntoine Ténart ahash_request_set_crypt(areq, &sg, pad, blocksize); 8661b44c5a6SAntoine Ténart init_completion(&result.completion); 8671b44c5a6SAntoine Ténart 8681b44c5a6SAntoine Ténart ret = crypto_ahash_init(areq); 8691b44c5a6SAntoine Ténart if (ret) 8701b44c5a6SAntoine Ténart return ret; 8711b44c5a6SAntoine Ténart 8721b44c5a6SAntoine Ténart req = ahash_request_ctx(areq); 8731b44c5a6SAntoine Ténart req->hmac = true; 8741b44c5a6SAntoine Ténart req->last_req = true; 8751b44c5a6SAntoine Ténart 8761b44c5a6SAntoine Ténart ret = crypto_ahash_update(areq); 87712bf4142SOfer Heifetz if (ret && ret != -EINPROGRESS && ret != -EBUSY) 8781b44c5a6SAntoine Ténart return ret; 8791b44c5a6SAntoine Ténart 8801b44c5a6SAntoine Ténart wait_for_completion_interruptible(&result.completion); 8811b44c5a6SAntoine Ténart if (result.error) 8821b44c5a6SAntoine Ténart return result.error; 8831b44c5a6SAntoine Ténart 8841b44c5a6SAntoine Ténart return crypto_ahash_export(areq, state); 8851b44c5a6SAntoine Ténart } 8861b44c5a6SAntoine Ténart 8871b44c5a6SAntoine Ténart static int safexcel_hmac_setkey(const char *alg, const u8 *key, 8881b44c5a6SAntoine Ténart unsigned int keylen, void *istate, void *ostate) 8891b44c5a6SAntoine Ténart { 8901b44c5a6SAntoine Ténart struct ahash_request *areq; 8911b44c5a6SAntoine Ténart struct crypto_ahash *tfm; 8921b44c5a6SAntoine Ténart unsigned int blocksize; 8931b44c5a6SAntoine Ténart u8 *ipad, *opad; 8941b44c5a6SAntoine Ténart int ret; 8951b44c5a6SAntoine Ténart 8961b44c5a6SAntoine Ténart tfm = crypto_alloc_ahash(alg, CRYPTO_ALG_TYPE_AHASH, 8971b44c5a6SAntoine Ténart CRYPTO_ALG_TYPE_AHASH_MASK); 8981b44c5a6SAntoine Ténart if (IS_ERR(tfm)) 8991b44c5a6SAntoine Ténart return PTR_ERR(tfm); 9001b44c5a6SAntoine Ténart 9011b44c5a6SAntoine Ténart areq = ahash_request_alloc(tfm, GFP_KERNEL); 9021b44c5a6SAntoine Ténart if (!areq) { 9031b44c5a6SAntoine Ténart ret = -ENOMEM; 9041b44c5a6SAntoine Ténart goto free_ahash; 9051b44c5a6SAntoine Ténart } 9061b44c5a6SAntoine Ténart 9071b44c5a6SAntoine Ténart crypto_ahash_clear_flags(tfm, ~0); 9081b44c5a6SAntoine Ténart blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm)); 9091b44c5a6SAntoine Ténart 9101b44c5a6SAntoine Ténart ipad = kzalloc(2 * blocksize, GFP_KERNEL); 9111b44c5a6SAntoine Ténart if (!ipad) { 9121b44c5a6SAntoine Ténart ret = -ENOMEM; 9131b44c5a6SAntoine Ténart goto free_request; 9141b44c5a6SAntoine Ténart } 9151b44c5a6SAntoine Ténart 9161b44c5a6SAntoine Ténart opad = ipad + blocksize; 9171b44c5a6SAntoine Ténart 9181b44c5a6SAntoine Ténart ret = safexcel_hmac_init_pad(areq, blocksize, key, keylen, ipad, opad); 9191b44c5a6SAntoine Ténart if (ret) 9201b44c5a6SAntoine Ténart goto free_ipad; 9211b44c5a6SAntoine Ténart 9221b44c5a6SAntoine Ténart ret = safexcel_hmac_init_iv(areq, blocksize, ipad, istate); 9231b44c5a6SAntoine Ténart if (ret) 9241b44c5a6SAntoine Ténart goto free_ipad; 9251b44c5a6SAntoine Ténart 9261b44c5a6SAntoine Ténart ret = safexcel_hmac_init_iv(areq, blocksize, opad, ostate); 9271b44c5a6SAntoine Ténart 9281b44c5a6SAntoine Ténart free_ipad: 9291b44c5a6SAntoine Ténart kfree(ipad); 9301b44c5a6SAntoine Ténart free_request: 9311b44c5a6SAntoine Ténart ahash_request_free(areq); 9321b44c5a6SAntoine Ténart free_ahash: 9331b44c5a6SAntoine Ténart crypto_free_ahash(tfm); 9341b44c5a6SAntoine Ténart 9351b44c5a6SAntoine Ténart return ret; 9361b44c5a6SAntoine Ténart } 9371b44c5a6SAntoine Ténart 9381b44c5a6SAntoine Ténart static int safexcel_hmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key, 9391b44c5a6SAntoine Ténart unsigned int keylen) 9401b44c5a6SAntoine Ténart { 9411b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm)); 942871df319SAntoine Ténart struct safexcel_crypto_priv *priv = ctx->priv; 9431b44c5a6SAntoine Ténart struct safexcel_ahash_export_state istate, ostate; 9441b44c5a6SAntoine Ténart int ret, i; 9451b44c5a6SAntoine Ténart 9461b44c5a6SAntoine Ténart ret = safexcel_hmac_setkey("safexcel-sha1", key, keylen, &istate, &ostate); 9471b44c5a6SAntoine Ténart if (ret) 9481b44c5a6SAntoine Ténart return ret; 9491b44c5a6SAntoine Ténart 950871df319SAntoine Ténart if (priv->version == EIP197 && ctx->base.ctxr) { 95160c4081eSAntoine Ténart for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) { 9521b44c5a6SAntoine Ténart if (ctx->ipad[i] != le32_to_cpu(istate.state[i]) || 9531b44c5a6SAntoine Ténart ctx->opad[i] != le32_to_cpu(ostate.state[i])) { 9541b44c5a6SAntoine Ténart ctx->base.needs_inv = true; 9551b44c5a6SAntoine Ténart break; 9561b44c5a6SAntoine Ténart } 9571b44c5a6SAntoine Ténart } 958c4daf4ccSOfer Heifetz } 9591b44c5a6SAntoine Ténart 96042ef3bedSAntoine Ténart memcpy(ctx->ipad, &istate.state, SHA1_DIGEST_SIZE); 96142ef3bedSAntoine Ténart memcpy(ctx->opad, &ostate.state, SHA1_DIGEST_SIZE); 96242ef3bedSAntoine Ténart 9631b44c5a6SAntoine Ténart return 0; 9641b44c5a6SAntoine Ténart } 9651b44c5a6SAntoine Ténart 9661b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_hmac_sha1 = { 9671b44c5a6SAntoine Ténart .type = SAFEXCEL_ALG_TYPE_AHASH, 9681b44c5a6SAntoine Ténart .alg.ahash = { 9691b44c5a6SAntoine Ténart .init = safexcel_hmac_sha1_init, 9701b44c5a6SAntoine Ténart .update = safexcel_ahash_update, 9711b44c5a6SAntoine Ténart .final = safexcel_ahash_final, 9721b44c5a6SAntoine Ténart .finup = safexcel_ahash_finup, 9731b44c5a6SAntoine Ténart .digest = safexcel_hmac_sha1_digest, 9741b44c5a6SAntoine Ténart .setkey = safexcel_hmac_sha1_setkey, 9751b44c5a6SAntoine Ténart .export = safexcel_ahash_export, 9761b44c5a6SAntoine Ténart .import = safexcel_ahash_import, 9771b44c5a6SAntoine Ténart .halg = { 9781b44c5a6SAntoine Ténart .digestsize = SHA1_DIGEST_SIZE, 9791b44c5a6SAntoine Ténart .statesize = sizeof(struct safexcel_ahash_export_state), 9801b44c5a6SAntoine Ténart .base = { 9811b44c5a6SAntoine Ténart .cra_name = "hmac(sha1)", 9821b44c5a6SAntoine Ténart .cra_driver_name = "safexcel-hmac-sha1", 9831b44c5a6SAntoine Ténart .cra_priority = 300, 9841b44c5a6SAntoine Ténart .cra_flags = CRYPTO_ALG_ASYNC | 9851b44c5a6SAntoine Ténart CRYPTO_ALG_KERN_DRIVER_ONLY, 9861b44c5a6SAntoine Ténart .cra_blocksize = SHA1_BLOCK_SIZE, 9871b44c5a6SAntoine Ténart .cra_ctxsize = sizeof(struct safexcel_ahash_ctx), 9881b44c5a6SAntoine Ténart .cra_init = safexcel_ahash_cra_init, 9891b44c5a6SAntoine Ténart .cra_exit = safexcel_ahash_cra_exit, 9901b44c5a6SAntoine Ténart .cra_module = THIS_MODULE, 9911b44c5a6SAntoine Ténart }, 9921b44c5a6SAntoine Ténart }, 9931b44c5a6SAntoine Ténart }, 9941b44c5a6SAntoine Ténart }; 9951b44c5a6SAntoine Ténart 9961b44c5a6SAntoine Ténart static int safexcel_sha256_init(struct ahash_request *areq) 9971b44c5a6SAntoine Ténart { 9981b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 9991b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 10001b44c5a6SAntoine Ténart 10011b44c5a6SAntoine Ténart memset(req, 0, sizeof(*req)); 10021b44c5a6SAntoine Ténart 10031b44c5a6SAntoine Ténart req->state[0] = SHA256_H0; 10041b44c5a6SAntoine Ténart req->state[1] = SHA256_H1; 10051b44c5a6SAntoine Ténart req->state[2] = SHA256_H2; 10061b44c5a6SAntoine Ténart req->state[3] = SHA256_H3; 10071b44c5a6SAntoine Ténart req->state[4] = SHA256_H4; 10081b44c5a6SAntoine Ténart req->state[5] = SHA256_H5; 10091b44c5a6SAntoine Ténart req->state[6] = SHA256_H6; 10101b44c5a6SAntoine Ténart req->state[7] = SHA256_H7; 10111b44c5a6SAntoine Ténart 10121b44c5a6SAntoine Ténart ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA256; 10131b44c5a6SAntoine Ténart ctx->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED; 10141b44c5a6SAntoine Ténart req->state_sz = SHA256_DIGEST_SIZE; 10151b44c5a6SAntoine Ténart 10161b44c5a6SAntoine Ténart return 0; 10171b44c5a6SAntoine Ténart } 10181b44c5a6SAntoine Ténart 10191b44c5a6SAntoine Ténart static int safexcel_sha256_digest(struct ahash_request *areq) 10201b44c5a6SAntoine Ténart { 10211b44c5a6SAntoine Ténart int ret = safexcel_sha256_init(areq); 10221b44c5a6SAntoine Ténart 10231b44c5a6SAntoine Ténart if (ret) 10241b44c5a6SAntoine Ténart return ret; 10251b44c5a6SAntoine Ténart 10261b44c5a6SAntoine Ténart return safexcel_ahash_finup(areq); 10271b44c5a6SAntoine Ténart } 10281b44c5a6SAntoine Ténart 10291b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_sha256 = { 10301b44c5a6SAntoine Ténart .type = SAFEXCEL_ALG_TYPE_AHASH, 10311b44c5a6SAntoine Ténart .alg.ahash = { 10321b44c5a6SAntoine Ténart .init = safexcel_sha256_init, 10331b44c5a6SAntoine Ténart .update = safexcel_ahash_update, 10341b44c5a6SAntoine Ténart .final = safexcel_ahash_final, 10351b44c5a6SAntoine Ténart .finup = safexcel_ahash_finup, 10361b44c5a6SAntoine Ténart .digest = safexcel_sha256_digest, 10371b44c5a6SAntoine Ténart .export = safexcel_ahash_export, 10381b44c5a6SAntoine Ténart .import = safexcel_ahash_import, 10391b44c5a6SAntoine Ténart .halg = { 10401b44c5a6SAntoine Ténart .digestsize = SHA256_DIGEST_SIZE, 10411b44c5a6SAntoine Ténart .statesize = sizeof(struct safexcel_ahash_export_state), 10421b44c5a6SAntoine Ténart .base = { 10431b44c5a6SAntoine Ténart .cra_name = "sha256", 10441b44c5a6SAntoine Ténart .cra_driver_name = "safexcel-sha256", 10451b44c5a6SAntoine Ténart .cra_priority = 300, 10461b44c5a6SAntoine Ténart .cra_flags = CRYPTO_ALG_ASYNC | 10471b44c5a6SAntoine Ténart CRYPTO_ALG_KERN_DRIVER_ONLY, 10481b44c5a6SAntoine Ténart .cra_blocksize = SHA256_BLOCK_SIZE, 10491b44c5a6SAntoine Ténart .cra_ctxsize = sizeof(struct safexcel_ahash_ctx), 10501b44c5a6SAntoine Ténart .cra_init = safexcel_ahash_cra_init, 10511b44c5a6SAntoine Ténart .cra_exit = safexcel_ahash_cra_exit, 10521b44c5a6SAntoine Ténart .cra_module = THIS_MODULE, 10531b44c5a6SAntoine Ténart }, 10541b44c5a6SAntoine Ténart }, 10551b44c5a6SAntoine Ténart }, 10561b44c5a6SAntoine Ténart }; 10571b44c5a6SAntoine Ténart 10581b44c5a6SAntoine Ténart static int safexcel_sha224_init(struct ahash_request *areq) 10591b44c5a6SAntoine Ténart { 10601b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 10611b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 10621b44c5a6SAntoine Ténart 10631b44c5a6SAntoine Ténart memset(req, 0, sizeof(*req)); 10641b44c5a6SAntoine Ténart 10651b44c5a6SAntoine Ténart req->state[0] = SHA224_H0; 10661b44c5a6SAntoine Ténart req->state[1] = SHA224_H1; 10671b44c5a6SAntoine Ténart req->state[2] = SHA224_H2; 10681b44c5a6SAntoine Ténart req->state[3] = SHA224_H3; 10691b44c5a6SAntoine Ténart req->state[4] = SHA224_H4; 10701b44c5a6SAntoine Ténart req->state[5] = SHA224_H5; 10711b44c5a6SAntoine Ténart req->state[6] = SHA224_H6; 10721b44c5a6SAntoine Ténart req->state[7] = SHA224_H7; 10731b44c5a6SAntoine Ténart 10741b44c5a6SAntoine Ténart ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA224; 10751b44c5a6SAntoine Ténart ctx->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED; 10761b44c5a6SAntoine Ténart req->state_sz = SHA256_DIGEST_SIZE; 10771b44c5a6SAntoine Ténart 10781b44c5a6SAntoine Ténart return 0; 10791b44c5a6SAntoine Ténart } 10801b44c5a6SAntoine Ténart 10811b44c5a6SAntoine Ténart static int safexcel_sha224_digest(struct ahash_request *areq) 10821b44c5a6SAntoine Ténart { 10831b44c5a6SAntoine Ténart int ret = safexcel_sha224_init(areq); 10841b44c5a6SAntoine Ténart 10851b44c5a6SAntoine Ténart if (ret) 10861b44c5a6SAntoine Ténart return ret; 10871b44c5a6SAntoine Ténart 10881b44c5a6SAntoine Ténart return safexcel_ahash_finup(areq); 10891b44c5a6SAntoine Ténart } 10901b44c5a6SAntoine Ténart 10911b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_sha224 = { 10921b44c5a6SAntoine Ténart .type = SAFEXCEL_ALG_TYPE_AHASH, 10931b44c5a6SAntoine Ténart .alg.ahash = { 10941b44c5a6SAntoine Ténart .init = safexcel_sha224_init, 10951b44c5a6SAntoine Ténart .update = safexcel_ahash_update, 10961b44c5a6SAntoine Ténart .final = safexcel_ahash_final, 10971b44c5a6SAntoine Ténart .finup = safexcel_ahash_finup, 10981b44c5a6SAntoine Ténart .digest = safexcel_sha224_digest, 10991b44c5a6SAntoine Ténart .export = safexcel_ahash_export, 11001b44c5a6SAntoine Ténart .import = safexcel_ahash_import, 11011b44c5a6SAntoine Ténart .halg = { 11021b44c5a6SAntoine Ténart .digestsize = SHA224_DIGEST_SIZE, 11031b44c5a6SAntoine Ténart .statesize = sizeof(struct safexcel_ahash_export_state), 11041b44c5a6SAntoine Ténart .base = { 11051b44c5a6SAntoine Ténart .cra_name = "sha224", 11061b44c5a6SAntoine Ténart .cra_driver_name = "safexcel-sha224", 11071b44c5a6SAntoine Ténart .cra_priority = 300, 11081b44c5a6SAntoine Ténart .cra_flags = CRYPTO_ALG_ASYNC | 11091b44c5a6SAntoine Ténart CRYPTO_ALG_KERN_DRIVER_ONLY, 11101b44c5a6SAntoine Ténart .cra_blocksize = SHA224_BLOCK_SIZE, 11111b44c5a6SAntoine Ténart .cra_ctxsize = sizeof(struct safexcel_ahash_ctx), 11121b44c5a6SAntoine Ténart .cra_init = safexcel_ahash_cra_init, 11131b44c5a6SAntoine Ténart .cra_exit = safexcel_ahash_cra_exit, 11141b44c5a6SAntoine Ténart .cra_module = THIS_MODULE, 11151b44c5a6SAntoine Ténart }, 11161b44c5a6SAntoine Ténart }, 11171b44c5a6SAntoine Ténart }, 11181b44c5a6SAntoine Ténart }; 1119