xref: /freebsd/crypto/openssh/mlkem768.sh (revision 8e28d84935f2f0ee081d44f9803f3052b960e50b)
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
52__builtin_popcount_replacement='
53  const uint8_t v[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
54  return v[x0 & 0xf] + v[(x0 >> 4) & 0xf];
55'
56
57for i in $FILES; do
58	echo "/* from $i */"
59	# Changes to all files:
60	#  - remove all includes, we inline everything required.
61	#  - cleanup whitespace
62	sed -e "/#include/d" \
63	    -e 's/[	 ]*$//' \
64	    $i | \
65	case "$i" in
66	*/libcrux-ml-kem/cg/eurydice_glue.h)
67		# Replace endian functions with versions that work.
68		perl -0777 -pe 's/(static inline void core_num__u64_9__to_le_bytes.*\n)([^}]*\n)/\1  v = htole64(v);\n\2/' |
69		perl -0777 -pe 's/(static inline uint64_t core_num__u64_9__from_le_bytes.*?)return v;/\1return le64toh(v);/s' |
70		perl -0777 -pe 's/(static inline uint32_t core_num__u32_8__from_le_bytes.*?)return v;/\1return le32toh(v);/s' |
71		# Compat for popcount.
72		perl -0777 -pe 's/\#ifdef (_MSC_VER)(.*?return __popcnt\(x0\);)/\#if defined(\1)\2/s' |
73		perl -0777 -pe "s/\\#else(\\n\\s+return __builtin_popcount\\(x0\\);)/\\#elif !defined(MISSING_BUILTIN_POPCOUNT)\\1\\n#else$__builtin_popcount_replacement/s"
74		;;
75	# Default: pass through.
76	*)
77		cat
78		;;
79	esac
80	echo
81done
82
83echo
84echo '/* rename some types to be a bit more ergonomic */'
85echo '#define libcrux_mlkem768_keypair libcrux_ml_kem_mlkem768_MlKem768KeyPair_s'
86echo '#define libcrux_mlkem768_pk_valid_result Option_92_s'
87echo '#define libcrux_mlkem768_pk libcrux_ml_kem_types_MlKemPublicKey_15_s'
88echo '#define libcrux_mlkem768_sk libcrux_ml_kem_types_MlKemPrivateKey_55_s'
89echo '#define libcrux_mlkem768_ciphertext libcrux_ml_kem_mlkem768_MlKem768Ciphertext_s'
90echo '#define libcrux_mlkem768_enc_result tuple_3c_s'
91) > libcrux_mlkem768_sha3.h_new
92
93# Do some checks on the resultant file
94
95cat > libcrux_mlkem768_sha3_check.c << _EOF
96#include <sys/types.h>
97#include <stdio.h>
98#include <stdint.h>
99#include <stdlib.h>
100#include <stdbool.h>
101#include <string.h>
102#include <signal.h>
103#include <err.h>
104#include "crypto_api.h"
105#define fatal_f(x) exit(1)
106#include "libcrux_mlkem768_sha3.h_new"
107int main(void) {
108	struct libcrux_mlkem768_keypair keypair = {0};
109	struct libcrux_mlkem768_pk pk = {0};
110	struct libcrux_mlkem768_sk sk = {0};
111	struct libcrux_mlkem768_ciphertext ct = {0};
112	struct libcrux_mlkem768_enc_result enc_result = {0};
113	uint8_t kp_seed[64] = {0}, enc_seed[32] = {0};
114	uint8_t shared_key[crypto_kem_mlkem768_BYTES];
115
116	if (sizeof(keypair.pk.value) != crypto_kem_mlkem768_PUBLICKEYBYTES)
117		errx(1, "keypair.pk bad");
118	if (sizeof(keypair.sk.value) != crypto_kem_mlkem768_SECRETKEYBYTES)
119		errx(1, "keypair.sk bad");
120	if (sizeof(pk.value) != crypto_kem_mlkem768_PUBLICKEYBYTES)
121		errx(1, "pk bad");
122	if (sizeof(sk.value) != crypto_kem_mlkem768_SECRETKEYBYTES)
123		errx(1, "sk bad");
124	if (sizeof(ct.value) != crypto_kem_mlkem768_CIPHERTEXTBYTES)
125		errx(1, "ct bad");
126	if (sizeof(enc_result.fst.value) != crypto_kem_mlkem768_CIPHERTEXTBYTES)
127		errx(1, "enc_result ct bad");
128	if (sizeof(enc_result.snd) != crypto_kem_mlkem768_BYTES)
129		errx(1, "enc_result shared key bad");
130
131	keypair = libcrux_ml_kem_mlkem768_portable_generate_key_pair(kp_seed);
132	if (!libcrux_ml_kem_mlkem768_portable_validate_public_key(&keypair.pk))
133		errx(1, "valid smoke failed");
134	enc_result = libcrux_ml_kem_mlkem768_portable_encapsulate(&keypair.pk,
135	    enc_seed);
136	libcrux_ml_kem_mlkem768_portable_decapsulate(&keypair.sk,
137	    &enc_result.fst, shared_key);
138	if (memcmp(shared_key, enc_result.snd, sizeof(shared_key)) != 0)
139		errx(1, "smoke failed");
140	return 0;
141}
142_EOF
143cc -Wall -Wextra -Wno-unused-parameter -o libcrux_mlkem768_sha3_check \
144	libcrux_mlkem768_sha3_check.c
145./libcrux_mlkem768_sha3_check
146
147# Extract PRNG inputs; there's no nice #defines for these
148key_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/'`
149enc_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/'`
150test -z "$key_pair_rng_len" && die "couldn't find size of libcrux_ml_kem_mlkem768_portable_kyber_generate_key_pair randomness argument"
151test -z "$enc_rng_len" && die "couldn't find size of libcrux_ml_kem_mlkem768_portable_kyber_encapsulate randomness argument"
152
153(
154echo "/* defines for PRNG inputs */"
155echo "#define LIBCRUX_ML_KEM_KEY_PAIR_PRNG_LEN $key_pair_rng_len"
156echo "#define LIBCRUX_ML_KEM_ENC_PRNG_LEN $enc_rng_len"
157) >> libcrux_mlkem768_sha3.h_new
158
159mv libcrux_mlkem768_sha3.h_new libcrux_mlkem768_sha3.h
160rm libcrux_mlkem768_sha3_check libcrux_mlkem768_sha3_check.c
161echo 1>&2
162echo "libcrux_mlkem768_sha3.h OK" 1>&2
163
164