aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/sha1.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /crypto/sha1.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'crypto/sha1.c')
-rw-r--r--crypto/sha1.c139
1 files changed, 139 insertions, 0 deletions
diff --git a/crypto/sha1.c b/crypto/sha1.c
new file mode 100644
index 000000000000..4016f3b8ce9b
--- /dev/null
+++ b/crypto/sha1.c
@@ -0,0 +1,139 @@
1/*
2 * Cryptographic API.
3 *
4 * SHA1 Secure Hash Algorithm.
5 *
6 * Derived from cryptoapi implementation, adapted for in-place
7 * scatterlist interface.
8 *
9 * Copyright (c) Alan Smithee.
10 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
11 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 *
18 */
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/mm.h>
22#include <linux/crypto.h>
23#include <linux/cryptohash.h>
24#include <asm/scatterlist.h>
25#include <asm/byteorder.h>
26
27#define SHA1_DIGEST_SIZE 20
28#define SHA1_HMAC_BLOCK_SIZE 64
29
30struct sha1_ctx {
31 u64 count;
32 u32 state[5];
33 u8 buffer[64];
34};
35
36static void sha1_init(void *ctx)
37{
38 struct sha1_ctx *sctx = ctx;
39 static const struct sha1_ctx initstate = {
40 0,
41 { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 },
42 { 0, }
43 };
44
45 *sctx = initstate;
46}
47
48static void sha1_update(void *ctx, const u8 *data, unsigned int len)
49{
50 struct sha1_ctx *sctx = ctx;
51 unsigned int i, j;
52 u32 temp[SHA_WORKSPACE_WORDS];
53
54 j = (sctx->count >> 3) & 0x3f;
55 sctx->count += len << 3;
56
57 if ((j + len) > 63) {
58 memcpy(&sctx->buffer[j], data, (i = 64-j));
59 sha_transform(sctx->state, sctx->buffer, temp);
60 for ( ; i + 63 < len; i += 64) {
61 sha_transform(sctx->state, &data[i], temp);
62 }
63 j = 0;
64 }
65 else i = 0;
66 memset(temp, 0, sizeof(temp));
67 memcpy(&sctx->buffer[j], &data[i], len - i);
68}
69
70
71/* Add padding and return the message digest. */
72static void sha1_final(void* ctx, u8 *out)
73{
74 struct sha1_ctx *sctx = ctx;
75 u32 i, j, index, padlen;
76 u64 t;
77 u8 bits[8] = { 0, };
78 static const u8 padding[64] = { 0x80, };
79
80 t = sctx->count;
81 bits[7] = 0xff & t; t>>=8;
82 bits[6] = 0xff & t; t>>=8;
83 bits[5] = 0xff & t; t>>=8;
84 bits[4] = 0xff & t; t>>=8;
85 bits[3] = 0xff & t; t>>=8;
86 bits[2] = 0xff & t; t>>=8;
87 bits[1] = 0xff & t; t>>=8;
88 bits[0] = 0xff & t;
89
90 /* Pad out to 56 mod 64 */
91 index = (sctx->count >> 3) & 0x3f;
92 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
93 sha1_update(sctx, padding, padlen);
94
95 /* Append length */
96 sha1_update(sctx, bits, sizeof bits);
97
98 /* Store state in digest */
99 for (i = j = 0; i < 5; i++, j += 4) {
100 u32 t2 = sctx->state[i];
101 out[j+3] = t2 & 0xff; t2>>=8;
102 out[j+2] = t2 & 0xff; t2>>=8;
103 out[j+1] = t2 & 0xff; t2>>=8;
104 out[j ] = t2 & 0xff;
105 }
106
107 /* Wipe context */
108 memset(sctx, 0, sizeof *sctx);
109}
110
111static struct crypto_alg alg = {
112 .cra_name = "sha1",
113 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
114 .cra_blocksize = SHA1_HMAC_BLOCK_SIZE,
115 .cra_ctxsize = sizeof(struct sha1_ctx),
116 .cra_module = THIS_MODULE,
117 .cra_list = LIST_HEAD_INIT(alg.cra_list),
118 .cra_u = { .digest = {
119 .dia_digestsize = SHA1_DIGEST_SIZE,
120 .dia_init = sha1_init,
121 .dia_update = sha1_update,
122 .dia_final = sha1_final } }
123};
124
125static int __init init(void)
126{
127 return crypto_register_alg(&alg);
128}
129
130static void __exit fini(void)
131{
132 crypto_unregister_alg(&alg);
133}
134
135module_init(init);
136module_exit(fini);
137
138MODULE_LICENSE("GPL");
139MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");