0001
0002
0003
0004
0005
0006
0007
0008 #ifndef _CRYPTO_SHA512_BASE_H
0009 #define _CRYPTO_SHA512_BASE_H
0010
0011 #include <crypto/internal/hash.h>
0012 #include <crypto/sha2.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 (sha512_block_fn)(struct sha512_state *sst, u8 const *src,
0020 int blocks);
0021
0022 static inline int sha384_base_init(struct shash_desc *desc)
0023 {
0024 struct sha512_state *sctx = shash_desc_ctx(desc);
0025
0026 sctx->state[0] = SHA384_H0;
0027 sctx->state[1] = SHA384_H1;
0028 sctx->state[2] = SHA384_H2;
0029 sctx->state[3] = SHA384_H3;
0030 sctx->state[4] = SHA384_H4;
0031 sctx->state[5] = SHA384_H5;
0032 sctx->state[6] = SHA384_H6;
0033 sctx->state[7] = SHA384_H7;
0034 sctx->count[0] = sctx->count[1] = 0;
0035
0036 return 0;
0037 }
0038
0039 static inline int sha512_base_init(struct shash_desc *desc)
0040 {
0041 struct sha512_state *sctx = shash_desc_ctx(desc);
0042
0043 sctx->state[0] = SHA512_H0;
0044 sctx->state[1] = SHA512_H1;
0045 sctx->state[2] = SHA512_H2;
0046 sctx->state[3] = SHA512_H3;
0047 sctx->state[4] = SHA512_H4;
0048 sctx->state[5] = SHA512_H5;
0049 sctx->state[6] = SHA512_H6;
0050 sctx->state[7] = SHA512_H7;
0051 sctx->count[0] = sctx->count[1] = 0;
0052
0053 return 0;
0054 }
0055
0056 static inline int sha512_base_do_update(struct shash_desc *desc,
0057 const u8 *data,
0058 unsigned int len,
0059 sha512_block_fn *block_fn)
0060 {
0061 struct sha512_state *sctx = shash_desc_ctx(desc);
0062 unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
0063
0064 sctx->count[0] += len;
0065 if (sctx->count[0] < len)
0066 sctx->count[1]++;
0067
0068 if (unlikely((partial + len) >= SHA512_BLOCK_SIZE)) {
0069 int blocks;
0070
0071 if (partial) {
0072 int p = SHA512_BLOCK_SIZE - partial;
0073
0074 memcpy(sctx->buf + partial, data, p);
0075 data += p;
0076 len -= p;
0077
0078 block_fn(sctx, sctx->buf, 1);
0079 }
0080
0081 blocks = len / SHA512_BLOCK_SIZE;
0082 len %= SHA512_BLOCK_SIZE;
0083
0084 if (blocks) {
0085 block_fn(sctx, data, blocks);
0086 data += blocks * SHA512_BLOCK_SIZE;
0087 }
0088 partial = 0;
0089 }
0090 if (len)
0091 memcpy(sctx->buf + partial, data, len);
0092
0093 return 0;
0094 }
0095
0096 static inline int sha512_base_do_finalize(struct shash_desc *desc,
0097 sha512_block_fn *block_fn)
0098 {
0099 const int bit_offset = SHA512_BLOCK_SIZE - sizeof(__be64[2]);
0100 struct sha512_state *sctx = shash_desc_ctx(desc);
0101 __be64 *bits = (__be64 *)(sctx->buf + bit_offset);
0102 unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
0103
0104 sctx->buf[partial++] = 0x80;
0105 if (partial > bit_offset) {
0106 memset(sctx->buf + partial, 0x0, SHA512_BLOCK_SIZE - partial);
0107 partial = 0;
0108
0109 block_fn(sctx, sctx->buf, 1);
0110 }
0111
0112 memset(sctx->buf + partial, 0x0, bit_offset - partial);
0113 bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61);
0114 bits[1] = cpu_to_be64(sctx->count[0] << 3);
0115 block_fn(sctx, sctx->buf, 1);
0116
0117 return 0;
0118 }
0119
0120 static inline int sha512_base_finish(struct shash_desc *desc, u8 *out)
0121 {
0122 unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
0123 struct sha512_state *sctx = shash_desc_ctx(desc);
0124 __be64 *digest = (__be64 *)out;
0125 int i;
0126
0127 for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be64))
0128 put_unaligned_be64(sctx->state[i], digest++);
0129
0130 memzero_explicit(sctx, sizeof(*sctx));
0131 return 0;
0132 }
0133
0134 #endif