digest.c 3.7 KB
Newer Older
L
Linus Torvalds 已提交
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.
 *
 */
14

L
Linus Torvalds 已提交
15 16
#include <linux/mm.h>
#include <linux/errno.h>
17
#include <linux/hardirq.h>
L
Linus Torvalds 已提交
18
#include <linux/highmem.h>
19
#include <linux/kernel.h>
20 21 22
#include <linux/module.h>
#include <linux/scatterlist.h>

L
Linus Torvalds 已提交
23
#include "internal.h"
24
#include "scatterwalk.h"
L
Linus Torvalds 已提交
25

26 27 28 29 30 31 32 33
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;
}

34 35
static int update2(struct hash_desc *desc,
		   struct scatterlist *sg, unsigned int nbytes)
36 37
{
	struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
38
	unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
L
Linus Torvalds 已提交
39

40 41 42 43 44 45 46
	if (!nbytes)
		return 0;

	for (;;) {
		struct page *pg = sg->page;
		unsigned int offset = sg->offset;
		unsigned int l = sg->length;
L
Linus Torvalds 已提交
47

48 49 50
		if (unlikely(l > nbytes))
			l = nbytes;
		nbytes -= l;
L
Linus Torvalds 已提交
51 52 53 54 55

		do {
			unsigned int bytes_from_page = min(l, ((unsigned int)
							   (PAGE_SIZE)) - 
							   offset);
56 57
			char *src = crypto_kmap(pg, 0);
			char *p = src + offset;
L
Linus Torvalds 已提交
58

59 60 61 62
			if (unlikely(offset & alignmask)) {
				unsigned int bytes =
					alignmask + 1 - (offset & alignmask);
				bytes = min(bytes, bytes_from_page);
63 64
				tfm->__crt_alg->cra_digest.dia_update(tfm, p,
								      bytes);
65 66 67 68
				p += bytes;
				bytes_from_page -= bytes;
				l -= bytes;
			}
69 70
			tfm->__crt_alg->cra_digest.dia_update(tfm, p,
							      bytes_from_page);
71
			crypto_kunmap(src, 0);
72
			crypto_yield(desc->flags);
L
Linus Torvalds 已提交
73 74 75 76
			offset = 0;
			pg++;
			l -= bytes_from_page;
		} while (l > 0);
77 78 79 80

		if (!nbytes)
			break;
		sg = sg_next(sg);
L
Linus Torvalds 已提交
81
	}
82 83

	return 0;
L
Linus Torvalds 已提交
84 85
}

86 87 88 89 90 91 92 93
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);
}

94
static int final(struct hash_desc *desc, u8 *out)
L
Linus Torvalds 已提交
95
{
96
	struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm);
97
	unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
98 99
	struct digest_alg *digest = &tfm->__crt_alg->cra_digest;

100
	if (unlikely((unsigned long)out & alignmask)) {
101 102 103 104 105 106 107
		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);
108
	} else
109
		digest->dia_final(tfm, out);
110 111

	return 0;
L
Linus Torvalds 已提交
112 113
}

114
static int nosetkey(struct crypto_hash *tfm, const u8 *key, unsigned int keylen)
115
{
116
	crypto_hash_clear_flags(tfm, CRYPTO_TFM_RES_MASK);
117 118 119
	return -ENOSYS;
}

120
static int setkey(struct crypto_hash *hash, const u8 *key, unsigned int keylen)
L
Linus Torvalds 已提交
121
{
122 123 124
	struct crypto_tfm *tfm = crypto_hash_tfm(hash);

	crypto_hash_clear_flags(hash, CRYPTO_TFM_RES_MASK);
125
	return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen);
L
Linus Torvalds 已提交
126 127
}

128 129
static int digest(struct hash_desc *desc,
		  struct scatterlist *sg, unsigned int nbytes, u8 *out)
L
Linus Torvalds 已提交
130
{
131 132 133
	if (WARN_ON_ONCE(in_irq()))
		return -EDEADLK;

134
	init(desc);
135
	update2(desc, sg, nbytes);
136
	return final(desc, out);
L
Linus Torvalds 已提交
137 138 139 140
}

int crypto_init_digest_ops(struct crypto_tfm *tfm)
{
141
	struct hash_tfm *ops = &tfm->crt_hash;
142
	struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
143 144 145

	if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
		return -EINVAL;
L
Linus Torvalds 已提交
146
	
147 148 149 150 151 152
	ops->init	= init;
	ops->update	= update;
	ops->final	= final;
	ops->digest	= digest;
	ops->setkey	= dalg->dia_setkey ? setkey : nosetkey;
	ops->digestsize	= dalg->dia_digestsize;
L
Linus Torvalds 已提交
153
	
154
	return 0;
L
Linus Torvalds 已提交
155 156 157 158 159
}

void crypto_exit_digest_ops(struct crypto_tfm *tfm)
{
}