| 1 |
import os |
| 2 |
import sys |
| 3 |
import platform |
| 4 |
|
| 5 |
from cffi import FFI |
| 6 |
|
| 7 |
IS_WIN = sys.platform == 'win32' |
| 8 |
if IS_WIN: |
| 9 |
BLAKE2_USE_SSE = True |
| 10 |
extra_compile_args = [] |
| 11 |
define_macros = [('__SSE2__', '1')] |
| 12 |
elif platform.machine().startswith('x86'): |
| 13 |
BLAKE2_USE_SSE = True |
| 14 |
extra_compile_args = ['-msse2'] |
| 15 |
define_macros = [] |
| 16 |
else: |
| 17 |
BLAKE2_USE_SSE = False |
| 18 |
extra_compile_args = [] |
| 19 |
define_macros = [] |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
blake_cdef = """ |
| 24 |
#define BLAKE_OUTBYTES ... |
| 25 |
#define BLAKE_SALTBYTES ... |
| 26 |
#define BLAKE_BLOCKBYTES ... |
| 27 |
#define BLAKE_PERSONALBYTES ... |
| 28 |
#define BLAKE_KEYBYTES ... |
| 29 |
|
| 30 |
typedef struct { |
| 31 |
uint8_t digest_length; |
| 32 |
uint8_t key_length; |
| 33 |
uint8_t fanout; |
| 34 |
uint8_t depth; |
| 35 |
uint32_t leaf_length; |
| 36 |
uint8_t node_depth; |
| 37 |
// node_offset is a bit special |
| 38 |
uint8_t inner_length; |
| 39 |
uint8_t salt[...]; |
| 40 |
uint8_t personal[...]; |
| 41 |
...; |
| 42 |
} blake_param ; |
| 43 |
|
| 44 |
typedef struct { |
| 45 |
uint8_t last_node; |
| 46 |
...; |
| 47 |
} blake_state ; |
| 48 |
|
| 49 |
int blake_init_param( blake_state *S, const blake_param *P ); |
| 50 |
int blake_update( blake_state *S, const uint8_t *in, uint64_t inlen ); |
| 51 |
int blake_final( blake_state *S, uint8_t *out, uint8_t outlen ); |
| 52 |
|
| 53 |
void* addressof_node_offset(blake_param *S); |
| 54 |
|
| 55 |
void store32(void* dst, uint32_t w); |
| 56 |
void store48(void* dst, uint64_t w); |
| 57 |
void store64(void* dst, uint64_t w); |
| 58 |
""" |
| 59 |
|
| 60 |
blake2b_source = """ |
| 61 |
#include "impl/blake2.h" |
| 62 |
#include "impl/blake2-impl.h" |
| 63 |
|
| 64 |
#define BLAKE_OUTBYTES BLAKE2B_OUTBYTES |
| 65 |
#define BLAKE_SALTBYTES BLAKE2B_SALTBYTES |
| 66 |
#define BLAKE_BLOCKBYTES BLAKE2B_BLOCKBYTES |
| 67 |
#define BLAKE_PERSONALBYTES BLAKE2B_PERSONALBYTES |
| 68 |
#define BLAKE_KEYBYTES BLAKE2B_KEYBYTES |
| 69 |
|
| 70 |
typedef blake2b_state blake_state; |
| 71 |
typedef blake2b_param blake_param; |
| 72 |
|
| 73 |
#define blake_init_param blake2b_init_param |
| 74 |
#define blake_update blake2b_update |
| 75 |
#define blake_final blake2b_final |
| 76 |
|
| 77 |
void* addressof_node_offset(blake_param *S) { |
| 78 |
return &(S->node_offset); |
| 79 |
} |
| 80 |
""" |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
_libdir = 'impl' |
| 86 |
if BLAKE2_USE_SSE: |
| 87 |
sourcesB=[os.path.join(_libdir, 'blake2b.c'), ] |
| 88 |
sourcesS=[os.path.join(_libdir, 'blake2s.c'), ] |
| 89 |
else: |
| 90 |
sourcesB=[os.path.join(_libdir, 'blake2b-ref.c'), ] |
| 91 |
sourcesS=[os.path.join(_libdir, 'blake2s-ref.c'), ] |
| 92 |
|
| 93 |
blake2b_ffi = FFI() |
| 94 |
blake2b_ffi.cdef(blake_cdef) |
| 95 |
blake2b_ffi.set_source( |
| 96 |
'_blake2b_cffi', blake2b_source, |
| 97 |
sources=sourcesB, |
| 98 |
include_dirs=[_libdir], |
| 99 |
extra_compile_args=extra_compile_args, |
| 100 |
define_macros=define_macros, |
| 101 |
) |
| 102 |
|
| 103 |
def _replace_b2s(src): |
| 104 |
for b, s in (('blake2b', 'blake2s'), |
| 105 |
('BLAKE2B', 'BLAKE2S')): |
| 106 |
src = src.replace(b, s) |
| 107 |
return src |
| 108 |
|
| 109 |
blake2s_ffi = FFI() |
| 110 |
blake2s_ffi.cdef(blake_cdef) |
| 111 |
blake2s_ffi.set_source( |
| 112 |
'_blake2s_cffi', _replace_b2s(blake2b_source), |
| 113 |
sources=sourcesS, |
| 114 |
include_dirs=[_libdir], |
| 115 |
extra_compile_args=extra_compile_args, |
| 116 |
define_macros=define_macros, |
| 117 |
) |
| 118 |
|
| 119 |
if __name__ == '__main__': |
| 120 |
os.chdir(os.path.dirname(__file__)) |
| 121 |
blake2b_ffi.compile() |
| 122 |
blake2s_ffi.compile() |