Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding elements to array of structs in C

Tags:

arrays

c

struct

So for a crypto class, we are implementing Mental Poker with the SRA protocol. We are using the openSSL library for BIGNUM http://www.openssl.org/docs/crypto/bn.html and I am having an issue when adding encrypted cards to an array of BIGNUM structs.

#include <openssl/bn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

BIGNUM* encryptedDeck[52];
BIGNUM* bobHand[5];
BIGNUM* aliceHand[5];

int main(int argc, char* argv[]){
    int lcv = 0;
    BIGNUM *P,*Q,*N,*alpha,*alphaPrime,*beta,*betaPrime,*temp,*Pminus;
    BN_CTX *ctx = BN_CTX_new();
    P = BN_new(); //same for all BIGNUM pointers

    BN_generate_prime(P,512,1,NULL,NULL,NULL,NULL);
    BN_generate_prime(Q,512,1,NULL,NULL,NULL,NULL);
    BN_generate_prime(alpha,128,1,NULL,NULL,NULL,NULL); //Alice's key [ gcd(alpha,P) = 1 ]
    BN_generate_prime(beta,128,1,NULL,NULL,NULL,NULL);  //Bob's key. Same rule as alice's

    BN_one(temp);  //set temp to be a BIGNUM equivalent to integer 1
    BN_sub(Pminus,P,temp);

    BN_mul(N,P,Q,ctx);

    temp = BN_new();
    BIGNUM *encryptedCard = BN_new();
    for(lcv; lcv < 52; lcv++){
        BN_bin2bn(deck[lcv],strlen(deck[lcv]),temp);
        BN_mod_exp(encryptedCard,temp,beta,P,ctx);    //encrypt temp, store in encryptedCard

        printf("ec: %s\n\n",BN_bn2dec(encryptedCard));  //prints *correct numbers

        encryptedDeck[lcv] = encryptedCard;  //store cards in the array of encrypted cards
    }

    printf("00: %s\n\n",BN_bn2dec(encryptedDeck[0]));
    printf("01: %s\n\n",BN_bn2dec(encryptedDeck[1]));
    //...
    printf("40: %s\n\n",BN_bn2dec(encryptedDeck[40]));
}

The print statement in the forloop prints 52 different values (1 for each card.)

I intended for each of those values to simply be added to the array encryptedDeck, but when I check the values after exiting the loop, they are all equivalent to the 52nd card. So for some reason, each index in the array is being overwritten for every new card? Or something weird like that.

Is there some obvious thing that I am missing when dealing with arrays of structs? The only thing that I would think of right off the bat is something about the array not being initialized with enough space or something.

I think that it is possible to convert the BIGNUM values into char* and store them in an array that way, but I tried to avoid this because I don't know how big the pointers need to be, or even have a guess of their range. something like

char encryptedDeck[52][something ridiculous];

I omitted some code, but I believe this should still compile (provided you have the openssl library, and link it when compiling) and fill in the BN_new() for all the other BIGNUM pointers that I didn't initialize.

like image 340
AChrapko Avatar asked Dec 07 '25 02:12

AChrapko


1 Answers

Just above your for-loop:

BIGNUM *encryptedCard = BN_new();

You never change what this points to. You just keep dropping new data into the same card, then save that card into the current slot of the for-loop.

My surmise is to move the card inside the loop.

for(lcv; lcv < 52; lcv++){
    BIGNUM *encryptedCard = BN_new();
    BN_bin2bn(deck[lcv],strlen(deck[lcv]),temp);
    BN_mod_exp(encryptedCard,temp,beta,P,ctx);
    printf("ec: %p: %s\n\n",encryptedCard, BN_bn2dec(encryptedCard));
    encryptedDeck[lcv] = encryptedCard;
}

Noted: I don't use this crypto library, so I can't tell you if temp needs the same locality in the loop, but seems like where your problem may be.

like image 198
WhozCraig Avatar answered Dec 08 '25 14:12

WhozCraig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!