|
14 | 14 |
|
15 | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
16 | 16 |
|
| 17 | +#include <asm/elf.h> |
| 18 | +#include <asm/pstate.h> |
17 | 19 | #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> |
22 | 20 | #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> |
26 | 26 |
|
27 | 27 | #include "opcodes.h"
|
28 | 28 |
|
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, |
30 | 35 | unsigned int rounds);
|
31 | 36 |
|
32 | 37 | static int md5_sparc64_init(struct shash_desc *desc)
|
33 | 38 | {
|
34 |
| - struct md5_state *mctx = shash_desc_ctx(desc); |
| 39 | + struct sparc_md5_state *mctx = shash_desc_ctx(desc); |
35 | 40 |
|
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); |
41 | 45 | mctx->byte_count = 0;
|
42 | 46 |
|
43 | 47 | return 0;
|
44 | 48 | }
|
45 | 49 |
|
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 |
| - |
67 | 50 | static int md5_sparc64_update(struct shash_desc *desc, const u8 *data,
|
68 | 51 | unsigned int len)
|
69 | 52 | {
|
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); |
79 | 54 |
|
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); |
81 | 58 | }
|
82 | 59 |
|
83 | 60 | /* 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) |
85 | 63 | {
|
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)); |
106 | 79 |
|
107 | 80 | /* Store state in digest */
|
108 | 81 | for (i = 0; i < MD5_HASH_WORDS; i++)
|
109 | 82 | dst[i] = sctx->hash[i];
|
110 | 83 |
|
111 |
| - /* Wipe context */ |
112 |
| - memset(sctx, 0, sizeof(*sctx)); |
113 |
| - |
114 | 84 | return 0;
|
115 | 85 | }
|
116 | 86 |
|
117 | 87 | static int md5_sparc64_export(struct shash_desc *desc, void *out)
|
118 | 88 | {
|
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; |
122 | 96 |
|
| 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); |
123 | 100 | return 0;
|
124 | 101 | }
|
125 | 102 |
|
126 | 103 | static int md5_sparc64_import(struct shash_desc *desc, const void *in)
|
127 | 104 | {
|
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; |
131 | 112 |
|
| 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); |
132 | 116 | return 0;
|
133 | 117 | }
|
134 | 118 |
|
135 | 119 | static struct shash_alg alg = {
|
136 | 120 | .digestsize = MD5_DIGEST_SIZE,
|
137 | 121 | .init = md5_sparc64_init,
|
138 | 122 | .update = md5_sparc64_update,
|
139 |
| - .final = md5_sparc64_final, |
| 123 | + .finup = md5_sparc64_finup, |
140 | 124 | .export = md5_sparc64_export,
|
141 | 125 | .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), |
144 | 128 | .base = {
|
145 | 129 | .cra_name = "md5",
|
146 | 130 | .cra_driver_name= "md5-sparc64",
|
147 | 131 | .cra_priority = SPARC_CR_OPCODE_PRIORITY,
|
| 132 | + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, |
148 | 133 | .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
|
149 | 134 | .cra_module = THIS_MODULE,
|
150 | 135 | }
|
|
0 commit comments