summaryrefslogtreecommitdiff
path: root/devel/import-ecc-from-nettle.sh
blob: 2ce6285d39204cf994e8d623b2a9a011f7e78f7d (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/sh

# This script copies the Curve448 and Ed448, GOST 34.10 implementation from the
# nettle upstream, with necessary adjustments for bundling in GnuTLS.

set +e

: ${srcdir=.}
SRC=$srcdir/devel/nettle
DST=$srcdir/lib/nettle/ecc

IMPORTS="
cnd-copy.c
curve448-eh-to-x.c
curve448.h
curve448-mul.c
curve448-mul-g.c
eccdata.c
ecc-curve448.c
ecc-add-eh.c
ecc-add-ehh.c
ecc-add-jja.c
ecc-add-jjj.c
ecc-a-to-j.c
ecc-dup-eh.c
ecc-dup-jj.c
ecc-eh-to-a.c
ecc-gost-gc256b.c
ecc-gost-gc512a.c
ecc-gostdsa-sign.c
ecc-gostdsa-verify.c
ecc-internal.h
ecc-j-to-a.c
ecc-mod-arith.c
ecc-mod.c
ecc-mod-inv.c
ecc-mul-a.c
ecc-mul-a-eh.c
ecc-mul-g.c
ecc-mul-g-eh.c
ecc-mul-m.c
ecc-random.c
ed448-shake256.c
ed448-shake256-pubkey.c
ed448-shake256-sign.c
ed448-shake256-verify.c
eddsa-compress.c
eddsa-decompress.c
eddsa-expand.c
eddsa.h
eddsa-hash.c
eddsa-internal.h
eddsa-pubkey.c
eddsa-sign.c
eddsa-verify.c
gostdsa.h
gostdsa-sign.c
gostdsa-verify.c
gostdsa-vko.c
gmp-glue.h
gmp-glue.c
nettle-write.h
sec-add-1.c
sec-tabselect.c
sha3.c
sha3.h
sha3-256.c
sha3-internal.h
sha3-permute.c
shake256.c
write-le64.c
"

PUBLIC="
bignum.h
dsa.h
ecc-curve.h
ecc.h
ecdsa.h
macros.h
memxor.h
nettle-meta.h
nettle-types.h
"

test -d $DST || mkdir $DST

for f in $IMPORTS; do
  src=$SRC/$f
  dst=$DST/$f
  if test -f $src; then
    if test -f $dst; then
      echo "Replacing $dst (existing file backed up in $dst~)"
      mv $dst $dst~
    else
      echo "Copying file $dst"
    fi
    cp $src $dst
    # Use <nettle/*.h> for public headers.
    for h in $PUBLIC; do
      p=$(echo $h | sed 's/\./\\./g')
      if grep '^#include "'$p'"' $dst 2>&1 >/dev/null; then
	sed 's!^#include "'$p'"!#include <nettle/'$h'>!' $dst > $dst-t && \
	  mv $dst-t $dst
      fi
    done
    # Remove unused <assert.h>.
    if grep '^#include <assert\.h>' $dst 2>&1 >/dev/null; then
      if ! grep 'assert *(' $dst 2>&1 >/dev/null; then
	sed '/^#include <assert\.h>/d' $dst > $dst-t && mv $dst-t $dst
      fi
    fi
    case $dst in
      *.h)
	# Rename header guard so as not to conflict with the public ones.
	if grep '^#ifndef NETTLE_.*_H\(_INCLUDED\)*' $dst 2>&1 >/dev/null; then
	  g=$(sed -n 's/^#ifndef NETTLE_\(.*_H\(_INCLUDED\)*\)/\1/p' $dst)
	  sed 's/\(NETTLE_'$g'\)/GNUTLS_LIB_NETTLE_ECC_\1/' $dst > $dst-t && \
	    mv $dst-t $dst
	fi
	# Add prefix to function symbols avoid clashing with the public ones.
	sed -e 's/^#define \(.*\) nettle_\1/#define \1 gnutls_nettle_ecc_\1/' \
	    -e 's/^#define \(.*\) _nettle_\1/#define \1 _gnutls_nettle_ecc_\1/' \
	    -e 's/^#define _\(.*\) _nettle_\1/#define _\1 _gnutls_nettle_ecc_\1/' \
	    -e '/^_nettle_/ { h ; s/^_nettle_\(.*\)(.*/#define _nettle_\1 _gnutls_nettle_ecc_\1/g ; p; x; }' \
	    -e '/^extern const struct ecc_curve _nettle_\(.*\);/ { h ; s/.*_nettle\(.*\);/#define _nettle_\1 _gnutls_nettle_ecc_\1/ ; p; x; }' \
	    -e '/^extern const struct ecc_eddsa _nettle_\(.*\);/ { h ; s/.*_nettle\(.*\);/#define _nettle_\1 _gnutls_nettle_ecc_\1/ ; p; x; }' \
	    -e '/gostdsa_generate_keypair/d' \
	    $dst > $dst-t && \
	  mv $dst-t $dst
      ;;
      */eccdata.c)
	sed 's/^#include "mini-gmp.c"/#include <gmp.h>/' $dst > $dst-t && \
	  mv $dst-t $dst
	;;
      */ecc-curve448.c)
	# The generated file is arch dependent, conditionalize the
	# inclusion.
	sed '/^#include "ecc-curve448\.h"/ { i\
#if defined __clang__ || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)\
# pragma GCC diagnostic ignored "-Wunused-const-variable"\
#endif\
#if GMP_NUMB_BITS == 32\
#include "ecc/ecc-curve448-32.h"\
#elif GMP_NUMB_BITS == 64\
#include "ecc/ecc-curve448-64.h"\
#else\
#error unsupported configuration\
#endif
; d
}' $dst > $dst-t && mv $dst-t $dst
	;;
      */ecc-gost-gc256b.c)
	# The generated file is arch dependent, conditionalize the
	# inclusion.
	sed -e '/^#include "ecc-gost-gc256b\.h"/ { i\
#if defined __clang__ || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)\
# pragma GCC diagnostic ignored "-Wunused-const-variable"\
#endif\
#if GMP_NUMB_BITS == 32\
#include "ecc/ecc-gost-gc256b-32.h"\
#elif GMP_NUMB_BITS == 64\
#include "ecc/ecc-gost-gc256b-64.h"\
#else\
#error unsupported configuration\
#endif
; d
}' \
	  -e '/#include "ecc-internal.h"/ { i\
#include "ecc-gost-curve.h"
; }' \
	  $dst > $dst-t && mv $dst-t $dst
	;;
      */ecc-gost-gc512a.c)
	# The generated file is arch dependent, conditionalize the
	# inclusion.
	sed -e '/^#include "ecc-gost-gc512a\.h"/ { i\
#if defined __clang__ || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)\
# pragma GCC diagnostic ignored "-Wunused-const-variable"\
#endif\
#if GMP_NUMB_BITS == 32\
#include "ecc/ecc-gost-gc512a-32.h"\
#elif GMP_NUMB_BITS == 64\
#include "ecc/ecc-gost-gc512a-64.h"\
#else\
#error unsupported configuration\
#endif
; d
}' \
	  -e '/#include "ecc-internal.h"/ { i\
#include "ecc-gost-curve.h"
; }' \
	  $dst > $dst-t && mv $dst-t $dst
	;;
      */eddsa-hash.c)
	# Known to be unnecessary.
	sed '/^#include "nettle-internal\.h"/d' $dst > $dst-t && mv $dst-t $dst
	;;
      */ecc-add-eh*.c)
	# Suppress whitespace errors in 'make syntax-check'.
	sed 's/ *	/		/g' $dst > $dst-t && mv $dst-t $dst
	;;
      */ecc-random.c )
	sed \
	  -e '/^#include "nettle-internal\.h"/ { i\
#include "nettle-alloca.h"\
\
void gnutls_ecc_scalar_random(struct ecc_scalar *, void *, nettle_random_func *);
; d
}' \
	  -e 's/ecc_scalar_random/gnutls_ecc_scalar_random/' \
	  -e 's/^    & (mpn_sub_n/    \& (int)(mpn_sub_n/' \
	  $dst > $dst-t && mv $dst-t $dst
	;;
      */gostdsa-sign.c)
	sed \
	  -e 's/"nettle-internal\.h"/"nettle-alloca.h"/' \
	  $dst > $dst-t && mv $dst-t $dst
	;;
    esac
  else
    echo "Error: $src not found" 1>&2
    exit 1
  fi
done