On 4/2/2012 5:09 PM, Theodore Tolstoy wrote:
Hi!

There is a widely known and used AES implementation in C by "Niyaz PK" for
encryption/decryption:
http://www.hoozi.com/posts/advanced-encryption-standard-aes-implementation-in-cc-with-comments-part-1-encryption/
.

It seems to implement AES-{128,192,256} ECB mode of
encryption/decryption(?). Am I wrong?

Is it possible to use OpenSSL to achieve equivalent results?

That is one lousy demo implementation!

State is in global variables, only shows encryption of a single 128 bit
(16 byte) block with a single fixed key.

Besides the one in OpenSSL, there are plenty of better free AES implementations, including the one in Rijmen and Daemen's original submission to the AES competition, and the
optimized implementation by Gladman.

Note: If this is a homework assignment, the following can't be used, because I have done a few things that your teacher will probably not like, and he is probably reading this list too!

To do the same in openssl, the code would be something like this:

#include <openssl/evp.h>

void main()
{
    int i;
    int Nr;
    size_t keySiz;
    size_t blockSiz;
    size_t outLen;
    int outStep;
    const EVP_CIPHER *cipher;
    EVP_CIPHER_CTX ctx;
    unsigned char key[EVP_MAX_KEY_LENGTH];
    unsigned char in[EVP_MAX_BLOCK_LENGTH];
    unsigned char out[EVP_MAX_BLOCK_LENGTH];

    // Receive the length of key here.
    for (;;) {
        printf("Enter the length of Key(128, 192 or 256 only): ");
        scanf("%d",&Nr);
        switch (Nr) {
           case 128:
              cipher = EVP_aes_128_ecb();
              break;
           case 192:
              cipher = EVP_aes_192_ecb();
              break;
           case 192:
              cipher = EVP_aes_256_ecb();
              break;
           default:
              printf("Sorry, %d is not a valid answer, try again\n", Nr);
              continue; // Ask again
       }
       break; // Leave the loop
    }

    keySiz = EVP_CIPHER_key_length(cipher);
    blockSiz = EVP_CIPHER_block_size(cipher);

// Part 1 is for demonstrative purpose. The key and plaintext are given in the program itself.
//     Part 1: ********************************************************
    // The array temp stores the key.
    // The array temp2 stores the plaintext.
const unsigned char temp[16] = {0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07 ,0x08 ,0x09 ,0x0a ,0x0b ,0x0c ,0x0d ,0x0e ,0x0f}; const unsigned char temp2[16]= {0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff};

    // Copy the key and plaintext
    memcpy(key, temp, keySiz);
    memcpy(in, temp2, blockSiz);


//           *********************************************************
// Uncomment Part 2 if you need to read Key and PlainText from the keyboard.
//     Part 2: ********************************************************
/*
    //Clear the input buffer
    flushall();
    //Recieve the Key from the user
    printf("Enter the Key in hexadecimal: ");
    for(i=0;i<keySiz;i++) {
        scanf("%x",&key[i]);
    }
    printf("Enter the PlainText in hexadecimal: ");
    for(i=0;i<blockSiz;i++) {
        scanf("%x",&in[i]);
    }
*/
//             ********************************************************
   // The KeyExpansion routine must be called before encryption.
    EVP_CIPHER_CTX_init(&ctx);
// Copy the key, set the IV to the key too, as it is not used in ECB mode
    if (!EVP_EncryptInit_Ex(&ctx, cipher, NULL, key, key))
       goto fail1;
// Specify that we will encrypt whole blocks without padding to a larger size
    if (!EVP_CIPHER_CTX_set_padding(&ctx, 0))
       goto fail1;
// The next function calls encrypts the PlainText with the Key using the chosen algorithm.
    // Encrypt in to out
    outLen = 0;
    if (!EVP_EncryptUpdate(&ctx, out, &outStep, in, blockSiz))
       goto fail1;
    outLen += outStep;
    if (!EVP_EncryptFinal_Ex(&ctx, out + outLen, &outStep))
       goto fail1;
    outLen += outStep;
    if (!EVP_CIPHER_CTX_cleanup(&ctx))
       goto fail0;
    // Output the encrypted text.
    printf("\nText after encryption:\n");
    for(i=0;i<outLen;i++) {
        printf("%02x ",out[i]);
    }
    printf("\n\n");
    return 0;
   fail1: ;
(void)EVP_CIPHER_CTX_cleanup(&ctx);
   fail0: ;
    printf("FAILED!");
    return 1;
}



--
Jakob Bohm, CIO, partner, WiseMo A/S. http://www.wisemo.com
Transformervej 29, 2730 Herlev, Denmark. direct: +45 31 13 16 10 <call:+4531131610>
This message is only for its intended recipient, delete if misaddressed.
WiseMo - Remote Service Management for PCs, Phones and Embedded
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to