1 /*
2 * Copyright (c) 2015 South Silicon Valley Microelectronics Inc.
3 * Copyright (c) 2015 iComm Corporation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include <crypto/internal/hash.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/cryptohash.h>
21 #include <linux/types.h>
22 #include <crypto/sha.h>
23 #include <asm/byteorder.h>
24 struct SHA1_CTX {
25 uint32_t h0,h1,h2,h3,h4;
26 u64 count;
27 u8 data[SHA1_BLOCK_SIZE];
28 };
29 asmlinkage void sha1_block_data_order(struct SHA1_CTX *digest,
30 const unsigned char *data, unsigned int rounds);
sha1_init(struct shash_desc * desc)31 static int sha1_init(struct shash_desc *desc)
32 {
33 struct SHA1_CTX *sctx = shash_desc_ctx(desc);
34 memset(sctx, 0, sizeof(*sctx));
35 sctx->h0 = SHA1_H0;
36 sctx->h1 = SHA1_H1;
37 sctx->h2 = SHA1_H2;
38 sctx->h3 = SHA1_H3;
39 sctx->h4 = SHA1_H4;
40 return 0;
41 }
__sha1_update(struct SHA1_CTX * sctx,const u8 * data,unsigned int len,unsigned int partial)42 static int __sha1_update(struct SHA1_CTX *sctx, const u8 *data,
43 unsigned int len, unsigned int partial)
44 {
45 unsigned int done = 0;
46 sctx->count += len;
47 if (partial) {
48 done = SHA1_BLOCK_SIZE - partial;
49 memcpy(sctx->data + partial, data, done);
50 sha1_block_data_order(sctx, sctx->data, 1);
51 }
52 if (len - done >= SHA1_BLOCK_SIZE) {
53 const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
54 sha1_block_data_order(sctx, data + done, rounds);
55 done += rounds * SHA1_BLOCK_SIZE;
56 }
57 memcpy(sctx->data, data + done, len - done);
58 return 0;
59 }
sha1_update(struct shash_desc * desc,const u8 * data,unsigned int len)60 static int sha1_update(struct shash_desc *desc, const u8 *data,
61 unsigned int len)
62 {
63 struct SHA1_CTX *sctx = shash_desc_ctx(desc);
64 unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
65 int res;
66 if (partial + len < SHA1_BLOCK_SIZE) {
67 sctx->count += len;
68 memcpy(sctx->data + partial, data, len);
69 return 0;
70 }
71 res = __sha1_update(sctx, data, len, partial);
72 return res;
73 }
sha1_final(struct shash_desc * desc,u8 * out)74 static int sha1_final(struct shash_desc *desc, u8 *out)
75 {
76 struct SHA1_CTX *sctx = shash_desc_ctx(desc);
77 unsigned int i, index, padlen;
78 __be32 *dst = (__be32 *)out;
79 __be64 bits;
80 static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
81 bits = cpu_to_be64(sctx->count << 3);
82 index = sctx->count % SHA1_BLOCK_SIZE;
83 padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
84 if (padlen <= 56) {
85 sctx->count += padlen;
86 memcpy(sctx->data + index, padding, padlen);
87 } else {
88 __sha1_update(sctx, padding, padlen, index);
89 }
90 __sha1_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
91 for (i = 0; i < 5; i++)
92 dst[i] = cpu_to_be32(((u32 *)sctx)[i]);
93 memset(sctx, 0, sizeof(*sctx));
94 return 0;
95 }
sha1_export(struct shash_desc * desc,void * out)96 static int sha1_export(struct shash_desc *desc, void *out)
97 {
98 struct SHA1_CTX *sctx = shash_desc_ctx(desc);
99 memcpy(out, sctx, sizeof(*sctx));
100 return 0;
101 }
sha1_import(struct shash_desc * desc,const void * in)102 static int sha1_import(struct shash_desc *desc, const void *in)
103 {
104 struct SHA1_CTX *sctx = shash_desc_ctx(desc);
105 memcpy(sctx, in, sizeof(*sctx));
106 return 0;
107 }
108 static struct shash_alg alg = {
109 .digestsize = SHA1_DIGEST_SIZE,
110 .init = sha1_init,
111 .update = sha1_update,
112 .final = sha1_final,
113 .export = sha1_export,
114 .import = sha1_import,
115 .descsize = sizeof(struct SHA1_CTX),
116 .statesize = sizeof(struct SHA1_CTX),
117 .base = {
118 .cra_name = "sha1",
119 .cra_driver_name= "sha1-asm",
120 .cra_priority = 150,
121 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
122 .cra_blocksize = SHA1_BLOCK_SIZE,
123 .cra_module = THIS_MODULE,
124 }
125 };
sha1_mod_init(void)126 int sha1_mod_init(void)
127 {
128 return crypto_register_shash(&alg);
129 }
sha1_mod_fini(void)130 void sha1_mod_fini(void)
131 {
132 crypto_unregister_shash(&alg);
133 }
134 #if 0
135 module_init(sha1_mod_init);
136 module_exit(sha1_mod_fini);
137 MODULE_LICENSE("GPL");
138 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm (ARM)");
139 MODULE_ALIAS("sha1");
140 MODULE_AUTHOR("David McCullough <ucdevel@gmail.com>");
141 #endif
142