【问题标题】:Wrapping blake hash function C implementation into Python using ctypes module, simple python ctypes testvector script also included使用 ctypes 模块将 blake 哈希函数 C 实现包装到 Python 中,还包括简单的 python ctypes testvector 脚本
【发布时间】:2012-04-30 05:32:22
【问题描述】:

我正在尝试使用 C 中的 python ctypes 制作简单的库 blake 哈希函数包装器。但最初只是为了测试我的简单 C 辅助函数是否可以正常工作,我编写了小的 python 脚本 blake 哈希函数测试向量。这个小脚本出现了我的问题。我在几个小时内偶然发现解决这个问题。通过使用这个小助手测试向量脚本,我总是得到意外的“blake 512 usage”的 64 字节输出值。我意识到,我的问题来自我在尝试返回 64 个字节的数量(并且应该是准确的)时编写的 c 辅助函数,以便稍后通过使用这个小的 python ctypes 脚本测试向量来使用。

我使用的纯 C 实现源是直接从 NIST Blake Submission for Optimiezed 32bit Processor 下载的,文件名为 blake_opt32.c 和 blake_opt32.h 。并且可以在http://csrc.nist.gov/groups/ST/hash/sha-3/Round3/documents/Blake_FinalRnd.zip这里下载

这是我的简单 C 辅助函数,供以后使用 python ctypes 调用。

#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'

因此,我将不胜感激对我上面的辅助 C 函数进行任何更正,以便稍后使用这个小的 Python 脚本测试向量调用,以便得到预期的输出值,非常感谢您! 顺便说一下,上面是更新的代码。

【问题讨论】:

    标签: python c hash ctypes


    【解决方案1】:

    在函数BitSequence * bl(char *input)的末尾,返回值output是一个局部变量,函数退出后就不存在了。

    【讨论】:

    • 那么,返回值应该是全局变量吗?
    • 只从 gcc 警告消息中解决了一点问题,但是通过使用上面的 testvector 仍然多次检测到意外值......?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 2022-11-20
    • 1970-01-01
    • 2017-10-06
    • 2018-10-02
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多