aes_ccm.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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;
  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. num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
  51. last_len = data_len % AES_BLOCK_LEN;
  52. aes_ccm_prepare(tfm, scratch, 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. crypto_cipher_encrypt_one(tfm, b, b);
  63. b_0[14] = (j >> 8) & 0xff;
  64. b_0[15] = j & 0xff;
  65. crypto_cipher_encrypt_one(tfm, e, b_0);
  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 *cdata, size_t data_len, u8 *mic, u8 *data)
  74. {
  75. int i, j, last_len, num_blocks;
  76. u8 *pos, *cpos, *b, *s_0, *a, *b_0;
  77. b = scratch;
  78. s_0 = scratch + AES_BLOCK_LEN;
  79. a = scratch + 2 * AES_BLOCK_LEN;
  80. b_0 = scratch + 3 * 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, scratch, 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. crypto_cipher_encrypt_one(tfm, b, b_0);
  94. for (i = 0; i < blen; i++) {
  95. *pos = *cpos++ ^ b[i];
  96. a[i] ^= *pos++;
  97. }
  98. crypto_cipher_encrypt_one(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. crypto_cipher_setkey(tfm, key, ALG_CCMP_KEY_LEN);
  112. return tfm;
  113. }
  114. void ieee80211_aes_key_free(struct crypto_cipher *tfm)
  115. {
  116. crypto_free_cipher(tfm);
  117. }