2 * Glue code for MD5 implementation for PPC assembler
4 * Based on generic implementation.
6 * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
15 #include <crypto/internal/hash.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
19 #include <linux/cryptohash.h>
20 #include <linux/types.h>
21 #include <crypto/md5.h>
22 #include <asm/byteorder.h>
24 extern void ppc_md5_transform(u32 *state, const u8 *src, u32 blocks);
26 static inline void ppc_md5_clear_context(struct md5_state *sctx)
28 int count = sizeof(struct md5_state) >> 2;
29 u32 *ptr = (u32 *)sctx;
31 /* make sure we can clear the fast way */
32 BUILD_BUG_ON(sizeof(struct md5_state) % 4);
33 do { *ptr++ = 0; } while (--count);
36 static int ppc_md5_init(struct shash_desc *desc)
38 struct md5_state *sctx = shash_desc_ctx(desc);
40 sctx->hash[0] = MD5_H0;
41 sctx->hash[1] = MD5_H1;
42 sctx->hash[2] = MD5_H2;
43 sctx->hash[3] = MD5_H3;
49 static int ppc_md5_update(struct shash_desc *desc, const u8 *data,
52 struct md5_state *sctx = shash_desc_ctx(desc);
53 const unsigned int offset = sctx->byte_count & 0x3f;
54 unsigned int avail = 64 - offset;
57 sctx->byte_count += len;
60 memcpy((char *)sctx->block + offset, src, len);
65 memcpy((char *)sctx->block + offset, src, avail);
66 ppc_md5_transform(sctx->hash, (const u8 *)sctx->block, 1);
72 ppc_md5_transform(sctx->hash, src, len >> 6);
77 memcpy((char *)sctx->block, src, len);
81 static int ppc_md5_final(struct shash_desc *desc, u8 *out)
83 struct md5_state *sctx = shash_desc_ctx(desc);
84 const unsigned int offset = sctx->byte_count & 0x3f;
85 const u8 *src = (const u8 *)sctx->block;
86 u8 *p = (u8 *)src + offset;
87 int padlen = 55 - offset;
88 __le64 *pbits = (__le64 *)((char *)sctx->block + 56);
89 __le32 *dst = (__le32 *)out;
94 memset(p, 0x00, padlen + sizeof (u64));
95 ppc_md5_transform(sctx->hash, src, 1);
96 p = (char *)sctx->block;
100 memset(p, 0, padlen);
101 *pbits = cpu_to_le64(sctx->byte_count << 3);
102 ppc_md5_transform(sctx->hash, src, 1);
104 dst[0] = cpu_to_le32(sctx->hash[0]);
105 dst[1] = cpu_to_le32(sctx->hash[1]);
106 dst[2] = cpu_to_le32(sctx->hash[2]);
107 dst[3] = cpu_to_le32(sctx->hash[3]);
109 ppc_md5_clear_context(sctx);
113 static int ppc_md5_export(struct shash_desc *desc, void *out)
115 struct md5_state *sctx = shash_desc_ctx(desc);
117 memcpy(out, sctx, sizeof(*sctx));
121 static int ppc_md5_import(struct shash_desc *desc, const void *in)
123 struct md5_state *sctx = shash_desc_ctx(desc);
125 memcpy(sctx, in, sizeof(*sctx));
129 static struct shash_alg alg = {
130 .digestsize = MD5_DIGEST_SIZE,
131 .init = ppc_md5_init,
132 .update = ppc_md5_update,
133 .final = ppc_md5_final,
134 .export = ppc_md5_export,
135 .import = ppc_md5_import,
136 .descsize = sizeof(struct md5_state),
137 .statesize = sizeof(struct md5_state),
140 .cra_driver_name= "md5-ppc",
142 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
143 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
144 .cra_module = THIS_MODULE,
148 static int __init ppc_md5_mod_init(void)
150 return crypto_register_shash(&alg);
153 static void __exit ppc_md5_mod_fini(void)
155 crypto_unregister_shash(&alg);
158 module_init(ppc_md5_mod_init);
159 module_exit(ppc_md5_mod_fini);
161 MODULE_LICENSE("GPL");
162 MODULE_DESCRIPTION("MD5 Secure Hash Algorithm, PPC assembler");
164 MODULE_ALIAS_CRYPTO("md5");
165 MODULE_ALIAS_CRYPTO("md5-ppc");