wlan_crypto_aes_ctr.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2017 The Linux Foundation. All rights reserved.
  3. */
  4. /*
  5. * AES-128/192/256 CTR
  6. *
  7. * Copyright (c) 2003-2007, Jouni Malinen <[email protected]>
  8. *
  9. * This software may be distributed under the terms of the BSD license.
  10. * See README for more details.
  11. */
  12. #ifdef WLAN_SUPPORT_FILS
  13. #include <qdf_crypto.h>
  14. #include "wlan_crypto_aes_i.h"
  15. int32_t wlan_crypto_aes_ctr_encrypt(const uint8_t *key, size_t key_len,
  16. const uint8_t *nonce, uint8_t *data,
  17. size_t data_len)
  18. {
  19. void *ctx;
  20. size_t j, len, left = data_len;
  21. int32_t i;
  22. uint8_t *pos = data;
  23. uint8_t counter[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
  24. int32_t status = -1;
  25. ctx = wlan_crypto_aes_encrypt_init(key, key_len);
  26. if (!ctx)
  27. return status;
  28. qdf_mem_copy(counter, nonce, AES_BLOCK_SIZE);
  29. while (left > 0) {
  30. wlan_crypto_aes_encrypt(ctx, counter, buf);
  31. len = (left < AES_BLOCK_SIZE) ? left : AES_BLOCK_SIZE;
  32. for (j = 0; j < len; j++)
  33. pos[j] ^= buf[j];
  34. pos += len;
  35. left -= len;
  36. for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
  37. counter[i]++;
  38. if (counter[i])
  39. break;
  40. }
  41. }
  42. wlan_crypto_aes_encrypt_deinit(ctx);
  43. return 0;
  44. }
  45. int32_t wlan_crypto_aes_128_ctr_encrypt(const uint8_t *key,
  46. const uint8_t *nonce, uint8_t *data,
  47. size_t data_len)
  48. {
  49. return wlan_crypto_aes_ctr_encrypt(key, 16, nonce, data, data_len);
  50. }
  51. #endif /* WLAN_SUPPORT_FILS */