Blame view
crypto/sha1_generic.c
3.45 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 |
} |
54ccb3677
|
36 |
static int 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 63 |
memset(temp, 0, sizeof(temp)); |
9d70a6c86
|
64 |
partial = 0; |
1da177e4c
|
65 |
} |
9d70a6c86
|
66 |
memcpy(sctx->buffer + partial, src, len - done); |
54ccb3677
|
67 68 |
return 0; |
1da177e4c
|
69 70 71 72 |
} /* Add padding and return the message digest. */ |
54ccb3677
|
73 |
static int sha1_final(struct shash_desc *desc, u8 *out) |
1da177e4c
|
74 |
{ |
e2a7ce4e1
|
75 |
struct sha1_state *sctx = shash_desc_ctx(desc); |
06ace7a9b
|
76 77 78 |
__be32 *dst = (__be32 *)out; u32 i, index, padlen; __be64 bits; |
1da177e4c
|
79 |
static const u8 padding[64] = { 0x80, }; |
fa9b98fda
|
80 |
bits = cpu_to_be64(sctx->count << 3); |
1da177e4c
|
81 82 |
/* Pad out to 56 mod 64 */ |
fa9b98fda
|
83 |
index = sctx->count & 0x3f; |
1da177e4c
|
84 |
padlen = (index < 56) ? (56 - index) : ((64+56) - index); |
54ccb3677
|
85 |
sha1_update(desc, padding, padlen); |
1da177e4c
|
86 87 |
/* Append length */ |
54ccb3677
|
88 |
sha1_update(desc, (const u8 *)&bits, sizeof(bits)); |
1da177e4c
|
89 90 |
/* Store state in digest */ |
06ace7a9b
|
91 92 |
for (i = 0; i < 5; i++) dst[i] = cpu_to_be32(sctx->state[i]); |
1da177e4c
|
93 94 95 |
/* Wipe context */ memset(sctx, 0, sizeof *sctx); |
54ccb3677
|
96 97 |
return 0; |
1da177e4c
|
98 |
} |
e2a7ce4e1
|
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
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
|
114 115 116 117 118 |
static struct shash_alg alg = { .digestsize = SHA1_DIGEST_SIZE, .init = sha1_init, .update = sha1_update, .final = sha1_final, |
e2a7ce4e1
|
119 120 121 122 |
.export = sha1_export, .import = sha1_import, .descsize = sizeof(struct sha1_state), .statesize = sizeof(struct sha1_state), |
54ccb3677
|
123 124 125 126 127 128 129 |
.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
|
130 |
}; |
3af5b90bd
|
131 |
static int __init sha1_generic_mod_init(void) |
1da177e4c
|
132 |
{ |
54ccb3677
|
133 |
return crypto_register_shash(&alg); |
1da177e4c
|
134 |
} |
3af5b90bd
|
135 |
static void __exit sha1_generic_mod_fini(void) |
1da177e4c
|
136 |
{ |
54ccb3677
|
137 |
crypto_unregister_shash(&alg); |
1da177e4c
|
138 |
} |
3af5b90bd
|
139 140 |
module_init(sha1_generic_mod_init); module_exit(sha1_generic_mod_fini); |
1da177e4c
|
141 142 143 |
MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm"); |
b3be9a6d9
|
144 |
|
ad5d27899
|
145 |
MODULE_ALIAS("sha1"); |