1

all! I'm running into some difficulty with a school project in which we're implementing the Serpent cipher. The problem is in the function

setKey(unsigned char (&user_key[32]))

I would like to pass a byte array of size 32 to my function, and then have my function do all manner of stuff to the values of that array. My code as I've printed it will not compile, as I get the error -

no known conversion for argument 1 from ‘unsigned char (*)[32]’ to 
‘unsigned char (&)   [32]’

I've looked through a number of similar posts, and none of the solutions I've found seem to lead to my code compiling, or if it compiles, doing what I'd like it to. Unfortunately, I can't remember how I last got my code to compile, but when I did, printing out the values of user_key[] within the function setKey() gave me the following output:

19, 0, 0, 0, 51, 0, 0, 0, 83, 0, 0, 0, 115, 0, 0, 0, 20, 0, 0, 0, 52, 0, 0, 0,
84, 0, 0, 0, 116, 0, 0, 0  

In addition, when it was running, it was exiting with a segfault after printing out the line "This is the end. My only friend, the end." as well as a "stack smashing detected" warning. In short, my code seems to be all sorts of wrong. Any help would be greatly appreciated!!

#include <iostream>
#include <math.h>

using namespace std;

class KeySchedule{

  int ip[64];
  int key_size;
  unsigned long long int k0;
  unsigned long long int k1;
  unsigned long long int k2;
  unsigned long long int k3;

public:

  KeySchedule(){

    /*The initial permutation. To be applied to the plaintext and keys.
      ip = {0, 32, 64, 96, 1, 33, 65, 97, 2, 34, 66, 98, 3, 35, 67, 99,
      4, 36, 68, 100, 5, 37, 69, 101, 6, 38, 70, 102, 7, 39, 71, 103,
      8, 40, 72, 104, 9, 41, 73, 105, 10, 42, 74, 106, 11, 43, 75, 107,
      12, 44, 76, 108, 13, 45, 77, 109, 14, 46, 78, 110, 15, 47, 79, 111,
      16, 48, 80, 112, 17, 49, 81, 113, 18, 50, 82, 114, 19, 51, 83, 115,
      20, 52, 84, 116, 21, 53, 85, 117, 22, 54, 86, 118, 23, 55, 87, 119,
      24, 56, 88, 120, 25, 57, 89, 121, 26, 58, 90, 122, 27, 59, 91, 123,
      28, 60, 92, 124, 29, 61, 93, 125, 30, 62, 94, 126, 31, 63, 95, 127};
    */

    for (int i = 0; i < 127; i++ ){
      ip[i] = (32*i) % 127;
    } 
    ip[127] = 127;

    k3 = 0; 
    k2 = 0; 
    k1 = 0;
    k0 = 0;  
    key_size = -1;
  }

  void setKey (unsigned char (&user_key)[32]){

    for (int i = 0; i<32; i++){
      cout << (int)(user_key)[i] << endl;
    }

    for (int i = 0; i<8; i++){
      k3 ^= (int)(user_key)[i] << (8-i);
      k2 ^= (int)(user_key)[i+8] << (8-i);
      k1 ^= (int)(user_key)[i+16] << (8-i);
      k0 ^= (int)(user_key)[i+24] << (8-i);
    }

  }
};

int main(){

   unsigned char testkey[]= {0x01, 0x02, 0x03, 0x04, 
                 0x05, 0x06, 0x07, 0x08, 
                 0x09, 0x0a, 0x0b, 0x0c, 
                 0x0d, 0x0e, 0x0f, 0x10,
                 0x20, 0x30, 0x40, 0x50, 
                 0x60, 0x70, 0x80, 0x90, 
                 0xa0, 0xb0, 0xc0, 0xd0, 
                 0xe0, 0xf0, 0x00, 0xff};

   cout << "The size of testkey is: " 
        << sizeof(testkey)/sizeof(*testkey) << endl;

   KeySchedule ks = KeySchedule();

   ks.setKey(&testkey);
   cout << "This is the end. My only friend, the end." << endl;
};

1 Answer 1

3

You are passing the address your unsigned char array, i.e. a pointer to unsigned char[32]. You need this:

ks.setKey(testkey);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the help! Now I'm having the same problem when printing the values of user_key, however. Any idea as to why it would be printing 19, 0, 0, 0, 51, 0, 0, 0, ... for the values of the array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.