Exemplo n.º 1
0
Arquivo: dh.c Projeto: kjanz1899/ren-c
void DH_compute_key(DH_CTX *dh_ctx)
{
    BI_CTX *bi_ctx = bi_initialize();
    int len = dh_ctx->len;
    bigint *p = bi_import(bi_ctx, dh_ctx->p, len); //p modulus
    bigint *x = bi_import(bi_ctx, dh_ctx->x, len); //private key
    bigint *gy = bi_import(bi_ctx, dh_ctx->gy, len);  //public key(peer)
    bigint *k;                                      //negotiated(session) key

    bi_permanent(x);
    bi_permanent(gy);

    //calculate session key k = gy^x mod p
    bi_set_mod(bi_ctx, p,  BIGINT_M_OFFSET);
    bi_ctx->mod_offset = BIGINT_M_OFFSET;
    k = bi_mod_power(bi_ctx, gy, x);
    bi_permanent(k);

    bi_export(bi_ctx, k, dh_ctx->k, len);

    bi_depermanent(x);
    bi_depermanent(gy);
    bi_depermanent(k);
    bi_free(bi_ctx, x);
    bi_free(bi_ctx, gy);
    bi_free(bi_ctx, k);

    bi_free_mod(bi_ctx, BIGINT_M_OFFSET);
    bi_terminate(bi_ctx);
}
Exemplo n.º 2
0
Arquivo: dh.c Projeto: kjanz1899/ren-c
void DH_generate_key(DH_CTX *dh_ctx)
{
    BI_CTX *bi_ctx = bi_initialize();
    int len = dh_ctx->len;
    bigint *p = bi_import(bi_ctx, dh_ctx->p, len); //p modulus
    bigint *g = bi_import(bi_ctx, dh_ctx->g, dh_ctx->glen); //generator
    bigint *x, *gx;

    bi_permanent(g);

    //generate private key  X
    get_random_NZ(len, dh_ctx->x);
    x = bi_import(bi_ctx, dh_ctx->x, len);
    bi_permanent(x);

    //calculate public key gx = g^x mod p
    bi_set_mod(bi_ctx, p,  BIGINT_M_OFFSET);
    bi_ctx->mod_offset = BIGINT_M_OFFSET;
    gx = bi_mod_power(bi_ctx, g, x);
    bi_permanent(gx);

    bi_export(bi_ctx, x, dh_ctx->x, len);
    bi_export(bi_ctx, gx, dh_ctx->gx, len);

    bi_depermanent(g);
    bi_depermanent(x);
    bi_depermanent(gx);
    bi_free(bi_ctx, g);
    bi_free(bi_ctx, x);
    bi_free(bi_ctx, gx);

    bi_free_mod(bi_ctx, BIGINT_M_OFFSET);
    bi_terminate(bi_ctx);
}
Exemplo n.º 3
0
/**
 * Free up any RSA context resources.
 */
void RSA_free(RSA_CTX *rsa_ctx) {
  BI_CTX *bi_ctx;
  if (rsa_ctx == NULL) /* deal with ptrs that are null */
    return;

  bi_ctx = rsa_ctx->bi_ctx;

  bi_depermanent(rsa_ctx->e);
  bi_free(bi_ctx, rsa_ctx->e);
  bi_free_mod(rsa_ctx->bi_ctx, BIGINT_M_OFFSET);

  if (rsa_ctx->d) {
    bi_depermanent(rsa_ctx->d);
    bi_free(bi_ctx, rsa_ctx->d);
    bi_depermanent(rsa_ctx->dP);
    bi_depermanent(rsa_ctx->dQ);
    bi_depermanent(rsa_ctx->qInv);
    bi_free(bi_ctx, rsa_ctx->dP);
    bi_free(bi_ctx, rsa_ctx->dQ);
    bi_free(bi_ctx, rsa_ctx->qInv);
    bi_free_mod(rsa_ctx->bi_ctx, BIGINT_P_OFFSET);
    bi_free_mod(rsa_ctx->bi_ctx, BIGINT_Q_OFFSET);
  }

  bi_terminate(bi_ctx);
  free(rsa_ctx);
}
Exemplo n.º 4
0
/**
 * Free up any RSA context resources.
 */
void ICACHE_FLASH_ATTR RSA_free(RSA_CTX *rsa_ctx)
{
    BI_CTX *bi_ctx;
    if (rsa_ctx == NULL)                /* deal with ptrs that are null */
        return;

    bi_ctx = rsa_ctx->bi_ctx;

    bi_depermanent(rsa_ctx->e);
    bi_free(bi_ctx, rsa_ctx->e);
    bi_free_mod(rsa_ctx->bi_ctx, BIGINT_M_OFFSET);

    if (rsa_ctx->d)
    {
        bi_depermanent(rsa_ctx->d);
        bi_free(bi_ctx, rsa_ctx->d);
#ifdef CONFIG_BIGINT_CRT
        bi_depermanent(rsa_ctx->dP);
        bi_depermanent(rsa_ctx->dQ);
        bi_depermanent(rsa_ctx->qInv);
        bi_free(bi_ctx, rsa_ctx->dP);
        bi_free(bi_ctx, rsa_ctx->dQ);
        bi_free(bi_ctx, rsa_ctx->qInv);
        bi_free_mod(rsa_ctx->bi_ctx, BIGINT_P_OFFSET);
        bi_free_mod(rsa_ctx->bi_ctx, BIGINT_Q_OFFSET);
#endif
    }

    bi_terminate(bi_ctx);
    os_free(rsa_ctx);
}
Exemplo n.º 5
0
int main() {
	bi_initialize();
	int k = 2;
	bigint f1 = int_to_bi(1), f2 = int_to_bi(1), f3, tmp;
	bigint zeros = int_to_bi(1000000000);
	while (1) {
		k++;
		f3 = bi_add(f1, bi_copy(f2));
		int mod = bi_int_mod(bi_copy(f3), 1000000000);
		if (mod >= 100000000) {
			if (is_pandigital(mod)) {
				tmp = bi_copy(f3);
				while (bi_compare(bi_copy(tmp), bi_copy(zeros)) > 0) {
					tmp = bi_int_divide(tmp, 10);
				}
				if (is_pandigital(bi_to_int(tmp))) {
					break;
				}
			}
		}
		f1 = f2;
		f2 = f3;
	}
	bi_free(f2);
	bi_free(f3);
	bi_free(zeros);
	printf("%d\n", k);
	bi_terminate();
	return 0;
}
Exemplo n.º 6
0
int main() {
	bi_initialize();
	bigint sum = int_to_bi(1);
	bigint last = int_to_bi(1);
	int i, j;
	for (i = 1; i <= (SIZE-1)/2; ++i) {
		for (j = 0; j < 4; ++j) {
			last = bi_add(last, bi_int_multiply(int_to_bi(i), 2));
			sum = bi_add(sum, bi_copy(last));
		}
	}
	bi_print(stdout, sum);
	printf("\n");
	bi_free(last);
	bi_terminate();
	return 0;
}
Exemplo n.º 7
0
/**************************************************************************
 * main()
 *
 **************************************************************************/
int main(int argc, char *argv[])
{
	int ret=1;
	BI_CTX *bi_ctx;
#ifdef SELF_MALLOC
	mem_init(8000);
#endif
	mem_show();
	ret = RSA_test();
	mem_show();
	bi_ctx = bi_initialize();
	mem_show();
    ret = AES_test(bi_ctx);
    mem_show();
    bi_terminate(bi_ctx);
    mem_show();
    return ret;
}
Exemplo n.º 8
0
int main() {
	bi_initialize();
	int i, j;
	for (i=1000;i<=10000;++i) {
		if (i==1487) continue;
		if (!is_prime(i)) continue;
		for (j=(10000-i)/2; j>0;--j) {
			if (is_prime(i+j) && is_prime(i+2*j)) {
				if (is_permutation(i,i+j) && is_permutation(i,i+2*j)) {
					printf("%d%d%d\n",i,i+j,i+2*j);
					i = 10000;
					break;
				}
			}
		}
	}
	bi_terminate();
	return 0;
}
Exemplo n.º 9
0
int main() {
	bi_initialize();
	int a, b, n;
	int max_number, prod;
	for (a = -999; a < 1000; ++a) {
		for (b = -999; b < 1000; ++b) {
			n = 0;
			for (;;) {
				bigint a_ = int_to_bi(a);
				bigint b_ = int_to_bi(b);
				bigint n_ = int_to_bi(n);
				bigint n2 = bi_multiply( bi_copy( n_ ), bi_copy( n_ ) );
				bigint an = bi_multiply(bi_copy(a_),bi_copy(n_));
				bigint num = bi_add(bi_copy(n2), bi_copy(an));
				bigint num2 = bi_add(bi_copy(num), bi_copy(b_));
				int should_break = 0;
				if (bi_is_probable_prime(bi_copy(num2), 99)) {
					++n;
				} else {
					if (n > max_number) {
						max_number = n;
						prod = a * b;
					}
					should_break = 1;
				}
				bi_free(num); 
				bi_free(num2); 
				bi_free(a_); 
				bi_free(b_); 
				bi_free(n_); 
				bi_free(n2);
				bi_free(an);
				if (should_break) break;
			}
		}
	}
	printf("%d\n", prod);
	bi_terminate();
	return 0;
}
Exemplo n.º 10
0
/**
 * @brief Perform a modular exponentiation using a temporary modulus.
 *
 * We need this function to check the signatures of certificates. The modulus
 * of this function is temporary as it's just used for authentication.
 * @param ctx [in]  The bigint session context.
 * @param bi  [in]  The bigint to perform the exp/mod.
 * @param bim [in]  The temporary modulus.
 * @param biexp [in] The bigint exponent.
 * @return The result of the mod exponentiation operation
 * @see bi_set_mod().
 */
bigint *ICACHE_FLASH_ATTR bi_mod_power2(BI_CTX *ctx, bigint *bi, bigint *bim, bigint *biexp) {
	bigint *biR, *tmp_biR;

	/* Set up a temporary bigint context and transfer what we need between
	 * them. We need to do this since we want to keep the original modulus
	 * which is already in this context. This operation is only called when
	 * doing peer verification, and so is not expensive :-) */
	BI_CTX *tmp_ctx = bi_initialize();
	bi_set_mod(tmp_ctx, bi_clone(tmp_ctx, bim), BIGINT_M_OFFSET);
	tmp_biR = bi_mod_power(tmp_ctx,
						   bi_clone(tmp_ctx, bi),
						   bi_clone(tmp_ctx, biexp));
	biR = bi_clone(ctx, tmp_biR);
	bi_free(tmp_ctx, tmp_biR);
	bi_free_mod(tmp_ctx, BIGINT_M_OFFSET);
	bi_terminate(tmp_ctx);

	bi_free(ctx, bi);
	bi_free(ctx, bim);
	bi_free(ctx, biexp);
	return biR;
}
Exemplo n.º 11
0
int main() {
	bi_initialize();
	bigint last = int_to_bi(1);
	int i, j;
	int number_of_primes = 0;
	for (i = 1;; ++i) {
		for (j = 0; j < 4; ++j) {
			last = bi_add(last, bi_int_multiply(int_to_bi(i), 2));
			if (j != 3 && bi_is_probable_prime(bi_copy(last), 4)) {
				++number_of_primes;
			}
		}
		if ((double)number_of_primes / (4*i+1) < 0.1) {
			printf("%d\n", i*2+1);
			break;
		}
#ifdef DEBUG
		printf("%d/%d on iteration %d ~%f\n", number_of_primes, (4*i+1), i, (float)number_of_primes/(4*i+1));
#endif
	}
	bi_free(last);
	bi_terminate();
	return 0;
}
Exemplo n.º 12
0
int main() {
	int found = 0;
	int i, j, k, n;
	int possible;
	int total = 0;
	bi_initialize();
	for (i=11;found<11;i+=2) {
#ifdef DEBUG
		if (i % 1000 == 1) {
			printf("Looping on %d\n", i);
		}
#endif
		int digits = 1+(int)floor(log10((double)i));
		possible = 1;
		for (j=0;j<digits;++j) {
			n = (i / (int)pow(10, j)) % 10;
			if (j == digits-1) {
				if (n != 2 && n != 3 && n != 5 && n != 7) {
					possible = 0; break;
				}
			} else if (j == 0) {
				if (n != 3 && n != 7) {
					possible = 0; break;
				}
			} else {
				if (n != 1 && n != 2 && n != 3 && n != 5 && n != 7 && n != 9) {
					possible = 0; break;
				}
			}
		}
		if (possible == 0) continue;
		if (!is_prime(i)) continue;

		for (j=1;j<digits;++j) {
			n = 0;
			for (k=0;k<j;++k) {
				n += ((i / (int)pow(10, k)) % 10) * pow(10, k);
			}
			if (!is_prime(n)) {
				possible = 0;
				break;
			}
		}
		if (possible == 0) continue;

		for (j=1;j<digits;++j) {
			n = 0;
			for (k=0;k<j;++k) {
				n += ((i / (int)pow(10, digits-k-1)) % 10) * (int)pow(10,j-k-1);
			}
			if (!is_prime(n)) {
				possible = 0;
				break;
			}
		}

		if (possible == 0) continue;

#ifdef DEBUG
		printf("%d\n", i);
#endif
		total += i;
		++found;
	}
	printf("%d\n", total);
	bi_terminate();
	return 0;
}
Exemplo n.º 13
0
int main() {
	bi_initialize();
	bigint sum = int_to_bi(0);
	int a,b,c,d,e,f,g,h,i,j;
	int n;
	for (a=9;a>0;--a) {
	for (b=9;b>=0;--b) {
		if (b==a) continue;
	for (c=9;c>=0;--c) {
		if (c==a||c==b) continue;
	for (d=9;d>=0;--d) {
		if (d==a||d==b||d==c) continue;
	for (e=9;e>=0;--e) {
		if (e==a||e==b||e==c||e==d) continue;
	for (f=9;f>=0;--f) {
		if (f==a||f==b||f==c||f==d||f==e) continue;
	for (g=9;g>=0;--g) {
		if (g==a||g==b||g==c||g==d||g==e||g==f) continue;
	for (h=9;h>=0;--h) {
		if (h==a||h==b||h==c||h==d||h==e||h==f||h==g) continue;
	for (i=9;i>=0;--i) {
		if (i==a||i==b||i==c||i==d||i==e||i==f||i==g||i==h) continue;
	for (j=9;j>=0;--j) {
		if (j==a||j==b||j==c||j==d||j==e||j==f||j==g||j==h||j==i) continue;
		if ((b * 100 + c * 10 + d) % 2 == 0)
		if ((c * 100 + d * 10 + e) % 3 == 0)
		if ((d * 100 + e * 10 + f) % 5 == 0)
		if ((e * 100 + f * 10 + g) % 7 == 0)
		if ((f * 100 + g * 10 + h) % 11 == 0)
		if ((g * 100 + h * 10 + i) % 13 == 0)
		if ((h * 100 + i * 10 + j) % 17 == 0) {
			n = 
			    b * 100000000 +
			    c * 10000000 +
			    d * 1000000 +
			    e * 100000 +
			    f * 10000 +
			    g * 1000 +
			    h * 100 +
			    i * 10 +
			    j * 1;
			sum = bi_add(sum, int_to_bi(n));
			sum = bi_add(sum, bi_int_multiply(int_to_bi(1000000000), a));
#ifdef DEBUG
printf("%d%d%d%d%d%d%d%d%d%d\n", a,b,c,d,e,f,g,h,i,j);
bi_print(stdout,bi_copy(sum));
printf("\n");
#endif
		}
	}
	}
	}
	}
	}
	}
	}
	}
	}
	}
	bi_print(stdout,sum);
	printf("\n");
	bi_terminate();
	return 0;
}