1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Glue code for MD5 implementation for PPC assembler
5 * Based on generic implementation.
7 * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
10 #include <crypto/internal/hash.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <crypto/md5.h>
16 #include <asm/byteorder.h>
18 extern void ppc_md5_transform(u32
*state
, const u8
*src
, u32 blocks
);
20 static inline void ppc_md5_clear_context(struct md5_state
*sctx
)
22 int count
= sizeof(struct md5_state
) >> 2;
23 u32
*ptr
= (u32
*)sctx
;
25 /* make sure we can clear the fast way */
26 BUILD_BUG_ON(sizeof(struct md5_state
) % 4);
27 do { *ptr
++ = 0; } while (--count
);
30 static int ppc_md5_init(struct shash_desc
*desc
)
32 struct md5_state
*sctx
= shash_desc_ctx(desc
);
34 sctx
->hash
[0] = MD5_H0
;
35 sctx
->hash
[1] = MD5_H1
;
36 sctx
->hash
[2] = MD5_H2
;
37 sctx
->hash
[3] = MD5_H3
;
43 static int ppc_md5_update(struct shash_desc
*desc
, const u8
*data
,
46 struct md5_state
*sctx
= shash_desc_ctx(desc
);
47 const unsigned int offset
= sctx
->byte_count
& 0x3f;
48 unsigned int avail
= 64 - offset
;
51 sctx
->byte_count
+= len
;
54 memcpy((char *)sctx
->block
+ offset
, src
, len
);
59 memcpy((char *)sctx
->block
+ offset
, src
, avail
);
60 ppc_md5_transform(sctx
->hash
, (const u8
*)sctx
->block
, 1);
66 ppc_md5_transform(sctx
->hash
, src
, len
>> 6);
71 memcpy((char *)sctx
->block
, src
, len
);
75 static int ppc_md5_final(struct shash_desc
*desc
, u8
*out
)
77 struct md5_state
*sctx
= shash_desc_ctx(desc
);
78 const unsigned int offset
= sctx
->byte_count
& 0x3f;
79 const u8
*src
= (const u8
*)sctx
->block
;
80 u8
*p
= (u8
*)src
+ offset
;
81 int padlen
= 55 - offset
;
82 __le64
*pbits
= (__le64
*)((char *)sctx
->block
+ 56);
83 __le32
*dst
= (__le32
*)out
;
88 memset(p
, 0x00, padlen
+ sizeof (u64
));
89 ppc_md5_transform(sctx
->hash
, src
, 1);
90 p
= (char *)sctx
->block
;
95 *pbits
= cpu_to_le64(sctx
->byte_count
<< 3);
96 ppc_md5_transform(sctx
->hash
, src
, 1);
98 dst
[0] = cpu_to_le32(sctx
->hash
[0]);
99 dst
[1] = cpu_to_le32(sctx
->hash
[1]);
100 dst
[2] = cpu_to_le32(sctx
->hash
[2]);
101 dst
[3] = cpu_to_le32(sctx
->hash
[3]);
103 ppc_md5_clear_context(sctx
);
107 static int ppc_md5_export(struct shash_desc
*desc
, void *out
)
109 struct md5_state
*sctx
= shash_desc_ctx(desc
);
111 memcpy(out
, sctx
, sizeof(*sctx
));
115 static int ppc_md5_import(struct shash_desc
*desc
, const void *in
)
117 struct md5_state
*sctx
= shash_desc_ctx(desc
);
119 memcpy(sctx
, in
, sizeof(*sctx
));
123 static struct shash_alg alg
= {
124 .digestsize
= MD5_DIGEST_SIZE
,
125 .init
= ppc_md5_init
,
126 .update
= ppc_md5_update
,
127 .final
= ppc_md5_final
,
128 .export
= ppc_md5_export
,
129 .import
= ppc_md5_import
,
130 .descsize
= sizeof(struct md5_state
),
131 .statesize
= sizeof(struct md5_state
),
134 .cra_driver_name
= "md5-ppc",
136 .cra_blocksize
= MD5_HMAC_BLOCK_SIZE
,
137 .cra_module
= THIS_MODULE
,
141 static int __init
ppc_md5_mod_init(void)
143 return crypto_register_shash(&alg
);
146 static void __exit
ppc_md5_mod_fini(void)
148 crypto_unregister_shash(&alg
);
151 module_init(ppc_md5_mod_init
);
152 module_exit(ppc_md5_mod_fini
);
154 MODULE_LICENSE("GPL");
155 MODULE_DESCRIPTION("MD5 Secure Hash Algorithm, PPC assembler");
157 MODULE_ALIAS_CRYPTO("md5");
158 MODULE_ALIAS_CRYPTO("md5-ppc");