-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jwe): add chacha20-poly1305 support encryption for ecdhes and ec…
…dh1pu (#10)
- Loading branch information
Showing
10 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
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
60 changes: 60 additions & 0 deletions
60
Sources/JSONWebAlgorithms/ContentEncryption/AES/XC20P+ContentEncryption.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,60 @@ | ||
import CryptoKit | ||
import Foundation | ||
|
||
struct C20PKW: ContentEncryptor, ContentDecryptor { | ||
|
||
let contentEncryptionAlgorithm: String = ContentEncryptionAlgorithm.c20PKW.rawValue | ||
let initializationVectorSizeInBits: Int = ContentEncryptionAlgorithm.c20PKW.initializationVectorSizeInBits | ||
let cekKeySize: Int = ContentEncryptionAlgorithm.c20PKW.keySizeInBits | ||
|
||
func generateInitializationVector() throws -> Data { | ||
try SecureRandom.secureRandomData(count: initializationVectorSizeInBits / 8) | ||
} | ||
|
||
func generateCEK() throws -> Data { | ||
try SecureRandom.secureRandomData(count: cekKeySize / 8) | ||
} | ||
|
||
func encrypt(payload: Data, using key: Data, arguments: [ContentEncryptionArguments]) throws -> ContentEncryptionResult { | ||
guard let iv = arguments.initializationVector else { | ||
throw CryptoError.missingInitializationVector | ||
} | ||
|
||
guard iv.count * 8 == initializationVectorSizeInBits else { | ||
throw CryptoError.initializationVectorWrongSize(sizeInBits: initializationVectorSizeInBits) | ||
} | ||
|
||
guard let aad = arguments.additionalAuthenticationData else { | ||
throw CryptoError.missingAdditionalAuthenticatingData | ||
} | ||
|
||
let aead = try ChaChaPoly.seal( | ||
payload, | ||
using: .init(data: key), | ||
nonce: .init(data: iv), | ||
authenticating: aad | ||
) | ||
|
||
return .init(cipher: aead.ciphertext, authenticationData: aead.tag) | ||
} | ||
|
||
func decrypt(cipher: Data, using key: Data, arguments: [ContentEncryptionArguments]) throws -> Data { | ||
guard let iv = arguments.initializationVector else { | ||
throw CryptoError.missingInitializationVector | ||
} | ||
|
||
guard let tag = arguments.authenticationTag else { | ||
throw CryptoError.missingAuthenticationTag | ||
} | ||
|
||
guard let aad = arguments.additionalAuthenticationData else { | ||
throw CryptoError.missingAdditionalAuthenticatingData | ||
} | ||
|
||
return try ChaChaPoly.open(.init( | ||
nonce: .init(data: iv), | ||
ciphertext: cipher, | ||
tag: tag | ||
), using: .init(data: key), authenticating: aad) | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2024 Gonçalo Frade | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
@testable import JSONWebAlgorithms | ||
import JSONWebKey | ||
import XCTest | ||
|
||
final class C20PTests: XCTestCase { | ||
|
||
func testC20PCycle() throws { | ||
let payload = "Test".data(using: .utf8)! | ||
let encryptor = ContentEncryptionAlgorithm.c20PKW.encryptor | ||
let decryptor = ContentEncryptionAlgorithm.c20PKW.decryptor | ||
let key = try encryptor.generateCEK() | ||
let iv = try encryptor.generateInitializationVector() | ||
let aad = Data() | ||
|
||
let encryption = try encryptor.encrypt(payload: payload, using: key, arguments: [ | ||
.initializationVector(iv), | ||
.additionalAuthenticationData(aad) | ||
]) | ||
|
||
let decryption = try decryptor.decrypt(cipher: encryption.cipher, using: key, arguments: [ | ||
.initializationVector(iv), | ||
.authenticationTag(encryption.authenticationData), | ||
.additionalAuthenticationData(aad) | ||
]) | ||
|
||
XCTAssertEqual(try! payload.tryToString(), try! decryption.tryToString()) | ||
} | ||
} |
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