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