Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Glue code for the SHA256 Secure Hash Algorithm assembly implementation
0004  * using NEON instructions.
0005  *
0006  * Copyright © 2015 Google Inc.
0007  *
0008  * This file is based on sha512_neon_glue.c:
0009  *   Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi>
0010  */
0011 
0012 #include <crypto/internal/hash.h>
0013 #include <crypto/internal/simd.h>
0014 #include <linux/types.h>
0015 #include <linux/string.h>
0016 #include <crypto/sha2.h>
0017 #include <crypto/sha256_base.h>
0018 #include <asm/byteorder.h>
0019 #include <asm/simd.h>
0020 #include <asm/neon.h>
0021 
0022 #include "sha256_glue.h"
0023 
0024 asmlinkage void sha256_block_data_order_neon(u32 *digest, const void *data,
0025                          unsigned int num_blks);
0026 
0027 static int crypto_sha256_neon_update(struct shash_desc *desc, const u8 *data,
0028                      unsigned int len)
0029 {
0030     struct sha256_state *sctx = shash_desc_ctx(desc);
0031 
0032     if (!crypto_simd_usable() ||
0033         (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
0034         return crypto_sha256_arm_update(desc, data, len);
0035 
0036     kernel_neon_begin();
0037     sha256_base_do_update(desc, data, len,
0038             (sha256_block_fn *)sha256_block_data_order_neon);
0039     kernel_neon_end();
0040 
0041     return 0;
0042 }
0043 
0044 static int crypto_sha256_neon_finup(struct shash_desc *desc, const u8 *data,
0045                     unsigned int len, u8 *out)
0046 {
0047     if (!crypto_simd_usable())
0048         return crypto_sha256_arm_finup(desc, data, len, out);
0049 
0050     kernel_neon_begin();
0051     if (len)
0052         sha256_base_do_update(desc, data, len,
0053             (sha256_block_fn *)sha256_block_data_order_neon);
0054     sha256_base_do_finalize(desc,
0055             (sha256_block_fn *)sha256_block_data_order_neon);
0056     kernel_neon_end();
0057 
0058     return sha256_base_finish(desc, out);
0059 }
0060 
0061 static int crypto_sha256_neon_final(struct shash_desc *desc, u8 *out)
0062 {
0063     return crypto_sha256_neon_finup(desc, NULL, 0, out);
0064 }
0065 
0066 struct shash_alg sha256_neon_algs[] = { {
0067     .digestsize =   SHA256_DIGEST_SIZE,
0068     .init       =   sha256_base_init,
0069     .update     =   crypto_sha256_neon_update,
0070     .final      =   crypto_sha256_neon_final,
0071     .finup      =   crypto_sha256_neon_finup,
0072     .descsize   =   sizeof(struct sha256_state),
0073     .base       =   {
0074         .cra_name   =   "sha256",
0075         .cra_driver_name =  "sha256-neon",
0076         .cra_priority   =   250,
0077         .cra_blocksize  =   SHA256_BLOCK_SIZE,
0078         .cra_module =   THIS_MODULE,
0079     }
0080 }, {
0081     .digestsize =   SHA224_DIGEST_SIZE,
0082     .init       =   sha224_base_init,
0083     .update     =   crypto_sha256_neon_update,
0084     .final      =   crypto_sha256_neon_final,
0085     .finup      =   crypto_sha256_neon_finup,
0086     .descsize   =   sizeof(struct sha256_state),
0087     .base       =   {
0088         .cra_name   =   "sha224",
0089         .cra_driver_name =  "sha224-neon",
0090         .cra_priority   =   250,
0091         .cra_blocksize  =   SHA224_BLOCK_SIZE,
0092         .cra_module =   THIS_MODULE,
0093     }
0094 } };