#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Cryptography with C


    Hi folks,

    I've been using C in university for simulation (physics) and now I'm studding a new area . I decided to start with XTEA , and using the Wiki code (XTEA - Wikipedia, the free encyclopedia) and I'm having some technical difficulties.

    1 - How to properly convert a string like "teste" to a uint32_t (in python there is unicode(str, "utf-8") , but I didn't found a C equivalent)
    2 - I saw that the argument is v[2] , after converting I should passing the array in blocks of 2 right?

    Or anybody knows a full example to how implementing that?

    Thanks in advance
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,179
    Rep Power
    9398
    You're missing out on an important part of XTEA: the fact that it uses the Feistel cipher. Feistel forms a larger algorithm and XTEA fills a key role in it.

    Feistel:
    1. Breaks the input into two halves
    2. Each "character" in both halves is fed through some other algorithm (like XTEA) to get new "characters"
    3. Those are then reassembled into the output

    To answer the questions,
    1. Any way you want - only the raw numbers matter. To begin with you could use byte values. More advanced would be using sets of four bytes at once (to fill the whole 32-bit space available). Later you can deal with character encoding - UTF-8 and the like; C doesn't have anything built-in to support encodings but there's plenty of free code on the internet you can use.
    2. v[2] means you're supposed to pass an array with two values. You don't pass in an array of blocks (=arrays) of numbers but a one-dimensional array of two numbers. Also, when in doubt, check what the code does.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    I see,so XTEA would be the "round function"? I can understand the logic but I'm having difficulties to be able to write down the code.
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,179
    Rep Power
    9398
    Yeah, and if you didn't realize (it took me a minute when I read through it) their use of "round function" does not mean numeric rounding, it means the function applied during the round of processing.

    What do you have so far?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    My problem is to actually having a string i.e char message = "Test" and char key[] = "key",how to pass as argument of the function like, encrypt(message,key) , since the arguments are uint
  10. #6
  11. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,179
    Rep Power
    9398
    Like I said earlier,
    Originally Posted by requinix
    To begin with you could use byte values. More advanced would be using sets of four bytes at once (to fill the whole 32-bit space available). Later you can deal with character encoding - UTF-8 and the like; C doesn't have anything built-in to support encodings but there's plenty of free code on the internet you can use.
    What's your current implementation of everything besides the message and key parts? I mean the function(s) for Feistel and XTEA. And whatever calls those functions, like main().
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    So , my first thought was doing something like

    Code:
    int main(){
        char message[] = "Hi!",key[] = "KEY";
        unsigned int *mesInt = (unsigned int*) (&message),*keyInt = (unsigned int*)(&key);
    
        encrypt(mesInt,keyInt);
    
        for(i=0;i<3;i++){
    	printf("%ud ",mesInt);
        }
    }
    but since it gives a different output at time ,it doesn't look a properly implementation ...
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,403
    Rep Power
    1871
    All your program proves is that your OS implements ASLR, since it prints out different values each time (for your pointer).

    > bar.c:12:3: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned int *’ [-Wformat]

    > printf("
    ",mesInt);
    This is wrong on two counts.
    1. You don't have 3 unsigned ints, you have 3 chars.
    2. You're not subscripting your pointer anyway.
    Last edited by salem; July 12th, 2014 at 11:39 PM.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by salem
    All your program proves is that your OS implements ASLR, since it prints out different values each time (for your pointer).

    > bar.c:12:3: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned int *’ [-Wformat]

    > printf("
    ",mesInt);
    This is wrong on two counts.
    1. You don't have 3 unsigned ints, you have 3 chars.
    2. You're not subscripting your pointer anyway.
    Yeah, before compiling it seems that wasn't right , but like I said I don't know how to properly do it.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    Yes, I don't know how to convert correctly the types
  20. #11
  21. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,403
    Rep Power
    1871
    Originally Posted by wikipedia
    XTEA is a 64-bit block Feistel network with a 128-bit key and a suggested 64 rounds.
    > char message[] = "Hi!",key[] = "KEY";
    To satisfy the input requirements, you need at least this.
    Otherwise, you end up encrypting unknown data with an unknown key.
    Code:
    char message[8] = "Hi!",key[16] = "KEY";
    > Yes, I don't know how to convert correctly the types
    The result is in your message array.
    I suggest using the %d format to print each element of message.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    First I want to thank you guys for the patience. But when the string is bigger than 8 chars , I have to break it into blocks right?
  24. #13
  25. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,403
    Rep Power
    1871
    Correct.
    You also need to read the specification to find out how to 'pad' the last block to a multiple of 8 bytes. Is it just zeros, or something else?

    Likewise, you need to make sure the key is exactly 128 bits to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    Thank you guys for the help

IMN logo majestic logo threadwatch logo seochat tools logo