AES_XCBC算法的实现

Implemetation of AES_XCBC algorithm

我正在尝试在基于 C++ 的应用程序中包含不同的 IPSec 算法。因为我想使用此处描述的 AES-XCBC 算法:http://www.faqs.org/rfcs/rfc3566.html

我查看了 OpenSSL 的 API,但没有找到适合 AES-XCBC 的,例如 OpenSSL 维基中描述的 AES_GCM:https://www.openssl.org/docs/man1.1.0/man3/EVP_EncryptInit_ex.html

但是能够从 OpenSwan 库中找到这个测试程序:https://github.com/xelerance/Openswan/blob/6055fc6fa444f3d5b89ad0f7d3ec277eedaa9282/lib/libcrypto/libaes/test_main_mac.c

我修改了程序以使用 RFC 3566 中算法描述 link 中的第二个测试向量,例如:

 Key (K)        : 000102030405060708090a0b0c0d0e0f

 Message (M)    : 000102

 AES-XCBC-MAC   : 5b376580ae2f19afe7219ceef172756f
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include "aes.h"
#include "aes_xcbc_mac.h"
#define STR "Hola guasssso c|mo estais ...012"  
void print_hash(const u_int8_t *hash) {
        printf("%08x %08x %08x %08x\n",
                        *(u_int32_t*)(&hash[0]),
                        *(u_int32_t*)(&hash[4]),
                        *(u_int32_t*)(&hash[8]),
                        *(u_int32_t*)(&hash[12]));
}
int main(int argc, char *argv[]) {
        aes_block key= { 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f };
        u_int8_t  hash[16];
        unsigned char str[3] = {0x00, 0x01, 0x02};
        aes_context_mac ctx;
        AES_xcbc_mac_set_key(&ctx, (u_int8_t *)&key, sizeof(key));
        AES_xcbc_mac_hash(&ctx, (u_int8_t *) &str, sizeof(str), hash);
        print_has(hash);
        str[2]='x';
        AES_xcbc_mac_hash(&ctx,(u_int8_t *) &str, sizeof(str), hash);
        print_hash(hash);
        return 0;
}

为上述输入打印的输出是:

b176b661 d551b66f 64889d60 18e71c76

这与 RFC 3566 中预期的 mac 不同。我是否正确使用了 API?这个算法有没有参考实现?

另一个库 libtomcrypt 确实实现了 AES_XCBC。该程序可以修改为使用 xcbc_memory 函数形式 libtomcrypt and it's test program

#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <tomcrypt.h> 
#define STR "Hola guasssso c|mo estais ...012"  
void print_hash(const u_int8_t *hash) {
        printf("%08x %08x %08x %08x\n",
                        *(u_int32_t*)(&hash[0]),
                        *(u_int32_t*)(&hash[4]),
                        *(u_int32_t*)(&hash[8]),
                        *(u_int32_t*)(&hash[12]));
}
int main(int argc, char *argv[]) {
        uint8_t key[16]= { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
                           0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f  };
        uint8_t hash[16];
        int idx,err;
        unsigned char str[3] = {0x00, 0x01, 0x02};
        unsigned long taglen = 16;
        register_cipher( & aes_desc); 
        if ((idx = find_cipher("aes")) == -1) {
           if ((idx = find_cipher("rijndael")) == -1) {
             printf("idx:%d\n",idx);
             return CRYPT_NOP;
         }
        }
        if ((err = xcbc_memory(idx, key, 16, str, 3,hash, &taglen)) != CRYPT_OK) {
          printf("err:%d\n",idx);
          return err;
       }

        print_has(hash);
        str[2]='x';
        if ((err = xcbc_memory(idx, key, 16, str, 3,hash, &taglen)) != CRYPT_OK) {
          printf("err:%d\n",idx);
          return err;
        }
        print_hash(hash);
        return 0;
}