aes_ccm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2003-2004, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. *
  5. * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/err.h>
  14. #include <crypto/aead.h>
  15. #include <net/mac80211.h>
  16. #include "key.h"
  17. #include "aes_ccm.h"
  18. int ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
  19. u8 *data, size_t data_len, u8 *mic,
  20. size_t mic_len)
  21. {
  22. struct scatterlist sg[3];
  23. struct aead_request *aead_req;
  24. int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
  25. u8 *__aad;
  26. aead_req = kzalloc(reqsize + CCM_AAD_LEN, GFP_ATOMIC);
  27. if (!aead_req)
  28. return -ENOMEM;
  29. __aad = (u8 *)aead_req + reqsize;
  30. memcpy(__aad, aad, CCM_AAD_LEN);
  31. sg_init_table(sg, 3);
  32. sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad));
  33. sg_set_buf(&sg[1], data, data_len);
  34. sg_set_buf(&sg[2], mic, mic_len);
  35. aead_request_set_tfm(aead_req, tfm);
  36. aead_request_set_crypt(aead_req, sg, sg, data_len, b_0);
  37. aead_request_set_ad(aead_req, sg[0].length);
  38. crypto_aead_encrypt(aead_req);
  39. kzfree(aead_req);
  40. return 0;
  41. }
  42. int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
  43. u8 *data, size_t data_len, u8 *mic,
  44. size_t mic_len)
  45. {
  46. struct scatterlist sg[3];
  47. struct aead_request *aead_req;
  48. int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
  49. u8 *__aad;
  50. int err;
  51. if (data_len == 0)
  52. return -EINVAL;
  53. aead_req = kzalloc(reqsize + CCM_AAD_LEN, GFP_ATOMIC);
  54. if (!aead_req)
  55. return -ENOMEM;
  56. __aad = (u8 *)aead_req + reqsize;
  57. memcpy(__aad, aad, CCM_AAD_LEN);
  58. sg_init_table(sg, 3);
  59. sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad));
  60. sg_set_buf(&sg[1], data, data_len);
  61. sg_set_buf(&sg[2], mic, mic_len);
  62. aead_request_set_tfm(aead_req, tfm);
  63. aead_request_set_crypt(aead_req, sg, sg, data_len + mic_len, b_0);
  64. aead_request_set_ad(aead_req, sg[0].length);
  65. err = crypto_aead_decrypt(aead_req);
  66. kzfree(aead_req);
  67. return err;
  68. }
  69. struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
  70. size_t key_len,
  71. size_t mic_len)
  72. {
  73. struct crypto_aead *tfm;
  74. int err;
  75. tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
  76. if (IS_ERR(tfm))
  77. return tfm;
  78. err = crypto_aead_setkey(tfm, key, key_len);
  79. if (err)
  80. goto free_aead;
  81. err = crypto_aead_setauthsize(tfm, mic_len);
  82. if (err)
  83. goto free_aead;
  84. return tfm;
  85. free_aead:
  86. crypto_free_aead(tfm);
  87. return ERR_PTR(err);
  88. }
  89. void ieee80211_aes_key_free(struct crypto_aead *tfm)
  90. {
  91. crypto_free_aead(tfm);
  92. }