| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- /* WPA2-PSK crypto: see wpa_crypto.h. Small and byte-oriented on purpose;
- * the hot path (PBKDF2, 8192 SHA-1 blocks) still takes well under a
- * second on the Braswell. */
- #include "wpa_crypto.h"
- static void cpy(unsigned char *d, const unsigned char *s, unsigned int n)
- {
- while (n--)
- *d++ = *s++;
- }
- /* ---- SHA-1 -------------------------------------------------------------- */
- struct sha1_ctx {
- unsigned int h[5];
- unsigned char buf[64];
- unsigned int used;
- unsigned long long total;
- };
- static unsigned int rol(unsigned int x, int n)
- {
- return (x << n) | (x >> (32 - n));
- }
- static void sha1_block(unsigned int h[5], const unsigned char *p)
- {
- unsigned int w[80], a, b, c, d, e, t, i;
- for (i = 0; i < 16; i++)
- w[i] = ((unsigned int)p[4 * i] << 24) | (p[4 * i + 1] << 16) |
- (p[4 * i + 2] << 8) | p[4 * i + 3];
- for (; i < 80; i++)
- w[i] = rol(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
- a = h[0]; b = h[1]; c = h[2]; d = h[3]; e = h[4];
- for (i = 0; i < 80; i++) {
- unsigned int f, k;
- if (i < 20) {
- f = (b & c) | (~b & d);
- k = 0x5a827999u;
- } else if (i < 40) {
- f = b ^ c ^ d;
- k = 0x6ed9eba1u;
- } else if (i < 60) {
- f = (b & c) | (b & d) | (c & d);
- k = 0x8f1bbcdcu;
- } else {
- f = b ^ c ^ d;
- k = 0xca62c1d6u;
- }
- t = rol(a, 5) + f + e + k + w[i];
- e = d; d = c; c = rol(b, 30); b = a; a = t;
- }
- h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e;
- }
- static void sha1_init(struct sha1_ctx *c)
- {
- c->h[0] = 0x67452301u;
- c->h[1] = 0xefcdab89u;
- c->h[2] = 0x98badcfeu;
- c->h[3] = 0x10325476u;
- c->h[4] = 0xc3d2e1f0u;
- c->used = 0;
- c->total = 0;
- }
- static void sha1_update(struct sha1_ctx *c, const unsigned char *d,
- unsigned int n)
- {
- c->total += n;
- while (n) {
- unsigned int take = 64 - c->used;
- if (take > n)
- take = n;
- cpy(c->buf + c->used, d, take);
- c->used += take;
- d += take;
- n -= take;
- if (c->used == 64) {
- sha1_block(c->h, c->buf);
- c->used = 0;
- }
- }
- }
- static void sha1_final(struct sha1_ctx *c, unsigned char out[20])
- {
- unsigned long long bits = c->total * 8;
- unsigned char pad = 0x80, z = 0, len[8];
- unsigned int i;
- sha1_update(c, &pad, 1);
- while (c->used != 56)
- sha1_update(c, &z, 1);
- for (i = 0; i < 8; i++)
- len[i] = (unsigned char)(bits >> (56 - 8 * i));
- sha1_update(c, len, 8);
- for (i = 0; i < 5; i++) {
- out[4 * i] = c->h[i] >> 24;
- out[4 * i + 1] = c->h[i] >> 16;
- out[4 * i + 2] = c->h[i] >> 8;
- out[4 * i + 3] = c->h[i];
- }
- }
- void sha1(const unsigned char *d, unsigned int n, unsigned char out[20])
- {
- struct sha1_ctx c;
- sha1_init(&c);
- sha1_update(&c, d, n);
- sha1_final(&c, out);
- }
- void hmac_sha1_2(const unsigned char *key, unsigned int klen,
- const unsigned char *d1, unsigned int n1,
- const unsigned char *d2, unsigned int n2,
- unsigned char out[20])
- {
- unsigned char k[64], pad[64], inner[20];
- struct sha1_ctx c;
- unsigned int i;
- for (i = 0; i < 64; i++)
- k[i] = 0;
- if (klen > 64)
- sha1(key, klen, k);
- else
- cpy(k, key, klen);
- for (i = 0; i < 64; i++)
- pad[i] = k[i] ^ 0x36;
- sha1_init(&c);
- sha1_update(&c, pad, 64);
- sha1_update(&c, d1, n1);
- if (n2)
- sha1_update(&c, d2, n2);
- sha1_final(&c, inner);
- for (i = 0; i < 64; i++)
- pad[i] = k[i] ^ 0x5c;
- sha1_init(&c);
- sha1_update(&c, pad, 64);
- sha1_update(&c, inner, 20);
- sha1_final(&c, out);
- }
- void hmac_sha1(const unsigned char *key, unsigned int klen,
- const unsigned char *d, unsigned int n, unsigned char out[20])
- {
- hmac_sha1_2(key, klen, d, n, 0, 0, out);
- }
- void pbkdf2_sha1(const unsigned char *pass, unsigned int plen,
- const unsigned char *salt, unsigned int slen,
- unsigned int iterations, unsigned char *out, unsigned int olen)
- {
- unsigned int blk = 1;
- while (olen) {
- unsigned char cnt[4], u[20], t[20];
- unsigned int i, j, take = olen < 20 ? olen : 20;
- cnt[0] = blk >> 24; cnt[1] = blk >> 16; cnt[2] = blk >> 8;
- cnt[3] = blk;
- hmac_sha1_2(pass, plen, salt, slen, cnt, 4, u);
- cpy(t, u, 20);
- for (i = 1; i < iterations; i++) {
- hmac_sha1(pass, plen, u, 20, u);
- for (j = 0; j < 20; j++)
- t[j] ^= u[j];
- }
- cpy(out, t, take);
- out += take;
- olen -= take;
- blk++;
- }
- }
- void wpa_prf(const unsigned char *key, unsigned int klen, const char *label,
- const unsigned char *b, unsigned int blen,
- unsigned char *out, unsigned int olen)
- {
- unsigned char msg[128 + 100], h[20];
- unsigned int llen = 0, i, n;
- while (label[llen])
- llen++;
- if (llen + 1 + blen + 1 > sizeof(msg))
- return;
- cpy(msg, (const unsigned char *)label, llen);
- msg[llen] = 0;
- cpy(msg + llen + 1, b, blen);
- n = llen + 1 + blen;
- for (i = 0; olen; i++) {
- unsigned int take = olen < 20 ? olen : 20;
- msg[n] = (unsigned char)i;
- hmac_sha1(key, klen, msg, n + 1, h);
- cpy(out, h, take);
- out += take;
- olen -= take;
- }
- }
- /* ---- AES-128 ----------------------------------------------------------- */
- static const unsigned char sbox[256] = {
- 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
- 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
- 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
- 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
- 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
- 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
- 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
- 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
- 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
- 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
- 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
- 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
- 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
- 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
- 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
- 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16,
- };
- static unsigned char inv_sbox(unsigned char v)
- {
- unsigned int i;
- for (i = 0; i < 256; i++)
- if (sbox[i] == v)
- return (unsigned char)i;
- return 0;
- }
- static unsigned char xt(unsigned char x)
- {
- return (unsigned char)((x << 1) ^ ((x & 0x80) ? 0x1b : 0));
- }
- static unsigned char gmul(unsigned char a, unsigned char b)
- {
- unsigned char r = 0;
- while (b) {
- if (b & 1)
- r ^= a;
- a = xt(a);
- b >>= 1;
- }
- return r;
- }
- void aes128_setkey(unsigned int rk_words[44], const unsigned char key[16])
- {
- unsigned char *rk = (unsigned char *)rk_words;
- unsigned char rcon = 1;
- unsigned int i;
- cpy(rk, key, 16);
- for (i = 16; i < 176; i += 4) {
- unsigned char t[4];
- cpy(t, rk + i - 4, 4);
- if (i % 16 == 0) {
- unsigned char u = t[0];
- t[0] = sbox[t[1]] ^ rcon;
- t[1] = sbox[t[2]];
- t[2] = sbox[t[3]];
- t[3] = sbox[u];
- rcon = xt(rcon);
- }
- rk[i] = rk[i - 16] ^ t[0];
- rk[i + 1] = rk[i - 15] ^ t[1];
- rk[i + 2] = rk[i - 14] ^ t[2];
- rk[i + 3] = rk[i - 13] ^ t[3];
- }
- }
- static void add_round_key(unsigned char s[16], const unsigned char *rk)
- {
- unsigned int i;
- for (i = 0; i < 16; i++)
- s[i] ^= rk[i];
- }
- void aes128_encrypt(const unsigned int rk_words[44], const unsigned char in[16],
- unsigned char out[16])
- {
- const unsigned char *rk = (const unsigned char *)rk_words;
- unsigned char s[16], t[16];
- unsigned int r, i, c;
- cpy(s, in, 16);
- add_round_key(s, rk);
- for (r = 1; r <= 10; r++) {
- for (i = 0; i < 16; i++) /* sub + shift rows */
- t[i] = sbox[s[(i + 4 * (i % 4)) % 16]];
- if (r < 10) {
- for (c = 0; c < 4; c++) { /* mix columns */
- unsigned char *col = t + 4 * c;
- unsigned char a0 = col[0], a1 = col[1];
- unsigned char a2 = col[2], a3 = col[3];
- col[0] = xt(a0) ^ (xt(a1) ^ a1) ^ a2 ^ a3;
- col[1] = a0 ^ xt(a1) ^ (xt(a2) ^ a2) ^ a3;
- col[2] = a0 ^ a1 ^ xt(a2) ^ (xt(a3) ^ a3);
- col[3] = (xt(a0) ^ a0) ^ a1 ^ a2 ^ xt(a3);
- }
- }
- cpy(s, t, 16);
- add_round_key(s, rk + 16 * r);
- }
- cpy(out, s, 16);
- }
- void aes128_decrypt(const unsigned int rk_words[44], const unsigned char in[16],
- unsigned char out[16])
- {
- const unsigned char *rk = (const unsigned char *)rk_words;
- unsigned char s[16], t[16];
- unsigned int r, i, c;
- cpy(s, in, 16);
- add_round_key(s, rk + 160);
- for (r = 9;; r--) {
- for (i = 0; i < 16; i++) /* inv shift + inv sub */
- t[(i + 4 * (i % 4)) % 16] = inv_sbox(s[i]);
- add_round_key(t, rk + 16 * r);
- if (r == 0) {
- cpy(s, t, 16);
- break;
- }
- for (c = 0; c < 4; c++) { /* inv mix columns */
- unsigned char *col = t + 4 * c;
- unsigned char a0 = col[0], a1 = col[1];
- unsigned char a2 = col[2], a3 = col[3];
- col[0] = gmul(a0, 14) ^ gmul(a1, 11) ^ gmul(a2, 13) ^ gmul(a3, 9);
- col[1] = gmul(a0, 9) ^ gmul(a1, 14) ^ gmul(a2, 11) ^ gmul(a3, 13);
- col[2] = gmul(a0, 13) ^ gmul(a1, 9) ^ gmul(a2, 14) ^ gmul(a3, 11);
- col[3] = gmul(a0, 11) ^ gmul(a1, 13) ^ gmul(a2, 9) ^ gmul(a3, 14);
- }
- cpy(s, t, 16);
- }
- cpy(out, s, 16);
- }
- int aes_unwrap(const unsigned char kek[16], const unsigned char *in,
- unsigned int n, unsigned char *out)
- {
- unsigned int rk[44], blocks, i, j, k;
- unsigned char a[8], b[16];
- if (n < 24 || n % 8)
- return -1;
- blocks = n / 8 - 1;
- aes128_setkey(rk, kek);
- cpy(a, in, 8);
- cpy(out, in + 8, n - 8);
- for (j = 6; j-- > 0;) {
- for (i = blocks; i >= 1; i--) {
- unsigned int t = blocks * j + i;
- cpy(b, a, 8);
- b[7] ^= t;
- b[6] ^= t >> 8;
- b[5] ^= t >> 16;
- b[4] ^= t >> 24;
- cpy(b + 8, out + 8 * (i - 1), 8);
- aes128_decrypt(rk, b, b);
- cpy(a, b, 8);
- cpy(out + 8 * (i - 1), b + 8, 8);
- }
- }
- for (k = 0; k < 8; k++)
- if (a[k] != 0xa6)
- return -1;
- return 0;
- }
|