xref: /linux/arch/arm/crypto/sha1_glue.c (revision 1f8673d31a999ed7e20d9f66fcdad39e39f6b276)
1 /*
2  * Cryptographic API.
3  * Glue code for the SHA1 Secure Hash Algorithm assembler implementation
4  *
5  * This file is based on sha1_generic.c and sha1_ssse3_glue.c
6  *
7  * Copyright (c) Alan Smithee.
8  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
9  * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
10  * Copyright (c) Mathias Krause <minipli@googlemail.com>
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 2 of the License, or (at your option)
15  * any later version.
16  *
17  */
18 
19 #include <crypto/internal/hash.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/cryptohash.h>
23 #include <linux/types.h>
24 #include <crypto/sha.h>
25 #include <asm/byteorder.h>
26 
27 
28 asmlinkage void sha1_block_data_order(u32 *digest,
29 		const unsigned char *data, unsigned int rounds);
30 
31 
32 static int sha1_init(struct shash_desc *desc)
33 {
34 	struct sha1_state *sctx = shash_desc_ctx(desc);
35 
36 	*sctx = (struct sha1_state){
37 		.state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
38 	};
39 
40 	return 0;
41 }
42 
43 
44 static int __sha1_update(struct sha1_state *sctx, const u8 *data,
45 			 unsigned int len, unsigned int partial)
46 {
47 	unsigned int done = 0;
48 
49 	sctx->count += len;
50 
51 	if (partial) {
52 		done = SHA1_BLOCK_SIZE - partial;
53 		memcpy(sctx->buffer + partial, data, done);
54 		sha1_block_data_order(sctx->state, sctx->buffer, 1);
55 	}
56 
57 	if (len - done >= SHA1_BLOCK_SIZE) {
58 		const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
59 		sha1_block_data_order(sctx->state, data + done, rounds);
60 		done += rounds * SHA1_BLOCK_SIZE;
61 	}
62 
63 	memcpy(sctx->buffer, data + done, len - done);
64 	return 0;
65 }
66 
67 
68 static int sha1_update(struct shash_desc *desc, const u8 *data,
69 			     unsigned int len)
70 {
71 	struct sha1_state *sctx = shash_desc_ctx(desc);
72 	unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
73 	int res;
74 
75 	/* Handle the fast case right here */
76 	if (partial + len < SHA1_BLOCK_SIZE) {
77 		sctx->count += len;
78 		memcpy(sctx->buffer + partial, data, len);
79 		return 0;
80 	}
81 	res = __sha1_update(sctx, data, len, partial);
82 	return res;
83 }
84 
85 
86 /* Add padding and return the message digest. */
87 static int sha1_final(struct shash_desc *desc, u8 *out)
88 {
89 	struct sha1_state *sctx = shash_desc_ctx(desc);
90 	unsigned int i, index, padlen;
91 	__be32 *dst = (__be32 *)out;
92 	__be64 bits;
93 	static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
94 
95 	bits = cpu_to_be64(sctx->count << 3);
96 
97 	/* Pad out to 56 mod 64 and append length */
98 	index = sctx->count % SHA1_BLOCK_SIZE;
99 	padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
100 	/* We need to fill a whole block for __sha1_update() */
101 	if (padlen <= 56) {
102 		sctx->count += padlen;
103 		memcpy(sctx->buffer + index, padding, padlen);
104 	} else {
105 		__sha1_update(sctx, padding, padlen, index);
106 	}
107 	__sha1_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
108 
109 	/* Store state in digest */
110 	for (i = 0; i < 5; i++)
111 		dst[i] = cpu_to_be32(sctx->state[i]);
112 
113 	/* Wipe context */
114 	memset(sctx, 0, sizeof(*sctx));
115 	return 0;
116 }
117 
118 
119 static int sha1_export(struct shash_desc *desc, void *out)
120 {
121 	struct sha1_state *sctx = shash_desc_ctx(desc);
122 	memcpy(out, sctx, sizeof(*sctx));
123 	return 0;
124 }
125 
126 
127 static int sha1_import(struct shash_desc *desc, const void *in)
128 {
129 	struct sha1_state *sctx = shash_desc_ctx(desc);
130 	memcpy(sctx, in, sizeof(*sctx));
131 	return 0;
132 }
133 
134 
135 static struct shash_alg alg = {
136 	.digestsize	=	SHA1_DIGEST_SIZE,
137 	.init		=	sha1_init,
138 	.update		=	sha1_update,
139 	.final		=	sha1_final,
140 	.export		=	sha1_export,
141 	.import		=	sha1_import,
142 	.descsize	=	sizeof(struct sha1_state),
143 	.statesize	=	sizeof(struct sha1_state),
144 	.base		=	{
145 		.cra_name	=	"sha1",
146 		.cra_driver_name=	"sha1-asm",
147 		.cra_priority	=	150,
148 		.cra_flags	=	CRYPTO_ALG_TYPE_SHASH,
149 		.cra_blocksize	=	SHA1_BLOCK_SIZE,
150 		.cra_module	=	THIS_MODULE,
151 	}
152 };
153 
154 
155 static int __init sha1_mod_init(void)
156 {
157 	return crypto_register_shash(&alg);
158 }
159 
160 
161 static void __exit sha1_mod_fini(void)
162 {
163 	crypto_unregister_shash(&alg);
164 }
165 
166 
167 module_init(sha1_mod_init);
168 module_exit(sha1_mod_fini);
169 
170 MODULE_LICENSE("GPL");
171 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm (ARM)");
172 MODULE_ALIAS("sha1");
173 MODULE_AUTHOR("David McCullough <ucdevel@gmail.com>");
174