C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-02-2009, 08:42 PM   #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.
bryon is offline   Reply With Quote
Old 05-02-2009, 08:49 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,259
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 05-02-2009, 11:03 PM   #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.
bryon is offline   Reply With Quote
Reply

Tags
'/0', char, char array, pbkdf2, strings

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why return malloc'd char array not work, but local char array does? 6tr6tr C++ Programming 3 11-17-2008 12:36 PM
code condensing bcianfrocca C++ Programming 4 09-07-2005 09:22 AM
code help required Yobbo C Programming 9 09-02-2005 11:15 PM
Creating 2D arrays on heap sundeeptuteja C++ Programming 6 08-16-2002 11:44 AM
Strings are V important... NANO C++ Programming 15 04-14-2002 11:57 AM


All times are GMT -6. The time now is 01:26 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22