x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / arch / powerpc / crypto / sha1.c
blobc154cebc1041c4ac24d72c5e51b93bfec2f0d9ee
1 /*
2 * Cryptographic API.
4 * powerpc implementation of the SHA1 Secure Hash Algorithm.
6 * Derived from cryptoapi implementation, adapted for in-place
7 * scatterlist interface.
9 * Derived from "crypto/sha1.c"
10 * Copyright (c) Alan Smithee.
11 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
12 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
20 #include <crypto/internal/hash.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/mm.h>
24 #include <linux/cryptohash.h>
25 #include <linux/types.h>
26 #include <crypto/sha.h>
27 #include <asm/byteorder.h>
29 extern void powerpc_sha_transform(u32 *state, const u8 *src, u32 *temp);
31 static int sha1_init(struct shash_desc *desc)
33 struct sha1_state *sctx = shash_desc_ctx(desc);
35 *sctx = (struct sha1_state){
36 .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
39 return 0;
42 static int sha1_update(struct shash_desc *desc, const u8 *data,
43 unsigned int len)
45 struct sha1_state *sctx = shash_desc_ctx(desc);
46 unsigned int partial, done;
47 const u8 *src;
49 partial = sctx->count & 0x3f;
50 sctx->count += len;
51 done = 0;
52 src = data;
54 if ((partial + len) > 63) {
55 u32 temp[SHA_WORKSPACE_WORDS];
57 if (partial) {
58 done = -partial;
59 memcpy(sctx->buffer + partial, data, done + 64);
60 src = sctx->buffer;
63 do {
64 powerpc_sha_transform(sctx->state, src, temp);
65 done += 64;
66 src = data + done;
67 } while (done + 63 < len);
69 memzero_explicit(temp, sizeof(temp));
70 partial = 0;
72 memcpy(sctx->buffer + partial, src, len - done);
74 return 0;
78 /* Add padding and return the message digest. */
79 static int sha1_final(struct shash_desc *desc, u8 *out)
81 struct sha1_state *sctx = shash_desc_ctx(desc);
82 __be32 *dst = (__be32 *)out;
83 u32 i, index, padlen;
84 __be64 bits;
85 static const u8 padding[64] = { 0x80, };
87 bits = cpu_to_be64(sctx->count << 3);
89 /* Pad out to 56 mod 64 */
90 index = sctx->count & 0x3f;
91 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
92 sha1_update(desc, padding, padlen);
94 /* Append length */
95 sha1_update(desc, (const u8 *)&bits, sizeof(bits));
97 /* Store state in digest */
98 for (i = 0; i < 5; i++)
99 dst[i] = cpu_to_be32(sctx->state[i]);
101 /* Wipe context */
102 memset(sctx, 0, sizeof *sctx);
104 return 0;
107 static int sha1_export(struct shash_desc *desc, void *out)
109 struct sha1_state *sctx = shash_desc_ctx(desc);
111 memcpy(out, sctx, sizeof(*sctx));
112 return 0;
115 static int sha1_import(struct shash_desc *desc, const void *in)
117 struct sha1_state *sctx = shash_desc_ctx(desc);
119 memcpy(sctx, in, sizeof(*sctx));
120 return 0;
123 static struct shash_alg alg = {
124 .digestsize = SHA1_DIGEST_SIZE,
125 .init = sha1_init,
126 .update = sha1_update,
127 .final = sha1_final,
128 .export = sha1_export,
129 .import = sha1_import,
130 .descsize = sizeof(struct sha1_state),
131 .statesize = sizeof(struct sha1_state),
132 .base = {
133 .cra_name = "sha1",
134 .cra_driver_name= "sha1-powerpc",
135 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
136 .cra_blocksize = SHA1_BLOCK_SIZE,
137 .cra_module = THIS_MODULE,
141 static int __init sha1_powerpc_mod_init(void)
143 return crypto_register_shash(&alg);
146 static void __exit sha1_powerpc_mod_fini(void)
148 crypto_unregister_shash(&alg);
151 module_init(sha1_powerpc_mod_init);
152 module_exit(sha1_powerpc_mod_fini);
154 MODULE_LICENSE("GPL");
155 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
157 MODULE_ALIAS_CRYPTO("sha1");
158 MODULE_ALIAS_CRYPTO("sha1-powerpc");