Blame view
crypto/sha1_generic.c
3.55 KB
1da177e4c
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/* * Cryptographic API. * * SHA1 Secure Hash Algorithm. * * Derived from cryptoapi implementation, adapted for in-place * scatterlist interface. * * Copyright (c) Alan Smithee. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> * Copyright (c) Jean-Francois Dive <jef@linuxbe.org> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * */ |
54ccb3677
|
19 |
#include <crypto/internal/hash.h> |
1da177e4c
|
20 21 22 |
#include <linux/init.h> #include <linux/module.h> #include <linux/mm.h> |
1da177e4c
|
23 |
#include <linux/cryptohash.h> |
06ace7a9b
|
24 |
#include <linux/types.h> |
5265eeb2b
|
25 |
#include <crypto/sha.h> |
1da177e4c
|
26 |
#include <asm/byteorder.h> |
54ccb3677
|
27 |
static int sha1_init(struct shash_desc *desc) |
1da177e4c
|
28 |
{ |
e2a7ce4e1
|
29 |
struct sha1_state *sctx = shash_desc_ctx(desc); |
54ccb3677
|
30 |
|
e2a7ce4e1
|
31 32 |
*sctx = (struct sha1_state){ .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 }, |
1da177e4c
|
33 |
}; |
54ccb3677
|
34 |
return 0; |
1da177e4c
|
35 |
} |
7c390170b
|
36 |
int crypto_sha1_update(struct shash_desc *desc, const u8 *data, |
6c2bb98bc
|
37 |
unsigned int len) |
1da177e4c
|
38 |
{ |
e2a7ce4e1
|
39 |
struct sha1_state *sctx = shash_desc_ctx(desc); |
9d70a6c86
|
40 |
unsigned int partial, done; |
cfa8d17cc
|
41 |
const u8 *src; |
1da177e4c
|
42 |
|
36ca239b2
|
43 |
partial = sctx->count % SHA1_BLOCK_SIZE; |
fa9b98fda
|
44 |
sctx->count += len; |
9d70a6c86
|
45 |
done = 0; |
cfa8d17cc
|
46 |
src = data; |
1da177e4c
|
47 |
|
36ca239b2
|
48 |
if ((partial + len) >= SHA1_BLOCK_SIZE) { |
cfa8d17cc
|
49 |
u32 temp[SHA_WORKSPACE_WORDS]; |
9d70a6c86
|
50 |
if (partial) { |
bcb0ad2b3
|
51 |
done = -partial; |
36ca239b2
|
52 53 |
memcpy(sctx->buffer + partial, data, done + SHA1_BLOCK_SIZE); |
cfa8d17cc
|
54 |
src = sctx->buffer; |
1da177e4c
|
55 |
} |
cfa8d17cc
|
56 57 58 |
do { sha_transform(sctx->state, src, temp); |
36ca239b2
|
59 |
done += SHA1_BLOCK_SIZE; |
9d70a6c86
|
60 |
src = data + done; |
36ca239b2
|
61 |
} while (done + SHA1_BLOCK_SIZE <= len); |
cfa8d17cc
|
62 |
|
7185ad267
|
63 |
memzero_explicit(temp, sizeof(temp)); |
9d70a6c86
|
64 |
partial = 0; |
1da177e4c
|
65 |
} |
9d70a6c86
|
66 |
memcpy(sctx->buffer + partial, src, len - done); |
54ccb3677
|
67 68 |
return 0; |
1da177e4c
|
69 |
} |
7c390170b
|
70 |
EXPORT_SYMBOL(crypto_sha1_update); |
1da177e4c
|
71 72 73 |
/* Add padding and return the message digest. */ |
54ccb3677
|
74 |
static int sha1_final(struct shash_desc *desc, u8 *out) |
1da177e4c
|
75 |
{ |
e2a7ce4e1
|
76 |
struct sha1_state *sctx = shash_desc_ctx(desc); |
06ace7a9b
|
77 78 79 |
__be32 *dst = (__be32 *)out; u32 i, index, padlen; __be64 bits; |
1da177e4c
|
80 |
static const u8 padding[64] = { 0x80, }; |
fa9b98fda
|
81 |
bits = cpu_to_be64(sctx->count << 3); |
1da177e4c
|
82 83 |
/* Pad out to 56 mod 64 */ |
fa9b98fda
|
84 |
index = sctx->count & 0x3f; |
1da177e4c
|
85 |
padlen = (index < 56) ? (56 - index) : ((64+56) - index); |
7c390170b
|
86 |
crypto_sha1_update(desc, padding, padlen); |
1da177e4c
|
87 88 |
/* Append length */ |
7c390170b
|
89 |
crypto_sha1_update(desc, (const u8 *)&bits, sizeof(bits)); |
1da177e4c
|
90 91 |
/* Store state in digest */ |
06ace7a9b
|
92 93 |
for (i = 0; i < 5; i++) dst[i] = cpu_to_be32(sctx->state[i]); |
1da177e4c
|
94 95 96 |
/* Wipe context */ memset(sctx, 0, sizeof *sctx); |
54ccb3677
|
97 98 |
return 0; |
1da177e4c
|
99 |
} |
e2a7ce4e1
|
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
static int sha1_export(struct shash_desc *desc, void *out) { struct sha1_state *sctx = shash_desc_ctx(desc); memcpy(out, sctx, sizeof(*sctx)); return 0; } static int sha1_import(struct shash_desc *desc, const void *in) { struct sha1_state *sctx = shash_desc_ctx(desc); memcpy(sctx, in, sizeof(*sctx)); return 0; } |
54ccb3677
|
115 116 117 |
static struct shash_alg alg = { .digestsize = SHA1_DIGEST_SIZE, .init = sha1_init, |
7c390170b
|
118 |
.update = crypto_sha1_update, |
54ccb3677
|
119 |
.final = sha1_final, |
e2a7ce4e1
|
120 121 122 123 |
.export = sha1_export, .import = sha1_import, .descsize = sizeof(struct sha1_state), .statesize = sizeof(struct sha1_state), |
54ccb3677
|
124 125 126 127 128 129 130 |
.base = { .cra_name = "sha1", .cra_driver_name= "sha1-generic", .cra_flags = CRYPTO_ALG_TYPE_SHASH, .cra_blocksize = SHA1_BLOCK_SIZE, .cra_module = THIS_MODULE, } |
1da177e4c
|
131 |
}; |
3af5b90bd
|
132 |
static int __init sha1_generic_mod_init(void) |
1da177e4c
|
133 |
{ |
54ccb3677
|
134 |
return crypto_register_shash(&alg); |
1da177e4c
|
135 |
} |
3af5b90bd
|
136 |
static void __exit sha1_generic_mod_fini(void) |
1da177e4c
|
137 |
{ |
54ccb3677
|
138 |
crypto_unregister_shash(&alg); |
1da177e4c
|
139 |
} |
3af5b90bd
|
140 141 |
module_init(sha1_generic_mod_init); module_exit(sha1_generic_mod_fini); |
1da177e4c
|
142 143 144 |
MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm"); |
b3be9a6d9
|
145 |
|
5d26a105b
|
146 |
MODULE_ALIAS_CRYPTO("sha1"); |
3e14dcf7c
|
147 |
MODULE_ALIAS_CRYPTO("sha1-generic"); |