Cのpythonctypesを使用して単純なライブラリblakeハッシュ関数ラッパーを作成しようとしています。しかし、最初に単純なCヘルパー関数が正しく機能するかどうかをテストするために、小さなpythonスクリプトblakeハッシュ関数テストベクトルを作成しました。そして、私の問題はこの小さなスクリプトで発生しました。私はこの問題を解決するために何時間もつまずいています。この小さなヘルパーテストベクトルスクリプトを使用すると、「blake512usage」の64バイトの出力値を常に予期していませんでした。私の問題は、この小さなpython ctypesスクリプトテストベクトルを使用して後で使用される64バイトの量(正確である必要があります)を返そうとしたときに作成したcヘルパー関数に起因することを認識しています。
私が使用した純粋なC実装ソースは、ファイルblake_opt32.cおよびblake_opt32.hという名前でNIST Blake Submission forOptimiezed32bitプロセッサから直接ダウンロードされました。また、 http://csrc.nist.gov/groups/ST/hash/sha-3/Round3/documents/Blake_FinalRnd.zipからダウンロードできます。
これが、後でpythonctypesを使用して呼び出される単純なCヘルパー関数です。
#include <stdio.h>
#include "blake_opt32.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <stdint.h>
BitSequence msg[65];
size_t strlcpy(unsigned char *dst, const char *src, size_t siz)
{
unsigned char *d = dst;
const char *s = src;
size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0) {
while (--n != 0) {
if ((*d++ = *s++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
BitSequence * bl(char *input)
{
BitSequence output[65];
BitSequence msg[sizeof(output)];
int dInt;
memset(output,0,sizeof(output));
dInt = strlen(input);
if (dInt > 0xffff){
exit( 1);
}
BitSequence data[dInt];
memset(data, 0, dInt);
strlcpy(data, input, sizeof(data));
DataLength dLen =1152;
Hash(512, data, dLen, output);
int x;
for (x=0;x<64;++x){
printf("%02X",output[x]);
}
memcpy(msg,output,sizeof(output));
//here the problem araised, when trying to return unsigned char or BitSequence value, the unexpected output of small python scipt test vectors value is detected
return msg;
}
そして、単純な小さなpythonスクリプトテストベクトルがここにあります。出力をファイルの任意のテキストにリダイレクトしてこの小さなスクリプトを試してみてください。後で予期しない値がキャッチされます。
from ctypes import *
from string import printable
from itertools import permutations
from random import *
d = CDLL('blake.dll') #edit here your own dll or .so library
d.bl.restype = c_char_p
print '[+] Simple Test-vectors Blake hash function\n'
s = SystemRandom()
for x in permutations(printable):
p = ''.join(map(str,x))
q = list(p)
s.shuffle(q)
r= d.bl(''.join(map(str,q)))
if ((len(r)*2) != 0x80):
print '\n[-] Not persistent value of 64 bytes was detected : %d'% len(r)
w = r.encode('hex')
print w, '-->', len(w)
print '\n'
elif ((len(r)*2) == 0x80):
print '\n',len(r), '\n',r.encode('hex')
print '\n'
したがって、出力値が期待されるように、この小さなPythonスクリプトテストベクトルを使用して後で呼び出されるように、上記のヘルパーC関数を修正していただければ幸いです。ありがとうございました!ちなみに上記は更新されたコードです。