Skip to content

Commit

Permalink
Merge 8b04384 into f8434ce
Browse files Browse the repository at this point in the history
  • Loading branch information
gocarlos committed Apr 29, 2020
2 parents f8434ce + 8b04384 commit 7cde9e8
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 39 deletions.
12 changes: 8 additions & 4 deletions CMakeLists.txt
Expand Up @@ -83,7 +83,7 @@ endif()
if(NOT COSE_C_INCLUDE_SIGN1)
add_definitions(-DINCLUDE_SIGN1=0)
endif()
if (NOT COSE_C_INCLUDE_COUNTERSIGN)
if(NOT COSE_C_INCLUDE_COUNTERSIGN)
add_definitions(-DINCLUDE_COUNTERSIGN=0)
endif()
if(COSE_C_USE_CONTEXT)
Expand All @@ -98,8 +98,8 @@ if(versbose)
endif()

# Control the order of packages - get the latest not the first
SET(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)
set(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
set(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)

###############################################################################
# DOCS
Expand Down Expand Up @@ -186,7 +186,8 @@ else()
endif()

if(COSE_C_USE_MBEDTLS)
add_definitions(-DUSE_MBED_TLS)
add_definitions(-DCOSE_C_USE_MBEDTLS)
set(COSE_C_USE_OPENSSL OFF)

if(COSE_C_USE_FIND_PACKAGE)
find_package(MbedTLS)
Expand Down Expand Up @@ -219,6 +220,7 @@ if(COSE_C_USE_MBEDTLS)
endif()
else()
find_package(OpenSSL REQUIRED)
set(COSE_C_USE_OPENSSL ON)
endif()

###############################################################################
Expand Down Expand Up @@ -272,3 +274,5 @@ message(STATUS "CMAKE_CXX_COMPILER:..............${CMAKE_CXX_COMPILER}")
message(STATUS "CLANG_TIDY_EXE:..................${CLANG_TIDY_EXE}")
message(STATUS "project_cn_cbor_SOURCE_DIR:......${project_cn_cbor_SOURCE_DIR}")
message(STATUS "project_cn_cbor_BINARY_DIR:......${project_cn_cbor_BINARY_DIR}")
message(STATUS "project_mbedtls_SOURCE_DIR:......${project_mbedtls_SOURCE_DIR}")
message(STATUS "project_mbedtls_BINARY_DIR:......${project_mbedtls_BINARY_DIR}")
40 changes: 22 additions & 18 deletions include/cose/cose_configure.h → include/cose/cose_configure.h.in
Expand Up @@ -4,17 +4,21 @@

#pragma once

#if defined(USE_MBED_TLS)
#if defined(USE_OPEN_SSL) || defined(USE_BCRYPT)
#cmakedefine COSE_C_USE_MBEDTLS
#cmakedefine COSE_C_USE_OPENSSL
#cmakedefine COSE_C_USE_BCRYPT

#if defined(COSE_C_USE_MBEDTLS)
#if defined(COSE_C_USE_OPENSSL) || defined(COSE_C_USE_BCRYPT)
#error Only Define One Crypto Package
#endif
#elif defined(USE_BCRYPT)
#if defined(USE_OPENSSL)
#elif defined(COSE_C_USE_BCRYPT)
#if defined(COSE_C_USE_OPENSSL)
#error Only Define One Crypto Package
#endif
#elif !defined(USE_OPEN_SSL)
#elif !defined(COSE_C_USE_OPENSSL)
#include <openssl/opensslv.h>
#define USE_OPEN_SSL
#define COSE_C_USE_OPENSSL
#endif

//
Expand Down Expand Up @@ -62,20 +66,20 @@
// Define which AES CBC-MAC algorithms are to be used
//

#if !defined(USE_MBED_TLS)
#if !defined(COSE_C_USE_MBEDTLS)

#define USE_AES_CBC_MAC_128_64
#define USE_AES_CBC_MAC_128_128
#define USE_AES_CBC_MAC_256_64
#define USE_AES_CBC_MAC_256_128

#endif // !defined(USE_MBED_TLS)
#endif // !defined(COSE_C_USE_MBEDTLS)

//
// Define which ECDH algorithms are to be used
//

#if !defined(USE_MBED_TLS)
#if !defined(COSE_C_USE_MBEDTLS)
#define USE_ECDH_ES_HKDF_256
#define USE_ECDH_ES_HKDF_512
#define USE_ECDH_SS_HKDF_256
Expand All @@ -85,9 +89,9 @@
#define USE_ECDH 1
#define USE_HKDF_SHA2 1
#endif
#endif // !defined(USE_MBED_TLS)
#endif // !defined(COSE_C_USE_MBEDTLS)

#if !defined(USE_MBED_TLS)
#if !defined(COSE_C_USE_MBEDTLS)
#define USE_ECDH_ES_A128KW
#define USE_ECDH_ES_A192KW
#define USE_ECDH_ES_A256KW
Expand All @@ -100,23 +104,23 @@
#define USE_ECDH 1
#define USE_HKDF_AES 1
#endif
#endif // !defined(USE_MBED_TLS)
#endif // !defined(COSE_C_USE_MBEDTLS)

//
// Define which Key Wrap functions are to be used
//

#if !defined(USE_MBED_TLS)
#if !defined(COSE_C_USE_MBEDTLS)
#define USE_AES_KW_128
#define USE_AES_KW_192
#define USE_AES_KW_256
#endif // !defined(USE_MBED_TLS)
#endif // !defined(COSE_C_USE_MBEDTLS)

//
// Define which of the DIRECT + KDF algorithms are to be used
//

#if !defined(USE_MBED_TLS)
#if !defined(COSE_C_USE_MBEDTLS)
#define USE_Direct_HKDF_HMAC_SHA_256
#define USE_Direct_HKDF_HMAC_SHA_512
#define USE_Direct_HKDF_AES_128
Expand All @@ -128,7 +132,7 @@
#if defined(USE_Direct_HKDF_AES_128) || defined(USE_Direct_KDF_AES_256)
#define USE_HKDF_AES 1
#endif
#endif // !defined(USE_MBED_TLS)
#endif // !defined(COSE_C_USE_MBEDTLS)

//
// Define which of the signature algorithms are to be used
Expand All @@ -137,7 +141,7 @@
#define USE_ECDSA_SHA_256
#define USE_ECDSA_SHA_384
#define USE_ECDSA_SHA_512
#if !defined(USE_MBED_TLS)
#if !defined(COSE_C_USE_MBEDTLS)
// MBEDTLS currently supports ECDH for X25519 but not EdDSA
#if OPENSSL_VERSION_NUMBER > 0x10100000L
// Requires OPEN SSL 1.1.1 to build
Expand All @@ -147,7 +151,7 @@
#define TOSTRING(x) STRINGIFY(x)
#pragma message("OPENSSL VERSION IS " OPENSSL_VERSION_TEXT)
#endif
#endif // !defined (USE_MBED_TLS)
#endif // !defined (COSE_C_USE_MBEDTLS)

//
// Define which COSE objects are included
Expand Down
16 changes: 14 additions & 2 deletions src/CMakeLists.txt
Expand Up @@ -22,6 +22,11 @@ endif()

add_library(${PROJECT_NAME})

# some structs look differently depending on the crypto lib used
# therefore we create the config header file at configure time.
configure_file(${CMAKE_CURRENT_LIST_DIR}/../include/cose/cose_configure.h.in
${CMAKE_BINARY_DIR}/include/cose/cose_configure.h)

if(COSE_C_USE_MBEDTLS)
set(cose_crypto mbedtls.c)
else()
Expand All @@ -30,7 +35,7 @@ endif()

set(cose_sources
${PROJECT_SOURCE_DIR}/include/cose/cose.h
${PROJECT_SOURCE_DIR}/include/cose/cose_configure.h
${CMAKE_BINARY_DIR}/include/cose/cose_configure.h
crypto.h
cose_int.h
crypto.h
Expand All @@ -52,7 +57,8 @@ target_sources(${PROJECT_NAME} PRIVATE ${cose_sources})

target_include_directories(
${PROJECT_NAME}
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE ../src)
target_link_libraries(${PROJECT_NAME} PRIVATE cn-cbor::cn-cbor)

Expand Down Expand Up @@ -125,3 +131,9 @@ install(
COMPONENT dev
FILES_MATCHING
PATTERN "*.h")
install(
DIRECTORY ${CMAKE_BINARY_DIR}/include/cose
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT dev
FILES_MATCHING
PATTERN "*.h")
14 changes: 7 additions & 7 deletions src/Sign1.c
@@ -1,4 +1,4 @@
/** \file Sign.c
/** \file Sign1.c
* Contains implementation of the functions related to HCOSE_SIGN handle
* objects.
*/
Expand All @@ -12,10 +12,10 @@

#if INCLUDE_SIGN1

bool _COSE_Signer0_sign(COSE_Sign1Message *pSigner,
bool _COSE_Signer1_sign(COSE_Sign1Message *pSigner,
const cn_cbor *pKey,
cose_errback *perr);
bool _COSE_Signer0_validate(COSE_Sign1Message *pSign,
bool _COSE_Signer1_validate(COSE_Sign1Message *pSign,
const cn_cbor *pKey,
cose_errback *perr);
void _COSE_Sign1_Release(COSE_Sign1Message *p);
Expand Down Expand Up @@ -237,7 +237,7 @@ bool COSE_Sign1_Sign(HCOSE_SIGN1 h, const cn_cbor *pKey, cose_errback *perr)
goto errorReturn;
}

if (!_COSE_Signer0_sign(pMessage, pKey, perr)) {
if (!_COSE_Signer1_sign(pMessage, pKey, perr)) {
goto errorReturn;
}

Expand Down Expand Up @@ -274,7 +274,7 @@ bool COSE_Sign1_validate(HCOSE_SIGN1 hSign,
CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES,
COSE_ERR_INVALID_PARAMETER);

f = _COSE_Signer0_validate(pSign, pKey, perr);
f = _COSE_Signer1_validate(pSign, pKey, perr);

return f;

Expand Down Expand Up @@ -405,7 +405,7 @@ static bool CreateSign1AAD(COSE_Sign1Message *pMessage,
return false;
}

bool _COSE_Signer0_sign(COSE_Sign1Message *pSigner,
bool _COSE_Signer1_sign(COSE_Sign1Message *pSigner,
const cn_cbor *pKey,
cose_errback *perr)
{
Expand Down Expand Up @@ -501,7 +501,7 @@ bool _COSE_Signer0_sign(COSE_Sign1Message *pSigner,
return f;
}

bool _COSE_Signer0_validate(COSE_Sign1Message *pSign,
bool _COSE_Signer1_validate(COSE_Sign1Message *pSign,
const cn_cbor *pKey,
cose_errback *perr)
{
Expand Down
4 changes: 2 additions & 2 deletions src/bcrypt.c
Expand Up @@ -3,7 +3,7 @@
#include "cose_int.h"
#include "crypto.h"

#if USE_BCRYPT
#if COSE_C_USE_BCRYPT

#include <Windows.h>

Expand Down Expand Up @@ -77,4 +77,4 @@ bool AES_CCM_Encrypt(COSE_Encrypt *pcose,
return true;
}

#endif // USE_BCRYPT
#endif // COSE_C_USE_BCRYPT
4 changes: 2 additions & 2 deletions src/mbedtls.c
Expand Up @@ -9,7 +9,7 @@
#endif
#include <stdlib.h>

#ifdef USE_MBED_TLS
#ifdef COSE_C_USE_MBEDTLS

#include "mbedtls/ccm.h"
#include "mbedtls/md.h"
Expand Down Expand Up @@ -1600,4 +1600,4 @@ bool ECDH_ComputeSecret(COSE *pRecipient,
return fRet;
}
#endif // USE_ECDH
#endif // USE_MBED_TLS
#endif // COSE_C_USE_MBEDTLS
4 changes: 2 additions & 2 deletions src/openssl.c
Expand Up @@ -7,7 +7,7 @@
#include <memory.h>
#include <stdbool.h>

#ifdef USE_OPEN_SSL
#ifdef COSE_C_USE_OPENSSL

#include <openssl/evp.h>
#include <openssl/aes.h>
Expand Down Expand Up @@ -1779,4 +1779,4 @@ bool ECDH_ComputeSecret(COSE *pRecipient,
return fRet;
}

#endif // USE_OPEN_SSL
#endif // COSE_C_USE_OPENSSL
4 changes: 2 additions & 2 deletions test/test.c
Expand Up @@ -21,7 +21,7 @@
#include "json.h"
#include "test.h"

#ifdef USE_MBED_TLS
#ifdef COSE_C_USE_MBEDTLS
#include "mbedtls/entropy.h"
#endif

Expand Down Expand Up @@ -1315,7 +1315,7 @@ int main(int argc, char** argv)
}
}

#ifdef USE_MBED_TLS
#ifdef COSE_C_USE_MBEDTLS
mbedtls_entropy_context entropy;
mbedtls_entropy_init(&entropy);
#endif
Expand Down

0 comments on commit 7cde9e8

Please sign in to comment.