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; 187666a9c70SAntoine Tenart 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 192809778e0SAntoine Ténart if (!req->last_req) { 193809778e0SAntoine Ténart /* If this is not the last request and the queued data does not 194809778e0SAntoine Ténart * fit into full blocks, cache it for the next send() call. 1951b44c5a6SAntoine Ténart */ 1961b44c5a6SAntoine Ténart extra = queued & (crypto_ahash_blocksize(ahash) - 1); 197809778e0SAntoine Ténart if (!extra) 198809778e0SAntoine Ténart /* If this is not the last request and the queued data 199809778e0SAntoine Ténart * is a multiple of a block, cache the last one for now. 200809778e0SAntoine Ténart */ 201c1a8fa6eSAntoine Tenart extra = crypto_ahash_blocksize(ahash); 202809778e0SAntoine Ténart 203809778e0SAntoine Ténart if (extra) { 2041b44c5a6SAntoine Ténart sg_pcopy_to_buffer(areq->src, sg_nents(areq->src), 205809778e0SAntoine Ténart req->cache_next, extra, 206809778e0SAntoine Ténart areq->nbytes - extra); 2071b44c5a6SAntoine Ténart 2081b44c5a6SAntoine Ténart queued -= extra; 2091b44c5a6SAntoine Ténart len -= extra; 210809778e0SAntoine Ténart 211809778e0SAntoine Ténart if (!queued) { 212809778e0SAntoine Ténart *commands = 0; 213809778e0SAntoine Ténart *results = 0; 214809778e0SAntoine Ténart return 0; 215809778e0SAntoine Ténart } 216809778e0SAntoine Ténart } 2171b44c5a6SAntoine Ténart } 2181b44c5a6SAntoine Ténart 2191b44c5a6SAntoine Ténart spin_lock_bh(&priv->ring[ring].egress_lock); 2201b44c5a6SAntoine Ténart 2211b44c5a6SAntoine Ténart /* Add a command descriptor for the cached data, if any */ 2221b44c5a6SAntoine Ténart if (cache_len) { 2231b44c5a6SAntoine Ténart ctx->base.cache = kzalloc(cache_len, EIP197_GFP_FLAGS(*async)); 2241b44c5a6SAntoine Ténart if (!ctx->base.cache) { 2251b44c5a6SAntoine Ténart ret = -ENOMEM; 2261b44c5a6SAntoine Ténart goto unlock; 2271b44c5a6SAntoine Ténart } 2281b44c5a6SAntoine Ténart memcpy(ctx->base.cache, req->cache, cache_len); 2291b44c5a6SAntoine Ténart ctx->base.cache_dma = dma_map_single(priv->dev, ctx->base.cache, 2301b44c5a6SAntoine Ténart cache_len, DMA_TO_DEVICE); 2311b44c5a6SAntoine Ténart if (dma_mapping_error(priv->dev, ctx->base.cache_dma)) { 2321b44c5a6SAntoine Ténart ret = -EINVAL; 2331b44c5a6SAntoine Ténart goto free_cache; 2341b44c5a6SAntoine Ténart } 2351b44c5a6SAntoine Ténart 2361b44c5a6SAntoine Ténart ctx->base.cache_sz = cache_len; 2371b44c5a6SAntoine Ténart first_cdesc = safexcel_add_cdesc(priv, ring, 1, 2381b44c5a6SAntoine Ténart (cache_len == len), 2391b44c5a6SAntoine Ténart ctx->base.cache_dma, 2401b44c5a6SAntoine Ténart cache_len, len, 2411b44c5a6SAntoine Ténart ctx->base.ctxr_dma); 2421b44c5a6SAntoine Ténart if (IS_ERR(first_cdesc)) { 2431b44c5a6SAntoine Ténart ret = PTR_ERR(first_cdesc); 2441b44c5a6SAntoine Ténart goto unmap_cache; 2451b44c5a6SAntoine Ténart } 2461b44c5a6SAntoine Ténart n_cdesc++; 2471b44c5a6SAntoine Ténart 2481b44c5a6SAntoine Ténart queued -= cache_len; 2491b44c5a6SAntoine Ténart if (!queued) 2501b44c5a6SAntoine Ténart goto send_command; 2511b44c5a6SAntoine Ténart } 2521b44c5a6SAntoine Ténart 2531b44c5a6SAntoine Ténart /* Now handle the current ahash request buffer(s) */ 254c957f8b3SAntoine Ténart req->nents = dma_map_sg(priv->dev, areq->src, 2551b44c5a6SAntoine Ténart sg_nents_for_len(areq->src, areq->nbytes), 2561b44c5a6SAntoine Ténart DMA_TO_DEVICE); 257c957f8b3SAntoine Ténart if (!req->nents) { 2581b44c5a6SAntoine Ténart ret = -ENOMEM; 2591b44c5a6SAntoine Ténart goto cdesc_rollback; 2601b44c5a6SAntoine Ténart } 2611b44c5a6SAntoine Ténart 262c957f8b3SAntoine Ténart for_each_sg(areq->src, sg, req->nents, i) { 2631b44c5a6SAntoine Ténart int sglen = sg_dma_len(sg); 2641b44c5a6SAntoine Ténart 2651b44c5a6SAntoine Ténart /* Do not overflow the request */ 2661b44c5a6SAntoine Ténart if (queued - sglen < 0) 2671b44c5a6SAntoine Ténart sglen = queued; 2681b44c5a6SAntoine Ténart 2691b44c5a6SAntoine Ténart cdesc = safexcel_add_cdesc(priv, ring, !n_cdesc, 2701b44c5a6SAntoine Ténart !(queued - sglen), sg_dma_address(sg), 2711b44c5a6SAntoine Ténart sglen, len, ctx->base.ctxr_dma); 2721b44c5a6SAntoine Ténart if (IS_ERR(cdesc)) { 2731b44c5a6SAntoine Ténart ret = PTR_ERR(cdesc); 2741b44c5a6SAntoine Ténart goto cdesc_rollback; 2751b44c5a6SAntoine Ténart } 2761b44c5a6SAntoine Ténart n_cdesc++; 2771b44c5a6SAntoine Ténart 2781b44c5a6SAntoine Ténart if (n_cdesc == 1) 2791b44c5a6SAntoine Ténart first_cdesc = cdesc; 2801b44c5a6SAntoine Ténart 2811b44c5a6SAntoine Ténart queued -= sglen; 2821b44c5a6SAntoine Ténart if (!queued) 2831b44c5a6SAntoine Ténart break; 2841b44c5a6SAntoine Ténart } 2851b44c5a6SAntoine Ténart 2861b44c5a6SAntoine Ténart send_command: 2871b44c5a6SAntoine Ténart /* Setup the context options */ 2881b44c5a6SAntoine Ténart safexcel_context_control(ctx, req, first_cdesc, req->state_sz, 2891b44c5a6SAntoine Ténart crypto_ahash_blocksize(ahash)); 2901b44c5a6SAntoine Ténart 2911b44c5a6SAntoine Ténart /* Add the token */ 2921b44c5a6SAntoine Ténart safexcel_hash_token(first_cdesc, len, req->state_sz); 2931b44c5a6SAntoine Ténart 2942973633eSAntoine Ténart ctx->base.result_dma = dma_map_single(priv->dev, req->state, 2951b44c5a6SAntoine Ténart req->state_sz, DMA_FROM_DEVICE); 2961b44c5a6SAntoine Ténart if (dma_mapping_error(priv->dev, ctx->base.result_dma)) { 2971b44c5a6SAntoine Ténart ret = -EINVAL; 2981b44c5a6SAntoine Ténart goto cdesc_rollback; 2991b44c5a6SAntoine Ténart } 3001b44c5a6SAntoine Ténart 3011b44c5a6SAntoine Ténart /* Add a result descriptor */ 3021b44c5a6SAntoine Ténart rdesc = safexcel_add_rdesc(priv, ring, 1, 1, ctx->base.result_dma, 3031b44c5a6SAntoine Ténart req->state_sz); 3041b44c5a6SAntoine Ténart if (IS_ERR(rdesc)) { 3051b44c5a6SAntoine Ténart ret = PTR_ERR(rdesc); 3061b44c5a6SAntoine Ténart goto cdesc_rollback; 3071b44c5a6SAntoine Ténart } 3081b44c5a6SAntoine Ténart 3091b44c5a6SAntoine Ténart spin_unlock_bh(&priv->ring[ring].egress_lock); 3101b44c5a6SAntoine Ténart 31197858434SAntoine Ténart req->processed += len; 31297858434SAntoine Ténart request->req = &areq->base; 31397858434SAntoine Ténart 3141b44c5a6SAntoine Ténart *commands = n_cdesc; 3151b44c5a6SAntoine Ténart *results = 1; 3161b44c5a6SAntoine Ténart return 0; 3171b44c5a6SAntoine Ténart 3181b44c5a6SAntoine Ténart cdesc_rollback: 3191b44c5a6SAntoine Ténart for (i = 0; i < n_cdesc; i++) 3201b44c5a6SAntoine Ténart safexcel_ring_rollback_wptr(priv, &priv->ring[ring].cdr); 3211b44c5a6SAntoine Ténart unmap_cache: 3221b44c5a6SAntoine Ténart if (ctx->base.cache_dma) { 3231b44c5a6SAntoine Ténart dma_unmap_single(priv->dev, ctx->base.cache_dma, 3241b44c5a6SAntoine Ténart ctx->base.cache_sz, DMA_TO_DEVICE); 3251b44c5a6SAntoine Ténart ctx->base.cache_sz = 0; 3261b44c5a6SAntoine Ténart } 3271b44c5a6SAntoine Ténart free_cache: 3281b44c5a6SAntoine Ténart kfree(ctx->base.cache); 3291b44c5a6SAntoine Ténart ctx->base.cache = NULL; 3301b44c5a6SAntoine Ténart 3311b44c5a6SAntoine Ténart unlock: 3321b44c5a6SAntoine Ténart spin_unlock_bh(&priv->ring[ring].egress_lock); 3331b44c5a6SAntoine Ténart return ret; 3341b44c5a6SAntoine Ténart } 3351b44c5a6SAntoine Ténart 3361b44c5a6SAntoine Ténart static inline bool safexcel_ahash_needs_inv_get(struct ahash_request *areq) 3371b44c5a6SAntoine Ténart { 3381b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 3391b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 3401b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 3411b44c5a6SAntoine Ténart unsigned int state_w_sz = req->state_sz / sizeof(u32); 3421b44c5a6SAntoine Ténart int i; 3431b44c5a6SAntoine Ténart 3441b44c5a6SAntoine Ténart for (i = 0; i < state_w_sz; i++) 3451b44c5a6SAntoine Ténart if (ctx->base.ctxr->data[i] != cpu_to_le32(req->state[i])) 3461b44c5a6SAntoine Ténart return true; 3471b44c5a6SAntoine Ténart 3481b44c5a6SAntoine Ténart if (ctx->base.ctxr->data[state_w_sz] != 3491b44c5a6SAntoine Ténart cpu_to_le32(req->processed / crypto_ahash_blocksize(ahash))) 3501b44c5a6SAntoine Ténart return true; 3511b44c5a6SAntoine Ténart 3521b44c5a6SAntoine Ténart return false; 3531b44c5a6SAntoine Ténart } 3541b44c5a6SAntoine Ténart 3551b44c5a6SAntoine Ténart static int safexcel_handle_inv_result(struct safexcel_crypto_priv *priv, 3561b44c5a6SAntoine Ténart int ring, 3571b44c5a6SAntoine Ténart struct crypto_async_request *async, 3581b44c5a6SAntoine Ténart bool *should_complete, int *ret) 3591b44c5a6SAntoine Ténart { 3601b44c5a6SAntoine Ténart struct safexcel_result_desc *rdesc; 3611b44c5a6SAntoine Ténart struct ahash_request *areq = ahash_request_cast(async); 3621b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 3631b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(ahash); 3641b44c5a6SAntoine Ténart int enq_ret; 3651b44c5a6SAntoine Ténart 3661b44c5a6SAntoine Ténart *ret = 0; 3671b44c5a6SAntoine Ténart 3681b44c5a6SAntoine Ténart spin_lock_bh(&priv->ring[ring].egress_lock); 3691b44c5a6SAntoine Ténart rdesc = safexcel_ring_next_rptr(priv, &priv->ring[ring].rdr); 3701b44c5a6SAntoine Ténart if (IS_ERR(rdesc)) { 3711b44c5a6SAntoine Ténart dev_err(priv->dev, 3721b44c5a6SAntoine Ténart "hash: invalidate: could not retrieve the result descriptor\n"); 3731b44c5a6SAntoine Ténart *ret = PTR_ERR(rdesc); 3741b44c5a6SAntoine Ténart } else if (rdesc->result_data.error_code) { 3751b44c5a6SAntoine Ténart dev_err(priv->dev, 3761b44c5a6SAntoine Ténart "hash: invalidate: result descriptor error (%d)\n", 3771b44c5a6SAntoine Ténart rdesc->result_data.error_code); 3781b44c5a6SAntoine Ténart *ret = -EINVAL; 3791b44c5a6SAntoine Ténart } 3801b44c5a6SAntoine Ténart 3811b44c5a6SAntoine Ténart safexcel_complete(priv, ring); 3821b44c5a6SAntoine Ténart spin_unlock_bh(&priv->ring[ring].egress_lock); 3831b44c5a6SAntoine Ténart 3841b44c5a6SAntoine Ténart if (ctx->base.exit_inv) { 3851b44c5a6SAntoine Ténart dma_pool_free(priv->context_pool, ctx->base.ctxr, 3861b44c5a6SAntoine Ténart ctx->base.ctxr_dma); 3871b44c5a6SAntoine Ténart 3881b44c5a6SAntoine Ténart *should_complete = true; 3891b44c5a6SAntoine Ténart return 1; 3901b44c5a6SAntoine Ténart } 3911b44c5a6SAntoine Ténart 39286671abbSAntoine Ténart ring = safexcel_select_ring(priv); 39386671abbSAntoine Ténart ctx->base.ring = ring; 3941b44c5a6SAntoine Ténart 39586671abbSAntoine Ténart spin_lock_bh(&priv->ring[ring].queue_lock); 39686671abbSAntoine Ténart enq_ret = crypto_enqueue_request(&priv->ring[ring].queue, async); 39786671abbSAntoine Ténart spin_unlock_bh(&priv->ring[ring].queue_lock); 3981b44c5a6SAntoine Ténart 3991b44c5a6SAntoine Ténart if (enq_ret != -EINPROGRESS) 4001b44c5a6SAntoine Ténart *ret = enq_ret; 4011b44c5a6SAntoine Ténart 4028472e778SAntoine Ténart queue_work(priv->ring[ring].workqueue, 4038472e778SAntoine Ténart &priv->ring[ring].work_data.work); 40486671abbSAntoine Ténart 4051b44c5a6SAntoine Ténart *should_complete = false; 4061b44c5a6SAntoine Ténart 4071b44c5a6SAntoine Ténart return 1; 4081b44c5a6SAntoine Ténart } 4091b44c5a6SAntoine Ténart 4101eb7b403SOfer Heifetz static int safexcel_handle_result(struct safexcel_crypto_priv *priv, int ring, 4111eb7b403SOfer Heifetz struct crypto_async_request *async, 4121eb7b403SOfer Heifetz bool *should_complete, int *ret) 4131eb7b403SOfer Heifetz { 4141eb7b403SOfer Heifetz struct ahash_request *areq = ahash_request_cast(async); 4151eb7b403SOfer Heifetz struct safexcel_ahash_req *req = ahash_request_ctx(areq); 4161eb7b403SOfer Heifetz int err; 4171eb7b403SOfer Heifetz 418871df319SAntoine Ténart BUG_ON(priv->version == EIP97 && req->needs_inv); 419871df319SAntoine Ténart 4201eb7b403SOfer Heifetz if (req->needs_inv) { 4211eb7b403SOfer Heifetz req->needs_inv = false; 4221eb7b403SOfer Heifetz err = safexcel_handle_inv_result(priv, ring, async, 4231eb7b403SOfer Heifetz should_complete, ret); 4241eb7b403SOfer Heifetz } else { 4251eb7b403SOfer Heifetz err = safexcel_handle_req_result(priv, ring, async, 4261eb7b403SOfer Heifetz should_complete, ret); 4271eb7b403SOfer Heifetz } 4281eb7b403SOfer Heifetz 4291eb7b403SOfer Heifetz return err; 4301eb7b403SOfer Heifetz } 4311eb7b403SOfer Heifetz 4321b44c5a6SAntoine Ténart static int safexcel_ahash_send_inv(struct crypto_async_request *async, 4331b44c5a6SAntoine Ténart int ring, struct safexcel_request *request, 4341b44c5a6SAntoine Ténart int *commands, int *results) 4351b44c5a6SAntoine Ténart { 4361b44c5a6SAntoine Ténart struct ahash_request *areq = ahash_request_cast(async); 4371b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 4381b44c5a6SAntoine Ténart int ret; 4391b44c5a6SAntoine Ténart 4405290ad6eSOfer Heifetz ret = safexcel_invalidate_cache(async, ctx->priv, 4411b44c5a6SAntoine Ténart ctx->base.ctxr_dma, ring, request); 4421b44c5a6SAntoine Ténart if (unlikely(ret)) 4431b44c5a6SAntoine Ténart return ret; 4441b44c5a6SAntoine Ténart 4451b44c5a6SAntoine Ténart *commands = 1; 4461b44c5a6SAntoine Ténart *results = 1; 4471b44c5a6SAntoine Ténart 4481b44c5a6SAntoine Ténart return 0; 4491b44c5a6SAntoine Ténart } 4501b44c5a6SAntoine Ténart 4511eb7b403SOfer Heifetz static int safexcel_ahash_send(struct crypto_async_request *async, 4521eb7b403SOfer Heifetz int ring, struct safexcel_request *request, 4531eb7b403SOfer Heifetz int *commands, int *results) 4541eb7b403SOfer Heifetz { 4551eb7b403SOfer Heifetz struct ahash_request *areq = ahash_request_cast(async); 4561eb7b403SOfer Heifetz struct safexcel_ahash_req *req = ahash_request_ctx(areq); 4571eb7b403SOfer Heifetz int ret; 4581eb7b403SOfer Heifetz 4591eb7b403SOfer Heifetz if (req->needs_inv) 4601eb7b403SOfer Heifetz ret = safexcel_ahash_send_inv(async, ring, request, 4611eb7b403SOfer Heifetz commands, results); 4621eb7b403SOfer Heifetz else 4631eb7b403SOfer Heifetz ret = safexcel_ahash_send_req(async, ring, request, 4641eb7b403SOfer Heifetz commands, results); 4651eb7b403SOfer Heifetz return ret; 4661eb7b403SOfer Heifetz } 4671eb7b403SOfer Heifetz 4681b44c5a6SAntoine Ténart static int safexcel_ahash_exit_inv(struct crypto_tfm *tfm) 4691b44c5a6SAntoine Ténart { 4701b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm); 4711b44c5a6SAntoine Ténart struct safexcel_crypto_priv *priv = ctx->priv; 4727cad2fabSAntoine Ténart AHASH_REQUEST_ON_STACK(req, __crypto_ahash_cast(tfm)); 4737cad2fabSAntoine Ténart struct safexcel_ahash_req *rctx = ahash_request_ctx(req); 4743e1166b9SArnd Bergmann struct safexcel_inv_result result = {}; 47586671abbSAntoine Ténart int ring = ctx->base.ring; 4761b44c5a6SAntoine Ténart 4777cad2fabSAntoine Ténart memset(req, 0, sizeof(struct ahash_request)); 4781b44c5a6SAntoine Ténart 4791b44c5a6SAntoine Ténart /* create invalidation request */ 4801b44c5a6SAntoine Ténart init_completion(&result.completion); 4817cad2fabSAntoine Ténart ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 4821b44c5a6SAntoine Ténart safexcel_inv_complete, &result); 4831b44c5a6SAntoine Ténart 4847cad2fabSAntoine Ténart ahash_request_set_tfm(req, __crypto_ahash_cast(tfm)); 4857cad2fabSAntoine Ténart ctx = crypto_tfm_ctx(req->base.tfm); 4861b44c5a6SAntoine Ténart ctx->base.exit_inv = true; 4871eb7b403SOfer Heifetz rctx->needs_inv = true; 4881b44c5a6SAntoine Ténart 48986671abbSAntoine Ténart spin_lock_bh(&priv->ring[ring].queue_lock); 4907cad2fabSAntoine Ténart crypto_enqueue_request(&priv->ring[ring].queue, &req->base); 49186671abbSAntoine Ténart spin_unlock_bh(&priv->ring[ring].queue_lock); 4921b44c5a6SAntoine Ténart 4938472e778SAntoine Ténart queue_work(priv->ring[ring].workqueue, 4948472e778SAntoine Ténart &priv->ring[ring].work_data.work); 4951b44c5a6SAntoine Ténart 4961b44c5a6SAntoine Ténart wait_for_completion_interruptible(&result.completion); 4971b44c5a6SAntoine Ténart 4981b44c5a6SAntoine Ténart if (result.error) { 4991b44c5a6SAntoine Ténart dev_warn(priv->dev, "hash: completion error (%d)\n", 5001b44c5a6SAntoine Ténart result.error); 5011b44c5a6SAntoine Ténart return result.error; 5021b44c5a6SAntoine Ténart } 5031b44c5a6SAntoine Ténart 5041b44c5a6SAntoine Ténart return 0; 5051b44c5a6SAntoine Ténart } 5061b44c5a6SAntoine Ténart 507cc75f5ceSAntoine Ténart /* safexcel_ahash_cache: cache data until at least one request can be sent to 508cc75f5ceSAntoine Ténart * the engine, aka. when there is at least 1 block size in the pipe. 509cc75f5ceSAntoine Ténart */ 5101b44c5a6SAntoine Ténart static int safexcel_ahash_cache(struct ahash_request *areq) 5111b44c5a6SAntoine Ténart { 5121b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 5131b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 5141b44c5a6SAntoine Ténart int queued, cache_len; 5151b44c5a6SAntoine Ténart 516cc75f5ceSAntoine Ténart /* cache_len: everyting accepted by the driver but not sent yet, 517cc75f5ceSAntoine Ténart * tot sz handled by update() - last req sz - tot sz handled by send() 518cc75f5ceSAntoine Ténart */ 5191b44c5a6SAntoine Ténart cache_len = req->len - areq->nbytes - req->processed; 520cc75f5ceSAntoine Ténart /* queued: everything accepted by the driver which will be handled by 521cc75f5ceSAntoine Ténart * the next send() calls. 522cc75f5ceSAntoine Ténart * tot sz handled by update() - tot sz handled by send() 523cc75f5ceSAntoine Ténart */ 5241b44c5a6SAntoine Ténart queued = req->len - req->processed; 5251b44c5a6SAntoine Ténart 5261b44c5a6SAntoine Ténart /* 5271b44c5a6SAntoine Ténart * In case there isn't enough bytes to proceed (less than a 5281b44c5a6SAntoine Ténart * block size), cache the data until we have enough. 5291b44c5a6SAntoine Ténart */ 5301b44c5a6SAntoine Ténart if (cache_len + areq->nbytes <= crypto_ahash_blocksize(ahash)) { 5311b44c5a6SAntoine Ténart sg_pcopy_to_buffer(areq->src, sg_nents(areq->src), 5321b44c5a6SAntoine Ténart req->cache + cache_len, 5331b44c5a6SAntoine Ténart areq->nbytes, 0); 5341b44c5a6SAntoine Ténart return areq->nbytes; 5351b44c5a6SAntoine Ténart } 5361b44c5a6SAntoine Ténart 537dfbcc08fSAntoine Ténart /* We couldn't cache all the data */ 5381b44c5a6SAntoine Ténart return -E2BIG; 5391b44c5a6SAntoine Ténart } 5401b44c5a6SAntoine Ténart 5411b44c5a6SAntoine Ténart static int safexcel_ahash_enqueue(struct ahash_request *areq) 5421b44c5a6SAntoine Ténart { 5431b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 5441b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 5451b44c5a6SAntoine Ténart struct safexcel_crypto_priv *priv = ctx->priv; 54686671abbSAntoine Ténart int ret, ring; 5471b44c5a6SAntoine Ténart 5481eb7b403SOfer Heifetz req->needs_inv = false; 5491b44c5a6SAntoine Ténart 550c4daf4ccSOfer Heifetz if (ctx->base.ctxr) { 551871df319SAntoine Ténart if (priv->version == EIP197 && 552871df319SAntoine Ténart !ctx->base.needs_inv && req->processed && 553c4daf4ccSOfer Heifetz ctx->digest == CONTEXT_CONTROL_DIGEST_PRECOMPUTED) 554c4daf4ccSOfer Heifetz /* We're still setting needs_inv here, even though it is 555c4daf4ccSOfer Heifetz * cleared right away, because the needs_inv flag can be 556c4daf4ccSOfer Heifetz * set in other functions and we want to keep the same 557c4daf4ccSOfer Heifetz * logic. 558c4daf4ccSOfer Heifetz */ 5591b44c5a6SAntoine Ténart ctx->base.needs_inv = safexcel_ahash_needs_inv_get(areq); 5601b44c5a6SAntoine Ténart 5611eb7b403SOfer Heifetz if (ctx->base.needs_inv) { 5621eb7b403SOfer Heifetz ctx->base.needs_inv = false; 5631eb7b403SOfer Heifetz req->needs_inv = true; 5641eb7b403SOfer Heifetz } 5651b44c5a6SAntoine Ténart } else { 5661b44c5a6SAntoine Ténart ctx->base.ring = safexcel_select_ring(priv); 5671b44c5a6SAntoine Ténart ctx->base.ctxr = dma_pool_zalloc(priv->context_pool, 5681b44c5a6SAntoine Ténart EIP197_GFP_FLAGS(areq->base), 5691b44c5a6SAntoine Ténart &ctx->base.ctxr_dma); 5701b44c5a6SAntoine Ténart if (!ctx->base.ctxr) 5711b44c5a6SAntoine Ténart return -ENOMEM; 5721b44c5a6SAntoine Ténart } 5731b44c5a6SAntoine Ténart 57486671abbSAntoine Ténart ring = ctx->base.ring; 5751b44c5a6SAntoine Ténart 57686671abbSAntoine Ténart spin_lock_bh(&priv->ring[ring].queue_lock); 57786671abbSAntoine Ténart ret = crypto_enqueue_request(&priv->ring[ring].queue, &areq->base); 57886671abbSAntoine Ténart spin_unlock_bh(&priv->ring[ring].queue_lock); 57986671abbSAntoine Ténart 5808472e778SAntoine Ténart queue_work(priv->ring[ring].workqueue, 5818472e778SAntoine Ténart &priv->ring[ring].work_data.work); 5821b44c5a6SAntoine Ténart 5831b44c5a6SAntoine Ténart return ret; 5841b44c5a6SAntoine Ténart } 5851b44c5a6SAntoine Ténart 5861b44c5a6SAntoine Ténart static int safexcel_ahash_update(struct ahash_request *areq) 5871b44c5a6SAntoine Ténart { 5881b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 5891b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 5901b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 5911b44c5a6SAntoine Ténart 5921b44c5a6SAntoine Ténart /* If the request is 0 length, do nothing */ 5931b44c5a6SAntoine Ténart if (!areq->nbytes) 5941b44c5a6SAntoine Ténart return 0; 5951b44c5a6SAntoine Ténart 5961b44c5a6SAntoine Ténart req->len += areq->nbytes; 5971b44c5a6SAntoine Ténart 5981b44c5a6SAntoine Ténart safexcel_ahash_cache(areq); 5991b44c5a6SAntoine Ténart 6001b44c5a6SAntoine Ténart /* 6011b44c5a6SAntoine Ténart * We're not doing partial updates when performing an hmac request. 6021b44c5a6SAntoine Ténart * Everything will be handled by the final() call. 6031b44c5a6SAntoine Ténart */ 6041b44c5a6SAntoine Ténart if (ctx->digest == CONTEXT_CONTROL_DIGEST_HMAC) 6051b44c5a6SAntoine Ténart return 0; 6061b44c5a6SAntoine Ténart 6071b44c5a6SAntoine Ténart if (req->hmac) 6081b44c5a6SAntoine Ténart return safexcel_ahash_enqueue(areq); 6091b44c5a6SAntoine Ténart 6101b44c5a6SAntoine Ténart if (!req->last_req && 6111b44c5a6SAntoine Ténart req->len - req->processed > crypto_ahash_blocksize(ahash)) 6121b44c5a6SAntoine Ténart return safexcel_ahash_enqueue(areq); 6131b44c5a6SAntoine Ténart 6141b44c5a6SAntoine Ténart return 0; 6151b44c5a6SAntoine Ténart } 6161b44c5a6SAntoine Ténart 6171b44c5a6SAntoine Ténart static int safexcel_ahash_final(struct ahash_request *areq) 6181b44c5a6SAntoine Ténart { 6191b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 6201b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 6211b44c5a6SAntoine Ténart 6221b44c5a6SAntoine Ténart req->last_req = true; 6231b44c5a6SAntoine Ténart req->finish = true; 6241b44c5a6SAntoine Ténart 6251b44c5a6SAntoine Ténart /* If we have an overall 0 length request */ 6261b44c5a6SAntoine Ténart if (!(req->len + areq->nbytes)) { 6271b44c5a6SAntoine Ténart if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA1) 6281b44c5a6SAntoine Ténart memcpy(areq->result, sha1_zero_message_hash, 6291b44c5a6SAntoine Ténart SHA1_DIGEST_SIZE); 6301b44c5a6SAntoine Ténart else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA224) 6311b44c5a6SAntoine Ténart memcpy(areq->result, sha224_zero_message_hash, 6321b44c5a6SAntoine Ténart SHA224_DIGEST_SIZE); 6331b44c5a6SAntoine Ténart else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA256) 6341b44c5a6SAntoine Ténart memcpy(areq->result, sha256_zero_message_hash, 6351b44c5a6SAntoine Ténart SHA256_DIGEST_SIZE); 6361b44c5a6SAntoine Ténart 6371b44c5a6SAntoine Ténart return 0; 6381b44c5a6SAntoine Ténart } 6391b44c5a6SAntoine Ténart 6401b44c5a6SAntoine Ténart return safexcel_ahash_enqueue(areq); 6411b44c5a6SAntoine Ténart } 6421b44c5a6SAntoine Ténart 6431b44c5a6SAntoine Ténart static int safexcel_ahash_finup(struct ahash_request *areq) 6441b44c5a6SAntoine Ténart { 6451b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 6461b44c5a6SAntoine Ténart 6471b44c5a6SAntoine Ténart req->last_req = true; 6481b44c5a6SAntoine Ténart req->finish = true; 6491b44c5a6SAntoine Ténart 6501b44c5a6SAntoine Ténart safexcel_ahash_update(areq); 6511b44c5a6SAntoine Ténart return safexcel_ahash_final(areq); 6521b44c5a6SAntoine Ténart } 6531b44c5a6SAntoine Ténart 6541b44c5a6SAntoine Ténart static int safexcel_ahash_export(struct ahash_request *areq, void *out) 6551b44c5a6SAntoine Ténart { 6561b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 6571b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 6581b44c5a6SAntoine Ténart struct safexcel_ahash_export_state *export = out; 6591b44c5a6SAntoine Ténart 6601b44c5a6SAntoine Ténart export->len = req->len; 6611b44c5a6SAntoine Ténart export->processed = req->processed; 6621b44c5a6SAntoine Ténart 6631b44c5a6SAntoine Ténart memcpy(export->state, req->state, req->state_sz); 6641b44c5a6SAntoine Ténart memcpy(export->cache, req->cache, crypto_ahash_blocksize(ahash)); 6651b44c5a6SAntoine Ténart 6661b44c5a6SAntoine Ténart return 0; 6671b44c5a6SAntoine Ténart } 6681b44c5a6SAntoine Ténart 6691b44c5a6SAntoine Ténart static int safexcel_ahash_import(struct ahash_request *areq, const void *in) 6701b44c5a6SAntoine Ténart { 6711b44c5a6SAntoine Ténart struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 6721b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 6731b44c5a6SAntoine Ténart const struct safexcel_ahash_export_state *export = in; 6741b44c5a6SAntoine Ténart int ret; 6751b44c5a6SAntoine Ténart 6761b44c5a6SAntoine Ténart ret = crypto_ahash_init(areq); 6771b44c5a6SAntoine Ténart if (ret) 6781b44c5a6SAntoine Ténart return ret; 6791b44c5a6SAntoine Ténart 6801b44c5a6SAntoine Ténart req->len = export->len; 6811b44c5a6SAntoine Ténart req->processed = export->processed; 6821b44c5a6SAntoine Ténart 6831b44c5a6SAntoine Ténart memcpy(req->cache, export->cache, crypto_ahash_blocksize(ahash)); 6841b44c5a6SAntoine Ténart memcpy(req->state, export->state, req->state_sz); 6851b44c5a6SAntoine Ténart 6861b44c5a6SAntoine Ténart return 0; 6871b44c5a6SAntoine Ténart } 6881b44c5a6SAntoine Ténart 6891b44c5a6SAntoine Ténart static int safexcel_ahash_cra_init(struct crypto_tfm *tfm) 6901b44c5a6SAntoine Ténart { 6911b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm); 6921b44c5a6SAntoine Ténart struct safexcel_alg_template *tmpl = 6931b44c5a6SAntoine Ténart container_of(__crypto_ahash_alg(tfm->__crt_alg), 6941b44c5a6SAntoine Ténart struct safexcel_alg_template, alg.ahash); 6951b44c5a6SAntoine Ténart 6961b44c5a6SAntoine Ténart ctx->priv = tmpl->priv; 6971eb7b403SOfer Heifetz ctx->base.send = safexcel_ahash_send; 6981eb7b403SOfer Heifetz ctx->base.handle_result = safexcel_handle_result; 6991b44c5a6SAntoine Ténart 7001b44c5a6SAntoine Ténart crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 7011b44c5a6SAntoine Ténart sizeof(struct safexcel_ahash_req)); 7021b44c5a6SAntoine Ténart return 0; 7031b44c5a6SAntoine Ténart } 7041b44c5a6SAntoine Ténart 7051b44c5a6SAntoine Ténart static int safexcel_sha1_init(struct ahash_request *areq) 7061b44c5a6SAntoine Ténart { 7071b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 7081b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 7091b44c5a6SAntoine Ténart 7101b44c5a6SAntoine Ténart memset(req, 0, sizeof(*req)); 7111b44c5a6SAntoine Ténart 7121b44c5a6SAntoine Ténart req->state[0] = SHA1_H0; 7131b44c5a6SAntoine Ténart req->state[1] = SHA1_H1; 7141b44c5a6SAntoine Ténart req->state[2] = SHA1_H2; 7151b44c5a6SAntoine Ténart req->state[3] = SHA1_H3; 7161b44c5a6SAntoine Ténart req->state[4] = SHA1_H4; 7171b44c5a6SAntoine Ténart 7181b44c5a6SAntoine Ténart ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA1; 7191b44c5a6SAntoine Ténart ctx->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED; 7201b44c5a6SAntoine Ténart req->state_sz = SHA1_DIGEST_SIZE; 7211b44c5a6SAntoine Ténart 7221b44c5a6SAntoine Ténart return 0; 7231b44c5a6SAntoine Ténart } 7241b44c5a6SAntoine Ténart 7251b44c5a6SAntoine Ténart static int safexcel_sha1_digest(struct ahash_request *areq) 7261b44c5a6SAntoine Ténart { 7271b44c5a6SAntoine Ténart int ret = safexcel_sha1_init(areq); 7281b44c5a6SAntoine Ténart 7291b44c5a6SAntoine Ténart if (ret) 7301b44c5a6SAntoine Ténart return ret; 7311b44c5a6SAntoine Ténart 7321b44c5a6SAntoine Ténart return safexcel_ahash_finup(areq); 7331b44c5a6SAntoine Ténart } 7341b44c5a6SAntoine Ténart 7351b44c5a6SAntoine Ténart static void safexcel_ahash_cra_exit(struct crypto_tfm *tfm) 7361b44c5a6SAntoine Ténart { 7371b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm); 7381b44c5a6SAntoine Ténart struct safexcel_crypto_priv *priv = ctx->priv; 7391b44c5a6SAntoine Ténart int ret; 7401b44c5a6SAntoine Ténart 7411b44c5a6SAntoine Ténart /* context not allocated, skip invalidation */ 7421b44c5a6SAntoine Ténart if (!ctx->base.ctxr) 7431b44c5a6SAntoine Ténart return; 7441b44c5a6SAntoine Ténart 745871df319SAntoine Ténart if (priv->version == EIP197) { 7461b44c5a6SAntoine Ténart ret = safexcel_ahash_exit_inv(tfm); 7471b44c5a6SAntoine Ténart if (ret) 7481b44c5a6SAntoine Ténart dev_warn(priv->dev, "hash: invalidation error %d\n", ret); 749871df319SAntoine Ténart } else { 750871df319SAntoine Ténart dma_pool_free(priv->context_pool, ctx->base.ctxr, 751871df319SAntoine Ténart ctx->base.ctxr_dma); 752871df319SAntoine Ténart } 7531b44c5a6SAntoine Ténart } 7541b44c5a6SAntoine Ténart 7551b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_sha1 = { 7561b44c5a6SAntoine Ténart .type = SAFEXCEL_ALG_TYPE_AHASH, 7571b44c5a6SAntoine Ténart .alg.ahash = { 7581b44c5a6SAntoine Ténart .init = safexcel_sha1_init, 7591b44c5a6SAntoine Ténart .update = safexcel_ahash_update, 7601b44c5a6SAntoine Ténart .final = safexcel_ahash_final, 7611b44c5a6SAntoine Ténart .finup = safexcel_ahash_finup, 7621b44c5a6SAntoine Ténart .digest = safexcel_sha1_digest, 7631b44c5a6SAntoine Ténart .export = safexcel_ahash_export, 7641b44c5a6SAntoine Ténart .import = safexcel_ahash_import, 7651b44c5a6SAntoine Ténart .halg = { 7661b44c5a6SAntoine Ténart .digestsize = SHA1_DIGEST_SIZE, 7671b44c5a6SAntoine Ténart .statesize = sizeof(struct safexcel_ahash_export_state), 7681b44c5a6SAntoine Ténart .base = { 7691b44c5a6SAntoine Ténart .cra_name = "sha1", 7701b44c5a6SAntoine Ténart .cra_driver_name = "safexcel-sha1", 7711b44c5a6SAntoine Ténart .cra_priority = 300, 7721b44c5a6SAntoine Ténart .cra_flags = CRYPTO_ALG_ASYNC | 7731b44c5a6SAntoine Ténart CRYPTO_ALG_KERN_DRIVER_ONLY, 7741b44c5a6SAntoine Ténart .cra_blocksize = SHA1_BLOCK_SIZE, 7751b44c5a6SAntoine Ténart .cra_ctxsize = sizeof(struct safexcel_ahash_ctx), 7761b44c5a6SAntoine Ténart .cra_init = safexcel_ahash_cra_init, 7771b44c5a6SAntoine Ténart .cra_exit = safexcel_ahash_cra_exit, 7781b44c5a6SAntoine Ténart .cra_module = THIS_MODULE, 7791b44c5a6SAntoine Ténart }, 7801b44c5a6SAntoine Ténart }, 7811b44c5a6SAntoine Ténart }, 7821b44c5a6SAntoine Ténart }; 7831b44c5a6SAntoine Ténart 7841b44c5a6SAntoine Ténart static int safexcel_hmac_sha1_init(struct ahash_request *areq) 7851b44c5a6SAntoine Ténart { 7861b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 7871b44c5a6SAntoine Ténart 7881b44c5a6SAntoine Ténart safexcel_sha1_init(areq); 7891b44c5a6SAntoine Ténart ctx->digest = CONTEXT_CONTROL_DIGEST_HMAC; 7901b44c5a6SAntoine Ténart return 0; 7911b44c5a6SAntoine Ténart } 7921b44c5a6SAntoine Ténart 7931b44c5a6SAntoine Ténart static int safexcel_hmac_sha1_digest(struct ahash_request *areq) 7941b44c5a6SAntoine Ténart { 7951b44c5a6SAntoine Ténart int ret = safexcel_hmac_sha1_init(areq); 7961b44c5a6SAntoine Ténart 7971b44c5a6SAntoine Ténart if (ret) 7981b44c5a6SAntoine Ténart return ret; 7991b44c5a6SAntoine Ténart 8001b44c5a6SAntoine Ténart return safexcel_ahash_finup(areq); 8011b44c5a6SAntoine Ténart } 8021b44c5a6SAntoine Ténart 8031b44c5a6SAntoine Ténart struct safexcel_ahash_result { 8041b44c5a6SAntoine Ténart struct completion completion; 8051b44c5a6SAntoine Ténart int error; 8061b44c5a6SAntoine Ténart }; 8071b44c5a6SAntoine Ténart 8081b44c5a6SAntoine Ténart static void safexcel_ahash_complete(struct crypto_async_request *req, int error) 8091b44c5a6SAntoine Ténart { 8101b44c5a6SAntoine Ténart struct safexcel_ahash_result *result = req->data; 8111b44c5a6SAntoine Ténart 8121b44c5a6SAntoine Ténart if (error == -EINPROGRESS) 8131b44c5a6SAntoine Ténart return; 8141b44c5a6SAntoine Ténart 8151b44c5a6SAntoine Ténart result->error = error; 8161b44c5a6SAntoine Ténart complete(&result->completion); 8171b44c5a6SAntoine Ténart } 8181b44c5a6SAntoine Ténart 8191b44c5a6SAntoine Ténart static int safexcel_hmac_init_pad(struct ahash_request *areq, 8201b44c5a6SAntoine Ténart unsigned int blocksize, const u8 *key, 8211b44c5a6SAntoine Ténart unsigned int keylen, u8 *ipad, u8 *opad) 8221b44c5a6SAntoine Ténart { 8231b44c5a6SAntoine Ténart struct safexcel_ahash_result result; 8241b44c5a6SAntoine Ténart struct scatterlist sg; 8251b44c5a6SAntoine Ténart int ret, i; 8261b44c5a6SAntoine Ténart u8 *keydup; 8271b44c5a6SAntoine Ténart 8281b44c5a6SAntoine Ténart if (keylen <= blocksize) { 8291b44c5a6SAntoine Ténart memcpy(ipad, key, keylen); 8301b44c5a6SAntoine Ténart } else { 8311b44c5a6SAntoine Ténart keydup = kmemdup(key, keylen, GFP_KERNEL); 8321b44c5a6SAntoine Ténart if (!keydup) 8331b44c5a6SAntoine Ténart return -ENOMEM; 8341b44c5a6SAntoine Ténart 8351b44c5a6SAntoine Ténart ahash_request_set_callback(areq, CRYPTO_TFM_REQ_MAY_BACKLOG, 8361b44c5a6SAntoine Ténart safexcel_ahash_complete, &result); 8371b44c5a6SAntoine Ténart sg_init_one(&sg, keydup, keylen); 8381b44c5a6SAntoine Ténart ahash_request_set_crypt(areq, &sg, ipad, keylen); 8391b44c5a6SAntoine Ténart init_completion(&result.completion); 8401b44c5a6SAntoine Ténart 8411b44c5a6SAntoine Ténart ret = crypto_ahash_digest(areq); 8421b44c5a6SAntoine Ténart if (ret == -EINPROGRESS) { 8431b44c5a6SAntoine Ténart wait_for_completion_interruptible(&result.completion); 8441b44c5a6SAntoine Ténart ret = result.error; 8451b44c5a6SAntoine Ténart } 8461b44c5a6SAntoine Ténart 8471b44c5a6SAntoine Ténart /* Avoid leaking */ 8481b44c5a6SAntoine Ténart memzero_explicit(keydup, keylen); 8491b44c5a6SAntoine Ténart kfree(keydup); 8501b44c5a6SAntoine Ténart 8511b44c5a6SAntoine Ténart if (ret) 8521b44c5a6SAntoine Ténart return ret; 8531b44c5a6SAntoine Ténart 8541b44c5a6SAntoine Ténart keylen = crypto_ahash_digestsize(crypto_ahash_reqtfm(areq)); 8551b44c5a6SAntoine Ténart } 8561b44c5a6SAntoine Ténart 8571b44c5a6SAntoine Ténart memset(ipad + keylen, 0, blocksize - keylen); 8581b44c5a6SAntoine Ténart memcpy(opad, ipad, blocksize); 8591b44c5a6SAntoine Ténart 8601b44c5a6SAntoine Ténart for (i = 0; i < blocksize; i++) { 861aed3731eSAntoine Ténart ipad[i] ^= HMAC_IPAD_VALUE; 862aed3731eSAntoine Ténart opad[i] ^= HMAC_OPAD_VALUE; 8631b44c5a6SAntoine Ténart } 8641b44c5a6SAntoine Ténart 8651b44c5a6SAntoine Ténart return 0; 8661b44c5a6SAntoine Ténart } 8671b44c5a6SAntoine Ténart 8681b44c5a6SAntoine Ténart static int safexcel_hmac_init_iv(struct ahash_request *areq, 8691b44c5a6SAntoine Ténart unsigned int blocksize, u8 *pad, void *state) 8701b44c5a6SAntoine Ténart { 8711b44c5a6SAntoine Ténart struct safexcel_ahash_result result; 8721b44c5a6SAntoine Ténart struct safexcel_ahash_req *req; 8731b44c5a6SAntoine Ténart struct scatterlist sg; 8741b44c5a6SAntoine Ténart int ret; 8751b44c5a6SAntoine Ténart 8761b44c5a6SAntoine Ténart ahash_request_set_callback(areq, CRYPTO_TFM_REQ_MAY_BACKLOG, 8771b44c5a6SAntoine Ténart safexcel_ahash_complete, &result); 8781b44c5a6SAntoine Ténart sg_init_one(&sg, pad, blocksize); 8791b44c5a6SAntoine Ténart ahash_request_set_crypt(areq, &sg, pad, blocksize); 8801b44c5a6SAntoine Ténart init_completion(&result.completion); 8811b44c5a6SAntoine Ténart 8821b44c5a6SAntoine Ténart ret = crypto_ahash_init(areq); 8831b44c5a6SAntoine Ténart if (ret) 8841b44c5a6SAntoine Ténart return ret; 8851b44c5a6SAntoine Ténart 8861b44c5a6SAntoine Ténart req = ahash_request_ctx(areq); 8871b44c5a6SAntoine Ténart req->hmac = true; 8881b44c5a6SAntoine Ténart req->last_req = true; 8891b44c5a6SAntoine Ténart 8901b44c5a6SAntoine Ténart ret = crypto_ahash_update(areq); 89112bf4142SOfer Heifetz if (ret && ret != -EINPROGRESS && ret != -EBUSY) 8921b44c5a6SAntoine Ténart return ret; 8931b44c5a6SAntoine Ténart 8941b44c5a6SAntoine Ténart wait_for_completion_interruptible(&result.completion); 8951b44c5a6SAntoine Ténart if (result.error) 8961b44c5a6SAntoine Ténart return result.error; 8971b44c5a6SAntoine Ténart 8981b44c5a6SAntoine Ténart return crypto_ahash_export(areq, state); 8991b44c5a6SAntoine Ténart } 9001b44c5a6SAntoine Ténart 9011b44c5a6SAntoine Ténart static int safexcel_hmac_setkey(const char *alg, const u8 *key, 9021b44c5a6SAntoine Ténart unsigned int keylen, void *istate, void *ostate) 9031b44c5a6SAntoine Ténart { 9041b44c5a6SAntoine Ténart struct ahash_request *areq; 9051b44c5a6SAntoine Ténart struct crypto_ahash *tfm; 9061b44c5a6SAntoine Ténart unsigned int blocksize; 9071b44c5a6SAntoine Ténart u8 *ipad, *opad; 9081b44c5a6SAntoine Ténart int ret; 9091b44c5a6SAntoine Ténart 9101b44c5a6SAntoine Ténart tfm = crypto_alloc_ahash(alg, CRYPTO_ALG_TYPE_AHASH, 9111b44c5a6SAntoine Ténart CRYPTO_ALG_TYPE_AHASH_MASK); 9121b44c5a6SAntoine Ténart if (IS_ERR(tfm)) 9131b44c5a6SAntoine Ténart return PTR_ERR(tfm); 9141b44c5a6SAntoine Ténart 9151b44c5a6SAntoine Ténart areq = ahash_request_alloc(tfm, GFP_KERNEL); 9161b44c5a6SAntoine Ténart if (!areq) { 9171b44c5a6SAntoine Ténart ret = -ENOMEM; 9181b44c5a6SAntoine Ténart goto free_ahash; 9191b44c5a6SAntoine Ténart } 9201b44c5a6SAntoine Ténart 9211b44c5a6SAntoine Ténart crypto_ahash_clear_flags(tfm, ~0); 9221b44c5a6SAntoine Ténart blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm)); 9231b44c5a6SAntoine Ténart 9241b44c5a6SAntoine Ténart ipad = kzalloc(2 * blocksize, GFP_KERNEL); 9251b44c5a6SAntoine Ténart if (!ipad) { 9261b44c5a6SAntoine Ténart ret = -ENOMEM; 9271b44c5a6SAntoine Ténart goto free_request; 9281b44c5a6SAntoine Ténart } 9291b44c5a6SAntoine Ténart 9301b44c5a6SAntoine Ténart opad = ipad + blocksize; 9311b44c5a6SAntoine Ténart 9321b44c5a6SAntoine Ténart ret = safexcel_hmac_init_pad(areq, blocksize, key, keylen, ipad, opad); 9331b44c5a6SAntoine Ténart if (ret) 9341b44c5a6SAntoine Ténart goto free_ipad; 9351b44c5a6SAntoine Ténart 9361b44c5a6SAntoine Ténart ret = safexcel_hmac_init_iv(areq, blocksize, ipad, istate); 9371b44c5a6SAntoine Ténart if (ret) 9381b44c5a6SAntoine Ténart goto free_ipad; 9391b44c5a6SAntoine Ténart 9401b44c5a6SAntoine Ténart ret = safexcel_hmac_init_iv(areq, blocksize, opad, ostate); 9411b44c5a6SAntoine Ténart 9421b44c5a6SAntoine Ténart free_ipad: 9431b44c5a6SAntoine Ténart kfree(ipad); 9441b44c5a6SAntoine Ténart free_request: 9451b44c5a6SAntoine Ténart ahash_request_free(areq); 9461b44c5a6SAntoine Ténart free_ahash: 9471b44c5a6SAntoine Ténart crypto_free_ahash(tfm); 9481b44c5a6SAntoine Ténart 9491b44c5a6SAntoine Ténart return ret; 9501b44c5a6SAntoine Ténart } 9511b44c5a6SAntoine Ténart 9521b44c5a6SAntoine Ténart static int safexcel_hmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key, 9531b44c5a6SAntoine Ténart unsigned int keylen) 9541b44c5a6SAntoine Ténart { 9551b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm)); 956871df319SAntoine Ténart struct safexcel_crypto_priv *priv = ctx->priv; 9571b44c5a6SAntoine Ténart struct safexcel_ahash_export_state istate, ostate; 9581b44c5a6SAntoine Ténart int ret, i; 9591b44c5a6SAntoine Ténart 9601b44c5a6SAntoine Ténart ret = safexcel_hmac_setkey("safexcel-sha1", key, keylen, &istate, &ostate); 9611b44c5a6SAntoine Ténart if (ret) 9621b44c5a6SAntoine Ténart return ret; 9631b44c5a6SAntoine Ténart 964871df319SAntoine Ténart if (priv->version == EIP197 && ctx->base.ctxr) { 96560c4081eSAntoine Ténart for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) { 9661b44c5a6SAntoine Ténart if (ctx->ipad[i] != le32_to_cpu(istate.state[i]) || 9671b44c5a6SAntoine Ténart ctx->opad[i] != le32_to_cpu(ostate.state[i])) { 9681b44c5a6SAntoine Ténart ctx->base.needs_inv = true; 9691b44c5a6SAntoine Ténart break; 9701b44c5a6SAntoine Ténart } 9711b44c5a6SAntoine Ténart } 972c4daf4ccSOfer Heifetz } 9731b44c5a6SAntoine Ténart 97442ef3bedSAntoine Ténart memcpy(ctx->ipad, &istate.state, SHA1_DIGEST_SIZE); 97542ef3bedSAntoine Ténart memcpy(ctx->opad, &ostate.state, SHA1_DIGEST_SIZE); 97642ef3bedSAntoine Ténart 9771b44c5a6SAntoine Ténart return 0; 9781b44c5a6SAntoine Ténart } 9791b44c5a6SAntoine Ténart 9801b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_hmac_sha1 = { 9811b44c5a6SAntoine Ténart .type = SAFEXCEL_ALG_TYPE_AHASH, 9821b44c5a6SAntoine Ténart .alg.ahash = { 9831b44c5a6SAntoine Ténart .init = safexcel_hmac_sha1_init, 9841b44c5a6SAntoine Ténart .update = safexcel_ahash_update, 9851b44c5a6SAntoine Ténart .final = safexcel_ahash_final, 9861b44c5a6SAntoine Ténart .finup = safexcel_ahash_finup, 9871b44c5a6SAntoine Ténart .digest = safexcel_hmac_sha1_digest, 9881b44c5a6SAntoine Ténart .setkey = safexcel_hmac_sha1_setkey, 9891b44c5a6SAntoine Ténart .export = safexcel_ahash_export, 9901b44c5a6SAntoine Ténart .import = safexcel_ahash_import, 9911b44c5a6SAntoine Ténart .halg = { 9921b44c5a6SAntoine Ténart .digestsize = SHA1_DIGEST_SIZE, 9931b44c5a6SAntoine Ténart .statesize = sizeof(struct safexcel_ahash_export_state), 9941b44c5a6SAntoine Ténart .base = { 9951b44c5a6SAntoine Ténart .cra_name = "hmac(sha1)", 9961b44c5a6SAntoine Ténart .cra_driver_name = "safexcel-hmac-sha1", 9971b44c5a6SAntoine Ténart .cra_priority = 300, 9981b44c5a6SAntoine Ténart .cra_flags = CRYPTO_ALG_ASYNC | 9991b44c5a6SAntoine Ténart CRYPTO_ALG_KERN_DRIVER_ONLY, 10001b44c5a6SAntoine Ténart .cra_blocksize = SHA1_BLOCK_SIZE, 10011b44c5a6SAntoine Ténart .cra_ctxsize = sizeof(struct safexcel_ahash_ctx), 10021b44c5a6SAntoine Ténart .cra_init = safexcel_ahash_cra_init, 10031b44c5a6SAntoine Ténart .cra_exit = safexcel_ahash_cra_exit, 10041b44c5a6SAntoine Ténart .cra_module = THIS_MODULE, 10051b44c5a6SAntoine Ténart }, 10061b44c5a6SAntoine Ténart }, 10071b44c5a6SAntoine Ténart }, 10081b44c5a6SAntoine Ténart }; 10091b44c5a6SAntoine Ténart 10101b44c5a6SAntoine Ténart static int safexcel_sha256_init(struct ahash_request *areq) 10111b44c5a6SAntoine Ténart { 10121b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 10131b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 10141b44c5a6SAntoine Ténart 10151b44c5a6SAntoine Ténart memset(req, 0, sizeof(*req)); 10161b44c5a6SAntoine Ténart 10171b44c5a6SAntoine Ténart req->state[0] = SHA256_H0; 10181b44c5a6SAntoine Ténart req->state[1] = SHA256_H1; 10191b44c5a6SAntoine Ténart req->state[2] = SHA256_H2; 10201b44c5a6SAntoine Ténart req->state[3] = SHA256_H3; 10211b44c5a6SAntoine Ténart req->state[4] = SHA256_H4; 10221b44c5a6SAntoine Ténart req->state[5] = SHA256_H5; 10231b44c5a6SAntoine Ténart req->state[6] = SHA256_H6; 10241b44c5a6SAntoine Ténart req->state[7] = SHA256_H7; 10251b44c5a6SAntoine Ténart 10261b44c5a6SAntoine Ténart ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA256; 10271b44c5a6SAntoine Ténart ctx->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED; 10281b44c5a6SAntoine Ténart req->state_sz = SHA256_DIGEST_SIZE; 10291b44c5a6SAntoine Ténart 10301b44c5a6SAntoine Ténart return 0; 10311b44c5a6SAntoine Ténart } 10321b44c5a6SAntoine Ténart 10331b44c5a6SAntoine Ténart static int safexcel_sha256_digest(struct ahash_request *areq) 10341b44c5a6SAntoine Ténart { 10351b44c5a6SAntoine Ténart int ret = safexcel_sha256_init(areq); 10361b44c5a6SAntoine Ténart 10371b44c5a6SAntoine Ténart if (ret) 10381b44c5a6SAntoine Ténart return ret; 10391b44c5a6SAntoine Ténart 10401b44c5a6SAntoine Ténart return safexcel_ahash_finup(areq); 10411b44c5a6SAntoine Ténart } 10421b44c5a6SAntoine Ténart 10431b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_sha256 = { 10441b44c5a6SAntoine Ténart .type = SAFEXCEL_ALG_TYPE_AHASH, 10451b44c5a6SAntoine Ténart .alg.ahash = { 10461b44c5a6SAntoine Ténart .init = safexcel_sha256_init, 10471b44c5a6SAntoine Ténart .update = safexcel_ahash_update, 10481b44c5a6SAntoine Ténart .final = safexcel_ahash_final, 10491b44c5a6SAntoine Ténart .finup = safexcel_ahash_finup, 10501b44c5a6SAntoine Ténart .digest = safexcel_sha256_digest, 10511b44c5a6SAntoine Ténart .export = safexcel_ahash_export, 10521b44c5a6SAntoine Ténart .import = safexcel_ahash_import, 10531b44c5a6SAntoine Ténart .halg = { 10541b44c5a6SAntoine Ténart .digestsize = SHA256_DIGEST_SIZE, 10551b44c5a6SAntoine Ténart .statesize = sizeof(struct safexcel_ahash_export_state), 10561b44c5a6SAntoine Ténart .base = { 10571b44c5a6SAntoine Ténart .cra_name = "sha256", 10581b44c5a6SAntoine Ténart .cra_driver_name = "safexcel-sha256", 10591b44c5a6SAntoine Ténart .cra_priority = 300, 10601b44c5a6SAntoine Ténart .cra_flags = CRYPTO_ALG_ASYNC | 10611b44c5a6SAntoine Ténart CRYPTO_ALG_KERN_DRIVER_ONLY, 10621b44c5a6SAntoine Ténart .cra_blocksize = SHA256_BLOCK_SIZE, 10631b44c5a6SAntoine Ténart .cra_ctxsize = sizeof(struct safexcel_ahash_ctx), 10641b44c5a6SAntoine Ténart .cra_init = safexcel_ahash_cra_init, 10651b44c5a6SAntoine Ténart .cra_exit = safexcel_ahash_cra_exit, 10661b44c5a6SAntoine Ténart .cra_module = THIS_MODULE, 10671b44c5a6SAntoine Ténart }, 10681b44c5a6SAntoine Ténart }, 10691b44c5a6SAntoine Ténart }, 10701b44c5a6SAntoine Ténart }; 10711b44c5a6SAntoine Ténart 10721b44c5a6SAntoine Ténart static int safexcel_sha224_init(struct ahash_request *areq) 10731b44c5a6SAntoine Ténart { 10741b44c5a6SAntoine Ténart struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); 10751b44c5a6SAntoine Ténart struct safexcel_ahash_req *req = ahash_request_ctx(areq); 10761b44c5a6SAntoine Ténart 10771b44c5a6SAntoine Ténart memset(req, 0, sizeof(*req)); 10781b44c5a6SAntoine Ténart 10791b44c5a6SAntoine Ténart req->state[0] = SHA224_H0; 10801b44c5a6SAntoine Ténart req->state[1] = SHA224_H1; 10811b44c5a6SAntoine Ténart req->state[2] = SHA224_H2; 10821b44c5a6SAntoine Ténart req->state[3] = SHA224_H3; 10831b44c5a6SAntoine Ténart req->state[4] = SHA224_H4; 10841b44c5a6SAntoine Ténart req->state[5] = SHA224_H5; 10851b44c5a6SAntoine Ténart req->state[6] = SHA224_H6; 10861b44c5a6SAntoine Ténart req->state[7] = SHA224_H7; 10871b44c5a6SAntoine Ténart 10881b44c5a6SAntoine Ténart ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA224; 10891b44c5a6SAntoine Ténart ctx->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED; 10901b44c5a6SAntoine Ténart req->state_sz = SHA256_DIGEST_SIZE; 10911b44c5a6SAntoine Ténart 10921b44c5a6SAntoine Ténart return 0; 10931b44c5a6SAntoine Ténart } 10941b44c5a6SAntoine Ténart 10951b44c5a6SAntoine Ténart static int safexcel_sha224_digest(struct ahash_request *areq) 10961b44c5a6SAntoine Ténart { 10971b44c5a6SAntoine Ténart int ret = safexcel_sha224_init(areq); 10981b44c5a6SAntoine Ténart 10991b44c5a6SAntoine Ténart if (ret) 11001b44c5a6SAntoine Ténart return ret; 11011b44c5a6SAntoine Ténart 11021b44c5a6SAntoine Ténart return safexcel_ahash_finup(areq); 11031b44c5a6SAntoine Ténart } 11041b44c5a6SAntoine Ténart 11051b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_sha224 = { 11061b44c5a6SAntoine Ténart .type = SAFEXCEL_ALG_TYPE_AHASH, 11071b44c5a6SAntoine Ténart .alg.ahash = { 11081b44c5a6SAntoine Ténart .init = safexcel_sha224_init, 11091b44c5a6SAntoine Ténart .update = safexcel_ahash_update, 11101b44c5a6SAntoine Ténart .final = safexcel_ahash_final, 11111b44c5a6SAntoine Ténart .finup = safexcel_ahash_finup, 11121b44c5a6SAntoine Ténart .digest = safexcel_sha224_digest, 11131b44c5a6SAntoine Ténart .export = safexcel_ahash_export, 11141b44c5a6SAntoine Ténart .import = safexcel_ahash_import, 11151b44c5a6SAntoine Ténart .halg = { 11161b44c5a6SAntoine Ténart .digestsize = SHA224_DIGEST_SIZE, 11171b44c5a6SAntoine Ténart .statesize = sizeof(struct safexcel_ahash_export_state), 11181b44c5a6SAntoine Ténart .base = { 11191b44c5a6SAntoine Ténart .cra_name = "sha224", 11201b44c5a6SAntoine Ténart .cra_driver_name = "safexcel-sha224", 11211b44c5a6SAntoine Ténart .cra_priority = 300, 11221b44c5a6SAntoine Ténart .cra_flags = CRYPTO_ALG_ASYNC | 11231b44c5a6SAntoine Ténart CRYPTO_ALG_KERN_DRIVER_ONLY, 11241b44c5a6SAntoine Ténart .cra_blocksize = SHA224_BLOCK_SIZE, 11251b44c5a6SAntoine Ténart .cra_ctxsize = sizeof(struct safexcel_ahash_ctx), 11261b44c5a6SAntoine Ténart .cra_init = safexcel_ahash_cra_init, 11271b44c5a6SAntoine Ténart .cra_exit = safexcel_ahash_cra_exit, 11281b44c5a6SAntoine Ténart .cra_module = THIS_MODULE, 11291b44c5a6SAntoine Ténart }, 11301b44c5a6SAntoine Ténart }, 11311b44c5a6SAntoine Ténart }, 11321b44c5a6SAntoine Ténart }; 1133