Blame view
crypto/sha1_generic.c
3.09 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> |
1da177e4c
|
27 28 29 30 31 |
struct sha1_ctx { u64 count; u32 state[5]; u8 buffer[64]; }; |
54ccb3677
|
32 |
static int sha1_init(struct shash_desc *desc) |
1da177e4c
|
33 |
{ |
54ccb3677
|
34 |
struct sha1_ctx *sctx = shash_desc_ctx(desc); |
1da177e4c
|
35 36 |
static const struct sha1_ctx initstate = { 0, |
5265eeb2b
|
37 |
{ SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 }, |
1da177e4c
|
38 39 40 41 |
{ 0, } }; *sctx = initstate; |
54ccb3677
|
42 43 |
return 0; |
1da177e4c
|
44 |
} |
54ccb3677
|
45 |
static int sha1_update(struct shash_desc *desc, const u8 *data, |
6c2bb98bc
|
46 |
unsigned int len) |
1da177e4c
|
47 |
{ |
54ccb3677
|
48 |
struct sha1_ctx *sctx = shash_desc_ctx(desc); |
9d70a6c86
|
49 |
unsigned int partial, done; |
cfa8d17cc
|
50 |
const u8 *src; |
1da177e4c
|
51 |
|
fa9b98fda
|
52 53 |
partial = sctx->count & 0x3f; sctx->count += len; |
9d70a6c86
|
54 |
done = 0; |
cfa8d17cc
|
55 |
src = data; |
1da177e4c
|
56 |
|
9d70a6c86
|
57 |
if ((partial + len) > 63) { |
cfa8d17cc
|
58 |
u32 temp[SHA_WORKSPACE_WORDS]; |
9d70a6c86
|
59 |
if (partial) { |
bcb0ad2b3
|
60 61 |
done = -partial; memcpy(sctx->buffer + partial, data, done + 64); |
cfa8d17cc
|
62 |
src = sctx->buffer; |
1da177e4c
|
63 |
} |
cfa8d17cc
|
64 65 66 |
do { sha_transform(sctx->state, src, temp); |
9d70a6c86
|
67 68 69 |
done += 64; src = data + done; } while (done + 63 < len); |
cfa8d17cc
|
70 71 |
memset(temp, 0, sizeof(temp)); |
9d70a6c86
|
72 |
partial = 0; |
1da177e4c
|
73 |
} |
9d70a6c86
|
74 |
memcpy(sctx->buffer + partial, src, len - done); |
54ccb3677
|
75 76 |
return 0; |
1da177e4c
|
77 78 79 80 |
} /* Add padding and return the message digest. */ |
54ccb3677
|
81 |
static int sha1_final(struct shash_desc *desc, u8 *out) |
1da177e4c
|
82 |
{ |
54ccb3677
|
83 |
struct sha1_ctx *sctx = shash_desc_ctx(desc); |
06ace7a9b
|
84 85 86 |
__be32 *dst = (__be32 *)out; u32 i, index, padlen; __be64 bits; |
1da177e4c
|
87 |
static const u8 padding[64] = { 0x80, }; |
fa9b98fda
|
88 |
bits = cpu_to_be64(sctx->count << 3); |
1da177e4c
|
89 90 |
/* Pad out to 56 mod 64 */ |
fa9b98fda
|
91 |
index = sctx->count & 0x3f; |
1da177e4c
|
92 |
padlen = (index < 56) ? (56 - index) : ((64+56) - index); |
54ccb3677
|
93 |
sha1_update(desc, padding, padlen); |
1da177e4c
|
94 95 |
/* Append length */ |
54ccb3677
|
96 |
sha1_update(desc, (const u8 *)&bits, sizeof(bits)); |
1da177e4c
|
97 98 |
/* Store state in digest */ |
06ace7a9b
|
99 100 |
for (i = 0; i < 5; i++) dst[i] = cpu_to_be32(sctx->state[i]); |
1da177e4c
|
101 102 103 |
/* Wipe context */ memset(sctx, 0, sizeof *sctx); |
54ccb3677
|
104 105 |
return 0; |
1da177e4c
|
106 |
} |
54ccb3677
|
107 108 109 110 111 112 113 114 115 116 117 118 119 |
static struct shash_alg alg = { .digestsize = SHA1_DIGEST_SIZE, .init = sha1_init, .update = sha1_update, .final = sha1_final, .descsize = sizeof(struct sha1_ctx), .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
|
120 |
}; |
3af5b90bd
|
121 |
static int __init sha1_generic_mod_init(void) |
1da177e4c
|
122 |
{ |
54ccb3677
|
123 |
return crypto_register_shash(&alg); |
1da177e4c
|
124 |
} |
3af5b90bd
|
125 |
static void __exit sha1_generic_mod_fini(void) |
1da177e4c
|
126 |
{ |
54ccb3677
|
127 |
crypto_unregister_shash(&alg); |
1da177e4c
|
128 |
} |
3af5b90bd
|
129 130 |
module_init(sha1_generic_mod_init); module_exit(sha1_generic_mod_fini); |
1da177e4c
|
131 132 133 |
MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm"); |
b3be9a6d9
|
134 |
|
ad5d27899
|
135 |
MODULE_ALIAS("sha1"); |