btrfs: migrate the block group ref counting stuff
[linux/fpc-iii.git] / include / crypto / sha512_base.h
blob099be8027f3f2da796a30beec62fa61062dfa5c3
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * sha512_base.h - core logic for SHA-512 implementations
5 * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
6 */
8 #include <crypto/internal/hash.h>
9 #include <crypto/sha.h>
10 #include <linux/crypto.h>
11 #include <linux/module.h>
13 #include <asm/unaligned.h>
15 typedef void (sha512_block_fn)(struct sha512_state *sst, u8 const *src,
16 int blocks);
18 static inline int sha384_base_init(struct shash_desc *desc)
20 struct sha512_state *sctx = shash_desc_ctx(desc);
22 sctx->state[0] = SHA384_H0;
23 sctx->state[1] = SHA384_H1;
24 sctx->state[2] = SHA384_H2;
25 sctx->state[3] = SHA384_H3;
26 sctx->state[4] = SHA384_H4;
27 sctx->state[5] = SHA384_H5;
28 sctx->state[6] = SHA384_H6;
29 sctx->state[7] = SHA384_H7;
30 sctx->count[0] = sctx->count[1] = 0;
32 return 0;
35 static inline int sha512_base_init(struct shash_desc *desc)
37 struct sha512_state *sctx = shash_desc_ctx(desc);
39 sctx->state[0] = SHA512_H0;
40 sctx->state[1] = SHA512_H1;
41 sctx->state[2] = SHA512_H2;
42 sctx->state[3] = SHA512_H3;
43 sctx->state[4] = SHA512_H4;
44 sctx->state[5] = SHA512_H5;
45 sctx->state[6] = SHA512_H6;
46 sctx->state[7] = SHA512_H7;
47 sctx->count[0] = sctx->count[1] = 0;
49 return 0;
52 static inline int sha512_base_do_update(struct shash_desc *desc,
53 const u8 *data,
54 unsigned int len,
55 sha512_block_fn *block_fn)
57 struct sha512_state *sctx = shash_desc_ctx(desc);
58 unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
60 sctx->count[0] += len;
61 if (sctx->count[0] < len)
62 sctx->count[1]++;
64 if (unlikely((partial + len) >= SHA512_BLOCK_SIZE)) {
65 int blocks;
67 if (partial) {
68 int p = SHA512_BLOCK_SIZE - partial;
70 memcpy(sctx->buf + partial, data, p);
71 data += p;
72 len -= p;
74 block_fn(sctx, sctx->buf, 1);
77 blocks = len / SHA512_BLOCK_SIZE;
78 len %= SHA512_BLOCK_SIZE;
80 if (blocks) {
81 block_fn(sctx, data, blocks);
82 data += blocks * SHA512_BLOCK_SIZE;
84 partial = 0;
86 if (len)
87 memcpy(sctx->buf + partial, data, len);
89 return 0;
92 static inline int sha512_base_do_finalize(struct shash_desc *desc,
93 sha512_block_fn *block_fn)
95 const int bit_offset = SHA512_BLOCK_SIZE - sizeof(__be64[2]);
96 struct sha512_state *sctx = shash_desc_ctx(desc);
97 __be64 *bits = (__be64 *)(sctx->buf + bit_offset);
98 unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
100 sctx->buf[partial++] = 0x80;
101 if (partial > bit_offset) {
102 memset(sctx->buf + partial, 0x0, SHA512_BLOCK_SIZE - partial);
103 partial = 0;
105 block_fn(sctx, sctx->buf, 1);
108 memset(sctx->buf + partial, 0x0, bit_offset - partial);
109 bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61);
110 bits[1] = cpu_to_be64(sctx->count[0] << 3);
111 block_fn(sctx, sctx->buf, 1);
113 return 0;
116 static inline int sha512_base_finish(struct shash_desc *desc, u8 *out)
118 unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
119 struct sha512_state *sctx = shash_desc_ctx(desc);
120 __be64 *digest = (__be64 *)out;
121 int i;
123 for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be64))
124 put_unaligned_be64(sctx->state[i], digest++);
126 *sctx = (struct sha512_state){};
127 return 0;