Skip to content

Commit bd9d20d

Browse files
Kensanherbertx
authored andcommitted
crypto: sha512 - Switch to shash
This patch changes sha512 and sha384 to the new shash interface. Signed-off-by: Adrian-Ken Rueegsegger <ken@codelabs.ch> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent f9e2bca commit bd9d20d

2 files changed

Lines changed: 60 additions & 54 deletions

File tree

‎crypto/Kconfig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ config CRYPTO_SHA256
369369

370370
config CRYPTO_SHA512
371371
tristate "SHA384 and SHA512 digest algorithms"
372-
select CRYPTO_ALGAPI
372+
select CRYPTO_HASH
373373
help
374374
SHA512 secure hash standard (DFIPS 180-2).
375375

‎crypto/sha512_generic.c‎

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* later version.
1111
*
1212
*/
13-
13+
#include <crypto/internal/hash.h>
1414
#include <linux/kernel.h>
1515
#include <linux/module.h>
1616
#include <linux/mm.h>
@@ -138,10 +138,10 @@ sha512_transform(u64 *state, const u8 *input)
138138
put_cpu_var(msg_schedule);
139139
}
140140

141-
static void
142-
sha512_init(struct crypto_tfm *tfm)
141+
static int
142+
sha512_init(struct shash_desc *desc)
143143
{
144-
struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
144+
struct sha512_ctx *sctx = shash_desc_ctx(desc);
145145
sctx->state[0] = SHA512_H0;
146146
sctx->state[1] = SHA512_H1;
147147
sctx->state[2] = SHA512_H2;
@@ -151,12 +151,14 @@ sha512_init(struct crypto_tfm *tfm)
151151
sctx->state[6] = SHA512_H6;
152152
sctx->state[7] = SHA512_H7;
153153
sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
154+
155+
return 0;
154156
}
155157

156-
static void
157-
sha384_init(struct crypto_tfm *tfm)
158+
static int
159+
sha384_init(struct shash_desc *desc)
158160
{
159-
struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
161+
struct sha512_ctx *sctx = shash_desc_ctx(desc);
160162
sctx->state[0] = SHA384_H0;
161163
sctx->state[1] = SHA384_H1;
162164
sctx->state[2] = SHA384_H2;
@@ -166,12 +168,14 @@ sha384_init(struct crypto_tfm *tfm)
166168
sctx->state[6] = SHA384_H6;
167169
sctx->state[7] = SHA384_H7;
168170
sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
171+
172+
return 0;
169173
}
170174

171-
static void
172-
sha512_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
175+
static int
176+
sha512_update(struct shash_desc *desc, const u8 *data, unsigned int len)
173177
{
174-
struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
178+
struct sha512_ctx *sctx = shash_desc_ctx(desc);
175179

176180
unsigned int i, index, part_len;
177181

@@ -203,12 +207,14 @@ sha512_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
203207

204208
/* Buffer remaining input */
205209
memcpy(&sctx->buf[index], &data[i], len - i);
210+
211+
return 0;
206212
}
207213

208-
static void
209-
sha512_final(struct crypto_tfm *tfm, u8 *hash)
214+
static int
215+
sha512_final(struct shash_desc *desc, u8 *hash)
210216
{
211-
struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
217+
struct sha512_ctx *sctx = shash_desc_ctx(desc);
212218
static u8 padding[128] = { 0x80, };
213219
__be64 *dst = (__be64 *)hash;
214220
__be32 bits[4];
@@ -224,77 +230,77 @@ sha512_final(struct crypto_tfm *tfm, u8 *hash)
224230
/* Pad out to 112 mod 128. */
225231
index = (sctx->count[0] >> 3) & 0x7f;
226232
pad_len = (index < 112) ? (112 - index) : ((128+112) - index);
227-
sha512_update(tfm, padding, pad_len);
233+
sha512_update(desc, padding, pad_len);
228234

229235
/* Append length (before padding) */
230-
sha512_update(tfm, (const u8 *)bits, sizeof(bits));
236+
sha512_update(desc, (const u8 *)bits, sizeof(bits));
231237

232238
/* Store state in digest */
233239
for (i = 0; i < 8; i++)
234240
dst[i] = cpu_to_be64(sctx->state[i]);
235241

236242
/* Zeroize sensitive information. */
237243
memset(sctx, 0, sizeof(struct sha512_ctx));
244+
245+
return 0;
238246
}
239247

240-
static void sha384_final(struct crypto_tfm *tfm, u8 *hash)
248+
static int sha384_final(struct shash_desc *desc, u8 *hash)
241249
{
242-
u8 D[64];
250+
u8 D[64];
243251

244-
sha512_final(tfm, D);
252+
sha512_final(desc, D);
245253

246-
memcpy(hash, D, 48);
247-
memset(D, 0, 64);
254+
memcpy(hash, D, 48);
255+
memset(D, 0, 64);
256+
257+
return 0;
248258
}
249259

250-
static struct crypto_alg sha512 = {
251-
.cra_name = "sha512",
252-
.cra_flags = CRYPTO_ALG_TYPE_DIGEST,
253-
.cra_blocksize = SHA512_BLOCK_SIZE,
254-
.cra_ctxsize = sizeof(struct sha512_ctx),
255-
.cra_module = THIS_MODULE,
256-
.cra_alignmask = 3,
257-
.cra_list = LIST_HEAD_INIT(sha512.cra_list),
258-
.cra_u = { .digest = {
259-
.dia_digestsize = SHA512_DIGEST_SIZE,
260-
.dia_init = sha512_init,
261-
.dia_update = sha512_update,
262-
.dia_final = sha512_final }
263-
}
260+
static struct shash_alg sha512 = {
261+
.digestsize = SHA512_DIGEST_SIZE,
262+
.init = sha512_init,
263+
.update = sha512_update,
264+
.final = sha512_final,
265+
.descsize = sizeof(struct sha512_ctx),
266+
.base = {
267+
.cra_name = "sha512",
268+
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
269+
.cra_blocksize = SHA512_BLOCK_SIZE,
270+
.cra_module = THIS_MODULE,
271+
}
264272
};
265273

266-
static struct crypto_alg sha384 = {
267-
.cra_name = "sha384",
268-
.cra_flags = CRYPTO_ALG_TYPE_DIGEST,
269-
.cra_blocksize = SHA384_BLOCK_SIZE,
270-
.cra_ctxsize = sizeof(struct sha512_ctx),
271-
.cra_alignmask = 3,
272-
.cra_module = THIS_MODULE,
273-
.cra_list = LIST_HEAD_INIT(sha384.cra_list),
274-
.cra_u = { .digest = {
275-
.dia_digestsize = SHA384_DIGEST_SIZE,
276-
.dia_init = sha384_init,
277-
.dia_update = sha512_update,
278-
.dia_final = sha384_final }
279-
}
274+
static struct shash_alg sha384 = {
275+
.digestsize = SHA384_DIGEST_SIZE,
276+
.init = sha384_init,
277+
.update = sha512_update,
278+
.final = sha384_final,
279+
.descsize = sizeof(struct sha512_ctx),
280+
.base = {
281+
.cra_name = "sha384",
282+
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
283+
.cra_blocksize = SHA384_BLOCK_SIZE,
284+
.cra_module = THIS_MODULE,
285+
}
280286
};
281287

282288
static int __init sha512_generic_mod_init(void)
283289
{
284290
int ret = 0;
285291

286-
if ((ret = crypto_register_alg(&sha384)) < 0)
292+
if ((ret = crypto_register_shash(&sha384)) < 0)
287293
goto out;
288-
if ((ret = crypto_register_alg(&sha512)) < 0)
289-
crypto_unregister_alg(&sha384);
294+
if ((ret = crypto_register_shash(&sha512)) < 0)
295+
crypto_unregister_shash(&sha384);
290296
out:
291297
return ret;
292298
}
293299

294300
static void __exit sha512_generic_mod_fini(void)
295301
{
296-
crypto_unregister_alg(&sha384);
297-
crypto_unregister_alg(&sha512);
302+
crypto_unregister_shash(&sha384);
303+
crypto_unregister_shash(&sha512);
298304
}
299305

300306
module_init(sha512_generic_mod_init);

0 commit comments

Comments
 (0)