2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/crypto.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/highmem.h>
|
|
|
|
#include <asm/scatterlist.h>
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
static void init(struct crypto_tfm *tfm)
|
|
|
|
{
|
2006-05-16 12:09:29 +00:00
|
|
|
tfm->__crt_alg->cra_digest.dia_init(tfm);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void update(struct crypto_tfm *tfm,
|
|
|
|
struct scatterlist *sg, unsigned int nsg)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2006-04-09 22:42:35 +00:00
|
|
|
unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nsg; i++) {
|
|
|
|
|
|
|
|
struct page *pg = sg[i].page;
|
|
|
|
unsigned int offset = sg[i].offset;
|
|
|
|
unsigned int l = sg[i].length;
|
|
|
|
|
|
|
|
do {
|
|
|
|
unsigned int bytes_from_page = min(l, ((unsigned int)
|
|
|
|
(PAGE_SIZE)) -
|
|
|
|
offset);
|
2006-04-09 22:42:35 +00:00
|
|
|
char *src = crypto_kmap(pg, 0);
|
|
|
|
char *p = src + offset;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-04-09 22:42:35 +00:00
|
|
|
if (unlikely(offset & alignmask)) {
|
|
|
|
unsigned int bytes =
|
|
|
|
alignmask + 1 - (offset & alignmask);
|
|
|
|
bytes = min(bytes, bytes_from_page);
|
2006-05-16 12:09:29 +00:00
|
|
|
tfm->__crt_alg->cra_digest.dia_update(tfm, p,
|
|
|
|
bytes);
|
2006-04-09 22:42:35 +00:00
|
|
|
p += bytes;
|
|
|
|
bytes_from_page -= bytes;
|
|
|
|
l -= bytes;
|
|
|
|
}
|
2006-05-16 12:09:29 +00:00
|
|
|
tfm->__crt_alg->cra_digest.dia_update(tfm, p,
|
|
|
|
bytes_from_page);
|
2006-04-09 22:42:35 +00:00
|
|
|
crypto_kunmap(src, 0);
|
2006-07-30 01:53:45 +00:00
|
|
|
crypto_yield(tfm->crt_flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
offset = 0;
|
|
|
|
pg++;
|
|
|
|
l -= bytes_from_page;
|
|
|
|
} while (l > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void final(struct crypto_tfm *tfm, u8 *out)
|
|
|
|
{
|
2006-04-09 22:42:35 +00:00
|
|
|
unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
|
2006-07-09 04:49:42 +00:00
|
|
|
struct digest_alg *digest = &tfm->__crt_alg->cra_digest;
|
|
|
|
|
2006-04-09 22:42:35 +00:00
|
|
|
if (unlikely((unsigned long)out & alignmask)) {
|
2006-07-09 04:49:42 +00:00
|
|
|
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);
|
2006-04-09 22:42:35 +00:00
|
|
|
} else
|
2006-07-09 04:49:42 +00:00
|
|
|
digest->dia_final(tfm, out);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-08-13 04:16:39 +00:00
|
|
|
static int nosetkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
|
|
|
|
{
|
|
|
|
tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
|
|
|
|
{
|
2006-08-13 04:16:39 +00:00
|
|
|
tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
|
|
|
|
return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void digest(struct crypto_tfm *tfm,
|
|
|
|
struct scatterlist *sg, unsigned int nsg, u8 *out)
|
|
|
|
{
|
2006-04-09 22:42:35 +00:00
|
|
|
init(tfm);
|
|
|
|
update(tfm, sg, nsg);
|
|
|
|
final(tfm, out);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags)
|
|
|
|
{
|
|
|
|
return flags ? -EINVAL : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int crypto_init_digest_ops(struct crypto_tfm *tfm)
|
|
|
|
{
|
|
|
|
struct digest_tfm *ops = &tfm->crt_digest;
|
2006-08-13 04:16:39 +00:00
|
|
|
struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
ops->dit_init = init;
|
|
|
|
ops->dit_update = update;
|
|
|
|
ops->dit_final = final;
|
|
|
|
ops->dit_digest = digest;
|
2006-08-13 04:16:39 +00:00
|
|
|
ops->dit_setkey = dalg->dia_setkey ? setkey : nosetkey;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return crypto_alloc_hmac_block(tfm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void crypto_exit_digest_ops(struct crypto_tfm *tfm)
|
|
|
|
{
|
|
|
|
crypto_free_hmac_block(tfm);
|
|
|
|
}
|