mic.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <linux/crypto.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_hash("michael_mic", 0, 0);
  18. if (IS_ERR(priv->tx_tfm_mic)) {
  19. printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
  20. "crypto API michael_mic\n");
  21. priv->tx_tfm_mic = NULL;
  22. return -ENOMEM;
  23. }
  24. priv->rx_tfm_mic = crypto_alloc_hash("michael_mic", 0, 0);
  25. if (IS_ERR(priv->rx_tfm_mic)) {
  26. printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
  27. "crypto API michael_mic\n");
  28. priv->rx_tfm_mic = NULL;
  29. return -ENOMEM;
  30. }
  31. return 0;
  32. }
  33. void orinoco_mic_free(struct orinoco_private *priv)
  34. {
  35. if (priv->tx_tfm_mic)
  36. crypto_free_hash(priv->tx_tfm_mic);
  37. if (priv->rx_tfm_mic)
  38. crypto_free_hash(priv->rx_tfm_mic);
  39. }
  40. int orinoco_mic(struct crypto_hash *tfm_michael, u8 *key,
  41. u8 *da, u8 *sa, u8 priority,
  42. u8 *data, size_t data_len, u8 *mic)
  43. {
  44. struct hash_desc desc;
  45. struct scatterlist sg[2];
  46. u8 hdr[ETH_HLEN + 2]; /* size of header + padding */
  47. if (tfm_michael == NULL) {
  48. printk(KERN_WARNING "orinoco_mic: tfm_michael == NULL\n");
  49. return -1;
  50. }
  51. /* Copy header into buffer. We need the padding on the end zeroed */
  52. memcpy(&hdr[0], da, ETH_ALEN);
  53. memcpy(&hdr[ETH_ALEN], sa, ETH_ALEN);
  54. hdr[ETH_ALEN * 2] = priority;
  55. hdr[ETH_ALEN * 2 + 1] = 0;
  56. hdr[ETH_ALEN * 2 + 2] = 0;
  57. hdr[ETH_ALEN * 2 + 3] = 0;
  58. /* Use scatter gather to MIC header and data in one go */
  59. sg_init_table(sg, 2);
  60. sg_set_buf(&sg[0], hdr, sizeof(hdr));
  61. sg_set_buf(&sg[1], data, data_len);
  62. if (crypto_hash_setkey(tfm_michael, key, MIC_KEYLEN))
  63. return -1;
  64. desc.tfm = tfm_michael;
  65. desc.flags = 0;
  66. return crypto_hash_digest(&desc, sg, data_len + sizeof(hdr),
  67. mic);
  68. }