blob: 00ae60eb925435efe5295a9d7315283bd258e613 [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
Mandeep Singh Baines36ca2392011-06-27 15:41:56 +080046 partial = sctx->count % SHA1_BLOCK_SIZE;
Nicolas Pitrefa9b98f2005-11-13 11:17:33 +110047 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
Mandeep Singh Baines36ca2392011-06-27 15:41:56 +080051 if ((partial + len) >= SHA1_BLOCK_SIZE) {
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;
Mandeep Singh Baines36ca2392011-06-27 15:41:56 +080056 memcpy(sctx->buffer + partial, data,
57 done + SHA1_BLOCK_SIZE);
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110058 src = sctx->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110060
61 do {
62 sha_transform(sctx->state, src, temp);
Mandeep Singh Baines36ca2392011-06-27 15:41:56 +080063 done += SHA1_BLOCK_SIZE;
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110064 src = data + done;
Mandeep Singh Baines36ca2392011-06-27 15:41:56 +080065 } while (done + SHA1_BLOCK_SIZE <= len);
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110066
67 memset(temp, 0, sizeof(temp));
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110068 partial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110070 memcpy(sctx->buffer + partial, src, len - done);
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080071
72 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75
76/* Add padding and return the message digest. */
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080077static int sha1_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Herbert Xue2a7ce42009-07-09 21:27:13 +080079 struct sha1_state *sctx = shash_desc_ctx(desc);
Herbert Xu06ace7a2005-10-30 21:25:15 +110080 __be32 *dst = (__be32 *)out;
81 u32 i, index, padlen;
82 __be64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 static const u8 padding[64] = { 0x80, };
84
Nicolas Pitrefa9b98f2005-11-13 11:17:33 +110085 bits = cpu_to_be64(sctx->count << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /* Pad out to 56 mod 64 */
Nicolas Pitrefa9b98f2005-11-13 11:17:33 +110088 index = sctx->count & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080090 sha1_update(desc, padding, padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* Append length */
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080093 sha1_update(desc, (const u8 *)&bits, sizeof(bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 /* Store state in digest */
Herbert Xu06ace7a2005-10-30 21:25:15 +110096 for (i = 0; i < 5; i++)
97 dst[i] = cpu_to_be32(sctx->state[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /* Wipe context */
100 memset(sctx, 0, sizeof *sctx);
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800101
102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Herbert Xue2a7ce42009-07-09 21:27:13 +0800105static int sha1_export(struct shash_desc *desc, void *out)
106{
107 struct sha1_state *sctx = shash_desc_ctx(desc);
108
109 memcpy(out, sctx, sizeof(*sctx));
110 return 0;
111}
112
113static int sha1_import(struct shash_desc *desc, const void *in)
114{
115 struct sha1_state *sctx = shash_desc_ctx(desc);
116
117 memcpy(sctx, in, sizeof(*sctx));
118 return 0;
119}
120
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800121static struct shash_alg alg = {
122 .digestsize = SHA1_DIGEST_SIZE,
123 .init = sha1_init,
124 .update = sha1_update,
125 .final = sha1_final,
Herbert Xue2a7ce42009-07-09 21:27:13 +0800126 .export = sha1_export,
127 .import = sha1_import,
128 .descsize = sizeof(struct sha1_state),
129 .statesize = sizeof(struct sha1_state),
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800130 .base = {
131 .cra_name = "sha1",
132 .cra_driver_name= "sha1-generic",
133 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
134 .cra_blocksize = SHA1_BLOCK_SIZE,
135 .cra_module = THIS_MODULE,
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800139static int __init sha1_generic_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800141 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800144static void __exit sha1_generic_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800146 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800149module_init(sha1_generic_mod_init);
150module_exit(sha1_generic_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152MODULE_LICENSE("GPL");
153MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
Michal Ludvigb3be9a62006-07-09 08:59:38 +1000154
Sebastian Siewiorad5d2782007-10-08 11:45:10 +0800155MODULE_ALIAS("sha1");