mic.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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>
  9. #include <crypto/hash.h>
  10. #include "orinoco.h"
  11. #include "mic.h"
  12. /********************************************************************/
  13. /* Michael MIC crypto setup */
  14. /********************************************************************/
  15. int orinoco_mic_init(struct orinoco_private *priv)
  16. {
  17. priv->tx_tfm_mic = crypto_alloc_ahash("michael_mic", 0,
  18. CRYPTO_ALG_ASYNC);
  19. if (IS_ERR(priv->tx_tfm_mic)) {
  20. printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
  21. "crypto API michael_mic\n");
  22. priv->tx_tfm_mic = NULL;
  23. return -ENOMEM;
  24. }
  25. priv->rx_tfm_mic = crypto_alloc_ahash("michael_mic", 0,
  26. CRYPTO_ALG_ASYNC);
  27. if (IS_ERR(priv->rx_tfm_mic)) {
  28. printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
  29. "crypto API michael_mic\n");
  30. priv->rx_tfm_mic = NULL;
  31. return -ENOMEM;
  32. }
  33. return 0;
  34. }
  35. void orinoco_mic_free(struct orinoco_private *priv)
  36. {
  37. if (priv->tx_tfm_mic)
  38. crypto_free_ahash(priv->tx_tfm_mic);
  39. if (priv->rx_tfm_mic)
  40. crypto_free_ahash(priv->rx_tfm_mic);
  41. }
  42. int orinoco_mic(struct crypto_ahash *tfm_michael, u8 *key,
  43. u8 *da, u8 *sa, u8 priority,
  44. u8 *data, size_t data_len, u8 *mic)
  45. {
  46. AHASH_REQUEST_ON_STACK(req, tfm_michael);
  47. struct scatterlist sg[2];
  48. u8 hdr[ETH_HLEN + 2]; /* size of header + padding */
  49. int err;
  50. if (tfm_michael == NULL) {
  51. printk(KERN_WARNING "orinoco_mic: tfm_michael == NULL\n");
  52. return -1;
  53. }
  54. /* Copy header into buffer. We need the padding on the end zeroed */
  55. memcpy(&hdr[0], da, ETH_ALEN);
  56. memcpy(&hdr[ETH_ALEN], sa, ETH_ALEN);
  57. hdr[ETH_ALEN * 2] = priority;
  58. hdr[ETH_ALEN * 2 + 1] = 0;
  59. hdr[ETH_ALEN * 2 + 2] = 0;
  60. hdr[ETH_ALEN * 2 + 3] = 0;
  61. /* Use scatter gather to MIC header and data in one go */
  62. sg_init_table(sg, 2);
  63. sg_set_buf(&sg[0], hdr, sizeof(hdr));
  64. sg_set_buf(&sg[1], data, data_len);
  65. if (crypto_ahash_setkey(tfm_michael, key, MIC_KEYLEN))
  66. return -1;
  67. ahash_request_set_tfm(req, tfm_michael);
  68. ahash_request_set_callback(req, 0, NULL, NULL);
  69. ahash_request_set_crypt(req, sg, mic, data_len + sizeof(hdr));
  70. err = crypto_ahash_digest(req);
  71. ahash_request_zero(req);
  72. return err;
  73. }