#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    MD, USA
    Posts
    2
    Rep Power
    0

    C code specific help requested... AES encryption.


    Hello to all experts... Please help! This is a script from AES cryptography C code written by karl malbrain.

    I am most interested in finding what the for loop does. Especially understanding its single line and *in++ and << 24.

    state[idx] = *in++ << 24;

    Hope you can help me. I just have no clue at the moment.

    Thank you very much in advance!



    // encrypt one 128 bit block

    void Encrypt (uchar *in, unsigned *expkey, uchar *out)
    {
    unsigned state[Nb], round, idx;

    for( idx = 0; idx < Nb; idx++ )
    {
    state[idx] = *in++ << 24;
    state[idx] |= *in++ << 16;
    state[idx] |= *in++ << 8;
    state[idx] |= *in++;
    }

    AddRoundKey (state, expkey);

    for( round = 1; round < Nr + 1; round++ ) {
    SubBytes((uchar *)state);
    ShiftRows ((uchar *)state);
    if( round < Nr )
    MixColumns ((uchar *)state);
    AddRoundKey (state, expkey + round * Nb);
    }

    for( idx = 0; idx < Nb; idx++ ) {
    *out++ = state[idx] >> 24;
    *out++ = state[idx] >> 16;
    *out++ = state[idx] >> 8;
    *out++ = state[idx];
    }
    }
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    The '<<' is bit shifting (left vs right, look up which is which). The '*' is pointing to the actual value of a pointer (de-referencing the pointer). As for the '++', that is an incrementing function (add one to the current value), due to precedence (and without runing experiments) I would speculate that it is incrementing the value the pointer is pointing to before the bit shifting (it could also be incrementing the actual pointer, that is what would require the experimentation (or a better memory for precedence)). This sort of stuff is very common in compute intensive code (which encryption/decryption is). Bit shifting is typically done in the register, so is very fast. Most compilers will also be able to do the incrementing (++) in a register as well, though more often it is just a notational convenience for C programmers.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    that line first dereferences the pointer in, and then increments in to point to the next byte. the ' << 24 ' shifts the bits of what in points to 24 to the left. there is also an error in the function, the 2nd parameter is "unsigned *expkey" so it's missing the type.
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    heh ya beat me to it.. i was also debating the operator precedence. im pretty sure from teh context teh pointer is being inc'ed.

    Code:
    #include <stdio.h>
    
    main()
    {
            int x = 0,      m[4] = {1,2,3,4},       *ptr = m;
    
            for(x; x < 4; x++)
            {
                    printf("%d\n", *ptr++ << 1 );
            }
    
    }
    yields
    2
    4
    6
    8

    so it's incrementing the pointer not what it points to. :)
    Last edited by infamous41md; September 25th, 2003 at 01:05 PM.

IMN logo majestic logo threadwatch logo seochat tools logo