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