131-Revert-mac80211-aes-cmac-switch-to-shash-CMAC-driver.patch 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. From: Felix Fietkau <[email protected]>
  2. Date: Sat, 7 Oct 2017 09:37:28 +0200
  3. Subject: [PATCH] Revert "mac80211: aes-cmac: switch to shash CMAC
  4. driver"
  5. This reverts commit 26717828b75dd5c46e97f7f4a9b937d038bb2852.
  6. Reduces mac80211 dependencies for LEDE
  7. Signed-off-by: Felix Fietkau <[email protected]>
  8. ---
  9. --- a/net/mac80211/aes_cmac.c
  10. +++ b/net/mac80211/aes_cmac.c
  11. @@ -22,50 +22,126 @@
  12. #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
  13. #define AAD_LEN 20
  14. -static const u8 zero[CMAC_TLEN_256];
  15. -void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
  16. - const u8 *data, size_t data_len, u8 *mic)
  17. +void gf_mulx(u8 *pad)
  18. {
  19. - SHASH_DESC_ON_STACK(desc, tfm);
  20. - u8 out[AES_BLOCK_SIZE];
  21. + int i, carry;
  22. - desc->tfm = tfm;
  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. - crypto_shash_init(desc);
  31. - crypto_shash_update(desc, aad, AAD_LEN);
  32. - crypto_shash_update(desc, data, data_len - CMAC_TLEN);
  33. - crypto_shash_finup(desc, zero, CMAC_TLEN, out);
  34. +void aes_cmac_vector(struct crypto_cipher *tfm, size_t num_elem,
  35. + const u8 *addr[], const size_t *len, u8 *mac,
  36. + size_t mac_len)
  37. +{
  38. + u8 cbc[AES_BLOCK_SIZE], pad[AES_BLOCK_SIZE];
  39. + const u8 *pos, *end;
  40. + size_t i, e, left, total_len;
  41. - memcpy(mic, out, CMAC_TLEN);
  42. + memset(cbc, 0, AES_BLOCK_SIZE);
  43. +
  44. + total_len = 0;
  45. + for (e = 0; e < num_elem; e++)
  46. + total_len += len[e];
  47. + left = total_len;
  48. +
  49. + e = 0;
  50. + pos = addr[0];
  51. + end = pos + len[0];
  52. +
  53. + while (left >= AES_BLOCK_SIZE) {
  54. + for (i = 0; i < AES_BLOCK_SIZE; i++) {
  55. + cbc[i] ^= *pos++;
  56. + if (pos >= end) {
  57. + e++;
  58. + pos = addr[e];
  59. + end = pos + len[e];
  60. + }
  61. + }
  62. + if (left > AES_BLOCK_SIZE)
  63. + crypto_cipher_encrypt_one(tfm, cbc, cbc);
  64. + left -= AES_BLOCK_SIZE;
  65. + }
  66. +
  67. + memset(pad, 0, AES_BLOCK_SIZE);
  68. + crypto_cipher_encrypt_one(tfm, pad, pad);
  69. + gf_mulx(pad);
  70. +
  71. + if (left || total_len == 0) {
  72. + for (i = 0; i < left; i++) {
  73. + cbc[i] ^= *pos++;
  74. + if (pos >= end) {
  75. + e++;
  76. + pos = addr[e];
  77. + end = pos + len[e];
  78. + }
  79. + }
  80. + cbc[left] ^= 0x80;
  81. + gf_mulx(pad);
  82. + }
  83. +
  84. + for (i = 0; i < AES_BLOCK_SIZE; i++)
  85. + pad[i] ^= cbc[i];
  86. + crypto_cipher_encrypt_one(tfm, pad, pad);
  87. + memcpy(mac, pad, mac_len);
  88. }
  89. -void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
  90. +
  91. +void ieee80211_aes_cmac(struct crypto_cipher *tfm, const u8 *aad,
  92. + const u8 *data, size_t data_len, u8 *mic)
  93. +{
  94. + const u8 *addr[3];
  95. + size_t len[3];
  96. + u8 zero[CMAC_TLEN];
  97. +
  98. + memset(zero, 0, CMAC_TLEN);
  99. + addr[0] = aad;
  100. + len[0] = AAD_LEN;
  101. + addr[1] = data;
  102. + len[1] = data_len - CMAC_TLEN;
  103. + addr[2] = zero;
  104. + len[2] = CMAC_TLEN;
  105. +
  106. + aes_cmac_vector(tfm, 3, addr, len, mic, CMAC_TLEN);
  107. +}
  108. +
  109. +void ieee80211_aes_cmac_256(struct crypto_cipher *tfm, const u8 *aad,
  110. const u8 *data, size_t data_len, u8 *mic)
  111. {
  112. - SHASH_DESC_ON_STACK(desc, tfm);
  113. + const u8 *addr[3];
  114. + size_t len[3];
  115. + u8 zero[CMAC_TLEN_256];
  116. - desc->tfm = tfm;
  117. + memset(zero, 0, CMAC_TLEN_256);
  118. + addr[0] = aad;
  119. + len[0] = AAD_LEN;
  120. + addr[1] = data;
  121. + len[1] = data_len - CMAC_TLEN_256;
  122. + addr[2] = zero;
  123. + len[2] = CMAC_TLEN_256;
  124. - crypto_shash_init(desc);
  125. - crypto_shash_update(desc, aad, AAD_LEN);
  126. - crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
  127. - crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
  128. + aes_cmac_vector(tfm, 3, addr, len, mic, CMAC_TLEN_256);
  129. }
  130. -struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
  131. - size_t key_len)
  132. +struct crypto_cipher *ieee80211_aes_cmac_key_setup(const u8 key[],
  133. + size_t key_len)
  134. {
  135. - struct crypto_shash *tfm;
  136. + struct crypto_cipher *tfm;
  137. - tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
  138. + tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
  139. if (!IS_ERR(tfm))
  140. - crypto_shash_setkey(tfm, key, key_len);
  141. + crypto_cipher_setkey(tfm, key, key_len);
  142. return tfm;
  143. }
  144. -void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
  145. +
  146. +void ieee80211_aes_cmac_key_free(struct crypto_cipher *tfm)
  147. {
  148. - crypto_free_shash(tfm);
  149. + crypto_free_cipher(tfm);
  150. }
  151. --- a/net/mac80211/aes_cmac.h
  152. +++ b/net/mac80211/aes_cmac.h
  153. @@ -10,14 +10,13 @@
  154. #define AES_CMAC_H
  155. #include <linux/crypto.h>
  156. -#include <crypto/hash.h>
  157. -struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
  158. - size_t key_len);
  159. -void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
  160. +struct crypto_cipher *ieee80211_aes_cmac_key_setup(const u8 key[],
  161. + size_t key_len);
  162. +void ieee80211_aes_cmac(struct crypto_cipher *tfm, const u8 *aad,
  163. const u8 *data, size_t data_len, u8 *mic);
  164. -void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
  165. +void ieee80211_aes_cmac_256(struct crypto_cipher *tfm, const u8 *aad,
  166. const u8 *data, size_t data_len, u8 *mic);
  167. -void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm);
  168. +void ieee80211_aes_cmac_key_free(struct crypto_cipher *tfm);
  169. #endif /* AES_CMAC_H */
  170. --- a/net/mac80211/key.h
  171. +++ b/net/mac80211/key.h
  172. @@ -93,7 +93,7 @@ struct ieee80211_key {
  173. } ccmp;
  174. struct {
  175. u8 rx_pn[IEEE80211_CMAC_PN_LEN];
  176. - struct crypto_shash *tfm;
  177. + struct crypto_cipher *tfm;
  178. u32 replays; /* dot11RSNAStatsCMACReplays */
  179. u32 icverrors; /* dot11RSNAStatsCMACICVErrors */
  180. } aes_cmac;