Skip to content

Commit cc1f5bb

Browse files
committed
crypto: sparc/md5 - Use API partial block handling
Use the Crypto API partial block handling. Also switch to the generic export format. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 60fded5 commit cc1f5bb

File tree

1 file changed

+63
-78
lines changed

1 file changed

+63
-78
lines changed

arch/sparc/crypto/md5_glue.c

Lines changed: 63 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,137 +14,122 @@
1414

1515
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1616

17+
#include <asm/elf.h>
18+
#include <asm/pstate.h>
1719
#include <crypto/internal/hash.h>
18-
#include <linux/init.h>
19-
#include <linux/module.h>
20-
#include <linux/mm.h>
21-
#include <linux/types.h>
2220
#include <crypto/md5.h>
23-
24-
#include <asm/pstate.h>
25-
#include <asm/elf.h>
21+
#include <linux/errno.h>
22+
#include <linux/kernel.h>
23+
#include <linux/module.h>
24+
#include <linux/string.h>
25+
#include <linux/unaligned.h>
2626

2727
#include "opcodes.h"
2828

29-
asmlinkage void md5_sparc64_transform(u32 *digest, const char *data,
29+
struct sparc_md5_state {
30+
__le32 hash[MD5_HASH_WORDS];
31+
u64 byte_count;
32+
};
33+
34+
asmlinkage void md5_sparc64_transform(__le32 *digest, const char *data,
3035
unsigned int rounds);
3136

3237
static int md5_sparc64_init(struct shash_desc *desc)
3338
{
34-
struct md5_state *mctx = shash_desc_ctx(desc);
39+
struct sparc_md5_state *mctx = shash_desc_ctx(desc);
3540

36-
mctx->hash[0] = MD5_H0;
37-
mctx->hash[1] = MD5_H1;
38-
mctx->hash[2] = MD5_H2;
39-
mctx->hash[3] = MD5_H3;
40-
le32_to_cpu_array(mctx->hash, 4);
41+
mctx->hash[0] = cpu_to_le32(MD5_H0);
42+
mctx->hash[1] = cpu_to_le32(MD5_H1);
43+
mctx->hash[2] = cpu_to_le32(MD5_H2);
44+
mctx->hash[3] = cpu_to_le32(MD5_H3);
4145
mctx->byte_count = 0;
4246

4347
return 0;
4448
}
4549

46-
static void __md5_sparc64_update(struct md5_state *sctx, const u8 *data,
47-
unsigned int len, unsigned int partial)
48-
{
49-
unsigned int done = 0;
50-
51-
sctx->byte_count += len;
52-
if (partial) {
53-
done = MD5_HMAC_BLOCK_SIZE - partial;
54-
memcpy((u8 *)sctx->block + partial, data, done);
55-
md5_sparc64_transform(sctx->hash, (u8 *)sctx->block, 1);
56-
}
57-
if (len - done >= MD5_HMAC_BLOCK_SIZE) {
58-
const unsigned int rounds = (len - done) / MD5_HMAC_BLOCK_SIZE;
59-
60-
md5_sparc64_transform(sctx->hash, data + done, rounds);
61-
done += rounds * MD5_HMAC_BLOCK_SIZE;
62-
}
63-
64-
memcpy(sctx->block, data + done, len - done);
65-
}
66-
6750
static int md5_sparc64_update(struct shash_desc *desc, const u8 *data,
6851
unsigned int len)
6952
{
70-
struct md5_state *sctx = shash_desc_ctx(desc);
71-
unsigned int partial = sctx->byte_count % MD5_HMAC_BLOCK_SIZE;
72-
73-
/* Handle the fast case right here */
74-
if (partial + len < MD5_HMAC_BLOCK_SIZE) {
75-
sctx->byte_count += len;
76-
memcpy((u8 *)sctx->block + partial, data, len);
77-
} else
78-
__md5_sparc64_update(sctx, data, len, partial);
53+
struct sparc_md5_state *sctx = shash_desc_ctx(desc);
7954

80-
return 0;
55+
sctx->byte_count += round_down(len, MD5_HMAC_BLOCK_SIZE);
56+
md5_sparc64_transform(sctx->hash, data, len / MD5_HMAC_BLOCK_SIZE);
57+
return len - round_down(len, MD5_HMAC_BLOCK_SIZE);
8158
}
8259

8360
/* Add padding and return the message digest. */
84-
static int md5_sparc64_final(struct shash_desc *desc, u8 *out)
61+
static int md5_sparc64_finup(struct shash_desc *desc, const u8 *src,
62+
unsigned int offset, u8 *out)
8563
{
86-
struct md5_state *sctx = shash_desc_ctx(desc);
87-
unsigned int i, index, padlen;
88-
u32 *dst = (u32 *)out;
89-
__le64 bits;
90-
static const u8 padding[MD5_HMAC_BLOCK_SIZE] = { 0x80, };
91-
92-
bits = cpu_to_le64(sctx->byte_count << 3);
93-
94-
/* Pad out to 56 mod 64 and append length */
95-
index = sctx->byte_count % MD5_HMAC_BLOCK_SIZE;
96-
padlen = (index < 56) ? (56 - index) : ((MD5_HMAC_BLOCK_SIZE+56) - index);
97-
98-
/* We need to fill a whole block for __md5_sparc64_update() */
99-
if (padlen <= 56) {
100-
sctx->byte_count += padlen;
101-
memcpy((u8 *)sctx->block + index, padding, padlen);
102-
} else {
103-
__md5_sparc64_update(sctx, padding, padlen, index);
104-
}
105-
__md5_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
64+
struct sparc_md5_state *sctx = shash_desc_ctx(desc);
65+
__le64 block[MD5_BLOCK_WORDS] = {};
66+
u8 *p = memcpy(block, src, offset);
67+
__le32 *dst = (__le32 *)out;
68+
__le64 *pbits;
69+
int i;
70+
71+
src = p;
72+
p += offset;
73+
*p++ = 0x80;
74+
sctx->byte_count += offset;
75+
pbits = &block[(MD5_BLOCK_WORDS / (offset > 55 ? 1 : 2)) - 1];
76+
*pbits = cpu_to_le64(sctx->byte_count << 3);
77+
md5_sparc64_transform(sctx->hash, src, (pbits - block + 1) / 8);
78+
memzero_explicit(block, sizeof(block));
10679

10780
/* Store state in digest */
10881
for (i = 0; i < MD5_HASH_WORDS; i++)
10982
dst[i] = sctx->hash[i];
11083

111-
/* Wipe context */
112-
memset(sctx, 0, sizeof(*sctx));
113-
11484
return 0;
11585
}
11686

11787
static int md5_sparc64_export(struct shash_desc *desc, void *out)
11888
{
119-
struct md5_state *sctx = shash_desc_ctx(desc);
120-
121-
memcpy(out, sctx, sizeof(*sctx));
89+
struct sparc_md5_state *sctx = shash_desc_ctx(desc);
90+
union {
91+
u8 *u8;
92+
u32 *u32;
93+
u64 *u64;
94+
} p = { .u8 = out };
95+
int i;
12296

97+
for (i = 0; i < MD5_HASH_WORDS; i++)
98+
put_unaligned(le32_to_cpu(sctx->hash[i]), p.u32++);
99+
put_unaligned(sctx->byte_count, p.u64);
123100
return 0;
124101
}
125102

126103
static int md5_sparc64_import(struct shash_desc *desc, const void *in)
127104
{
128-
struct md5_state *sctx = shash_desc_ctx(desc);
129-
130-
memcpy(sctx, in, sizeof(*sctx));
105+
struct sparc_md5_state *sctx = shash_desc_ctx(desc);
106+
union {
107+
const u8 *u8;
108+
const u32 *u32;
109+
const u64 *u64;
110+
} p = { .u8 = in };
111+
int i;
131112

113+
for (i = 0; i < MD5_HASH_WORDS; i++)
114+
sctx->hash[i] = cpu_to_le32(get_unaligned(p.u32++));
115+
sctx->byte_count = get_unaligned(p.u64);
132116
return 0;
133117
}
134118

135119
static struct shash_alg alg = {
136120
.digestsize = MD5_DIGEST_SIZE,
137121
.init = md5_sparc64_init,
138122
.update = md5_sparc64_update,
139-
.final = md5_sparc64_final,
123+
.finup = md5_sparc64_finup,
140124
.export = md5_sparc64_export,
141125
.import = md5_sparc64_import,
142-
.descsize = sizeof(struct md5_state),
143-
.statesize = sizeof(struct md5_state),
126+
.descsize = sizeof(struct sparc_md5_state),
127+
.statesize = sizeof(struct sparc_md5_state),
144128
.base = {
145129
.cra_name = "md5",
146130
.cra_driver_name= "md5-sparc64",
147131
.cra_priority = SPARC_CR_OPCODE_PRIORITY,
132+
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
148133
.cra_blocksize = MD5_HMAC_BLOCK_SIZE,
149134
.cra_module = THIS_MODULE,
150135
}

0 commit comments

Comments
 (0)