aes_gcm.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2014-2015, Qualcomm Atheros, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include <linux/err.h>
  11. #include <crypto/aead.h>
  12. #include <net/mac80211.h>
  13. #include "key.h"
  14. #include "aes_gcm.h"
  15. void ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
  16. u8 *data, size_t data_len, u8 *mic)
  17. {
  18. struct scatterlist sg[3];
  19. char aead_req_data[sizeof(struct aead_request) +
  20. crypto_aead_reqsize(tfm)]
  21. __aligned(__alignof__(struct aead_request));
  22. struct aead_request *aead_req = (void *)aead_req_data;
  23. memset(aead_req, 0, sizeof(aead_req_data));
  24. sg_init_table(sg, 3);
  25. sg_set_buf(&sg[0], &aad[2], be16_to_cpup((__be16 *)aad));
  26. sg_set_buf(&sg[1], data, data_len);
  27. sg_set_buf(&sg[2], mic, IEEE80211_GCMP_MIC_LEN);
  28. aead_request_set_tfm(aead_req, tfm);
  29. aead_request_set_crypt(aead_req, sg, sg, data_len, j_0);
  30. aead_request_set_ad(aead_req, sg[0].length);
  31. crypto_aead_encrypt(aead_req);
  32. }
  33. int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
  34. u8 *data, size_t data_len, u8 *mic)
  35. {
  36. struct scatterlist sg[3];
  37. char aead_req_data[sizeof(struct aead_request) +
  38. crypto_aead_reqsize(tfm)]
  39. __aligned(__alignof__(struct aead_request));
  40. struct aead_request *aead_req = (void *)aead_req_data;
  41. if (data_len == 0)
  42. return -EINVAL;
  43. memset(aead_req, 0, sizeof(aead_req_data));
  44. sg_init_table(sg, 3);
  45. sg_set_buf(&sg[0], &aad[2], be16_to_cpup((__be16 *)aad));
  46. sg_set_buf(&sg[1], data, data_len);
  47. sg_set_buf(&sg[2], mic, IEEE80211_GCMP_MIC_LEN);
  48. aead_request_set_tfm(aead_req, tfm);
  49. aead_request_set_crypt(aead_req, sg, sg,
  50. data_len + IEEE80211_GCMP_MIC_LEN, j_0);
  51. aead_request_set_ad(aead_req, sg[0].length);
  52. return crypto_aead_decrypt(aead_req);
  53. }
  54. struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[],
  55. size_t key_len)
  56. {
  57. struct crypto_aead *tfm;
  58. int err;
  59. tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
  60. if (IS_ERR(tfm))
  61. return tfm;
  62. err = crypto_aead_setkey(tfm, key, key_len);
  63. if (err)
  64. goto free_aead;
  65. err = crypto_aead_setauthsize(tfm, IEEE80211_GCMP_MIC_LEN);
  66. if (err)
  67. goto free_aead;
  68. return tfm;
  69. free_aead:
  70. crypto_free_aead(tfm);
  71. return ERR_PTR(err);
  72. }
  73. void ieee80211_aes_gcm_key_free(struct crypto_aead *tfm)
  74. {
  75. crypto_free_aead(tfm);
  76. }