Debian Codegraph @

use-s2n-bignum

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/env python3
2
•3 prefix = 'file:debian/s2n-bignum/'
4
5 todo = (
6 ('3c3f8f9496266306172251ed3b64c989b10b8dfbe1ee8353a19b932de22cf0d0','x86/curve25519','curve25519_x25519base' ,'crypto_nG/montgomery25519/amd64-s2nbignum-adx','amd64 bmi2 adx'),
7 ('4b6eef9217cc3822667734eedd23a6a0a7068d6f9b2f401d647f5fde3629c2db','x86/curve25519','curve25519_x25519base_alt' ,'crypto_nG/montgomery25519/amd64-s2nbignum' ,'amd64'),
8 ('64b08e280715ce2e8b6e48156bcb6bc9cd7ac117e52415e2e5d4b11fe57fa715','x86/curve25519','curve25519_x25519' ,'crypto_nP/montgomery25519/amd64-s2nbignum-adx','amd64 bmi2 adx'),
9 ('dff3fafb80cf180012d71e41e2c508622dca256f03ae6010943528cbfcbc37d3','x86/curve25519','curve25519_x25519_alt' ,'crypto_nP/montgomery25519/amd64-s2nbignum' ,'amd64'),
10 ('f433a1eb22d03ab1f9c3b68166dd855e06bceef74d5a29c0d60ba67fb3b19d2b','x86/curve25519','bignum_inv_p25519' ,'crypto_pow/inv25519/amd64-s2nbignum' ,'amd64'),
11 ('ee7bc766256150ab216f828d11f690485487e1cb86bc288a24fa5953b011050b','arm/curve25519','curve25519_x25519base_byte' ,'crypto_nG/montgomery25519/arm64-s2nbignum' ,'arm64'),
12 ('afdd9d920cf40fbfb0032e94efe98f7b415ab977c670ec56cb21c32fff9e5b96','arm/curve25519','curve25519_x25519base_byte_alt','crypto_nG/montgomery25519/arm64-s2nbignum-alt','arm64'),
13 ('29ad8c639e1d8edac6712900192d9ac1ee5fc4097e1ff09d0b754193ae1acaf6','arm/curve25519','curve25519_x25519_byte' ,'crypto_nP/montgomery25519/arm64-s2nbignum' ,'arm64'),
14 ('1ea1ef06b85faa46ac7c01578ceaee69142ba36d5a1e2abd035afc2b07c5dc1b','arm/curve25519','curve25519_x25519_byte_alt' ,'crypto_nP/montgomery25519/arm64-s2nbignum-alt','arm64'),
15 ('aa040c94e1ee5ff350fb3bf649a0254a3ac3e852e142f643dff134ba094bf03a','arm/curve25519','bignum_inv_p25519' ,'crypto_pow/inv25519/arm64-s2nbignum' ,'arm64'),
16 )
17
18 import os
19 import re
20 import urllib.request
21 import hashlib
22
23 rodata = False
24
25 def preprocess(asm): # expand #define x(y) and remove semicolons
26 macros = {}
27 processed = []
28
29 def expand(processed,line):
30 m = re.match(rb'\s*([a-zA-Z0-9_]*)\((.*)\)$',line)
31 if m is None:
32 processed += [line]
33 return
34 fun = m.group(1)
35 args = m.group(2)
36 processed += [b'// expanding %b(%b):' % (fun,args)]
37 assert fun in macros
38 assert macros[fun][0].count(b',') == args.count(b',')
39 tr = {param:arg for param,arg in zip(macros[fun][0].split(b','),args.split(b','))}
40 for result in macros[fun][1]:
41 for param in tr:
42 result = re.sub(param,tr[param],result)
43 expand(processed,result)
44
45 inmacro = False
46 for line in asm:
47 if inmacro:
48 insn = line
49 while insn.endswith(b'\\') or insn.endswith(b' ') or insn.endswith(b';'): insn = insn[:-1]
50 macro_out += [insn]
51 if not line.endswith(b'\\'):
52 macros[macro] = macro_args,macro_out
53 inmacro = False
54 else:
55 m = re.match(rb'#define (.*)\((.*)\) *\\$',line)
56 if m is not None:
57 inmacro = True
58 macro = m.group(1)
59 macro_args = m.group(2)
60 macro_out = []
61 else:
62 expand(processed,line)
63
64 return processed
65
66 for sha256,dir,source,target,architectures in todo:
67 print(f'{prefix}/{dir}/{source}.S')
68 with urllib.request.urlopen(f'{prefix}/{dir}/{source}.S') as http:
69 asm = http.read()
70
71 if hashlib.sha256(asm).hexdigest() != sha256:
72 raise Exception(f'hash mismatch: expected {sha256}, received {hashlib.sha256(asm).hexdigest()}')
73
74 asm = asm.splitlines()
75 asm2 = []
76 for line in asm:
77 if line == b'#include "_internal_s2n_bignum.h"': continue
78 if line.startswith(b'S2N_BN_SYMBOL') and line.endswith(b':'):
79 parenfun = line[len(b'S2N_BN_SYMBOL'):-1]
80 asm2 += [b'ASM_HIDDEN CRYPTO_SHARED_NAMESPACE'+parenfun]
81 asm2 += [b'.globl CRYPTO_SHARED_NAMESPACE'+parenfun]
82 asm2 += [b'ASM_HIDDEN _CRYPTO_SHARED_NAMESPACE'+parenfun]
83 asm2 += [b'.globl _CRYPTO_SHARED_NAMESPACE'+parenfun]
84 asm2 += [b'CRYPTO_SHARED_NAMESPACE'+parenfun+b':']
85 asm2 += [b'_CRYPTO_SHARED_NAMESPACE'+parenfun+b':']
86 continue
87 if line.startswith(b' S2N'): continue
88 if rodata and line.startswith(b'// .section .rodata'):
89 asm2 = [b'#include "crypto_asm_rodata.h"']+asm2
90 line = b'ASM_RODATA'
91 asm2 += [line]
92 asm = b'\n'.join(preprocess(asm2))+b'\n'
93
94 if '_byte' in source:
95 fun = source
96 else:
97 fun = source.replace('x25519','x25519_byte')
98 fun = fun.replace('x25519_bytebase','x25519base_byte')
99
100 os.makedirs(target,exist_ok=True)
101
102 with open(f'{target}/architectures','w') as f:
103 f.write(architectures+'\n')
104 with open(f'{target}/{source}.S','wb') as f:
105 f.write(b'// generated by use-s2n-bignum script\n')
106 f.write(b'// starting from s2n-bignum\n')
107 f.write(b'\n')
108 f.write(b'#include "crypto_asm_hidden.h"\n')
109 f.write(b'\n')
110 f.write(asm)
111
112 nG = target.startswith('crypto_nG')
113 with open(f'{target}/wrapper.c','w') as f:
114 if target.startswith('crypto_nG'):
115 f.write('#include <stdint.h>\n')
116 f.write('#include "crypto_nG.h"\n\n')
117 f.write(f'#define wrapped CRYPTO_SHARED_NAMESPACE({fun})\n\n')
118 f.write('extern void wrapped(uint8_t *,const uint8_t *);\n\n')
119 f.write('void crypto_nG(unsigned char *nG,const unsigned char *n)\n')
120 f.write('{\n')
121 f.write(' wrapped(nG,n);\n')
122 f.write('}\n')
123 elif target.startswith('crypto_nP'):
124 f.write('#include <stdint.h>\n')
125 f.write('#include "crypto_nP.h"\n\n')
126 f.write(f'#define wrapped CRYPTO_SHARED_NAMESPACE({fun})\n\n')
127 f.write('extern void wrapped(uint8_t *,const uint8_t *,const uint8_t *);\n\n')
128 f.write('void crypto_nP(unsigned char *q,const unsigned char *n,const unsigned char *p)\n')
129 f.write('{\n')
130 f.write(' wrapped(q,n,p);\n')
131 f.write('}\n')
132 elif target.startswith('crypto_pow'):
133 f.write('#include <string.h>\n')
134 f.write('#include <stdint.h>\n')
135 f.write('#include "crypto_pow.h"\n\n')
136 f.write(f'#define wrapped CRYPTO_SHARED_NAMESPACE({fun})\n\n')
137 f.write('extern void wrapped(uint8_t *,const uint8_t *);\n\n')
138 f.write('void crypto_pow(unsigned char *out,const unsigned char *in)\n')
139 f.write('{\n')
140 f.write(' unsigned char x[32];\n')
141 f.write(' memcpy(x,in,32);\n')
142 f.write(' x[31] &= 127;\n')
143 f.write(' wrapped(out,x);\n')
144 f.write('}\n')
145 else:
146 raise Exception(f'unknown target type {target}')
147
148 with open(f'{target}/verified','w') as f:
149 pass