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 | */ |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 14 | |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 15 | #include <crypto/scatterwalk.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/mm.h> |
| 17 | #include <linux/errno.h> |
Herbert Xu | fb46984 | 2006-12-10 10:45:28 +1100 | [diff] [blame] | 18 | #include <linux/hardirq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/highmem.h> |
Herbert Xu | fb46984 | 2006-12-10 10:45:28 +1100 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 24 | static int init(struct hash_desc *desc) |
| 25 | { |
| 26 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); |
| 27 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 28 | tfm->__crt_alg->cra_digest.dia_init(tfm); |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 29 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Herbert Xu | fb46984 | 2006-12-10 10:45:28 +1100 | [diff] [blame] | 32 | static int update2(struct hash_desc *desc, |
| 33 | struct scatterlist *sg, unsigned int nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 35 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 36 | unsigned int alignmask = crypto_tfm_alg_alignmask(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 38 | if (!nbytes) |
| 39 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 41 | for (;;) { |
Jens Axboe | 78c2f0b | 2007-10-22 19:40:16 +0200 | [diff] [blame] | 42 | struct page *pg = sg_page(sg); |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 43 | unsigned int offset = sg->offset; |
| 44 | unsigned int l = sg->length; |
| 45 | |
| 46 | if (unlikely(l > nbytes)) |
| 47 | l = nbytes; |
| 48 | nbytes -= l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | do { |
| 51 | unsigned int bytes_from_page = min(l, ((unsigned int) |
| 52 | (PAGE_SIZE)) - |
| 53 | offset); |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 54 | char *src = crypto_kmap(pg, 0); |
| 55 | char *p = src + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 57 | if (unlikely(offset & alignmask)) { |
| 58 | unsigned int bytes = |
| 59 | alignmask + 1 - (offset & alignmask); |
| 60 | bytes = min(bytes, bytes_from_page); |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 61 | tfm->__crt_alg->cra_digest.dia_update(tfm, p, |
| 62 | bytes); |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 63 | p += bytes; |
| 64 | bytes_from_page -= bytes; |
| 65 | l -= bytes; |
| 66 | } |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 67 | tfm->__crt_alg->cra_digest.dia_update(tfm, p, |
| 68 | bytes_from_page); |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 69 | crypto_kunmap(src, 0); |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 70 | crypto_yield(desc->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | offset = 0; |
| 72 | pg++; |
| 73 | l -= bytes_from_page; |
| 74 | } while (l > 0); |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 75 | |
| 76 | if (!nbytes) |
| 77 | break; |
Herbert Xu | b2ab4a5 | 2007-12-05 20:59:25 +1100 | [diff] [blame] | 78 | sg = scatterwalk_sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 80 | |
| 81 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Herbert Xu | fb46984 | 2006-12-10 10:45:28 +1100 | [diff] [blame] | 84 | static int update(struct hash_desc *desc, |
| 85 | struct scatterlist *sg, unsigned int nbytes) |
| 86 | { |
| 87 | if (WARN_ON_ONCE(in_irq())) |
| 88 | return -EDEADLK; |
| 89 | return update2(desc, sg, nbytes); |
| 90 | } |
| 91 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 92 | static int final(struct hash_desc *desc, u8 *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 94 | struct crypto_tfm *tfm = crypto_hash_tfm(desc->tfm); |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 95 | unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
Herbert Xu | ee75641 | 2006-07-09 14:49:42 +1000 | [diff] [blame] | 96 | struct digest_alg *digest = &tfm->__crt_alg->cra_digest; |
| 97 | |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 98 | if (unlikely((unsigned long)out & alignmask)) { |
Herbert Xu | ee75641 | 2006-07-09 14:49:42 +1000 | [diff] [blame] | 99 | unsigned long align = alignmask + 1; |
| 100 | unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm); |
| 101 | u8 *dst = (u8 *)ALIGN(addr, align) + |
| 102 | ALIGN(tfm->__crt_alg->cra_ctxsize, align); |
| 103 | |
| 104 | digest->dia_final(tfm, dst); |
| 105 | memcpy(out, dst, digest->dia_digestsize); |
Atsushi Nemoto | e1147d8 | 2006-04-10 08:42:35 +1000 | [diff] [blame] | 106 | } else |
Herbert Xu | ee75641 | 2006-07-09 14:49:42 +1000 | [diff] [blame] | 107 | digest->dia_final(tfm, out); |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 108 | |
| 109 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 112 | static int nosetkey(struct crypto_hash *tfm, const u8 *key, unsigned int keylen) |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 113 | { |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 114 | crypto_hash_clear_flags(tfm, CRYPTO_TFM_RES_MASK); |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 115 | return -ENOSYS; |
| 116 | } |
| 117 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 118 | static int setkey(struct crypto_hash *hash, const u8 *key, unsigned int keylen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 120 | struct crypto_tfm *tfm = crypto_hash_tfm(hash); |
| 121 | |
| 122 | crypto_hash_clear_flags(hash, CRYPTO_TFM_RES_MASK); |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 123 | return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 126 | static int digest(struct hash_desc *desc, |
| 127 | struct scatterlist *sg, unsigned int nbytes, u8 *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Herbert Xu | fb46984 | 2006-12-10 10:45:28 +1100 | [diff] [blame] | 129 | if (WARN_ON_ONCE(in_irq())) |
| 130 | return -EDEADLK; |
| 131 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 132 | init(desc); |
Herbert Xu | fb46984 | 2006-12-10 10:45:28 +1100 | [diff] [blame] | 133 | update2(desc, sg, nbytes); |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 134 | return final(desc, out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | int crypto_init_digest_ops(struct crypto_tfm *tfm) |
| 138 | { |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 139 | struct hash_tfm *ops = &tfm->crt_hash; |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 140 | struct digest_alg *dalg = &tfm->__crt_alg->cra_digest; |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 141 | |
| 142 | if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm)) |
| 143 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 145 | ops->init = init; |
| 146 | ops->update = update; |
| 147 | ops->final = final; |
| 148 | ops->digest = digest; |
| 149 | ops->setkey = dalg->dia_setkey ? setkey : nosetkey; |
| 150 | ops->digestsize = dalg->dia_digestsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Herbert Xu | 8425165 | 2006-08-20 15:25:22 +1000 | [diff] [blame] | 152 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void crypto_exit_digest_ops(struct crypto_tfm *tfm) |
| 156 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |