Writing and reading a binary file

152 Views Asked by At

I am trying to to write crypt->public_key->data into a binary file. If I use size of sizeof(unsigned int) as the second argument in fwrite(), it works. However, it is declared as unsigned short * type in the header file. I am not sure why it behaves as such. Also, I face problem while writing public key and reading them as well. Although I use the size of the exactly same type in the fwrite() and fread().

Edit: It appears that the size of my crypt->public_key->data is incorrect as pointed out by usr2564301.

*I'm not sure whether I need to cite the source of this code or not. But I'll add the URL here: https://github.com/Varad0612/The-McEliece-Cryptosystem

Code from a matrix.h

typedef struct matrix
{
   int rows;             //number of rows.
   int cols;             //number of columns.
   unsigned short *data;
}*bin_matrix;

This is the code from mceliece.c

//Initialize the mceliece cryptosystem
mcc mceliece_init(int n0, int p, int w, int t)
{
    FILE *publicKey, *privateKey;
    mcc crypt;  
    crypt = (mcc)safe_malloc(sizeof(struct mceliece));  
    //crypt->code = qc_mdpc_init(n0, p, w, t);
    //crypt->public_key = generator_matrix(crypt->code);

    //printf("%d\n",crypt->public_key->rows);
    //printf("%d\n",crypt->public_key->cols);

    //Write public key into a binary file
    /*publicKey = fopen("PublicKey.bin", "wb");
    privateKey = fopen("PrivateKey.bin", "wb");

    if(privateKey != NULL){
        fwrite(crypt->code->row, n0*p*sizeof(unsigned short), n0 * p, privateKey);
        fclose(privateKey); 
    }
    else{
        printf("Unable to write private key\n");    
    }

    //Write public key into a binary file
    if(publicKey != NULL){
        fwrite(crypt->public_key->data, p*p*n0*sizeof(unsigned short), crypt->public_key->rows*crypt->public_key->cols, publicKey);
        fclose(publicKey);
    }
    else{
        printf("Unable to write public key\n");
    }*/

    //Read private key from a binary file
    crypt->code = (mdpc)safe_malloc(sizeof(struct qc_mdpc));
    crypt->code->n0 = n0;
    crypt->code->p = p;
    crypt->code->w = w;
    crypt->code->t = t;
    crypt->code->n = n0 * p;
    crypt->code->r = p;
    crypt->code->k = (n0 - 1) * p;
    crypt->code->row = (unsigned short*)calloc(n0 * p, sizeof(unsigned short));
    privateKey = fopen("PrivateKey.bin", "rb");
    if(privateKey != NULL){
        fread(crypt->code->row, p*n0*sizeof(unsigned short), p*n0, privateKey);
        fclose(privateKey); 
    }
    else
        printf("Unable to read private key\n");

    //Read public key from a binary file
    /*crypt->public_key = (bin_matrix)safe_malloc(sizeof(struct matrix));
    crypt->public_key->data = (unsigned short*)safe_malloc(p*p*n0*sizeof(unsigned short));
    crypt->public_key->rows = p;
    crypt->public_key->cols = n0*p; 
    publicKey = fopen("PublicKey.bin", "rb");
    if(publicKey != NULL){
        fread(crypt->public_key->data, p*p*n0*sizeof(unsigned short), crypt->public_key->rows*crypt->public_key->cols, publicKey);
        fclose(publicKey);  
    }
    else{
        printf("Unable to read public key\n");
    }*/

    printf("Successful\n");
    //printf("mceliece generated...\n");
    return crypt;
}
1

There are 1 best solutions below

12
On

There is a confusion in our reading statement:

fread(crypt->code->row, p*n0*sizeof(unsigned short), p*n0, privateKey);

This will attempt to read p*n0 elements, each with a size of p*n0*sizeof(unsigned short) bytes. If the file is not larger than the allocated size, you will be lucky as fread would not attempt to write beyond the end of the allocated block.

You should instead write:

size_t nread = fread(crypt->code->row, sizeof(unsigned short), p * n0, privateKey);
if (nread == p * n0) {
    /* private key was read successfully */
} else {
    /* file is too short, only nread words were read */
}