Thread: /0 char within a char array?

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    /0 char within a char array?

    I am working on converting this C++ code into C so that I can optimize it with the CUDA architecture. Long story short I am running into problems with strings.

    The PBKDF2 spec section I am having trouble with is below
    Code:
    where
    
         U_1 = PRF (P, S || INT (i)) ,
         U_2 = PRF (P, U_1) ,
         ...
         U_c = PRF (P, U_{c-1}) .
    
         Here, INT (i) is a four-octet encoding of the integer i, mostsignificant octet first.
    So in the first round given a salt of "mysalt" the resulting 'key' passed to the HMAC function should be "mysalt0001" except the numbers 0001 are characters (and the char 0 is the same thing as'\0') which cases the end of the string prematurly. This worked fine with the C++ implementation using strings that accepted the '\0' and did not truncate the string. In my C implementation the char array is passed to HMAC and the trailing char's are lost.

    I tried using other values, like '0' which didnt work because its value was not actually 0. Thank you for any help.

    Some of the code.
    Code:
         char U[14+5];  // my test salt is 14 characters
         char appended[] = {(char)0, (char)0, (char)0, (char)i, '\0'};
    
         strcpy(U,S);
         for (j=0;j<5;++j)
         {
              U[14+j] = appended[j];
         }
    Then later HMAC is called...
    Code:
         HMAC(U,P);
    HMAC's header...
    Code:
    void HMAC(char* text, char* key)
    I have verified and tested my versions of HMAC and SHA1
    hmac_sha1.c
    Last edited by bryon; 05-02-2009 at 08:46 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can have null characters in your arrays of characters, if you treat them as that: arrays of charaters, and not as strings. That means no using string functions, and passing the actual size of the array along to whatever function uses it.

    So stop treating them as strings, and start treating them as arrays of characters of a specified size, and you should be fine.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2
    Thank you!

    That and a few corrected wrong turns made it all work.
    Also, thanks for your quick reply, I've been working feverishly since your post and it all works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

Tags for this Thread