blob: 039e58cfa155655f42aec3ddcb8d2761aa22b264 [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
Mathias Krause7c390172011-08-04 20:19:24 +020039int crypto_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
Daniel Borkmann7185ad22014-09-07 23:23:38 +020067 memzero_explicit(temp, 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}
Mathias Krause7c390172011-08-04 20:19:24 +020074EXPORT_SYMBOL(crypto_sha1_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76
77/* Add padding and return the message digest. */
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080078static int sha1_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Herbert Xue2a7ce42009-07-09 21:27:13 +080080 struct sha1_state *sctx = shash_desc_ctx(desc);
Herbert Xu06ace7a2005-10-30 21:25:15 +110081 __be32 *dst = (__be32 *)out;
82 u32 i, index, padlen;
83 __be64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 static const u8 padding[64] = { 0x80, };
85
Nicolas Pitrefa9b98f2005-11-13 11:17:33 +110086 bits = cpu_to_be64(sctx->count << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 /* Pad out to 56 mod 64 */
Nicolas Pitrefa9b98f2005-11-13 11:17:33 +110089 index = sctx->count & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
Mathias Krause7c390172011-08-04 20:19:24 +020091 crypto_sha1_update(desc, padding, padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 /* Append length */
Mathias Krause7c390172011-08-04 20:19:24 +020094 crypto_sha1_update(desc, (const u8 *)&bits, sizeof(bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 /* Store state in digest */
Herbert Xu06ace7a2005-10-30 21:25:15 +110097 for (i = 0; i < 5; i++)
98 dst[i] = cpu_to_be32(sctx->state[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /* Wipe context */
101 memset(sctx, 0, sizeof *sctx);
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800102
103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Herbert Xue2a7ce42009-07-09 21:27:13 +0800106static int sha1_export(struct shash_desc *desc, void *out)
107{
108 struct sha1_state *sctx = shash_desc_ctx(desc);
109
110 memcpy(out, sctx, sizeof(*sctx));
111 return 0;
112}
113
114static int sha1_import(struct shash_desc *desc, const void *in)
115{
116 struct sha1_state *sctx = shash_desc_ctx(desc);
117
118 memcpy(sctx, in, sizeof(*sctx));
119 return 0;
120}
121
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800122static struct shash_alg alg = {
123 .digestsize = SHA1_DIGEST_SIZE,
124 .init = sha1_init,
Mathias Krause7c390172011-08-04 20:19:24 +0200125 .update = crypto_sha1_update,
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800126 .final = sha1_final,
Herbert Xue2a7ce42009-07-09 21:27:13 +0800127 .export = sha1_export,
128 .import = sha1_import,
129 .descsize = sizeof(struct sha1_state),
130 .statesize = sizeof(struct sha1_state),
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800131 .base = {
132 .cra_name = "sha1",
133 .cra_driver_name= "sha1-generic",
134 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
135 .cra_blocksize = SHA1_BLOCK_SIZE,
136 .cra_module = THIS_MODULE,
137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800140static int __init sha1_generic_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800142 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800145static void __exit sha1_generic_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800147 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800150module_init(sha1_generic_mod_init);
151module_exit(sha1_generic_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153MODULE_LICENSE("GPL");
154MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
Michal Ludvigb3be9a62006-07-09 08:59:38 +1000155
Kees Cook5d26a102014-11-20 17:05:53 -0800156MODULE_ALIAS_CRYPTO("sha1");