2 * sha1-ce-glue.c - SHA-1 secure hash 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 <linux/crypto.h>
14 #include <linux/module.h>
16 #include <asm/crypto/sha1.h>
17 #include <asm/hwcap.h>
20 #include <asm/unaligned.h>
22 MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
23 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24 MODULE_LICENSE("GPL v2");
26 asmlinkage void sha1_ce_transform(int blocks, u8 const *src, u32 *state,
29 static int sha1_init(struct shash_desc *desc)
31 struct sha1_state *sctx = shash_desc_ctx(desc);
33 *sctx = (struct sha1_state){
34 .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
39 static int sha1_update(struct shash_desc *desc, const u8 *data,
42 struct sha1_state *sctx = shash_desc_ctx(desc);
46 return sha1_update_arm(desc, data, len);
48 partial = sctx->count % SHA1_BLOCK_SIZE;
51 if ((partial + len) >= SHA1_BLOCK_SIZE) {
55 int p = SHA1_BLOCK_SIZE - partial;
57 memcpy(sctx->buffer + partial, data, p);
62 blocks = len / SHA1_BLOCK_SIZE;
63 len %= SHA1_BLOCK_SIZE;
66 sha1_ce_transform(blocks, data, sctx->state,
67 partial ? sctx->buffer : NULL);
70 data += blocks * SHA1_BLOCK_SIZE;
74 memcpy(sctx->buffer + partial, data, len);
78 static int sha1_final(struct shash_desc *desc, u8 *out)
80 static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
82 struct sha1_state *sctx = shash_desc_ctx(desc);
83 __be64 bits = cpu_to_be64(sctx->count << 3);
84 __be32 *dst = (__be32 *)out;
87 u32 padlen = SHA1_BLOCK_SIZE
88 - ((sctx->count + sizeof(bits)) % SHA1_BLOCK_SIZE);
90 sha1_update(desc, padding, padlen);
91 sha1_update(desc, (const u8 *)&bits, sizeof(bits));
93 for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
94 put_unaligned_be32(sctx->state[i], dst++);
96 *sctx = (struct sha1_state){};
100 static int sha1_export(struct shash_desc *desc, void *out)
102 struct sha1_state *sctx = shash_desc_ctx(desc);
103 struct sha1_state *dst = out;
109 static int sha1_import(struct shash_desc *desc, const void *in)
111 struct sha1_state *sctx = shash_desc_ctx(desc);
112 struct sha1_state const *src = in;
118 static struct shash_alg alg = {
120 .update = sha1_update,
122 .export = sha1_export,
123 .import = sha1_import,
124 .descsize = sizeof(struct sha1_state),
125 .digestsize = SHA1_DIGEST_SIZE,
126 .statesize = sizeof(struct sha1_state),
129 .cra_driver_name = "sha1-ce",
131 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
132 .cra_blocksize = SHA1_BLOCK_SIZE,
133 .cra_module = THIS_MODULE,
137 static int __init sha1_ce_mod_init(void)
139 if (!(elf_hwcap2 & HWCAP2_SHA1))
141 return crypto_register_shash(&alg);
144 static void __exit sha1_ce_mod_fini(void)
146 crypto_unregister_shash(&alg);
149 module_init(sha1_ce_mod_init);
150 module_exit(sha1_ce_mod_fini);