On Tue, 2004-05-04 at 03:03, Ramon Amat Jimenez wrote:
> Using aes-128-encryption with a testvector from NIST for encryption does 
> not match! I have been mostly using code from example 
> ./openssl-0.9.7d/crypto/evp/evp_test.c to conduct this test.
> 
> #cipher:key:iv:plaintext:ciphertext:0/1(decrypt/encrypt)  (from NIST 
> test vectors, encrypt)
> #AES-128-CBC:00000000000000000000000000000000:00000000000000000000000000000000: 
> 
> 00000000000000000000000000000000:8A05FC5E095AF4848A08D328D3688E3D:1
> 
> key: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> iv:    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> plain =>:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> cipher=>: 66 e9 4b d4 ef 8a 2c 3b 88 4c fa 59 ca 34 2b 2e   <===== does 
> not match !!!
> cipher =>:8a 05 fc 5e 09 5a f4 84 8a 08 d3 28 d3 68 8e 3d   <==== expected
> 
> Decrypting 66 e9 4b d4 ef 8a 2c 3b 88 4c fa 59 ca 34 2b 2e <=====  with 
> key, iv as stated above
> does give 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00!
> 
> Can somebody explain me that ? I have attached below the code used.
> Kind regards


I tried your sample with the NIST test vectors found at:
http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf and it
worked fine.  Are you sure the test vectors you are using are correct? 
Where did you get those vectors?

Test Vector:

   char line[] =
"2b7e151628aed2a6abf7158809cf4f3c:000102030405060708090a0b0c0d0e0f:6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710:7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7:";
 


Result:

key:
0000 2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c
iv:
0000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
plain =>:
0000 6b c1 be e2 2e 40 9f 96 e9 3d 7e 11 73 93 17 2a
0010 ae 2d 8a 57 1e 03 ac 9c 9e b7 6f ac 45 af 8e 51
0020 30 c8 1c 46 a3 5c e4 11 e5 fb c1 19 1a 0a 52 ef
0030 f6 9f 24 45 df 4f 9b 17 ad 2b 41 7b e6 6c 37 10
cipher=>:
0000 76 49 ab ac 81 19 b2 46 ce e9 8e 9b 12 e9 19 7d
0010 50 86 cb 9b 50 72 19 ee 95 db 11 3a 91 76 78 b2
0020 73 be d6 b8 e3 c1 74 3b 71 16 e6 9e 22 22 95 16
0030 3f f1 ca a1 68 1f ac 09 12 0e ca 30 75 86 e1 a7
 
--------------------
key:
0000 2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c
iv:
0000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
cipher <=:
0000 76 49 ab ac 81 19 b2 46 ce e9 8e 9b 12 e9 19 7d
0010 50 86 cb 9b 50 72 19 ee 95 db 11 3a 91 76 78 b2
0020 73 be d6 b8 e3 c1 74 3b 71 16 e6 9e 22 22 95 16
0030 3f f1 ca a1 68 1f ac 09 12 0e ca 30 75 86 e1 a7
plain  <=:
0000 6b c1 be e2 2e 40 9f 96 e9 3d 7e 11 73 93 17 2a
0010 ae 2d 8a 57 1e 03 ac 9c 9e b7 6f ac 45 af 8e 51
0020 30 c8 1c 46 a3 5c e4 11 e5 fb c1 19 1a 0a 52 ef
0030 f6 9f 24 45 df 4f 9b 17 ad 2b 41 7b e6 6c 37 10
plain <=:
0000 6b c1 be e2 2e 40 9f 96 e9 3d 7e 11 73 93 17 2a
0010 ae 2d 8a 57 1e 03 ac 9c 9e b7 6f ac 45 af 8e 51
0020 30 c8 1c 46 a3 5c e4 11 e5 fb c1 19 1a 0a 52 ef
0030 f6 9f 24 45 df 4f 9b 17 ad 2b 41 7b e6 6c 37 10


> ========================================================
> // test_ex.cpp
> #include <stdio.h>
> #include <string.h>
> #include <openssl/err.h>
> #include <openssl/evp.h>
> #include <openssl/conf.h>
> 
> static void hexdump(FILE *f,const char *title,const unsigned char *s,int 
> l);
> static int convert(unsigned char *s);
> static unsigned char *ustrsep(char **p,const char *sep);
> static char *sstrsep(char **string, const char *delim);
> int do_encrypt(const unsigned char *plain, int pn, const unsigned char 
> *key, int kn, const unsigned char *iv, int in);
> int do_decrypt(const unsigned char *cipher, int cn, const unsigned char 
> *key, int kn, const unsigned char *iv, int in);
> 
> 
> int main(int argc, char *argv[])
> {      OpenSSL_add_all_algorithms();
>      // Modified Test Vector from NIST
>    char line[] = 
> "00000000000000000000000000000000:00000000000000000000000000000000:00000000000000000000000000000000:8A05FC5E095AF4848A08D328D3688E3D:";
>  
> 
> 
>    // Modified Test Vector for decryption with the wrong 66e9...
>    // char line[] = 
> "00000000000000000000000000000000:00000000000000000000000000000000:00000000000000000000000000000000:66e94bd4ef8a2c3b884cfa59ca342b2e:";
>      
> 
>    char *p = line;
>      unsigned char* key = ustrsep(&p,":");
>    unsigned char* iv = ustrsep(&p,":");
>    unsigned char* plain = ustrsep(&p,":");
>    unsigned char* cipher = ustrsep(&p,":");
>  
>    int kn = convert(key);
>    int in = convert(iv);
>    int pn = convert(plain);
>    int cn = convert(cipher);      
>    do_encrypt(plain, pn, key, kn, iv, in);
>   // what we expect        hexdump(stdout, "cipher =>:", cipher, cn);
>      printf ("\n--------------------\n");
>  
>    do_decrypt(cipher, cn, key, kn, iv, in);
>      hexdump(stdout, "plain <=:", plain, pn);
>      return 0;
> }
> 
> 
> int do_decrypt(const unsigned char *cipher, int cn, const unsigned char 
> *key, int kn, const unsigned char *iv, int in)
> {
>     unsigned char outbuf[4096];
>     int outl, outl2;
> 
>     EVP_CIPHER_CTX ctx;
>     EVP_CIPHER_CTX_init(&ctx);
> 
>     const EVP_CIPHER *c = EVP_get_cipherbyname("aes-128-cbc");
> 
>     if (kn != c->key_len) {
>         return 0;
>     }
>        if (!EVP_DecryptInit_ex(&ctx, c, NULL, key, iv))
>     {
>         return 0;
>     }
> 
>     EVP_CIPHER_CTX_set_padding(&ctx, 0);
> 
>     if(!EVP_DecryptUpdate(&ctx, outbuf, &outl, cipher, cn))
>     {
>          // Error
>          return 0;
>     }
>                         if(!EVP_DecryptFinal_ex(&ctx, outbuf + outl, 
> &outl2))
>     {
>          // Error
>          return 0;
>     }
>     outl = outl + outl2;
>        hexdump(stdout, "key: " ,key, kn);
>     hexdump(stdout, "iv: "  ,iv, in);
>     hexdump(stdout, "cipher <=: ",cipher, cn);
>     hexdump(stdout, "plain  <=: " ,outbuf, outl);
> 
>     EVP_CIPHER_CTX_cleanup(&ctx);
> 
>     return 1;
>             }
> 
> 
> int do_encrypt(const unsigned char *plain, int pn, const unsigned char 
> *key, int kn,
>          const unsigned char *iv, int in)
> {
>    unsigned char outbuf[4096];
>    int outl, outl2;
>          EVP_CIPHER_CTX ctx;
>    EVP_CIPHER_CTX_init(&ctx);
>      const EVP_CIPHER *c = EVP_get_cipherbyname("aes-128-cbc");
>      if (kn != c->key_len) {
>       return 0;      }
>  
>    if (!EVP_EncryptInit_ex(&ctx, c, NULL, key, iv))
>    {
>        return 0;
>    }
>      EVP_CIPHER_CTX_set_padding(&ctx, 0);
>      if(!EVP_EncryptUpdate(&ctx, outbuf, &outl, plain, pn))
>    {
>         // Error
>     return 0;
>    }
>      if(!EVP_EncryptFinal_ex(&ctx, outbuf + outl, &outl2))
>    {       // Error
>         return 0;
>    }
>    outl = outl + outl2;
>      hexdump(stdout, "key: " ,key, kn);
>    hexdump(stdout, "iv: "  ,iv, in);
>    hexdump(stdout, "plain =>: ",plain, pn);
>    hexdump(stdout, "cipher=>: ",outbuf, outl);
>      EVP_CIPHER_CTX_cleanup(&ctx);
>      return 1;
> }
> 
> 
> static void hexdump(FILE *f,const char *title,const unsigned char *s,int l)
> {
>    int n=0;
> 
>    fprintf(f,"%s",title);
>    for( ; n < l ; ++n)
>    {
>    if((n%16) == 0)
>        fprintf(f,"\n%04x",n);
>    fprintf(f," %02x",s[n]);
>    }
>    fprintf(f,"\n");
> }
> 
> 
> 
> static int convert(unsigned char *s)
> {
>    unsigned char *d;
> 
>    for(d=s; *s; s+=2, ++d) {
>    unsigned int n;
>    if(!s[1]) {
>        fprintf(stderr,"Odd number of hex digits!");
>        exit(-1);
>        }
>    sscanf((char *)s,"%2x",&n);
>    *d=(unsigned char)n;
>    }
>    return s-d;
> }
> 
> static char *sstrsep(char **string, const char *delim)
> {
>    char isdelim[256];
>    char *token = *string;
> 
>    if (**string == 0)
>        return NULL;
> 
>    memset(isdelim, 0, 256);
>    isdelim[0] = 1;
> 
>    while (*delim) {
>        isdelim[(unsigned char)(*delim)] = 1;
>        delim++;
>    }
> 
>    while (!isdelim[(unsigned char)(**string)]) {
>        (*string)++;
>    }
> 
>    if (**string) {
>        **string = 0;
>        (*string)++;
>    }
> 
>    return token;
> }
> 
> static unsigned char *ustrsep(char **p,const char *sep)
> {
>    return (unsigned char *)sstrsep(p,sep);
> }
> 
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [EMAIL PROTECTED]
> Automated List Manager                           [EMAIL PROTECTED]

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to