# RFC 9807 byte-level compliance check (Phase 1.5).
#
# Rebuilds the libopaque + liboprf source files with -DCFRG_TEST_VEC, then
# links them with the upstream's own opaque-testvectors.c into the
# `opaque-tv1` binary. The CFRG_TEST_VEC mode replaces random sources
# with the fixed byte sequences from RFC 9807 Appendix C.1.1, and the
# binary asserts byte-equality of KE1/KE2/KE3/session_key/export_key
# against the RFC's expected values.
#
# This is a SEPARATE build from the production libopaque (which uses
# real random). The libopaque and liboprf sources come from outside this
# tree: LIBOPAQUE_SOURCE_DIR and LIBOPRF_SOURCE_DIR name the source trees
# of the packages the plug-in links against, with their packaging patches
# applied, so the tested code is the code those packages ship. Debian's
# autopkgtest (debian/tests/ on the packaging branch) fetches them with
# apt-get source; without them the checks are not built.

set(LIBOPAQUE_SOURCE_DIR "" CACHE PATH
    "libopaque source tree with its packaging patches applied, for the RFC 9807 test-vector checks")
set(LIBOPRF_SOURCE_DIR "" CACHE PATH
    "liboprf source tree with its packaging patches applied, for the RFC 9807 test-vector checks")
if(NOT LIBOPAQUE_SOURCE_DIR OR NOT LIBOPRF_SOURCE_DIR)
    message(STATUS "RFC compliance build skipped: LIBOPAQUE_SOURCE_DIR or LIBOPRF_SOURCE_DIR not set")
    return()
endif()

set(LIBOPAQUE_SRC   ${LIBOPAQUE_SOURCE_DIR}/src)
set(LIBOPRF_SRC     ${LIBOPRF_SOURCE_DIR}/src)
set(NOISE_XK_SRC    ${LIBOPRF_SRC}/noise_xk/src)
set(NOISE_XK_INC    ${LIBOPRF_SRC}/noise_xk/include)

# The source directories were asked for, so missing files are an error.
if(NOT EXISTS ${LIBOPAQUE_SRC}/opaque.c
   OR NOT EXISTS ${LIBOPRF_SRC}/oprf.c
   OR NOT EXISTS ${LIBOPAQUE_SRC}/tests/opaque-testvectors.c
   OR NOT EXISTS ${LIBOPAQUE_SRC}/tests/cfrg_test_vectors.h)
    message(FATAL_ERROR "RFC compliance build: libopaque or liboprf sources not found under "
                        "${LIBOPAQUE_SOURCE_DIR} and ${LIBOPRF_SOURCE_DIR}")
endif()

# noise_xk: deterministic build is the same as the runtime build —
# CFRG_TEST_VEC doesn't affect this layer. Reuse the same source.
add_library(noise_xk_cfrg STATIC
    ${NOISE_XK_SRC}/Noise_XK.c
    ${NOISE_XK_SRC}/XK.c)
target_compile_definitions(noise_xk_cfrg PRIVATE WITH_SODIUM _BSD_SOURCE _DEFAULT_SOURCE)
target_include_directories(noise_xk_cfrg PRIVATE
    ${NOISE_XK_INC}
    ${NOISE_XK_INC}/karmel
    ${NOISE_XK_INC}/karmel/minimal)
target_link_libraries(noise_xk_cfrg PRIVATE PkgConfig::SODIUM)
target_compile_options(noise_xk_cfrg PRIVATE -Wno-unused-but-set-variable -Wno-unused-parameter)

# liboprf + libopaque rebuilt with -DCFRG_TEST_VEC.
add_library(opaque_cfrg STATIC
    ${LIBOPRF_SRC}/oprf.c
    ${LIBOPRF_SRC}/toprf.c
    ${LIBOPRF_SRC}/toprf-update.c
    ${LIBOPRF_SRC}/dkg.c
    ${LIBOPRF_SRC}/dkg-vss.c
    ${LIBOPRF_SRC}/utils.c
    ${LIBOPRF_SRC}/tp-dkg.c
    ${LIBOPRF_SRC}/stp-dkg.c
    ${LIBOPRF_SRC}/mpmult.c
    ${LIBOPAQUE_SRC}/common.c
    ${LIBOPAQUE_SRC}/opaque.c)
target_compile_definitions(opaque_cfrg PRIVATE
    CFRG_TEST_VEC HAVE_SODIUM_HKDF=1
    _BSD_SOURCE _DEFAULT_SOURCE)
target_include_directories(opaque_cfrg PRIVATE
    ${LIBOPAQUE_SRC}
    ${LIBOPAQUE_SRC}/aux_
    ${LIBOPRF_SRC}
    ${LIBOPAQUE_INCLUDE_DIRS}     # for <oprf/toprf.h> etc.
    ${NOISE_XK_INC}
    ${NOISE_XK_INC}/karmel
    ${NOISE_XK_INC}/karmel/minimal)
target_link_libraries(opaque_cfrg PRIVATE noise_xk_cfrg PkgConfig::SODIUM PkgConfig::ARGON2)
target_compile_options(opaque_cfrg PRIVATE -Wno-unused-but-set-variable -Wno-unused-parameter)

# The test binary — uses the upstream's own opaque-testvectors.c and
# the cfrg_test_vectors.h header that defines the fixed RFC inputs.
add_executable(opaque-tv1 ${LIBOPAQUE_SRC}/tests/opaque-testvectors.c)
target_compile_definitions(opaque-tv1 PRIVATE CFRG_TEST_VEC HAVE_SODIUM_HKDF=1)
target_include_directories(opaque-tv1 PRIVATE
    ${LIBOPAQUE_SRC}
    ${LIBOPAQUE_SRC}/tests
    ${LIBOPRF_SRC}
    ${LIBOPAQUE_INCLUDE_DIRS})
target_link_libraries(opaque-tv1 PRIVATE opaque_cfrg PkgConfig::SODIUM)
target_compile_options(opaque-tv1 PRIVATE -Wno-unused-but-set-variable -Wno-unused-parameter)

# Register as a CTest test so `ctest` / `make test` runs it.
add_test(NAME rfc9807_byte_compliance COMMAND opaque-tv1)

# Registration with the Appendix C.1.1 inputs and the RFC 9807 §7 Argon2id
# setting (docs/plans/argon2id_known_answer_plan.md). CFRG_TEST_VEC would
# also replace Argon2id by the identity function, so this backend links the
# shipped libopaque; opaque-argon2id-tv1 fixes the random draws through
# libsodium instead. The OPRF input is the password alone, as in Appendix C.
add_library(opaque_backend_rfc9807_oprf_input STATIC ${CMAKE_SOURCE_DIR}/libopaque/backend.c)
target_compile_definitions(opaque_backend_rfc9807_oprf_input PRIVATE OPAQUE_OPRF_INPUT_RFC9807_TEST_VECTORS)
target_include_directories(opaque_backend_rfc9807_oprf_input PUBLIC ${CMAKE_SOURCE_DIR}/driver)
target_link_libraries(opaque_backend_rfc9807_oprf_input PUBLIC PkgConfig::LIBOPAQUE PkgConfig::SODIUM)

add_executable(opaque-argon2id-tv1 opaque-argon2id-tv1.c)
target_include_directories(opaque-argon2id-tv1 PRIVATE
    ${LIBOPAQUE_SRC}/tests)  # cfrg_test_vectors.h
target_link_libraries(opaque-argon2id-tv1 PRIVATE opaque_backend_rfc9807_oprf_input)
add_test(NAME rfc9807_argon2id_compliance COMMAND opaque-argon2id-tv1)

# RFC 9380 §5.3.1 expand_message_xmd of the shipped liboprf at a 255-byte DST
# and outputs above 255 bytes, against values computed by the elliptic-curve
# crate (docs/plans/rfc9807_conformance_plan.md, test T-34).
find_library(LIBOPRF_LIBRARY oprf REQUIRED)
add_executable(expand-message-xmd-tv1 expand-message-xmd-tv1.c)
target_link_libraries(expand-message-xmd-tv1 PRIVATE ${LIBOPRF_LIBRARY} PkgConfig::SODIUM)
add_test(NAME rfc9380_expand_message_xmd_compliance COMMAND expand-message-xmd-tv1)

# ---------------------------------------------------------------------------
# GSSAPI plug-in wire-format byte-compliance test (gss-tv1).
#
# opaque-tv1 above verifies that *libopaque* produces the RFC 9807 expected
# byte sequences for KE1/KE2/KE3 under the fixed test-vector inputs. This
# test, one layer up, verifies that our GSSAPI plug-in's wire wrapping
# preserves those bytes verbatim — i.e. that we don't accidentally mutate
# the OPAQUE payload while adding our ASN.1 InitialContextToken wrapper
# and the [tag][cred_id_len][cred_id] prefix from §2.4 of the plan.
#
# Build chain: backend.c and opaque-mech.c are RECOMPILED here against
# the CFRG-flavored opaque_cfrg static library (so the random sources
# inside libopaque become the RFC test-vector constants). The plug-in
# code is the production source; only the libopaque layer underneath
# substitutes the randomness, and gss-tv1's build leaves the
# configuration string out of the OPAQUE Context (see below).
#
# Gated on BUILD_GSSAPI_PLUGIN so the test only builds when the plug-in
# itself is being built.
# ---------------------------------------------------------------------------
if(BUILD_GSSAPI_PLUGIN)
    set(BACKEND_SRC ${CMAKE_SOURCE_DIR}/libopaque/backend.c)
    set(PLUGIN_SRC  ${CMAKE_SOURCE_DIR}/gss-opaque/opaque-mech.c)

    # backend.c rebuilt with the CFRG-flavored libopaque source. RFC 9807's
    # Appendix C vectors use the password alone as OPRF input, so this build
    # leaves the credential identifier out of it
    # (OPAQUE_OPRF_INPUT_RFC9807_TEST_VECTORS).
    add_library(opaque_backend_cfrg STATIC ${BACKEND_SRC})
    target_compile_definitions(opaque_backend_cfrg PRIVATE
        CFRG_TEST_VEC HAVE_SODIUM_HKDF=1 OPAQUE_OPRF_INPUT_RFC9807_TEST_VECTORS)
    target_include_directories(opaque_backend_cfrg PUBLIC
        ${CMAKE_SOURCE_DIR}/driver)
    target_include_directories(opaque_backend_cfrg PRIVATE
        ${LIBOPAQUE_SRC}
        ${LIBOPRF_SRC}
        ${LIBOPAQUE_INCLUDE_DIRS})
    target_link_libraries(opaque_backend_cfrg PUBLIC
        opaque_cfrg PkgConfig::SODIUM)

    # opaque-mech.c rebuilt twice against the CFRG backend. RFC 9807's
    # published KE2 and KE3 use the channel-binding data alone as Context,
    # so gss-tv1 links a build without the configuration string
    # (OPAQUE_CONTEXT_RFC9807_TEST_VECTORS); gss-tv1-context links the
    # production Context and compares with libopaque called directly.
    # Appendix C.1.1 has no identities, so both builds pass none and accept
    # nameless credentials (OPAQUE_PLUGIN_RFC9807_TEST_VECTORS, which the
    # plug-in refuses without the backend's and libopaque's test-vector
    # definitions).
    foreach(variant IN ITEMS rfc_vectors production_context)
        add_library(gssopaque_obj_cfrg_${variant} STATIC ${PLUGIN_SRC})
        set_target_properties(gssopaque_obj_cfrg_${variant} PROPERTIES
            POSITION_INDEPENDENT_CODE ON)
        target_compile_definitions(gssopaque_obj_cfrg_${variant} PRIVATE
            _POSIX_C_SOURCE=200809L
            OPAQUE_PLUGIN_RFC9807_TEST_VECTORS OPAQUE_OPRF_INPUT_RFC9807_TEST_VECTORS CFRG_TEST_VEC)
        target_include_directories(gssopaque_obj_cfrg_${variant} PUBLIC
            ${CMAKE_SOURCE_DIR}/gss-opaque)
        target_link_libraries(gssopaque_obj_cfrg_${variant} PUBLIC
            opaque_backend_cfrg
            PkgConfig::SODIUM
            PkgConfig::GSSAPI)
    endforeach()
    target_compile_definitions(gssopaque_obj_cfrg_rfc_vectors PRIVATE
        OPAQUE_CONTEXT_RFC9807_TEST_VECTORS)

    add_executable(gss-tv1 gss-tv1.c)
    add_executable(gss-tv1-context gss-tv1.c)
    foreach(test_binary IN ITEMS gss-tv1 gss-tv1-context)
        target_compile_definitions(${test_binary} PRIVATE _POSIX_C_SOURCE=200809L)
        target_include_directories(${test_binary} PRIVATE
            ${LIBOPAQUE_SRC}/tests   # cfrg_test_vectors.h
            ${LIBOPAQUE_SRC}         # opaque.h
            ${LIBOPRF_SRC}
            ${LIBOPAQUE_INCLUDE_DIRS}
            ${CMAKE_SOURCE_DIR}/driver)
    endforeach()
    target_compile_definitions(gss-tv1 PRIVATE OPAQUE_CONTEXT_RFC9807_TEST_VECTORS)
    target_link_libraries(gss-tv1 PRIVATE gssopaque_obj_cfrg_rfc_vectors)
    target_link_libraries(gss-tv1-context PRIVATE gssopaque_obj_cfrg_production_context)

    add_test(NAME gss_wire_compliance COMMAND gss-tv1)
    add_test(NAME gss_wire_compliance_context COMMAND gss-tv1-context)

    # client_identity and server_identity (RFC 9807 §4, §9.1, §10.3;
    # docs/plans/oprf_identities_plan.md) under the fixed test-vector
    # randomness. The identity enters the OPRF input and Opaque_Ids; each
    # binding is tested with the other held fixed:
    #   opaque-tv2, opaque-fake-tv1: backend with the password alone as OPRF
    #     input, so only Opaque_Ids varies — Appendix C.1.2 and C.2.1, and the
    #     envelope and preamble bindings;
    #   oprf-input-identities: backend with the production OPRF input; it
    #     compares blinded_message, which Opaque_Ids does not enter.
    add_library(opaque_backend_cfrg_oprf_identities STATIC ${BACKEND_SRC})
    target_compile_definitions(opaque_backend_cfrg_oprf_identities PRIVATE CFRG_TEST_VEC HAVE_SODIUM_HKDF=1)
    target_include_directories(opaque_backend_cfrg_oprf_identities PUBLIC
        ${CMAKE_SOURCE_DIR}/driver)
    target_include_directories(opaque_backend_cfrg_oprf_identities PRIVATE
        ${LIBOPAQUE_SRC}
        ${LIBOPRF_SRC}
        ${LIBOPAQUE_INCLUDE_DIRS})
    target_link_libraries(opaque_backend_cfrg_oprf_identities PUBLIC
        opaque_cfrg PkgConfig::SODIUM)

    add_executable(opaque-tv2 opaque-tv2.c)
    add_executable(opaque-fake-tv1 opaque-fake-tv1.c)
    add_executable(oprf-input-identities oprf-input-identities.c)
    foreach(test_binary IN ITEMS opaque-tv2 opaque-fake-tv1 oprf-input-identities)
        target_include_directories(${test_binary} PRIVATE
            ${LIBOPAQUE_SRC}/tests   # cfrg_test_vectors.h, cfrg_test_vector_decl.h
            ${LIBOPAQUE_SRC}
            ${LIBOPAQUE_INCLUDE_DIRS})
    endforeach()
    target_link_libraries(opaque-tv2 PRIVATE opaque_backend_cfrg)
    target_link_libraries(opaque-fake-tv1 PRIVATE opaque_backend_cfrg)
    target_link_libraries(oprf-input-identities PRIVATE opaque_backend_cfrg_oprf_identities)

    add_test(NAME rfc9807_identities_compliance COMMAND opaque-tv2)
    add_test(NAME rfc9807_fake_record_compliance COMMAND opaque-fake-tv1)
    add_test(NAME oprf_input_identities_compliance COMMAND oprf-input-identities)
endif()
