blob: 8f4593268ce0bf5c5ef2e9e47d8ab72789adb842 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mm.h>
16#include <linux/errno.h>
17#include <linux/highmem.h>
Herbert Xu055bcee2006-08-19 22:24:23 +100018#include <linux/module.h>
19#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Herbert Xu055bcee2006-08-19 22:24:23 +100021#include "internal.h"
22#include "scatterwalk.h"
23
Herbert Xu055bcee2006-08-19 22:24:23 +100024static int init(struct hash_desc *desc)
25{
26 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
27
Herbert Xu6c2bb982006-05-16 22:09:29 +100028 tfm->__crt_alg->cra_digest.dia_init(tfm);
Herbert Xu055bcee2006-08-19 22:24:23 +100029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030}
31
Herbert Xu055bcee2006-08-19 22:24:23 +100032static int update(struct hash_desc *desc,
33 struct scatterlist *sg, unsigned int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Herbert Xu055bcee2006-08-19 22:24:23 +100035 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100036 unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Herbert Xu055bcee2006-08-19 22:24:23 +100038 if (!nbytes)
39 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Herbert Xu055bcee2006-08-19 22:24:23 +100041 for (;;) {
42 struct page *pg = sg->page;
43 unsigned int offset = sg->offset;
44 unsigned int l = sg->length;
45
46 if (unlikely(l > nbytes))
47 l = nbytes;
48 nbytes -= l;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 do {
51 unsigned int bytes_from_page = min(l, ((unsigned int)
52 (PAGE_SIZE)) -
53 offset);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100054 char *src = crypto_kmap(pg, 0);
55 char *p = src + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100057 if (unlikely(offset & alignmask)) {
58 unsigned int bytes =
59 alignmask + 1 - (offset & alignmask);
60 bytes = min(bytes, bytes_from_page);
Herbert Xu6c2bb982006-05-16 22:09:29 +100061 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
62 bytes);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100063 p += bytes;
64 bytes_from_page -= bytes;
65 l -= bytes;
66 }
Herbert Xu6c2bb982006-05-16 22:09:29 +100067 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
68 bytes_from_page);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100069 crypto_kunmap(src, 0);
Herbert Xu055bcee2006-08-19 22:24:23 +100070 crypto_yield(desc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 offset = 0;
72 pg++;
73 l -= bytes_from_page;
74 } while (l > 0);
Herbert Xu055bcee2006-08-19 22:24:23 +100075
76 if (!nbytes)
77 break;
78 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
Herbert Xu055bcee2006-08-19 22:24:23 +100080
81 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Herbert Xu055bcee2006-08-19 22:24:23 +100084static int final(struct hash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Herbert Xu055bcee2006-08-19 22:24:23 +100086 struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100087 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
Herbert Xuee756412006-07-09 14:49:42 +100088 struct digest_alg *digest = &tfm->__crt_alg->cra_digest;
89
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100090 if (unlikely((unsigned long)out & alignmask)) {
Herbert Xuee756412006-07-09 14:49:42 +100091 unsigned long align = alignmask + 1;
92 unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
93 u8 *dst = (u8 *)ALIGN(addr, align) +
94 ALIGN(tfm->__crt_alg->cra_ctxsize, align);
95
96 digest->dia_final(tfm, dst);
97 memcpy(out, dst, digest->dia_digestsize);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100098 } else
Herbert Xuee756412006-07-09 14:49:42 +100099 digest->dia_final(tfm, out);
Herbert Xu055bcee2006-08-19 22:24:23 +1000100
101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Herbert Xu055bcee2006-08-19 22:24:23 +1000104static int nosetkey(struct crypto_hash *tfm, const u8 *key, unsigned int keylen)
Herbert Xu560c06a2006-08-13 14:16:39 +1000105{
Herbert Xu055bcee2006-08-19 22:24:23 +1000106 crypto_hash_clear_flags(tfm, CRYPTO_TFM_RES_MASK);
Herbert Xu560c06a2006-08-13 14:16:39 +1000107 return -ENOSYS;
108}
109
Herbert Xu055bcee2006-08-19 22:24:23 +1000110static int setkey(struct crypto_hash *hash, const u8 *key, unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Herbert Xu055bcee2006-08-19 22:24:23 +1000112 struct crypto_tfm *tfm = crypto_hash_tfm(hash);
113
114 crypto_hash_clear_flags(hash, CRYPTO_TFM_RES_MASK);
Herbert Xu560c06a2006-08-13 14:16:39 +1000115 return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Herbert Xu055bcee2006-08-19 22:24:23 +1000118static int digest(struct hash_desc *desc,
119 struct scatterlist *sg, unsigned int nbytes, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Herbert Xu055bcee2006-08-19 22:24:23 +1000121 init(desc);
122 update(desc, sg, nbytes);
123 return final(desc, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags)
127{
128 return flags ? -EINVAL : 0;
129}
130
131int crypto_init_digest_ops(struct crypto_tfm *tfm)
132{
Herbert Xu055bcee2006-08-19 22:24:23 +1000133 struct hash_tfm *ops = &tfm->crt_hash;
Herbert Xu560c06a2006-08-13 14:16:39 +1000134 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000135
136 if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
137 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Herbert Xu055bcee2006-08-19 22:24:23 +1000139 ops->init = init;
140 ops->update = update;
141 ops->final = final;
142 ops->digest = digest;
143 ops->setkey = dalg->dia_setkey ? setkey : nosetkey;
144 ops->digestsize = dalg->dia_digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Herbert Xu84251652006-08-20 15:25:22 +1000146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149void crypto_exit_digest_ops(struct crypto_tfm *tfm)
150{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}