aes_ccm.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <crypto/aes.h>
  14. #include <net/mac80211.h>
  15. #include "key.h"
  16. #include "aes_ccm.h"
  17. static void aes_ccm_prepare(struct crypto_cipher *tfm, u8 *scratch, u8 *a)
  18. {
  19. int i;
  20. u8 *b_0, *aad, *b, *s_0;
  21. b_0 = scratch + 3 * AES_BLOCK_SIZE;
  22. aad = scratch + 4 * AES_BLOCK_SIZE;
  23. b = scratch;
  24. s_0 = scratch + AES_BLOCK_SIZE;
  25. crypto_cipher_encrypt_one(tfm, b, b_0);
  26. /* Extra Authenticate-only data (always two AES blocks) */
  27. for (i = 0; i < AES_BLOCK_SIZE; i++)
  28. aad[i] ^= b[i];
  29. crypto_cipher_encrypt_one(tfm, b, aad);
  30. aad += AES_BLOCK_SIZE;
  31. for (i = 0; i < AES_BLOCK_SIZE; i++)
  32. aad[i] ^= b[i];
  33. crypto_cipher_encrypt_one(tfm, a, aad);
  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. crypto_cipher_encrypt_one(tfm, s_0, b_0);
  40. }
  41. void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch,
  42. 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, *b_0;
  47. b = scratch;
  48. s_0 = scratch + AES_BLOCK_SIZE;
  49. e = scratch + 2 * AES_BLOCK_SIZE;
  50. b_0 = scratch + 3 * AES_BLOCK_SIZE;
  51. num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
  52. last_len = data_len % AES_BLOCK_SIZE;
  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_SIZE;
  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;
  78. b = scratch;
  79. s_0 = scratch + AES_BLOCK_SIZE;
  80. a = scratch + 2 * AES_BLOCK_SIZE;
  81. b_0 = scratch + 3 * AES_BLOCK_SIZE;
  82. num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
  83. last_len = data_len % AES_BLOCK_SIZE;
  84. aes_ccm_prepare(tfm, scratch, 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_SIZE;
  91. /* Decryption followed by authentication */
  92. b_0[14] = (j >> 8) & 0xff;
  93. b_0[15] = j & 0xff;
  94. crypto_cipher_encrypt_one(tfm, b, b_0);
  95. for (i = 0; i < blen; i++) {
  96. *pos = *cpos++ ^ b[i];
  97. a[i] ^= *pos++;
  98. }
  99. crypto_cipher_encrypt_one(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. 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. crypto_free_cipher(tfm);
  118. }