aes_ccm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 aes_ccm_prepare(struct crypto_cipher *tfm, u8 *scratch, u8 *a)
  17. {
  18. int i;
  19. u8 *b_0, *aad, *b, *s_0;
  20. b_0 = scratch + 3 * AES_BLOCK_LEN;
  21. aad = scratch + 4 * AES_BLOCK_LEN;
  22. b = scratch;
  23. s_0 = scratch + AES_BLOCK_LEN;
  24. crypto_cipher_encrypt_one(tfm, b, b_0);
  25. /* Extra Authenticate-only data (always two AES blocks) */
  26. for (i = 0; i < AES_BLOCK_LEN; i++)
  27. aad[i] ^= b[i];
  28. crypto_cipher_encrypt_one(tfm, b, aad);
  29. aad += AES_BLOCK_LEN;
  30. for (i = 0; i < AES_BLOCK_LEN; i++)
  31. aad[i] ^= b[i];
  32. crypto_cipher_encrypt_one(tfm, a, aad);
  33. /* Mask out bits from auth-only-b_0 */
  34. b_0[0] &= 0x07;
  35. /* S_0 is used to encrypt T (= MIC) */
  36. b_0[14] = 0;
  37. b_0[15] = 0;
  38. crypto_cipher_encrypt_one(tfm, s_0, b_0);
  39. }
  40. void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch,
  41. u8 *data, size_t data_len,
  42. u8 *cdata, u8 *mic)
  43. {
  44. int i, j, last_len, num_blocks;
  45. u8 *pos, *cpos, *b, *s_0, *e, *b_0, *aad;
  46. b = scratch;
  47. s_0 = scratch + AES_BLOCK_LEN;
  48. e = scratch + 2 * AES_BLOCK_LEN;
  49. b_0 = scratch + 3 * AES_BLOCK_LEN;
  50. aad = scratch + 4 * AES_BLOCK_LEN;
  51. num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
  52. last_len = data_len % AES_BLOCK_LEN;
  53. aes_ccm_prepare(tfm, scratch, b);
  54. /* Process payload blocks */
  55. pos = data;
  56. cpos = cdata;
  57. for (j = 1; j <= num_blocks; j++) {
  58. int blen = (j == num_blocks && last_len) ?
  59. last_len : AES_BLOCK_LEN;
  60. /* Authentication followed by encryption */
  61. for (i = 0; i < blen; i++)
  62. b[i] ^= pos[i];
  63. crypto_cipher_encrypt_one(tfm, b, b);
  64. b_0[14] = (j >> 8) & 0xff;
  65. b_0[15] = j & 0xff;
  66. crypto_cipher_encrypt_one(tfm, e, b_0);
  67. for (i = 0; i < blen; i++)
  68. *cpos++ = *pos++ ^ e[i];
  69. }
  70. for (i = 0; i < CCMP_MIC_LEN; i++)
  71. mic[i] = b[i] ^ s_0[i];
  72. }
  73. int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *scratch,
  74. u8 *cdata, size_t data_len, u8 *mic, u8 *data)
  75. {
  76. int i, j, last_len, num_blocks;
  77. u8 *pos, *cpos, *b, *s_0, *a, *b_0, *aad;
  78. b = scratch;
  79. s_0 = scratch + AES_BLOCK_LEN;
  80. a = scratch + 2 * AES_BLOCK_LEN;
  81. b_0 = scratch + 3 * AES_BLOCK_LEN;
  82. aad = scratch + 4 * AES_BLOCK_LEN;
  83. num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
  84. last_len = data_len % AES_BLOCK_LEN;
  85. aes_ccm_prepare(tfm, scratch, a);
  86. /* Process payload blocks */
  87. cpos = cdata;
  88. pos = data;
  89. for (j = 1; j <= num_blocks; j++) {
  90. int blen = (j == num_blocks && last_len) ?
  91. last_len : AES_BLOCK_LEN;
  92. /* Decryption followed by authentication */
  93. b_0[14] = (j >> 8) & 0xff;
  94. b_0[15] = j & 0xff;
  95. crypto_cipher_encrypt_one(tfm, b, b_0);
  96. for (i = 0; i < blen; i++) {
  97. *pos = *cpos++ ^ b[i];
  98. a[i] ^= *pos++;
  99. }
  100. crypto_cipher_encrypt_one(tfm, a, a);
  101. }
  102. for (i = 0; i < CCMP_MIC_LEN; i++) {
  103. if ((mic[i] ^ s_0[i]) != a[i])
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[])
  109. {
  110. struct crypto_cipher *tfm;
  111. tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
  112. if (IS_ERR(tfm))
  113. return NULL;
  114. crypto_cipher_setkey(tfm, key, ALG_CCMP_KEY_LEN);
  115. return tfm;
  116. }
  117. void ieee80211_aes_key_free(struct crypto_cipher *tfm)
  118. {
  119. if (tfm)
  120. crypto_free_cipher(tfm);
  121. }