/** Verify a signature @param hashname [in] String naming the hash @param keydatalen [in] The length of the public key @param keydata [in] The public key of the signer @param sigdatalen [in] The length of the signature data @param sigdata [in] The signature data @param filedatalen [in] The length of the file in octets @param filedata [in] The contents of the file being verified @param ... [in] Additional len,data pairs until len is 0 @return nonzero on error [or invalid], 0 on success If */ int verify_data( char *hashname, unsigned long keydatalen, unsigned char *keydata, unsigned long sigdatalen, unsigned char *sigdata, unsigned long filedatalen, const unsigned char *filedata, ...) { rsa_key rsakey; unsigned char rsabuf[2048], md[MAXBLOCKSIZE]; unsigned long rsalen, mdlen; int stat; int res; va_list args; const unsigned char *dataptr; unsigned long datalen; hash_state hs; struct ltc_hash_descriptor *hd; int hashid; heap_start(heap_mem, HEAP_SIZE); if (strcmp(hashname,"des") == 0) { symmetric_key skey; DO(des_setup(keydata, keydatalen, 0, &skey),0x400000); DO(des_ecb_encrypt(filedata, sigdata, &skey),0x500000); return res; } register_hash(&sha256_desc); // register_hash(&sha512_desc); // register_hash(&whirlpool_desc); register_hash(&rmd160_desc); register_hash(&md4_desc); register_hash(<c_md5_desc); register_hash(&sha1_desc); ltc_mp = tfm_desc; hashid = find_hash(hashname); if ((res = hash_is_valid(hashid)) != CRYPT_OK) return res; hd = &hash_descriptor[hashid]; if ((res = hd->init(&hs)) != CRYPT_OK) return res; va_start(args, filedata); dataptr = filedata; datalen = filedatalen; for(;;) { if((res = hd->process(&hs, dataptr, datalen)) != 0) return res; if((datalen = va_arg(args, unsigned long)) == 0) break; if((dataptr = va_arg(args, unsigned char *)) == NULL) break; } va_end(args); if (keydatalen == 0) { res = hd->done(&hs, sigdata); *keydata = hd->hashsize; return res+0x100000; } if((res = hd->done(&hs, md)) != 0) return res+0x200000; mdlen = hd->hashsize; DO(rsa_import(keydata, keydatalen, &rsakey),0x300000); DO(rsa_verify_hash(sigdata, sigdatalen, md, mdlen, find_hash(hashname), 8, &stat, &rsakey),0x400000); rsa_free(&rsakey); return (stat == 0) ? -1 : 0; }
static bool decode_base64_key(const char * szKeyBase64, rsa_key * key) { unsigned char decoded_key[0x200]; const char * szBase64Begin; const char * szBase64End; unsigned long decoded_length = sizeof(decoded_key); unsigned long length; // Find out the begin of the BASE64 data szBase64Begin = szKeyBase64 + strlen("-----BEGIN PUBLIC KEY-----"); szBase64End = szBase64Begin + strlen(szBase64Begin) - strlen("-----END PUBLIC KEY-----"); if(szBase64End[0] != '-') return false; // decode the base64 string length = (unsigned long)(szBase64End - szBase64Begin); if(base64_decode((unsigned char *)szBase64Begin, length, decoded_key, &decoded_length) != CRYPT_OK) return false; // Create RSA key if(rsa_import(decoded_key, decoded_length, key) != CRYPT_OK) return false; return true; }
static int rsa_compat_test(void) { rsa_key key; unsigned char buf[1024]; unsigned long len; /* try reading the key */ DO(rsa_import(openssl_private_rsa, sizeof(openssl_private_rsa), &key)); /* now try to export private/public and compare */ len = sizeof(buf); DO(rsa_export(buf, &len, PK_PRIVATE, &key)); if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) { fprintf(stderr, "RSA private export failed to match OpenSSL output, %lu, %lu\n", len, (unsigned long)sizeof(openssl_private_rsa)); return 1; } len = sizeof(buf); DO(rsa_export(buf, &len, PK_PUBLIC, &key)); if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) { fprintf(stderr, "RSA(private) public export failed to match OpenSSL output\n"); return 1; } rsa_free(&key); /* try reading the public key */ DO(rsa_import(openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), &key)); len = sizeof(buf); DO(rsa_export(buf, &len, PK_PUBLIC, &key)); if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) { fprintf(stderr, "RSA(public) stripped public import failed to match OpenSSL output\n"); return 1; } rsa_free(&key); /* try reading the public key */ DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key)); len = sizeof(buf); DO(rsa_export(buf, &len, PK_PUBLIC, &key)); if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) { fprintf(stderr, "RSA(public) SSL public import failed to match OpenSSL output\n"); return 1; } rsa_free(&key); return 0; }
static int rsa_compat_test(void) { rsa_key key; unsigned char buf[1024]; unsigned long len; /* try reading the key */ DO(rsa_import(openssl_private_rsa, sizeof(openssl_private_rsa), &key)); /* now try to export private/public and compare */ len = sizeof(buf); DO(rsa_export(buf, &len, PK_PRIVATE, &key)); if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) { fprintf(stderr, "RSA private export failed to match OpenSSL output, %lu, %lu\n", len, sizeof(openssl_private_rsa)); { int x; printf("\n\n"); for (x = 0; x < len; ) { if (buf[x] == openssl_private_rsa[x]) printf("-- "); else printf("%02x ", buf[x]^openssl_private_rsa[x]); if (!(++x & 15)) printf("\n"); } } printf("\n\n"); return 1; } len = sizeof(buf); DO(rsa_export(buf, &len, PK_PUBLIC, &key)); if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) { fprintf(stderr, "RSA(private) public export failed to match OpenSSL output\n"); return 1; } rsa_free(&key); /* try reading the public key */ DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key)); len = sizeof(buf); DO(rsa_export(buf, &len, PK_PUBLIC, &key)); if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) { fprintf(stderr, "RSA(public) public export failed to match OpenSSL output\n"); return 1; } rsa_free(&key); return 0; }
void read_rsakey(rsa_key *key, const char *fname) { unsigned char buf[4096]; unsigned long len = sizeof (buf); int rc; read_file(fname, buf, &len); if ((rc = rsa_import(buf, len, key)) != CRYPT_OK) { fail("rsa_import for '%s' failed: %s", fname, error_to_string(rc)); } }
void DB_AuthLoad_DecryptKey(unsigned char* encKey, ZoneKey* tKey) { int hash = find_hash("sha256"); rsa_key key; rsa_import(ffKey, sizeof(ffKey), &key); int stat; unsigned long outLen = 40; rsa_decrypt_key_ex(encKey, 256, (unsigned char*)tKey, &outLen, NULL, NULL, hash, 2, &stat, &key); rsa_free(&key); }
int main() { ltc_mp = ltm_desc; rsa_key priv_key, pub_key; int hash_idx, prng_idx; int ret = rsa_import(openssl_private_rsa, sizeof(openssl_private_rsa), &priv_key); ret = rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &pub_key); if (register_hash(&sha1_desc) == -1) { printf("Error registering SHA1\n"); return -1; } hash_idx = find_hash("sha1"); register_prng(&sprng_desc); prng_idx = find_prng("sprng"); prng_state prng; int err; if ((err = yarrow_start(&prng)) != CRYPT_OK) { printf("Start error: %s\n", error_to_string(err)); } /* add entropy */ if ((err = yarrow_add_entropy("hello world", 11, &prng)) != CRYPT_OK) { printf("Add_entropy error: %s\n", error_to_string(err)); } int stat; unsigned char buf[1024]; long size = 1024; //ret = rsa_decrypt_key(sig, strlen(sig), buf, &size, 0, 0, hash_idx, &stat, &key); ret = rsa_sign_hash(hash, strlen(hash), buf, &size, &prng, prng_idx, hash_idx, 0, &priv_key); ret = rsa_verify_hash(sig, strlen(sig), hash, strlen(hash), hash_idx, 0, &stat, &pub_key); printf("status is : %d\n", ret); //load stuff! return 0; }
bool CryptHelpers::SignFile( RageFileBasic &file, CString sPrivKey, CString &sSignatureOut, CString &sError ) { #ifdef _XBOX return false; #else unsigned char embedded_key[4096], filehash[20], sig[128]; unsigned long keysize = 4096, sigsize = 128; int ret; rsa_key key; bool bDecoded = PKCS8DecodePrivateKey((const unsigned char *)sPrivKey.data(), sPrivKey.size(), embedded_key, keysize); if ( bDecoded ) sPrivKey.assign((const char*)embedded_key, keysize); ret = rsa_import((const unsigned char*)sPrivKey.data(), sPrivKey.size(), &key); if ( ret != CRYPT_OK ) { LOG->Warn("Could not import private key: %s", error_to_string(ret)); return false; } bool bShaRet = GetSha1ForFile(file, filehash); if ( !bShaRet ) { LOG->Warn("Could not get SHA1 for file"); return false; } ret = rsa_sign_hash_ex( filehash, 20, sig, &sigsize, LTC_LTC_PKCS_1_V1_5, &g_PRNGState, g_PRNGDescId, g_SHA1DescId, 0, &key ); if ( ret != CRYPT_OK ) { LOG->Warn("Could not sign hash file: %s", error_to_string(ret)); return false; } ASSERT( sigsize == 128 ); sSignatureOut.assign( (const char *)sig, sigsize ); return true; #endif }
bool CryptHelpers::VerifyFile( RageFileBasic &file, CString sSignature, CString sPublicKey, CString &sError ) { unsigned char buf_hash[20]; rsa_key key; int ret = rsa_import( (const unsigned char *)sPublicKey.data(), sPublicKey.size(), &key ); if ( ret != CRYPT_OK ) { sError = ssprintf("Could not import public key: %s", error_to_string(ret)); LOG->Warn(sError); return false; } bool bHashed = GetSha1ForFile( file, buf_hash ); if ( !bHashed ) { sError = ssprintf("Error while SHA1 hashing file"); LOG->Warn(sError); return false; } int iMatch = 0; ret = rsa_verify_hash_ex( (const unsigned char*)sSignature.data(), sSignature.size(), buf_hash, sizeof(buf_hash), LTC_LTC_PKCS_1_V1_5, g_SHA1DescId, 0, &iMatch, &key ); if ( ret != CRYPT_OK ) { sError = ssprintf("Could not verify hash: %s", error_to_string(ret)); LOG->Warn(sError); return false; } if ( iMatch == 0 ) { sError = "Signature Mismatch"; LOG->Warn(sError); return false; } return true; }
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; }
int main(int argc, char **argv) { rsa_key rsakey; char fname[256]; char *hashname; char expiry[256]; unsigned char buf[4096], rsabuf[2048], md[MAXBLOCKSIZE], sig[512]; unsigned long buflen, rsalen, mdlen, siglen; FILE *infile; int i; int opt_v2 = 0; int opt_fullkey = 0; int argoffset = 0; if (argc < 3) { fprintf(stderr, "Usage: %s [--fullkey] [--v2 expiry] hashname key_file_name [signed_file_name]\n", argv[0]); return EXIT_FAILURE; } LTC_ARGCHK(register_hash(&sha256_desc) != -1); LTC_ARGCHK(register_hash(&sha512_desc) != -1); LTC_ARGCHK(register_hash(&rmd160_desc) != -1); LTC_ARGCHK(register_hash(&whirlpool_desc) != -1); LTC_ARGCHK(register_prng(&sprng_desc) != -1); ltc_mp = tfm_desc; for ( i=1; i < argc; i++) { if (strcmp(argv[i], OPT_V2)==0) { opt_v2 = 1; strncpy(expiry, argv[i+1], 256); i++; argoffset=argoffset+2; continue; } if (strcmp(argv[i], OPT_FULLKEY)==0) { opt_fullkey = 1; argoffset++; continue; } /* done! get out softly */ i=argc; } hashname = argv[1+argoffset]; /* get hashes of file */ mdlen = sizeof(md); if ( argc - argoffset > 3) { DO(hash_file(find_hash(argv[1+argoffset]), argv[3+argoffset], md, &mdlen)); } else { DO(hash_filehandle(find_hash(argv[1+argoffset]), stdin, md, &mdlen)); } /* read keyblob and import key from it */ strncpy(fname, argv[2+argoffset], 256); strncat(fname, ".private", 256); infile = fopen(fname, "rb"); LTC_ARGCHK(infile != NULL); buflen = fread(buf, 1, sizeof(buf), infile); fclose(infile); /* now try to import the RSA key */ DO(rsa_import(buf, buflen, &rsakey)); /* now sign the hashes */ siglen = sizeof(sig); DO(rsa_sign_hash(md, mdlen, sig, &siglen, NULL, find_prng("sprng"), find_hash(hashname), 8, &rsakey)); /* open output file */ if (opt_v2==1) { fprintf(stdout, "sig02: %s ", hashname); } else { fprintf(stdout, "sig01: %s ", hashname); } /* read keyblob and import key from it */ strncpy(fname, argv[2+argoffset], 256); strncat(fname, ".public", 256); infile = fopen(fname, "rb"); LTC_ARGCHK(infile != NULL); buflen = fread(buf, 1, sizeof(buf), infile); fclose(infile); if (opt_fullkey==1) { i = 0; } else { i = buflen-32; } for ( ; i < buflen; i++) fprintf(stdout, "%02x", buf[i]); fprintf(stdout, " "); if (opt_v2==1) { fprintf(stdout, expiry); fprintf(stdout, " "); } for (i = 0; i < siglen; i++) fprintf(stdout, "%02x", sig[i]); fprintf(stdout, "\n"); rsa_free(&rsakey); return EXIT_SUCCESS; }