void CryptManager::GenerateRSAKey( unsigned int keyLength, RString &sPrivKey, RString &sPubKey ) { int iRet; rsa_key key; iRet = rsa_make_key( &g_pPRNG->m_PRNG, g_pPRNG->m_iPRNG, keyLength / 8, 65537, &key ); if( iRet != CRYPT_OK ) { LOG->Warn( "GenerateRSAKey(%i) error: %s", keyLength, error_to_string(iRet) ); return; } unsigned char buf[1024]; unsigned long iSize = sizeof(buf); iRet = rsa_export( buf, &iSize, PK_PUBLIC, &key ); if( iRet != CRYPT_OK ) { LOG->Warn( "Export error: %s", error_to_string(iRet) ); return; } sPubKey = RString( (const char *) buf, iSize ); iSize = sizeof(buf); iRet = rsa_export( buf, &iSize, PK_PRIVATE, &key ); if( iRet != CRYPT_OK ) { LOG->Warn( "Export error: %s", error_to_string(iRet) ); return; } sPrivKey = RString( (const char *) buf, iSize ); }
int main(int argc, char **argv) { int err; rsa_key key; init_libtom(); //prng_state yarrow_prng; err = rsa_make_key(NULL, find_prng ("sprng"), 1024/8, 65537, &key); //err = rsa_make_key(NULL, find_prng ("sprng"), 1024/8, 17, &key); if (err) { printf("make_key failed\n"); return err; } err = export_key_to_file(&key, "private.key", PK_PRIVATE); if (err) { printf("export key failed\n"); return err; } err = export_key_to_file(&key, "public.key", PK_PUBLIC); if (err) { printf("export key failed\n"); return err; } return 0; }
OK_RESULT ok_create_key(rsa_key **key) { *key = (rsa_key *) kmalloc(sizeof(rsa_key), GFP_KERNEL); if(OK_SUCCESS != rsa_make_key(OK_RSA_KEY_SIZE, E_VALUE, *key)) return OK_KEY_CREATE_ERROR; return OK_SUCCESS; }
int main(int argc, char** argv) { // Usage: rsa_make_key <private key file> <public key file> const char* private_key = ARGV(1); const char* public_key = ARGV(2); ltc_mp = ltm_desc; // Register PRNG algorithm. const int prng_idx = register_prng(&sprng_desc); if (prng_idx < 0) return error(CRYPT_INVALID_PRNG); // Generate key. rsa_key key; const int bitsize = 2048; int err = rsa_make_key(NULL, prng_idx, bitsize/8, 65537, &key); if (err != CRYPT_OK) return error(err); // Export private key. unsigned char out[bitsize * 5 / 8]; // guesstimate unsigned long outlen = sizeof(out); err = rsa_export(out, &outlen, PK_PRIVATE, &key); if (err != CRYPT_OK) return error(err, &key); // Save private key. FILE* f = fopen(private_key, "wb"); if (!f) return error(CRYPT_FILE_NOTFOUND, &key); outlen = (unsigned long)fwrite(out, 1, outlen, f); fclose(f); if (!outlen) return error(CRYPT_ERROR, &key); // Export public key. outlen = sizeof(out); err = rsa_export(out, &outlen, PK_PUBLIC, &key); if (err != CRYPT_OK) return error(err, &key); // Save public key. f = fopen(public_key, "wb"); if (!f) return error(CRYPT_FILE_NOTFOUND, &key); outlen = (unsigned long)fwrite(out, 1, outlen, f); fclose(f); if (!outlen) return error(CRYPT_ERROR, &key); rsa_free(&key); return 0; }
int setup_crypt(void) { int err; if(register_prng(&yarrow_desc) != CRYPT_OK) { printf("Could not register prng.\n"); return -1; } printf("prng registered...\n"); if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) { printf("Could not make prng: %s\n", error_to_string(err)); return -1; } /* generate a 1536 bit RSA key. This duplicates the exported key size of Mythic's algorithm, but other sizes would work as well */ if ((err = rsa_make_key(&prng, find_prng("yarrow"), 192, 65537, &key)) != CRYPT_OK) { printf("Could not generate RSA key: %s\n", error_to_string(err)); return -1; } printf("RSA key generated...\n"); /* export the key starting at keybuff[10] so we can prepend the fixed header the client expects */ exported_key_len = sizeof(exported_key_buffer); if ((err = rsa_export(&exported_key_buffer[10], &exported_key_len, PK_PUBLIC, &key)) != CRYPT_OK) { printf("Could not export RSA public key: %s\n", error_to_string(err)); return -1; } printf("RSA public key exported (%lu bytes)...\n", exported_key_len); /* some sort of protocol version information proceeds the key when we send it. If not correct, login.dll generates version mismatch error message. */ *((unsigned long *)&exported_key_buffer[0]) = htonl(LOGIN_PROTOCOL_VERSION); *((unsigned short *)&exported_key_buffer[4]) = htons(1); /* add the size */ *((unsigned short *)&exported_key_buffer[6]) = htons(exported_key_len); *((unsigned short *)&exported_key_buffer[8]) = htons(exported_key_len); return 0; }
static int rpmltcGenerateRSA(pgpDig dig) /*@*/ { rpmltc ltc = dig->impl; int rc = 0; /* assume failure. */ static long _e = 0x10001; /* XXX FIXME */ if (ltc->nbits == 0) ltc->nbits = 1024; /* XXX FIXME */ rc = rpmltcErr(ltc, "rsa_make_key", rsa_make_key(&yarrow_prng, find_prng("yarrow"), ltc->nbits/8, _e, <c->rsa)); rc = (rc == CRYPT_OK); #ifdef DYING rpmltcDumpRSA(__FUNCTION__, ltc); #endif SPEW(!rc, rc, dig); return rc; }
bool CryptHelpers::GenerateRSAKey( unsigned int keyLength, CString sSeed, CString &sPublicKey, CString &sPrivateKey ) { #ifdef _XBOX return false; #else int iRet; rsa_key key; iRet = rsa_make_key( &g_PRNGState, g_PRNGDescId, KEY_BITLENGTH/8, 65537, &key ); if ( iRet != CRYPT_OK ) { LOG->Warn( "GenerateRSAKey error: %s", error_to_string(iRet) ); return false; } unsigned char buf[2048]; unsigned long bufsize = sizeof(buf); iRet = rsa_export( buf, &bufsize, PK_PUBLIC, &key ); if ( iRet != CRYPT_OK ) { LOG->Warn( "RSA Public Key Export error: %s", error_to_string(iRet) ); return false; } sPublicKey = CString( (const char*)buf, bufsize ); iRet = rsa_export( buf, &bufsize, PK_PRIVATE, &key ); if ( iRet != CRYPT_OK ) { LOG->Warn( "RSA Private Key Export error: %s", error_to_string(iRet) ); return false; } PKCS8EncodePrivateKey( buf, bufsize, sPrivateKey ); return true; #endif }
void CRSADlg::OnBnClickedBtnGenrsakey() { // TODO: Add your control notification handler code here rsa_key key; unsigned char outRSA[4096]; unsigned long outlenRSA = 4096; padding = LTC_PKCS_1_V1_5; switch(g_AsyAlgList_Index) { case eASYMKEY_ALG_1024: rsa_byte_len = 1024 / 8; break; case eASYMKEY_ALG_2048: rsa_byte_len = 2048 / 8; break; case eASYMKEY_ALG_4096: rsa_byte_len = 4096 / 8; break; default: break; } /* make an RSA-1024 key */ if ((err = rsa_make_key(NULL, /* PRNG state */ prng_idx, /* PRNG idx */ rsa_byte_len, /*key byte length*/ 65537, /* we like e=65537 */ &key) /* where to store the key */ ) != CRYPT_OK) { printf("rsa_make_key %s", error_to_string(err)); } if ((err = rsa_export((unsigned char*)outRSA, &outlenRSA, PK_PRIVATE, &key)) != CRYPT_OK) { printf("Export error: %s", error_to_string(err)); outlenRSA = 0; } //unsigned char *tmp; //int len = 0; //len = mp_unsigned_bin_size((mp_int *)key.N); //tmp = (unsigned char *) malloc(len); //mp_to_unsigned_bin((mp_int *)key.N, tmp); // unsigned char *msg; //// N // msg = (unsigned char *) malloc(2*len+1); // *(msg+(2*len)) = '\0'; // StdCharsToHexChars(tmp, msg, len); // SetDlgItemText(IDC_EDIT_N, (LPCTSTR)msg); // if(msg != NULL) //{ // free(msg); // msg = NULL; //} struct asn1struct certificate; struct RsaKeyBlob ExportKey; memset(&ExportKey, 0, sizeof(RsaKeyBlob)); asn1parse(outRSA, outlenRSA, &certificate); ExportKey.m_IndexCount = 0; ans1GetKey(0, &certificate, &ExportKey); asn1free( &certificate ); unsigned long len = ExportKey.m_ulKeyBits; unsigned char *msg; // N msg = (unsigned char *) malloc(2*len+1); *(msg+(2*len)) = '\0'; StdCharsToHexChars(ExportKey.m_N, msg, len); SetDlgItemText(IDC_EDIT_N, (LPCTSTR)msg); if(msg != NULL) { free(msg); msg = NULL; } //D len = ExportKey.m_ulKeyBits; msg = (unsigned char *) malloc(2*len+1); *(msg+(2*len)) = '\0'; StdCharsToHexChars(ExportKey.m_D, msg, len); SetDlgItemText(IDC_EDIT_D, (LPCTSTR)msg); if(msg != NULL) { free(msg); msg = NULL; } //P len = ExportKey.m_ulKeyBits / 2; msg = (unsigned char *) malloc(2*len+1); *(msg+(2*len)) = '\0'; StdCharsToHexChars(ExportKey.m_p, msg, len); SetDlgItemText(IDC_EDIT_P, (LPCTSTR)msg); if(msg != NULL) { free(msg); msg = NULL; } // Q len = ExportKey.m_ulKeyBits / 2; msg = (unsigned char *) malloc(2*len+1); *(msg+(2*len)) = '\0'; StdCharsToHexChars(ExportKey.m_q, msg, len); SetDlgItemText(IDC_EDIT_Q, (LPCTSTR)msg); if(msg != NULL) { free(msg); msg = NULL; } // DP len = ExportKey.m_ulKeyBits / 2; msg = (unsigned char *) malloc(2*len+1); *(msg+(2*len)) = '\0'; StdCharsToHexChars(ExportKey.m_dp, msg, len); SetDlgItemText(IDC_EDIT_DP, (LPCTSTR)msg); if(msg != NULL) { free(msg); msg = NULL; } // DQ len = ExportKey.m_ulKeyBits / 2; msg = (unsigned char *) malloc(2*len+1); *(msg+(2*len)) = '\0'; StdCharsToHexChars(ExportKey.m_dq, msg, len); SetDlgItemText(IDC_EDIT_DQ, (LPCTSTR)msg); if(msg != NULL) { free(msg); msg = NULL; } // Qinv len = ExportKey.m_ulKeyBits / 2; msg = (unsigned char *) malloc(2*len+1); *(msg+(2*len)) = '\0'; StdCharsToHexChars(ExportKey.m_qinv, msg, len); SetDlgItemText(IDC_EDIT_Qinv, (LPCTSTR)msg); if(msg != NULL) { free(msg); msg = NULL; } }
int rsa_test(void) { unsigned char in[1024], out[1024], tmp[1024]; rsa_key key, privKey, pubKey; int hash_idx, prng_idx, stat, stat2; unsigned long rsa_msgsize, len, len2, cnt; static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 }; if (rsa_compat_test() != 0) { return 1; } hash_idx = find_hash("sha1"); prng_idx = find_prng("yarrow"); if (hash_idx == -1 || prng_idx == -1) { fprintf(stderr, "rsa_test requires LTC_SHA1 and yarrow"); return 1; } /* make 10 random key */ for (cnt = 0; cnt < 10; cnt++) { DO(rsa_make_key(&yarrow_prng, prng_idx, 1024/8, 65537, &key)); if (mp_count_bits(key.N) != 1024) { fprintf(stderr, "rsa_1024 key modulus has %d bits\n", mp_count_bits(key.N)); len = mp_unsigned_bin_size(key.N); mp_to_unsigned_bin(key.N, tmp); fprintf(stderr, "N == \n"); for (cnt = 0; cnt < len; ) { fprintf(stderr, "%02x ", tmp[cnt]); if (!(++cnt & 15)) fprintf(stderr, "\n"); } len = mp_unsigned_bin_size(key.p); mp_to_unsigned_bin(key.p, tmp); fprintf(stderr, "p == \n"); for (cnt = 0; cnt < len; ) { fprintf(stderr, "%02x ", tmp[cnt]); if (!(++cnt & 15)) fprintf(stderr, "\n"); } len = mp_unsigned_bin_size(key.q); mp_to_unsigned_bin(key.q, tmp); fprintf(stderr, "\nq == \n"); for (cnt = 0; cnt < len; ) { fprintf(stderr, "%02x ", tmp[cnt]); if (!(++cnt & 15)) fprintf(stderr, "\n"); } fprintf(stderr, "\n"); return 1; } if (cnt != 9) { rsa_free(&key); } } /* encrypt the key (without lparam) */ for (cnt = 0; cnt < 4; cnt++) { for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) { /* make a random key/msg */ yarrow_read(in, rsa_msgsize, &yarrow_prng); len = sizeof(out); len2 = rsa_msgsize; DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, hash_idx, &key)); /* change a byte */ out[8] ^= 1; DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat2, &key)); /* change a byte back */ out[8] ^= 1; if (len2 != rsa_msgsize) { fprintf(stderr, "\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2); return 1; } len2 = rsa_msgsize; DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat, &key)); if (!(stat == 1 && stat2 == 0)) { fprintf(stderr, "rsa_decrypt_key failed"); return 1; } if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) { unsigned long x; fprintf(stderr, "\nrsa_decrypt_key mismatch, len %lu (second decrypt)\n", len2); fprintf(stderr, "Original contents: \n"); for (x = 0; x < rsa_msgsize; ) { fprintf(stderr, "%02x ", in[x]); if (!(++x % 16)) { fprintf(stderr, "\n"); } } fprintf(stderr, "\n"); fprintf(stderr, "Output contents: \n"); for (x = 0; x < rsa_msgsize; ) { fprintf(stderr, "%02x ", out[x]); if (!(++x % 16)) { fprintf(stderr, "\n"); } } fprintf(stderr, "\n"); return 1; } } } /* encrypt the key (with lparam) */ for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) { len = sizeof(out); len2 = rsa_msgsize; DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, lparam, sizeof(lparam), &yarrow_prng, prng_idx, hash_idx, &key)); /* change a byte */ out[8] ^= 1; DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat2, &key)); if (len2 != rsa_msgsize) { fprintf(stderr, "\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2); return 1; } /* change a byte back */ out[8] ^= 1; len2 = rsa_msgsize; DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat, &key)); if (!(stat == 1 && stat2 == 0)) { fprintf(stderr, "rsa_decrypt_key failed"); return 1; } if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) { fprintf(stderr, "rsa_decrypt_key mismatch len %lu", len2); return 1; } } /* encrypt the key LTC_PKCS #1 v1.5 (payload from 1 to 117 bytes) */ for (rsa_msgsize = 1; rsa_msgsize <= 117; rsa_msgsize++) { len = sizeof(out); len2 = rsa_msgsize; DO(rsa_encrypt_key_ex(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, 0, LTC_PKCS_1_V1_5, &key)); len2 = rsa_msgsize; DO(rsa_decrypt_key_ex(out, len, tmp, &len2, NULL, 0, 0, LTC_PKCS_1_V1_5, &stat, &key)); if (!(stat == 1 && stat2 == 0)) { fprintf(stderr, "rsa_decrypt_key_ex failed, %d, %d", stat, stat2); return 1; } if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) { fprintf(stderr, "rsa_decrypt_key_ex mismatch len %lu", len2); return 1; } } /* sign a message (unsalted, lower cholestorol and Atkins approved) now */ len = sizeof(out); DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 0, &key)); /* export key and import as both private and public */ len2 = sizeof(tmp); DO(rsa_export(tmp, &len2, PK_PRIVATE, &key)); DO(rsa_import(tmp, len2, &privKey)); len2 = sizeof(tmp); DO(rsa_export(tmp, &len2, PK_PUBLIC, &key)); DO(rsa_import(tmp, len2, &pubKey)); /* verify with original */ DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &key)); /* change a byte */ in[0] ^= 1; DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &key)); if (!(stat == 1 && stat2 == 0)) { fprintf(stderr, "rsa_verify_hash (unsalted, origKey) failed, %d, %d", stat, stat2); rsa_free(&key); rsa_free(&pubKey); rsa_free(&privKey); return 1; } /* verify with privKey */ /* change a byte */ in[0] ^= 1; DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &privKey)); /* change a byte */ in[0] ^= 1; DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &privKey)); if (!(stat == 1 && stat2 == 0)) { fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2); rsa_free(&key); rsa_free(&pubKey); rsa_free(&privKey); return 1; } /* verify with pubKey */ /* change a byte */ in[0] ^= 1; DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &pubKey)); /* change a byte */ in[0] ^= 1; DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &pubKey)); if (!(stat == 1 && stat2 == 0)) { fprintf(stderr, "rsa_verify_hash (unsalted, pubkey) failed, %d, %d", stat, stat2); rsa_free(&key); rsa_free(&pubKey); rsa_free(&privKey); return 1; } /* sign a message (salted) now (use privKey to make, pubKey to verify) */ len = sizeof(out); DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 8, &privKey)); DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat, &pubKey)); /* change a byte */ in[0] ^= 1; DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat2, &pubKey)); if (!(stat == 1 && stat2 == 0)) { fprintf(stderr, "rsa_verify_hash (salted) failed, %d, %d", stat, stat2); rsa_free(&key); rsa_free(&pubKey); rsa_free(&privKey); return 1; } /* sign a message with LTC_PKCS #1 v1.5 */ len = sizeof(out); DO(rsa_sign_hash_ex(in, 20, out, &len, LTC_PKCS_1_V1_5, &yarrow_prng, prng_idx, hash_idx, 8, &privKey)); DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat, &pubKey)); /* change a byte */ in[0] ^= 1; DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat2, &pubKey)); if (!(stat == 1 && stat2 == 0)) { fprintf(stderr, "rsa_verify_hash_ex failed, %d, %d", stat, stat2); rsa_free(&key); rsa_free(&pubKey); rsa_free(&privKey); return 1; } /* free the key and return */ rsa_free(&key); rsa_free(&pubKey); rsa_free(&privKey); return 0; }