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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. From: Felix Fietkau <nbd@nbd.name>
  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 <nbd@nbd.name>
  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. + int i, carry;
  20. +
  21. + carry = pad[0] & 0x80;
  22. + for (i = 0; i < AES_BLOCK_SIZE - 1; i++)
  23. + pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
  24. + pad[AES_BLOCK_SIZE - 1] <<= 1;
  25. + if (carry)
  26. + pad[AES_BLOCK_SIZE - 1] ^= 0x87;
  27. +}
  28. +
  29. +void aes_cmac_vector(struct crypto_cipher *tfm, size_t num_elem,
  30. + const u8 *addr[], const size_t *len, u8 *mac,
  31. + size_t mac_len)
  32. {
  33. - SHASH_DESC_ON_STACK(desc, tfm);
  34. - u8 out[AES_BLOCK_SIZE];
  35. + u8 cbc[AES_BLOCK_SIZE], pad[AES_BLOCK_SIZE];
  36. + const u8 *pos, *end;
  37. + size_t i, e, left, total_len;
  38. +
  39. + memset(cbc, 0, AES_BLOCK_SIZE);
  40. +
  41. + total_len = 0;
  42. + for (e = 0; e < num_elem; e++)
  43. + total_len += len[e];
  44. + left = total_len;
  45. +
  46. + e = 0;
  47. + pos = addr[0];
  48. + end = pos + len[0];
  49. +
  50. + while (left >= AES_BLOCK_SIZE) {
  51. + for (i = 0; i < AES_BLOCK_SIZE; i++) {
  52. + cbc[i] ^= *pos++;
  53. + if (pos >= end) {
  54. + e++;
  55. + pos = addr[e];
  56. + end = pos + len[e];
  57. + }
  58. + }
  59. + if (left > AES_BLOCK_SIZE)
  60. + crypto_cipher_encrypt_one(tfm, cbc, cbc);
  61. + left -= AES_BLOCK_SIZE;
  62. + }
  63. +
  64. + memset(pad, 0, AES_BLOCK_SIZE);
  65. + crypto_cipher_encrypt_one(tfm, pad, pad);
  66. + gf_mulx(pad);
  67. +
  68. + if (left || total_len == 0) {
  69. + for (i = 0; i < left; i++) {
  70. + cbc[i] ^= *pos++;
  71. + if (pos >= end) {
  72. + e++;
  73. + pos = addr[e];
  74. + end = pos + len[e];
  75. + }
  76. + }
  77. + cbc[left] ^= 0x80;
  78. + gf_mulx(pad);
  79. + }
  80. +
  81. + for (i = 0; i < AES_BLOCK_SIZE; i++)
  82. + pad[i] ^= cbc[i];
  83. + crypto_cipher_encrypt_one(tfm, pad, pad);
  84. + memcpy(mac, pad, mac_len);
  85. +}
  86. - desc->tfm = tfm;
  87. - crypto_shash_init(desc);
  88. - crypto_shash_update(desc, aad, AAD_LEN);
  89. - crypto_shash_update(desc, data, data_len - CMAC_TLEN);
  90. - crypto_shash_finup(desc, zero, CMAC_TLEN, out);
  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. - memcpy(mic, out, CMAC_TLEN);
  106. + aes_cmac_vector(tfm, 3, addr, len, mic, CMAC_TLEN);
  107. }
  108. -void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
  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. +
  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. - desc->tfm = tfm;
  125. -
  126. - crypto_shash_init(desc);
  127. - crypto_shash_update(desc, aad, AAD_LEN);
  128. - crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
  129. - crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
  130. + aes_cmac_vector(tfm, 3, addr, len, mic, CMAC_TLEN_256);
  131. }
  132. -struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
  133. - size_t key_len)
  134. +struct crypto_cipher *ieee80211_aes_cmac_key_setup(const u8 key[],
  135. + size_t key_len)
  136. {
  137. - struct crypto_shash *tfm;
  138. + struct crypto_cipher *tfm;
  139. - tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
  140. + tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
  141. if (!IS_ERR(tfm))
  142. - crypto_shash_setkey(tfm, key, key_len);
  143. + crypto_cipher_setkey(tfm, key, key_len);
  144. return tfm;
  145. }
  146. -void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
  147. +
  148. +void ieee80211_aes_cmac_key_free(struct crypto_cipher *tfm)
  149. {
  150. - crypto_free_shash(tfm);
  151. + crypto_free_cipher(tfm);
  152. }
  153. --- a/net/mac80211/aes_cmac.h
  154. +++ b/net/mac80211/aes_cmac.h
  155. @@ -10,14 +10,13 @@
  156. #define AES_CMAC_H
  157. #include <linux/crypto.h>
  158. -#include <crypto/hash.h>
  159. -struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
  160. - size_t key_len);
  161. -void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
  162. +struct crypto_cipher *ieee80211_aes_cmac_key_setup(const u8 key[],
  163. + size_t key_len);
  164. +void ieee80211_aes_cmac(struct crypto_cipher *tfm, const u8 *aad,
  165. const u8 *data, size_t data_len, u8 *mic);
  166. -void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
  167. +void ieee80211_aes_cmac_256(struct crypto_cipher *tfm, const u8 *aad,
  168. const u8 *data, size_t data_len, u8 *mic);
  169. -void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm);
  170. +void ieee80211_aes_cmac_key_free(struct crypto_cipher *tfm);
  171. #endif /* AES_CMAC_H */
  172. --- a/net/mac80211/key.h
  173. +++ b/net/mac80211/key.h
  174. @@ -93,7 +93,7 @@ struct ieee80211_key {
  175. } ccmp;
  176. struct {
  177. u8 rx_pn[IEEE80211_CMAC_PN_LEN];
  178. - struct crypto_shash *tfm;
  179. + struct crypto_cipher *tfm;
  180. u32 replays; /* dot11RSNAStatsCMACReplays */
  181. u32 icverrors; /* dot11RSNAStatsCMACICVErrors */
  182. } aes_cmac;