blob: 84f2a756588be55ed6842f1120a562a75c31e8bb [file] [log] [blame]
David McCulloughf0be44f2012-09-07 04:17:02 +08001/*
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>
Jussi Kivilinna60468252014-07-29 17:14:14 +010026#include <asm/crypto/sha1.h>
David McCulloughf0be44f2012-09-07 04:17:02 +080027
David McCulloughf0be44f2012-09-07 04:17:02 +080028
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +010029asmlinkage void sha1_block_data_order(u32 *digest,
David McCulloughf0be44f2012-09-07 04:17:02 +080030 const unsigned char *data, unsigned int rounds);
31
32
33static int sha1_init(struct shash_desc *desc)
34{
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +010035 struct sha1_state *sctx = shash_desc_ctx(desc);
36
37 *sctx = (struct sha1_state){
38 .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
39 };
40
David McCulloughf0be44f2012-09-07 04:17:02 +080041 return 0;
42}
43
44
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +010045static int __sha1_update(struct sha1_state *sctx, const u8 *data,
46 unsigned int len, unsigned int partial)
David McCulloughf0be44f2012-09-07 04:17:02 +080047{
48 unsigned int done = 0;
49
50 sctx->count += len;
51
52 if (partial) {
53 done = SHA1_BLOCK_SIZE - partial;
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +010054 memcpy(sctx->buffer + partial, data, done);
55 sha1_block_data_order(sctx->state, sctx->buffer, 1);
David McCulloughf0be44f2012-09-07 04:17:02 +080056 }
57
58 if (len - done >= SHA1_BLOCK_SIZE) {
59 const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +010060 sha1_block_data_order(sctx->state, data + done, rounds);
David McCulloughf0be44f2012-09-07 04:17:02 +080061 done += rounds * SHA1_BLOCK_SIZE;
62 }
63
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +010064 memcpy(sctx->buffer, data + done, len - done);
David McCulloughf0be44f2012-09-07 04:17:02 +080065 return 0;
66}
67
68
Jussi Kivilinna60468252014-07-29 17:14:14 +010069int sha1_update_arm(struct shash_desc *desc, const u8 *data,
70 unsigned int len)
David McCulloughf0be44f2012-09-07 04:17:02 +080071{
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +010072 struct sha1_state *sctx = shash_desc_ctx(desc);
David McCulloughf0be44f2012-09-07 04:17:02 +080073 unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
74 int res;
75
76 /* Handle the fast case right here */
77 if (partial + len < SHA1_BLOCK_SIZE) {
78 sctx->count += len;
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +010079 memcpy(sctx->buffer + partial, data, len);
David McCulloughf0be44f2012-09-07 04:17:02 +080080 return 0;
81 }
82 res = __sha1_update(sctx, data, len, partial);
83 return res;
84}
Jussi Kivilinna60468252014-07-29 17:14:14 +010085EXPORT_SYMBOL_GPL(sha1_update_arm);
David McCulloughf0be44f2012-09-07 04:17:02 +080086
87
88/* Add padding and return the message digest. */
89static int sha1_final(struct shash_desc *desc, u8 *out)
90{
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +010091 struct sha1_state *sctx = shash_desc_ctx(desc);
David McCulloughf0be44f2012-09-07 04:17:02 +080092 unsigned int i, index, padlen;
93 __be32 *dst = (__be32 *)out;
94 __be64 bits;
95 static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
96
97 bits = cpu_to_be64(sctx->count << 3);
98
99 /* Pad out to 56 mod 64 and append length */
100 index = sctx->count % SHA1_BLOCK_SIZE;
101 padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
102 /* We need to fill a whole block for __sha1_update() */
103 if (padlen <= 56) {
104 sctx->count += padlen;
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +0100105 memcpy(sctx->buffer + index, padding, padlen);
David McCulloughf0be44f2012-09-07 04:17:02 +0800106 } else {
107 __sha1_update(sctx, padding, padlen, index);
108 }
109 __sha1_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
110
111 /* Store state in digest */
112 for (i = 0; i < 5; i++)
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +0100113 dst[i] = cpu_to_be32(sctx->state[i]);
David McCulloughf0be44f2012-09-07 04:17:02 +0800114
115 /* Wipe context */
116 memset(sctx, 0, sizeof(*sctx));
117 return 0;
118}
119
120
121static int sha1_export(struct shash_desc *desc, void *out)
122{
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +0100123 struct sha1_state *sctx = shash_desc_ctx(desc);
David McCulloughf0be44f2012-09-07 04:17:02 +0800124 memcpy(out, sctx, sizeof(*sctx));
125 return 0;
126}
127
128
129static int sha1_import(struct shash_desc *desc, const void *in)
130{
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +0100131 struct sha1_state *sctx = shash_desc_ctx(desc);
David McCulloughf0be44f2012-09-07 04:17:02 +0800132 memcpy(sctx, in, sizeof(*sctx));
133 return 0;
134}
135
136
137static struct shash_alg alg = {
138 .digestsize = SHA1_DIGEST_SIZE,
139 .init = sha1_init,
Jussi Kivilinna60468252014-07-29 17:14:14 +0100140 .update = sha1_update_arm,
David McCulloughf0be44f2012-09-07 04:17:02 +0800141 .final = sha1_final,
142 .export = sha1_export,
143 .import = sha1_import,
Jussi Kivilinna1f8673d2014-07-29 17:14:09 +0100144 .descsize = sizeof(struct sha1_state),
145 .statesize = sizeof(struct sha1_state),
David McCulloughf0be44f2012-09-07 04:17:02 +0800146 .base = {
147 .cra_name = "sha1",
148 .cra_driver_name= "sha1-asm",
149 .cra_priority = 150,
150 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
151 .cra_blocksize = SHA1_BLOCK_SIZE,
152 .cra_module = THIS_MODULE,
153 }
154};
155
156
157static int __init sha1_mod_init(void)
158{
159 return crypto_register_shash(&alg);
160}
161
162
163static void __exit sha1_mod_fini(void)
164{
165 crypto_unregister_shash(&alg);
166}
167
168
169module_init(sha1_mod_init);
170module_exit(sha1_mod_fini);
171
172MODULE_LICENSE("GPL");
173MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm (ARM)");
174MODULE_ALIAS("sha1");
175MODULE_AUTHOR("David McCullough <ucdevel@gmail.com>");