Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | static void init(struct crypto_tfm *tfm) |
| 22 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 23 | tfm->__crt_alg->cra_digest.dia_init(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static void update(struct crypto_tfm *tfm, |
| 27 | struct scatterlist *sg, unsigned int nsg) |
| 28 | { |
| 29 | unsigned int i; |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 30 | unsigned int alignmask = crypto_tfm_alg_alignmask(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 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 Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 42 | char *src = crypto_kmap(pg, 0); |
| 43 | char *p = src + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 45 | if (unlikely(offset & alignmask)) { |
| 46 | unsigned int bytes = |
| 47 | alignmask + 1 - (offset & alignmask); |
| 48 | bytes = min(bytes, bytes_from_page); |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 49 | tfm->__crt_alg->cra_digest.dia_update(tfm, p, |
| 50 | bytes); |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 51 | p += bytes; |
| 52 | bytes_from_page -= bytes; |
| 53 | l -= bytes; |
| 54 | } |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 55 | tfm->__crt_alg->cra_digest.dia_update(tfm, p, |
| 56 | bytes_from_page); |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 57 | crypto_kunmap(src, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | crypto_yield(tfm); |
| 59 | offset = 0; |
| 60 | pg++; |
| 61 | l -= bytes_from_page; |
| 62 | } while (l > 0); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void final(struct crypto_tfm *tfm, u8 *out) |
| 67 | { |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 68 | unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
Herbert Xu | ee75641 | 2006-07-09 14:49:42 +1000 | [diff] [blame^] | 69 | struct digest_alg *digest = &tfm->__crt_alg->cra_digest; |
| 70 | |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 71 | if (unlikely((unsigned long)out & alignmask)) { |
Herbert Xu | ee75641 | 2006-07-09 14:49:42 +1000 | [diff] [blame^] | 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); |
| 76 | |
| 77 | digest->dia_final(tfm, dst); |
| 78 | memcpy(out, dst, digest->dia_digestsize); |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 79 | } else |
Herbert Xu | ee75641 | 2006-07-09 14:49:42 +1000 | [diff] [blame^] | 80 | digest->dia_final(tfm, out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 83 | static int nosetkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) |
| 84 | { |
| 85 | tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; |
| 86 | return -ENOSYS; |
| 87 | } |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) |
| 90 | { |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 91 | tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; |
| 92 | return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void digest(struct crypto_tfm *tfm, |
| 96 | struct scatterlist *sg, unsigned int nsg, u8 *out) |
| 97 | { |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 98 | init(tfm); |
| 99 | update(tfm, sg, nsg); |
| 100 | final(tfm, out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags) |
| 104 | { |
| 105 | return flags ? -EINVAL : 0; |
| 106 | } |
| 107 | |
| 108 | int crypto_init_digest_ops(struct crypto_tfm *tfm) |
| 109 | { |
| 110 | struct digest_tfm *ops = &tfm->crt_digest; |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 111 | struct digest_alg *dalg = &tfm->__crt_alg->cra_digest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | ops->dit_init = init; |
| 114 | ops->dit_update = update; |
| 115 | ops->dit_final = final; |
| 116 | ops->dit_digest = digest; |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 117 | ops->dit_setkey = dalg->dia_setkey ? setkey : nosetkey; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | return crypto_alloc_hmac_block(tfm); |
| 120 | } |
| 121 | |
| 122 | void crypto_exit_digest_ops(struct crypto_tfm *tfm) |
| 123 | { |
| 124 | crypto_free_hmac_block(tfm); |
| 125 | } |