0001
0002
0003
0004
0005
0006
0007
0008 #ifndef _CRYPTO_SHA1_BASE_H
0009 #define _CRYPTO_SHA1_BASE_H
0010
0011 #include <crypto/internal/hash.h>
0012 #include <crypto/sha1.h>
0013 #include <linux/crypto.h>
0014 #include <linux/module.h>
0015 #include <linux/string.h>
0016
0017 #include <asm/unaligned.h>
0018
0019 typedef void (sha1_block_fn)(struct sha1_state *sst, u8 const *src, int blocks);
0020
0021 static inline int sha1_base_init(struct shash_desc *desc)
0022 {
0023 struct sha1_state *sctx = shash_desc_ctx(desc);
0024
0025 sctx->state[0] = SHA1_H0;
0026 sctx->state[1] = SHA1_H1;
0027 sctx->state[2] = SHA1_H2;
0028 sctx->state[3] = SHA1_H3;
0029 sctx->state[4] = SHA1_H4;
0030 sctx->count = 0;
0031
0032 return 0;
0033 }
0034
0035 static inline int sha1_base_do_update(struct shash_desc *desc,
0036 const u8 *data,
0037 unsigned int len,
0038 sha1_block_fn *block_fn)
0039 {
0040 struct sha1_state *sctx = shash_desc_ctx(desc);
0041 unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
0042
0043 sctx->count += len;
0044
0045 if (unlikely((partial + len) >= SHA1_BLOCK_SIZE)) {
0046 int blocks;
0047
0048 if (partial) {
0049 int p = SHA1_BLOCK_SIZE - partial;
0050
0051 memcpy(sctx->buffer + partial, data, p);
0052 data += p;
0053 len -= p;
0054
0055 block_fn(sctx, sctx->buffer, 1);
0056 }
0057
0058 blocks = len / SHA1_BLOCK_SIZE;
0059 len %= SHA1_BLOCK_SIZE;
0060
0061 if (blocks) {
0062 block_fn(sctx, data, blocks);
0063 data += blocks * SHA1_BLOCK_SIZE;
0064 }
0065 partial = 0;
0066 }
0067 if (len)
0068 memcpy(sctx->buffer + partial, data, len);
0069
0070 return 0;
0071 }
0072
0073 static inline int sha1_base_do_finalize(struct shash_desc *desc,
0074 sha1_block_fn *block_fn)
0075 {
0076 const int bit_offset = SHA1_BLOCK_SIZE - sizeof(__be64);
0077 struct sha1_state *sctx = shash_desc_ctx(desc);
0078 __be64 *bits = (__be64 *)(sctx->buffer + bit_offset);
0079 unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
0080
0081 sctx->buffer[partial++] = 0x80;
0082 if (partial > bit_offset) {
0083 memset(sctx->buffer + partial, 0x0, SHA1_BLOCK_SIZE - partial);
0084 partial = 0;
0085
0086 block_fn(sctx, sctx->buffer, 1);
0087 }
0088
0089 memset(sctx->buffer + partial, 0x0, bit_offset - partial);
0090 *bits = cpu_to_be64(sctx->count << 3);
0091 block_fn(sctx, sctx->buffer, 1);
0092
0093 return 0;
0094 }
0095
0096 static inline int sha1_base_finish(struct shash_desc *desc, u8 *out)
0097 {
0098 struct sha1_state *sctx = shash_desc_ctx(desc);
0099 __be32 *digest = (__be32 *)out;
0100 int i;
0101
0102 for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
0103 put_unaligned_be32(sctx->state[i], digest++);
0104
0105 memzero_explicit(sctx, sizeof(*sctx));
0106 return 0;
0107 }
0108
0109 #endif