]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Cryptographic API. | |
3 | * | |
4 | * Digest operations. | |
5 | * | |
6 | * Copyright (c) 2002 James Morris <[email protected]> | |
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 | */ | |
055bcee3 | 14 | |
1da177e4 LT |
15 | #include <linux/mm.h> |
16 | #include <linux/errno.h> | |
fb469840 | 17 | #include <linux/hardirq.h> |
1da177e4 | 18 | #include <linux/highmem.h> |
fb469840 | 19 | #include <linux/kernel.h> |
055bcee3 HX |
20 | #include <linux/module.h> |
21 | #include <linux/scatterlist.h> | |
22 | ||
1da177e4 | 23 | #include "internal.h" |
055bcee3 | 24 | #include "scatterwalk.h" |
1da177e4 | 25 | |
055bcee3 HX |
26 | static int init(struct hash_desc *desc) |
27 | { | |
28 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); | |
29 | ||
30 | tfm->__crt_alg->cra_digest.dia_init(tfm); | |
31 | return 0; | |
32 | } | |
33 | ||
fb469840 HX |
34 | static int update2(struct hash_desc *desc, |
35 | struct scatterlist *sg, unsigned int nbytes) | |
055bcee3 HX |
36 | { |
37 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); | |
e1147d8f | 38 | unsigned int alignmask = crypto_tfm_alg_alignmask(tfm); |
1da177e4 | 39 | |
055bcee3 HX |
40 | if (!nbytes) |
41 | return 0; | |
42 | ||
43 | for (;;) { | |
44 | struct page *pg = sg->page; | |
45 | unsigned int offset = sg->offset; | |
46 | unsigned int l = sg->length; | |
1da177e4 | 47 | |
055bcee3 HX |
48 | if (unlikely(l > nbytes)) |
49 | l = nbytes; | |
50 | nbytes -= l; | |
1da177e4 LT |
51 | |
52 | do { | |
53 | unsigned int bytes_from_page = min(l, ((unsigned int) | |
54 | (PAGE_SIZE)) - | |
55 | offset); | |
e1147d8f AN |
56 | char *src = crypto_kmap(pg, 0); |
57 | char *p = src + offset; | |
1da177e4 | 58 | |
e1147d8f AN |
59 | if (unlikely(offset & alignmask)) { |
60 | unsigned int bytes = | |
61 | alignmask + 1 - (offset & alignmask); | |
62 | bytes = min(bytes, bytes_from_page); | |
6c2bb98b HX |
63 | tfm->__crt_alg->cra_digest.dia_update(tfm, p, |
64 | bytes); | |
e1147d8f AN |
65 | p += bytes; |
66 | bytes_from_page -= bytes; | |
67 | l -= bytes; | |
68 | } | |
6c2bb98b HX |
69 | tfm->__crt_alg->cra_digest.dia_update(tfm, p, |
70 | bytes_from_page); | |
e1147d8f | 71 | crypto_kunmap(src, 0); |
055bcee3 | 72 | crypto_yield(desc->flags); |
1da177e4 LT |
73 | offset = 0; |
74 | pg++; | |
75 | l -= bytes_from_page; | |
76 | } while (l > 0); | |
055bcee3 HX |
77 | |
78 | if (!nbytes) | |
79 | break; | |
80 | sg = sg_next(sg); | |
1da177e4 | 81 | } |
055bcee3 HX |
82 | |
83 | return 0; | |
1da177e4 LT |
84 | } |
85 | ||
fb469840 HX |
86 | static int update(struct hash_desc *desc, |
87 | struct scatterlist *sg, unsigned int nbytes) | |
88 | { | |
89 | if (WARN_ON_ONCE(in_irq())) | |
90 | return -EDEADLK; | |
91 | return update2(desc, sg, nbytes); | |
92 | } | |
93 | ||
055bcee3 | 94 | static int final(struct hash_desc *desc, u8 *out) |
1da177e4 | 95 | { |
055bcee3 | 96 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); |
e1147d8f | 97 | unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
ee756416 HX |
98 | struct digest_alg *digest = &tfm->__crt_alg->cra_digest; |
99 | ||
e1147d8f | 100 | if (unlikely((unsigned long)out & alignmask)) { |
ee756416 HX |
101 | unsigned long align = alignmask + 1; |
102 | unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm); | |
103 | u8 *dst = (u8 *)ALIGN(addr, align) + | |
104 | ALIGN(tfm->__crt_alg->cra_ctxsize, align); | |
105 | ||
106 | digest->dia_final(tfm, dst); | |
107 | memcpy(out, dst, digest->dia_digestsize); | |
e1147d8f | 108 | } else |
ee756416 | 109 | digest->dia_final(tfm, out); |
055bcee3 HX |
110 | |
111 | return 0; | |
1da177e4 LT |
112 | } |
113 | ||
055bcee3 | 114 | static int nosetkey(struct crypto_hash *tfm, const u8 *key, unsigned int keylen) |
560c06ae | 115 | { |
055bcee3 | 116 | crypto_hash_clear_flags(tfm, CRYPTO_TFM_RES_MASK); |
560c06ae HX |
117 | return -ENOSYS; |
118 | } | |
119 | ||
055bcee3 | 120 | static int setkey(struct crypto_hash *hash, const u8 *key, unsigned int keylen) |
1da177e4 | 121 | { |
055bcee3 HX |
122 | struct crypto_tfm *tfm = crypto_hash_tfm(hash); |
123 | ||
124 | crypto_hash_clear_flags(hash, CRYPTO_TFM_RES_MASK); | |
560c06ae | 125 | return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen); |
1da177e4 LT |
126 | } |
127 | ||
055bcee3 HX |
128 | static int digest(struct hash_desc *desc, |
129 | struct scatterlist *sg, unsigned int nbytes, u8 *out) | |
1da177e4 | 130 | { |
fb469840 HX |
131 | if (WARN_ON_ONCE(in_irq())) |
132 | return -EDEADLK; | |
133 | ||
055bcee3 | 134 | init(desc); |
fb469840 | 135 | update2(desc, sg, nbytes); |
055bcee3 | 136 | return final(desc, out); |
1da177e4 LT |
137 | } |
138 | ||
1da177e4 LT |
139 | int crypto_init_digest_ops(struct crypto_tfm *tfm) |
140 | { | |
055bcee3 | 141 | struct hash_tfm *ops = &tfm->crt_hash; |
560c06ae | 142 | struct digest_alg *dalg = &tfm->__crt_alg->cra_digest; |
055bcee3 HX |
143 | |
144 | if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm)) | |
145 | return -EINVAL; | |
1da177e4 | 146 | |
055bcee3 HX |
147 | ops->init = init; |
148 | ops->update = update; | |
149 | ops->final = final; | |
150 | ops->digest = digest; | |
151 | ops->setkey = dalg->dia_setkey ? setkey : nosetkey; | |
152 | ops->digestsize = dalg->dia_digestsize; | |
1da177e4 | 153 | |
8425165d | 154 | return 0; |
1da177e4 LT |
155 | } |
156 | ||
157 | void crypto_exit_digest_ops(struct crypto_tfm *tfm) | |
158 | { | |
1da177e4 | 159 | } |