sha1_generic.c 3.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Cryptographic API.
 *
 * SHA1 Secure Hash Algorithm.
 *
 * Derived from cryptoapi implementation, adapted for in-place
 * scatterlist interface.
 *
 * Copyright (c) Alan Smithee.
 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 */
19
#include <crypto/internal/hash.h>
Linus Torvalds's avatar
Linus Torvalds committed
20 21 22 23
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/cryptohash.h>
24
#include <linux/types.h>
25
#include <crypto/sha.h>
Linus Torvalds's avatar
Linus Torvalds committed
26 27
#include <asm/byteorder.h>

28
static int sha1_init(struct shash_desc *desc)
Linus Torvalds's avatar
Linus Torvalds committed
29
{
30
	struct sha1_state *sctx = shash_desc_ctx(desc);
31

32 33
	*sctx = (struct sha1_state){
		.state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
Linus Torvalds's avatar
Linus Torvalds committed
34 35
	};

36
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
37 38
}

39
int crypto_sha1_update(struct shash_desc *desc, const u8 *data,
40
			unsigned int len)
Linus Torvalds's avatar
Linus Torvalds committed
41
{
42
	struct sha1_state *sctx = shash_desc_ctx(desc);
43
	unsigned int partial, done;
44
	const u8 *src;
Linus Torvalds's avatar
Linus Torvalds committed
45

46
	partial = sctx->count % SHA1_BLOCK_SIZE;
47
	sctx->count += len;
48
	done = 0;
49
	src = data;
Linus Torvalds's avatar
Linus Torvalds committed
50

51
	if ((partial + len) >= SHA1_BLOCK_SIZE) {
52 53
		u32 temp[SHA_WORKSPACE_WORDS];

54
		if (partial) {
55
			done = -partial;
56 57
			memcpy(sctx->buffer + partial, data,
			       done + SHA1_BLOCK_SIZE);
58
			src = sctx->buffer;
Linus Torvalds's avatar
Linus Torvalds committed
59
		}
60 61 62

		do {
			sha_transform(sctx->state, src, temp);
63
			done += SHA1_BLOCK_SIZE;
64
			src = data + done;
65
		} while (done + SHA1_BLOCK_SIZE <= len);
66 67

		memset(temp, 0, sizeof(temp));
68
		partial = 0;
Linus Torvalds's avatar
Linus Torvalds committed
69
	}
70
	memcpy(sctx->buffer + partial, src, len - done);
71 72

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
73
}
74
EXPORT_SYMBOL(crypto_sha1_update);
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77


/* Add padding and return the message digest. */
78
static int sha1_final(struct shash_desc *desc, u8 *out)
Linus Torvalds's avatar
Linus Torvalds committed
79
{
80
	struct sha1_state *sctx = shash_desc_ctx(desc);
81 82 83
	__be32 *dst = (__be32 *)out;
	u32 i, index, padlen;
	__be64 bits;
Linus Torvalds's avatar
Linus Torvalds committed
84 85
	static const u8 padding[64] = { 0x80, };

86
	bits = cpu_to_be64(sctx->count << 3);
Linus Torvalds's avatar
Linus Torvalds committed
87 88

	/* Pad out to 56 mod 64 */
89
	index = sctx->count & 0x3f;
Linus Torvalds's avatar
Linus Torvalds committed
90
	padlen = (index < 56) ? (56 - index) : ((64+56) - index);
91
	crypto_sha1_update(desc, padding, padlen);
Linus Torvalds's avatar
Linus Torvalds committed
92 93

	/* Append length */
94
	crypto_sha1_update(desc, (const u8 *)&bits, sizeof(bits));
Linus Torvalds's avatar
Linus Torvalds committed
95 96

	/* Store state in digest */
97 98
	for (i = 0; i < 5; i++)
		dst[i] = cpu_to_be32(sctx->state[i]);
Linus Torvalds's avatar
Linus Torvalds committed
99 100 101

	/* Wipe context */
	memset(sctx, 0, sizeof *sctx);
102 103

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
104 105
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
static int sha1_export(struct shash_desc *desc, void *out)
{
	struct sha1_state *sctx = shash_desc_ctx(desc);

	memcpy(out, sctx, sizeof(*sctx));
	return 0;
}

static int sha1_import(struct shash_desc *desc, const void *in)
{
	struct sha1_state *sctx = shash_desc_ctx(desc);

	memcpy(sctx, in, sizeof(*sctx));
	return 0;
}

122 123 124
static struct shash_alg alg = {
	.digestsize	=	SHA1_DIGEST_SIZE,
	.init		=	sha1_init,
125
	.update		=	crypto_sha1_update,
126
	.final		=	sha1_final,
127 128 129 130
	.export		=	sha1_export,
	.import		=	sha1_import,
	.descsize	=	sizeof(struct sha1_state),
	.statesize	=	sizeof(struct sha1_state),
131 132 133 134 135 136 137
	.base		=	{
		.cra_name	=	"sha1",
		.cra_driver_name=	"sha1-generic",
		.cra_flags	=	CRYPTO_ALG_TYPE_SHASH,
		.cra_blocksize	=	SHA1_BLOCK_SIZE,
		.cra_module	=	THIS_MODULE,
	}
Linus Torvalds's avatar
Linus Torvalds committed
138 139
};

140
static int __init sha1_generic_mod_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
141
{
142
	return crypto_register_shash(&alg);
Linus Torvalds's avatar
Linus Torvalds committed
143 144
}

145
static void __exit sha1_generic_mod_fini(void)
Linus Torvalds's avatar
Linus Torvalds committed
146
{
147
	crypto_unregister_shash(&alg);
Linus Torvalds's avatar
Linus Torvalds committed
148 149
}

150 151
module_init(sha1_generic_mod_init);
module_exit(sha1_generic_mod_fini);
Linus Torvalds's avatar
Linus Torvalds committed
152 153 154

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
155

156
MODULE_ALIAS("sha1");