aes_cmac.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * AES-128-CMAC with TLen 16 for IEEE 802.11w BIP
  3. * Copyright 2008, Jouni Malinen <j@w1.fi>
  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_cmac.h"
  17. #define AES_CMAC_KEY_LEN 16
  18. #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
  19. #define AAD_LEN 20
  20. static void gf_mulx(u8 *pad)
  21. {
  22. int i, carry;
  23. carry = pad[0] & 0x80;
  24. for (i = 0; i < AES_BLOCK_SIZE - 1; i++)
  25. pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
  26. pad[AES_BLOCK_SIZE - 1] <<= 1;
  27. if (carry)
  28. pad[AES_BLOCK_SIZE - 1] ^= 0x87;
  29. }
  30. static void aes_128_cmac_vector(struct crypto_cipher *tfm, size_t num_elem,
  31. const u8 *addr[], const size_t *len, u8 *mac)
  32. {
  33. u8 cbc[AES_BLOCK_SIZE], pad[AES_BLOCK_SIZE];
  34. const u8 *pos, *end;
  35. size_t i, e, left, total_len;
  36. memset(cbc, 0, AES_BLOCK_SIZE);
  37. total_len = 0;
  38. for (e = 0; e < num_elem; e++)
  39. total_len += len[e];
  40. left = total_len;
  41. e = 0;
  42. pos = addr[0];
  43. end = pos + len[0];
  44. while (left >= AES_BLOCK_SIZE) {
  45. for (i = 0; i < AES_BLOCK_SIZE; i++) {
  46. cbc[i] ^= *pos++;
  47. if (pos >= end) {
  48. e++;
  49. pos = addr[e];
  50. end = pos + len[e];
  51. }
  52. }
  53. if (left > AES_BLOCK_SIZE)
  54. crypto_cipher_encrypt_one(tfm, cbc, cbc);
  55. left -= AES_BLOCK_SIZE;
  56. }
  57. memset(pad, 0, AES_BLOCK_SIZE);
  58. crypto_cipher_encrypt_one(tfm, pad, pad);
  59. gf_mulx(pad);
  60. if (left || total_len == 0) {
  61. for (i = 0; i < left; i++) {
  62. cbc[i] ^= *pos++;
  63. if (pos >= end) {
  64. e++;
  65. pos = addr[e];
  66. end = pos + len[e];
  67. }
  68. }
  69. cbc[left] ^= 0x80;
  70. gf_mulx(pad);
  71. }
  72. for (i = 0; i < AES_BLOCK_SIZE; i++)
  73. pad[i] ^= cbc[i];
  74. crypto_cipher_encrypt_one(tfm, pad, pad);
  75. memcpy(mac, pad, CMAC_TLEN);
  76. }
  77. void ieee80211_aes_cmac(struct crypto_cipher *tfm, const u8 *aad,
  78. const u8 *data, size_t data_len, u8 *mic)
  79. {
  80. const u8 *addr[3];
  81. size_t len[3];
  82. u8 zero[CMAC_TLEN];
  83. memset(zero, 0, CMAC_TLEN);
  84. addr[0] = aad;
  85. len[0] = AAD_LEN;
  86. addr[1] = data;
  87. len[1] = data_len - CMAC_TLEN;
  88. addr[2] = zero;
  89. len[2] = CMAC_TLEN;
  90. aes_128_cmac_vector(tfm, 3, addr, len, mic);
  91. }
  92. struct crypto_cipher * ieee80211_aes_cmac_key_setup(const u8 key[])
  93. {
  94. struct crypto_cipher *tfm;
  95. tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
  96. if (!IS_ERR(tfm))
  97. crypto_cipher_setkey(tfm, key, AES_CMAC_KEY_LEN);
  98. return tfm;
  99. }
  100. void ieee80211_aes_cmac_key_free(struct crypto_cipher *tfm)
  101. {
  102. crypto_free_cipher(tfm);
  103. }