我尝试了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
因此,数组的最后一个元素尚未写入,但验证已通过。
任何想法发生了什么?
答案 0 :(得分:0)
代码存在一些问题:\
ocb_encrypt()
需要相应的头文件#included。result
未使用p
已设置但未使用。考虑到代码的上述问题
ocb_encrypt()
的原型)
ocb_encrypt()
的编译器假设可能是主要原因
输出不正确。
如果编译器警告全部启用然后更正,导致编译干净,那么问题就是可调试的,因为它是任何人都猜测编译器传递给ocb_encrypt()
的内容以及{{} 1}}函数响应传递错误类型的参数