]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - crypto/digest.c
[CRYPTO] all: Pass tfm instead of ctx to algorithms
[mirror_ubuntu-jammy-kernel.git] / crypto / digest.c
CommitLineData
1da177e4
LT
1/*
2 * Cryptographic API.
3 *
4 * Digest operations.
5 *
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 */
14#include <linux/crypto.h>
15#include <linux/mm.h>
16#include <linux/errno.h>
17#include <linux/highmem.h>
18#include <asm/scatterlist.h>
19#include "internal.h"
20
21static void init(struct crypto_tfm *tfm)
22{
6c2bb98b 23 tfm->__crt_alg->cra_digest.dia_init(tfm);
1da177e4
LT
24}
25
26static void update(struct crypto_tfm *tfm,
27 struct scatterlist *sg, unsigned int nsg)
28{
29 unsigned int i;
e1147d8f 30 unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
1da177e4
LT
31
32 for (i = 0; i < nsg; i++) {
33
34 struct page *pg = sg[i].page;
35 unsigned int offset = sg[i].offset;
36 unsigned int l = sg[i].length;
37
38 do {
39 unsigned int bytes_from_page = min(l, ((unsigned int)
40 (PAGE_SIZE)) -
41 offset);
e1147d8f
AN
42 char *src = crypto_kmap(pg, 0);
43 char *p = src + offset;
1da177e4 44
e1147d8f
AN
45 if (unlikely(offset & alignmask)) {
46 unsigned int bytes =
47 alignmask + 1 - (offset & alignmask);
48 bytes = min(bytes, bytes_from_page);
6c2bb98b
HX
49 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
50 bytes);
e1147d8f
AN
51 p += bytes;
52 bytes_from_page -= bytes;
53 l -= bytes;
54 }
6c2bb98b
HX
55 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
56 bytes_from_page);
e1147d8f 57 crypto_kunmap(src, 0);
1da177e4
LT
58 crypto_yield(tfm);
59 offset = 0;
60 pg++;
61 l -= bytes_from_page;
62 } while (l > 0);
63 }
64}
65
66static void final(struct crypto_tfm *tfm, u8 *out)
67{
e1147d8f
AN
68 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
69 if (unlikely((unsigned long)out & alignmask)) {
70 unsigned int size = crypto_tfm_alg_digestsize(tfm);
71 u8 buffer[size + alignmask];
72 u8 *dst = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
73 tfm->__crt_alg->cra_digest.dia_final(crypto_tfm_ctx(tfm), dst);
74 memcpy(out, dst, size);
75 } else
76 tfm->__crt_alg->cra_digest.dia_final(crypto_tfm_ctx(tfm), out);
1da177e4
LT
77}
78
79static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
80{
81 u32 flags;
82 if (tfm->__crt_alg->cra_digest.dia_setkey == NULL)
83 return -ENOSYS;
6c2bb98b 84 return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen, &flags);
1da177e4
LT
85}
86
87static void digest(struct crypto_tfm *tfm,
88 struct scatterlist *sg, unsigned int nsg, u8 *out)
89{
e1147d8f
AN
90 init(tfm);
91 update(tfm, sg, nsg);
92 final(tfm, out);
1da177e4
LT
93}
94
95int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags)
96{
97 return flags ? -EINVAL : 0;
98}
99
100int crypto_init_digest_ops(struct crypto_tfm *tfm)
101{
102 struct digest_tfm *ops = &tfm->crt_digest;
103
104 ops->dit_init = init;
105 ops->dit_update = update;
106 ops->dit_final = final;
107 ops->dit_digest = digest;
108 ops->dit_setkey = setkey;
109
110 return crypto_alloc_hmac_block(tfm);
111}
112
113void crypto_exit_digest_ops(struct crypto_tfm *tfm)
114{
115 crypto_free_hmac_block(tfm);
116}