aes_cmac.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * AES-128-CMAC with TLen 16 for IEEE 802.11w BIP
  3. * Copyright 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/crypto.h>
  12. #include <linux/export.h>
  13. #include <linux/err.h>
  14. #include <crypto/aes.h>
  15. #include <net/mac80211.h>
  16. #include "key.h"
  17. #include "aes_cmac.h"
  18. #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
  19. #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
  20. #define AAD_LEN 20
  21. static const u8 zero[CMAC_TLEN_256];
  22. void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
  23. const u8 *data, size_t data_len, u8 *mic)
  24. {
  25. SHASH_DESC_ON_STACK(desc, tfm);
  26. u8 out[AES_BLOCK_SIZE];
  27. desc->tfm = tfm;
  28. crypto_shash_init(desc);
  29. crypto_shash_update(desc, aad, AAD_LEN);
  30. crypto_shash_update(desc, data, data_len - CMAC_TLEN);
  31. crypto_shash_finup(desc, zero, CMAC_TLEN, out);
  32. memcpy(mic, out, CMAC_TLEN);
  33. }
  34. void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
  35. const u8 *data, size_t data_len, u8 *mic)
  36. {
  37. SHASH_DESC_ON_STACK(desc, tfm);
  38. desc->tfm = tfm;
  39. crypto_shash_init(desc);
  40. crypto_shash_update(desc, aad, AAD_LEN);
  41. crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
  42. crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
  43. }
  44. struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
  45. size_t key_len)
  46. {
  47. struct crypto_shash *tfm;
  48. tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
  49. if (!IS_ERR(tfm))
  50. crypto_shash_setkey(tfm, key, key_len);
  51. return tfm;
  52. }
  53. void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
  54. {
  55. crypto_free_shash(tfm);
  56. }