byte* rsa_oaep_crypt(const rsa_pub_t key, const byte *msg, size_t len, size_t *rlen) { byte *oaep_enc = oaep_encode(msg, len); if (!oaep_enc) { return NULL; } byte *ret = rsa_encrypt_block(key, oaep_enc, OAEP_LEN, rlen); free(oaep_enc); return ret; }
int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "usage: %s priv_file pub_file\n", argv[0]); return EXIT_FAILURE; } byte msg[] = "It's not the red of the dying sun /" " The morning sheets surprising stain /" " It's not the red of which we bleed /" " The red of cabernet sauvignon /" " A world of ruby all in vain /" " It's not that red /" " It's not as golden as Zeus famous shower /" " It doesn't, not at a"; char *priv_name = argv[1]; char *pub_name = argv[2]; rsa_keypair_t kp; FILE *pub = fopen(pub_name, "rb"); FILE *priv = fopen(priv_name, "rb"); if (!pub || !priv) { fputs("error: cannot open key files for reading\n", stderr); return EXIT_FAILURE; } bool failed = !rsa_read_pubkey(&kp.pub, pub) || !rsa_read_privkey(&kp.priv, priv); fclose(pub); fclose(priv); if (failed) { fputs("error: cannot read keys\n", stderr); return EXIT_FAILURE; } puts((char*) msg); bytes_n_dump(msg, RSA_KEY); puts(""); size_t rlen; rsa_encrypt_block(kp.pub, msg, sizeof msg, &rlen); bytes_n_dump(msg, rlen); puts(""); rsa_decrypt_block(kp.priv, msg, rlen, &rlen); bytes_n_dump(msg, rlen); printf("\n%s\n", (char*) msg); }
int main() { int ret = 0; size_t len = STR_LEN; char E[STR_LEN], D[STR_LEN], N[STR_LEN]; uchar source[MSG_LEN]; uchar encrypted[MSG_LEN], decrypted[MSG_LEN]; // Buffers public_key pub; private_key priv; MPI_CHK(rsa_generate_keys(65537, pub, priv, 1024)); MPI_CHK(mpi_write_string(&pub.e, 10, E, &len)); len = STR_LEN; MPI_CHK(mpi_write_string(&pub.n, 10, N, &len)); len = STR_LEN; MPI_CHK(mpi_write_string(&priv.d, 10, D, &len)); printf("e = %s\nd = %s\nn = %s\n", E, D, N); printf("Enter message to encrypt:\n"); scanf("%[^\n]", source); len = strnlen((const char *)source, MSG_LEN) + 1; // Length of string + zero-char try { MPI_CHK(rsa_encrypt_block(source, len, encrypted, MSG_LEN, pub)); print_buffer("Encrypted", encrypted, MSG_LEN); MPI_CHK(rsa_decrypt_block(encrypted, MSG_LEN, decrypted, MSG_LEN, priv)); print_buffer("Decrypted", decrypted, MSG_LEN); MPI_CHK(rsa_sign_block(source, len, encrypted, MSG_LEN, priv)); print_buffer("Signature", encrypted, MSG_LEN); MPI_CHK(rsa_check_block(encrypted, MSG_LEN, decrypted, MSG_LEN, pub)); print_buffer("Preimage", decrypted, MSG_LEN); source[0] = InvertBit(source[0], 2); printf("Corrupted:\n%s\n\n", source); MPI_CHK(rsa_sign_block(source, len, encrypted, MSG_LEN, priv)); print_buffer("Signature of corrupted", encrypted, MSG_LEN); } catch (char *e) { printf("Error: %s\n", e); } cleanup: mpi_free(&pub.e); mpi_free(&priv.d); mpi_free(&priv.n); return ret; }