External objects have been moved to Types.kt.
JsSodiumInterface.kt changes: Ubyte -> Byte; UInt -> Int Next step: tests.
This commit is contained in:
		
							parent
							
								
									03116127e8
								
							
						
					
					
						commit
						6a9cfd5006
					
				| @ -10,61 +10,6 @@ import org.khronos.webgl.Uint8Array | ||||
|  * on 27-May-2020 | ||||
|  */ | ||||
| 
 | ||||
| typealias UByte = Int | ||||
| typealias UInt = Long | ||||
| 
 | ||||
| external object Chacha20poly1305EncryptDetachedResult : JsAny { | ||||
|     val ciphertext: Uint8Array | ||||
|     var mac: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoBoxDetachedResult : JsAny { | ||||
|     val ciphertext: Uint8Array | ||||
|     var mac: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoBoxKeypairResult: JsAny { | ||||
|     val publicKey: Uint8Array | ||||
|     val privateKey: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoKxClientSessionKeysResult: JsAny { | ||||
|     val sharedRx: Uint8Array | ||||
|     val sharedTx: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoKxKeypairResult: JsAny { | ||||
|     val publicKey: Uint8Array | ||||
|     val privateKey: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoKxSeedKeypairResult: JsAny { | ||||
|     val publicKey: Uint8Array | ||||
|     val privateKey: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoKxServerSessionKeysResult: JsAny { | ||||
|     val sharedRx: Uint8Array | ||||
|     val sharedTx: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoSecretboxDetachedResult: JsAny { | ||||
|     val cipher: Uint8Array | ||||
|     val mac: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoSecretstreamXchacha20poly1305InitPushResult: JsAny { | ||||
|     val state: Uint8Array | ||||
|     val header: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoSecretstreamXchacha20poly1305PullResult: JsAny { | ||||
|     val message: Uint8Array | ||||
|     val tag: UByte | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| @JsModule("libsodium-wrappers-sumo") | ||||
| external object JsSodiumInterface { | ||||
| 
 | ||||
| @ -153,13 +98,13 @@ external object JsSodiumInterface { | ||||
|     // TODO: два варианта:                      \/ | ||||
|     //  1. Меняем юбайт на байт и юинт на инт   \/ | ||||
|     //  2. Меняем юбайт на инт и юинт на лонг   \/    и далее по списку | ||||
|     fun crypto_secretstream_xchacha20poly1305_push(state: JsAny, message: Uint8Array, associatedData: Uint8Array, tag: UByte) : Uint8Array | ||||
|     fun crypto_secretstream_xchacha20poly1305_push(state: JsAny, message: Uint8Array, associatedData: Uint8Array, tag: Byte) : Uint8Array | ||||
| 
 | ||||
|     //decrypt | ||||
|     @JsName("crypto_secretstream_xchacha20poly1305_init_pull") | ||||
|     fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : JsAny | ||||
|     @JsName("crypto_secretstream_xchacha20poly1305_pull") | ||||
|     fun crypto_secretstream_xchacha20poly1305_pull(state: JsAny, ciphertext: Uint8Array, associatedData: Uint8Array) : CryptoSecretstreamXchacha20poly1305PullResult | ||||
|     fun crypto_secretstream_xchacha20poly1305_pull(state: JsAny, ciphertext: Uint8Array, associatedData: Uint8Array) : JsAny | ||||
| 
 | ||||
|     //keygen and rekey | ||||
|     @JsName("crypto_secretstream_xchacha20poly1305_keygen") | ||||
| @ -243,9 +188,9 @@ external object JsSodiumInterface { | ||||
|     // ---- Box ---- | ||||
| 
 | ||||
|     @JsName("crypto_box_keypair") | ||||
|     fun crypto_box_keypair() : CryptoBoxKeypairResult | ||||
|     fun crypto_box_keypair() : Keypair | ||||
|     @JsName("crypto_box_seed_keypair") | ||||
|     fun crypto_box_seed_keypair(seed : Uint8Array) : CryptoBoxKeypairResult | ||||
|     fun crypto_box_seed_keypair(seed : Uint8Array) : Keypair | ||||
|     @JsName("crypto_box_easy") | ||||
|     fun crypto_box_easy(message: Uint8Array, | ||||
|                         nonce: Uint8Array, | ||||
| @ -302,13 +247,13 @@ external object JsSodiumInterface { | ||||
|     @JsName("crypto_sign_final_verify") | ||||
|     fun crypto_sign_final_verify(state: JsAny, signature: Uint8Array, publicKey: Uint8Array) : Boolean | ||||
|     @JsName("crypto_sign_init") | ||||
|     fun crypto_sign_init() : JsAny | ||||
|     fun crypto_sign_init() : SignatureStateType | ||||
|     @JsName("crypto_sign_keypair") | ||||
|     fun crypto_sign_keypair() : JsAny | ||||
|     fun crypto_sign_keypair() : Keypair | ||||
|     @JsName("crypto_sign_open") | ||||
|     fun crypto_sign_open(signedMessage: Uint8Array, publicKey: Uint8Array) : Uint8Array | ||||
|     @JsName("crypto_sign_seed_keypair") | ||||
|     fun crypto_sign_seed_keypair(seed: Uint8Array) : JsAny | ||||
|     fun crypto_sign_seed_keypair(seed: Uint8Array) : Keypair | ||||
|     @JsName("crypto_sign_update") | ||||
|     fun crypto_sign_update(state: JsAny, message: Uint8Array) | ||||
|     @JsName("crypto_sign_verify_detached") | ||||
| @ -321,7 +266,7 @@ external object JsSodiumInterface { | ||||
|     // ---- KDF ---- | ||||
| 
 | ||||
|     @JsName("crypto_kdf_derive_from_key") | ||||
|     fun crypto_kdf_derive_from_key(subkey_len: UInt, subkeyId : UInt, ctx: String, key: Uint8Array) : Uint8Array | ||||
|     fun crypto_kdf_derive_from_key(subkey_len: Int, subkeyId : Int, ctx: String, key: Uint8Array) : Uint8Array | ||||
|     @JsName("crypto_kdf_keygen") | ||||
|     fun crypto_kdf_keygen() : Uint8Array | ||||
| 
 | ||||
| @ -330,11 +275,11 @@ external object JsSodiumInterface { | ||||
|     // ---- Password hashing ---- | ||||
| 
 | ||||
|     @JsName("crypto_pwhash") | ||||
|     fun crypto_pwhash(keyLength : UInt, password : Uint8Array, salt: Uint8Array, opsLimit: UInt, memLimit: UInt, algorithm: UInt) : Uint8Array | ||||
|     fun crypto_pwhash(keyLength : Int, password : Uint8Array, salt: Uint8Array, opsLimit: Int, memLimit: Int, algorithm: Int) : Uint8Array | ||||
|     @JsName("crypto_pwhash_str") | ||||
|     fun crypto_pwhash_str(password: Uint8Array, opsLimit: UInt, memLimit: UInt) : String | ||||
|     fun crypto_pwhash_str(password: Uint8Array, opsLimit: Int, memLimit: Int) : String | ||||
|     @JsName("crypto_pwhash_str_needs_rehash") | ||||
|     fun crypto_pwhash_str_needs_rehash(hashedPassword: String, opsLimit: UInt, memLimit: UInt) : Boolean | ||||
|     fun crypto_pwhash_str_needs_rehash(hashedPassword: String, opsLimit: Int, memLimit: Int) : Boolean | ||||
|     @JsName("crypto_pwhash_str_verify") | ||||
|     fun crypto_pwhash_str_verify(hashedPassword: String, password: Uint8Array) : Boolean | ||||
| 
 | ||||
| @ -369,11 +314,11 @@ external object JsSodiumInterface { | ||||
|     @JsName("randombytes_buf") | ||||
|     fun randombytes_buf(length: Int) : Uint8Array | ||||
|     @JsName("randombytes_buf_deterministic") | ||||
|     fun randombytes_buf_deterministic(length: UInt, seed : Uint8Array) : Uint8Array | ||||
|     fun randombytes_buf_deterministic(length: Int, seed : Uint8Array) : Uint8Array | ||||
|     @JsName("randombytes_random") | ||||
|     fun randombytes_random() : UInt | ||||
|     fun randombytes_random() : Int | ||||
|     @JsName("randombytes_uniform") | ||||
|     fun randombytes_uniform(upper_bound: UInt) : UInt | ||||
|     fun randombytes_uniform(upper_bound: Int) : Int | ||||
| 
 | ||||
|     // ---- Utils end ---- | ||||
| 
 | ||||
| @ -381,9 +326,9 @@ external object JsSodiumInterface { | ||||
|     @JsName("crypto_kx_client_session_keys") | ||||
|     fun crypto_kx_client_session_keys(clientPublicKey: Uint8Array, clientSecretKey: Uint8Array, serverPublicKey: Uint8Array) : CryptoKxClientSessionKeysResult | ||||
|     @JsName("crypto_kx_keypair") | ||||
|     fun crypto_kx_keypair() : CryptoKxKeypairResult | ||||
|     fun crypto_kx_keypair() : Keypair | ||||
|     @JsName("crypto_kx_seed_keypair") | ||||
|     fun crypto_kx_seed_keypair(seed: Uint8Array) : CryptoKxSeedKeypairResult | ||||
|     fun crypto_kx_seed_keypair(seed: Uint8Array) : Keypair | ||||
|     @JsName("crypto_kx_server_session_keys") | ||||
|     fun crypto_kx_server_session_keys(serverPublicKey: Uint8Array, serverSecretKey: Uint8Array, clientPublicKey: Uint8Array) : CryptoKxServerSessionKeysResult | ||||
| 
 | ||||
| @ -391,24 +336,24 @@ external object JsSodiumInterface { | ||||
| 
 | ||||
|     // -- Stream ---- | ||||
|     @JsName("crypto_stream_chacha20") | ||||
|     fun crypto_stream_chacha20(outLength: UInt, key: Uint8Array, nonce: Uint8Array) : Uint8Array | ||||
|     fun crypto_stream_chacha20(outLength: Int, key: Uint8Array, nonce: Uint8Array) : Uint8Array | ||||
|     @JsName("crypto_stream_chacha20_ietf_xor") | ||||
|     fun crypto_stream_chacha20_ietf_xor(message : Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array | ||||
|     @JsName("crypto_stream_chacha20_ietf_xor_ic") | ||||
|     fun crypto_stream_chacha20_ietf_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: UInt, key: Uint8Array) : Uint8Array | ||||
|     fun crypto_stream_chacha20_ietf_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: Int, key: Uint8Array) : Uint8Array | ||||
|     @JsName("crypto_stream_chacha20_keygen") | ||||
|     fun crypto_stream_chacha20_keygen() : Uint8Array | ||||
|     @JsName("crypto_stream_chacha20_xor") | ||||
|     fun crypto_stream_chacha20_xor(message : Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array | ||||
|     @JsName("crypto_stream_chacha20_xor_ic") | ||||
|     fun crypto_stream_chacha20_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: UInt, key: Uint8Array) : Uint8Array | ||||
|     fun crypto_stream_chacha20_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: Int, key: Uint8Array) : Uint8Array | ||||
| 
 | ||||
|     @JsName("crypto_stream_xchacha20_keygen") | ||||
|     fun crypto_stream_xchacha20_keygen() : Uint8Array | ||||
|     @JsName("crypto_stream_xchacha20_xor") | ||||
|     fun crypto_stream_xchacha20_xor(message : Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array | ||||
|     @JsName("crypto_stream_xchacha20_xor_ic") | ||||
|     fun crypto_stream_xchacha20_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: UInt, key: Uint8Array) : Uint8Array | ||||
|     fun crypto_stream_xchacha20_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: Int, key: Uint8Array) : Uint8Array | ||||
| 
 | ||||
|     // ---- Stream end ---- | ||||
| 
 | ||||
|  | ||||
| @ -0,0 +1,58 @@ | ||||
| package ext.libsodium.com.ionspin.kotlin.crypto | ||||
| 
 | ||||
| 
 | ||||
| import org.khronos.webgl.Uint8Array | ||||
| 
 | ||||
| //TODO: может быть стоит поудалять ненужное | ||||
| 
 | ||||
| external object SignatureStateType: JsAny | ||||
| 
 | ||||
| external object Chacha20poly1305EncryptDetachedResult : JsAny { | ||||
|     val ciphertext: Uint8Array | ||||
|     var mac: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoBoxDetachedResult : JsAny { | ||||
|     val ciphertext: Uint8Array | ||||
|     var mac: Uint8Array | ||||
| } | ||||
| 
 | ||||
| //external object CryptoBoxKeypairResult: JsAny { | ||||
| //    val publicKey: Uint8Array | ||||
| //    val privateKey: Uint8Array | ||||
| //} | ||||
| 
 | ||||
| external object CryptoKxClientSessionKeysResult: JsAny { | ||||
|     val sharedRx: Uint8Array | ||||
|     val sharedTx: Uint8Array | ||||
| } | ||||
| 
 | ||||
| //external object CryptoKxKeypairResult: JsAny { | ||||
| //    val publicKey: Uint8Array | ||||
| //    val privateKey: Uint8Array | ||||
| //} | ||||
| 
 | ||||
| external object Keypair: JsAny { | ||||
|     val publicKey: Uint8Array | ||||
|     val privateKey: Uint8Array | ||||
| } | ||||
| // | ||||
| external object CryptoKxServerSessionKeysResult: JsAny { | ||||
|     val sharedRx: Uint8Array | ||||
|     val sharedTx: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoSecretboxDetachedResult: JsAny { | ||||
|     val cipher: Uint8Array | ||||
|     val mac: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoSecretstreamXchacha20poly1305InitPushResult: JsAny { | ||||
|     val state: Uint8Array | ||||
|     val header: Uint8Array | ||||
| } | ||||
| 
 | ||||
| external object CryptoSecretstreamXchacha20poly1305PullResult: JsAny { | ||||
|     val message: Uint8Array | ||||
|     val tag: Byte | ||||
| } | ||||
| @ -26,8 +26,8 @@ actual object Kdf { | ||||
|         masterKey: UByteArray | ||||
|     ): UByteArray { | ||||
|         return getSodium().crypto_kdf_derive_from_key( | ||||
|             subkeyLength.toLong(), | ||||
|             subkeyId.toLong(), | ||||
|             subkeyLength, | ||||
|             subkeyId.toInt(), | ||||
|             context, | ||||
|             masterKey.toUInt8Array() | ||||
|         ).toUByteArray() | ||||
|  | ||||
| @ -26,12 +26,12 @@ actual object PasswordHash { | ||||
|             throw RuntimeException("Javascript doesnt support more than ${UInt.MAX_VALUE} for opslimit") | ||||
|         } | ||||
|         return getSodium().crypto_pwhash( | ||||
|             outputLength.toLong(), | ||||
|             outputLength, | ||||
|             password.encodeToUByteArray().toUInt8Array(), | ||||
|             salt.toUInt8Array(), | ||||
|             opsLimit.toLong(), | ||||
|             memLimit.toLong(), | ||||
|             algorithm.toLong() | ||||
|             opsLimit.toInt(), | ||||
|             memLimit, | ||||
|             algorithm | ||||
|         ).toUByteArray() | ||||
|     } | ||||
| 
 | ||||
| @ -51,8 +51,8 @@ actual object PasswordHash { | ||||
|         } | ||||
|         return getSodium().crypto_pwhash_str( | ||||
|             password.encodeToUByteArray().toUInt8Array(), | ||||
|             opslimit.toLong(), | ||||
|             memlimit.toLong() | ||||
|             opslimit.toInt(), | ||||
|             memlimit | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
| @ -73,8 +73,8 @@ actual object PasswordHash { | ||||
|         return if ( | ||||
|             getSodium().crypto_pwhash_str_needs_rehash( | ||||
|                 passwordHash, | ||||
|                 opslimit.toLong(), | ||||
|                 memlimit.toLong() | ||||
|                 opslimit.toInt(), | ||||
|                 memlimit | ||||
|             ) | ||||
|         ) { | ||||
|             1 | ||||
|  | ||||
| @ -1,15 +1,11 @@ | ||||
| package com.ionspin.kotlin.crypto.secretstream | ||||
| 
 | ||||
| import com.ionspin.kotlin.crypto.getSodium | ||||
| import ext.libsodium.com.ionspin.kotlin.crypto.CryptoSecretstreamXchacha20poly1305PullResult | ||||
| import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray | ||||
| import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array | ||||
| import org.khronos.webgl.Uint8Array | ||||
| 
 | ||||
| //TODO: вынести все external objects в отдельный файл (и в JsSodiumInterface.kt) | ||||
| external object CryptoSecretstreamXchacha20poly1305PullResult: JsAny { | ||||
|     val message: Uint8Array | ||||
|     val tag: Byte | ||||
| } | ||||
| 
 | ||||
| external object SecretStreamStateType: JsAny | ||||
| 
 | ||||
|  | ||||
| @ -1,11 +1,14 @@ | ||||
| package com.ionspin.kotlin.crypto.signature | ||||
| 
 | ||||
| import com.ionspin.kotlin.crypto.getSodium | ||||
| import ext.libsodium.com.ionspin.kotlin.crypto.SignatureStateType | ||||
| import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray | ||||
| import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array | ||||
| import org.khronos.webgl.Uint8Array | ||||
| 
 | ||||
| actual typealias SignatureState = Any | ||||
| 
 | ||||
| 
 | ||||
| actual typealias SignatureState = SignatureStateType | ||||
| 
 | ||||
| actual object Signature { | ||||
|     actual fun init(): SignatureState { | ||||
| @ -48,8 +51,8 @@ actual object Signature { | ||||
|     actual fun keypair(): SignatureKeyPair { | ||||
|         val keypair = getSodium().crypto_sign_keypair() | ||||
|         return SignatureKeyPair( | ||||
|             (keypair.publicKey as Uint8Array).toUByteArray(), | ||||
|             (keypair.privateKey as Uint8Array).toUByteArray() | ||||
|             keypair.publicKey.toUByteArray(), | ||||
|             keypair.privateKey.toUByteArray() | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
| @ -61,8 +64,8 @@ actual object Signature { | ||||
|     actual fun seedKeypair(seed: UByteArray): SignatureKeyPair { | ||||
|         val keypair = getSodium().crypto_sign_seed_keypair(seed.toUInt8Array()) | ||||
|         return SignatureKeyPair( | ||||
|             (keypair.publicKey as Uint8Array).toUByteArray(), | ||||
|             (keypair.privateKey as Uint8Array).toUByteArray() | ||||
|             keypair.publicKey.toUByteArray(), | ||||
|             keypair.privateKey.toUByteArray() | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -7,7 +7,7 @@ import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array | ||||
| actual object Stream { | ||||
|     actual fun chacha20(clen: Int, nonce: UByteArray, key: UByteArray): UByteArray { | ||||
|         //Note, unlike the other ones, here the positions of key and nonce are reversed. | ||||
|         val result = getSodium().crypto_stream_chacha20(clen.toUInt(), key.toUInt8Array(), nonce.toUInt8Array()) | ||||
|         val result = getSodium().crypto_stream_chacha20(clen, key.toUInt8Array(), nonce.toUInt8Array()) | ||||
| 
 | ||||
|         return result.toUByteArray() | ||||
|     } | ||||
| @ -35,7 +35,7 @@ actual object Stream { | ||||
|         val result = getSodium().crypto_stream_chacha20_ietf_xor_ic( | ||||
|             message.toUInt8Array(), | ||||
|             nonce.toUInt8Array(), | ||||
|             initialCounter, | ||||
|             initialCounter.toInt(), | ||||
|             key.toUInt8Array() | ||||
|         ) | ||||
| 
 | ||||
| @ -74,7 +74,7 @@ actual object Stream { | ||||
|         val result = getSodium().crypto_stream_chacha20_xor_ic( | ||||
|             message.toUInt8Array(), | ||||
|             nonce.toUInt8Array(), | ||||
|             initialCounter.toUInt(), | ||||
|             initialCounter.toInt(), | ||||
|             key.toUInt8Array() | ||||
|         ) | ||||
| 
 | ||||
|  | ||||
| @ -26,14 +26,14 @@ actual object LibsodiumRandom { | ||||
|      * | ||||
|      */ | ||||
|     actual fun bufDeterministic(size: Int, seed: UByteArray): UByteArray { | ||||
|         return getSodium().randombytes_buf_deterministic(size.toUInt(), seed.toUInt8Array()).toUByteArray() | ||||
|         return getSodium().randombytes_buf_deterministic(size, seed.toUInt8Array()).toUByteArray() | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * The randombytes_random() function returns an unpredictable value between 0 and 0xffffffff (included). | ||||
|      */ | ||||
|     actual fun random(): UInt { | ||||
|         return getSodium().randombytes_random() | ||||
|         return getSodium().randombytes_random().toUInt() | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
| @ -42,7 +42,7 @@ actual object LibsodiumRandom { | ||||
|      * upper_bound is not a power of 2. Note that an upper_bound < 2 leaves only a single element to be chosen, namely 0 | ||||
|      */ | ||||
|     actual fun uniform(upperBound: UInt): UInt { | ||||
|         return getSodium().randombytes_uniform(upperBound) | ||||
|         return getSodium().randombytes_uniform(upperBound.toInt()).toUInt() | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user