File: _blake2_build.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (122 lines) | stat: -rw-r--r-- 3,009 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import sys
import platform

from cffi import FFI

IS_WIN = sys.platform == 'win32'
if IS_WIN:
    BLAKE2_USE_SSE = True
    extra_compile_args = []
    define_macros = [('__SSE2__', '1')]
elif platform.machine().startswith('x86'):
    BLAKE2_USE_SSE = True
    extra_compile_args = ['-msse2']
    define_macros = []
else:
    BLAKE2_USE_SSE = False
    extra_compile_args = []
    define_macros = []
    
    

blake_cdef = """
#define BLAKE_OUTBYTES ...
#define BLAKE_SALTBYTES ...
#define BLAKE_BLOCKBYTES ...
#define BLAKE_PERSONALBYTES ...
#define BLAKE_KEYBYTES ...

typedef struct {
    uint8_t digest_length;
    uint8_t key_length;
    uint8_t fanout;
    uint8_t depth;
    uint32_t leaf_length;
    uint8_t node_depth;
    // node_offset is a bit special
    uint8_t inner_length;
    uint8_t salt[...];
    uint8_t personal[...];
    ...;
} blake_param ;

typedef struct {
   uint8_t last_node;
    ...;
} blake_state ;

int blake_init_param( blake_state *S, const blake_param *P );
int blake_update( blake_state *S, const uint8_t *in, uint64_t inlen );
int blake_final( blake_state *S, uint8_t *out, uint8_t outlen );

void* addressof_node_offset(blake_param *S);

void store32(void* dst, uint32_t w);
void store48(void* dst, uint64_t w);
void store64(void* dst, uint64_t w);
"""

blake2b_source = """
#include "impl/blake2.h"
#include "impl/blake2-impl.h"

#define BLAKE_OUTBYTES BLAKE2B_OUTBYTES
#define BLAKE_SALTBYTES BLAKE2B_SALTBYTES
#define BLAKE_BLOCKBYTES BLAKE2B_BLOCKBYTES
#define BLAKE_PERSONALBYTES BLAKE2B_PERSONALBYTES
#define BLAKE_KEYBYTES BLAKE2B_KEYBYTES

typedef blake2b_state blake_state;
typedef blake2b_param blake_param;

#define blake_init_param blake2b_init_param
#define blake_update blake2b_update
#define blake_final blake2b_final

void* addressof_node_offset(blake_param *S) {
  return &(S->node_offset);
}
"""


# since we cdir, we use a relative path. If we use an absolute path, we get
# compile cruft in a multi-level subdir
_libdir = 'impl'
if BLAKE2_USE_SSE:
    sourcesB=[os.path.join(_libdir, 'blake2b.c'), ]
    sourcesS=[os.path.join(_libdir, 'blake2s.c'), ]
else:    
    sourcesB=[os.path.join(_libdir, 'blake2b-ref.c'), ]
    sourcesS=[os.path.join(_libdir, 'blake2s-ref.c'), ]

blake2b_ffi = FFI()
blake2b_ffi.cdef(blake_cdef)
blake2b_ffi.set_source(
    '_blake2b_cffi', blake2b_source,
    sources=sourcesB,
    include_dirs=[_libdir],
    extra_compile_args=extra_compile_args,
    define_macros=define_macros,
)

def _replace_b2s(src):
    for b, s in (('blake2b', 'blake2s'),
                 ('BLAKE2B', 'BLAKE2S')):
        src = src.replace(b, s)
    return src

blake2s_ffi = FFI()
blake2s_ffi.cdef(blake_cdef)
blake2s_ffi.set_source(
    '_blake2s_cffi', _replace_b2s(blake2b_source),
    sources=sourcesS,
    include_dirs=[_libdir],
    extra_compile_args=extra_compile_args,
    define_macros=define_macros,
)

if __name__ == '__main__':
    os.chdir(os.path.dirname(__file__))
    blake2b_ffi.compile()
    blake2s_ffi.compile()