mic.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_shash("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_shash("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_shash(priv->tx_tfm_mic);
  39. if (priv->rx_tfm_mic)
  40. crypto_free_shash(priv->rx_tfm_mic);
  41. }
  42. int orinoco_mic(struct crypto_shash *tfm_michael, u8 *key,
  43. u8 *da, u8 *sa, u8 priority,
  44. u8 *data, size_t data_len, u8 *mic)
  45. {
  46. SHASH_DESC_ON_STACK(desc, tfm_michael);
  47. u8 hdr[ETH_HLEN + 2]; /* size of header + padding */
  48. int err;
  49. if (tfm_michael == NULL) {
  50. printk(KERN_WARNING "orinoco_mic: tfm_michael == NULL\n");
  51. return -1;
  52. }
  53. /* Copy header into buffer. We need the padding on the end zeroed */
  54. memcpy(&hdr[0], da, ETH_ALEN);
  55. memcpy(&hdr[ETH_ALEN], sa, ETH_ALEN);
  56. hdr[ETH_ALEN * 2] = priority;
  57. hdr[ETH_ALEN * 2 + 1] = 0;
  58. hdr[ETH_ALEN * 2 + 2] = 0;
  59. hdr[ETH_ALEN * 2 + 3] = 0;
  60. desc->tfm = tfm_michael;
  61. desc->flags = 0;
  62. err = crypto_shash_setkey(tfm_michael, key, MIC_KEYLEN);
  63. if (err)
  64. return err;
  65. err = crypto_shash_init(desc);
  66. if (err)
  67. return err;
  68. err = crypto_shash_update(desc, hdr, sizeof(hdr));
  69. if (err)
  70. return err;
  71. err = crypto_shash_update(desc, data, data_len);
  72. if (err)
  73. return err;
  74. err = crypto_shash_final(desc, mic);
  75. shash_desc_zero(desc);
  76. return err;
  77. }