aes_ccm.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2003-2004, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/crypto.h>
  12. #include <linux/err.h>
  13. #include <net/mac80211.h>
  14. #include "key.h"
  15. #include "aes_ccm.h"
  16. static void ieee80211_aes_encrypt(struct crypto_cipher *tfm,
  17. const u8 pt[16], u8 ct[16])
  18. {
  19. crypto_cipher_encrypt_one(tfm, ct, pt);
  20. }
  21. static inline void aes_ccm_prepare(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
  22. u8 *b, u8 *s_0, u8 *a)
  23. {
  24. int i;
  25. ieee80211_aes_encrypt(tfm, b_0, b);
  26. /* Extra Authenticate-only data (always two AES blocks) */
  27. for (i = 0; i < AES_BLOCK_LEN; i++)
  28. aad[i] ^= b[i];
  29. ieee80211_aes_encrypt(tfm, aad, b);
  30. aad += AES_BLOCK_LEN;
  31. for (i = 0; i < AES_BLOCK_LEN; i++)
  32. aad[i] ^= b[i];
  33. ieee80211_aes_encrypt(tfm, aad, a);
  34. /* Mask out bits from auth-only-b_0 */
  35. b_0[0] &= 0x07;
  36. /* S_0 is used to encrypt T (= MIC) */
  37. b_0[14] = 0;
  38. b_0[15] = 0;
  39. ieee80211_aes_encrypt(tfm, b_0, s_0);
  40. }
  41. void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch,
  42. u8 *b_0, u8 *aad, u8 *data, size_t data_len,
  43. u8 *cdata, u8 *mic)
  44. {
  45. int i, j, last_len, num_blocks;
  46. u8 *pos, *cpos, *b, *s_0, *e;
  47. b = scratch;
  48. s_0 = scratch + AES_BLOCK_LEN;
  49. e = scratch + 2 * AES_BLOCK_LEN;
  50. num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
  51. last_len = data_len % AES_BLOCK_LEN;
  52. aes_ccm_prepare(tfm, b_0, aad, b, s_0, b);
  53. /* Process payload blocks */
  54. pos = data;
  55. cpos = cdata;
  56. for (j = 1; j <= num_blocks; j++) {
  57. int blen = (j == num_blocks && last_len) ?
  58. last_len : AES_BLOCK_LEN;
  59. /* Authentication followed by encryption */
  60. for (i = 0; i < blen; i++)
  61. b[i] ^= pos[i];
  62. ieee80211_aes_encrypt(tfm, b, b);
  63. b_0[14] = (j >> 8) & 0xff;
  64. b_0[15] = j & 0xff;
  65. ieee80211_aes_encrypt(tfm, b_0, e);
  66. for (i = 0; i < blen; i++)
  67. *cpos++ = *pos++ ^ e[i];
  68. }
  69. for (i = 0; i < CCMP_MIC_LEN; i++)
  70. mic[i] = b[i] ^ s_0[i];
  71. }
  72. int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *scratch,
  73. u8 *b_0, u8 *aad, u8 *cdata, size_t data_len,
  74. u8 *mic, u8 *data)
  75. {
  76. int i, j, last_len, num_blocks;
  77. u8 *pos, *cpos, *b, *s_0, *a;
  78. b = scratch;
  79. s_0 = scratch + AES_BLOCK_LEN;
  80. a = scratch + 2 * AES_BLOCK_LEN;
  81. num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
  82. last_len = data_len % AES_BLOCK_LEN;
  83. aes_ccm_prepare(tfm, b_0, aad, b, s_0, a);
  84. /* Process payload blocks */
  85. cpos = cdata;
  86. pos = data;
  87. for (j = 1; j <= num_blocks; j++) {
  88. int blen = (j == num_blocks && last_len) ?
  89. last_len : AES_BLOCK_LEN;
  90. /* Decryption followed by authentication */
  91. b_0[14] = (j >> 8) & 0xff;
  92. b_0[15] = j & 0xff;
  93. ieee80211_aes_encrypt(tfm, b_0, b);
  94. for (i = 0; i < blen; i++) {
  95. *pos = *cpos++ ^ b[i];
  96. a[i] ^= *pos++;
  97. }
  98. ieee80211_aes_encrypt(tfm, a, a);
  99. }
  100. for (i = 0; i < CCMP_MIC_LEN; i++) {
  101. if ((mic[i] ^ s_0[i]) != a[i])
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. struct crypto_cipher * ieee80211_aes_key_setup_encrypt(const u8 key[])
  107. {
  108. struct crypto_cipher *tfm;
  109. tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
  110. if (IS_ERR(tfm))
  111. return NULL;
  112. crypto_cipher_setkey(tfm, key, ALG_CCMP_KEY_LEN);
  113. return tfm;
  114. }
  115. void ieee80211_aes_key_free(struct crypto_cipher *tfm)
  116. {
  117. if (tfm)
  118. crypto_free_cipher(tfm);
  119. }