Skip to content

Commit 07eb54d

Browse files
Ard Biesheuvelherbertx
authored andcommitted
crypto: arm64/sha1-ce - move SHA-1 ARMv8 implementation to base layer
This removes all the boilerplate from the existing implementation, and replaces it with calls into the base layer. Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 9205b94 commit 07eb54d

2 files changed

Lines changed: 59 additions & 125 deletions

File tree

‎arch/arm64/crypto/sha1-ce-core.S‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@
6666
.word 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6
6767

6868
/*
69-
* void sha1_ce_transform(int blocks, u8 const *src, u32 *state,
70-
* u8 *head, long bytes)
69+
* void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
70+
* int blocks)
7171
*/
7272
ENTRY(sha1_ce_transform)
7373
/* load round constants */
@@ -78,25 +78,22 @@ ENTRY(sha1_ce_transform)
7878
ld1r {k3.4s}, [x6]
7979

8080
/* load state */
81-
ldr dga, [x2]
82-
ldr dgb, [x2, #16]
81+
ldr dga, [x0]
82+
ldr dgb, [x0, #16]
8383

84-
/* load partial state (if supplied) */
85-
cbz x3, 0f
86-
ld1 {v8.4s-v11.4s}, [x3]
87-
b 1f
84+
/* load sha1_ce_state::finalize */
85+
ldr w4, [x0, #:lo12:sha1_ce_offsetof_finalize]
8886

8987
/* load input */
9088
0: ld1 {v8.4s-v11.4s}, [x1], #64
91-
sub w0, w0, #1
89+
sub w2, w2, #1
9290

93-
1:
9491
CPU_LE( rev32 v8.16b, v8.16b )
9592
CPU_LE( rev32 v9.16b, v9.16b )
9693
CPU_LE( rev32 v10.16b, v10.16b )
9794
CPU_LE( rev32 v11.16b, v11.16b )
9895

99-
2: add t0.4s, v8.4s, k0.4s
96+
1: add t0.4s, v8.4s, k0.4s
10097
mov dg0v.16b, dgav.16b
10198

10299
add_update c, ev, k0, 8, 9, 10, 11, dgb
@@ -127,15 +124,15 @@ CPU_LE( rev32 v11.16b, v11.16b )
127124
add dgbv.2s, dgbv.2s, dg1v.2s
128125
add dgav.4s, dgav.4s, dg0v.4s
129126

130-
cbnz w0, 0b
127+
cbnz w2, 0b
131128

132129
/*
133130
* Final block: add padding and total bit count.
134-
* Skip if we have no total byte count in x4. In that case, the input
135-
* size was not a round multiple of the block size, and the padding is
136-
* handled by the C code.
131+
* Skip if the input size was not a round multiple of the block size,
132+
* the padding is handled by the C code in that case.
137133
*/
138134
cbz x4, 3f
135+
ldr x4, [x0, #:lo12:sha1_ce_offsetof_count]
139136
movi v9.2d, #0
140137
mov x8, #0x80000000
141138
movi v10.2d, #0
@@ -144,10 +141,10 @@ CPU_LE( rev32 v11.16b, v11.16b )
144141
mov x4, #0
145142
mov v11.d[0], xzr
146143
mov v11.d[1], x7
147-
b 2b
144+
b 1b
148145

149146
/* store new state */
150-
3: str dga, [x2]
151-
str dgb, [x2, #16]
147+
3: str dga, [x0]
148+
str dgb, [x0, #16]
152149
ret
153150
ENDPROC(sha1_ce_transform)

‎arch/arm64/crypto/sha1-ce-glue.c‎

Lines changed: 44 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -12,144 +12,81 @@
1212
#include <asm/unaligned.h>
1313
#include <crypto/internal/hash.h>
1414
#include <crypto/sha.h>
15+
#include <crypto/sha1_base.h>
1516
#include <linux/cpufeature.h>
1617
#include <linux/crypto.h>
1718
#include <linux/module.h>
1819

20+
#define ASM_EXPORT(sym, val) \
21+
asm(".globl " #sym "; .set " #sym ", %0" :: "I"(val));
22+
1923
MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
2024
MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
2125
MODULE_LICENSE("GPL v2");
2226

23-
asmlinkage void sha1_ce_transform(int blocks, u8 const *src, u32 *state,
24-
u8 *head, long bytes);
27+
struct sha1_ce_state {
28+
struct sha1_state sst;
29+
u32 finalize;
30+
};
2531

26-
static int sha1_init(struct shash_desc *desc)
27-
{
28-
struct sha1_state *sctx = shash_desc_ctx(desc);
32+
asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
33+
int blocks);
2934

30-
*sctx = (struct sha1_state){
31-
.state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
32-
};
33-
return 0;
34-
}
35-
36-
static int sha1_update(struct shash_desc *desc, const u8 *data,
37-
unsigned int len)
35+
static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
36+
unsigned int len)
3837
{
39-
struct sha1_state *sctx = shash_desc_ctx(desc);
40-
unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
41-
42-
sctx->count += len;
43-
44-
if ((partial + len) >= SHA1_BLOCK_SIZE) {
45-
int blocks;
46-
47-
if (partial) {
48-
int p = SHA1_BLOCK_SIZE - partial;
38+
struct sha1_ce_state *sctx = shash_desc_ctx(desc);
4939

50-
memcpy(sctx->buffer + partial, data, p);
51-
data += p;
52-
len -= p;
53-
}
54-
55-
blocks = len / SHA1_BLOCK_SIZE;
56-
len %= SHA1_BLOCK_SIZE;
57-
58-
kernel_neon_begin_partial(16);
59-
sha1_ce_transform(blocks, data, sctx->state,
60-
partial ? sctx->buffer : NULL, 0);
61-
kernel_neon_end();
40+
sctx->finalize = 0;
41+
kernel_neon_begin_partial(16);
42+
sha1_base_do_update(desc, data, len,
43+
(sha1_block_fn *)sha1_ce_transform);
44+
kernel_neon_end();
6245

63-
data += blocks * SHA1_BLOCK_SIZE;
64-
partial = 0;
65-
}
66-
if (len)
67-
memcpy(sctx->buffer + partial, data, len);
6846
return 0;
6947
}
7048

71-
static int sha1_final(struct shash_desc *desc, u8 *out)
49+
static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
50+
unsigned int len, u8 *out)
7251
{
73-
static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
52+
struct sha1_ce_state *sctx = shash_desc_ctx(desc);
53+
bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE);
7454

75-
struct sha1_state *sctx = shash_desc_ctx(desc);
76-
__be64 bits = cpu_to_be64(sctx->count << 3);
77-
__be32 *dst = (__be32 *)out;
78-
int i;
79-
80-
u32 padlen = SHA1_BLOCK_SIZE
81-
- ((sctx->count + sizeof(bits)) % SHA1_BLOCK_SIZE);
82-
83-
sha1_update(desc, padding, padlen);
84-
sha1_update(desc, (const u8 *)&bits, sizeof(bits));
85-
86-
for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
87-
put_unaligned_be32(sctx->state[i], dst++);
88-
89-
*sctx = (struct sha1_state){};
90-
return 0;
91-
}
92-
93-
static int sha1_finup(struct shash_desc *desc, const u8 *data,
94-
unsigned int len, u8 *out)
95-
{
96-
struct sha1_state *sctx = shash_desc_ctx(desc);
97-
__be32 *dst = (__be32 *)out;
98-
int blocks;
99-
int i;
100-
101-
if (sctx->count || !len || (len % SHA1_BLOCK_SIZE)) {
102-
sha1_update(desc, data, len);
103-
return sha1_final(desc, out);
104-
}
55+
ASM_EXPORT(sha1_ce_offsetof_count,
56+
offsetof(struct sha1_ce_state, sst.count));
57+
ASM_EXPORT(sha1_ce_offsetof_finalize,
58+
offsetof(struct sha1_ce_state, finalize));
10559

10660
/*
107-
* Use a fast path if the input is a multiple of 64 bytes. In
108-
* this case, there is no need to copy data around, and we can
109-
* perform the entire digest calculation in a single invocation
110-
* of sha1_ce_transform()
61+
* Allow the asm code to perform the finalization if there is no
62+
* partial data and the input is a round multiple of the block size.
11163
*/
112-
blocks = len / SHA1_BLOCK_SIZE;
64+
sctx->finalize = finalize;
11365

11466
kernel_neon_begin_partial(16);
115-
sha1_ce_transform(blocks, data, sctx->state, NULL, len);
67+
sha1_base_do_update(desc, data, len,
68+
(sha1_block_fn *)sha1_ce_transform);
69+
if (!finalize)
70+
sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
11671
kernel_neon_end();
117-
118-
for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
119-
put_unaligned_be32(sctx->state[i], dst++);
120-
121-
*sctx = (struct sha1_state){};
122-
return 0;
72+
return sha1_base_finish(desc, out);
12373
}
12474

125-
static int sha1_export(struct shash_desc *desc, void *out)
75+
static int sha1_ce_final(struct shash_desc *desc, u8 *out)
12676
{
127-
struct sha1_state *sctx = shash_desc_ctx(desc);
128-
struct sha1_state *dst = out;
129-
130-
*dst = *sctx;
131-
return 0;
132-
}
133-
134-
static int sha1_import(struct shash_desc *desc, const void *in)
135-
{
136-
struct sha1_state *sctx = shash_desc_ctx(desc);
137-
struct sha1_state const *src = in;
138-
139-
*sctx = *src;
140-
return 0;
77+
kernel_neon_begin_partial(16);
78+
sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
79+
kernel_neon_end();
80+
return sha1_base_finish(desc, out);
14181
}
14282

14383
static struct shash_alg alg = {
144-
.init = sha1_init,
145-
.update = sha1_update,
146-
.final = sha1_final,
147-
.finup = sha1_finup,
148-
.export = sha1_export,
149-
.import = sha1_import,
150-
.descsize = sizeof(struct sha1_state),
84+
.init = sha1_base_init,
85+
.update = sha1_ce_update,
86+
.final = sha1_ce_final,
87+
.finup = sha1_ce_finup,
88+
.descsize = sizeof(struct sha1_ce_state),
15189
.digestsize = SHA1_DIGEST_SIZE,
152-
.statesize = sizeof(struct sha1_state),
15390
.base = {
15491
.cra_name = "sha1",
15592
.cra_driver_name = "sha1-ce",

0 commit comments

Comments
 (0)