-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9da1dc
commit 2744356
Showing
69 changed files
with
2,693 additions
and
1,045 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// AEAD.swift | ||
// CryptoSwift | ||
// | ||
// Copyright (C) 2014-2017 Marcin Krzyżanowski <[email protected]> | ||
// This software is provided 'as-is', without any express or implied warranty. | ||
// | ||
// In no event will the authors be held liable for any damages arising from the use of this software. | ||
// | ||
// Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: | ||
// | ||
// - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required. | ||
// - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. | ||
// - This notice may not be removed or altered from any source or binary distribution. | ||
// | ||
// | ||
|
||
// https://www.iana.org/assignments/aead-parameters/aead-parameters.xhtml | ||
|
||
/// Authenticated Encryption with Associated Data (AEAD) | ||
public protocol AEAD { | ||
static var kLen: Int { get } // key length | ||
static var ivRange: Range<Int> { get } // nonce length | ||
} | ||
|
||
extension AEAD { | ||
static func calculateAuthenticationTag(authenticator: Authenticator, cipherText: Array<UInt8>, authenticationHeader: Array<UInt8>) throws -> Array<UInt8> { | ||
let headerPadding = ((16 - (authenticationHeader.count & 0xf)) & 0xf) | ||
let cipherPadding = ((16 - (cipherText.count & 0xf)) & 0xf) | ||
|
||
var mac = authenticationHeader | ||
mac += Array<UInt8>(repeating: 0, count: headerPadding) | ||
mac += cipherText | ||
mac += Array<UInt8>(repeating: 0, count: cipherPadding) | ||
mac += UInt64(bigEndian: UInt64(authenticationHeader.count)).bytes() | ||
mac += UInt64(bigEndian: UInt64(cipherText.count)).bytes() | ||
|
||
return try authenticator.authenticate(mac) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
stellarsdk/stellarsdk/libs/CryptoSwift/AEAD/AEADChaCha20Poly1305.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// ChaCha20Poly1305.swift | ||
// CryptoSwift | ||
// | ||
// Copyright (C) 2014-2017 Marcin Krzyżanowski <[email protected]> | ||
// This software is provided 'as-is', without any express or implied warranty. | ||
// | ||
// In no event will the authors be held liable for any damages arising from the use of this software. | ||
// | ||
// Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: | ||
// | ||
// - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required. | ||
// - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. | ||
// - This notice may not be removed or altered from any source or binary distribution. | ||
// | ||
// | ||
// https://tools.ietf.org/html/rfc7539#section-2.8.1 | ||
|
||
/// AEAD_CHACHA20_POLY1305 | ||
public final class AEADChaCha20Poly1305: AEAD { | ||
public static let kLen = 32 // key length | ||
public static var ivRange = Range<Int>(12...12) | ||
|
||
/// Authenticated encryption | ||
public static func encrypt(_ plainText: Array<UInt8>, key: Array<UInt8>, iv: Array<UInt8>, authenticationHeader: Array<UInt8>) throws -> (cipherText: Array<UInt8>, authenticationTag: Array<UInt8>) { | ||
let cipher = try ChaCha20(key: key, iv: iv) | ||
|
||
var polykey = Array<UInt8>(repeating: 0, count: kLen) | ||
var toEncrypt = polykey | ||
polykey = try cipher.encrypt(polykey) | ||
toEncrypt += polykey | ||
toEncrypt += plainText | ||
|
||
let fullCipherText = try cipher.encrypt(toEncrypt) | ||
let cipherText = Array(fullCipherText.dropFirst(64)) | ||
|
||
let tag = try calculateAuthenticationTag(authenticator: Poly1305(key: polykey), cipherText: cipherText, authenticationHeader: authenticationHeader) | ||
return (cipherText, tag) | ||
} | ||
|
||
/// Authenticated decryption | ||
public static func decrypt(_ cipherText: Array<UInt8>, key: Array<UInt8>, iv: Array<UInt8>, authenticationHeader: Array<UInt8>, authenticationTag: Array<UInt8>) throws -> (plainText: Array<UInt8>, success: Bool) { | ||
let chacha = try ChaCha20(key: key, iv: iv) | ||
|
||
let polykey = try chacha.encrypt(Array<UInt8>(repeating: 0, count: kLen)) | ||
let mac = try calculateAuthenticationTag(authenticator: Poly1305(key: polykey), cipherText: cipherText, authenticationHeader: authenticationHeader) | ||
guard mac == authenticationTag else { | ||
return (cipherText, false) | ||
} | ||
|
||
var toDecrypt = Array<UInt8>(reserveCapacity: cipherText.count + 64) | ||
toDecrypt += polykey | ||
toDecrypt += polykey | ||
toDecrypt += cipherText | ||
let fullPlainText = try chacha.decrypt(toDecrypt) | ||
let plainText = Array(fullPlainText.dropFirst(64)) | ||
return (plainText, true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.