【问题标题】:OCB implementation issiuesOCB 实施问题
【发布时间】:2015-03-07 00:13:25
【问题描述】:

我尝试了 OCB 算法 http://web.cs.ucdavis.edu/~rogaway/ocb/news/code/ocb_ref.c 的参考实现,在添加了解密数据的打印后,出现了一个缺失的元素。

由于我在 pastebin 上发布的代码的大小 http://pastebin.com/sbNUpzmN

主要功能

  #include <stdio.h>
  #include <stdlib.h>
  #define PLAIN_SIZE 8
  int main() {
    uint8_t zeroes[PLAIN_SIZE];
    uint8_t nonce[12] = {0,};
    uint8_t p[PLAIN_SIZE+8] = {0,};
    uint8_t final[16];
    uint8_t *c;
    unsigned i, next;
    int result;
    for (i=0; i<(PLAIN_SIZE); i++) {
        zeroes[i]=2;
        p[i]=5;
        }
    p[i+8]=5;
    printf("Nonece: %d\n",NONCEBYTES);
    /* Encrypt and output RFC vector */
    c = malloc(22400);
    next = 0;
    for (i=0; i<PLAIN_SIZE; i++) {
        nonce[10] = i;
        ocb_encrypt(c+next, zeroes, nonce, zeroes, i, zeroes, i);
        next = next + i + TAGBYTES;
        ocb_encrypt(c+next, zeroes, nonce, zeroes, 0, zeroes, i);
        next = next + i + TAGBYTES;
        ocb_encrypt(c+next, zeroes, nonce, zeroes, i, zeroes, 0);
        next = next + TAGBYTES;
    }
    nonce[10] = 0;
    ocb_encrypt(final, zeroes, nonce, c, next, zeroes, 0);
    if (NONCEBYTES == 12) {
        printf("AEAD_AES_%d_OCB_TAGLEN%d Output: ", KEYBYTES*8, TAGBYTES*8);
        for (i=0; i<TAGBYTES; i++) printf("%02X", final[i]);  printf("\n");
    }

    /* Decrypt and test for all zeros and authenticity */
    result = ocb_decrypt(p, zeroes, nonce, c, next, final, TAGBYTES);
    if (result) { printf("FAIL\n"); return 0; }
    next = 0;
    for (i=0; i<PLAIN_SIZE; i++) {
        nonce[10] = i;
        result = ocb_decrypt(p, zeroes, nonce, zeroes, i, c+next, i+TAGBYTES);
        if (result || memcmp(p,zeroes,i)) { printf("FAIL\n"); return 0; }
        next = next + i + TAGBYTES;
        result = ocb_decrypt(p, zeroes, nonce, zeroes, 0, c+next, i+TAGBYTES);
        if (result || memcmp(p,zeroes,i)) { printf("FAIL\n"); return 0; }
        next = next + i + TAGBYTES;
        result = ocb_decrypt(p, zeroes, nonce, zeroes, i, c+next, TAGBYTES);
        if (result || memcmp(p,zeroes,i)) { printf("FAIL\n"); return 0; }
        next = next + TAGBYTES;
    }
    for (i=0; i<(PLAIN_SIZE); i++) printf("%d",  zeroes[i]); printf("\n");
    for (i=0; i<(PLAIN_SIZE); i++) printf("%d", p[i]); printf("\n");
    free(c);


    return 0;
}

输出产量

->gcc  -W -g -lcrypto ocb_rev.c && ./a.out
Nonece: 12
AEAD_AES_128_OCB_TAGLEN128 Output: 0DA35760F29E327625FBC0071E18E330
22222222
22222225

因此数组的最后一个元素还没有被写入,但是认证已经通过了。

有什么想法吗?

【问题讨论】:

    标签: c security encryption ocb-mode


    【解决方案1】:

    代码有几个问题:\

    1. 函数ocb_encrypt()需要相应的头文件#included。
    2. 变量result未使用
    3. 变量p 已设置但未使用。

    鉴于上述代码问题

    1. OP 在编译期间关闭了警告或忽略了警告
    2. 编译器将(由于ocb_encrypt() 没有原型)
      • 假设输入都是整数
      • 假设返回值,如果有的话,是整数

    ocb_encrypt() 的编译器假设可能是主要原因 输出不正确。

    如果编译器警告全部启用然后更正,导致编译干净,那么问题将是可调试的,因为它是任何人都猜测编译器传递给ocb_encrypt() 以及ocb_encrypt() 函数是如何传递的对传递错误类型的参数做出反应

    【讨论】:

    • 你在说什么?包括什么?你真的读过我写的吗? “由于我在 pastebin pastebin.com/sbNUpzmN 上发布的代码的大小”,当函数在 main 上方的同一文件中声明时,不需要原型。您还可以在我的帖子“gcc -W -g -lcrypto ocb_rev.c && ./a.out”中看到编译标志,所以我没有关闭任何警告。 p 被使用了....你在拖钓之前真的注意帖子怎么样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2013-09-16
    • 1970-01-01
    • 2016-11-28
    • 2015-05-11
    • 1970-01-01
    相关资源
    最近更新 更多