6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
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)
14 #include <linux/crypto.h>
16 #include <linux/errno.h>
17 #include <linux/highmem.h>
18 #include <asm/scatterlist.h>
21 static void init(struct crypto_tfm *tfm)
23 tfm->__crt_alg->cra_digest.dia_init(tfm);
26 static void update(struct crypto_tfm *tfm,
27 struct scatterlist *sg, unsigned int nsg)
30 unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
32 for (i = 0; i < nsg; i++) {
34 struct page *pg = sg[i].page;
35 unsigned int offset = sg[i].offset;
36 unsigned int l = sg[i].length;
39 unsigned int bytes_from_page = min(l, ((unsigned int)
42 char *src = crypto_kmap(pg, 0);
43 char *p = src + offset;
45 if (unlikely(offset & alignmask)) {
47 alignmask + 1 - (offset & alignmask);
48 bytes = min(bytes, bytes_from_page);
49 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
52 bytes_from_page -= bytes;
55 tfm->__crt_alg->cra_digest.dia_update(tfm, p,
57 crypto_kunmap(src, 0);
58 crypto_yield(tfm->crt_flags);
66 static void final(struct crypto_tfm *tfm, u8 *out)
68 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
69 struct digest_alg *digest = &tfm->__crt_alg->cra_digest;
71 if (unlikely((unsigned long)out & alignmask)) {
72 unsigned long align = alignmask + 1;
73 unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
74 u8 *dst = (u8 *)ALIGN(addr, align) +
75 ALIGN(tfm->__crt_alg->cra_ctxsize, align);
77 digest->dia_final(tfm, dst);
78 memcpy(out, dst, digest->dia_digestsize);
80 digest->dia_final(tfm, out);
83 static int nosetkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
85 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
89 static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
91 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
92 return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen);
95 static void digest(struct crypto_tfm *tfm,
96 struct scatterlist *sg, unsigned int nsg, u8 *out)
103 int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags)
105 return flags ? -EINVAL : 0;
108 int crypto_init_digest_ops(struct crypto_tfm *tfm)
110 struct digest_tfm *ops = &tfm->crt_digest;
111 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
113 ops->dit_init = init;
114 ops->dit_update = update;
115 ops->dit_final = final;
116 ops->dit_digest = digest;
117 ops->dit_setkey = dalg->dia_setkey ? setkey : nosetkey;
119 return crypto_alloc_hmac_block(tfm);
122 void crypto_exit_digest_ops(struct crypto_tfm *tfm)
124 crypto_free_hmac_block(tfm);