Skip to content

Commit

Permalink
Refactor BinaryParser and tests, update key representation
Browse files Browse the repository at this point in the history
This commit includes modifications to handling of key access in BinaryParser and changes the way the ephemeral public key data is represented in NanoTDF. Removed redundant code in BinaryParser by eliminating unnecessary if-else checks and updated how bindingSize is calculated. Some previously commented code in the test suite has been uncommented and refined, ensuring a more accurate validation of our tests. Now, the test suite also checks the existence of the ephemeral public key.
  • Loading branch information
arkavo-com committed Jun 10, 2024
1 parent 75a7a27 commit 25df072
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
10 changes: 4 additions & 6 deletions NanoTDF/BinaryParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class BinaryParser {
print("Failed to read Embedded Policy plaintext / ciphertext")
return nil
}
let keyAccess = policyType == .embeddedEncryptedWithPolicyKeyAccess ? readPolicyKeyAccess(bindingMode: bindingMode) : nil
// let keyAccess = policyType == .embeddedEncryptedWithPolicyKeyAccess ? readPolicyKeyAccess(bindingMode: bindingMode) : nil

return EmbeddedPolicyBody(length: plaintextCiphertext.count, body: plaintextCiphertext, keyAccess: nil)
}
Expand Down Expand Up @@ -138,8 +138,6 @@ class BinaryParser {
var bindingSize: Int
print("bindingMode", bindingMode)
if bindingMode.ecdsaBinding {
bindingSize = 64
} else {
switch bindingMode.curve {
case .secp256r1, .xsecp256k1:
bindingSize = 64
Expand All @@ -148,11 +146,11 @@ class BinaryParser {
case .secp521r1:
bindingSize = 132
}
} else {
// GMAC Tag Binding
bindingSize = 16
}
print("bindingSize", bindingSize)
if bindingMode.ecdsaBinding {
bindingSize = 64
}
return read(length: bindingSize)
}

Expand Down
2 changes: 1 addition & 1 deletion NanoTDF/NanoTDF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func createNanoTDF(kas: KasMetadata, policy: inout Policy, plaintext: Data) thro
let curve: Curve = .secp256r1
var ephemeralPublicKeyData: Data = Data()
if let ephemeralPublicKey = ephemeralPublicKey as? P256.KeyAgreement.PublicKey {
ephemeralPublicKeyData = ephemeralPublicKey.x963Representation
ephemeralPublicKeyData = ephemeralPublicKey.compressedRepresentation
}
print("ephemeralPublicKeyData.count", ephemeralPublicKeyData.count)
let header = Header(magicNumber: magicNumber,
Expand Down
11 changes: 6 additions & 5 deletions Tests/NanoTDFCreationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class NanoTDFCreationTests: XCTestCase {
XCTAssertNotNil(nanoTDF, "NanoTDF should not be nil")
XCTAssertNotNil(nanoTDF.header, "Header should not be nil")
XCTAssertNotNil(nanoTDF.header.policy.remote, "Policy body should not be nil")
XCTAssertNotNil(nanoTDF.header.ephemeralPublicKey, "Ephemeral PublicKey should not be nil")
XCTAssertNotNil(nanoTDF.payload, "Payload should not be nil")
XCTAssertNotNil(nanoTDF.payload.iv, "Payload nonce should not be nil")
XCTAssertNotNil(nanoTDF.payload.ciphertext, "Payload ciphertext should not be nil")
Expand Down Expand Up @@ -56,11 +57,11 @@ class NanoTDFCreationTests: XCTestCase {
// Ephemeral Key
let ephemeralKeyHexString = header.ephemeralPublicKey.map { String(format: "%02x", $0) }.joined(separator: " ")
print("Ephemeral Key:", ephemeralKeyHexString)
// FIXME payload length is incorrect
// let payload = try parser.parsePayload(config: header.payloadSignatureConfig)
// let snanoTDF = NanoTDF(header: header, payload: payload, signature: nil)
// // Print final the signature NanoTDF
// print(snanoTDF)
let payload = try parser.parsePayload(config: header.payloadSignatureConfig)
let snanoTDF = NanoTDF(header: header, payload: payload, signature: nil)
// Print final the signature NanoTDF
print(snanoTDF)
XCTAssertEqual(payload.length, 43)
}

func testCreateNanoTDFWithInvalidKasMetadata() {
Expand Down

0 comments on commit 25df072

Please sign in to comment.