|
| 1 | +#!/bin/sh |
| 2 | +# $OpenBSD: sntrup761.sh,v 1.9 2024/09/16 05:37:05 djm Exp $ |
| 3 | +# Placed in the Public Domain. |
| 4 | +# |
| 5 | +AUTHOR="supercop-20241022/crypto_kem/sntrup761/ref/implementors" |
| 6 | +FILES=" supercop-20241022/cryptoint/crypto_int16.h |
| 7 | + supercop-20241022/cryptoint/crypto_int32.h |
| 8 | + supercop-20241022/cryptoint/crypto_int64.h |
| 9 | + supercop-20241022/crypto_sort/int32/portable4/sort.c |
| 10 | + supercop-20241022/crypto_sort/uint32/useint32/sort.c |
| 11 | + supercop-20241022/crypto_kem/sntrup761/compact/kem.c |
| 12 | +" |
| 13 | +### |
| 14 | + |
| 15 | +set -euo pipefail |
| 16 | +cd $1 |
| 17 | +echo '/*' |
| 18 | +echo ' * Public Domain, Authors:' |
| 19 | +sed -e '/Alphabetical order:/d' -e 's/^/ * - /' < $AUTHOR |
| 20 | +echo ' */' |
| 21 | +echo |
| 22 | +echo '#include <string.h>' |
| 23 | +echo '#include "sntrup761_compat.h"' |
| 24 | +echo |
| 25 | +echo '#if DROPBEAR_SNTRUP761' |
| 26 | +echo |
| 27 | +echo '#define crypto_declassify(x, y) do {} while (0)' |
| 28 | +echo |
| 29 | +# Map the types used in this code to the ones in crypto_api.h. We use #define |
| 30 | +# instead of typedef since some systems have existing intXX types and do not |
| 31 | +# permit multiple typedefs even if they do not conflict. |
| 32 | +for t in int8 uint8 int16 uint16 int32 uint32 int64 uint64; do |
| 33 | + echo "#define $t crypto_${t}" |
| 34 | +done |
| 35 | + |
| 36 | +for x in 16 32 64 ; do |
| 37 | + echo "extern volatile crypto_int$x crypto_int${x}_optblocker;" |
| 38 | +done |
| 39 | + |
| 40 | +echo |
| 41 | +for i in $FILES; do |
| 42 | + echo "/* from $i */" |
| 43 | + # Changes to all files: |
| 44 | + # - remove all includes, we inline everything required. |
| 45 | + # - make functions not required elsewhere static. |
| 46 | + # - rename the functions we do use. |
| 47 | + # - remove unnecessary defines and externs. |
| 48 | + sed -e "/#include/d" \ |
| 49 | + -e "s/crypto_kem_/crypto_kem_sntrup761_/g" \ |
| 50 | + -e "s/^void /static void /g" \ |
| 51 | + -e "s/^int16 /static int16 /g" \ |
| 52 | + -e "s/^uint16 /static uint16 /g" \ |
| 53 | + -e "/^extern /d" \ |
| 54 | + -e '/CRYPTO_NAMESPACE/d' \ |
| 55 | + -e "/^#define int32 crypto_int32/d" \ |
| 56 | + -e 's/[ ]*$//' \ |
| 57 | + $i | \ |
| 58 | + case "$i" in |
| 59 | + */cryptoint/crypto_int16.h) |
| 60 | + sed -e "s/static void crypto_int16_store/void crypto_int16_store/" \ |
| 61 | + -e "s/^[#]define crypto_int16_optblocker.*//" \ |
| 62 | + -e "s/static void crypto_int16_minmax/void crypto_int16_minmax/" |
| 63 | + ;; |
| 64 | + */cryptoint/crypto_int32.h) |
| 65 | + # Use int64_t for intermediate values in crypto_int32_minmax to |
| 66 | + # prevent signed 32-bit integer overflow when called by |
| 67 | + # crypto_sort_int32. Original code depends on -fwrapv (we set -ftrapv) |
| 68 | + sed -e "s/static void crypto_int32_store/void crypto_int32_store/" \ |
| 69 | + -e "s/^[#]define crypto_int32_optblocker.*//" \ |
| 70 | + -e "s/crypto_int32 crypto_int32_r = crypto_int32_y ^ crypto_int32_x;/crypto_int64 crypto_int32_r = (crypto_int64)crypto_int32_y ^ (crypto_int64)crypto_int32_x;/" \ |
| 71 | + -e "s/crypto_int32 crypto_int32_z = crypto_int32_y - crypto_int32_x;/crypto_int64 crypto_int32_z = (crypto_int64)crypto_int32_y - (crypto_int64)crypto_int32_x;/" \ |
| 72 | + -e "s/static void crypto_int32_minmax/void crypto_int32_minmax/" |
| 73 | + ;; |
| 74 | + */cryptoint/crypto_int64.h) |
| 75 | + sed -e "s/static void crypto_int64_store/void crypto_int64_store/" \ |
| 76 | + -e "s/^[#]define crypto_int64_optblocker.*//" \ |
| 77 | + -e "s/static void crypto_int64_minmax/void crypto_int64_minmax/" |
| 78 | + ;; |
| 79 | + */int32/portable4/sort.c) |
| 80 | + sed -e "s/void crypto_sort[(]/void crypto_sort_int32(/g" |
| 81 | + ;; |
| 82 | + */int32/portable5/sort.c) |
| 83 | + sed -e "s/crypto_sort_smallindices/crypto_sort_int32_smallindices/"\ |
| 84 | + -e "s/void crypto_sort[(]/void crypto_sort_int32(/g" |
| 85 | + ;; |
| 86 | + */uint32/useint32/sort.c) |
| 87 | + sed -e "s/void crypto_sort/void crypto_sort_uint32/g" |
| 88 | + ;; |
| 89 | + # Remove unused function to prevent warning. |
| 90 | + */crypto_kem/sntrup761/ref/int32.c) |
| 91 | + sed -e '/ int32_div_uint14/,/^}$/d' |
| 92 | + ;; |
| 93 | + # Remove unused function to prevent warning. |
| 94 | + */crypto_kem/sntrup761/ref/uint32.c) |
| 95 | + sed -e '/ uint32_div_uint14/,/^}$/d' |
| 96 | + ;; |
| 97 | + # Default: pass through. |
| 98 | + *) |
| 99 | + cat |
| 100 | + ;; |
| 101 | + esac |
| 102 | + echo |
| 103 | +done |
| 104 | +echo '#endif /* DROPBEAR_SNTRUP761 */' |
0 commit comments