コード例 #1
0
void JNICALL Java_beecrypt_provider_RSAKeyPairGenerator_generate(JNIEnv* env, jobject obj)
{
	jclass cls = (*env)->GetObjectClass(env, obj);
	jfieldID sid = (*env)->GetFieldID(env, cls, "_size", "I");
	jfieldID fid;
	if (sid)
	{
		randomGeneratorContext rngc;
		rsakp pair;
		jint keybits = (*env)->GetIntField(env, obj, sid);

		randomGeneratorContextInit(&rngc, randomGeneratorDefault());

		rsakpInit(&pair);

		fid = (*env)->GetFieldID(env, cls, "_e", "[B");
		if (fid)
		{
			mpnsetbigint(&pair.e, env, (*env)->GetObjectField(env, obj, fid));
		}

		rsakpMake(&pair, &rngc, (size_t) keybits);

		if ((fid = (*env)->GetFieldID(env, cls, "_n", "[B")))
			(*env)->SetObjectField(env, obj, fid, mp_to_bigint(env, pair.n.size, pair.n.modl));

		if ((fid = (*env)->GetFieldID(env, cls, "_e", "[B")))
			(*env)->SetObjectField(env, obj, fid, mp_to_bigint(env, pair.e.size, pair.e.data));

		if ((fid = (*env)->GetFieldID(env, cls, "_d", "[B")))
			(*env)->SetObjectField(env, obj, fid, mp_to_bigint(env, pair.d.size, pair.d.data));

		if ((fid = (*env)->GetFieldID(env, cls, "_p", "[B")))
			(*env)->SetObjectField(env, obj, fid, mp_to_bigint(env, pair.p.size, pair.p.modl));

		if ((fid = (*env)->GetFieldID(env, cls, "_q", "[B")))
			(*env)->SetObjectField(env, obj, fid, mp_to_bigint(env, pair.q.size, pair.q.modl));

		if ((fid = (*env)->GetFieldID(env, cls, "_dp", "[B")))
			(*env)->SetObjectField(env, obj, fid, mp_to_bigint(env, pair.dp.size, pair.dp.data));

		if ((fid = (*env)->GetFieldID(env, cls, "_dq", "[B")))
			(*env)->SetObjectField(env, obj, fid, mp_to_bigint(env, pair.dq.size, pair.dq.data));

		if ((fid = (*env)->GetFieldID(env, cls, "_qi", "[B")))
			(*env)->SetObjectField(env, obj, fid, mp_to_bigint(env, pair.qi.size, pair.qi.data));

		rsakpFree(&pair);
		randomGeneratorContextFree(&rngc);
	}
}
コード例 #2
0
ファイル: genkeys.c プロジェクト: LucidOne/onyxfirmware
int main() {
  FILE *rng;
  unsigned long rnum;
  int i, j;
  randomGeneratorContext rngc;
  byte keyTest[32];
  char keyHex[80];
  rsakp keypair;
  unsigned char mac[6];
  byte digest[20];
  char digestHex[41];
  sha1Param sha1param;

  randomGeneratorContextInit(&rngc, randomGeneratorDefault());
  rsakpInit(&keypair);

  fscanf( stdin, "%02X:%02X:%02X:%02X:%02X:%02X", &(mac[0]),&(mac[1]),&(mac[2]),&(mac[3]),&(mac[4]),&(mac[5]) );
  sha1Reset( &sha1param );
  sha1Update( &sha1param, mac, 6 );  // put the MAC into the SHA-1
  rngc.rng->next(rngc.param, (byte*) keyTest, 16);
  sha1Update( &sha1param, keyTest, 16 ); // then add some random numbers for good measure
  sha1Digest( &sha1param, digest );
  toHex(digest, digestHex, 16); // and there you have our putative ID
  // just in case we are a bit too deterministic, we push around
  // the RNG some amount that is linked to the MAC...not really a permanent
  // solution, but it should help avoid collisions.
  for( i = 0; i < digest[0]; i++ ) {
    rngc.rng->next(rngc.param, (byte*) keyTest, 16);
  }
  for( i = 0; i < digest[12]; i++ ) {
    rngc.rng->next(rngc.param, (byte*) keyTest, 16);
  }
  for( i = 0; i < digest[7]; i++ ) {
    rngc.rng->next(rngc.param, (byte*) keyTest, 16);
  }
  for( i = 0; i < digest[17]; i++ ) {
    rngc.rng->next(rngc.param, (byte*) keyTest, 16);
  }

  fprintf(stdout, "# Indeed, these are crypto keys. The overall system is designed to tolerate people finding these keys. Please note that modifying or copying these keys may cause us to be unable to recover your account information.\n" );
  for( i = 0; i < MAX_KEY_INDEX; i++ ) {
    rngc.rng->next(rngc.param, (byte*) keyTest, 16);
    toHex(keyTest, keyHex, 16);
    fprintf(stdout, "AES:%d:", i );
    fprintf(stdout, "%s\n", keyHex );
    fflush(stdout);
  }
  fprintf( stderr, "Generating 2048-bit RSA key pair..." );
  rsakpMake(&keypair, &rngc, 2048);
  fprintf( stderr, "Done.\n" );
  fflush( stderr );

  for( j = 0; j < MAX_KEY_INDEX; j++ ) {
    // create the putative index
    //rngc.rng->next(rngc.param, (byte*) keyTest, 16);
    //toHex(keyTest, keyHex, 16);
    fprintf(stdout, "PKI_I:%d:", j );
    //    fprintf(stdout, "%s", keyHex );
    fprintf(stdout, "%s", digestHex );
    // save the public key
    fprintf(stdout, "\nPKI_N:%d:", j );
    for( i = 0; i < keypair.n.size; i++ )
      fprintf(stdout, "%08X", keypair.n.modl[i] );
    fprintf(stdout, "\nPKI_E:%d:", j );
    for( i = 0; i < keypair.e.size; i++ ) 
      fprintf(stdout, "%08X", keypair.e.data[i]);
    // now save the private key components used in the CRT
    fprintf(stdout, "\nPKI_P:%d:", j );
    for( i = 0; i < keypair.p.size; i++ )
      fprintf(stdout, "%08X", keypair.p.modl[i] );
    fprintf(stdout, "\nPKI_Q:%d:", j );
    for( i = 0; i < keypair.q.size; i++ )
      fprintf(stdout, "%08X", keypair.q.modl[i] );
    fprintf(stdout, "\nPKI_DP:%d:", j );
    for( i = 0; i < keypair.dp.size; i++ ) 
      fprintf(stdout, "%08X", keypair.dp.data[i]);
    fprintf(stdout, "\nPKI_DQ:%d:", j );
    for( i = 0; i < keypair.dq.size; i++ ) 
      fprintf(stdout, "%08X", keypair.dq.data[i]);
    fprintf(stdout, "\nPKI_QI:%d:", j );
    for( i = 0; i < keypair.qi.size; i++ ) 
      fprintf(stdout, "%08X", keypair.qi.data[i]);
    fprintf(stdout, "\n" );
  }
  
  return 0;
}
コード例 #3
0
int encode_function_beecrpt(V6_AUTHENTICATION_REQUEST_T in, V6_AUTHENTICATION_RESULT_T *out)
{
	char rsa_m[256+1];
	char temp[256+1];
	char buff[1024];
	char	confirm[1024];
	unsigned char un_buff[128+1];
	int len=0, file_folder_offset=0;
	int reserved_offset=0, i;

	mpnumber m, cipher;
	rsakp keypair;
	randomGeneratorContext rngc;

	if (randomGeneratorContextInit(&rngc, randomGeneratorDefault()) == 0) {
		rsakpInit(&keypair);
		rsakpMake(&keypair, &rngc, 1024);

		//Set publicKey1
		memset(temp, 0, 256+1);
		getHexFromMPW(keypair.n.size, keypair.n.modl, temp);
		printLog(HEAD_KEY, "(**)UUID(%.48s)PRODUCT_ID(%.18s) ==> PUBLIC_KEY1(%s)\n", in.uuid, in.product, temp);
		memset(buff, 0, 1024);
		Hex2Str__((unsigned char*)temp, 256, (unsigned char*)buff);
		memcpy(out->decKey1, buff, 128);

		//Set publicKey2
		memcpy(out->decKey2, "10001", 5);

		//Logging privateKey
		memset(temp, 0, 256+1);
		getHexFromMPW(keypair.d.size, keypair.d.data, temp);
		printLog(HEAD_KEY, "(**)UUID(%.48s)PRODUCT_ID(%.18s) ==> PRIVATE_KEY(%s)\n", in.uuid, in.product, temp);

		memset(un_buff, 0, 128+1); 	len = 0;
		memcpy(&un_buff[len], in.uuid, UUID_SIZE-2); 						len += UUID_SIZE-2;
		memcpy(&un_buff[len], in.device_type, DEVICE_TYPE_SIZE); 			len += DEVICE_TYPE_SIZE;
		memcpy(&un_buff[len], in.register_version, REGISTER_VERSION_SIZE); 	len += REGISTER_VERSION_SIZE;
		memcpy(&un_buff[len], in.app_version, APP_VERSION_SIZE); 			len += APP_VERSION_SIZE;
		memcpy(&un_buff[len], in.app_build_num, APP_BUILD_NUM_SIZE); 		len += APP_BUILD_NUM_SIZE;
		memcpy(&un_buff[len], in.map_version, MAP_VER_SIZE); 				len += MAP_VER_SIZE;
		memcpy(&un_buff[len], in.os_version, OS_VER_SIZE_M31); 				len += OS_VER_SIZE_M31;
		//in order not to alter m31-device's decoding function released at interface 3 protocol(0x15)
		file_folder_offset = len; 											len += FILE_SIZE;   
		memcpy(&un_buff[len], in.expire_date, EXPIRE_DATE_SIZE); 			len += EXPIRE_DATE_SIZE;
		reserved_offset = len;

		for(i=0; i<in.reqEncCnt; i++) {
			//Set changing part(file size, reserved)
			memcpy(&un_buff[file_folder_offset], in.AuthFileInfo[i].file_size, FILE_SIZE);
			memcpy(&un_buff[reserved_offset], in.AuthFileInfo[i].reserved, RESERVED_SIZE);

			memset(rsa_m, 0, 256+1);
			Str2Hex__(un_buff, 128, (unsigned char *)rsa_m);

			mpnzero(&m);
			mpnzero(&cipher);
			mpnsethex(&m, rsa_m);

			if(	rsapri(&keypair.n, &keypair.d, &m, &cipher) != 0 )
				return -1;

			//Set encryptStr
			memset(temp, 0, 256+1);
			getHexFromMPW(cipher.size, cipher.data, temp);
			printLog(HEAD_KEY, "(%.2d)UUID(%.48s)PRODUCT_ID(%.18s) ==> ENCRYPT_STR(%s)\n",i, in.uuid, in.product, temp);
			memset(buff, 0, 1024);
			Hex2Str__((unsigned char*)temp, 256, (unsigned char*)buff);
			memcpy(out->AuthResultData[i].encStr, buff, 128);

			//	검증용..
			decode_function_m31(out->decKey1, out->decKey2, out->AuthResultData[i].encStr, confirm);
			if(memcmp(un_buff, confirm, 128) != 0)	{
				printLog(HEAD, "ERR::ENCODINGINPUT(%s)OUTPUT(%s) not same\n", un_buff, confirm);
				mpnfree(&cipher);
				mpnfree(&m);
				rsakpFree(&keypair);
				randomGeneratorContextFree(&rngc);
				out->resultCnt = 0;
				return	-1;
			}

			//	free mpn objects
			mpnfree(&cipher);
			mpnfree(&m);
		}
		out->resultCnt = i;

		//	free rsakp object & random generator context
		rsakpFree(&keypair);
		randomGeneratorContextFree(&rngc);
		return 1;
	}
	return -1;
}
コード例 #4
0
ファイル: testrsa.c プロジェクト: sutajiokousagi/keygen
int main()
{
    int failures = 0;

    rsakp keypair;
    mpnumber m, cipher, decipher;
    randomGeneratorContext rngc;

    if (randomGeneratorContextInit(&rngc, randomGeneratorDefault()) == 0)
    {
        /* First we do the fixed value verification */
        rsakpInit(&keypair);

        mpbsethex(&keypair.n, rsa_n);
        mpnsethex(&keypair.e, rsa_e);
        mpbsethex(&keypair.p, rsa_p);
        mpbsethex(&keypair.q, rsa_q);
        mpnsethex(&keypair.dp, rsa_d1);
        mpnsethex(&keypair.dq, rsa_d2);
        mpnsethex(&keypair.qi, rsa_c);

        mpnzero(&m);
        mpnzero(&cipher);
        mpnzero(&decipher);

        mpnsethex(&m, rsa_m);

        /* it's safe to cast the keypair to a public key */
        if (rsapub(&keypair.n, &keypair.e, &m, &cipher))
            failures++;

        if (rsapricrt(&keypair.n, &keypair.p, &keypair.q, &keypair.dp, &keypair.dq, &keypair.qi, &cipher, &decipher))
            failures++;

        if (mpnex(m.size, m.data, decipher.size, decipher.data))
            failures++;

        mpnfree(&decipher);
        mpnfree(&cipher);
        mpnfree(&m);

        rsakpFree(&keypair);

        mpnzero(&m);
        mpnzero(&cipher);
        mpnzero(&decipher);

        /* Now we generate a keypair and do some tests on it */
        rsakpMake(&keypair, &rngc, 512);

        /* generate a random m in the range 0 < m < n */
        mpbnrnd(&keypair.n, &rngc, &m);

        /* it's safe to cast the keypair to a public key */
        if (rsapub(&keypair.n, &keypair.e, &m, &cipher))
            failures++;

        if (rsapricrt(&keypair.n, &keypair.p, &keypair.q, &keypair.dp, &keypair.dq, &keypair.qi, &cipher, &decipher))
            failures++;

        if (mpnex(m.size, m.data, decipher.size, decipher.data))
            failures++;

        rsakpFree(&keypair);
    }
    return failures;
}