wpa_crypto.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* WPA2-PSK crypto: see wpa_crypto.h. Small and byte-oriented on purpose;
  2. * the hot path (PBKDF2, 8192 SHA-1 blocks) still takes well under a
  3. * second on the Braswell. */
  4. #include "wpa_crypto.h"
  5. static void cpy(unsigned char *d, const unsigned char *s, unsigned int n)
  6. {
  7. while (n--)
  8. *d++ = *s++;
  9. }
  10. /* ---- SHA-1 -------------------------------------------------------------- */
  11. struct sha1_ctx {
  12. unsigned int h[5];
  13. unsigned char buf[64];
  14. unsigned int used;
  15. unsigned long long total;
  16. };
  17. static unsigned int rol(unsigned int x, int n)
  18. {
  19. return (x << n) | (x >> (32 - n));
  20. }
  21. static void sha1_block(unsigned int h[5], const unsigned char *p)
  22. {
  23. unsigned int w[80], a, b, c, d, e, t, i;
  24. for (i = 0; i < 16; i++)
  25. w[i] = ((unsigned int)p[4 * i] << 24) | (p[4 * i + 1] << 16) |
  26. (p[4 * i + 2] << 8) | p[4 * i + 3];
  27. for (; i < 80; i++)
  28. w[i] = rol(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
  29. a = h[0]; b = h[1]; c = h[2]; d = h[3]; e = h[4];
  30. for (i = 0; i < 80; i++) {
  31. unsigned int f, k;
  32. if (i < 20) {
  33. f = (b & c) | (~b & d);
  34. k = 0x5a827999u;
  35. } else if (i < 40) {
  36. f = b ^ c ^ d;
  37. k = 0x6ed9eba1u;
  38. } else if (i < 60) {
  39. f = (b & c) | (b & d) | (c & d);
  40. k = 0x8f1bbcdcu;
  41. } else {
  42. f = b ^ c ^ d;
  43. k = 0xca62c1d6u;
  44. }
  45. t = rol(a, 5) + f + e + k + w[i];
  46. e = d; d = c; c = rol(b, 30); b = a; a = t;
  47. }
  48. h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e;
  49. }
  50. static void sha1_init(struct sha1_ctx *c)
  51. {
  52. c->h[0] = 0x67452301u;
  53. c->h[1] = 0xefcdab89u;
  54. c->h[2] = 0x98badcfeu;
  55. c->h[3] = 0x10325476u;
  56. c->h[4] = 0xc3d2e1f0u;
  57. c->used = 0;
  58. c->total = 0;
  59. }
  60. static void sha1_update(struct sha1_ctx *c, const unsigned char *d,
  61. unsigned int n)
  62. {
  63. c->total += n;
  64. while (n) {
  65. unsigned int take = 64 - c->used;
  66. if (take > n)
  67. take = n;
  68. cpy(c->buf + c->used, d, take);
  69. c->used += take;
  70. d += take;
  71. n -= take;
  72. if (c->used == 64) {
  73. sha1_block(c->h, c->buf);
  74. c->used = 0;
  75. }
  76. }
  77. }
  78. static void sha1_final(struct sha1_ctx *c, unsigned char out[20])
  79. {
  80. unsigned long long bits = c->total * 8;
  81. unsigned char pad = 0x80, z = 0, len[8];
  82. unsigned int i;
  83. sha1_update(c, &pad, 1);
  84. while (c->used != 56)
  85. sha1_update(c, &z, 1);
  86. for (i = 0; i < 8; i++)
  87. len[i] = (unsigned char)(bits >> (56 - 8 * i));
  88. sha1_update(c, len, 8);
  89. for (i = 0; i < 5; i++) {
  90. out[4 * i] = c->h[i] >> 24;
  91. out[4 * i + 1] = c->h[i] >> 16;
  92. out[4 * i + 2] = c->h[i] >> 8;
  93. out[4 * i + 3] = c->h[i];
  94. }
  95. }
  96. void sha1(const unsigned char *d, unsigned int n, unsigned char out[20])
  97. {
  98. struct sha1_ctx c;
  99. sha1_init(&c);
  100. sha1_update(&c, d, n);
  101. sha1_final(&c, out);
  102. }
  103. void hmac_sha1_2(const unsigned char *key, unsigned int klen,
  104. const unsigned char *d1, unsigned int n1,
  105. const unsigned char *d2, unsigned int n2,
  106. unsigned char out[20])
  107. {
  108. unsigned char k[64], pad[64], inner[20];
  109. struct sha1_ctx c;
  110. unsigned int i;
  111. for (i = 0; i < 64; i++)
  112. k[i] = 0;
  113. if (klen > 64)
  114. sha1(key, klen, k);
  115. else
  116. cpy(k, key, klen);
  117. for (i = 0; i < 64; i++)
  118. pad[i] = k[i] ^ 0x36;
  119. sha1_init(&c);
  120. sha1_update(&c, pad, 64);
  121. sha1_update(&c, d1, n1);
  122. if (n2)
  123. sha1_update(&c, d2, n2);
  124. sha1_final(&c, inner);
  125. for (i = 0; i < 64; i++)
  126. pad[i] = k[i] ^ 0x5c;
  127. sha1_init(&c);
  128. sha1_update(&c, pad, 64);
  129. sha1_update(&c, inner, 20);
  130. sha1_final(&c, out);
  131. }
  132. void hmac_sha1(const unsigned char *key, unsigned int klen,
  133. const unsigned char *d, unsigned int n, unsigned char out[20])
  134. {
  135. hmac_sha1_2(key, klen, d, n, 0, 0, out);
  136. }
  137. void pbkdf2_sha1(const unsigned char *pass, unsigned int plen,
  138. const unsigned char *salt, unsigned int slen,
  139. unsigned int iterations, unsigned char *out, unsigned int olen)
  140. {
  141. unsigned int blk = 1;
  142. while (olen) {
  143. unsigned char cnt[4], u[20], t[20];
  144. unsigned int i, j, take = olen < 20 ? olen : 20;
  145. cnt[0] = blk >> 24; cnt[1] = blk >> 16; cnt[2] = blk >> 8;
  146. cnt[3] = blk;
  147. hmac_sha1_2(pass, plen, salt, slen, cnt, 4, u);
  148. cpy(t, u, 20);
  149. for (i = 1; i < iterations; i++) {
  150. hmac_sha1(pass, plen, u, 20, u);
  151. for (j = 0; j < 20; j++)
  152. t[j] ^= u[j];
  153. }
  154. cpy(out, t, take);
  155. out += take;
  156. olen -= take;
  157. blk++;
  158. }
  159. }
  160. void wpa_prf(const unsigned char *key, unsigned int klen, const char *label,
  161. const unsigned char *b, unsigned int blen,
  162. unsigned char *out, unsigned int olen)
  163. {
  164. unsigned char msg[128 + 100], h[20];
  165. unsigned int llen = 0, i, n;
  166. while (label[llen])
  167. llen++;
  168. if (llen + 1 + blen + 1 > sizeof(msg))
  169. return;
  170. cpy(msg, (const unsigned char *)label, llen);
  171. msg[llen] = 0;
  172. cpy(msg + llen + 1, b, blen);
  173. n = llen + 1 + blen;
  174. for (i = 0; olen; i++) {
  175. unsigned int take = olen < 20 ? olen : 20;
  176. msg[n] = (unsigned char)i;
  177. hmac_sha1(key, klen, msg, n + 1, h);
  178. cpy(out, h, take);
  179. out += take;
  180. olen -= take;
  181. }
  182. }
  183. /* ---- AES-128 ----------------------------------------------------------- */
  184. static const unsigned char sbox[256] = {
  185. 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
  186. 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
  187. 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
  188. 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
  189. 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
  190. 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
  191. 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
  192. 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
  193. 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
  194. 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
  195. 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
  196. 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
  197. 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
  198. 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
  199. 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
  200. 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16,
  201. };
  202. static unsigned char inv_sbox(unsigned char v)
  203. {
  204. unsigned int i;
  205. for (i = 0; i < 256; i++)
  206. if (sbox[i] == v)
  207. return (unsigned char)i;
  208. return 0;
  209. }
  210. static unsigned char xt(unsigned char x)
  211. {
  212. return (unsigned char)((x << 1) ^ ((x & 0x80) ? 0x1b : 0));
  213. }
  214. static unsigned char gmul(unsigned char a, unsigned char b)
  215. {
  216. unsigned char r = 0;
  217. while (b) {
  218. if (b & 1)
  219. r ^= a;
  220. a = xt(a);
  221. b >>= 1;
  222. }
  223. return r;
  224. }
  225. void aes128_setkey(unsigned int rk_words[44], const unsigned char key[16])
  226. {
  227. unsigned char *rk = (unsigned char *)rk_words;
  228. unsigned char rcon = 1;
  229. unsigned int i;
  230. cpy(rk, key, 16);
  231. for (i = 16; i < 176; i += 4) {
  232. unsigned char t[4];
  233. cpy(t, rk + i - 4, 4);
  234. if (i % 16 == 0) {
  235. unsigned char u = t[0];
  236. t[0] = sbox[t[1]] ^ rcon;
  237. t[1] = sbox[t[2]];
  238. t[2] = sbox[t[3]];
  239. t[3] = sbox[u];
  240. rcon = xt(rcon);
  241. }
  242. rk[i] = rk[i - 16] ^ t[0];
  243. rk[i + 1] = rk[i - 15] ^ t[1];
  244. rk[i + 2] = rk[i - 14] ^ t[2];
  245. rk[i + 3] = rk[i - 13] ^ t[3];
  246. }
  247. }
  248. static void add_round_key(unsigned char s[16], const unsigned char *rk)
  249. {
  250. unsigned int i;
  251. for (i = 0; i < 16; i++)
  252. s[i] ^= rk[i];
  253. }
  254. void aes128_encrypt(const unsigned int rk_words[44], const unsigned char in[16],
  255. unsigned char out[16])
  256. {
  257. const unsigned char *rk = (const unsigned char *)rk_words;
  258. unsigned char s[16], t[16];
  259. unsigned int r, i, c;
  260. cpy(s, in, 16);
  261. add_round_key(s, rk);
  262. for (r = 1; r <= 10; r++) {
  263. for (i = 0; i < 16; i++) /* sub + shift rows */
  264. t[i] = sbox[s[(i + 4 * (i % 4)) % 16]];
  265. if (r < 10) {
  266. for (c = 0; c < 4; c++) { /* mix columns */
  267. unsigned char *col = t + 4 * c;
  268. unsigned char a0 = col[0], a1 = col[1];
  269. unsigned char a2 = col[2], a3 = col[3];
  270. col[0] = xt(a0) ^ (xt(a1) ^ a1) ^ a2 ^ a3;
  271. col[1] = a0 ^ xt(a1) ^ (xt(a2) ^ a2) ^ a3;
  272. col[2] = a0 ^ a1 ^ xt(a2) ^ (xt(a3) ^ a3);
  273. col[3] = (xt(a0) ^ a0) ^ a1 ^ a2 ^ xt(a3);
  274. }
  275. }
  276. cpy(s, t, 16);
  277. add_round_key(s, rk + 16 * r);
  278. }
  279. cpy(out, s, 16);
  280. }
  281. void aes128_decrypt(const unsigned int rk_words[44], const unsigned char in[16],
  282. unsigned char out[16])
  283. {
  284. const unsigned char *rk = (const unsigned char *)rk_words;
  285. unsigned char s[16], t[16];
  286. unsigned int r, i, c;
  287. cpy(s, in, 16);
  288. add_round_key(s, rk + 160);
  289. for (r = 9;; r--) {
  290. for (i = 0; i < 16; i++) /* inv shift + inv sub */
  291. t[(i + 4 * (i % 4)) % 16] = inv_sbox(s[i]);
  292. add_round_key(t, rk + 16 * r);
  293. if (r == 0) {
  294. cpy(s, t, 16);
  295. break;
  296. }
  297. for (c = 0; c < 4; c++) { /* inv mix columns */
  298. unsigned char *col = t + 4 * c;
  299. unsigned char a0 = col[0], a1 = col[1];
  300. unsigned char a2 = col[2], a3 = col[3];
  301. col[0] = gmul(a0, 14) ^ gmul(a1, 11) ^ gmul(a2, 13) ^ gmul(a3, 9);
  302. col[1] = gmul(a0, 9) ^ gmul(a1, 14) ^ gmul(a2, 11) ^ gmul(a3, 13);
  303. col[2] = gmul(a0, 13) ^ gmul(a1, 9) ^ gmul(a2, 14) ^ gmul(a3, 11);
  304. col[3] = gmul(a0, 11) ^ gmul(a1, 13) ^ gmul(a2, 9) ^ gmul(a3, 14);
  305. }
  306. cpy(s, t, 16);
  307. }
  308. cpy(out, s, 16);
  309. }
  310. int aes_unwrap(const unsigned char kek[16], const unsigned char *in,
  311. unsigned int n, unsigned char *out)
  312. {
  313. unsigned int rk[44], blocks, i, j, k;
  314. unsigned char a[8], b[16];
  315. if (n < 24 || n % 8)
  316. return -1;
  317. blocks = n / 8 - 1;
  318. aes128_setkey(rk, kek);
  319. cpy(a, in, 8);
  320. cpy(out, in + 8, n - 8);
  321. for (j = 6; j-- > 0;) {
  322. for (i = blocks; i >= 1; i--) {
  323. unsigned int t = blocks * j + i;
  324. cpy(b, a, 8);
  325. b[7] ^= t;
  326. b[6] ^= t >> 8;
  327. b[5] ^= t >> 16;
  328. b[4] ^= t >> 24;
  329. cpy(b + 8, out + 8 * (i - 1), 8);
  330. aes128_decrypt(rk, b, b);
  331. cpy(a, b, 8);
  332. cpy(out + 8 * (i - 1), b + 8, 8);
  333. }
  334. }
  335. for (k = 0; k < 8; k++)
  336. if (a[k] != 0xa6)
  337. return -1;
  338. return 0;
  339. }