comparison src/c/openid.c @ 12:c778455fe570

Diffie-Hellman seems to be working
author Adam Chlipala <adam@chlipala.net>
date Sat, 01 Jan 2011 14:00:52 -0500
parents e637249abfd2
children de04a3fc6b72
comparison
equal deleted inserted replaced
11:e637249abfd2 12:c778455fe570
311 BIO_free_all(bmem); 311 BIO_free_all(bmem);
312 312
313 return n; 313 return n;
314 } 314 }
315 315
316 uw_Basis_string uw_OpenidFfi_sha1(uw_context ctx, uw_Basis_string key, uw_Basis_string data) { 316 uw_Basis_string uw_OpenidFfi_hmac_sha1(uw_context ctx, uw_Basis_string key, uw_Basis_string data) {
317 unsigned char keyBin[SHA_DIGEST_LENGTH], out[EVP_MAX_MD_SIZE]; 317 unsigned char keyBin[SHA_DIGEST_LENGTH], out[EVP_MAX_MD_SIZE];
318 unsigned outLen; 318 unsigned outLen;
319 319
320 unbase64((unsigned char *)key, strlen(key), keyBin, sizeof keyBin); 320 unbase64((unsigned char *)key, strlen(key), keyBin, sizeof keyBin);
321 321
322 HMAC(EVP_sha1(), keyBin, sizeof keyBin, (unsigned char *)data, strlen(data), out, &outLen); 322 HMAC(EVP_sha1(), keyBin, sizeof keyBin, (unsigned char *)data, strlen(data), out, &outLen);
323 return base64(ctx, out, outLen); 323 return base64(ctx, out, outLen);
324 } 324 }
325 325
326 uw_Basis_string uw_OpenidFfi_sha256(uw_context ctx, uw_Basis_string key, uw_Basis_string data) { 326 uw_Basis_string uw_OpenidFfi_hmac_sha256(uw_context ctx, uw_Basis_string key, uw_Basis_string data) {
327 unsigned char keyBin[SHA256_DIGEST_LENGTH], out[EVP_MAX_MD_SIZE]; 327 unsigned char keyBin[SHA256_DIGEST_LENGTH], out[EVP_MAX_MD_SIZE];
328 unsigned outLen; 328 unsigned outLen;
329 329
330 unbase64((unsigned char *)key, strlen(key), keyBin, sizeof keyBin); 330 unbase64((unsigned char *)key, strlen(key), keyBin, sizeof keyBin);
331 331
391 return dh; 391 return dh;
392 } 392 }
393 393
394 uw_Basis_string uw_OpenidFfi_compute(uw_context ctx, uw_OpenidFfi_dh dh, uw_Basis_string server_pub) { 394 uw_Basis_string uw_OpenidFfi_compute(uw_context ctx, uw_OpenidFfi_dh dh, uw_Basis_string server_pub) {
395 BIGNUM *bn = unbtwoc(ctx, server_pub); 395 BIGNUM *bn = unbtwoc(ctx, server_pub);
396 unsigned char secret[DH_size(dh)]; 396 unsigned char secret[DH_size(dh)+1], *secretP;
397 int size; 397 int size;
398 398
399 uw_push_cleanup(ctx, (void (*)(void *))BN_free, bn); 399 uw_push_cleanup(ctx, (void (*)(void *))BN_free, bn);
400 400
401 size = DH_compute_key(secret, bn, dh); 401 size = DH_compute_key(secret+1, bn, dh);
402 if (size == -1) 402 if (size == -1)
403 uw_error(ctx, FATAL, "Diffie-Hellman key computation failed"); 403 uw_error(ctx, FATAL, "Diffie-Hellman key computation failed");
404 404
405 uw_pop_cleanup(ctx); 405 uw_pop_cleanup(ctx);
406 406
407 return base64(ctx, secret, size); 407 if (size > 0 && (secret[1] & 0x80)) {
408 } 408 secret[0] = 0;
409 secretP = secret;
410 ++size;
411 } else
412 secretP = secret+1;
413
414 return base64(ctx, secretP, size);
415 }
416
417 uw_Basis_string uw_OpenidFfi_sha1(uw_context ctx, uw_Basis_string data) {
418 unsigned char dataBin[128], out[EVP_MAX_MD_SIZE];
419 int len;
420
421 len = unbase64((unsigned char *)data, strlen(data), dataBin, sizeof dataBin);
422
423 SHA1(dataBin, len, out);
424 return base64(ctx, out, SHA_DIGEST_LENGTH);
425 }
426
427 uw_Basis_string uw_OpenidFfi_sha256(uw_context ctx, uw_Basis_string data) {
428 unsigned char dataBin[128], out[EVP_MAX_MD_SIZE];
429 int len;
430
431 len = unbase64((unsigned char *)data, strlen(data), dataBin, sizeof dataBin);
432
433 SHA256(dataBin, len, out);
434 return base64(ctx, out, SHA256_DIGEST_LENGTH);
435 }
436
437 uw_Basis_string uw_OpenidFfi_xor(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
438 unsigned char buf1[128], buf2[128], bufO[128];
439 int len1, len2, i;
440
441 len1 = unbase64((unsigned char *)s1, strlen(s1), buf1, sizeof buf1);
442 len2 = unbase64((unsigned char *)s2, strlen(s2), buf2, sizeof buf2);
443
444 for (i = 0; i < len1; ++i)
445 bufO[i] = buf1[i] ^ buf2[i % len2];
446
447 return base64(ctx, bufO, len1);
448 }