David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 1 | /* Orinoco MIC helpers |
| 2 | * |
| 3 | * See copyright notice in main.c |
| 4 | */ |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/string.h> |
| 7 | #include <linux/if_ether.h> |
| 8 | #include <linux/scatterlist.h> |
Herbert Xu | a60b7fa | 2016-01-24 21:16:47 +0800 | [diff] [blame] | 9 | #include <crypto/hash.h> |
David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 10 | |
| 11 | #include "orinoco.h" |
| 12 | #include "mic.h" |
| 13 | |
| 14 | /********************************************************************/ |
| 15 | /* Michael MIC crypto setup */ |
| 16 | /********************************************************************/ |
| 17 | int orinoco_mic_init(struct orinoco_private *priv) |
| 18 | { |
Herbert Xu | a60b7fa | 2016-01-24 21:16:47 +0800 | [diff] [blame] | 19 | priv->tx_tfm_mic = crypto_alloc_ahash("michael_mic", 0, |
| 20 | CRYPTO_ALG_ASYNC); |
David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 21 | if (IS_ERR(priv->tx_tfm_mic)) { |
| 22 | printk(KERN_DEBUG "orinoco_mic_init: could not allocate " |
| 23 | "crypto API michael_mic\n"); |
| 24 | priv->tx_tfm_mic = NULL; |
| 25 | return -ENOMEM; |
| 26 | } |
| 27 | |
Herbert Xu | a60b7fa | 2016-01-24 21:16:47 +0800 | [diff] [blame] | 28 | priv->rx_tfm_mic = crypto_alloc_ahash("michael_mic", 0, |
| 29 | CRYPTO_ALG_ASYNC); |
David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 30 | if (IS_ERR(priv->rx_tfm_mic)) { |
| 31 | printk(KERN_DEBUG "orinoco_mic_init: could not allocate " |
| 32 | "crypto API michael_mic\n"); |
| 33 | priv->rx_tfm_mic = NULL; |
| 34 | return -ENOMEM; |
| 35 | } |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | void orinoco_mic_free(struct orinoco_private *priv) |
| 41 | { |
| 42 | if (priv->tx_tfm_mic) |
Herbert Xu | a60b7fa | 2016-01-24 21:16:47 +0800 | [diff] [blame] | 43 | crypto_free_ahash(priv->tx_tfm_mic); |
David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 44 | if (priv->rx_tfm_mic) |
Herbert Xu | a60b7fa | 2016-01-24 21:16:47 +0800 | [diff] [blame] | 45 | crypto_free_ahash(priv->rx_tfm_mic); |
David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Herbert Xu | a60b7fa | 2016-01-24 21:16:47 +0800 | [diff] [blame] | 48 | int orinoco_mic(struct crypto_ahash *tfm_michael, u8 *key, |
David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 49 | u8 *da, u8 *sa, u8 priority, |
| 50 | u8 *data, size_t data_len, u8 *mic) |
| 51 | { |
Herbert Xu | a60b7fa | 2016-01-24 21:16:47 +0800 | [diff] [blame] | 52 | AHASH_REQUEST_ON_STACK(req, tfm_michael); |
David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 53 | struct scatterlist sg[2]; |
| 54 | u8 hdr[ETH_HLEN + 2]; /* size of header + padding */ |
Herbert Xu | a60b7fa | 2016-01-24 21:16:47 +0800 | [diff] [blame] | 55 | int err; |
David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 56 | |
| 57 | if (tfm_michael == NULL) { |
| 58 | printk(KERN_WARNING "orinoco_mic: tfm_michael == NULL\n"); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | /* Copy header into buffer. We need the padding on the end zeroed */ |
| 63 | memcpy(&hdr[0], da, ETH_ALEN); |
| 64 | memcpy(&hdr[ETH_ALEN], sa, ETH_ALEN); |
Pavel Roskin | 933d594 | 2011-07-13 11:19:57 -0400 | [diff] [blame] | 65 | hdr[ETH_ALEN * 2] = priority; |
| 66 | hdr[ETH_ALEN * 2 + 1] = 0; |
| 67 | hdr[ETH_ALEN * 2 + 2] = 0; |
| 68 | hdr[ETH_ALEN * 2 + 3] = 0; |
David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 69 | |
| 70 | /* Use scatter gather to MIC header and data in one go */ |
| 71 | sg_init_table(sg, 2); |
| 72 | sg_set_buf(&sg[0], hdr, sizeof(hdr)); |
| 73 | sg_set_buf(&sg[1], data, data_len); |
| 74 | |
Herbert Xu | a60b7fa | 2016-01-24 21:16:47 +0800 | [diff] [blame] | 75 | if (crypto_ahash_setkey(tfm_michael, key, MIC_KEYLEN)) |
David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 76 | return -1; |
| 77 | |
Herbert Xu | a60b7fa | 2016-01-24 21:16:47 +0800 | [diff] [blame] | 78 | ahash_request_set_tfm(req, tfm_michael); |
| 79 | ahash_request_set_callback(req, 0, NULL, NULL); |
| 80 | ahash_request_set_crypt(req, sg, mic, data_len + sizeof(hdr)); |
| 81 | err = crypto_ahash_digest(req); |
| 82 | ahash_request_zero(req); |
| 83 | return err; |
David Kilroy | 4adb474 | 2009-02-04 23:05:51 +0000 | [diff] [blame] | 84 | } |