blob: 062d0a5a2c89ac84974908ac0f38b3ec880125ab [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 */
14#include <linux/crypto.h>
15#include <linux/mm.h>
16#include <linux/errno.h>
17#include <linux/highmem.h>
18#include <asm/scatterlist.h>
19#include "internal.h"
20
21static void init(struct crypto_tfm *tfm)
22{
23 tfm->__crt_alg->cra_digest.dia_init(crypto_tfm_ctx(tfm));
24}
25
26static void update(struct crypto_tfm *tfm,
27 struct scatterlist *sg, unsigned int nsg)
28{
29 unsigned int i;
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100030 unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32 for (i = 0; i < nsg; i++) {
33
34 struct page *pg = sg[i].page;
35 unsigned int offset = sg[i].offset;
36 unsigned int l = sg[i].length;
37
38 do {
39 unsigned int bytes_from_page = min(l, ((unsigned int)
40 (PAGE_SIZE)) -
41 offset);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100042 char *src = crypto_kmap(pg, 0);
43 char *p = src + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100045 if (unlikely(offset & alignmask)) {
46 unsigned int bytes =
47 alignmask + 1 - (offset & alignmask);
48 bytes = min(bytes, bytes_from_page);
49 tfm->__crt_alg->cra_digest.dia_update
50 (crypto_tfm_ctx(tfm), p,
51 bytes);
52 p += bytes;
53 bytes_from_page -= bytes;
54 l -= bytes;
55 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 tfm->__crt_alg->cra_digest.dia_update
57 (crypto_tfm_ctx(tfm), p,
58 bytes_from_page);
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100059 crypto_kunmap(src, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 crypto_yield(tfm);
61 offset = 0;
62 pg++;
63 l -= bytes_from_page;
64 } while (l > 0);
65 }
66}
67
68static void final(struct crypto_tfm *tfm, u8 *out)
69{
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100070 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
71 if (unlikely((unsigned long)out & alignmask)) {
72 unsigned int size = crypto_tfm_alg_digestsize(tfm);
73 u8 buffer[size + alignmask];
74 u8 *dst = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
75 tfm->__crt_alg->cra_digest.dia_final(crypto_tfm_ctx(tfm), dst);
76 memcpy(out, dst, size);
77 } else
78 tfm->__crt_alg->cra_digest.dia_final(crypto_tfm_ctx(tfm), out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
82{
83 u32 flags;
84 if (tfm->__crt_alg->cra_digest.dia_setkey == NULL)
85 return -ENOSYS;
86 return tfm->__crt_alg->cra_digest.dia_setkey(crypto_tfm_ctx(tfm),
87 key, keylen, &flags);
88}
89
90static void digest(struct crypto_tfm *tfm,
91 struct scatterlist *sg, unsigned int nsg, u8 *out)
92{
Atsushi Nemotoe1147d82006-04-10 08:42:35 +100093 init(tfm);
94 update(tfm, sg, nsg);
95 final(tfm, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags)
99{
100 return flags ? -EINVAL : 0;
101}
102
103int crypto_init_digest_ops(struct crypto_tfm *tfm)
104{
105 struct digest_tfm *ops = &tfm->crt_digest;
106
107 ops->dit_init = init;
108 ops->dit_update = update;
109 ops->dit_final = final;
110 ops->dit_digest = digest;
111 ops->dit_setkey = setkey;
112
113 return crypto_alloc_hmac_block(tfm);
114}
115
116void crypto_exit_digest_ops(struct crypto_tfm *tfm)
117{
118 crypto_free_hmac_block(tfm);
119}