blob: 0416091bf45ad3854a5987a0d05d2c227cfdaeb3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080019#include <crypto/internal/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/cryptohash.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110024#include <linux/types.h>
Jan Glauber5265eeb2007-10-09 22:43:13 +080025#include <crypto/sha.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/byteorder.h>
27
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080028static int sha1_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Herbert Xue2a7ce42009-07-09 21:27:13 +080030 struct sha1_state *sctx = shash_desc_ctx(desc);
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080031
Herbert Xue2a7ce42009-07-09 21:27:13 +080032 *sctx = (struct sha1_state){
33 .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 };
35
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080039static int sha1_update(struct shash_desc *desc, const u8 *data,
Herbert Xu6c2bb982006-05-16 22:09:29 +100040 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Herbert Xue2a7ce42009-07-09 21:27:13 +080042 struct sha1_state *sctx = shash_desc_ctx(desc);
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110043 unsigned int partial, done;
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110044 const u8 *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Nicolas Pitrefa9b98f2005-11-13 11:17:33 +110046 partial = sctx->count & 0x3f;
47 sctx->count += len;
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110048 done = 0;
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110049 src = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110051 if ((partial + len) > 63) {
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110052 u32 temp[SHA_WORKSPACE_WORDS];
53
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110054 if (partial) {
Herbert Xubcb0ad22005-12-21 19:01:58 +080055 done = -partial;
56 memcpy(sctx->buffer + partial, data, done + 64);
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110057 src = sctx->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110059
60 do {
61 sha_transform(sctx->state, src, temp);
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110062 done += 64;
63 src = data + done;
64 } while (done + 63 < len);
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110065
66 memset(temp, 0, sizeof(temp));
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110067 partial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110069 memcpy(sctx->buffer + partial, src, len - done);
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080070
71 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74
75/* Add padding and return the message digest. */
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080076static int sha1_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Herbert Xue2a7ce42009-07-09 21:27:13 +080078 struct sha1_state *sctx = shash_desc_ctx(desc);
Herbert Xu06ace7a2005-10-30 21:25:15 +110079 __be32 *dst = (__be32 *)out;
80 u32 i, index, padlen;
81 __be64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 static const u8 padding[64] = { 0x80, };
83
Nicolas Pitrefa9b98f2005-11-13 11:17:33 +110084 bits = cpu_to_be64(sctx->count << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /* Pad out to 56 mod 64 */
Nicolas Pitrefa9b98f2005-11-13 11:17:33 +110087 index = sctx->count & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080089 sha1_update(desc, padding, padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 /* Append length */
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080092 sha1_update(desc, (const u8 *)&bits, sizeof(bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* Store state in digest */
Herbert Xu06ace7a2005-10-30 21:25:15 +110095 for (i = 0; i < 5; i++)
96 dst[i] = cpu_to_be32(sctx->state[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* Wipe context */
99 memset(sctx, 0, sizeof *sctx);
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800100
101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Herbert Xue2a7ce42009-07-09 21:27:13 +0800104static int sha1_export(struct shash_desc *desc, void *out)
105{
106 struct sha1_state *sctx = shash_desc_ctx(desc);
107
108 memcpy(out, sctx, sizeof(*sctx));
109 return 0;
110}
111
112static int sha1_import(struct shash_desc *desc, const void *in)
113{
114 struct sha1_state *sctx = shash_desc_ctx(desc);
115
116 memcpy(sctx, in, sizeof(*sctx));
117 return 0;
118}
119
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800120static struct shash_alg alg = {
121 .digestsize = SHA1_DIGEST_SIZE,
122 .init = sha1_init,
123 .update = sha1_update,
124 .final = sha1_final,
Herbert Xue2a7ce42009-07-09 21:27:13 +0800125 .export = sha1_export,
126 .import = sha1_import,
127 .descsize = sizeof(struct sha1_state),
128 .statesize = sizeof(struct sha1_state),
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800129 .base = {
130 .cra_name = "sha1",
131 .cra_driver_name= "sha1-generic",
132 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
133 .cra_blocksize = SHA1_BLOCK_SIZE,
134 .cra_module = THIS_MODULE,
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800138static int __init sha1_generic_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800140 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800143static void __exit sha1_generic_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800145 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800148module_init(sha1_generic_mod_init);
149module_exit(sha1_generic_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151MODULE_LICENSE("GPL");
152MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
Michal Ludvigb3be9a62006-07-09 08:59:38 +1000153
Sebastian Siewiorad5d2782007-10-08 11:45:10 +0800154MODULE_ALIAS("sha1");