certsgen.c 8.88 KB
#include <openssl/rsa.h>
#include <openssl/bn.h>
#include <stdlib.h>
#include <integer_math.h>
#include <sha1.h>

#define MAX_KEY_SIZE 4096

BSL_error 
bigint_print(bigint *a, int int_max){
    short int i;
    
    for(i =0; i <= int_max; i++){
        printf("%d = %08x\n ", i, a->half_word[i]);
    }
    return BSL_OK;
}
saveRsaData(FILE *writeptr, RSA *prsa){
    unsigned char e_string[MAX_KEY_SIZE];
    int e_size, i;
    int n_size;
    unsigned char n_string[MAX_KEY_SIZE];
    unsigned char d_string[MAX_KEY_SIZE];
    unsigned char p_string[MAX_KEY_SIZE];
    unsigned char q_string[MAX_KEY_SIZE];
    unsigned char dmp1_string[MAX_KEY_SIZE];
    unsigned char dmq1_string[MAX_KEY_SIZE];
    unsigned char iqmp_string[MAX_KEY_SIZE];
    int d_size, p_size, q_size;
    int dmp1_size, dmq1_size, iqmp_size;

    e_size = BN_bn2bin(prsa->e, e_string);
    for( i =0 ; i< e_size; i++){
        printf("in save e[%d] = %02x\n", i, e_string[i]);
    }
    fprintf(writeptr, "\n%d\n", e_size);
    for( i =0 ; i< e_size; i++){
        fprintf(writeptr, "%02x ", e_string[i]);
    }
    n_size = BN_bn2bin(prsa->n, n_string);
    for( i =0 ; i< n_size; i++){
        printf("in save n[%d] = %02x\n", i, n_string[i]);
    }
    fprintf(writeptr, "\n%d\n", n_size);
    for( i =0 ; i< n_size; i++){
        fprintf(writeptr, "%02x ", n_string[i]);
    }
    d_size = BN_bn2bin(prsa->d, d_string);
    fprintf(writeptr, "\n%d\n", d_size);
    for( i =0 ; i< d_size; i++){
        fprintf(writeptr, "%02x ", d_string[i]);
    }
    p_size = BN_bn2bin(prsa->p, p_string);
    fprintf(writeptr, "\n%d\n", p_size);
    for( i =0 ; i< p_size; i++){
        fprintf(writeptr, "%02x ", p_string[i]);
    }
    q_size = BN_bn2bin(prsa->q, q_string);
    fprintf(writeptr, "\n%d\n", q_size);
    for( i =0 ; i< q_size; i++){
        fprintf(writeptr, "%02x ", q_string[i]);
    }
    dmp1_size = BN_bn2bin(prsa->dmp1, dmp1_string);
    fprintf(writeptr, "\n%d\n", dmp1_size);
    for( i =0 ; i< dmp1_size; i++){
        fprintf(writeptr, "%02x ", dmp1_string[i]);
    }
    dmq1_size = BN_bn2bin(prsa->dmq1, dmq1_string);
    fprintf(writeptr, "\n%d\n", dmq1_size);
    for( i =0 ; i< dmq1_size; i++){
        fprintf(writeptr, "%02x ", dmq1_string[i]);
    }
    iqmp_size = BN_bn2bin(prsa->iqmp, iqmp_string);
    fprintf(writeptr, "\n%d\n", iqmp_size);
    for( i =0 ; i< iqmp_size; i++){
        fprintf(writeptr, "%02x ", iqmp_string[i]);
    }
    
    
}

readRsaData(FILE *readptr, RSA *prsa){
    unsigned char e_string[MAX_KEY_SIZE], n_string[MAX_KEY_SIZE];
    int e_size, n_size, i;
    int d_size, p_size, q_size;
    unsigned char d_string[MAX_KEY_SIZE], p_string[MAX_KEY_SIZE], q_string[MAX_KEY_SIZE];
    unsigned char dmp1_string[MAX_KEY_SIZE];
    unsigned char dmq1_string[MAX_KEY_SIZE];
    unsigned char iqmp_string[MAX_KEY_SIZE];
    int dmp1_size, dmq1_size, iqmp_size;

    
    fscanf(readptr, "%d", &e_size);
    for( i =0 ; i< e_size; i++){
        fscanf(readptr, "%02x", &(e_string[i]));
    }
    prsa->e = BN_bin2bn(e_string, e_size, prsa->e);

    fscanf(readptr, "%d", &n_size);
    for( i =0 ; i< n_size; i++){
        fscanf(readptr, "%02x", &(n_string[i]));
    }
    prsa->n = BN_bin2bn(n_string, n_size, prsa->n);
        
    
    fscanf(readptr, "%d", &d_size);
    for( i =0 ; i< d_size; i++){
        fscanf(readptr, "%02x", &(d_string[i]));
    }
    prsa->d = BN_bin2bn(d_string, d_size, prsa->d);
    

    fscanf(readptr, "%d", &p_size);
    for( i =0 ; i< p_size; i++){
        fscanf(readptr, "%02x", &(p_string[i]));
    }
    prsa->p = BN_bin2bn(p_string, p_size, prsa->p);
    

    fscanf(readptr, "%d", &q_size);
    for( i =0 ; i< q_size; i++){
        fscanf(readptr, "%02x", &(q_string[i]));
    }
    prsa->q = BN_bin2bn(q_string, q_size, prsa->q);
    
    
    fscanf(readptr, "%d", &dmp1_size);
    for( i =0 ; i< dmp1_size; i++){
        fscanf(readptr, "%02x", &(dmp1_string[i]));
    }
    prsa->dmp1 = BN_bin2bn(dmp1_string, dmp1_size, prsa->dmp1);
    
    fscanf(readptr, "%d", &dmq1_size);
    for( i =0 ; i< dmq1_size; i++){
        fscanf(readptr, "%02x", &(dmq1_string[i]));
    }
    prsa->dmq1 = BN_bin2bn(dmq1_string, dmq1_size, prsa->dmq1);
    
    fscanf(readptr, "%d", &iqmp_size);
    for( i =0 ; i< iqmp_size; i++){
        fscanf(readptr, "%02x", &(iqmp_string[i]));
    }
    prsa->iqmp = BN_bin2bn(iqmp_string, iqmp_size, prsa->iqmp);
    


    
}
int main(int argc, char **argv){
    RSA *prsa;
    int equal = 0;
    bigint n, m, e, o;
    bigint a;
    int iter;
    int size, newsize, i, e_size, n_size;
    int num_bits, num_bytes, num_word, max_long, int_max, data_size;
    unsigned char e_string[4096];
    unsigned char n_string[4096];
    unsigned char hash_data[20];
    unsigned char *data;
    unsigned char *padded_data;
    unsigned char customResult[4096];
    unsigned char *sign;
    unsigned char *verify;
    unsigned char input_string[2048];
    int inputLen = 20;
    SHA1Context sha;
    /* cert types */
    unsigned long certpublickey[128];
    unsigned long certsign[128];
    unsigned long certexponent;
    
    FILE *rsawriteptr;
    FILE *rsareadptr;
    FILE *rsaptr;
    int j;
    
    prsa = RSA_new();
    
    num_bits = 4096;
    num_bytes = num_bits/8;
    data_size = 512;
    prsa = RSA_generate_key(num_bits, 3, NULL, NULL);

    rsaptr = fopen("rsadata", "w");
    rsawriteptr = fopen("rsabindata", "w");
    RSA_print_fp(rsaptr, prsa, 0);
        
    saveRsaData(rsawriteptr, prsa);
    fclose(rsawriteptr);

    free(prsa);
    
    prsa = RSA_new();
    
    num_bits = 4096;
        
    rsareadptr = fopen("rsabindata", "r");
    readRsaData(rsareadptr, prsa);



    size = RSA_size(prsa);
    sign = (unsigned char *) malloc(4096);
    verify = (unsigned char *) malloc(4096);
    
    data = (unsigned char *)malloc(4096);
    padded_data = (unsigned char *) malloc(4096);
    for(iter = 0; iter < 10; iter++){
        for (i =0; i < data_size; i+=4){
            *((unsigned long *) (data + i)) = (unsigned long) rand();
        }

        /* compute SHA-1 */
        SHA1Reset(&sha);
        SHA1Input(&sha, data, data_size);
        SHA1Result(&sha, hash_data);
    
        for( i = 0; i < 20; i++){
            printf("hash = %02x\n", hash_data[i]);
        }
        RSA_padding_add_PKCS1_type_2(padded_data, data_size, hash_data, 20);
        
        /*print padded data */
        for(i = 0; i < data_size; i++){
            printf("padded = %02x\n", padded_data[i]);
        } 

            
        /* zero the sign */
        for (i = 0; i <  size; i++){
            sign[i] = 0;
        }
        size = RSA_private_encrypt(data_size, padded_data, sign, prsa, RSA_NO_PADDING);
        printf("size of encrypted = %d\n", size);
        /* zero the result */
        for(i = 0; i < size; i++){
            verify[i]  = 0;
        }
        newsize = RSA_public_decrypt(RSA_size(prsa), sign, verify, prsa, RSA_NO_PADDING);
        printf("size of decrypted = %d\n", newsize);
        for(i =0; i< data_size; i++){
            printf("verify [%d] = %02x padded_data = %02x\n", i, verify[i], padded_data[i]);
        }

        /* call mod_exp from integer_math.h */
    
        /* initialise bigint */

        e_size = BN_bn2bin(prsa->e, e_string);

        for( i =0 ; i< e_size; i++){
            printf("e[%d] = %02x\n", i, e_string[i]);
        }


        n_size = BN_bn2bin(prsa->n, n_string);

        for( i =0 ; i< n_size; i++){
            printf("n[%d] = %02x\n", i, n_string[i]);
        }

        num_word = num_bits/WORD_SIZE;
        max_long = num_word + 1;
        int_max = (4*max_long) - 1;
        charstring_to_bigint(&m, sign, data_size, int_max);
        charstring_to_bigint(&n, n_string, n_size, int_max);
        charstring_to_bigint(&e, e_string, e_size, int_max);
/*
        printf("m\n");
        bigint_print(&m, int_max);
        printf("n\n");
        bigint_print(&n, int_max);
        printf("e\n");
        bigint_print(&e, int_max);
    
*/
        bigint_to_certint(certsign, num_word, &m, int_max);
        bigint_to_certint(certpublickey, num_word, &n, int_max);
        bigint_to_certint(&certexponent, 1, &e, int_max);

        bsl_rsa_verify(customResult, certsign, certpublickey, &certexponent, num_bits);
        /* print custom result of decryption */
        equal = 0;
        j= 0;
        /* print output*/
        for(i=data_size- 20; i < data_size; i++){
            printf(" input = %02x custom = %02x\n", padded_data[i], customResult[i]);
            if(hash_data[j] != customResult[i]){
                equal = 1;
            }
            j++;
        }
        if(equal ==0){
            printf("OK PASS test %d\n", iter);
        }
        else {
            printf("NOT OK FAIL test %d\n", iter);
        }
    }

    for(i =0 ; i< inputLen; i++){
        input_string[i] = i;
    }


    charstring_to_bigint(&a, input_string, inputLen, int_max);
    bigint_print(&a, int_max);
    RSA_free(prsa);
    free(sign);
    free(verify);
    free(data);
    free(padded_data);
    return 0;
}