![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
13 nov 12.30 pm
Dependencies: mbed-rtos mbed wolfSSL
main.cpp@0:1c8dabfe6522, 2018-11-13 (annotated)
- Committer:
- preyaa4
- Date:
- Tue Nov 13 07:06:14 2018 +0000
- Revision:
- 0:1c8dabfe6522
13 nov 12.30 pm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
preyaa4 | 0:1c8dabfe6522 | 1 | #include "mbed.h" |
preyaa4 | 0:1c8dabfe6522 | 2 | |
preyaa4 | 0:1c8dabfe6522 | 3 | #include <iostream> |
preyaa4 | 0:1c8dabfe6522 | 4 | #include <string.h> |
preyaa4 | 0:1c8dabfe6522 | 5 | #include <wolfssl/openssl/rsa.h> |
preyaa4 | 0:1c8dabfe6522 | 6 | #include <wolfssl/openssl/pem.h> |
preyaa4 | 0:1c8dabfe6522 | 7 | #include <wolfssl/openssl/err.h> |
preyaa4 | 0:1c8dabfe6522 | 8 | |
preyaa4 | 0:1c8dabfe6522 | 9 | #include "openssl_rsa.h" |
preyaa4 | 0:1c8dabfe6522 | 10 | |
preyaa4 | 0:1c8dabfe6522 | 11 | |
preyaa4 | 0:1c8dabfe6522 | 12 | using namespace std; |
preyaa4 | 0:1c8dabfe6522 | 13 | |
preyaa4 | 0:1c8dabfe6522 | 14 | |
preyaa4 | 0:1c8dabfe6522 | 15 | int main() { |
preyaa4 | 0:1c8dabfe6522 | 16 | |
preyaa4 | 0:1c8dabfe6522 | 17 | LOG("OpenSSL_RSA has been started."); |
preyaa4 | 0:1c8dabfe6522 | 18 | |
preyaa4 | 0:1c8dabfe6522 | 19 | RSA *private_key; |
preyaa4 | 0:1c8dabfe6522 | 20 | RSA *public_key; |
preyaa4 | 0:1c8dabfe6522 | 21 | |
preyaa4 | 0:1c8dabfe6522 | 22 | char message[KEY_LENGTH / 8] = "Batuhan AVLAYAN - OpenSSL_RSA demo"; |
preyaa4 | 0:1c8dabfe6522 | 23 | char *encrypt = NULL; |
preyaa4 | 0:1c8dabfe6522 | 24 | char *decrypt = NULL; |
preyaa4 | 0:1c8dabfe6522 | 25 | |
preyaa4 | 0:1c8dabfe6522 | 26 | char private_key_pem[12] = "private_key"; |
preyaa4 | 0:1c8dabfe6522 | 27 | char public_key_pem[11] = "public_key"; |
preyaa4 | 0:1c8dabfe6522 | 28 | |
preyaa4 | 0:1c8dabfe6522 | 29 | LOG(KEY_LENGTH); |
preyaa4 | 0:1c8dabfe6522 | 30 | LOG(PUBLIC_EXPONENT); |
preyaa4 | 0:1c8dabfe6522 | 31 | |
preyaa4 | 0:1c8dabfe6522 | 32 | RSA *keypair = RSA_generate_key(KEY_LENGTH, PUBLIC_EXPONENT, NULL, NULL); |
preyaa4 | 0:1c8dabfe6522 | 33 | LOG("Generate key has been created."); |
preyaa4 | 0:1c8dabfe6522 | 34 | |
preyaa4 | 0:1c8dabfe6522 | 35 | private_key = create_RSA(keypair, PRIVATE_KEY_PEM, private_key_pem); |
preyaa4 | 0:1c8dabfe6522 | 36 | LOG("Private key pem file has been created."); |
preyaa4 | 0:1c8dabfe6522 | 37 | |
preyaa4 | 0:1c8dabfe6522 | 38 | public_key = create_RSA(keypair, PUBLIC_KEY_PEM, public_key_pem); |
preyaa4 | 0:1c8dabfe6522 | 39 | LOG("Public key pem file has been created.");; |
preyaa4 | 0:1c8dabfe6522 | 40 | |
preyaa4 | 0:1c8dabfe6522 | 41 | encrypt = (char*)malloc(RSA_size(public_key)); |
preyaa4 | 0:1c8dabfe6522 | 42 | int encrypt_length = public_encrypt(strlen(message) + 1, (unsigned char*)message, (unsigned char*)encrypt, public_key, RSA_PKCS1_OAEP_PADDING); |
preyaa4 | 0:1c8dabfe6522 | 43 | if(encrypt_length == -1) { |
preyaa4 | 0:1c8dabfe6522 | 44 | LOG("An error occurred in public_encrypt() method"); |
preyaa4 | 0:1c8dabfe6522 | 45 | } |
preyaa4 | 0:1c8dabfe6522 | 46 | LOG("Data has been encrypted."); |
preyaa4 | 0:1c8dabfe6522 | 47 | |
preyaa4 | 0:1c8dabfe6522 | 48 | create_encrypted_file(encrypt, public_key); |
preyaa4 | 0:1c8dabfe6522 | 49 | LOG("Encrypted file has been created."); |
preyaa4 | 0:1c8dabfe6522 | 50 | |
preyaa4 | 0:1c8dabfe6522 | 51 | decrypt = (char *)malloc(encrypt_length); |
preyaa4 | 0:1c8dabfe6522 | 52 | int decrypt_length = private_decrypt(encrypt_length, (unsigned char*)encrypt, (unsigned char*)decrypt, private_key, RSA_PKCS1_OAEP_PADDING); |
preyaa4 | 0:1c8dabfe6522 | 53 | if(decrypt_length == -1) { |
preyaa4 | 0:1c8dabfe6522 | 54 | LOG("An error occurred in private_decrypt() method"); |
preyaa4 | 0:1c8dabfe6522 | 55 | } |
preyaa4 | 0:1c8dabfe6522 | 56 | LOG("Data has been decrypted."); |
preyaa4 | 0:1c8dabfe6522 | 57 | |
preyaa4 | 0:1c8dabfe6522 | 58 | FILE *decrypted_file = fopen("decrypted_file.txt", "w"); |
preyaa4 | 0:1c8dabfe6522 | 59 | fwrite(decrypt, sizeof(*decrypt), decrypt_length - 1, decrypted_file); |
preyaa4 | 0:1c8dabfe6522 | 60 | fclose(decrypted_file); |
preyaa4 | 0:1c8dabfe6522 | 61 | LOG("Decrypted file has been created."); |
preyaa4 | 0:1c8dabfe6522 | 62 | |
preyaa4 | 0:1c8dabfe6522 | 63 | RSA_free(keypair); |
preyaa4 | 0:1c8dabfe6522 | 64 | free(private_key); |
preyaa4 | 0:1c8dabfe6522 | 65 | free(public_key); |
preyaa4 | 0:1c8dabfe6522 | 66 | free(encrypt); |
preyaa4 | 0:1c8dabfe6522 | 67 | free(decrypt); |
preyaa4 | 0:1c8dabfe6522 | 68 | LOG("OpenSSL_RSA has been finished."); |
preyaa4 | 0:1c8dabfe6522 | 69 | |
preyaa4 | 0:1c8dabfe6522 | 70 | return 0; |
preyaa4 | 0:1c8dabfe6522 | 71 | } |