Skip to content

Commit

Permalink
Merge pull request #148 from aidantwoods/enhancement/remove-stream-po…
Browse files Browse the repository at this point in the history
…inters

Remove stream pointers
  • Loading branch information
jedisct1 committed Jun 8, 2018
2 parents c617c99 + 3f6486e commit 8107ccb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 85 deletions.
26 changes: 7 additions & 19 deletions Sodium/GenericHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,20 @@ public struct GenericHash {

extension GenericHash {
public class Stream {
private var state: UnsafeMutablePointer<State>
private var state: State
public var outputLength: Int = 0

init?(key: Bytes?, outputLength: Int) {
state = Stream.generate()
state = crypto_generichash_state()

guard .SUCCESS == crypto_generichash_init(
state,
&state,
key, key?.count ?? 0,
outputLength
).exitCode else {
free()
return nil
}
).exitCode else { return nil }

self.outputLength = outputLength
}

deinit {
free()
}
}
}

Expand Down Expand Up @@ -128,9 +121,10 @@ extension GenericHash.Stream {
- Returns: `true` if the data was consumed successfully.
*/
@discardableResult
public func update(input: Bytes) -> Bool {
return .SUCCESS == crypto_generichash_update(
state,
&state,
input, UInt64(input.count)
).exitCode
}
Expand All @@ -144,20 +138,14 @@ extension GenericHash.Stream {
let outputLen = outputLength
var output = Array<UInt8>(count: outputLen)
guard .SUCCESS == crypto_generichash_final(
state,
&state,
&output, outputLen
).exitCode else { return nil }

return output
}
}

extension GenericHash.Stream {
private func free() {
GenericHash.Stream.free(state)
}
}

extension GenericHash.Stream: StateStream {
typealias State = crypto_generichash_state
static let capacity = crypto_generichash_statebytes()
Expand Down
55 changes: 15 additions & 40 deletions Sodium/SecretStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,51 +26,40 @@ extension SecretStream.XChaCha20Poly1305 {

extension SecretStream.XChaCha20Poly1305 {
public class PushStream {
private var state: UnsafeMutablePointer<State>
private var state: State
private var _header: Header

init?(secretKey: Key) {
guard secretKey.count == KeyBytes else { return nil }

state = PushStream.generate()
state = crypto_secretstream_xchacha20poly1305_state()

_header = Bytes(count: HeaderBytes)
guard .SUCCESS == crypto_secretstream_xchacha20poly1305_init_push(
state,
&state,
&_header,
secretKey
).exitCode else {
free()
return nil
}
}

deinit {
free()
).exitCode else { return nil }
}
}
}

extension SecretStream.XChaCha20Poly1305 {
public class PullStream {
private var state: UnsafeMutablePointer<State>
private var state: State

init?(secretKey: Key, header: Header) {
guard header.count == HeaderBytes, secretKey.count == KeyBytes else {
return nil
}
state = PushStream.generate()

state = crypto_secretstream_xchacha20poly1305_state()

guard .SUCCESS == crypto_secretstream_xchacha20poly1305_init_pull(
state,
&state,
header,
secretKey
).exitCode else {
free()
return nil
}
}

deinit {
free()
).exitCode else { return nil }
}
}
}
Expand Down Expand Up @@ -135,7 +124,7 @@ extension SecretStream.XChaCha20Poly1305.PushStream {
let _ad = ad ?? Bytes(count: 0)
var cipherText = Bytes(count: message.count + XChaCha20Poly1305.ABytes)
guard .SUCCESS == crypto_secretstream_xchacha20poly1305_push(
state,
&state,
&cipherText,
nil,
message, UInt64(message.count),
Expand All @@ -150,7 +139,7 @@ extension SecretStream.XChaCha20Poly1305.PushStream {
Performs an explicit key rotation.
*/
public func rekey() {
crypto_secretstream_xchacha20poly1305_rekey(state)
crypto_secretstream_xchacha20poly1305_rekey(&state)
}
}

Expand All @@ -172,7 +161,7 @@ extension SecretStream.XChaCha20Poly1305.PullStream {
let _ad = ad ?? Bytes(count: 0)
var _tag: UInt8 = 0
let result = crypto_secretstream_xchacha20poly1305_pull(
state,
&state,
&message,
nil,
&_tag,
Expand All @@ -190,24 +179,10 @@ extension SecretStream.XChaCha20Poly1305.PullStream {
Performs an explicit key rotation.
*/
public func rekey() {
crypto_secretstream_xchacha20poly1305_rekey(state)
crypto_secretstream_xchacha20poly1305_rekey(&state)
}
}


extension SecretStream.XChaCha20Poly1305.PushStream {
private func free() {
XChaCha20Poly1305.PushStream.free(state)
}
}

extension SecretStream.XChaCha20Poly1305.PullStream {
private func free() {
XChaCha20Poly1305.PullStream.free(state)
}
}


extension SecretStream.XChaCha20Poly1305: SecretKeyGenerator {
var KeyBytes: Int { return SecretStream.XChaCha20Poly1305.KeyBytes }
public typealias Key = Bytes
Expand Down
24 changes: 0 additions & 24 deletions Sodium/StateStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,3 @@ protocol StateStream {

static var capacity: Int { get }
}

extension StateStream {
static func free(_ state: UnsafeMutablePointer<State>) {
let rawState = UnsafeMutableRawPointer(state).bindMemory(
to: UInt8.self,
capacity: capacity
)

#if swift(>=4.1)
rawState.deallocate()
#else
rawState.deallocate(capacity: 1)
#endif
}

static func generate() -> UnsafeMutablePointer<State> {
let rawState = UnsafeMutablePointer<UInt8>.allocate(capacity: capacity)

return UnsafeMutableRawPointer(rawState).bindMemory(
to: State.self,
capacity: 1
)
}
}
4 changes: 2 additions & 2 deletions Tests/SodiumTests/ReadmeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class ReadmeTests : XCTestCase {
let message2 = "Message".bytes
let key = "Secret key".bytes
let stream = sodium.genericHash.initStream(key: key)!
let _ = stream.update(input: message1)
let _ = stream.update(input: message2)
stream.update(input: message1)
stream.update(input: message2)
let h = stream.final()

XCTAssertNotNil(h)
Expand Down

0 comments on commit 8107ccb

Please sign in to comment.