39 #define __FILE_ID__ "ydef" 43 #include "Yocto/yocto.h" 44 #include "Yocto/yapi_ext.h" 45 #define ntohl(dw) swapl(dw) 54 return (b>9u) ? b+
'a'-10 : b+
'0';
60 return (b>9u) ? b+
'a'-10 : b+
'0';
63 void bin2str(
char *to,
const u8 *p, u16 len, u8 addnull)
69 if(addnull) *to =
'\0';
72 #if !defined(MICROCHIP_API) || defined(HTTP_ON_NET) 77 void ComputeAuthHA1(u8 *ha1,
const char *user,
const char *pass,
const char *realm)
88 #ifdef DEBUG_HTTP_AUTHENTICATION 92 dbglog(
"Compute HA1 u=%s r=%s p=%s -> %s\n", user, realm, pass, tmpha);
107 #ifdef DEBUG_HTTP_AUTHENTICATION 111 dbglog(
"Compute HA2 m=%s u=%s -> %s\n", method, uri, tmpha);
118 void ComputeAuthResponse(
char *buf,
const u8 *ha1,
const char *nonce,
const char *nc,
const char *cnonce,
const u8* ha2)
141 #ifdef DEBUG_HTTP_AUTHENTICATION 146 dbglog(
"Auth Resp ha1=%s nonce=%s nc=%s cnouce=%s ha2=%s -> %s\n",
147 tmpha1, nonce, nc, cnonce, tmpha, buf);
149 dbglog(
"Auth Resp ha1=%s nonce=%s (no nc/cnounce) ha2=%s -> %s\n",
150 tmpha1, nonce, tmpha, buf);
158 int CheckWSAuth(u32 nonce,
const u8 *ha1,
const u8 *to_verify, u8 *out)
166 #ifdef DEBUG_HTTP_AUTHENTICATION 167 dbglog(
"ha1=%s\n", tmpbuff);
170 #ifdef CPU_BIG_ENDIAN 174 #ifdef DEBUG_HTTP_AUTHENTICATION 175 dbglog(
"full=%s\n", tmpbuff);
177 sha1 =
ySHA1(tmpbuff);
179 memcpy(out, sha1, 20);
181 if (to_verify == NULL) {
184 res = memcmp(sha1, to_verify, 20)==0;
192 int yParseWWWAuthenticate(
char *replybuf,
int replysize,
char **method,
char **realm,
char **qop,
char **nonce,
char **opaque)
195 char *p=replybuf, *
start;
197 while(pos < replysize) {
198 while(pos < replysize && replybuf[pos] !=
'\r') pos++;
199 if(pos < replysize && replybuf[++pos] ==
'\n') pos++;
201 if(pos+25 >= replysize)
return -1;
202 if(
YSTRNICMP(replybuf+pos,
"WWW-Authenticate:", 17) != 0)
continue;
206 while(pos < replysize && replybuf[pos] !=
'\r') pos++;
209 if(pos >= replysize)
return -1;
212 while(*p ==
' ') p++;
214 while(*p && *p !=
' ') p++;
219 *realm = replybuf+pos;
224 while(*p ==
' ' || *p ==
',') p++;
229 while(*p && *p !=
'\"') p++;
233 }
else if(
YSTRNICMP(p,
"qop=\"",5) == 0) {
236 while(*p && *p !=
'\"') p++;
240 }
else if(
YSTRNICMP(p,
"nonce=\"",7) == 0) {
243 while(*p && *p !=
'\"') p++;
247 }
else if(
YSTRNICMP(p,
"opaque=\"",8) == 0) {
250 while(*p && *p !=
'\"') p++;
256 while(*p && *p !=
',') p++;
260 if(!**realm)
return -1;
270 const char *nonce,
const char *opaque, u32 *nc,
const char *method,
const char *uri)
273 char ncbuf[9], cnoncebuf[9];
278 YSTRCPY(buf, bufsize,
"Authorization: Digest username=\"");
280 YSTRCAT(buf, bufsize,
"\", realm=\"");
282 YSTRCAT(buf, bufsize,
"\", nonce=\"");
284 YSTRCAT(buf, bufsize,
"\", uri=\"");
289 yxtoa(*nc, ncbuf,
sizeof(ncbuf)-1);
290 yxtoa(cnonce, cnoncebuf,
sizeof(cnoncebuf)-1);
291 len = (int)strlen(buf);
294 YSTRCAT(buf, bufsize,
"\", qop=auth, nc=");
296 YSTRCAT(buf, bufsize,
", cnonce=\"");
297 YSTRCAT(buf, bufsize, cnoncebuf);
299 YSTRCAT(buf, bufsize,
"\", response=\"");
300 len = (int)strlen(buf);
305 len = (int)strlen(buf);
308 YSTRCAT(buf, bufsize,
"\", opaque=\"");
311 YSTRCAT(buf, bufsize,
"\"\r\n");
327 const u32
sha1_init[5] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
329 static void initshaw(
const char *s, u16 ofs, u8 pad, u16 xinit)
331 int ii, j = -1, k = 0;
332 int n = (int)strlen(s);
334 for(ii = 0; ii < 64; ii++) {
343 if (i == n + 3) c = pad;
344 else if (i == n + 4) c = 0x80;
353 wpak.
shaw[j] |= ((u32)c << k);
358 wpak.
shaw[15] = 8 * n;
361 wpak.
shaw[15] = 8 * (n + 68);
365 u32 xdw = ((u32)xinit << 16) | xinit;
366 for (j = 0; j < 16; j++) {
374 u32 a, b, c,
d, e, t;
382 for (k = 16; k < 80; k++) {
383 t = wpak.
shaw[k - 3] ^ wpak.
shaw[k - 8] ^ wpak.
shaw[k - 14] ^ wpak.
shaw[k - 16];
384 wpak.
shaw[k] = (t << 1) | (t >> 31);
386 for (k = 0; k < 20; k++) {
387 t = ((a << 5) | (a >> 27)) + e + wpak.
shaw[k] + 0x5A827999 + ((b & c) | ((~b) & d));
390 c = (b << 30) | (b >> 2);
394 for (k = 20; k < 40; k++) {
395 t = ((a << 5) | (a >> 27)) + e + wpak.
shaw[k] + 0x6ED9EBA1 + (b^c^d);
398 c = (b << 30) | (b >> 2);
402 for (k = 40; k < 60; k++) {
403 t = ((a << 5) | (a >> 27)) + e + wpak.
shaw[k] + 0x8F1BBCDC + ((b & c) | (b & d) | (c & d));
406 c = (b << 30) | (b >> 2);
410 for (k = 60; k < 80; k++) {
411 t = ((a << 5) | (a >> 27)) + e + wpak.
shaw[k] + 0xCA62C1D6 + (b^c^d);
414 c = (b << 30) | (b >> 2);
418 wpak.
shaw[0] = s[0] + a;
419 wpak.
shaw[1] = s[1] + b;
420 wpak.
shaw[2] = s[2] + c;
421 wpak.
shaw[3] = s[3] + d;
422 wpak.
shaw[4] = s[4] + e;
427 int ofs = 0, n = (int)strlen(text);
433 memcpy((u8 *)wpak.
shau, (u8 *)wpak.
shaw,
sizeof(wpak.
shau));
436 #ifndef CPU_BIG_ENDIAN 437 for(ofs = 0; ofs < 5; ofs++) {
438 wpak.
shau[ofs] = ntohl(wpak.
shau[ofs]);
442 return (u8 *)wpak.
shau;
459 memset(wpak.
shau, 0,
sizeof(wpak.
shau));
467 if(wpak.
iter < 0)
return -1;
468 if(wpak.
iter >= 8192)
return 0;
470 wpak.
shaw[5] = 0x80000000;
471 for (k = 6; k < 15; k++) {
474 wpak.
shaw[15] = 8 * (64 + 20);
483 if((wpak.
iter & 4095) == 0) {
484 for(k = 0; k < 5 && wpak.
pos < 32; k++) {
485 wpak.
res[wpak.
pos++] = (wpak.
shau[k] >> 24) & 0xff;
486 wpak.
res[wpak.
pos++] = (wpak.
shau[k] >> 16) & 0xff;
487 wpak.
res[wpak.
pos++] = (wpak.
shau[k] >> 8) & 0xff;
490 if(wpak.
iter == 4096) {
491 memset(wpak.
shau, 0,
sizeof(wpak.
shau));
495 memcpy(res, wpak.
res, 32);
505 #ifndef MICROCHIP_API 516 #ifndef CPU_BIG_ENDIAN 517 #define byteReverse(buf, len) // Do nothing 519 static void byteReverse(
unsigned char *buf,
unsigned longs) {
522 t = (u32) ((
unsigned) buf[3] << 8 | buf[2]) << 16 |
523 ((
unsigned) buf[1] << 8 | buf[0]);
530 #define F1(x, y, z) (z ^ (x & (y ^ z))) 531 #define F2(x, y, z) F1(z, x, y) 532 #define F3(x, y, z) (x ^ y ^ z) 533 #define F4(x, y, z) (y ^ (x | ~z)) 535 #define MD5STEP(f, w, x, y, z, data, s) \ 536 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 542 ctx->
buf[0] = 0x67452301;
543 ctx->
buf[1] = 0xefcdab89;
544 ctx->
buf[2] = 0x98badcfe;
545 ctx->
buf[3] = 0x10325476;
553 register u32 a, b, c,
d;
560 MD5STEP(
F1, a, b, c, d, in[0] + 0xd76aa478, 7);
561 MD5STEP(
F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
562 MD5STEP(
F1, c, d, a, b, in[2] + 0x242070db, 17);
563 MD5STEP(
F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
564 MD5STEP(
F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
565 MD5STEP(
F1, d, a, b, c, in[5] + 0x4787c62a, 12);
566 MD5STEP(
F1, c, d, a, b, in[6] + 0xa8304613, 17);
567 MD5STEP(
F1, b, c, d, a, in[7] + 0xfd469501, 22);
568 MD5STEP(
F1, a, b, c, d, in[8] + 0x698098d8, 7);
569 MD5STEP(
F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
570 MD5STEP(
F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
571 MD5STEP(
F1, b, c, d, a, in[11] + 0x895cd7be, 22);
572 MD5STEP(
F1, a, b, c, d, in[12] + 0x6b901122, 7);
573 MD5STEP(
F1, d, a, b, c, in[13] + 0xfd987193, 12);
574 MD5STEP(
F1, c, d, a, b, in[14] + 0xa679438e, 17);
575 MD5STEP(
F1, b, c, d, a, in[15] + 0x49b40821, 22);
577 MD5STEP(
F2, a, b, c, d, in[1] + 0xf61e2562, 5);
578 MD5STEP(
F2, d, a, b, c, in[6] + 0xc040b340, 9);
579 MD5STEP(
F2, c, d, a, b, in[11] + 0x265e5a51, 14);
580 MD5STEP(
F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
581 MD5STEP(
F2, a, b, c, d, in[5] + 0xd62f105d, 5);
582 MD5STEP(
F2, d, a, b, c, in[10] + 0x02441453, 9);
583 MD5STEP(
F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
584 MD5STEP(
F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
585 MD5STEP(
F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
586 MD5STEP(
F2, d, a, b, c, in[14] + 0xc33707d6, 9);
587 MD5STEP(
F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
588 MD5STEP(
F2, b, c, d, a, in[8] + 0x455a14ed, 20);
589 MD5STEP(
F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
590 MD5STEP(
F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
591 MD5STEP(
F2, c, d, a, b, in[7] + 0x676f02d9, 14);
592 MD5STEP(
F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
594 MD5STEP(
F3, a, b, c, d, in[5] + 0xfffa3942, 4);
595 MD5STEP(
F3, d, a, b, c, in[8] + 0x8771f681, 11);
596 MD5STEP(
F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
597 MD5STEP(
F3, b, c, d, a, in[14] + 0xfde5380c, 23);
598 MD5STEP(
F3, a, b, c, d, in[1] + 0xa4beea44, 4);
599 MD5STEP(
F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
600 MD5STEP(
F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
601 MD5STEP(
F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
602 MD5STEP(
F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
603 MD5STEP(
F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
604 MD5STEP(
F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
605 MD5STEP(
F3, b, c, d, a, in[6] + 0x04881d05, 23);
606 MD5STEP(
F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
607 MD5STEP(
F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
608 MD5STEP(
F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
609 MD5STEP(
F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
611 MD5STEP(
F4, a, b, c, d, in[0] + 0xf4292244, 6);
612 MD5STEP(
F4, d, a, b, c, in[7] + 0x432aff97, 10);
613 MD5STEP(
F4, c, d, a, b, in[14] + 0xab9423a7, 15);
614 MD5STEP(
F4, b, c, d, a, in[5] + 0xfc93a039, 21);
615 MD5STEP(
F4, a, b, c, d, in[12] + 0x655b59c3, 6);
616 MD5STEP(
F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
617 MD5STEP(
F4, c, d, a, b, in[10] + 0xffeff47d, 15);
618 MD5STEP(
F4, b, c, d, a, in[1] + 0x85845dd1, 21);
619 MD5STEP(
F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
620 MD5STEP(
F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
621 MD5STEP(
F4, c, d, a, b, in[6] + 0xa3014314, 15);
622 MD5STEP(
F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
623 MD5STEP(
F4, a, b, c, d, in[4] + 0xf7537e82, 6);
624 MD5STEP(
F4, d, a, b, c, in[11] + 0xbd3af235, 10);
625 MD5STEP(
F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
626 MD5STEP(
F4, b, c, d, a, in[9] + 0xeb86d391, 21);
639 if ((ctx->
bits[0] = t + ((u32) len << 3)) < t)
641 ctx->
bits[1] += len >> 29;
646 unsigned char *p = (
unsigned char *) ctx->
in + t;
661 memcpy(ctx->
in, buf, 64);
668 memcpy(ctx->
in, buf, len);
676 count = (ctx->
bits[0] >> 3) & 0x3F;
680 count = 64 - 1 - count;
685 memset(ctx->
in, 0, 56);
687 memset(p, 0, count - 8);
696 memcpy(digest, ctx->
buf, 16);
697 memset((
char *) ctx, 0,
sizeof(*ctx));
static WPA_CALC_STATE wpak
void ComputeAuthHA2(u8 *ha2, const char *method, const char *uri)
#define YSTRCAT(dst, dstsize, src)
u8 * ySHA1(const char *text)
static char btohexa_low_high(u8 b)
int yIterPsk(u8 *res, const char *ssid)
#define YSTRNICMP(A, B, len)
static void MD5Transform(u32 buf[4], u32 const in[16])
void bin2str(char *to, const u8 *p, u16 len, u8 addnull)
void MD5Calculate(HASH_SUM *ctx, u8 digest[16])
static void initshaw(const char *s, u16 ofs, u8 pad, u16 xinit)
#define YSTRCPY(dst, dstsize, src)
#define HTTP_AUTH_MD5_STRLEN
#define MD5STEP(f, w, x, y, z, data, s)
void yDigestAuthorization(char *buf, int bufsize, const char *user, const char *realm, const u8 *ha1, const char *nonce, const char *opaque, u32 *nc, const char *method, const char *uri)
static char btohexa_low_low(u8 b)
int CheckWSAuth(u32 nonce, const u8 *ha1, const u8 *to_verify, u8 *out)
void ComputeAuthHA1(u8 *ha1, const char *user, const char *pass, const char *realm)
void MD5Initialize(HASH_SUM *ctx)
void yxtoa(u32 x, char *buf, u16 len)
#define byteReverse(buf, len)
int yParseWWWAuthenticate(char *replybuf, int replysize, char **method, char **realm, char **qop, char **nonce, char **opaque)
static void itershaw(const u32 *s)
void yInitPsk(const char *pass, const char *ssid)
void ComputeAuthResponse(char *buf, const u8 *ha1, const char *nonce, const char *nc, const char *cnonce, const u8 *ha2)
u32 yapiGetCNonce(u32 nc)
void MD5AddData(HASH_SUM *ctx, const u8 *buf, u32 len)
#define HTTP_AUTH_MD5_SIZE