Blame view
crypto/digest.c
3.66 KB
1da177e4c Linux-2.6.12-rc2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/* * Cryptographic API. * * Digest operations. * * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * */ |
055bcee31 [CRYPTO] digest: ... |
14 |
|
1da177e4c Linux-2.6.12-rc2 |
15 16 |
#include <linux/mm.h> #include <linux/errno.h> |
fb469840b [CRYPTO] all: Che... |
17 |
#include <linux/hardirq.h> |
1da177e4c Linux-2.6.12-rc2 |
18 |
#include <linux/highmem.h> |
fb469840b [CRYPTO] all: Che... |
19 |
#include <linux/kernel.h> |
055bcee31 [CRYPTO] digest: ... |
20 21 |
#include <linux/module.h> #include <linux/scatterlist.h> |
1da177e4c Linux-2.6.12-rc2 |
22 |
#include "internal.h" |
055bcee31 [CRYPTO] digest: ... |
23 24 25 26 27 28 29 |
static int init(struct hash_desc *desc) { struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); tfm->__crt_alg->cra_digest.dia_init(tfm); return 0; } |
fb469840b [CRYPTO] all: Che... |
30 31 |
static int update2(struct hash_desc *desc, struct scatterlist *sg, unsigned int nbytes) |
055bcee31 [CRYPTO] digest: ... |
32 33 |
{ struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); |
e1147d8f4 [CRYPTO] digest: ... |
34 |
unsigned int alignmask = crypto_tfm_alg_alignmask(tfm); |
1da177e4c Linux-2.6.12-rc2 |
35 |
|
055bcee31 [CRYPTO] digest: ... |
36 37 38 39 |
if (!nbytes) return 0; for (;;) { |
78c2f0b8c [SG] Update crypt... |
40 |
struct page *pg = sg_page(sg); |
055bcee31 [CRYPTO] digest: ... |
41 42 |
unsigned int offset = sg->offset; unsigned int l = sg->length; |
1da177e4c Linux-2.6.12-rc2 |
43 |
|
055bcee31 [CRYPTO] digest: ... |
44 45 46 |
if (unlikely(l > nbytes)) l = nbytes; nbytes -= l; |
1da177e4c Linux-2.6.12-rc2 |
47 48 49 50 51 |
do { unsigned int bytes_from_page = min(l, ((unsigned int) (PAGE_SIZE)) - offset); |
e1147d8f4 [CRYPTO] digest: ... |
52 53 |
char *src = crypto_kmap(pg, 0); char *p = src + offset; |
1da177e4c Linux-2.6.12-rc2 |
54 |
|
e1147d8f4 [CRYPTO] digest: ... |
55 56 57 58 |
if (unlikely(offset & alignmask)) { unsigned int bytes = alignmask + 1 - (offset & alignmask); bytes = min(bytes, bytes_from_page); |
6c2bb98bc [CRYPTO] all: Pas... |
59 60 |
tfm->__crt_alg->cra_digest.dia_update(tfm, p, bytes); |
e1147d8f4 [CRYPTO] digest: ... |
61 62 63 64 |
p += bytes; bytes_from_page -= bytes; l -= bytes; } |
6c2bb98bc [CRYPTO] all: Pas... |
65 66 |
tfm->__crt_alg->cra_digest.dia_update(tfm, p, bytes_from_page); |
e1147d8f4 [CRYPTO] digest: ... |
67 |
crypto_kunmap(src, 0); |
055bcee31 [CRYPTO] digest: ... |
68 |
crypto_yield(desc->flags); |
1da177e4c Linux-2.6.12-rc2 |
69 70 71 72 |
offset = 0; pg++; l -= bytes_from_page; } while (l > 0); |
055bcee31 [CRYPTO] digest: ... |
73 74 75 |
if (!nbytes) break; |
468577abe [CRYPTO] scatterw... |
76 |
sg = sg_next(sg); |
1da177e4c Linux-2.6.12-rc2 |
77 |
} |
055bcee31 [CRYPTO] digest: ... |
78 79 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
80 |
} |
fb469840b [CRYPTO] all: Che... |
81 82 83 84 85 86 87 |
static int update(struct hash_desc *desc, struct scatterlist *sg, unsigned int nbytes) { if (WARN_ON_ONCE(in_irq())) return -EDEADLK; return update2(desc, sg, nbytes); } |
055bcee31 [CRYPTO] digest: ... |
88 |
static int final(struct hash_desc *desc, u8 *out) |
1da177e4c Linux-2.6.12-rc2 |
89 |
{ |
055bcee31 [CRYPTO] digest: ... |
90 |
struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); |
e1147d8f4 [CRYPTO] digest: ... |
91 |
unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
ee7564166 [CRYPTO] digest: ... |
92 |
struct digest_alg *digest = &tfm->__crt_alg->cra_digest; |
e1147d8f4 [CRYPTO] digest: ... |
93 |
if (unlikely((unsigned long)out & alignmask)) { |
ee7564166 [CRYPTO] digest: ... |
94 95 96 97 98 99 100 |
unsigned long align = alignmask + 1; unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm); u8 *dst = (u8 *)ALIGN(addr, align) + ALIGN(tfm->__crt_alg->cra_ctxsize, align); digest->dia_final(tfm, dst); memcpy(out, dst, digest->dia_digestsize); |
e1147d8f4 [CRYPTO] digest: ... |
101 |
} else |
ee7564166 [CRYPTO] digest: ... |
102 |
digest->dia_final(tfm, out); |
055bcee31 [CRYPTO] digest: ... |
103 104 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
105 |
} |
055bcee31 [CRYPTO] digest: ... |
106 |
static int nosetkey(struct crypto_hash *tfm, const u8 *key, unsigned int keylen) |
560c06ae1 [CRYPTO] api: Get... |
107 |
{ |
055bcee31 [CRYPTO] digest: ... |
108 |
crypto_hash_clear_flags(tfm, CRYPTO_TFM_RES_MASK); |
560c06ae1 [CRYPTO] api: Get... |
109 110 |
return -ENOSYS; } |
055bcee31 [CRYPTO] digest: ... |
111 |
static int setkey(struct crypto_hash *hash, const u8 *key, unsigned int keylen) |
1da177e4c Linux-2.6.12-rc2 |
112 |
{ |
055bcee31 [CRYPTO] digest: ... |
113 114 115 |
struct crypto_tfm *tfm = crypto_hash_tfm(hash); crypto_hash_clear_flags(hash, CRYPTO_TFM_RES_MASK); |
560c06ae1 [CRYPTO] api: Get... |
116 |
return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen); |
1da177e4c Linux-2.6.12-rc2 |
117 |
} |
055bcee31 [CRYPTO] digest: ... |
118 119 |
static int digest(struct hash_desc *desc, struct scatterlist *sg, unsigned int nbytes, u8 *out) |
1da177e4c Linux-2.6.12-rc2 |
120 |
{ |
fb469840b [CRYPTO] all: Che... |
121 122 |
if (WARN_ON_ONCE(in_irq())) return -EDEADLK; |
055bcee31 [CRYPTO] digest: ... |
123 |
init(desc); |
fb469840b [CRYPTO] all: Che... |
124 |
update2(desc, sg, nbytes); |
055bcee31 [CRYPTO] digest: ... |
125 |
return final(desc, out); |
1da177e4c Linux-2.6.12-rc2 |
126 |
} |
1da177e4c Linux-2.6.12-rc2 |
127 128 |
int crypto_init_digest_ops(struct crypto_tfm *tfm) { |
055bcee31 [CRYPTO] digest: ... |
129 |
struct hash_tfm *ops = &tfm->crt_hash; |
560c06ae1 [CRYPTO] api: Get... |
130 |
struct digest_alg *dalg = &tfm->__crt_alg->cra_digest; |
055bcee31 [CRYPTO] digest: ... |
131 132 133 |
if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm)) return -EINVAL; |
1da177e4c Linux-2.6.12-rc2 |
134 |
|
055bcee31 [CRYPTO] digest: ... |
135 136 137 138 139 140 |
ops->init = init; ops->update = update; ops->final = final; ops->digest = digest; ops->setkey = dalg->dia_setkey ? setkey : nosetkey; ops->digestsize = dalg->dia_digestsize; |
1da177e4c Linux-2.6.12-rc2 |
141 |
|
8425165df [CRYPTO] digest: ... |
142 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
143 144 145 146 |
} void crypto_exit_digest_ops(struct crypto_tfm *tfm) { |
1da177e4c Linux-2.6.12-rc2 |
147 |
} |