| 1 |
import os |
| 2 |
import sys |
| 3 |
|
| 4 |
from cffi import FFI |
| 5 |
|
| 6 |
|
| 7 |
ffi = FFI() |
| 8 |
ffi.cdef(""" |
| 9 |
typedef struct { |
| 10 |
unsigned int rate; |
| 11 |
...; |
| 12 |
} KeccakWidth1600_SpongeInstance; |
| 13 |
|
| 14 |
typedef struct { |
| 15 |
KeccakWidth1600_SpongeInstance sponge; |
| 16 |
unsigned int fixedOutputLength; |
| 17 |
unsigned char delimitedSuffix; |
| 18 |
...; |
| 19 |
} Keccak_HashInstance; |
| 20 |
|
| 21 |
typedef enum { SUCCESS = ..., FAIL = ..., BAD_HASHLEN = ... } HashReturn; |
| 22 |
typedef int... DataLength; |
| 23 |
typedef unsigned char BitSequence; |
| 24 |
|
| 25 |
HashReturn Keccak_HashInitialize_SHA3_224(Keccak_HashInstance*); |
| 26 |
HashReturn Keccak_HashInitialize_SHA3_256(Keccak_HashInstance*); |
| 27 |
HashReturn Keccak_HashInitialize_SHA3_384(Keccak_HashInstance*); |
| 28 |
HashReturn Keccak_HashInitialize_SHA3_512(Keccak_HashInstance*); |
| 29 |
HashReturn Keccak_HashInitialize_SHAKE128(Keccak_HashInstance*); |
| 30 |
HashReturn Keccak_HashInitialize_SHAKE256(Keccak_HashInstance*); |
| 31 |
|
| 32 |
HashReturn Keccak_HashUpdate(Keccak_HashInstance *, const BitSequence *, DataLength); |
| 33 |
HashReturn Keccak_HashFinal(Keccak_HashInstance *, BitSequence *); |
| 34 |
HashReturn Keccak_HashSqueeze(Keccak_HashInstance *hashInstance, BitSequence *data, DataLength databitlen); |
| 35 |
""") |
| 36 |
|
| 37 |
_libdir = os.path.join(os.path.dirname(__file__), 'kcp') |
| 38 |
|
| 39 |
if sys.byteorder == 'big': |
| 40 |
|
| 41 |
keccakOpt = 32 |
| 42 |
elif sys.maxsize > 2**32: |
| 43 |
keccakOpt = 64 |
| 44 |
else: |
| 45 |
keccakOpt = 32 |
| 46 |
|
| 47 |
ffi.set_source( |
| 48 |
'_sha3_cffi', |
| 49 |
("#define KeccakOpt %d\n" % keccakOpt) + |
| 50 |
""" |
| 51 |
/* we are only interested in KeccakP1600 */ |
| 52 |
#define KeccakP200_excluded 1 |
| 53 |
#define KeccakP400_excluded 1 |
| 54 |
#define KeccakP800_excluded 1 |
| 55 |
|
| 56 |
#if KeccakOpt == 64 |
| 57 |
/* 64bit platforms with unsigned int64 */ |
| 58 |
typedef uint64_t UINT64; |
| 59 |
typedef unsigned char UINT8; |
| 60 |
#endif |
| 61 |
|
| 62 |
/* inline all Keccak dependencies */ |
| 63 |
#include "kcp/KeccakHash.h" |
| 64 |
#include "kcp/KeccakSponge.h" |
| 65 |
#include "kcp/KeccakHash.c" |
| 66 |
#include "kcp/KeccakSponge.c" |
| 67 |
#if KeccakOpt == 64 |
| 68 |
#include "kcp/KeccakP-1600-opt64.c" |
| 69 |
#elif KeccakOpt == 32 |
| 70 |
#include "kcp/KeccakP-1600-inplace32BI.c" |
| 71 |
#endif |
| 72 |
""", |
| 73 |
include_dirs=[_libdir], |
| 74 |
) |
| 75 |
|
| 76 |
if __name__ == '__main__': |
| 77 |
os.chdir(os.path.dirname(__file__)) |
| 78 |
ffi.compile() |