powerpc: Hard wire PT_SOFTE value to 1 in ptrace & signals
[linux/fpc-iii.git] / arch / powerpc / crypto / md5-glue.c
blob92289679b4c47112d29b3753de84bc7cd1cb52b4
1 /*
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)
11 * any later version.
15 #include <crypto/internal/hash.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/mm.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;
44 sctx->byte_count = 0;
46 return 0;
49 static int ppc_md5_update(struct shash_desc *desc, const u8 *data,
50 unsigned int len)
52 struct md5_state *sctx = shash_desc_ctx(desc);
53 const unsigned int offset = sctx->byte_count & 0x3f;
54 unsigned int avail = 64 - offset;
55 const u8 *src = data;
57 sctx->byte_count += len;
59 if (avail > len) {
60 memcpy((char *)sctx->block + offset, src, len);
61 return 0;
64 if (offset) {
65 memcpy((char *)sctx->block + offset, src, avail);
66 ppc_md5_transform(sctx->hash, (const u8 *)sctx->block, 1);
67 len -= avail;
68 src += avail;
71 if (len > 63) {
72 ppc_md5_transform(sctx->hash, src, len >> 6);
73 src += len & ~0x3f;
74 len &= 0x3f;
77 memcpy((char *)sctx->block, src, len);
78 return 0;
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;
91 *p++ = 0x80;
93 if (padlen < 0) {
94 memset(p, 0x00, padlen + sizeof (u64));
95 ppc_md5_transform(sctx->hash, src, 1);
96 p = (char *)sctx->block;
97 padlen = 56;
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);
110 return 0;
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));
118 return 0;
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));
126 return 0;
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),
138 .base = {
139 .cra_name = "md5",
140 .cra_driver_name= "md5-ppc",
141 .cra_priority = 200,
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");