aes_gcm.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. int 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. struct aead_request *aead_req;
  20. int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
  21. u8 *__aad;
  22. aead_req = kzalloc(reqsize + GCM_AAD_LEN, GFP_ATOMIC);
  23. if (!aead_req)
  24. return -ENOMEM;
  25. __aad = (u8 *)aead_req + reqsize;
  26. memcpy(__aad, aad, GCM_AAD_LEN);
  27. sg_init_table(sg, 3);
  28. sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad));
  29. sg_set_buf(&sg[1], data, data_len);
  30. sg_set_buf(&sg[2], mic, IEEE80211_GCMP_MIC_LEN);
  31. aead_request_set_tfm(aead_req, tfm);
  32. aead_request_set_crypt(aead_req, sg, sg, data_len, j_0);
  33. aead_request_set_ad(aead_req, sg[0].length);
  34. crypto_aead_encrypt(aead_req);
  35. kzfree(aead_req);
  36. return 0;
  37. }
  38. int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
  39. u8 *data, size_t data_len, u8 *mic)
  40. {
  41. struct scatterlist sg[3];
  42. struct aead_request *aead_req;
  43. int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
  44. u8 *__aad;
  45. int err;
  46. if (data_len == 0)
  47. return -EINVAL;
  48. aead_req = kzalloc(reqsize + GCM_AAD_LEN, GFP_ATOMIC);
  49. if (!aead_req)
  50. return -ENOMEM;
  51. __aad = (u8 *)aead_req + reqsize;
  52. memcpy(__aad, aad, GCM_AAD_LEN);
  53. sg_init_table(sg, 3);
  54. sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad));
  55. sg_set_buf(&sg[1], data, data_len);
  56. sg_set_buf(&sg[2], mic, IEEE80211_GCMP_MIC_LEN);
  57. aead_request_set_tfm(aead_req, tfm);
  58. aead_request_set_crypt(aead_req, sg, sg,
  59. data_len + IEEE80211_GCMP_MIC_LEN, j_0);
  60. aead_request_set_ad(aead_req, sg[0].length);
  61. err = crypto_aead_decrypt(aead_req);
  62. kzfree(aead_req);
  63. return err;
  64. }
  65. struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[],
  66. size_t key_len)
  67. {
  68. struct crypto_aead *tfm;
  69. int err;
  70. tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
  71. if (IS_ERR(tfm))
  72. return tfm;
  73. err = crypto_aead_setkey(tfm, key, key_len);
  74. if (err)
  75. goto free_aead;
  76. err = crypto_aead_setauthsize(tfm, IEEE80211_GCMP_MIC_LEN);
  77. if (err)
  78. goto free_aead;
  79. return tfm;
  80. free_aead:
  81. crypto_free_aead(tfm);
  82. return ERR_PTR(err);
  83. }
  84. void ieee80211_aes_gcm_key_free(struct crypto_aead *tfm)
  85. {
  86. crypto_free_aead(tfm);
  87. }