blob: b526cc348b79313e21bea12808976f392b5505a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Herbert Xu055bcee2006-08-19 22:24:23 +100014
Herbert Xu42c271c2007-12-07 18:52:49 +080015#include <crypto/scatterwalk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/mm.h>
17#include <linux/errno.h>
Herbert Xufb469842006-12-10 10:45:28 +110018#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/highmem.h>
Herbert Xufb469842006-12-10 10:45:28 +110020#include <linux/kernel.h>
Herbert Xu055bcee2006-08-19 22:24:23 +100021#include <linux/module.h>
22#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Adrian Bunkbc97f192008-03-05 19:05:54 +080024#include "internal.h"
25
Herbert Xu055bcee2006-08-19 22:24:23 +100026static int init(struct hash_desc *desc)
27{
28 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
29
Herbert Xu6c2bb982006-05-16 22:09:29 +100030 tfm->__crt_alg->cra_digest.dia_init(tfm);
Herbert Xu055bcee2006-08-19 22:24:23 +100031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
Herbert Xufb469842006-12-10 10:45:28 +110034static int update2(struct hash_desc *desc,
35 struct scatterlist *sg, unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Herbert Xu055bcee2006-08-19 22:24:23 +100037 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100038 unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Herbert Xu055bcee2006-08-19 22:24:23 +100040 if (!nbytes)
41 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Herbert Xu055bcee2006-08-19 22:24:23 +100043 for (;;) {
Jens Axboe78c2f0b2007-10-22 19:40:16 +020044 struct page *pg = sg_page(sg);
Herbert Xu055bcee2006-08-19 22:24:23 +100045 unsigned int offset = sg->offset;
46 unsigned int l = sg->length;
47
48 if (unlikely(l > nbytes))
49 l = nbytes;
50 nbytes -= l;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 do {
53 unsigned int bytes_from_page = min(l, ((unsigned int)
54 (PAGE_SIZE)) -
55 offset);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100056 char *src = crypto_kmap(pg, 0);
57 char *p = src + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100059 if (unlikely(offset & alignmask)) {
60 unsigned int bytes =
61 alignmask + 1 - (offset & alignmask);
62 bytes = min(bytes, bytes_from_page);
Herbert Xu6c2bb982006-05-16 22:09:29 +100063 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
64 bytes);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100065 p += bytes;
66 bytes_from_page -= bytes;
67 l -= bytes;
68 }
Herbert Xu6c2bb982006-05-16 22:09:29 +100069 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
70 bytes_from_page);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100071 crypto_kunmap(src, 0);
Herbert Xu055bcee2006-08-19 22:24:23 +100072 crypto_yield(desc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 offset = 0;
74 pg++;
75 l -= bytes_from_page;
76 } while (l > 0);
Herbert Xu055bcee2006-08-19 22:24:23 +100077
78 if (!nbytes)
79 break;
Herbert Xub2ab4a52007-12-05 20:59:25 +110080 sg = scatterwalk_sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
Herbert Xu055bcee2006-08-19 22:24:23 +100082
83 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Herbert Xufb469842006-12-10 10:45:28 +110086static 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
Herbert Xu055bcee2006-08-19 22:24:23 +100094static int final(struct hash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Herbert Xu055bcee2006-08-19 22:24:23 +100096 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100097 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
Herbert Xuee756412006-07-09 14:49:42 +100098 struct digest_alg *digest = &tfm->__crt_alg->cra_digest;
99
Atsushi Nemotoe1147d82006-04-10 08:42:35 +1000100 if (unlikely((unsigned long)out & alignmask)) {
Herbert Xuee756412006-07-09 14:49:42 +1000101 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);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +1000108 } else
Herbert Xuee756412006-07-09 14:49:42 +1000109 digest->dia_final(tfm, out);
Herbert Xu055bcee2006-08-19 22:24:23 +1000110
111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Herbert Xu055bcee2006-08-19 22:24:23 +1000114static int nosetkey(struct crypto_hash *tfm, const u8 *key, unsigned int keylen)
Herbert Xu560c06a2006-08-13 14:16:39 +1000115{
Herbert Xu055bcee2006-08-19 22:24:23 +1000116 crypto_hash_clear_flags(tfm, CRYPTO_TFM_RES_MASK);
Herbert Xu560c06a2006-08-13 14:16:39 +1000117 return -ENOSYS;
118}
119
Herbert Xu055bcee2006-08-19 22:24:23 +1000120static int setkey(struct crypto_hash *hash, const u8 *key, unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Herbert Xu055bcee2006-08-19 22:24:23 +1000122 struct crypto_tfm *tfm = crypto_hash_tfm(hash);
123
124 crypto_hash_clear_flags(hash, CRYPTO_TFM_RES_MASK);
Herbert Xu560c06a2006-08-13 14:16:39 +1000125 return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Herbert Xu055bcee2006-08-19 22:24:23 +1000128static int digest(struct hash_desc *desc,
129 struct scatterlist *sg, unsigned int nbytes, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Herbert Xufb469842006-12-10 10:45:28 +1100131 if (WARN_ON_ONCE(in_irq()))
132 return -EDEADLK;
133
Herbert Xu055bcee2006-08-19 22:24:23 +1000134 init(desc);
Herbert Xufb469842006-12-10 10:45:28 +1100135 update2(desc, sg, nbytes);
Herbert Xu055bcee2006-08-19 22:24:23 +1000136 return final(desc, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139int crypto_init_digest_ops(struct crypto_tfm *tfm)
140{
Herbert Xu055bcee2006-08-19 22:24:23 +1000141 struct hash_tfm *ops = &tfm->crt_hash;
Herbert Xu560c06a2006-08-13 14:16:39 +1000142 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000143
144 if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
145 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Herbert Xu055bcee2006-08-19 22:24:23 +1000147 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Herbert Xu84251652006-08-20 15:25:22 +1000154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157void crypto_exit_digest_ops(struct crypto_tfm *tfm)
158{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}