Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * HMAC: Keyed-Hashing for Message Authentication (RFC2104). |
| 5 | * |
| 6 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
| 7 | * |
| 8 | * The HMAC implementation is derived from USAGI. |
| 9 | * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the Free |
| 13 | * Software Foundation; either version 2 of the License, or (at your option) |
| 14 | * any later version. |
| 15 | * |
| 16 | */ |
| 17 | #include <linux/crypto.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/highmem.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <asm/scatterlist.h> |
| 22 | #include "internal.h" |
| 23 | |
| 24 | static void hash_key(struct crypto_tfm *tfm, u8 *key, unsigned int keylen) |
| 25 | { |
| 26 | struct scatterlist tmp; |
| 27 | |
| 28 | tmp.page = virt_to_page(key); |
| 29 | tmp.offset = offset_in_page(key); |
| 30 | tmp.length = keylen; |
| 31 | crypto_digest_digest(tfm, &tmp, 1, key); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | int crypto_alloc_hmac_block(struct crypto_tfm *tfm) |
| 36 | { |
| 37 | int ret = 0; |
| 38 | |
| 39 | BUG_ON(!crypto_tfm_alg_blocksize(tfm)); |
| 40 | |
| 41 | tfm->crt_digest.dit_hmac_block = kmalloc(crypto_tfm_alg_blocksize(tfm), |
| 42 | GFP_KERNEL); |
| 43 | if (tfm->crt_digest.dit_hmac_block == NULL) |
| 44 | ret = -ENOMEM; |
| 45 | |
| 46 | return ret; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | void crypto_free_hmac_block(struct crypto_tfm *tfm) |
| 51 | { |
Jesper Juhl | 8279dd7 | 2005-07-06 13:51:00 -0700 | [diff] [blame] | 52 | kfree(tfm->crt_digest.dit_hmac_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen) |
| 56 | { |
| 57 | unsigned int i; |
| 58 | struct scatterlist tmp; |
| 59 | char *ipad = tfm->crt_digest.dit_hmac_block; |
| 60 | |
| 61 | if (*keylen > crypto_tfm_alg_blocksize(tfm)) { |
| 62 | hash_key(tfm, key, *keylen); |
| 63 | *keylen = crypto_tfm_alg_digestsize(tfm); |
| 64 | } |
| 65 | |
| 66 | memset(ipad, 0, crypto_tfm_alg_blocksize(tfm)); |
| 67 | memcpy(ipad, key, *keylen); |
| 68 | |
| 69 | for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++) |
| 70 | ipad[i] ^= 0x36; |
| 71 | |
| 72 | tmp.page = virt_to_page(ipad); |
| 73 | tmp.offset = offset_in_page(ipad); |
| 74 | tmp.length = crypto_tfm_alg_blocksize(tfm); |
| 75 | |
| 76 | crypto_digest_init(tfm); |
| 77 | crypto_digest_update(tfm, &tmp, 1); |
| 78 | } |
| 79 | |
| 80 | void crypto_hmac_update(struct crypto_tfm *tfm, |
| 81 | struct scatterlist *sg, unsigned int nsg) |
| 82 | { |
| 83 | crypto_digest_update(tfm, sg, nsg); |
| 84 | } |
| 85 | |
| 86 | void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key, |
| 87 | unsigned int *keylen, u8 *out) |
| 88 | { |
| 89 | unsigned int i; |
| 90 | struct scatterlist tmp; |
| 91 | char *opad = tfm->crt_digest.dit_hmac_block; |
| 92 | |
| 93 | if (*keylen > crypto_tfm_alg_blocksize(tfm)) { |
| 94 | hash_key(tfm, key, *keylen); |
| 95 | *keylen = crypto_tfm_alg_digestsize(tfm); |
| 96 | } |
| 97 | |
| 98 | crypto_digest_final(tfm, out); |
| 99 | |
| 100 | memset(opad, 0, crypto_tfm_alg_blocksize(tfm)); |
| 101 | memcpy(opad, key, *keylen); |
| 102 | |
| 103 | for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++) |
| 104 | opad[i] ^= 0x5c; |
| 105 | |
| 106 | tmp.page = virt_to_page(opad); |
| 107 | tmp.offset = offset_in_page(opad); |
| 108 | tmp.length = crypto_tfm_alg_blocksize(tfm); |
| 109 | |
| 110 | crypto_digest_init(tfm); |
| 111 | crypto_digest_update(tfm, &tmp, 1); |
| 112 | |
| 113 | tmp.page = virt_to_page(out); |
| 114 | tmp.offset = offset_in_page(out); |
| 115 | tmp.length = crypto_tfm_alg_digestsize(tfm); |
| 116 | |
| 117 | crypto_digest_update(tfm, &tmp, 1); |
| 118 | crypto_digest_final(tfm, out); |
| 119 | } |
| 120 | |
| 121 | void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen, |
| 122 | struct scatterlist *sg, unsigned int nsg, u8 *out) |
| 123 | { |
| 124 | crypto_hmac_init(tfm, key, keylen); |
| 125 | crypto_hmac_update(tfm, sg, nsg); |
| 126 | crypto_hmac_final(tfm, key, keylen, out); |
| 127 | } |
| 128 | |
| 129 | EXPORT_SYMBOL_GPL(crypto_hmac_init); |
| 130 | EXPORT_SYMBOL_GPL(crypto_hmac_update); |
| 131 | EXPORT_SYMBOL_GPL(crypto_hmac_final); |
| 132 | EXPORT_SYMBOL_GPL(crypto_hmac); |
| 133 | |