Skip to content

Commit efd62c8

Browse files
committed
crypto: md5-generic - Use API partial block handling
Use the Crypto API partial block handling. Signed-off-by: Herbert Xu <[email protected]>
1 parent 3942654 commit efd62c8

File tree

2 files changed

+40
-65
lines changed

2 files changed

+40
-65
lines changed

crypto/md5.c

Lines changed: 38 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
*/
1818
#include <crypto/internal/hash.h>
1919
#include <crypto/md5.h>
20-
#include <linux/init.h>
20+
#include <linux/kernel.h>
2121
#include <linux/module.h>
2222
#include <linux/string.h>
23-
#include <linux/types.h>
24-
#include <asm/byteorder.h>
2523

2624
const u8 md5_zero_message_hash[MD5_DIGEST_SIZE] = {
2725
0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
@@ -120,10 +118,11 @@ static void md5_transform(__u32 *hash, __u32 const *in)
120118
hash[3] += d;
121119
}
122120

123-
static inline void md5_transform_helper(struct md5_state *ctx)
121+
static inline void md5_transform_helper(struct md5_state *ctx,
122+
u32 block[MD5_BLOCK_WORDS])
124123
{
125-
le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
126-
md5_transform(ctx->hash, ctx->block);
124+
le32_to_cpu_array(block, MD5_BLOCK_WORDS);
125+
md5_transform(ctx->hash, block);
127126
}
128127

129128
static int md5_init(struct shash_desc *desc)
@@ -142,91 +141,66 @@ static int md5_init(struct shash_desc *desc)
142141
static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len)
143142
{
144143
struct md5_state *mctx = shash_desc_ctx(desc);
145-
const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
144+
u32 block[MD5_BLOCK_WORDS];
146145

147146
mctx->byte_count += len;
148-
149-
if (avail > len) {
150-
memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
151-
data, len);
152-
return 0;
153-
}
154-
155-
memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
156-
data, avail);
157-
158-
md5_transform_helper(mctx);
159-
data += avail;
160-
len -= avail;
161-
162-
while (len >= sizeof(mctx->block)) {
163-
memcpy(mctx->block, data, sizeof(mctx->block));
164-
md5_transform_helper(mctx);
165-
data += sizeof(mctx->block);
166-
len -= sizeof(mctx->block);
167-
}
168-
169-
memcpy(mctx->block, data, len);
170-
171-
return 0;
147+
do {
148+
memcpy(block, data, sizeof(block));
149+
md5_transform_helper(mctx, block);
150+
data += sizeof(block);
151+
len -= sizeof(block);
152+
} while (len >= sizeof(block));
153+
memzero_explicit(block, sizeof(block));
154+
mctx->byte_count -= len;
155+
return len;
172156
}
173157

174-
static int md5_final(struct shash_desc *desc, u8 *out)
158+
static int md5_finup(struct shash_desc *desc, const u8 *data, unsigned int len,
159+
u8 *out)
175160
{
176161
struct md5_state *mctx = shash_desc_ctx(desc);
177-
const unsigned int offset = mctx->byte_count & 0x3f;
178-
char *p = (char *)mctx->block + offset;
179-
int padding = 56 - (offset + 1);
162+
u32 block[MD5_BLOCK_WORDS];
163+
unsigned int offset;
164+
int padding;
165+
char *p;
166+
167+
memcpy(block, data, len);
168+
169+
offset = len;
170+
p = (char *)block + offset;
171+
padding = 56 - (offset + 1);
180172

181173
*p++ = 0x80;
182174
if (padding < 0) {
183175
memset(p, 0x00, padding + sizeof (u64));
184-
md5_transform_helper(mctx);
185-
p = (char *)mctx->block;
176+
md5_transform_helper(mctx, block);
177+
p = (char *)block;
186178
padding = 56;
187179
}
188180

189181
memset(p, 0, padding);
190-
mctx->block[14] = mctx->byte_count << 3;
191-
mctx->block[15] = mctx->byte_count >> 29;
192-
le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
193-
sizeof(u64)) / sizeof(u32));
194-
md5_transform(mctx->hash, mctx->block);
182+
mctx->byte_count += len;
183+
block[14] = mctx->byte_count << 3;
184+
block[15] = mctx->byte_count >> 29;
185+
le32_to_cpu_array(block, (sizeof(block) - sizeof(u64)) / sizeof(u32));
186+
md5_transform(mctx->hash, block);
187+
memzero_explicit(block, sizeof(block));
195188
cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
196189
memcpy(out, mctx->hash, sizeof(mctx->hash));
197-
memset(mctx, 0, sizeof(*mctx));
198-
199-
return 0;
200-
}
201-
202-
static int md5_export(struct shash_desc *desc, void *out)
203-
{
204-
struct md5_state *ctx = shash_desc_ctx(desc);
205-
206-
memcpy(out, ctx, sizeof(*ctx));
207-
return 0;
208-
}
209-
210-
static int md5_import(struct shash_desc *desc, const void *in)
211-
{
212-
struct md5_state *ctx = shash_desc_ctx(desc);
213190

214-
memcpy(ctx, in, sizeof(*ctx));
215191
return 0;
216192
}
217193

218194
static struct shash_alg alg = {
219195
.digestsize = MD5_DIGEST_SIZE,
220196
.init = md5_init,
221197
.update = md5_update,
222-
.final = md5_final,
223-
.export = md5_export,
224-
.import = md5_import,
225-
.descsize = sizeof(struct md5_state),
226-
.statesize = sizeof(struct md5_state),
198+
.finup = md5_finup,
199+
.descsize = MD5_STATE_SIZE,
227200
.base = {
228201
.cra_name = "md5",
229202
.cra_driver_name = "md5-generic",
203+
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
230204
.cra_blocksize = MD5_HMAC_BLOCK_SIZE,
231205
.cra_module = THIS_MODULE,
232206
}

include/crypto/md5.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define MD5_HMAC_BLOCK_SIZE 64
99
#define MD5_BLOCK_WORDS 16
1010
#define MD5_HASH_WORDS 4
11+
#define MD5_STATE_SIZE 24
1112

1213
#define MD5_H0 0x67452301UL
1314
#define MD5_H1 0xefcdab89UL
@@ -18,8 +19,8 @@ extern const u8 md5_zero_message_hash[MD5_DIGEST_SIZE];
1819

1920
struct md5_state {
2021
u32 hash[MD5_HASH_WORDS];
21-
u32 block[MD5_BLOCK_WORDS];
2222
u64 byte_count;
23+
u32 block[MD5_BLOCK_WORDS];
2324
};
2425

2526
#endif

0 commit comments

Comments
 (0)