Пример #1
0
int main() {
	int t;
	unsigned long long f;
	generatePrime();
	scanf("%d", &t);
	while (t--) {
		scanf("%llu", &f);
		
		if(f<5)
			printf("%d",2);
		
		while(1) {
			if(f%2==0)
				f--;
			else
				f-=2;
		
			if(preProcess(f)) {
				printf("%llu\n", f);
				break;		
			}
			
		}
		
	}
	return 0;
} 
Пример #2
0
/*
*   It is a function that generates the keys for RSA algorithm
*   input: mpz_t e, mpz_t, d, mpz_t n and int bitUsed;
*   output: mpz_t e, mpz_t d, mpz_t n
*/
void findRSAKeys(mpz_t e, mpz_t d, mpz_t n, int bitUsed){
    mpz_t p;
    mpz_init(p);
    mpz_t q;
    mpz_init(q);
    //mpz_t n;
    //mpz_init(n);
    mpz_t p1;
    mpz_init(p1);
    mpz_t q1;
    mpz_init(q1);
    mpz_t eulerN;
    mpz_init(eulerN);
    //steps according to wikipedia
    //step 1
    generatePrime(p, bitUsed);
    while(mpz_cmp_ui(q, 0) == 0 || mpz_cmp(p, q) == 0){
        generatePrime(q, bitUsed);
    }
    //step 2
    mpz_mul(n,p,q);
    //step 3
    mpz_sub_ui(p1, p, 1);
    mpz_sub_ui(q1, q, 1);
    mpz_mul(eulerN, p1, q1);
    //step 4
    //mpz_t e;
    //mpz_init(e);
    findESimple(e, eulerN);
    //gmp_printf("e is %Zd\n", e);
    //step 5
    mpz_t g, s;
    mpz_init(g);
    mpz_init(s);
    mpz_gcdext(g, s, d, eulerN, e);
}
Пример #3
0
static int generate_pwd_shares(sc_card_t *card, char **pwd, int *pwdlen, int password_shares_threshold, int password_shares_total)
{
	int r, i;
	BIGNUM prime;
	BIGNUM secret;
	char buf[64];
	char hex[64];
	int l;

	secret_share_t *shares = NULL;
	secret_share_t *sp;

	u8 rngseed[16];

	printf(	"\nThe DKEK will be enciphered using a randomly generated 64 bit password.\n");
	printf(	"This password is split using a (%i-of-%i) threshold scheme.\n\n", password_shares_threshold, password_shares_total);

	printf(	"Please keep the generated and encrypted DKEK file in a safe location. We also recommend \n");
	printf(	"to keep a paper printout, in case the electronic version becomes unavailable. A printable version\n");
	printf(	"of the file can be generated using \"openssl base64 -in <filename>\".\n");

	printf("\n\nPress <enter> to continue");

	waitForEnterKeyPressed();

	*pwd = calloc(1, 8);
	*pwdlen = 8;

	r = sc_get_challenge(card, *pwd, 8);
	if (r < 0) {
		printf("Error generating random key failed with ", sc_strerror(r));
		OPENSSL_cleanse(pwd, *pwdlen);
		free(pwd);
		return r;
	}
	**pwd |= 0x80;

	/*
	 * Initialize prime and secret
	 */
	BN_init(&prime);
	BN_init(&secret);

	/*
	 * Encode the secret value
	 */
	BN_bin2bn(*pwd, *pwdlen, &secret);

	/*
	 * Generate seed and calculate a prime depending on the size of the secret
	 */
	r = sc_get_challenge(card, rngseed, 16);
	if (r < 0) {
		printf("Error generating random seed failed with ", sc_strerror(r));
		OPENSSL_cleanse(pwd, *pwdlen);
		free(pwd);
		return r;
	}

	generatePrime(&prime, &secret, password_shares_total, rngseed);

	// Allocate data buffer for the generated shares
	shares = malloc(password_shares_total * sizeof(secret_share_t));

	createShares(&secret, password_shares_threshold, password_shares_total, prime, shares);

	sp = shares;
	for (i = 0; i < password_shares_total; i++) {
		clearScreen();

		printf("Press <enter> to display key share %i of %i\n\n", i + 1, password_shares_total);
		waitForEnterKeyPressed();

		clearScreen();

		printf("Share %i of %i\n\n", i + 1, password_shares_total);

		l = BN_bn2bin(&prime, buf);
		sc_bin_to_hex(buf, l, hex, 64, ':');
		printf("\nPrime       : %s\n", hex);

		printf("Share ID    : %s\n", BN_bn2dec(&(sp->x)));
		l = BN_bn2bin(&(sp->y), buf);
		sc_bin_to_hex(buf, l, hex, 64, ':');
		printf("Share value : %s\n", hex);

		printf("\n\nPlease note ALL values above and press <enter> when finished");
		waitForEnterKeyPressed();

		sp++;
	}

	clearScreen();

	cleanUpShares(shares, password_shares_total);

	BN_clear_free(&prime);
	BN_clear_free(&secret);

	return 0;
}
Пример #4
0
int main(){

	generatePrime();					// generating primes from 1 to 30000

	srand((unsigned int)time(NULL));	// For rand() function to generate unique random number

	pid_t childPID[250], mainPID;		// pids of all childs and parent
	int partochild[500];				// File descriptors of parent to child pipes
	int childtopar[500];				// File descriptors of child to parent pipes
	int indexTopipe;					// Index of the corresponding pipe of the child
	int i,j;
	int err;							// Error number for error checking
	char buff[MAX_SIZE];				// Buff used for reading and writing to the pipe

	// Reading input
	printf("Enter the value of k(Number of child processes) and n = Number of Primes to be found\n");

	while(1){
		scanf("%d %d",&k,&n);

		if(k > 250 || k <=0 || n > 500 || n <=0){
			printf("Invalid input(out of scope) try again!\n");

		}
		else	break;
	}
	
	// n = 2*k;

	mainPID = getpid();				// Storing pid of the parent
	
	// Creating k childs
	for (i = 0; i < k; ++i)
	{
		if(pipe(&partochild[2*i]) == -1)				// creating pipes from parent to child
		{
			perror("Error in creating pipe!");
			exit(0);
		}

		if(pipe2(&childtopar[2*i], O_NONBLOCK) == -1)	// Creating pipes from child to parent in Non Blocking mode
		{
			perror("Error in creating pipe!");
			exit(0);
		}

		childPID[i] = fork();							// Creating child

		if(childPID[i] < 0){							// Error
			perror("Error in forking children!");
			exit(0);
		}
		else if(!childPID[i]){							// Child process
			indexTopipe = 2*i;							// Assigning pipe index in child
			break;
		}	
	}

	if(getpid() == mainPID){							// Parent Process
		while(1){
			for (i = 0; i < k; ++i)
			{
				if(read(childtopar[2*i], buff, MAX_SIZE) == -1){		// Skipping the read if pipe empty
					continue;
				}

				int status = atoi(buff);						// Reading from child

				if(status == AVAILABLE){						// Available
					for (j = 0; j < k; ++j)
					{
						sprintf(buff,"%d",rand()%30001);		// Generating random numbers to sent to child

						if(write(partochild[2*i+1], buff, MAX_SIZE) == -1){
							perror("Error in writing in pipes!");
							exit(0);
						}
					}
				}
				else if(status == BUSY){					// Busy
					//Skip this child process
				}
				else{										
					insertintoprimes(status);				// Checking if prime exists from first

					if(primeCount >= n){					// N primes found so killing all child processes and printing n primes
						for (j = 0; j < k; ++j)
						{
							if(kill(childPID[j],SIGKILL) == -1){
								perror("");
							}
						}

						printf("\nRespective %d prime numbers are as follows:\n",n);

						for (j = 0; j < n; ++j)
						{
							printf("%d. %d\n",j+1,FoundPrimes[j]);
						}
						printf("\n");
						exit(0);
					}

				}
			}
		}
	}
	else{											// Child Process
		int numbers[100];							// Input from parent
		close(partochild[indexTopipe+1]);			// Closing write end of parent to child in child process as not needed
		close(childtopar[indexTopipe]);				// Closing read end of child to parent in child process as not needed

		while(1){
			if(write(childtopar[indexTopipe+1],"30001",MAX_SIZE) == -1){	// Sending Available signal
				perror("Error in writing in pipes!");
				exit(0);
			}

			for (i = 0; i < k; ++i)											// reading k numbers from parent
			{
				if(read(partochild[indexTopipe], buff, MAX_SIZE) == -1){	
					perror("Error in reading pipes!");
					exit(0);
				}
				numbers[i] = atoi(buff);
			}

			if(write(childtopar[indexTopipe+1],"30002",MAX_SIZE) == -1){	// Sending Busy Signal
				perror("Error in writing in pipes!");
				exit(0);
			}

			for (i = 0; i < k; ++i)											// Checking for the primality of the k numbers and sending them to parent one by one
			{
				if(isprime[numbers[i]]){
				    //printf("Prime = %d child = %d\n",numbers[i],getpid());
					sprintf(buff,"%d",numbers[i]);
					if(write(childtopar[indexTopipe+1], buff, MAX_SIZE) == -1){
						perror("Error in writing in pipes!");
						exit(0);
					}
				}
			}
		}
	}

	return 0;
}
Пример #5
0
static int generate_pwd_shares(sc_card_t *card, char **pwd, int *pwdlen, int password_shares_threshold, int password_shares_total)
{
	int r, i;
	BIGNUM *prime;
	BIGNUM *secret;
	unsigned char buf[64];
	char hex[64];
	int l;

	secret_share_t *shares = NULL;
	secret_share_t *sp;

	u8 rngseed[16];

	if ((password_shares_threshold == -1) || (password_shares_total == -1)) {
		fprintf(stderr, "Must specify both, --pwd-shares-total and --pwd-shares-threshold\n");
		return -1;
	}

	if (password_shares_total < 3) {
		fprintf(stderr, "--pwd-shares-total must be 3 or larger\n");
		return -1;
	}

	if (password_shares_threshold < 2) {
		fprintf(stderr, "--pwd-shares-threshold must 2 or larger\n");
		return -1;
	}

	if (password_shares_threshold > password_shares_total) {
		fprintf(stderr, "--pwd-shares-threshold must be smaller or equal to --pwd-shares-total\n");
		return -1;
	}

	printf(	"\nThe DKEK will be enciphered using a randomly generated 64 bit password.\n");
	printf(	"This password is split using a (%i-of-%i) threshold scheme.\n\n", password_shares_threshold, password_shares_total);

	printf(	"Please keep the generated and encrypted DKEK file in a safe location. We also recommend \n");
	printf(	"to keep a paper printout, in case the electronic version becomes unavailable. A printable version\n");
	printf(	"of the file can be generated using \"openssl base64 -in <filename>\".\n");

	printf("\n\nPress <enter> to continue");

	waitForEnterKeyPressed();

	*pwd = calloc(1, 8);
	*pwdlen = 8;

	r = sc_get_challenge(card, (unsigned char *)*pwd, 8);
	if (r < 0) {
		printf("Error generating random key failed with %s", sc_strerror(r));
		OPENSSL_cleanse(*pwd, *pwdlen);
		free(*pwd);
		return r;
	}
	**pwd &= 0x7F; // Make sure the bit size of the secret is not bigger than 63 bits

	/*
	 * Initialize prime and secret
	 */
	prime = BN_new();
	secret = BN_new();

	/*
	 * Encode the secret value
	 */
	BN_bin2bn((unsigned char *)*pwd, *pwdlen, secret);

	/*
	 * Generate seed and calculate a prime depending on the size of the secret
	 */
	r = sc_get_challenge(card, rngseed, SEED_LENGTH);
	if (r < 0) {
		printf("Error generating random seed failed with %s", sc_strerror(r));
		OPENSSL_cleanse(*pwd, *pwdlen);
		free(*pwd);
		return r;
	}

	r = generatePrime(prime, secret, 64, rngseed, SEED_LENGTH);
	if (r < 0) {
		printf("Error generating valid prime number. Please try again.");
		OPENSSL_cleanse(*pwd, *pwdlen);
		free(*pwd);
		return r;
	}

	// Allocate data buffer for the generated shares
	shares = malloc(password_shares_total * sizeof(secret_share_t));

	createShares(secret, password_shares_threshold, password_shares_total, prime, shares);

	sp = shares;
	for (i = 0; i < password_shares_total; i++) {
		clearScreen();

		printf("Press <enter> to display key share %i of %i\n\n", i + 1, password_shares_total);
		waitForEnterKeyPressed();

		clearScreen();

		printf("Share %i of %i\n\n", i + 1, password_shares_total);

		l = BN_bn2bin(prime, buf);
		sc_bin_to_hex(buf, l, hex, 64, ':');
		printf("\nPrime       : %s\n", hex);

		printf("Share ID    : %s\n", BN_bn2dec((sp->x)));
		l = BN_bn2bin((sp->y), buf);
		sc_bin_to_hex(buf, l, hex, 64, ':');
		printf("Share value : %s\n", hex);

		printf("\n\nPlease note ALL values above and press <enter> when finished");
		waitForEnterKeyPressed();

		sp++;
	}

	clearScreen();

	cleanUpShares(shares, password_shares_total);

	BN_clear_free(prime);
	BN_clear_free(secret);

	return 0;
}