aes_ccm.c 3.4 KB

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