diff --git a/.gitignore b/.gitignore index e10443cc0..e7b477883 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .DS_Store /.build +/out /Packages /*.xcodeproj Package.pins @@ -13,3 +14,4 @@ DerivedData **/gyb.pyc .*.sw[nop] + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 000000000..6c5314192 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,43 @@ +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftCrypto open source project +## +## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.md for the list of SwiftCrypto project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +cmake_minimum_required(VERSION 3.15.1) + +project(SwiftCrypto + LANGUAGES ASM C Swift) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules) + +include(SwiftSupport) + +if(CMAKE_VERSION VERSION_LESS 3.16 AND CMAKE_SYSTEM_NAME STREQUAL Windows) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +else() + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +endif() +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift) + +if(CMAKE_SYSTEM_NAME STREQUAL Windows OR CMAKE_SYSTEM_NAME STREQUAL Darwin) + option(BUILD_SHARED_LIBS "Build shared libraries by default" YES) +endif() + +if(BUILD_SHARED_LIBS) + set(CMAKE_POSITION_INDEPENDENT_CODE YES) +endif() + +add_subdirectory(Sources) +add_subdirectory(cmake/modules) diff --git a/Sources/CCryptoBoringSSL/CMakeLists.txt b/Sources/CCryptoBoringSSL/CMakeLists.txt new file mode 100644 index 000000000..0a47c0886 --- /dev/null +++ b/Sources/CCryptoBoringSSL/CMakeLists.txt @@ -0,0 +1,370 @@ +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftCrypto open source project +## +## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.md for the list of SwiftCrypto project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +add_library(CCryptoBoringSSL STATIC + "crypto/asn1/a_bitstr.c" + "crypto/asn1/a_bool.c" + "crypto/asn1/a_d2i_fp.c" + "crypto/asn1/a_dup.c" + "crypto/asn1/a_enum.c" + "crypto/asn1/a_gentm.c" + "crypto/asn1/a_i2d_fp.c" + "crypto/asn1/a_int.c" + "crypto/asn1/a_mbstr.c" + "crypto/asn1/a_object.c" + "crypto/asn1/a_octet.c" + "crypto/asn1/a_print.c" + "crypto/asn1/a_strnid.c" + "crypto/asn1/a_time.c" + "crypto/asn1/a_type.c" + "crypto/asn1/a_utctm.c" + "crypto/asn1/a_utf8.c" + "crypto/asn1/asn1_lib.c" + "crypto/asn1/asn1_par.c" + "crypto/asn1/asn_pack.c" + "crypto/asn1/f_enum.c" + "crypto/asn1/f_int.c" + "crypto/asn1/f_string.c" + "crypto/asn1/tasn_dec.c" + "crypto/asn1/tasn_enc.c" + "crypto/asn1/tasn_fre.c" + "crypto/asn1/tasn_new.c" + "crypto/asn1/tasn_typ.c" + "crypto/asn1/tasn_utl.c" + "crypto/asn1/time_support.c" + "crypto/base64/base64.c" + "crypto/bio/bio.c" + "crypto/bio/bio_mem.c" + "crypto/bio/connect.c" + "crypto/bio/fd.c" + "crypto/bio/file.c" + "crypto/bio/hexdump.c" + "crypto/bio/pair.c" + "crypto/bio/printf.c" + "crypto/bio/socket.c" + "crypto/bio/socket_helper.c" + "crypto/bn_extra/bn_asn1.c" + "crypto/bn_extra/convert.c" + "crypto/buf/buf.c" + "crypto/bytestring/asn1_compat.c" + "crypto/bytestring/ber.c" + "crypto/bytestring/cbb.c" + "crypto/bytestring/cbs.c" + "crypto/bytestring/unicode.c" + "crypto/chacha/chacha.c" + "crypto/cipher_extra/cipher_extra.c" + "crypto/cipher_extra/derive_key.c" + "crypto/cipher_extra/e_aesccm.c" + "crypto/cipher_extra/e_aesctrhmac.c" + "crypto/cipher_extra/e_aesgcmsiv.c" + "crypto/cipher_extra/e_chacha20poly1305.c" + "crypto/cipher_extra/e_null.c" + "crypto/cipher_extra/e_rc2.c" + "crypto/cipher_extra/e_rc4.c" + "crypto/cipher_extra/e_tls.c" + "crypto/cipher_extra/tls_cbc.c" + "crypto/cmac/cmac.c" + "crypto/conf/conf.c" + "crypto/cpu-aarch64-fuchsia.c" + "crypto/cpu-aarch64-linux.c" + "crypto/cpu-arm-linux.c" + "crypto/cpu-arm.c" + "crypto/cpu-intel.c" + "crypto/cpu-ppc64le.c" + "crypto/crypto.c" + "crypto/curve25519/curve25519.c" + "crypto/curve25519/spake25519.c" + "crypto/dh/check.c" + "crypto/dh/dh.c" + "crypto/dh/dh_asn1.c" + "crypto/dh/params.c" + "crypto/digest_extra/digest_extra.c" + "crypto/dsa/dsa.c" + "crypto/dsa/dsa_asn1.c" + "crypto/ec_extra/ec_asn1.c" + "crypto/ec_extra/ec_derive.c" + "crypto/ec_extra/hash_to_curve.c" + "crypto/ecdh_extra/ecdh_extra.c" + "crypto/ecdsa_extra/ecdsa_asn1.c" + "crypto/engine/engine.c" + "crypto/err/err.c" + "crypto/err/err_data.c" + "crypto/evp/digestsign.c" + "crypto/evp/evp.c" + "crypto/evp/evp_asn1.c" + "crypto/evp/evp_ctx.c" + "crypto/evp/p_dsa_asn1.c" + "crypto/evp/p_ec.c" + "crypto/evp/p_ec_asn1.c" + "crypto/evp/p_ed25519.c" + "crypto/evp/p_ed25519_asn1.c" + "crypto/evp/p_rsa.c" + "crypto/evp/p_rsa_asn1.c" + "crypto/evp/p_x25519.c" + "crypto/evp/p_x25519_asn1.c" + "crypto/evp/pbkdf.c" + "crypto/evp/print.c" + "crypto/evp/scrypt.c" + "crypto/evp/sign.c" + "crypto/ex_data.c" + "crypto/fipsmodule/aes/aes.c" + "crypto/fipsmodule/aes/aes_nohw.c" + "crypto/fipsmodule/aes/key_wrap.c" + "crypto/fipsmodule/aes/mode_wrappers.c" + "crypto/fipsmodule/bn/add.c" + "crypto/fipsmodule/bn/asm/x86_64-gcc.c" + "crypto/fipsmodule/bn/bn.c" + "crypto/fipsmodule/bn/bytes.c" + "crypto/fipsmodule/bn/cmp.c" + "crypto/fipsmodule/bn/ctx.c" + "crypto/fipsmodule/bn/div.c" + "crypto/fipsmodule/bn/div_extra.c" + "crypto/fipsmodule/bn/exponentiation.c" + "crypto/fipsmodule/bn/gcd.c" + "crypto/fipsmodule/bn/gcd_extra.c" + "crypto/fipsmodule/bn/generic.c" + "crypto/fipsmodule/bn/jacobi.c" + "crypto/fipsmodule/bn/montgomery.c" + "crypto/fipsmodule/bn/montgomery_inv.c" + "crypto/fipsmodule/bn/mul.c" + "crypto/fipsmodule/bn/prime.c" + "crypto/fipsmodule/bn/random.c" + "crypto/fipsmodule/bn/rsaz_exp.c" + "crypto/fipsmodule/bn/shift.c" + "crypto/fipsmodule/bn/sqrt.c" + "crypto/fipsmodule/cipher/aead.c" + "crypto/fipsmodule/cipher/cipher.c" + "crypto/fipsmodule/cipher/e_aes.c" + "crypto/fipsmodule/cipher/e_des.c" + "crypto/fipsmodule/des/des.c" + "crypto/fipsmodule/digest/digest.c" + "crypto/fipsmodule/digest/digests.c" + "crypto/fipsmodule/ec/ec.c" + "crypto/fipsmodule/ec/ec_key.c" + "crypto/fipsmodule/ec/ec_montgomery.c" + "crypto/fipsmodule/ec/felem.c" + "crypto/fipsmodule/ec/oct.c" + "crypto/fipsmodule/ec/p224-64.c" + "crypto/fipsmodule/ec/p256-x86_64.c" + "crypto/fipsmodule/ec/p256.c" + "crypto/fipsmodule/ec/scalar.c" + "crypto/fipsmodule/ec/simple.c" + "crypto/fipsmodule/ec/simple_mul.c" + "crypto/fipsmodule/ec/util.c" + "crypto/fipsmodule/ec/wnaf.c" + "crypto/fipsmodule/ecdh/ecdh.c" + "crypto/fipsmodule/ecdsa/ecdsa.c" + "crypto/fipsmodule/fips_shared_support.c" + "crypto/fipsmodule/hmac/hmac.c" + "crypto/fipsmodule/is_fips.c" + "crypto/fipsmodule/md4/md4.c" + "crypto/fipsmodule/md5/md5.c" + "crypto/fipsmodule/modes/cbc.c" + "crypto/fipsmodule/modes/cfb.c" + "crypto/fipsmodule/modes/ctr.c" + "crypto/fipsmodule/modes/gcm.c" + "crypto/fipsmodule/modes/gcm_nohw.c" + "crypto/fipsmodule/modes/ofb.c" + "crypto/fipsmodule/modes/polyval.c" + "crypto/fipsmodule/rand/ctrdrbg.c" + "crypto/fipsmodule/rand/fork_detect.c" + "crypto/fipsmodule/rand/rand.c" + "crypto/fipsmodule/rand/urandom.c" + "crypto/fipsmodule/rsa/blinding.c" + "crypto/fipsmodule/rsa/padding.c" + "crypto/fipsmodule/rsa/rsa.c" + "crypto/fipsmodule/rsa/rsa_impl.c" + "crypto/fipsmodule/self_check/self_check.c" + "crypto/fipsmodule/sha/sha1-altivec.c" + "crypto/fipsmodule/sha/sha1.c" + "crypto/fipsmodule/sha/sha256.c" + "crypto/fipsmodule/sha/sha512.c" + "crypto/fipsmodule/tls/kdf.c" + "crypto/hkdf/hkdf.c" + "crypto/hpke/hpke.c" + "crypto/hrss/hrss.c" + "crypto/lhash/lhash.c" + "crypto/mem.c" + "crypto/obj/obj.c" + "crypto/obj/obj_xref.c" + "crypto/pem/pem_all.c" + "crypto/pem/pem_info.c" + "crypto/pem/pem_lib.c" + "crypto/pem/pem_oth.c" + "crypto/pem/pem_pk8.c" + "crypto/pem/pem_pkey.c" + "crypto/pem/pem_x509.c" + "crypto/pem/pem_xaux.c" + "crypto/pkcs7/pkcs7.c" + "crypto/pkcs7/pkcs7_x509.c" + "crypto/pkcs8/p5_pbev2.c" + "crypto/pkcs8/pkcs8.c" + "crypto/pkcs8/pkcs8_x509.c" + "crypto/poly1305/poly1305.c" + "crypto/poly1305/poly1305_arm.c" + "crypto/poly1305/poly1305_vec.c" + "crypto/pool/pool.c" + "crypto/rand_extra/deterministic.c" + "crypto/rand_extra/forkunsafe.c" + "crypto/rand_extra/fuchsia.c" + "crypto/rand_extra/rand_extra.c" + "crypto/rand_extra/windows.c" + "crypto/rc4/rc4.c" + "crypto/refcount_c11.c" + "crypto/refcount_lock.c" + "crypto/rsa_extra/rsa_asn1.c" + "crypto/rsa_extra/rsa_print.c" + "crypto/siphash/siphash.c" + "crypto/stack/stack.c" + "crypto/thread.c" + "crypto/thread_none.c" + "crypto/thread_pthread.c" + "crypto/thread_win.c" + "crypto/trust_token/pmbtoken.c" + "crypto/trust_token/trust_token.c" + "crypto/trust_token/voprf.c" + "crypto/x509/a_digest.c" + "crypto/x509/a_sign.c" + "crypto/x509/a_strex.c" + "crypto/x509/a_verify.c" + "crypto/x509/algorithm.c" + "crypto/x509/asn1_gen.c" + "crypto/x509/by_dir.c" + "crypto/x509/by_file.c" + "crypto/x509/i2d_pr.c" + "crypto/x509/rsa_pss.c" + "crypto/x509/t_crl.c" + "crypto/x509/t_req.c" + "crypto/x509/t_x509.c" + "crypto/x509/t_x509a.c" + "crypto/x509/x509.c" + "crypto/x509/x509_att.c" + "crypto/x509/x509_cmp.c" + "crypto/x509/x509_d2.c" + "crypto/x509/x509_def.c" + "crypto/x509/x509_ext.c" + "crypto/x509/x509_lu.c" + "crypto/x509/x509_obj.c" + "crypto/x509/x509_r2x.c" + "crypto/x509/x509_req.c" + "crypto/x509/x509_set.c" + "crypto/x509/x509_trs.c" + "crypto/x509/x509_txt.c" + "crypto/x509/x509_v3.c" + "crypto/x509/x509_vfy.c" + "crypto/x509/x509_vpm.c" + "crypto/x509/x509cset.c" + "crypto/x509/x509name.c" + "crypto/x509/x509rset.c" + "crypto/x509/x509spki.c" + "crypto/x509/x_algor.c" + "crypto/x509/x_all.c" + "crypto/x509/x_attrib.c" + "crypto/x509/x_crl.c" + "crypto/x509/x_exten.c" + "crypto/x509/x_info.c" + "crypto/x509/x_name.c" + "crypto/x509/x_pkey.c" + "crypto/x509/x_pubkey.c" + "crypto/x509/x_req.c" + "crypto/x509/x_sig.c" + "crypto/x509/x_spki.c" + "crypto/x509/x_val.c" + "crypto/x509/x_x509.c" + "crypto/x509/x_x509a.c" + "crypto/x509v3/pcy_cache.c" + "crypto/x509v3/pcy_data.c" + "crypto/x509v3/pcy_lib.c" + "crypto/x509v3/pcy_map.c" + "crypto/x509v3/pcy_node.c" + "crypto/x509v3/pcy_tree.c" + "crypto/x509v3/v3_akey.c" + "crypto/x509v3/v3_akeya.c" + "crypto/x509v3/v3_alt.c" + "crypto/x509v3/v3_bcons.c" + "crypto/x509v3/v3_bitst.c" + "crypto/x509v3/v3_conf.c" + "crypto/x509v3/v3_cpols.c" + "crypto/x509v3/v3_crld.c" + "crypto/x509v3/v3_enum.c" + "crypto/x509v3/v3_extku.c" + "crypto/x509v3/v3_genn.c" + "crypto/x509v3/v3_ia5.c" + "crypto/x509v3/v3_info.c" + "crypto/x509v3/v3_int.c" + "crypto/x509v3/v3_lib.c" + "crypto/x509v3/v3_ncons.c" + "crypto/x509v3/v3_ocsp.c" + "crypto/x509v3/v3_pci.c" + "crypto/x509v3/v3_pcia.c" + "crypto/x509v3/v3_pcons.c" + "crypto/x509v3/v3_pmaps.c" + "crypto/x509v3/v3_prn.c" + "crypto/x509v3/v3_purp.c" + "crypto/x509v3/v3_skey.c" + "crypto/x509v3/v3_utl.c") + +if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64") + if(CMAKE_SYSTEM_NAME STREQUAL Darwin) + target_sources(CCryptoBoringSSL PRIVATE + crypto/chacha/chacha-x86_64.mac.x86_64.S + crypto/cipher_extra/aes128gcmsiv-x86_64.mac.x86_64.S + crypto/cipher_extra/chacha20_poly1305_x86_64.mac.x86_64.S + crypto/fipsmodule/aesni-gcm-x86_64.mac.x86_64.S + crypto/fipsmodule/aesni-x86_64.mac.x86_64.S + crypto/fipsmodule/ghash-ssse3-x86_64.mac.x86_64.S + crypto/fipsmodule/ghash-x86_64.mac.x86_64.S + crypto/fipsmodule/md5-x86_64.mac.x86_64.S + crypto/fipsmodule/p256-x86_64-asm.mac.x86_64.S + crypto/fipsmodule/p256_beeu-x86_64-asm.mac.x86_64.S + crypto/fipsmodule/rdrand-x86_64.mac.x86_64.S + crypto/fipsmodule/rsaz-avx2.mac.x86_64.S + crypto/fipsmodule/sha1-x86_64.mac.x86_64.S + crypto/fipsmodule/sha256-x86_64.mac.x86_64.S + crypto/fipsmodule/sha512-x86_64.mac.x86_64.S + crypto/fipsmodule/vpaes-x86_64.mac.x86_64.S + crypto/fipsmodule/x86_64-mont.mac.x86_64.S + crypto/fipsmodule/x86_64-mont5.mac.x86_64.S) + elseif(CMAKE_SYSTEM_NAME STREQUAL Linux) + target_sources(CCryptoBoringSSL PRIVATE + crypto/chacha/chacha-x86_64.linux.x86_64.S + crypto/cipher_extra/aes128gcmsiv-x86_64.linux.x86_64.S + crypto/cipher_extra/chacha20_poly1305_x86_64.linux.x86_64.S + crypto/fipsmodule/aesni-gcm-x86_64.linux.x86_64.S + crypto/fipsmodule/aesni-x86_64.linux.x86_64.S + crypto/fipsmodule/ghash-ssse3-x86_64.linux.x86_64.S + crypto/fipsmodule/ghash-x86_64.linux.x86_64.S + crypto/fipsmodule/md5-x86_64.linux.x86_64.S + crypto/fipsmodule/p256-x86_64-asm.linux.x86_64.S + crypto/fipsmodule/p256_beeu-x86_64-asm.linux.x86_64.S + crypto/fipsmodule/rdrand-x86_64.linux.x86_64.S + crypto/fipsmodule/rsaz-avx2.linux.x86_64.S + crypto/fipsmodule/sha1-x86_64.linux.x86_64.S + crypto/fipsmodule/sha256-x86_64.linux.x86_64.S + crypto/fipsmodule/sha512-x86_64.linux.x86_64.S + crypto/fipsmodule/vpaes-x86_64.linux.x86_64.S + crypto/fipsmodule/x86_64-mont.linux.x86_64.S + crypto/fipsmodule/x86_64-mont5.linux.x86_64.S) + endif() +endif() + +target_include_directories(CCryptoBoringSSL PUBLIC + include) + +target_compile_definitions(CCryptoBoringSSL PRIVATE + $<$:WIN32_LEAN_AND_MEAN>) +set_target_properties(CCryptoBoringSSL PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) + +set_property(GLOBAL APPEND PROPERTY SWIFT_CRYPTO_EXPORTS CCryptoBoringSSL) diff --git a/Sources/CCryptoBoringSSL/include/module.modulemap b/Sources/CCryptoBoringSSL/include/module.modulemap new file mode 100644 index 000000000..6c5352d53 --- /dev/null +++ b/Sources/CCryptoBoringSSL/include/module.modulemap @@ -0,0 +1,4 @@ +module CCryptoBoringSSL { + header "CCryptoBoringSSL.h" + export * +} diff --git a/Sources/CCryptoBoringSSLShims/CMakeLists.txt b/Sources/CCryptoBoringSSLShims/CMakeLists.txt new file mode 100644 index 000000000..5550b15e9 --- /dev/null +++ b/Sources/CCryptoBoringSSLShims/CMakeLists.txt @@ -0,0 +1,28 @@ +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftCrypto open source project +## +## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.md for the list of SwiftCrypto project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +add_library(CCryptoBoringSSLShims STATIC + "shims.c") + +target_include_directories(CCryptoBoringSSLShims PUBLIC + include + $) + +target_link_libraries(CCryptoBoringSSLShims PUBLIC + CCryptoBoringSSL) + +set_target_properties(CCryptoBoringSSLShims PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) + +set_property(GLOBAL APPEND PROPERTY SWIFT_CRYPTO_EXPORTS CCryptoBoringSSLShims) diff --git a/Sources/CCryptoBoringSSLShims/include/module.modulemap b/Sources/CCryptoBoringSSLShims/include/module.modulemap new file mode 100644 index 000000000..533962ae3 --- /dev/null +++ b/Sources/CCryptoBoringSSLShims/include/module.modulemap @@ -0,0 +1,4 @@ +module CCryptoBoringSSLShims { + header "CCryptoBoringSSLShims.h" + export * +} diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt new file mode 100644 index 000000000..7f1909f72 --- /dev/null +++ b/Sources/CMakeLists.txt @@ -0,0 +1,17 @@ +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftCrypto open source project +## +## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.md for the list of SwiftCrypto project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +add_subdirectory(CCryptoBoringSSL) +add_subdirectory(CCryptoBoringSSLShims) +add_subdirectory(Crypto) diff --git a/Sources/Crypto/CMakeLists.txt b/Sources/Crypto/CMakeLists.txt new file mode 100644 index 000000000..683528f1f --- /dev/null +++ b/Sources/Crypto/CMakeLists.txt @@ -0,0 +1,89 @@ +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftCrypto open source project +## +## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.md for the list of SwiftCrypto project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +add_library(Crypto + "AEADs/AES/GCM/AES-GCM.swift" + "AEADs/AES/GCM/BoringSSL/AES-GCM_boring.swift" + "AEADs/ChachaPoly/BoringSSL/ChaChaPoly_boring.swift" + "AEADs/ChachaPoly/ChaChaPoly.swift" + "AEADs/Cipher.swift" + "AEADs/Nonces.swift" + "ASN1/ASN1.swift" + "ASN1/Basic ASN1 Types/ASN1BitString.swift" + "ASN1/Basic ASN1 Types/ASN1Identifier.swift" + "ASN1/Basic ASN1 Types/ASN1Integer.swift" + "ASN1/Basic ASN1 Types/ASN1OctetString.swift" + "ASN1/Basic ASN1 Types/ArraySliceBigint.swift" + "ASN1/Basic ASN1 Types/ObjectIdentifier.swift" + "ASN1/ECDSASignature.swift" + "ASN1/PEMDocument.swift" + "ASN1/PKCS8PrivateKey.swift" + "ASN1/SEC1PrivateKey.swift" + "ASN1/SubjectPublicKeyInfo.swift" + "CryptoKitErrors.swift" + "Digests/BoringSSL/Digest_boring.swift" + "Digests/Digest.swift" + "Digests/Digests.swift" + "Digests/HashFunctions.swift" + "Digests/HashFunctions_SHA2.swift" + "Insecure/Insecure.swift" + "Insecure/Insecure_HashFunctions.swift" + "Key Agreement/BoringSSL/ECDH_boring.swift" + "Key Agreement/DH.swift" + "Key Agreement/ECDH.swift" + "Key Derivation/HKDF.swift" + "Keys/EC/BoringSSL/Ed25519_boring.swift" + "Keys/EC/BoringSSL/EllipticCurvePoint_boring.swift" + "Keys/EC/BoringSSL/EllipticCurve_boring.swift" + "Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift" + "Keys/EC/BoringSSL/X25519Keys_boring.swift" + "Keys/EC/Curve25519.swift" + "Keys/EC/Ed25519.swift" + "Keys/EC/NISTCurvesKeys.swift" + "Keys/EC/X25519Keys.swift" + "Keys/Symmetric/SymmetricKeys.swift" + "Message Authentication Codes/HMAC/HMAC.swift" + "Message Authentication Codes/MACFunctions.swift" + "Message Authentication Codes/MessageAuthenticationCode.swift" + "PRF/AES.swift" + "Signatures/BoringSSL/ECDSASignature_boring.swift" + "Signatures/BoringSSL/ECDSA_boring.swift" + "Signatures/BoringSSL/EdDSA_boring.swift" + "Signatures/ECDSA.swift" + "Signatures/EdDSA.swift" + "Signatures/Signature.swift" + "Util/BoringSSL/ArbitraryPrecisionInteger_boring.swift" + "Util/BoringSSL/CryptoKitErrors_boring.swift" + "Util/BoringSSL/FiniteFieldArithmeticContext_boring.swift" + "Util/BoringSSL/RNG_boring.swift" + "Util/BoringSSL/SafeCompare_boring.swift" + "Util/BoringSSL/Zeroization_boring.swift" + "Util/PrettyBytes.swift" + "Util/SafeCompare.swift" + "Util/SecureBytes.swift" + "Util/Zeroization.swift") + +target_compile_definitions(Crypto PRIVATE + CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) +target_include_directories(Crypto PRIVATE + $ + $) +target_link_libraries(Crypto PUBLIC + CCryptoBoringSSL + CCryptoBoringSSLShims) +set_target_properties(Crypto PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) + +_install_target(Crypto) +set_property(GLOBAL APPEND PROPERTY SWIFT_CRYPTO_EXPORTS Crypto) diff --git a/cmake/modules/CMakeLists.txt b/cmake/modules/CMakeLists.txt new file mode 100644 index 000000000..3a91f84f7 --- /dev/null +++ b/cmake/modules/CMakeLists.txt @@ -0,0 +1,20 @@ +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftCrypto open source project +## +## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.md for the list of SwiftCrypto project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +set(CRYPTO_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/CryptoExports.cmake) +configure_file(CryptoConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/CryptoConfig.cmake) + +get_property(CRYPTO_EXPORTS GLOBAL PROPERTY CRYPTO_EXPORTS) +export(TARGETS ${CRYPTO_EXPORTS} FILE ${CRYPTO_EXPORTS_FILE}) diff --git a/cmake/modules/CryptoConfig.cmake.in b/cmake/modules/CryptoConfig.cmake.in new file mode 100644 index 000000000..d4d43ddf2 --- /dev/null +++ b/cmake/modules/CryptoConfig.cmake.in @@ -0,0 +1,13 @@ +# This source file is part of the SwiftCrypto open source project +# +# Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors +# Licensed under Apache License v2.0 +# +# See LICENSE.txt for license information +# See CONTRIBUTORS.md for the list of SwiftCrypto project authors +# +# SPDX-License-Identifier: Apache-2.0 + +if(NOT TARGET Crypto) + include(@CRYPTO_EXPORTS_FILE@) +endif() diff --git a/cmake/modules/SwiftSupport.cmake b/cmake/modules/SwiftSupport.cmake new file mode 100644 index 000000000..889edcd8b --- /dev/null +++ b/cmake/modules/SwiftSupport.cmake @@ -0,0 +1,108 @@ +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftCrypto open source project +## +## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.md for the list of SwiftCrypto project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +# Returns the architecture name in a variable +# +# Usage: +# get_swift_host_arch(result_var_name) +# +# Sets ${result_var_name} with the converted architecture name derived from +# CMAKE_SYSTEM_PROCESSOR. +function(get_swift_host_arch result_var_name) + if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64") + set("${result_var_name}" "x86_64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64") + set("${result_var_name}" "aarch64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64") + set("${result_var_name}" "aarch64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64") + set("${result_var_name}" "powerpc64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le") + set("${result_var_name}" "powerpc64le" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x") + set("${result_var_name}" "s390x" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l") + set("${result_var_name}" "armv6" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l") + set("${result_var_name}" "armv7" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a") + set("${result_var_name}" "armv7" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64") + set("${result_var_name}" "x86_64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64") + set("${result_var_name}" "itanium" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86") + set("${result_var_name}" "i686" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686") + set("${result_var_name}" "i686" PARENT_SCOPE) + else() + message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}") + endif() +endfunction() + +# Returns the os name in a variable +# +# Usage: +# get_swift_host_os(result_var_name) +# +# +# Sets ${result_var_name} with the converted OS name derived from +# CMAKE_SYSTEM_NAME. +function(get_swift_host_os result_var_name) + if(CMAKE_SYSTEM_NAME STREQUAL Darwin) + set(${result_var_name} macosx PARENT_SCOPE) + else() + string(TOLOWER ${CMAKE_SYSTEM_NAME} cmake_system_name_lc) + set(${result_var_name} ${cmake_system_name_lc} PARENT_SCOPE) + endif() +endfunction() + +function(_install_target module) + get_swift_host_os(swift_os) + get_target_property(type ${module} TYPE) + + if(type STREQUAL STATIC_LIBRARY) + set(swift swift_static) + else() + set(swift swift) + endif() + + install(TARGETS ${module} + ARCHIVE DESTINATION lib/${swift}/${swift_os} + LIBRARY DESTINATION lib/${swift}/${swift_os} + RUNTIME DESTINATION bin) + if(type STREQUAL EXECUTABLE) + return() + endif() + + get_swift_host_arch(swift_arch) + get_target_property(module_name ${module} Swift_MODULE_NAME) + if(NOT module_name) + set(module_name ${module}) + endif() + + if(CMAKE_SYSTEM_NAME STREQUAL Darwin) + install(FILES $/${module_name}.swiftdoc + DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule + RENAME ${swift_arch}.swiftdoc) + install(FILES $/${module_name}.swiftmodule + DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule + RENAME ${swift_arch}.swiftmodule) + else() + install(FILES + $/${module_name}.swiftdoc + $/${module_name}.swiftmodule + DESTINATION lib/${swift}/${swift_os}/${swift_arch}) + endif() +endfunction() diff --git a/scripts/check_no_api_breakages.sh b/scripts/check_no_api_breakages.sh index c6bff9eea..293bd26f6 100755 --- a/scripts/check_no_api_breakages.sh +++ b/scripts/check_no_api_breakages.sh @@ -36,6 +36,8 @@ function build_and_do() { ( cd "$repodir" git checkout -q "$tag" + # Clean up output from previous build to prevent potential interference + rm -rf .build swift build while read -r module; do swift api-digester -sdk "$sdk" -dump-sdk -module "$module" \ diff --git a/scripts/soundness.sh b/scripts/soundness.sh index 52601ca41..cf3237be5 100755 --- a/scripts/soundness.sh +++ b/scripts/soundness.sh @@ -3,7 +3,7 @@ ## ## This source file is part of the SwiftCrypto open source project ## -## Copyright (c) 2019 Apple Inc. and the SwiftCrypto project authors +## Copyright (c) 2019-2021 Apple Inc. and the SwiftCrypto project authors ## Licensed under Apache License v2.0 ## ## See LICENSE.txt for license information @@ -17,7 +17,7 @@ set -eu here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" function replace_acceptable_years() { # this needs to replace all acceptable forms with 'YEARS' - sed -e 's/20[12][890]-20[12][90]/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/' + sed -e 's/20[12][890]-20[12][190]/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/' -e 's/2021/YEARS/' } printf "=> Checking for unacceptable language... " @@ -48,14 +48,24 @@ if [ "$out" == *"error"* ]; then exit 1 fi if [[ "$FIRST_OUT" != "$SECOND_OUT" ]]; then - printf "\033[0;31mRunning gyb results in changes! Have you manually editted the generated swift files? Or did you forget to run gyb and commit changes?\033[0m\n" + printf "\033[0;31mRunning gyb results in changes! Have you manually edited the generated Swift files? Or did you forget to run gyb and commit changes?\033[0m\n" + exit 1 +fi +printf "\033[0;32mokay.\033[0m\n" + +printf "=> Detecting changes in source files for CMake build\n" +FIRST_OUT="$(git status --porcelain)" +out=$($here/update_cmakelists.sh 2>&1) +SECOND_OUT="$(git status --porcelain)" +if [[ "$FIRST_OUT" != "$SECOND_OUT" ]]; then + printf "\033[0;31mThere are source file changes! Have you added or renamed source files? Or did you forget to run 'update_cmakelists.sh' and commit changes?\033[0m\n" exit 1 fi printf "\033[0;32mokay.\033[0m\n" printf "=> Checking format\n" FIRST_OUT="$(git status --porcelain)" -# only checking direcotry named BoringSSL, rest is shared code and we need to preserve original format +# only checking directory named BoringSSL, rest is shared code and we need to preserve original format shopt -u dotglob find Sources/* Tests/* -name BoringSSL -type d | while IFS= read -r d; do printf " * checking $d... " @@ -85,7 +95,7 @@ fi printf "=> Checking license headers\n" tmp=$(mktemp /tmp/.swift-crypto-soundness_XXXXXX) -for language in swift-or-c bash dtrace; do +for language in swift-or-c bash dtrace cmake; do printf " * $language... " declare -a matching_files declare -a exceptions @@ -147,6 +157,24 @@ EOF * SPDX-License-Identifier: Apache-2.0 * *===----------------------------------------------------------------------===*/ +EOF + ;; + cmake) + matching_files=( -name 'SwiftSupport.cmake' -o -name 'CMakeLists.txt' ) + cat > "$tmp" <<"EOF" +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftCrypto open source project +## +## Copyright (c) YEARS Apple Inc. and the SwiftCrypto project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.md for the list of SwiftCrypto project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## EOF ;; *) diff --git a/scripts/update_cmakelists.sh b/scripts/update_cmakelists.sh new file mode 100755 index 000000000..7e45f2392 --- /dev/null +++ b/scripts/update_cmakelists.sh @@ -0,0 +1,79 @@ +#!/bin/bash +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftCrypto open source project +## +## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.md for the list of SwiftCrypto project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +set -eu + +here=$(pwd) + +case "$(uname -s)" in + Darwin) + find=gfind # brew install findutils + ;; + *) + find=find + ;; +esac + +function update_cmakelists_source() { + src_root="$here/Sources/$1" + + # Build an array with the rest of the arguments + shift + src_exts=("$@") + echo "Finding source files (${src_exts[@]}) under $src_root" + + num_exts=${#src_exts[@]} + + # Build file extensions argument for `find` + declare -a exts_arg + exts_arg+=(-name "${src_exts[0]}") + for (( i=1; i<$num_exts; i++ )); + do + exts_arg+=(-o -name "${src_exts[$i]}") + done + + # Wrap quotes around each filename since it might contain spaces + srcs=$($find "${src_root}" -type f \( "${exts_arg[@]}" \) -printf ' "%P"\n' | LC_ALL=POSIX sort) + echo "$srcs" + + # Update list of source files in CMakeLists.txt + # The first part in `BEGIN` (i.e., `undef $/;`) is for working with multi-line; + # the second is so that we can pass in a variable to replace with. + perl -pi -e 'BEGIN { undef $/; $replace = shift } s/add_library\(([^\n]+)\n([^\)]+)/add_library\($1\n$replace/' "$srcs" "$src_root/CMakeLists.txt" + echo "Updated $src_root/CMakeLists.txt" +} + +function update_cmakelists_assembly() { + src_root="$here/Sources/$1" + echo "Finding assembly files (.S) under $src_root" + + mac_asms=$($find "${src_root}" -type f -name "*.mac.x86_64.S" -printf ' %P\n' | LC_ALL=POSIX sort) + linux_asms=$($find "${src_root}" -type f -name "*.linux.x86_64.S" -printf ' %P\n' | LC_ALL=POSIX sort) + echo "$mac_asms" + echo "$linux_asms" + + # Update list of assembly files in CMakeLists.txt + # The first part in `BEGIN` (i.e., `undef $/;`) is for working with multi-line; + # the second is so that we can pass in a variable to replace with. + perl -pi -e 'BEGIN { undef $/; $replace = shift } s/Darwin\)\n(.+)target_sources\(([^\n]+)\n([^\)]+)/Darwin\)\n$1target_sources\($2\n$replace/' "$mac_asms" "$src_root/CMakeLists.txt" + perl -pi -e 'BEGIN { undef $/; $replace = shift } s/Linux\)\n(.+)target_sources\(([^\n]+)\n([^\)]+)/Linux\)\n$1target_sources\($2\n$replace/' "$linux_asms" "$src_root/CMakeLists.txt" + echo "Updated $src_root/CMakeLists.txt" +} + +update_cmakelists_source "CCryptoBoringSSL" "*.c" +update_cmakelists_source "CCryptoBoringSSLShims" "*.c" +update_cmakelists_source "Crypto" "*.swift" + +update_cmakelists_assembly "CCryptoBoringSSL"