Adding crypto_box functions

This commit is contained in:
Ugljesa Jovanovic 2020-08-31 22:56:37 +02:00 committed by Ugljesa Jovanovic
parent 1894a5d995
commit 57f9bd9d20
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
4 changed files with 114 additions and 16 deletions

View File

@ -0,0 +1,90 @@
package com.ionspin.kotlin.crypto.box
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 31-Aug-2020
*/
val crypto_box_PUBLICKEYBYTES = 32
val crypto_box_SECRETKEYBYTES = 32
val crypto_box_MACBYTES = 16
val crypto_box_SEEDBYTES = 32
val crypto_box_NONCEBYTES = 24
val crypto_box_SEALBYTES = 48
val crypto_box_BEFORENMBYTES = 32
data class BoxKeyPair(val publicKey : UByteArray, val secretKey: UByteArray)
data class BoxEncryptedDataAndTag(val ciphertext: UByteArray, val tag: UByteArray)
class BoxCorruptedOrTamperedDataException() : RuntimeException("MAC validation failed. Data is corrupted or tampered with.")
expect object Box {
/**
* The crypto_box_keypair() function randomly generates a secret key and a corresponding public key.
* The public key is put into pk (crypto_box_PUBLICKEYBYTES bytes) and the secret key into
* sk (crypto_box_SECRETKEYBYTES bytes).
*/
fun keypair() : BoxKeyPair
/**
* Using crypto_box_seed_keypair(), the key pair can also be deterministically derived from a single key seed (crypto_box_SEEDBYTES bytes).
*/
fun seedKeypair(seed: UByteArray) : BoxKeyPair
/**
* The crypto_box_easy() function encrypts a message m whose length is mlen bytes, with a recipient's public key pk, a sender's secret key sk and a nonce n.
* n should be crypto_box_NONCEBYTES bytes.
* c should be at least crypto_box_MACBYTES + mlen bytes long.
* This function writes the authentication tag, whose length is crypto_box_MACBYTES bytes, in c,
* immediately followed by the encrypted message, whose length is the same as the plaintext: mlen.
*/
fun easy(message : UByteArray, nonce : UByteArray, recipientsPublicKey: UByteArray, sendersSecretKey: UByteArray) : UByteArray
/**
* The crypto_box_open_easy() function verifies and decrypts a ciphertext produced by crypto_box_easy().
* c is a pointer to an authentication tag + encrypted message combination, as produced by crypto_box_easy(). clen is the length of this authentication tag + encrypted message combination. Put differently, clen is the number of bytes written by crypto_box_easy(), which is crypto_box_MACBYTES + the length of the message.
* The nonce n has to match the nonce used to encrypt and authenticate the message.
* pk is the public key of the sender that encrypted the message. sk is the secret key of the recipient that is willing to verify and decrypt it.
* The function throws [BoxCorruptedOrTamperedDataException] if the verification fails.
*/
fun openEasy(ciphertext : UByteArray, nonce: UByteArray, sendersPublicKey: UByteArray, recipientsSecretKey: UByteArray) : UByteArray
/**
* The crypto_box_beforenm() function computes a shared secret key given a public key pk and a secret key sk,
* and puts it into k (crypto_box_BEFORENMBYTES bytes).
*/
fun beforeNM(publicKey: UByteArray, secretKey: UByteArray) : UByteArray
/**
* The _afternm variants of the previously described functions accept a precalculated shared secret key k instead of a key pair.
*/
fun easyAfterNM(message : UByteArray, nonce: UByteArray, precomputedKey: UByteArray) : UByteArray
/**
* The _afternm variants of the previously described functions accept a precalculated shared secret key k instead of a key pair.
*/
fun openEasyAfterNM(ciphertext: UByteArray, nonce: UByteArray, precomputedKey: UByteArray) : UByteArray
/**
* This function encrypts a message m of length mlen with a nonce n and a secret key sk for a recipient whose
* public key is pk, and puts the encrypted message into c.
* Exactly mlen bytes will be put into c, since this function does not prepend the authentication tag.
* The tag, whose size is crypto_box_MACBYTES bytes, will be put into mac.
*/
fun detached(message: UByteArray, nonce: UByteArray, recipientsPublicKey: UByteArray, sendersSecretKey: UByteArray) : BoxEncryptedDataAndTag
/**
* The crypto_box_open_detached() function verifies and decrypts an encrypted message c whose length is clen using the recipient's secret key sk and the sender's public key pk.
* clen doesn't include the tag, so this length is the same as the plaintext.
* The plaintext is put into m after verifying that mac is a valid authentication tag for this ciphertext, with the given nonce n and key k.
* The function throws [BoxCorruptedOrTamperedDataException] if the verification fails.
*/
fun openDetached(ciphertext: UByteArray, tag: UByteArray, nonce: UByteArray, sendersPublicKey: UByteArray, recipientsSecretKey: UByteArray) : UByteArray
fun seal(message: UByteArray, recipientsPublicKey: UByteArray) : UByteArray
fun sealOpen(ciphertext: UByteArray, recipientsSecretKey: UByteArray) : UByteArray
}

View File

@ -22,7 +22,7 @@ val crypto_secretstream_xchacha20poly1305_HEADERBYTES = 24
val crypto_secretstream_xchacha20poly1305_KEYBYTES = 32
val crypto_secretstream_xchacha20poly1305_ABYTES = 17
class SecretStreamCorrupedOrTamperedDataException() : RuntimeException("MAC validation failed. Data is corrupted or tampered with.")
class SecretStreamCorruptedOrTamperedDataException() : RuntimeException("MAC validation failed. Data is corrupted or tampered with.")
expect object SecretStream {

View File

@ -0,0 +1,10 @@
package com.ionspin.kotlin.crypto.util
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 31-Aug-2020
*/
//expect object LibsodiumUtil {
//
//}

View File

@ -6,8 +6,6 @@
| output_formats | |
| pad | |
| unpad | |
| ready | |
| [[ | |
| symbols | |
| to_base64 | |
| to_hex | |
@ -37,9 +35,9 @@
| crypto_auth_keygen | :heavy_check_mark: |
| crypto_auth_verify | :heavy_check_mark: |
| crypto_box_beforenm | |
| crypto_box_curve25519xchacha20poly1305_keypair | |
| crypto_box_curve25519xchacha20poly1305_seal | |
| crypto_box_curve25519xchacha20poly1305_seal_open | |
| crypto_box_curve25519xchacha20poly1305_keypair | not present in LazySodium |
| crypto_box_curve25519xchacha20poly1305_seal | not present in LazySodium |
| crypto_box_curve25519xchacha20poly1305_seal_open |not present in LazySodium |
| crypto_box_detached | |
| crypto_box_easy | |
| crypto_box_easy_afternm | |
@ -70,14 +68,14 @@
| crypto_generichash_keygen | :heavy_check_mark: |
| crypto_generichash_update | :heavy_check_mark: |
| crypto_hash | |
| crypto_hash_sha256 | |
| crypto_hash_sha256_final | |
| crypto_hash_sha256_init | |
| crypto_hash_sha256_update | |
| crypto_hash_sha512 | |
| crypto_hash_sha512_final | |
| crypto_hash_sha512_init | |
| crypto_hash_sha512_update | |
| crypto_hash_sha256 | :heavy_check_mark |
| crypto_hash_sha256_final | :heavy_check_mark |
| crypto_hash_sha256_init | :heavy_check_mark |
| crypto_hash_sha256_update | :heavy_check_mark |
| crypto_hash_sha512 | :heavy_check_mark |
| crypto_hash_sha512_final | :heavy_check_mark |
| crypto_hash_sha512_init | :heavy_check_mark |
| crypto_hash_sha512_update | :heavy_check_mark |
| crypto_kdf_derive_from_key | |
| crypto_kdf_keygen | |
| crypto_kx_client_session_keys | |
@ -245,8 +243,8 @@
| crypto_generichash_blake2b_PERSONALBYTES | |
| crypto_generichash_blake2b_SALTBYTES | |
| crypto_hash_BYTES | |
| crypto_hash_sha256_BYTES | |
| crypto_hash_sha512_BYTES | |
| crypto_hash_sha256_BYTES | :heavy_check_mark: |
| crypto_hash_sha512_BYTES | :heavy_check_mark: |
| crypto_kdf_BYTES_MAX | |
| crypto_kdf_BYTES_MIN | |
| crypto_kdf_CONTEXTBYTES | |