Skip to content

Commit

Permalink
Use low level AEAD api
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Tacke committed Apr 3, 2020
1 parent 76464ad commit 323cae3
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ class DoubleRatchet {
associatedData?.let { headerData += it }

val nonce = sodium.nonce(AEAD.XCHACHA20POLY1305_IETF_NPUBBYTES)
val cipherString = sodium.encrypt(sodium.str(plaintext), sodium.str(headerData), nonce, messageKey, AEAD.Method.XCHACHA20_POLY1305_IETF)
val nonceAndCipher = nonce + sodium.sodiumHex2Bin(cipherString)
val cipher = ByteArray(plaintext.size + AEAD.XCHACHA20POLY1305_IETF_ABYTES)
sodium.cryptoAeadXChaCha20Poly1305IetfEncrypt(cipher, null, plaintext, plaintext.size.toLong(), headerData, headerData.size.toLong(), null, nonce, messageKey.asBytes)

val nonceAndCipher = nonce + cipher
return Message(header, nonceAndCipher)
}

Expand Down Expand Up @@ -121,8 +122,10 @@ class DoubleRatchet {
val nonce = message.cipher.sliceArray(0 until AEAD.XCHACHA20POLY1305_IETF_NPUBBYTES)
val cipher = message.cipher.sliceArray(AEAD.XCHACHA20POLY1305_IETF_NPUBBYTES until message.cipher.size)

val plaintext = sodium.decrypt(sodium.sodiumBin2Hex(cipher), sodium.str(headerData), nonce, key, AEAD.Method.XCHACHA20_POLY1305_IETF)
return sodium.bytes(plaintext)
val plaintextLength = cipher.size - AEAD.XCHACHA20POLY1305_IETF_ABYTES
val plaintext = ByteArray(plaintextLength)
sodium.cryptoAeadXChaCha20Poly1305IetfDecrypt(plaintext, null, null, cipher, cipher.size.toLong(), headerData, headerData.size.toLong(), nonce, key.asBytes)
return plaintext
}

@ExperimentalStdlibApi
Expand Down

0 comments on commit 323cae3

Please sign in to comment.