2 * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions
4 * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <crypto/internal/hash.h>
12 #include <crypto/sha.h>
13 #include <crypto/sha256_base.h>
14 #include <linux/crypto.h>
15 #include <linux/module.h>
17 #include <asm/hwcap.h>
20 #include <asm/unaligned.h>
22 #include "sha256_glue.h"
24 MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
25 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
26 MODULE_LICENSE("GPL v2");
28 asmlinkage
void sha2_ce_transform(struct sha256_state
*sst
, u8
const *src
,
31 static int sha2_ce_update(struct shash_desc
*desc
, const u8
*data
,
34 struct sha256_state
*sctx
= shash_desc_ctx(desc
);
36 if (!may_use_simd() ||
37 (sctx
->count
% SHA256_BLOCK_SIZE
) + len
< SHA256_BLOCK_SIZE
)
38 return crypto_sha256_arm_update(desc
, data
, len
);
41 sha256_base_do_update(desc
, data
, len
,
42 (sha256_block_fn
*)sha2_ce_transform
);
48 static int sha2_ce_finup(struct shash_desc
*desc
, const u8
*data
,
49 unsigned int len
, u8
*out
)
52 return crypto_sha256_arm_finup(desc
, data
, len
, out
);
56 sha256_base_do_update(desc
, data
, len
,
57 (sha256_block_fn
*)sha2_ce_transform
);
58 sha256_base_do_finalize(desc
, (sha256_block_fn
*)sha2_ce_transform
);
61 return sha256_base_finish(desc
, out
);
64 static int sha2_ce_final(struct shash_desc
*desc
, u8
*out
)
66 return sha2_ce_finup(desc
, NULL
, 0, out
);
69 static struct shash_alg algs
[] = { {
70 .init
= sha224_base_init
,
71 .update
= sha2_ce_update
,
72 .final
= sha2_ce_final
,
73 .finup
= sha2_ce_finup
,
74 .descsize
= sizeof(struct sha256_state
),
75 .digestsize
= SHA224_DIGEST_SIZE
,
78 .cra_driver_name
= "sha224-ce",
80 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
81 .cra_blocksize
= SHA256_BLOCK_SIZE
,
82 .cra_module
= THIS_MODULE
,
85 .init
= sha256_base_init
,
86 .update
= sha2_ce_update
,
87 .final
= sha2_ce_final
,
88 .finup
= sha2_ce_finup
,
89 .descsize
= sizeof(struct sha256_state
),
90 .digestsize
= SHA256_DIGEST_SIZE
,
93 .cra_driver_name
= "sha256-ce",
95 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
96 .cra_blocksize
= SHA256_BLOCK_SIZE
,
97 .cra_module
= THIS_MODULE
,
101 static int __init
sha2_ce_mod_init(void)
103 if (!(elf_hwcap2
& HWCAP2_SHA2
))
105 return crypto_register_shashes(algs
, ARRAY_SIZE(algs
));
108 static void __exit
sha2_ce_mod_fini(void)
110 crypto_unregister_shashes(algs
, ARRAY_SIZE(algs
));
113 module_init(sha2_ce_mod_init
);
114 module_exit(sha2_ce_mod_fini
);