Added secretbox functions and constants
This commit is contained in:
1 parent
555b69f15c
commit
99b9ee5e9d
14 files changed
+464
-32
No files matched your search
+82
@@ -0,0 +1,82 @@
|
||||
package com.ionspin.kotlin.crypto.secretbox
|
||||
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium
|
||||
|
||||
actual object SecretBox {
|
||||
actual fun easy(message: UByteArray, nonce: UByteArray, key: UByteArray): UByteArray {
|
||||
val ciphertext = UByteArray(message.size + crypto_secretbox_MACBYTES)
|
||||
sodium.crypto_secretbox_easy(
|
||||
ciphertext.asByteArray(),
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray()
|
||||
)
|
||||
return ciphertext
|
||||
}
|
||||
|
||||
actual fun openEasy(
|
||||
ciphertext: UByteArray,
|
||||
nonce: UByteArray,
|
||||
key: UByteArray
|
||||
): UByteArray {
|
||||
val decrypted = UByteArray(ciphertext.size - crypto_secretbox_MACBYTES)
|
||||
val validationResult = sodium.crypto_secretbox_open_easy(
|
||||
decrypted.asByteArray(),
|
||||
ciphertext.asByteArray(),
|
||||
ciphertext.size.toLong(),
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray()
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
||||
}
|
||||
return decrypted
|
||||
}
|
||||
|
||||
actual fun detached(
|
||||
message: UByteArray,
|
||||
nonce: UByteArray,
|
||||
key: UByteArray
|
||||
): SecretBoxEncryptedDataAndTag {
|
||||
val ciphertext = UByteArray(message.size)
|
||||
val authenticationTag = UByteArray(crypto_secretbox_MACBYTES)
|
||||
sodium.crypto_secretbox_detached(
|
||||
ciphertext.asByteArray(),
|
||||
authenticationTag.asByteArray(),
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray()
|
||||
)
|
||||
return SecretBoxEncryptedDataAndTag(ciphertext, authenticationTag)
|
||||
}
|
||||
|
||||
actual fun openDetached(
|
||||
ciphertext: UByteArray,
|
||||
tag: UByteArray,
|
||||
nonce: UByteArray,
|
||||
key: UByteArray
|
||||
): UByteArray {
|
||||
val message = UByteArray(ciphertext.size)
|
||||
val validationResult = sodium.crypto_secretbox_open_detached(
|
||||
message.asByteArray(),
|
||||
ciphertext.asByteArray(),
|
||||
tag.asByteArray(),
|
||||
ciphertext.size.toLong(),
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray()
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
actual fun keygen() : UByteArray {
|
||||
val generatedKey = UByteArray(crypto_secretbox_KEYBYTES)
|
||||
sodium.crypto_secretbox_keygen(generatedKey.asByteArray())
|
||||
return generatedKey
|
||||
}
|
||||
|
||||
}
|
||||
+5
-2
@@ -49,7 +49,7 @@ actual object SecretStream {
|
||||
): DecryptedDataAndTag {
|
||||
val result = UByteArray(ciphertext.size - crypto_secretstream_xchacha20poly1305_ABYTES)
|
||||
val tagArray = UByteArray(1) { 0U }
|
||||
sodium.crypto_secretstream_xchacha20poly1305_pull(
|
||||
val validationResult = sodium.crypto_secretstream_xchacha20poly1305_pull(
|
||||
state,
|
||||
result.asByteArray(),
|
||||
null,
|
||||
@@ -59,11 +59,14 @@ actual object SecretStream {
|
||||
additionalData.asByteArray(),
|
||||
additionalData.size.toLong()
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
throw SecretStreamCorrupedOrTamperedDataException()
|
||||
}
|
||||
return DecryptedDataAndTag(result, tagArray[0])
|
||||
}
|
||||
|
||||
actual fun xChaCha20Poly1305Keygen(): UByteArray {
|
||||
val generatedKey = UByteArray(crypto_aead_xchacha20poly1305_ietf_KEYBYTES)
|
||||
val generatedKey = UByteArray(crypto_secretstream_xchacha20poly1305_KEYBYTES)
|
||||
sodium.crypto_secretstream_xchacha20poly1305_keygen(generatedKey.asByteArray())
|
||||
return generatedKey
|
||||
}
|
||||
Reference in new issue
Block a user