[PATCH] USB: ftdi_sio: fix a couple of timeouts
[linux-2.6/s3c2410-cpufreq.git] / crypto / sha1.c
blob4016f3b8ce9b6233f24a8200d3b1661588da7f96
1 /*
2 * Cryptographic API.
4 * SHA1 Secure Hash Algorithm.
6 * Derived from cryptoapi implementation, adapted for in-place
7 * scatterlist interface.
9 * Copyright (c) Alan Smithee.
10 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
11 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
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.
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>
27 #define SHA1_DIGEST_SIZE 20
28 #define SHA1_HMAC_BLOCK_SIZE 64
30 struct sha1_ctx {
31 u64 count;
32 u32 state[5];
33 u8 buffer[64];
36 static void sha1_init(void *ctx)
38 struct sha1_ctx *sctx = ctx;
39 static const struct sha1_ctx initstate = {
41 { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 },
42 { 0, }
45 *sctx = initstate;
48 static void sha1_update(void *ctx, const u8 *data, unsigned int len)
50 struct sha1_ctx *sctx = ctx;
51 unsigned int i, j;
52 u32 temp[SHA_WORKSPACE_WORDS];
54 j = (sctx->count >> 3) & 0x3f;
55 sctx->count += len << 3;
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);
63 j = 0;
65 else i = 0;
66 memset(temp, 0, sizeof(temp));
67 memcpy(&sctx->buffer[j], &data[i], len - i);
71 /* Add padding and return the message digest. */
72 static void sha1_final(void* ctx, u8 *out)
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, };
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;
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);
95 /* Append length */
96 sha1_update(sctx, bits, sizeof bits);
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;
107 /* Wipe context */
108 memset(sctx, 0, sizeof *sctx);
111 static 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 } }
125 static int __init init(void)
127 return crypto_register_alg(&alg);
130 static void __exit fini(void)
132 crypto_unregister_alg(&alg);
135 module_init(init);
136 module_exit(fini);
138 MODULE_LICENSE("GPL");
139 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");