【发布时间】: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