AES CTR128 - Openssl

This is a discussion on AES CTR128 - Openssl ; Good morning, I want to test the AES CTR128 encryption of openSSL but infortunately i could not find one. I have tested one but still i got errors in the output of the program. here is the code i tested ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: AES CTR128

  1. AES CTR128

    Good morning,
    I want to test the AES CTR128 encryption of openSSL but infortunately i
    could not find one. I have tested one but still i got errors in the output
    of the program. here is the code i tested (i found it on the internet), plz
    can anyone provide a really simple test program for the counter mode of AES
    in openSSL, or simply see what s the problem in that source code. Thank you
    very much for consideration.

    #include
    #include
    #include < openssl
    /aes.h>


    #define KEY_LENGTH 16
    #define TEXT_LENGTH 36
    #define COUNTER_LENGTH 16

    void mirror(const unsigned char *in, unsigned char *out,
    const unsigned long length, const AES_KEY *key,
    unsigned char *counter, unsigned int *num) {

    unsigned int n;
    unsigned long l=length;
    unsigned char tmp[AES_BLOCK_SIZE];

    memset(tmp, 0xFF, AES_BLOCK_SIZE);
    }

    int main(void)
    {
    unsigned char key[KEY_LENGTH + 1] = "9dfe5ea4c3f84ae3";
    unsigned char plaintext1[(TEXT_LENGTH / 2) + 1] =
    "123456789012345678";
    unsigned char plaintext2[(TEXT_LENGTH / 2) + 1] =
    "876543210987654321";
    unsigned char ciphertext[TEXT_LENGTH + 1];
    unsigned char plaintext[TEXT_LENGTH + 1];
    unsigned char ecounter[COUNTER_LENGTH + 1] = "6543210987654321";
    unsigned char dcounter[COUNTER_LENGTH + 1] = "6543210987654321";
    AES_KEY k;
    int num;
    int retval;


    retval = AES_set_encrypt_key(key, strlen(key) * 8, &k);
    if (retval < 0) {
    printf("couldn't set encrypt key: %d\n", retval);
    return -1;
    }

    num = 0;
    AES_ctr128_encrypt(plaintext1, ciphertext, strlen(plaintext1),
    &k, ecounter, &num);

    mirror(NULL, NULL, 0, NULL, NULL, NULL);

    AES_ctr128_encrypt(plaintext2, ciphertext + strlen(plaintext1),
    strlen(plaintext2), &k, ecounter, &num);

    num = 0;
    AES_ctr128_encrypt(ciphertext, plaintext,
    strlen(plaintext1) + strlen(plaintext2),
    &k, dcounter, &num);

    retval = strncmp(plaintext, plaintext1, strlen(plaintext1));
    if (retval != 0) {
    printf("error\n");
    return -1;
    }
    retval = strncmp(plaintext + strlen(plaintext1), plaintext2,
    strlen(plaintext2));
    if (retval != 0) {
    printf("error\n");
    return -1;
    }

    printf("success\n");

    return 0;
    }


  2. Re: AES CTR128

    Try this: http://www.grandville.net/pmwiki.php...SL/AES-CBC-EVP

    Openssl EVP API makes AES encryption/decryption.

    On Mon, May 26, 2008 at 5:54 PM, Mohamed Amine Ourmech
    wrote:

    > Good morning,
    > I want to test the AES CTR128 encryption of openSSL but infortunately i
    > could not find one. I have tested one but still i got errors in the output
    > of the program. here is the code i tested (i found it on the internet), plz
    > can anyone provide a really simple test program for the counter mode of AES
    > in openSSL, or simply see what s the problem in that source code. Thank you
    > very much for consideration.
    >
    > #include
    > #include
    > #include < openssl
    > /aes.h>
    >
    >
    > #define KEY_LENGTH 16
    > #define TEXT_LENGTH 36
    > #define COUNTER_LENGTH 16
    >
    > void mirror(const unsigned char *in, unsigned char *out,
    > const unsigned long length, const AES_KEY *key,
    > unsigned char *counter, unsigned int *num) {
    >
    > unsigned int n;
    > unsigned long l=length;
    > unsigned char tmp[AES_BLOCK_SIZE];
    >
    > memset(tmp, 0xFF, AES_BLOCK_SIZE);
    > }
    >
    > int main(void)
    > {
    > unsigned char key[KEY_LENGTH + 1] = "9dfe5ea4c3f84ae3";
    > unsigned char plaintext1[(TEXT_LENGTH / 2) + 1] =
    > "123456789012345678";
    > unsigned char plaintext2[(TEXT_LENGTH / 2) + 1] =
    > "876543210987654321";
    > unsigned char ciphertext[TEXT_LENGTH + 1];
    > unsigned char plaintext[TEXT_LENGTH + 1];
    > unsigned char ecounter[COUNTER_LENGTH + 1] = "6543210987654321";
    > unsigned char dcounter[COUNTER_LENGTH + 1] = "6543210987654321";
    > AES_KEY k;
    > int num;
    > int retval;
    >
    >
    > retval = AES_set_encrypt_key(key, strlen(key) * 8, &k);
    > if (retval < 0) {
    > printf("couldn't set encrypt key: %d\n", retval);
    > return -1;
    > }
    >
    > num = 0;
    > AES_ctr128_encrypt(plaintext1, ciphertext, strlen(plaintext1),
    > &k, ecounter, &num);
    >
    > mirror(NULL, NULL, 0, NULL, NULL, NULL);
    >
    > AES_ctr128_encrypt(plaintext2, ciphertext + strlen(plaintext1),
    > strlen(plaintext2), &k, ecounter, &num);
    >
    > num = 0;
    > AES_ctr128_encrypt(ciphertext, plaintext,
    > strlen(plaintext1) + strlen(plaintext2),
    > &k, dcounter, &num);
    >
    > retval = strncmp(plaintext, plaintext1, strlen(plaintext1));
    > if (retval != 0) {
    > printf("error\n");
    > return -1;
    > }
    > retval = strncmp(plaintext + strlen(plaintext1), plaintext2,
    > strlen(plaintext2));
    > if (retval != 0) {
    > printf("error\n");
    > return -1;
    > }
    >
    > printf("success\n");
    >
    > return 0;
    > }
    >
    >



    --
    Karim


  3. Re: AES CTR128

    EVP_DecryptFinal_ex:bad decrypt


    On Tue, May 27, 2008 at 10:58 AM, karim Bendadda wrote:

    > Try this: http://www.grandville.net/pmwiki.php...SL/AES-CBC-EVP
    >
    > Openssl EVP API makes AES encryption/decryption.
    >
    >
    > On Mon, May 26, 2008 at 5:54 PM, Mohamed Amine Ourmech <
    > m.ourmech@gmail.com> wrote:
    >
    >> Good morning,
    >> I want to test the AES CTR128 encryption of openSSL but infortunately i
    >> could not find one. I have tested one but still i got errors in the output
    >> of the program. here is the code i tested (i found it on the internet), plz
    >> can anyone provide a really simple test program for the counter mode of AES
    >> in openSSL, or simply see what s the problem in that source code. Thank you
    >> very much for consideration.
    >>
    >> #include
    >> #include
    >> #include < openssl
    >> /aes.h>
    >>
    >>
    >> #define KEY_LENGTH 16
    >> #define TEXT_LENGTH 36
    >> #define COUNTER_LENGTH 16
    >>
    >> void mirror(const unsigned char *in, unsigned char *out,
    >> const unsigned long length, const AES_KEY *key,
    >> unsigned char *counter, unsigned int *num) {
    >>
    >> unsigned int n;
    >> unsigned long l=length;
    >> unsigned char tmp[AES_BLOCK_SIZE];
    >>
    >> memset(tmp, 0xFF, AES_BLOCK_SIZE);
    >> }
    >>
    >> int main(void)
    >> {
    >> unsigned char key[KEY_LENGTH + 1] = "9dfe5ea4c3f84ae3";
    >> unsigned char plaintext1[(TEXT_LENGTH / 2) + 1] =
    >> "123456789012345678";
    >> unsigned char plaintext2[(TEXT_LENGTH / 2) + 1] =
    >> "876543210987654321";
    >> unsigned char ciphertext[TEXT_LENGTH + 1];
    >> unsigned char plaintext[TEXT_LENGTH + 1];
    >> unsigned char ecounter[COUNTER_LENGTH + 1] = "6543210987654321";
    >> unsigned char dcounter[COUNTER_LENGTH + 1] = "6543210987654321";
    >> AES_KEY k;
    >> int num;
    >> int retval;
    >>
    >>
    >> retval = AES_set_encrypt_key(key, strlen(key) * 8, &k);
    >> if (retval < 0) {
    >> printf("couldn't set encrypt key: %d\n", retval);
    >> return -1;
    >> }
    >>
    >> num = 0;
    >> AES_ctr128_encrypt(plaintext1, ciphertext, strlen(plaintext1),
    >> &k, ecounter, &num);
    >>
    >> mirror(NULL, NULL, 0, NULL, NULL, NULL);
    >>
    >> AES_ctr128_encrypt(plaintext2, ciphertext + strlen(plaintext1),
    >> strlen(plaintext2), &k, ecounter, &num);
    >>
    >> num = 0;
    >> AES_ctr128_encrypt(ciphertext, plaintext,
    >> strlen(plaintext1) + strlen(plaintext2),
    >> &k, dcounter, &num);
    >>
    >> retval = strncmp(plaintext, plaintext1, strlen(plaintext1));
    >> if (retval != 0) {
    >> printf("error\n");
    >> return -1;
    >> }
    >> retval = strncmp(plaintext + strlen(plaintext1), plaintext2,
    >> strlen(plaintext2));
    >> if (retval != 0) {
    >> printf("error\n");
    >> return -1;
    >> }
    >>
    >> printf("success\n");
    >>
    >> return 0;
    >> }
    >>
    >>

    >
    >
    > --
    > Karim





    --
    Karim


+ Reply to Thread