Thread: How to convert char array of 64 bytes to long long int

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    19

    How to convert char array of 64 bytes to long long int

    I am using openssl API for encryption in my project. Currently i have a char key[64] =' some hex value' which i need to convert to long long int so that i can pass it to AES_key_wrap() function in openssl. I have used strtol() and sscanf() but both of them result in gving = 4738338 as result... not sure what i am missing here... any help is highly appreciated..

    Thanks
    Prashanth

    code snippet
    u8 DEK[64];
    long long test;

    sprintf(&DEK, "%s%s", aes,xts);
    or

    test = strtoul(DEK, NULL, 0);

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Give us an example of what DEK is when you call strtoul and what the corresponding long long int you get is, and what you expect it to be. Are you sure you want to use 0 in strtoul when you're using base 16 ("some hex value")?

    Also, what are you trying to pass to AES_key_wrap because the only function I see of that name doesn't even HAVE long long int in the prototype.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are confusing bytes with bits.


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

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    Thanks ledow for the answer. Here is my DEK key which is 64 bytes(512 bit) which i want to pass to AES_Key_wrap() function.
    DEK = 016211c7f60824a8ec5df54737a08ad0a11b2e451160601a94 ccd53555a335

    Regarding strtoul(), i found eg on net and it said '0' would mean starting from beginning so i used it... is this wrong?

    "long long int" - I was trying to experiment to see if i can convert DEK into long long int and noting to do with AES key wrap function ...

    For my case, i would require unsigned long rd_key[4 *(AES_MAXNR + 1)]; to be enabled but again this is enabled/disabled using #ifdef AES_LONG (is this compile time parameter? i couldn't find any header file which has option to enable/disable)

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That's a hex number, so you have to use 16 as the final arg to your strtol() function. However, I'd guess that this number will never fit into even an unsigned long long (using strtoull), and a test confirms:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    int main(int argc, const char *argv[]) {
    	char key[]="016211c7f60824a8ec5df54737a08ad0a11b2e451160601a94ccd53555a335";
    	unsigned long long x = strtoull(key, NULL, 16);
    	if (x == ULLONG_MAX) puts("nope!");
    	return 0;
    }
    nope!

    So you've got something wrong. I can't find a reference to an AES_key_wrap(), but if you mean AES_wrap_key(), methinks you have misunderstood the arguments:

    Code:
    int AES_wrap_key(AES_KEY *key, const unsigned char *iv,
                    unsigned char *out,
                    const unsigned char *in, unsigned int inlen);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I've already said what the problem is: 64 bytes will not fit in 64 bits.
    Currently i have a char key[64]
    ...
    Code:
    u8 DEK[64];
    long long test;
    'test' is not sixty four BYTES in size.


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

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    sorry for confusing between bits and bytes... i got that ...

    AES_wrap_key() is the correct function ... if i want this into an array, then how do i assign this to AES_KEY *key.
    Code:
    struct aes_key_st {
    #ifdef AES_LONG
        unsigned long rd_key[4 *(AES_MAXNR + 1)];
    #else
        unsigned int rd_key[4 *(AES_MAXNR + 1)];
    #endif
        int rounds;
    };
    typedef struct aes_key_st AES_KEY;
    where max size for storage offered is 480 bits but i need to store 512 boits ... am i missing something here? Do i need to implement my own AES Key wrap algo?

    -Prashanth

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pkumarn
    AES_wrap_key() is the correct function ... if i want this into an array, then how do i assign this to AES_KEY *key.
    It might help if you read the documentation for AES_wrap_key. I don't have it at hand (and never used this library either), but it should explain what you are looking for. If you don't understand it, post it here.

    Quote Originally Posted by pkumarn
    where max size for storage offered is 480 bits but i need to store 512 boits
    AES has a key length of at most 256 bits, so I do not understand what is up with 480 bits or 512 bits, unless you're really trying to use Rijndael rather than AES. If you have an arbitrarily long string that you want to use as a key, use a cryptographic hash like SHA-256 to obtain the key with the desired number of bits. If you already have 512 bits that were randomly generated for use as a key, just truncate it to 256 bits.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    When i do math on the AES structure, i see it can support upto 480bits (correct me if i am wrong)
    unsigned long rd_key[4 *(AES_MAXNR + 1)];

    Considering AES supports only 256 bits, how do i convert my string value into 256 bit number...

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    We can't do the math because you haven't told us what AES_MAXNR is.

    At any rate, your key is 64 chars, each two of which represent the hex value of a byte; so it is 32 bytes or 256 bits. Your question is how do you turn this hex string into the actual 256 bits that you need. Here's the basic idea:

    EDIT: Upon actually running my original code, I had to make a couple of changes. Also note that I had to add two characters to your dek string (it was only 62 chars long).
    Code:
    #include <stdio.h>
    
    int main() {
        char dek[] = "016211c7f60824a8ec5df54737a08ad0a11b2e451160601a94ccd53555a335ab";
        unsigned char byte[32];
        unsigned x;
        int n;
    
        for (n = 0; n < 32; n++) {
            sscanf(dek+n*2, "%2x", &x);
            byte[n] = x;
        }
    
        printf("%s\n", dek);
    
        for (n = 0; n < 32; n++)
            printf("%02x", (unsigned)byte[n]);
    
        return 0;
    }
    Last edited by oogabooga; 03-06-2012 at 12:22 AM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    As per my openssl code base, AES_MAXNR is #define AES_MAXNR 14. So based on this math, i get 408 bits.

    From your code i see you are just copying to array but i am looking at how to assign Int i = DEK (this is pseudo code and not actual code syntax) as i need to pass this to AES_KEY struct in AES_wrap_key() function

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Given:
    unsigned long rd_key[4 *(AES_MAXNR + 1)]
    with AES_MAXNR equal to 14 and assuming unsigned long is 32 bits, that's 32 * 4 * (14 + 1) = 1920 bits.
    But the fact is that you're looking for a 256 bit key from your string which is what I showed you how to do. You haven't specified how you'd like to stuff those bytes into the rd_key array.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    All you really need is a hex string to binary converter. You pas in the address of the string and the address of the thing where you want the data to be put.
    Internally it's just a loop converting two hexadecimal characters at once into the corresponding byte value.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    May be i am confusing things... just to make it in simple words. How do i pass my DEK[64] to AES_wrap_key() as first parameter...

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    @iMalc: Flight of the Conchords!

    @pkumarn: You take the bytes that I showed you how to get and put them into the first 8 entries of rd_key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C]Convert Char* to Unsigned Long
    By MaSSaSLaYeR in forum C Programming
    Replies: 10
    Last Post: 11-17-2011, 06:46 AM
  2. Convert two 32-bit longs to one 64-bit long long
    By chriscapitolo in forum C Programming
    Replies: 2
    Last Post: 04-27-2011, 01:43 PM
  3. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  4. cannot convert parameter 1 from 'char *' to 'long'
    By MrLucky in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2006, 07:31 AM
  5. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM

Tags for this Thread