blob: a9dd90df9fd762e984f7207c5af887d5cf0d35e5 [file] [log] [blame]
Ard Biesheuvel864cbee2015-03-10 09:47:45 +01001/*
2 * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
3 *
4 * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
5 *
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.
9 */
10
11#include <crypto/internal/hash.h>
12#include <crypto/sha.h>
13#include <linux/crypto.h>
14#include <linux/module.h>
15
16#include <asm/crypto/sha1.h>
17#include <asm/hwcap.h>
18#include <asm/neon.h>
19#include <asm/simd.h>
20#include <asm/unaligned.h>
21
22MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
23MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24MODULE_LICENSE("GPL v2");
25
26asmlinkage void sha1_ce_transform(int blocks, u8 const *src, u32 *state,
27 u8 *head);
28
29static int sha1_init(struct shash_desc *desc)
30{
31 struct sha1_state *sctx = shash_desc_ctx(desc);
32
33 *sctx = (struct sha1_state){
34 .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
35 };
36 return 0;
37}
38
39static int sha1_update(struct shash_desc *desc, const u8 *data,
40 unsigned int len)
41{
42 struct sha1_state *sctx = shash_desc_ctx(desc);
43 unsigned int partial;
44
45 if (!may_use_simd())
46 return sha1_update_arm(desc, data, len);
47
48 partial = sctx->count % SHA1_BLOCK_SIZE;
49 sctx->count += len;
50
51 if ((partial + len) >= SHA1_BLOCK_SIZE) {
52 int blocks;
53
54 if (partial) {
55 int p = SHA1_BLOCK_SIZE - partial;
56
57 memcpy(sctx->buffer + partial, data, p);
58 data += p;
59 len -= p;
60 }
61
62 blocks = len / SHA1_BLOCK_SIZE;
63 len %= SHA1_BLOCK_SIZE;
64
65 kernel_neon_begin();
66 sha1_ce_transform(blocks, data, sctx->state,
67 partial ? sctx->buffer : NULL);
68 kernel_neon_end();
69
70 data += blocks * SHA1_BLOCK_SIZE;
71 partial = 0;
72 }
73 if (len)
74 memcpy(sctx->buffer + partial, data, len);
75 return 0;
76}
77
78static int sha1_final(struct shash_desc *desc, u8 *out)
79{
80 static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
81
82 struct sha1_state *sctx = shash_desc_ctx(desc);
83 __be64 bits = cpu_to_be64(sctx->count << 3);
84 __be32 *dst = (__be32 *)out;
85 int i;
86
87 u32 padlen = SHA1_BLOCK_SIZE
88 - ((sctx->count + sizeof(bits)) % SHA1_BLOCK_SIZE);
89
90 sha1_update(desc, padding, padlen);
91 sha1_update(desc, (const u8 *)&bits, sizeof(bits));
92
93 for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
94 put_unaligned_be32(sctx->state[i], dst++);
95
96 *sctx = (struct sha1_state){};
97 return 0;
98}
99
100static int sha1_export(struct shash_desc *desc, void *out)
101{
102 struct sha1_state *sctx = shash_desc_ctx(desc);
103 struct sha1_state *dst = out;
104
105 *dst = *sctx;
106 return 0;
107}
108
109static int sha1_import(struct shash_desc *desc, const void *in)
110{
111 struct sha1_state *sctx = shash_desc_ctx(desc);
112 struct sha1_state const *src = in;
113
114 *sctx = *src;
115 return 0;
116}
117
118static struct shash_alg alg = {
119 .init = sha1_init,
120 .update = sha1_update,
121 .final = sha1_final,
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),
127 .base = {
128 .cra_name = "sha1",
129 .cra_driver_name = "sha1-ce",
130 .cra_priority = 200,
131 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
132 .cra_blocksize = SHA1_BLOCK_SIZE,
133 .cra_module = THIS_MODULE,
134 }
135};
136
137static int __init sha1_ce_mod_init(void)
138{
139 if (!(elf_hwcap2 & HWCAP2_SHA1))
140 return -ENODEV;
141 return crypto_register_shash(&alg);
142}
143
144static void __exit sha1_ce_mod_fini(void)
145{
146 crypto_unregister_shash(&alg);
147}
148
149module_init(sha1_ce_mod_init);
150module_exit(sha1_ce_mod_fini);