aboutsummaryrefslogtreecommitdiff
path: root/tests/external_override/CMakeLists.txt
blob: 365a10836bb5f45ad989fa9112c1403c7ccc8935 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
project(external-override C)

include_directories(${CMAKE_SOURCE_DIR}/include)

set(LIBSSH_OVERRIDE_TESTS
    torture_override
)

# chacha20_override
add_library(chacha20_override SHARED
            chacha20_override.c
            ${libssh_SOURCE_DIR}/src/external/chacha.c
           )
set(CHACHA20_OVERRIDE_LIBRARY
    ${libssh_BINARY_DIR}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}chacha20_override${CMAKE_SHARED_LIBRARY_SUFFIX})

# poly1305_override
add_library(poly1305_override SHARED
            poly1305_override.c
            ${libssh_SOURCE_DIR}/src/external/poly1305.c
           )
set(POLY1305_OVERRIDE_LIBRARY
${libssh_BINARY_DIR}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}poly1305_override${CMAKE_SHARED_LIBRARY_SUFFIX})

if (WITH_GCRYPT)
    set (override_src
        ${libssh_SOURCE_DIR}/src/getrandom_gcrypt.c
        ${libssh_SOURCE_DIR}/src/md_gcrypt.c
    )
    set(override_libs
        ${GCRYPT_LIBRARIES}
    )
elseif (WITH_MBEDTLS)
    set (override_src
        ${libssh_SOURCE_DIR}/src/getrandom_mbedcrypto.c
        ${libssh_SOURCE_DIR}/src/md_mbedcrypto.c
    )
    set(override_libs
        ${MBEDTLS_CRYPTO_LIBRARY}
    )
else ()
    set (override_src
        ${libssh_SOURCE_DIR}/src/getrandom_crypto.c
        ${libssh_SOURCE_DIR}/src/md_crypto.c
    )
    set(override_libs
        OpenSSL::Crypto
    )
endif (WITH_GCRYPT)

# ed25519_override
add_library(ed25519_override SHARED
    ed25519_override.c
    ${libssh_SOURCE_DIR}/src/external/fe25519.c
    ${libssh_SOURCE_DIR}/src/external/ge25519.c
    ${libssh_SOURCE_DIR}/src/external/sc25519.c
    ${libssh_SOURCE_DIR}/src/external/ed25519.c
    ${override_src}
    )
target_link_libraries(ed25519_override
                      PRIVATE ${override_libs})
set(ED25519_OVERRIDE_LIBRARY
${libssh_BINARY_DIR}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}ed25519_override${CMAKE_SHARED_LIBRARY_SUFFIX})

# curve25519_override
add_library(curve25519_override SHARED
    curve25519_override.c
    ${libssh_SOURCE_DIR}/src/external/curve25519_ref.c
    ${libssh_SOURCE_DIR}/src/external/fe25519.c
    ${libssh_SOURCE_DIR}/src/external/ge25519.c
    ${libssh_SOURCE_DIR}/src/external/sc25519.c
    ${libssh_SOURCE_DIR}/src/external/ed25519.c
    ${override_src}
)
target_link_libraries(curve25519_override
                      PRIVATE ${override_libs})
set(CURVE25519_OVERRIDE_LIBRARY
${libssh_BINARY_DIR}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}curve25519_override${CMAKE_SHARED_LIBRARY_SUFFIX})

set(OVERRIDE_LIBRARIES
    ${CHACHA20_OVERRIDE_LIBRARY}:${POLY1305_OVERRIDE_LIBRARY}:${ED25519_OVERRIDE_LIBRARY}:${CURVE25519_OVERRIDE_LIBRARY}
)

if (WITH_MBEDTLS)
    if (HAVE_MBEDTLS_CHACHA20_H AND HAVE_MBEDTLS_POLY1305_H)
        list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_CHACHAPOLY=0")
    else ()
        list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_CHACHAPOLY=1")
    endif ()
    list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_ED25519=1")
    list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_CURVE25519=1")
elseif (WITH_GCRYPT)
    if (HAVE_GCRYPT_CHACHA_POLY)
        list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_CHACHAPOLY=0")
    else ()
        list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_CHACHAPOLY=1")
    endif ()
    list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_ED25519=1")
    list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_CURVE25519=1")
else ()
    if (HAVE_OPENSSL_EVP_CHACHA20)
        list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_CHACHAPOLY=0")
    else ()
        list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_CHACHAPOLY=1")
    endif ()
    list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_CURVE25519=0")
    list(APPEND OVERRIDE_RESULTS "-DSHOULD_CALL_INTERNAL_ED25519=0")
endif ()

if (NOT OSX)
    # Remove any preload string from the environment variables list
    foreach(env_string ${TORTURE_ENVIRONMENT})
        if (${env_string} MATCHES "^LD_PRELOAD=*")
            list(REMOVE_ITEM TORTURE_ENVIRONMENT ${env_string})
            set(PRELOAD_STRING "${env_string}:")
        endif ()
    endforeach ()

    if ("${PRELOAD_STRING}" STREQUAL "")
        set(PRELOAD_STRING "LD_PRELOAD=")
    endif ()

    list(APPEND TORTURE_ENVIRONMENT
         "${PRELOAD_STRING}${OVERRIDE_LIBRARIES}")
endif()

foreach(_OVERRIDE_TEST ${LIBSSH_OVERRIDE_TESTS})
    add_cmocka_test(${_OVERRIDE_TEST}
                    SOURCES ${_OVERRIDE_TEST}.c
                    COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS}
                        ${OVERRIDE_RESULTS}
                    LINK_LIBRARIES
                        ${TORTURE_SHARED_LIBRARY}
                        chacha20_override
                        poly1305_override
                        ed25519_override
                        curve25519_override
    )

    if (OSX)
        set_property(
            TEST
                ${_OVERRIDE_TEST}
            PROPERTY
            ENVIRONMENT DYLD_FORCE_FLAT_NAMESPACE=1;DYLD_INSERT_LIBRARIES=${OVERRIDE_LIBRARIES})

    else ()
        set_property(
            TEST
                ${_OVERRIDE_TEST}
            PROPERTY
                ENVIRONMENT ${TORTURE_ENVIRONMENT})

    endif()
endforeach()