#!/bin/sh

test_description="Check public-key encryption"
. $SHARNESS_TEST_SRCDIR/sharness.sh

dd if=/dev/urandom of=enc.data bs=300K count=1 2>/dev/null
balloonparams="-balloon-s 123 -balloon-t 2"

algo=mceliece6960119-x25519
algo0=$algo
test_expect_success "$algo: pub generation" "
    cmkeytool -algo $algo -ku kem -sub A=$algo 5>enc.$algo.pub 9>enc.$algo.prv"
algo=sntrup761-x25519
algo1=$algo
test_expect_success "$algo: pub generation" "
    cmkeytool -algo $algo -ku kem -sub A=$algo 5>enc.$algo.pub 9>enc.$algo.prv"

test_expect_success "encrypting" "
    cat enc.$algo0.pub enc.$algo1.pub |
    cmenctool 4<&0 <enc.data >enc.enc"

test_expect_success "any: decrypting" "
    cat enc.$algo0.prv enc.$algo1.prv |
    cmenctool -d 8<&0 <enc.enc >enc.data.got"
test_expect_success "comparing" "test_cmp enc.data enc.data.got"

test_expect_success "$algo0: decrypting" "
    cmenctool -d 8<enc.$algo0.prv <enc.enc >enc.data.got"
test_expect_success "$algo0: comparing" "test_cmp enc.data enc.data.got"

test_expect_success "$algo1: decrypting" "
    cmenctool -d 8<enc.$algo1.prv <enc.enc >enc.data.got"
test_expect_success "$algo1: comparing" "test_cmp enc.data enc.data.got"

export CM_PASSPHRASE=$(dd if=/dev/urandom bs=32 count=1 2>/dev/null | xxd -p)
test_expect_success "encrypting also with passphrase" "
    cat enc.$algo0.pub enc.$algo1.pub |
    cmenctool $balloonparams -p 4<&0 <enc.data >enc.enc"
test_expect_success "any: decrypting" "
    cat enc.$algo0.prv enc.$algo1.prv |
    cmenctool -d 8<&0 <enc.enc >enc.data.got"
test_expect_success "comparing" "test_cmp enc.data enc.data.got"
test_expect_success "passphrase: decrypting" "
    cmenctool -d -p <enc.enc >enc.data.got"
test_expect_success "comparing" "test_cmp enc.data enc.data.got"

test_done
