Skip to content

prepare project for public CI #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
### Expected behavior
_[what you expected to happen]_

### Actual behavior
_[what actually happened]_

### Steps to reproduce

1. ...
2. ...

### If possible, minimal yet complete reproducer code (or URL to code)

_[anything to help us reproducing the issue]_

### SwiftLog version/commit hash

_[the SwiftLog tag/commit hash]_

### Swift & OS version (output of `swift --version && uname -a`)
13 changes: 13 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
_[One line description of your change]_

### Motivation:

_[Explain here the context, and why you're making that change. What is the problem you're trying to solve.]_

### Modifications:

_[Describe the modifications you've done.]_

### Result:

_[After your change, what will change.]_
13 changes: 13 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# file options

--exclude .build

# format options

--self insert
--patternlet inline
--stripunusedargs unnamed-only
--comments ignore
--ifdef no-indent

# rules
9 changes: 9 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ This product contains test vectors from Google's wycheproof project.
* https://github.com/google/wycheproof/blob/master/LICENSE
* HOMEPAGE:
* https://github.com/google/wycheproof

---

This product contains a derivation of various scripts from SwiftNIO.

* LICENSE (Apache License 2.0):
* https://www.apache.org/licenses/LICENSE-2.0
* HOMEPAGE:
* https://github.com/apple/swift-nio
7 changes: 3 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import PackageDescription
let swiftSettings: [SwiftSetting] = [
.define("CRYPTO_IN_SWIFTPM"),
// To develop this on Apple platforms, uncomment this define.
//.define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"),
// .define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"),
]

let package = Package(
Expand All @@ -41,8 +41,8 @@ let package = Package(
products: [
.library(name: "Crypto", targets: ["Crypto"]),
/* This target is used only for symbol mangling. It's added and removed automatically because it emits build warnings. MANGLE_START
.library(name: "CCryptoBoringSSL", type: .static, targets: ["CCryptoBoringSSL"]),
MANGLE_END */
.library(name: "CCryptoBoringSSL", type: .static, targets: ["CCryptoBoringSSL"]),
MANGLE_END */
],
dependencies: [],
targets: [
Expand All @@ -54,4 +54,3 @@ let package = Package(
],
cxxLanguageStandard: .cxx11
)

6 changes: 3 additions & 3 deletions Sources/Crypto/AEADs/AES/GCM/BoringSSL/AES-GCM_boring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
import Foundation
@_implementationOnly import CCryptoBoringSSL
import Foundation

enum OpenSSLAESGCMImpl {
@inlinable
static func seal<Plaintext: DataProtocol, AuthenticatedData: DataProtocol>
(key: SymmetricKey, message: Plaintext, nonce: AES.GCM.Nonce?, authenticatedData: AuthenticatedData? = nil) throws -> AES.GCM.SealedBox {
(key: SymmetricKey, message: Plaintext, nonce: AES.GCM.Nonce?, authenticatedData: AuthenticatedData? = nil) throws -> AES.GCM.SealedBox {
let nonce = nonce ?? AES.GCM.Nonce()

let aead = try Self._backingAEAD(key: key)
Expand All @@ -38,7 +38,7 @@ enum OpenSSLAESGCMImpl {

@inlinable
static func open<AuthenticatedData: DataProtocol>
(key: SymmetricKey, sealedBox: AES.GCM.SealedBox, authenticatedData: AuthenticatedData? = nil) throws -> Data {
(key: SymmetricKey, sealedBox: AES.GCM.SealedBox, authenticatedData: AuthenticatedData? = nil) throws -> Data {
let aead = try Self._backingAEAD(key: key)

if let ad = authenticatedData {
Expand Down
27 changes: 12 additions & 15 deletions Sources/Crypto/AEADs/ChachaPoly/BoringSSL/ChaChaPoly_boring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
import Foundation
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims

import Foundation

enum OpenSSLChaChaPolyImpl {
static func encrypt<M: DataProtocol, AD: DataProtocol>(key: SymmetricKey, message: M, nonce: ChaChaPoly.Nonce?, authenticatedData: AD?) throws -> ChaChaPoly.SealedBox {
Expand Down Expand Up @@ -50,7 +49,6 @@ enum OpenSSLChaChaPolyImpl {
}
}


/// An abstraction over a BoringSSL AEAD
@usableFromInline
enum BoringSSLAEAD {
Expand All @@ -73,7 +71,6 @@ enum BoringSSLAEAD {
}
}


extension BoringSSLAEAD {
// Arguably this class is excessive, but it's probably better for this API to be as safe as possible
// rather than rely on defer statements for our cleanup.
Expand All @@ -86,7 +83,7 @@ extension BoringSSLAEAD {
let rc: CInt = key.withUnsafeBytes { keyPointer in
withUnsafeMutablePointer(to: &self.context) { contextPointer in
// Create the AEAD context with a default tag length using the given key.
return CCryptoBoringSSLShims_EVP_AEAD_CTX_init(contextPointer, cipher.boringSSLCipher, keyPointer.baseAddress, keyPointer.count, 0, nil)
CCryptoBoringSSLShims_EVP_AEAD_CTX_init(contextPointer, cipher.boringSSLCipher, keyPointer.baseAddress, keyPointer.count, 0, nil)
}
}
guard rc == 1 else {
Expand All @@ -102,7 +99,8 @@ extension BoringSSLAEAD {
}
}

// MARK:- Sealing
// MARK: - Sealing

extension BoringSSLAEAD.AEADContext {
/// The main entry point for sealing data. Covers the full gamut of types, including discontiguous data types. This must be inlinable.
@inlinable
Expand All @@ -128,7 +126,6 @@ extension BoringSSLAEAD.AEADContext {
let contiguousAD = Array(authenticatedData)
return try self._sealContiguous(message: contiguousMessage, nonce: nonce, authenticatedData: contiguousAD)
}

}

/// A fast-path for sealing contiguous data. Also inlinable to gain specialization information.
Expand Down Expand Up @@ -177,8 +174,8 @@ extension BoringSSLAEAD.AEADContext {
}
}

// MARK: - Opening

// MARK:- Opening
extension BoringSSLAEAD.AEADContext {
/// The main entry point for opening data. Covers the full gamut of types, including discontiguous data types. This must be inlinable.
@inlinable
Expand Down Expand Up @@ -220,12 +217,12 @@ extension BoringSSLAEAD.AEADContext {
let outputBuffer = UnsafeMutableRawBufferPointer(start: malloc(ciphertext.count)!, count: ciphertext.count)

let rc = withUnsafePointer(to: &self.context) { contextPointer in
return CCryptoBoringSSLShims_EVP_AEAD_CTX_open_gather(contextPointer,
outputBuffer.baseAddress,
nonceBytes.baseAddress, nonceBytes.count,
ciphertext.baseAddress, ciphertext.count,
tagBytes.baseAddress, tagBytes.count,
authenticatedData.baseAddress, authenticatedData.count)
CCryptoBoringSSLShims_EVP_AEAD_CTX_open_gather(contextPointer,
outputBuffer.baseAddress,
nonceBytes.baseAddress, nonceBytes.count,
ciphertext.baseAddress, ciphertext.count,
tagBytes.baseAddress, tagBytes.count,
authenticatedData.baseAddress, authenticatedData.count)
}

guard rc == 1 else {
Expand All @@ -239,8 +236,8 @@ extension BoringSSLAEAD.AEADContext {
}
}

// MARK: - Supported ciphers

// MARK:- Supported ciphers
extension BoringSSLAEAD {
var boringSSLCipher: OpaquePointer {
switch self {
Expand Down
14 changes: 4 additions & 10 deletions Sources/Crypto/Digests/BoringSSL/Digest_boring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
#else
@_implementationOnly import CCryptoBoringSSL


protocol HashFunctionImplementationDetails: HashFunction where Digest: DigestPrivate { }

protocol HashFunctionImplementationDetails: HashFunction where Digest: DigestPrivate {}

protocol BoringSSLBackedHashFunction: HashFunctionImplementationDetails {
static var digestType: DigestContext.DigestType { get }
}


extension Insecure.MD5: BoringSSLBackedHashFunction {
static var digestType: DigestContext.DigestType {
return .md5
Expand Down Expand Up @@ -55,7 +52,6 @@ extension SHA512: BoringSSLBackedHashFunction {
}
}


struct OpenSSLDigestImpl<H: BoringSSLBackedHashFunction> {
private var context: DigestContext

Expand All @@ -76,19 +72,18 @@ struct OpenSSLDigestImpl<H: BoringSSLBackedHashFunction> {
let digestBytes = copyContext.finalize()
return digestBytes.withUnsafeBytes {
// We force unwrap here because if the digest size is wrong it's an internal error.
return H.Digest(bufferPointer: $0)!
H.Digest(bufferPointer: $0)!
}
}
}


class DigestContext {
private var contextPointer: UnsafeMutablePointer<EVP_MD_CTX>

init(digest: DigestType) {
// We force unwrap because we cannot recover from allocation failure.
self.contextPointer = CCryptoBoringSSL_EVP_MD_CTX_new()!
guard 0 != CCryptoBoringSSL_EVP_DigestInit(self.contextPointer, digest.dispatchTable) else {
guard CCryptoBoringSSL_EVP_DigestInit(self.contextPointer, digest.dispatchTable) != 0 else {
// We can't do much but crash here.
fatalError("Unable to initialize digest state: \(CCryptoBoringSSL_ERR_get_error())")
}
Expand All @@ -97,7 +92,7 @@ class DigestContext {
init(copying original: DigestContext) {
// We force unwrap because we cannot recover from allocation failure.
self.contextPointer = CCryptoBoringSSL_EVP_MD_CTX_new()!
guard 0 != CCryptoBoringSSL_EVP_MD_CTX_copy(self.contextPointer, original.contextPointer) else {
guard CCryptoBoringSSL_EVP_MD_CTX_copy(self.contextPointer, original.contextPointer) != 0 else {
// We can't do much but crash here.
fatalError("Unable to copy digest state: \(CCryptoBoringSSL_ERR_get_error())")
}
Expand Down Expand Up @@ -130,7 +125,6 @@ class DigestContext {
}
}


extension DigestContext {
struct DigestType {
var dispatchTable: OpaquePointer
Expand Down
2 changes: 1 addition & 1 deletion Sources/Crypto/Key Agreement/BoringSSL/ECDH_boring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ extension P384.KeyAgreement.PrivateKey {
return SharedSecret(ss: key)
}
}

extension P521.KeyAgreement.PrivateKey {
internal func openSSLSharedSecretFromKeyAgreement(with publicKeyShare: P521.KeyAgreement.PublicKey) throws -> SharedSecret {
let key = try self.impl.key.keyExchange(publicKey: publicKeyShare.impl.key)
return SharedSecret(ss: key)
}
}
#endif // (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

6 changes: 3 additions & 3 deletions Sources/Crypto/Keys/EC/BoringSSL/Ed25519_boring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
import Foundation
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import Foundation

// For signing and verifying, we use BoringSSL's Ed25519, not the X25519 stuff.
extension Curve25519.Signing {
Expand All @@ -30,7 +30,7 @@ extension Curve25519.Signing {
// BoringSSL's Ed25519 implementation stores the private key concatenated with the public key, so we do
// as well. We also store the public key because it makes our lives easier.
var publicKey = Array(repeating: UInt8(0), count: 32)
let privateKey = SecureBytes(unsafeUninitializedCapacity: 64) { (privateKeyPtr, privateKeyBytes) in
let privateKey = SecureBytes(unsafeUninitializedCapacity: 64) { privateKeyPtr, privateKeyBytes in
privateKeyBytes = 64
publicKey.withUnsafeMutableBytes { publicKeyPtr in
CCryptoBoringSSLShims_ED25519_keypair(publicKeyPtr.baseAddress, privateKeyPtr.baseAddress)
Expand Down Expand Up @@ -60,7 +60,7 @@ extension Curve25519.Signing {
throw CryptoKitError.incorrectKeySize
}

let privateKey = SecureBytes(unsafeUninitializedCapacity: 64) { (privateKeyPtr, privateKeyBytes) in
let privateKey = SecureBytes(unsafeUninitializedCapacity: 64) { privateKeyPtr, privateKeyBytes in
privateKeyBytes = 64
publicKey.withUnsafeMutableBytes { publicKeyPtr in
CCryptoBoringSSLShims_ED25519_keypair_from_seed(publicKeyPtr.baseAddress, privateKeyPtr.baseAddress, seedPtr.baseAddress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//
@_implementationOnly import CCryptoBoringSSL


/// A wrapper around BoringSSL's EC_POINT with some lifetime management.
@usableFromInline
class EllipticCurvePoint {
Expand All @@ -30,7 +29,7 @@ class EllipticCurvePoint {

try group.withUnsafeGroupPointer { groupPtr in
try scalar.withUnsafeBignumPointer { bigNumPtr in
guard 0 != CCryptoBoringSSL_EC_POINT_mul(groupPtr, self._basePoint, bigNumPtr, nil, nil, nil) else {
guard CCryptoBoringSSL_EC_POINT_mul(groupPtr, self._basePoint, bigNumPtr, nil, nil, nil) != 0 else {
throw CryptoKitError.internalBoringSSLError()
}
}
Expand All @@ -51,23 +50,23 @@ class EllipticCurvePoint {
}
}

// MARK: - Helpers

// MARK:- Helpers
extension EllipticCurvePoint {
@inlinable
func withPointPointer<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
return try body(self._basePoint)
}

@usableFromInline
func affineCoordinates(group: BoringSSLEllipticCurveGroup) throws -> (x: ArbitraryPrecisionInteger, y: ArbitraryPrecisionInteger) {
func affineCoordinates(group: BoringSSLEllipticCurveGroup) throws -> (x: ArbitraryPrecisionInteger, y: ArbitraryPrecisionInteger) {
var x = ArbitraryPrecisionInteger()
var y = ArbitraryPrecisionInteger()

try x.withUnsafeMutableBignumPointer { xPtr in
try y.withUnsafeMutableBignumPointer { yPtr in
try group.withUnsafeGroupPointer { groupPtr in
guard 0 != CCryptoBoringSSL_EC_POINT_get_affine_coordinates_GFp(groupPtr, self._basePoint, xPtr, yPtr, nil) else {
guard CCryptoBoringSSL_EC_POINT_get_affine_coordinates_GFp(groupPtr, self._basePoint, xPtr, yPtr, nil) != 0 else {
throw CryptoKitError.internalBoringSSLError()
}
}
Expand Down
Loading