1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include "rtl_crypto.h"
16#include <linux/mm.h>
17#include <linux/errno.h>
18#include <linux/highmem.h>
19#include <asm/scatterlist.h>
20#include "internal.h"
21
22static void init(struct crypto_tfm *tfm)
23{
24 tfm->__crt_alg->cra_digest.dia_init(crypto_tfm_ctx(tfm));
25}
26
27static void update(struct crypto_tfm *tfm,
28 struct scatterlist *sg, unsigned int nsg)
29{
30 unsigned int i;
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);
42 char *p = kmap_atomic(pg) + offset;
43
44 tfm->__crt_alg->cra_digest.dia_update
45 (crypto_tfm_ctx(tfm), p,
46 bytes_from_page);
47 kunmap_atomic(p);
48 crypto_yield(tfm);
49 offset = 0;
50 pg++;
51 l -= bytes_from_page;
52 } while (l > 0);
53 }
54}
55
56static void final(struct crypto_tfm *tfm, u8 *out)
57{
58 tfm->__crt_alg->cra_digest.dia_final(crypto_tfm_ctx(tfm), out);
59}
60
61static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
62{
63 u32 flags;
64 if (tfm->__crt_alg->cra_digest.dia_setkey == NULL)
65 return -ENOSYS;
66 return tfm->__crt_alg->cra_digest.dia_setkey(crypto_tfm_ctx(tfm),
67 key, keylen, &flags);
68}
69
70static void digest(struct crypto_tfm *tfm,
71 struct scatterlist *sg, unsigned int nsg, u8 *out)
72{
73 unsigned int i;
74
75 tfm->crt_digest.dit_init(tfm);
76
77 for (i = 0; i < nsg; i++) {
78 char *p = kmap_atomic(sg[i].page) + sg[i].offset;
79 tfm->__crt_alg->cra_digest.dia_update(crypto_tfm_ctx(tfm),
80 p, sg[i].length);
81 kunmap_atomic(p);
82 crypto_yield(tfm);
83 }
84 crypto_digest_final(tfm, out);
85}
86
87int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags)
88{
89 return flags ? -EINVAL : 0;
90}
91
92int crypto_init_digest_ops(struct crypto_tfm *tfm)
93{
94 struct digest_tfm *ops = &tfm->crt_digest;
95
96 ops->dit_init = init;
97 ops->dit_update = update;
98 ops->dit_final = final;
99 ops->dit_digest = digest;
100 ops->dit_setkey = setkey;
101
102 return crypto_alloc_hmac_block(tfm);
103}
104
105void crypto_exit_digest_ops(struct crypto_tfm *tfm)
106{
107 crypto_free_hmac_block(tfm);
108}
109