Merge tag 'media/v5.3-2' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6-microblaze.git] / include / crypto / sha512_base.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * sha512_base.h - core logic for SHA-512 implementations
4  *
5  * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
6  */
7
8 #include <crypto/internal/hash.h>
9 #include <crypto/sha.h>
10 #include <linux/crypto.h>
11 #include <linux/module.h>
12
13 #include <asm/unaligned.h>
14
15 typedef void (sha512_block_fn)(struct sha512_state *sst, u8 const *src,
16                                int blocks);
17
18 static inline int sha384_base_init(struct shash_desc *desc)
19 {
20         struct sha512_state *sctx = shash_desc_ctx(desc);
21
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;
31
32         return 0;
33 }
34
35 static inline int sha512_base_init(struct shash_desc *desc)
36 {
37         struct sha512_state *sctx = shash_desc_ctx(desc);
38
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;
48
49         return 0;
50 }
51
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)
56 {
57         struct sha512_state *sctx = shash_desc_ctx(desc);
58         unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
59
60         sctx->count[0] += len;
61         if (sctx->count[0] < len)
62                 sctx->count[1]++;
63
64         if (unlikely((partial + len) >= SHA512_BLOCK_SIZE)) {
65                 int blocks;
66
67                 if (partial) {
68                         int p = SHA512_BLOCK_SIZE - partial;
69
70                         memcpy(sctx->buf + partial, data, p);
71                         data += p;
72                         len -= p;
73
74                         block_fn(sctx, sctx->buf, 1);
75                 }
76
77                 blocks = len / SHA512_BLOCK_SIZE;
78                 len %= SHA512_BLOCK_SIZE;
79
80                 if (blocks) {
81                         block_fn(sctx, data, blocks);
82                         data += blocks * SHA512_BLOCK_SIZE;
83                 }
84                 partial = 0;
85         }
86         if (len)
87                 memcpy(sctx->buf + partial, data, len);
88
89         return 0;
90 }
91
92 static inline int sha512_base_do_finalize(struct shash_desc *desc,
93                                           sha512_block_fn *block_fn)
94 {
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;
99
100         sctx->buf[partial++] = 0x80;
101         if (partial > bit_offset) {
102                 memset(sctx->buf + partial, 0x0, SHA512_BLOCK_SIZE - partial);
103                 partial = 0;
104
105                 block_fn(sctx, sctx->buf, 1);
106         }
107
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);
112
113         return 0;
114 }
115
116 static inline int sha512_base_finish(struct shash_desc *desc, u8 *out)
117 {
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;
122
123         for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be64))
124                 put_unaligned_be64(sctx->state[i], digest++);
125
126         *sctx = (struct sha512_state){};
127         return 0;
128 }