xref: /freebsd/crypto/openssh/mlkem768.sh (revision 0ae642c7dd0c2cfd965a22bf73876cd26cceadd2)
1#!/bin/sh
2#       $OpenBSD: mlkem768.sh,v 1.3 2024/10/27 02:06:01 djm Exp $
3#       Placed in the Public Domain.
4#
5
6#WANT_LIBCRUX_REVISION="origin/main"
7WANT_LIBCRUX_REVISION="84c5d87b3092c59294345aa269ceefe0eb97cc35"
8
9FILES="
10	libcrux/libcrux-ml-kem/cg/eurydice_glue.h
11	libcrux/libcrux-ml-kem/cg/libcrux_core.h
12	libcrux/libcrux-ml-kem/cg/libcrux_ct_ops.h
13	libcrux/libcrux-ml-kem/cg/libcrux_sha3_portable.h
14	libcrux/libcrux-ml-kem/cg/libcrux_mlkem768_portable.h
15"
16
17START="$PWD"
18die() {
19	echo "$@" 1>&2
20	exit 1
21}
22
23set -xeuo pipefail
24test -d libcrux || git clone https://github.com/cryspen/libcrux
25cd libcrux
26test `git diff | wc -l` -ne 0 && die "tree has unstaged changes"
27git fetch
28git checkout -B extract 1>&2
29git reset --hard $WANT_LIBCRUX_REVISION 1>&2
30LIBCRUX_REVISION=`git rev-parse HEAD`
31set +x
32
33cd $START
34(
35printf '/*  $Open'; printf 'BSD$ */\n' # Sigh
36echo
37echo "/* Extracted from libcrux revision $LIBCRUX_REVISION */"
38echo
39echo '/*'
40cat libcrux/LICENSE-MIT | sed 's/^/ * /;s/ *$//'
41echo ' */'
42echo
43echo '#if !defined(__GNUC__) || (__GNUC__ < 2)'
44echo '# define __attribute__(x)'
45echo '#endif'
46echo '#define KRML_MUSTINLINE inline'
47echo '#define KRML_NOINLINE __attribute__((noinline, unused))'
48echo '#define KRML_HOST_EPRINTF(...)'
49echo '#define KRML_HOST_EXIT(x) fatal_f("internal error")'
50echo
51
52for i in $FILES; do
53	echo "/* from $i */"
54	# Changes to all files:
55	#  - remove all includes, we inline everything required.
56	#  - cleanup whitespace
57	sed -e "/#include/d" \
58	    -e 's/[	 ]*$//' \
59	    $i | \
60	case "$i" in
61	*/libcrux-ml-kem/cg/eurydice_glue.h)
62		# Replace endian functions with versions that work.
63		perl -0777 -pe 's/(static inline void core_num__u64_9__to_le_bytes.*\n)([^}]*\n)/\1  v = htole64(v);\n\2/' |
64		perl -0777 -pe 's/(static inline uint64_t core_num__u64_9__from_le_bytes.*?)return v;/\1return le64toh(v);/s' |
65		perl -0777 -pe 's/(static inline uint32_t core_num__u32_8__from_le_bytes.*?)return v;/\1return le32toh(v);/s'
66		;;
67	# Default: pass through.
68	*)
69		cat
70		;;
71	esac
72	echo
73done
74
75echo
76echo '/* rename some types to be a bit more ergonomic */'
77echo '#define libcrux_mlkem768_keypair libcrux_ml_kem_mlkem768_MlKem768KeyPair_s'
78echo '#define libcrux_mlkem768_pk_valid_result Option_92_s'
79echo '#define libcrux_mlkem768_pk libcrux_ml_kem_types_MlKemPublicKey_15_s'
80echo '#define libcrux_mlkem768_sk libcrux_ml_kem_types_MlKemPrivateKey_55_s'
81echo '#define libcrux_mlkem768_ciphertext libcrux_ml_kem_mlkem768_MlKem768Ciphertext_s'
82echo '#define libcrux_mlkem768_enc_result tuple_3c_s'
83) > libcrux_mlkem768_sha3.h_new
84
85# Do some checks on the resultant file
86
87cat > libcrux_mlkem768_sha3_check.c << _EOF
88#include <sys/types.h>
89#include <stdio.h>
90#include <stdint.h>
91#include <stdlib.h>
92#include <stdbool.h>
93#include <string.h>
94#include <signal.h>
95#include <err.h>
96#include "crypto_api.h"
97#define fatal_f(x) exit(1)
98#include "libcrux_mlkem768_sha3.h_new"
99int main(void) {
100	struct libcrux_mlkem768_keypair keypair = {0};
101	struct libcrux_mlkem768_pk pk = {0};
102	struct libcrux_mlkem768_sk sk = {0};
103	struct libcrux_mlkem768_ciphertext ct = {0};
104	struct libcrux_mlkem768_enc_result enc_result = {0};
105	uint8_t kp_seed[64] = {0}, enc_seed[32] = {0};
106	uint8_t shared_key[crypto_kem_mlkem768_BYTES];
107
108	if (sizeof(keypair.pk.value) != crypto_kem_mlkem768_PUBLICKEYBYTES)
109		errx(1, "keypair.pk bad");
110	if (sizeof(keypair.sk.value) != crypto_kem_mlkem768_SECRETKEYBYTES)
111		errx(1, "keypair.sk bad");
112	if (sizeof(pk.value) != crypto_kem_mlkem768_PUBLICKEYBYTES)
113		errx(1, "pk bad");
114	if (sizeof(sk.value) != crypto_kem_mlkem768_SECRETKEYBYTES)
115		errx(1, "sk bad");
116	if (sizeof(ct.value) != crypto_kem_mlkem768_CIPHERTEXTBYTES)
117		errx(1, "ct bad");
118	if (sizeof(enc_result.fst.value) != crypto_kem_mlkem768_CIPHERTEXTBYTES)
119		errx(1, "enc_result ct bad");
120	if (sizeof(enc_result.snd) != crypto_kem_mlkem768_BYTES)
121		errx(1, "enc_result shared key bad");
122
123	keypair = libcrux_ml_kem_mlkem768_portable_generate_key_pair(kp_seed);
124	if (!libcrux_ml_kem_mlkem768_portable_validate_public_key(&keypair.pk))
125		errx(1, "valid smoke failed");
126	enc_result = libcrux_ml_kem_mlkem768_portable_encapsulate(&keypair.pk,
127	    enc_seed);
128	libcrux_ml_kem_mlkem768_portable_decapsulate(&keypair.sk,
129	    &enc_result.fst, shared_key);
130	if (memcmp(shared_key, enc_result.snd, sizeof(shared_key)) != 0)
131		errx(1, "smoke failed");
132	return 0;
133}
134_EOF
135cc -Wall -Wextra -Wno-unused-parameter -o libcrux_mlkem768_sha3_check \
136	libcrux_mlkem768_sha3_check.c
137./libcrux_mlkem768_sha3_check
138
139# Extract PRNG inputs; there's no nice #defines for these
140key_pair_rng_len=`sed -e '/^libcrux_ml_kem_mlkem768_portable_kyber_generate_key_pair[(]$/,/[)] {$/!d' < libcrux_mlkem768_sha3.h_new | grep 'uint8_t randomness\[[0-9]*U\][)]' | sed 's/.*randomness\[\([0-9]*\)U\].*/\1/'`
141enc_rng_len=`sed -e '/^static inline tuple_3c libcrux_ml_kem_mlkem768_portable_kyber_encapsulate[(]$/,/[)] {$/!d' < libcrux_mlkem768_sha3.h_new | grep 'uint8_t randomness\[[0-9]*U\][)]' | sed 's/.*randomness\[\([0-9]*\)U\].*/\1/'`
142test -z "$key_pair_rng_len" && die "couldn't find size of libcrux_ml_kem_mlkem768_portable_kyber_generate_key_pair randomness argument"
143test -z "$enc_rng_len" && die "couldn't find size of libcrux_ml_kem_mlkem768_portable_kyber_encapsulate randomness argument"
144
145(
146echo "/* defines for PRNG inputs */"
147echo "#define LIBCRUX_ML_KEM_KEY_PAIR_PRNG_LEN $key_pair_rng_len"
148echo "#define LIBCRUX_ML_KEM_ENC_PRNG_LEN $enc_rng_len"
149) >> libcrux_mlkem768_sha3.h_new
150
151mv libcrux_mlkem768_sha3.h_new libcrux_mlkem768_sha3.h
152rm libcrux_mlkem768_sha3_check libcrux_mlkem768_sha3_check.c
153echo 1>&2
154echo "libcrux_mlkem768_sha3.h OK" 1>&2
155
156