Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1. #include<string.h>
  2. #include<stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <openssl/rsa.h>
  6. #include <openssl/bn.h>
  7. #include <openssl/sha.h>
  8. #include <openssl/pem.h>
  9. #include <openssl/evp.h>
  10. #include <openssl/objects.h>
  11.  
  12. void WrongUsage(){
  13.     printf("Usage: docsign -sv\n");
  14.     printf("-s sign document\n");
  15.     printf("-v validate signature\n");
  16.     exit(0);
  17. }
  18. void generateRSAkeys(){
  19.     printf("Generating keys\n");
  20.     RSA *rsa=RSA_new();
  21.     BIGNUM *bn=BN_new();
  22.     BN_set_word(bn, RSA_F4);
  23.     RSA_generate_key_ex(rsa, 2048, bn, NULL);
  24.     BN_free(bn);
  25.  
  26.     FILE* private=fopen("private.pem", "w");
  27.     PEM_write_RSAPrivateKey(private, rsa, NULL, NULL, 0, 0, NULL);
  28.     fclose(private);
  29.  
  30.     FILE* public=fopen("public.pem", "w");
  31.     PEM_write_RSA_PUBKEY(public, rsa);
  32.     fclose(public);
  33.  
  34.     RSA_free(rsa);
  35. }
  36. void printHEX(char* arr, int sz){
  37.     for(int i=0; i<sz; i++)
  38.         printf("%02x", arr[i]);
  39.     printf("\n");
  40. }
  41. int main(int argc, char** argv){
  42.     OpenSSL_add_all_digests();
  43.     if(argc!=3)
  44.         WrongUsage();
  45.     if(access( argv[2], F_OK ) == -1) {
  46.         printf("File doesn't exist!!!\n");
  47.         WrongUsage();
  48.     }
  49.     if(!strcmp("-s", argv[1])){
  50.         printf("Starting Signing Proccess\n");
  51.         printf("-------------------------\n");
  52.         printf("Check if keys exist\n");
  53.         if(access("private.pem", F_OK ) == -1 || access("public.pem", F_OK )==-1){
  54.             printf("Keys Don't exist");
  55.             generateRSAkeys();
  56.         }
  57.         printf("Reading private key\n");
  58.         FILE* private=fopen("private.pem", "r");
  59.         RSA *rsa = NULL;
  60.         PEM_read_RSAPrivateKey(private, &rsa, NULL, NULL);
  61.         fclose(private);
  62.         EVP_PKEY *key=EVP_PKEY_new();
  63.         EVP_PKEY_assign_RSA(key, rsa);
  64.         printf("Calculating Digest\n");
  65.         FILE* fin=fopen(argv[2], "rb");
  66.         FILE* fout=fopen(strcat(argv[2],".signed"), "wb");
  67.         EVP_MD_CTX *ctx = EVP_MD_CTX_create();
  68.         const EVP_MD *md = EVP_get_digestbyname("SHA256");
  69.         EVP_SignInit_ex(ctx, md, NULL);
  70.         unsigned char *buffer = malloc(32768);
  71.         int bytesRead;
  72.         while((bytesRead = fread(buffer, 1, 32768, fin))){
  73.             EVP_SignUpdate(ctx, buffer, bytesRead);
  74.             fwrite(buffer, 1, bytesRead, fout);
  75.         }
  76.         printf("Signing the digest\n");
  77.         unsigned char signature[256];
  78.         int len;
  79.         EVP_SignFinal(ctx, signature, &len, key);
  80.         printf("Writing signature\n");
  81.         fwrite(signature, 1, len, fout);
  82.         printHEX(signature, 256);
  83.         fclose(fin);
  84.         fclose(fout);
  85.     }else if(!strcmp("-v", argv[1])){
  86.         printf("Starting Validating Proccess\n");
  87.         printf("----------------------------\n");
  88.         FILE *fin = fopen(argv[2], "rb");
  89.         fseek (fin, 0, SEEK_END);
  90.         int size=ftell(fin);
  91.         printf("%d\n", size);
  92.         char* dataplus=malloc(size);
  93.         fseek (fin, 0, SEEK_SET);
  94.         fread(dataplus, 1, size, fin);
  95.         char* data=malloc(size-256);
  96.         memcpy(data, dataplus, size-256);
  97.         char* signature=malloc(256);
  98.         memcpy(signature, dataplus+size-256, 256);
  99.         size-=256;
  100.         if(access("public.pem", F_OK ) == -1){
  101.             printf("Public Key doesn't exist");
  102.             exit(0);
  103.         }
  104.         RSA *rsa = NULL;
  105.         FILE* public=fopen("public.pem", "r");
  106.         PEM_read_RSA_PUBKEY(public, &rsa, NULL, NULL);
  107.         fclose(public);
  108.         EVP_PKEY *key=EVP_PKEY_new();
  109.         EVP_PKEY_assign_RSA(key, rsa);
  110.         EVP_MD_CTX *ctx = EVP_MD_CTX_create();
  111.         const EVP_MD *md = EVP_get_digestbyname("SHA256");
  112.         EVP_VerifyInit_ex(ctx, md, NULL);
  113.         EVP_VerifyUpdate(ctx, data, size);
  114.         int res=EVP_VerifyFinal(ctx, signature, 256, key);
  115.         if(res==1)
  116.             printf("Digital signature is valid.\n");
  117.         else if(!res)
  118.             printf("Digital signature is not valid.\n");
  119.     }else{
  120.         WrongUsage();
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement