aes_gcm.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/crypto.h>
  11. #include <linux/err.h>
  12. #include <crypto/aes.h>
  13. #include <net/mac80211.h>
  14. #include "key.h"
  15. #include "aes_gcm.h"
  16. void ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
  17. u8 *data, size_t data_len, u8 *mic)
  18. {
  19. struct scatterlist assoc, pt, ct[2];
  20. char aead_req_data[sizeof(struct aead_request) +
  21. crypto_aead_reqsize(tfm)]
  22. __aligned(__alignof__(struct aead_request));
  23. struct aead_request *aead_req = (void *)aead_req_data;
  24. memset(aead_req, 0, sizeof(aead_req_data));
  25. sg_init_one(&pt, data, data_len);
  26. sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
  27. sg_init_table(ct, 2);
  28. sg_set_buf(&ct[0], data, data_len);
  29. sg_set_buf(&ct[1], mic, IEEE80211_GCMP_MIC_LEN);
  30. aead_request_set_tfm(aead_req, tfm);
  31. aead_request_set_assoc(aead_req, &assoc, assoc.length);
  32. aead_request_set_crypt(aead_req, &pt, ct, data_len, j_0);
  33. crypto_aead_encrypt(aead_req);
  34. }
  35. int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
  36. u8 *data, size_t data_len, u8 *mic)
  37. {
  38. struct scatterlist assoc, pt, ct[2];
  39. char aead_req_data[sizeof(struct aead_request) +
  40. crypto_aead_reqsize(tfm)]
  41. __aligned(__alignof__(struct aead_request));
  42. struct aead_request *aead_req = (void *)aead_req_data;
  43. if (data_len == 0)
  44. return -EINVAL;
  45. memset(aead_req, 0, sizeof(aead_req_data));
  46. sg_init_one(&pt, data, data_len);
  47. sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
  48. sg_init_table(ct, 2);
  49. sg_set_buf(&ct[0], data, data_len);
  50. sg_set_buf(&ct[1], mic, IEEE80211_GCMP_MIC_LEN);
  51. aead_request_set_tfm(aead_req, tfm);
  52. aead_request_set_assoc(aead_req, &assoc, assoc.length);
  53. aead_request_set_crypt(aead_req, ct, &pt,
  54. data_len + IEEE80211_GCMP_MIC_LEN, j_0);
  55. return crypto_aead_decrypt(aead_req);
  56. }
  57. struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[],
  58. size_t key_len)
  59. {
  60. struct crypto_aead *tfm;
  61. int err;
  62. tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
  63. if (IS_ERR(tfm))
  64. return tfm;
  65. err = crypto_aead_setkey(tfm, key, key_len);
  66. if (!err)
  67. err = crypto_aead_setauthsize(tfm, IEEE80211_GCMP_MIC_LEN);
  68. if (!err)
  69. return tfm;
  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. }