History

crypto: lib/sha256 - Use generic code from sha256_base

Instead of duplicating the sha256 block processing code, reuse the common code from crypto/sha256_base.h.

Herbert Xu committed 3 years ago

Commit 6c19f3b1 parent 70d391a2 files · +55 −68Browse files

Unified · Split

2 files
  1. include/crypto/sha256_base.h +36 −14
  2. lib/crypto/sha256.c +19 −54
include/crypto/sha256_base.h+36−14
@@ -8,13 +8,12 @@ · 7 unmodified lines
88 #ifndef _CRYPTO_SHA256_BASE_H
99 #define _CRYPTO_SHA256_BASE_H
1010
11+#include <asm/byteorder.h>
12+#include <asm/unaligned.h>
1113 #include <crypto/internal/hash.h>
1214 #include <crypto/sha2.h>
13−#include <linux/crypto.h>
14−#include <linux/module.h>
1515 #include <linux/string.h>
16−
17−#include <asm/unaligned.h>
16+#include <linux/types.h>
1817
1918 typedef void (sha256_block_fn)(struct sha256_state *sst, u8 const *src,
2019 int blocks);
@@ -35,12 +34,11 @@ static inline int sha256_base_init(struct shash_desc *desc) · 14 unmodified lines
3534 return 0;
3635 }
3736
38−static inline int sha256_base_do_update(struct shash_desc *desc,
39− const u8 *data,
40− unsigned int len,
41− sha256_block_fn *block_fn)
37+static inline int lib_sha256_base_do_update(struct sha256_state *sctx,
38+ const u8 *data,
39+ unsigned int len,
40+ sha256_block_fn *block_fn)
4241 {
43− struct sha256_state *sctx = shash_desc_ctx(desc);
4442 unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
4543
4644 sctx->count += len;
@@ -73,11 +71,20 @@ static inline int sha256_base_do_update(struct shash_desc *desc, · 26 unmodified lines
7371 return 0;
7472 }
7573
76−static inline int sha256_base_do_finalize(struct shash_desc *desc,
77− sha256_block_fn *block_fn)
74+static inline int sha256_base_do_update(struct shash_desc *desc,
75+ const u8 *data,
76+ unsigned int len,
77+ sha256_block_fn *block_fn)
7878 {
79− const int bit_offset = SHA256_BLOCK_SIZE - sizeof(__be64);
8079 struct sha256_state *sctx = shash_desc_ctx(desc);
80+
81+ return lib_sha256_base_do_update(sctx, data, len, block_fn);
82+}
83+
84+static inline int lib_sha256_base_do_finalize(struct sha256_state *sctx,
85+ sha256_block_fn *block_fn)
86+{
87+ const int bit_offset = SHA256_BLOCK_SIZE - sizeof(__be64);
8188 __be64 *bits = (__be64 *)(sctx->buf + bit_offset);
8289 unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
8390
@@ -96,10 +103,17 @@ static inline int sha256_base_do_finalize(struct shash_desc *desc, · 12 unmodified lines
96103 return 0;
97104 }
98105
99−static inline int sha256_base_finish(struct shash_desc *desc, u8 *out)
106+static inline int sha256_base_do_finalize(struct shash_desc *desc,
107+ sha256_block_fn *block_fn)
100108 {
101− unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
102109 struct sha256_state *sctx = shash_desc_ctx(desc);
110+
111+ return lib_sha256_base_do_finalize(sctx, block_fn);
112+}
113+
114+static inline int lib_sha256_base_finish(struct sha256_state *sctx, u8 *out,
115+ unsigned int digest_size)
116+{
103117 __be32 *digest = (__be32 *)out;
104118 int i;
105119
@@ -110,4 +124,12 @@ static inline int sha256_base_finish(struct shash_desc *desc, u8 *out) · 4 unmodified lines
110124 return 0;
111125 }
112126
127+static inline int sha256_base_finish(struct shash_desc *desc, u8 *out)
128+{
129+ unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
130+ struct sha256_state *sctx = shash_desc_ctx(desc);
131+
132+ return lib_sha256_base_finish(sctx, out, digest_size);
133+}
134+
113135 #endif /* _CRYPTO_SHA256_BASE_H */
lib/crypto/sha256.c+19−54
@@ -11,12 +11,11 @@ · 10 unmodified lines
1111 * Copyright (c) 2014 Red Hat Inc.
1212 */
1313
14−#include <linux/bitops.h>
15−#include <linux/export.h>
14+#include <asm/unaligned.h>
15+#include <crypto/sha256_base.h>
16+#include <linux/kernel.h>
1617 #include <linux/module.h>
1718 #include <linux/string.h>
18−#include <crypto/sha2.h>
19−#include <asm/unaligned.h>
2019
2120 static const u32 SHA256_K[] = {
2221 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
@@ -119,74 +118,40 @@ static void sha256_transform(u32 *state, const u8 *input, u32 *W) · 96 unmodified lines
119118 state[4] += e; state[5] += f; state[6] += g; state[7] += h;
120119 }
121120
122−void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len)
121+static void sha256_transform_blocks(struct sha256_state *sctx,
122+ const u8 *input, int blocks)
123123 {
124− unsigned int partial, done;
125− const u8 *src;
126124 u32 W[64];
127125
128− partial = sctx->count & 0x3f;
129− sctx->count += len;
130− done = 0;
131− src = data;
132−
133− if ((partial + len) > 63) {
134− if (partial) {
135− done = -partial;
136− memcpy(sctx->buf + partial, data, done + 64);
137− src = sctx->buf;
138− }
126+ do {
127+ sha256_transform(sctx->state, input, W);
128+ input += SHA256_BLOCK_SIZE;
129+ } while (--blocks);
139130
140− do {
141− sha256_transform(sctx->state, src, W);
142− done += 64;
143− src = data + done;
144− } while (done + 63 < len);
145−
146− memzero_explicit(W, sizeof(W));
131+ memzero_explicit(W, sizeof(W));
132+}
147133
148− partial = 0;
149− }
150− memcpy(sctx->buf + partial, src, len - done);
134+void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len)
135+{
136+ lib_sha256_base_do_update(sctx, data, len, sha256_transform_blocks);
151137 }
152138 EXPORT_SYMBOL(sha256_update);
153139
154−static void __sha256_final(struct sha256_state *sctx, u8 *out, int digest_words)
140+static void __sha256_final(struct sha256_state *sctx, u8 *out, int digest_size)
155141 {
156− __be32 *dst = (__be32 *)out;
157− __be64 bits;
158− unsigned int index, pad_len;
159− int i;
160− static const u8 padding[64] = { 0x80, };
161−
162− /* Save number of bits */
163− bits = cpu_to_be64(sctx->count << 3);
164−
165− /* Pad out to 56 mod 64. */
166− index = sctx->count & 0x3f;
167− pad_len = (index < 56) ? (56 - index) : ((64+56) - index);
168− sha256_update(sctx, padding, pad_len);
169−
170− /* Append length (before padding) */
171− sha256_update(sctx, (const u8 *)&bits, sizeof(bits));
172−
173− /* Store state in digest */
174− for (i = 0; i < digest_words; i++)
175− put_unaligned_be32(sctx->state[i], &dst[i]);
176−
177− /* Zeroize sensitive information. */
178− memzero_explicit(sctx, sizeof(*sctx));
142+ lib_sha256_base_do_finalize(sctx, sha256_transform_blocks);
143+ lib_sha256_base_finish(sctx, out, digest_size);
179144 }
180145
181146 void sha256_final(struct sha256_state *sctx, u8 *out)
182147 {
183− __sha256_final(sctx, out, 8);
148+ __sha256_final(sctx, out, 32);
184149 }
185150 EXPORT_SYMBOL(sha256_final);
186151
187152 void sha224_final(struct sha256_state *sctx, u8 *out)
188153 {
189− __sha256_final(sctx, out, 7);
154+ __sha256_final(sctx, out, 28);
190155 }
191156 EXPORT_SYMBOL(sha224_final);
192157