diff --git a/libsodium_js_debug_list.txt b/libsodium_js_debug_list.txt new file mode 100644 index 0000000..01d4ff7 --- /dev/null +++ b/libsodium_js_debug_list.txt @@ -0,0 +1,395 @@ +add = function(e,t){if(!(e instanceof Uint8Array&&t instanceof Uint8Array))throw new TypeError("Only Uint8Array instances can added");var r=e.length,a=0,_=0;if(t.length!=e.length)throw new TypeError("Arguments must have the same length");for(_=0;_>=8,a+=e[_]+t[_],e[_]=255&a} +base64_variants = Object {ORIGINAL: 1, +ORIGINAL_NO_PADDING: 3, +URLSAFE: 5, +URLSAFE_NO_PADDING: 7} +compare = function(e,t){if(!(e instanceof Uint8Array&&t instanceof Uint8Array))throw new TypeError("Only Uint8Array instances can be compared");if(e.length!==t.length)throw new TypeError("Only instances of identical length can be compared");for(var r=0,a=1,_=e.length;_-- >0;)r|=t[_]-e[_]>>8&a,a&=(t[_]^e[_])-1>>8;return r+r+a-1} +from_base64 = function(e,t){t=o(t);var a,_=[],n=new l(3*(e=m(_,e,"input")).length/4),s=u(e),c=d(4),h=d(4);return _.push(s),_.push(n.address),_.push(n.result_bin_len_p),_.push(n.b64_end_p),0!==r._sodium_base642bin(n.address,n.length,s,e.length,0,c,h,t)&&g(_,"invalid input"),r.getValue(h,"i32")-s!==e.length&&g(_,"incomplete input"),n.length=r.getValue(c,"i32"),a=n.to_Uint8Array(),v(_),a} +from_hex = function(e){var t,a=[],_=new l((e=m(a,e,"input")).length/2),n=u(e),s=d(4);return a.push(n),a.push(_.address),a.push(_.hex_end_p),0!==r._sodium_hex2bin(_.address,_.length,n,e.length,0,0,s)&&g(a,"invalid input"),r.getValue(s,"i32")-n!==e.length&&g(a,"incomplete input"),t=_.to_Uint8Array(),v(a),t} +from_string = function _(e){if("function"==typeof TextEncoder)return(new TextEncoder).encode(e);e=unescape(encodeURIComponent(e));for(var t=new Uint8Array(e.length),r=0,a=e.length;r>=8,t+=e[r],e[r]=255&t} +is_zero = function(e){if(!(e instanceof Uint8Array))throw new TypeError("Only Uint8Array instances can be checked");for(var t=0,r=0,a=e.length;r 0");var a,_=[],n=d(4),s=1,c=0,o=0|e.length,h=new l(o+t);_.push(n),_.push(h.address);for(var p=h.address,y=h.address+o+t;p>>48|o>>>32|o>>>16|o))-1>>16);return 0!==r._sodium_pad(n,h.address,e.length,t,h.length)&&g(_,"internal error"),h.length=r.getValue(n,"i32"),a=h.to_Uint8Array(),v(_),a} +unpad = function(e,t){if(!(e instanceof Uint8Array))throw new TypeError("buffer must be a Uint8Array");if((t|=0)<=0)throw new Error("block size must be > 0");var a=[],_=u(e),n=d(4);return a.push(_),a.push(n),0!==r._sodium_unpad(n,_,e.length,t)&&g(a,"unsupported/invalid padding"),e=(e=new Uint8Array(e)).subarray(0,r.getValue(n,"i32")),v(a),e} +ready = Promise {[[PromiseStatus]]: "resolved", +[[PromiseValue]]: undefined} +symbols = function(){return Object.keys(e).sort()} +to_base64 = function h(e,t){t=o(t),e=m(_,e,"input");var a,_=[],s=0|Math.floor(e.length/3),c=e.length-3*s,h=4*s+(0!==c?0==(2&t)?4:2+(c>>>1):0),p=new l(h+1),y=u(e);return _.push(y),_.push(p.address),0===r._sodium_bin2base64(p.address,p.length,y,e.length,t)&&g(_,"conversion failed"),p.length=h,a=n(p.to_Uint8Array()),v(_),a} +to_hex = function s(e){e=m(null,e,"input");for(var t,r,a,_="",n=0;n>8&-39)<<8|87+(t=e[n]>>>4)+(t-10>>8&-39),_+=String.fromCharCode(255&a)+String.fromCharCode(a>>>8);return _} +to_string = function n(e){if("function"==typeof TextDecoder)return new TextDecoder("utf-8",{fatal:!0}).decode(e);var t=Math.ceil(e.length/8192);if(t<=1)try{return decodeURIComponent(escape(String.fromCharCode.apply(null,e)))}catch(e){throw new TypeError("The encoded data was not valid.")}for(var r="",a=0,_=0;_=240?(h=4,c=!0):p>=224?(h=3,c=!0):p>=192?(h=2,c=!0):p<128&&(h=1,c=!0)}while(!c);for(var y=h-(s.length-o),i=0;i>>24>>>8,o,y);var x=p(g,s);return v(c),x} +crypto_kdf_keygen = function Le(e){var t=[];i(e);var a=new l(0|r._crypto_kdf_keybytes()),_=a.address;t.push(_),r._crypto_kdf_keygen(_);var n=p(a,e);return v(t),n} +crypto_kx_client_session_keys = function Ue(e,t,a,_){var n=[];i(_),e=m(n,e,"clientPublicKey");var s,c=0|r._crypto_kx_publickeybytes();e.length!==c&&b(n,"invalid clientPublicKey length"),s=u(e),n.push(s),t=m(n,t,"clientSecretKey");var o,h=0|r._crypto_kx_secretkeybytes();t.length!==h&&b(n,"invalid clientSecretKey length"),o=u(t),n.push(o),a=m(n,a,"serverPublicKey");var y,d=0|r._crypto_kx_publickeybytes();a.length!==d&&b(n,"invalid serverPublicKey length"),y=u(a),n.push(y);var f=new l(0|r._crypto_kx_sessionkeybytes()),E=f.address;n.push(E);var x=new l(0|r._crypto_kx_sessionkeybytes()),k=x.address;if(n.push(k),0==(0|r._crypto_kx_client_session_keys(E,k,s,o,y))){var S=p({sharedRx:f,sharedTx:x},_);return v(n),S}g(n,"invalid usage")} +crypto_kx_keypair = function Oe(e){var t=[];i(e);var a=new l(0|r._crypto_kx_publickeybytes()),_=a.address;t.push(_);var n=new l(0|r._crypto_kx_secretkeybytes()),s=n.address;if(t.push(s),0==(0|r._crypto_kx_keypair(_,s))){var c={publicKey:p(a,e),privateKey:p(n,e),keyType:"x25519"};return v(t),c}g(t,"internal error")} +crypto_kx_seed_keypair = function Ce(e,t){var a=[];i(t),e=m(a,e,"seed");var _,n=0|r._crypto_kx_seedbytes();e.length!==n&&b(a,"invalid seed length"),_=u(e),a.push(_);var s=new l(0|r._crypto_kx_publickeybytes()),c=s.address;a.push(c);var o=new l(0|r._crypto_kx_secretkeybytes()),h=o.address;if(a.push(h),0==(0|r._crypto_kx_seed_keypair(c,h,_))){var y={publicKey:p(s,t),privateKey:p(o,t),keyType:"x25519"};return v(a),y}g(a,"internal error")} +crypto_kx_server_session_keys = function Re(e,t,a,_){var n=[];i(_),e=m(n,e,"serverPublicKey");var s,c=0|r._crypto_kx_publickeybytes();e.length!==c&&b(n,"invalid serverPublicKey length"),s=u(e),n.push(s),t=m(n,t,"serverSecretKey");var o,h=0|r._crypto_kx_secretkeybytes();t.length!==h&&b(n,"invalid serverSecretKey length"),o=u(t),n.push(o),a=m(n,a,"clientPublicKey");var y,d=0|r._crypto_kx_publickeybytes();a.length!==d&&b(n,"invalid clientPublicKey length"),y=u(a),n.push(y);var f=new l(0|r._crypto_kx_sessionkeybytes()),E=f.address;n.push(E);var x=new l(0|r._crypto_kx_sessionkeybytes()),k=x.address;if(n.push(k),0==(0|r._crypto_kx_server_session_keys(E,k,s,o,y))){var S=p({sharedRx:f,sharedTx:x},_);return v(n),S}g(n,"invalid usage")} +crypto_onetimeauth = function Pe(e,t,a){var _=[];i(a);var n=u(e=m(_,e,"message")),s=e.length;_.push(n),t=m(_,t,"key");var c,o=0|r._crypto_onetimeauth_keybytes();t.length!==o&&b(_,"invalid key length"),c=u(t),_.push(c);var h=new l(0|r._crypto_onetimeauth_bytes()),y=h.address;if(_.push(y),0==(0|r._crypto_onetimeauth(y,n,s,0,c))){var d=p(h,a);return v(_),d}g(_,"invalid usage")} +crypto_onetimeauth_final = function Ge(e,t){var a=[];i(t),f(a,e,"state_address");var _=new l(0|r._crypto_onetimeauth_bytes()),n=_.address;if(a.push(n),0==(0|r._crypto_onetimeauth_final(e,n))){var s=(r._free(e),p(_,t));return v(a),s}g(a,"invalid usage")} +crypto_onetimeauth_init = function Xe(e,t){var a=[];i(t);var _=null;null!=e&&(_=u(e=m(a,e,"key")),e.length,a.push(_));var n=new l(144).address;if(0==(0|r._crypto_onetimeauth_init(n,_))){var s=n;return v(a),s}g(a,"invalid usage")} +crypto_onetimeauth_keygen = function De(e){var t=[];i(e);var a=new l(0|r._crypto_onetimeauth_keybytes()),_=a.address;t.push(_),r._crypto_onetimeauth_keygen(_);var n=p(a,e);return v(t),n} +crypto_onetimeauth_update = function Fe(e,t,a){var _=[];i(a),f(_,e,"state_address");var n=u(t=m(_,t,"message_chunk")),s=t.length;_.push(n),0!=(0|r._crypto_onetimeauth_update(e,n,s))&&g(_,"invalid usage"),v(_)} +crypto_onetimeauth_verify = function Ve(e,t,a){var _=[];e=m(_,e,"hash");var n,s=0|r._crypto_onetimeauth_bytes();e.length!==s&&b(_,"invalid hash length"),n=u(e),_.push(n);var c=u(t=m(_,t,"message")),o=t.length;_.push(c),a=m(_,a,"key");var h,p=0|r._crypto_onetimeauth_keybytes();a.length!==p&&b(_,"invalid key length"),h=u(a),_.push(h);var y=0==(0|r._crypto_onetimeauth_verify(n,c,o,0,h));return v(_),y} +crypto_pwhash = function He(e,t,a,_,n,s,c){var o=[];i(c),f(o,e,"keyLength"),("number"!=typeof e||(0|e)!==e||e<0)&&b(o,"keyLength must be an unsigned integer");var h=u(t=m(o,t,"password")),y=t.length;o.push(h),a=m(o,a,"salt");var d,E=0|r._crypto_pwhash_saltbytes();a.length!==E&&b(o,"invalid salt length"),d=u(a),o.push(d),f(o,_,"opsLimit"),("number"!=typeof _||(0|_)!==_||_<0)&&b(o,"opsLimit must be an unsigned integer"),f(o,n,"memLimit"),("number"!=typeof n||(0|n)!==n||n<0)&&b(o,"memLimit must be an unsigned integer"),f(o,s,"algorithm"),("number"!=typeof s||(0|s)!==s||s<0)&&b(o,"algorithm must be an unsigned integer");var x=new l(0|e),k=x.address;if(o.push(k),0==(0|r._crypto_pwhash(k,e,0,h,y,0,d,_,0,n,s))){var S=p(x,c);return v(o),S}g(o,"invalid usage")} +crypto_pwhash_scryptsalsa208sha256 = function ze(e,t,a,_,n,s){var c=[];i(s),f(c,e,"keyLength"),("number"!=typeof e||(0|e)!==e||e<0)&&b(c,"keyLength must be an unsigned integer");var o=u(t=m(c,t,"password")),h=t.length;c.push(o),a=m(c,a,"salt");var y,d=0|r._crypto_pwhash_scryptsalsa208sha256_saltbytes();a.length!==d&&b(c,"invalid salt length"),y=u(a),c.push(y),f(c,_,"opsLimit"),("number"!=typeof _||(0|_)!==_||_<0)&&b(c,"opsLimit must be an unsigned integer"),f(c,n,"memLimit"),("number"!=typeof n||(0|n)!==n||n<0)&&b(c,"memLimit must be an unsigned integer");var E=new l(0|e),x=E.address;if(c.push(x),0==(0|r._crypto_pwhash_scryptsalsa208sha256(x,e,0,o,h,0,y,_,0,n))){var k=p(E,s);return v(c),k}g(c,"invalid usage")} +crypto_pwhash_scryptsalsa208sha256_ll = function je(e,t,a,_,n,s,c){var o=[];i(c);var h=u(e=m(o,e,"password")),y=e.length;o.push(h);var d=u(t=m(o,t,"salt")),E=t.length;o.push(d),f(o,a,"opsLimit"),("number"!=typeof a||(0|a)!==a||a<0)&&b(o,"opsLimit must be an unsigned integer"),f(o,_,"r"),("number"!=typeof _||(0|_)!==_||_<0)&&b(o,"r must be an unsigned integer"),f(o,n,"p"),("number"!=typeof n||(0|n)!==n||n<0)&&b(o,"p must be an unsigned integer"),f(o,s,"keyLength"),("number"!=typeof s||(0|s)!==s||s<0)&&b(o,"keyLength must be an unsigned integer");var x=new l(0|s),k=x.address;if(o.push(k),0==(0|r._crypto_pwhash_scryptsalsa208sha256_ll(h,y,d,E,a,0,_,n,k,s))){var S=p(x,c);return v(o),S}g(o,"invalid usage")} +crypto_pwhash_scryptsalsa208sha256_str = function qe(e,t,a,_){var n=[];i(_);var s=u(e=m(n,e,"password")),c=e.length;n.push(s),f(n,t,"opsLimit"),("number"!=typeof t||(0|t)!==t||t<0)&&b(n,"opsLimit must be an unsigned integer"),f(n,a,"memLimit"),("number"!=typeof a||(0|a)!==a||a<0)&&b(n,"memLimit must be an unsigned integer");var o=new l(0|r._crypto_pwhash_scryptsalsa208sha256_strbytes()).address;if(n.push(o),0==(0|r._crypto_pwhash_scryptsalsa208sha256_str(o,s,c,0,t,0,a))){var h=r.UTF8ToString(o);return v(n),h}g(n,"invalid usage")} +crypto_pwhash_scryptsalsa208sha256_str_verify = function We(e,t,a){var n=[];i(a),"string"!=typeof e&&b(n,"hashed_password must be a string"),e=_(e+"\0"),null!=c&&e.length-1!==c&&b(n,"invalid hashed_password length");var s=u(e),c=e.length-1;n.push(s);var o=u(t=m(n,t,"password")),h=t.length;n.push(o);var p=0==(0|r._crypto_pwhash_scryptsalsa208sha256_str_verify(s,o,h,0));return v(n),p} +crypto_pwhash_str = function Je(e,t,a,_){var n=[];i(_);var s=u(e=m(n,e,"password")),c=e.length;n.push(s),f(n,t,"opsLimit"),("number"!=typeof t||(0|t)!==t||t<0)&&b(n,"opsLimit must be an unsigned integer"),f(n,a,"memLimit"),("number"!=typeof a||(0|a)!==a||a<0)&&b(n,"memLimit must be an unsigned integer");var o=new l(0|r._crypto_pwhash_strbytes()).address;if(n.push(o),0==(0|r._crypto_pwhash_str(o,s,c,0,t,0,a))){var h=r.UTF8ToString(o);return v(n),h}g(n,"invalid usage")} +crypto_pwhash_str_needs_rehash = function Qe(e,t,a,n){var s=[];i(n),"string"!=typeof e&&b(s,"hashed_password must be a string"),e=_(e+"\0"),null!=o&&e.length-1!==o&&b(s,"invalid hashed_password length");var c=u(e),o=e.length-1;s.push(c),f(s,t,"opsLimit"),("number"!=typeof t||(0|t)!==t||t<0)&&b(s,"opsLimit must be an unsigned integer"),f(s,a,"memLimit"),("number"!=typeof a||(0|a)!==a||a<0)&&b(s,"memLimit must be an unsigned integer");var h=0!=(0|r._crypto_pwhash_str_needs_rehash(c,t,0,a));return v(s),h} +crypto_pwhash_str_verify = function Ze(e,t,a){var n=[];i(a),"string"!=typeof e&&b(n,"hashed_password must be a string"),e=_(e+"\0"),null!=c&&e.length-1!==c&&b(n,"invalid hashed_password length");var s=u(e),c=e.length-1;n.push(s);var o=u(t=m(n,t,"password")),h=t.length;n.push(o);var p=0==(0|r._crypto_pwhash_str_verify(s,o,h,0));return v(n),p} +crypto_scalarmult = function $e(e,t,a){var _=[];i(a),e=m(_,e,"privateKey");var n,s=0|r._crypto_scalarmult_scalarbytes();e.length!==s&&b(_,"invalid privateKey length"),n=u(e),_.push(n),t=m(_,t,"publicKey");var c,o=0|r._crypto_scalarmult_scalarbytes();t.length!==o&&b(_,"invalid publicKey length"),c=u(t),_.push(c);var h=new l(0|r._crypto_scalarmult_bytes()),y=h.address;if(_.push(y),0==(0|r._crypto_scalarmult(y,n,c))){var d=p(h,a);return v(_),d}g(_,"weak public key")} +crypto_scalarmult_base = function et(e,t){var a=[];i(t),e=m(a,e,"privateKey");var _,n=0|r._crypto_scalarmult_scalarbytes();e.length!==n&&b(a,"invalid privateKey length"),_=u(e),a.push(_);var s=new l(0|r._crypto_scalarmult_scalarbytes()),c=s.address;if(a.push(c),0==(0|r._crypto_scalarmult_base(c,_))){var o=p(s,t);return v(a),o}g(a,"insecure scalar")} +crypto_scalarmult_ristretto255 = function tt(e,t,a){var _=[];i(a),e=m(_,e,"scalar");var n,s=0|r._crypto_scalarmult_ristretto255_scalarbytes();e.length!==s&&b(_,"invalid scalar length"),n=u(e),_.push(n),t=m(_,t,"privateKey");var c,o=0|r._crypto_scalarmult_ristretto255_bytes();t.length!==o&&b(_,"invalid privateKey length"),c=u(t),_.push(c);var h=new l(0|r._crypto_scalarmult_ristretto255_bytes()),y=h.address;if(_.push(y),0==(0|r._crypto_scalarmult_ristretto255(y,n,c))){var d=p(h,a);return v(_),d}g(_,"input is zero element")} +crypto_scalarmult_ristretto255_base = function rt(e,t){var a=[];i(t);var _=u(e=m(a,e,"scalar"));e.length,a.push(_);var n=new l(0|r._crypto_core_ristretto255_bytes()),s=n.address;if(a.push(s),0==(0|r._crypto_scalarmult_ristretto255_base(s,_))){var c=p(n,t);return v(a),c}g(a,"scalar is 0")} +crypto_secretbox_detached = function at(e,t,a,_){var n=[];i(_);var s=u(e=m(n,e,"message")),c=e.length;n.push(s),t=m(n,t,"nonce");var o,h=0|r._crypto_secretbox_noncebytes();t.length!==h&&b(n,"invalid nonce length"),o=u(t),n.push(o),a=m(n,a,"key");var y,d=0|r._crypto_secretbox_keybytes();a.length!==d&&b(n,"invalid key length"),y=u(a),n.push(y);var f=new l(0|c),E=f.address;n.push(E);var x=new l(0|r._crypto_secretbox_macbytes()),k=x.address;if(n.push(k),0==(0|r._crypto_secretbox_detached(E,k,s,c,0,o,y))){var S=p({mac:x,cipher:f},_);return v(n),S}g(n,"invalid usage")} +crypto_secretbox_easy = function _t(e,t,a,_){var n=[];i(_);var s=u(e=m(n,e,"message")),c=e.length;n.push(s),t=m(n,t,"nonce");var o,h=0|r._crypto_secretbox_noncebytes();t.length!==h&&b(n,"invalid nonce length"),o=u(t),n.push(o),a=m(n,a,"key");var y,d=0|r._crypto_secretbox_keybytes();a.length!==d&&b(n,"invalid key length"),y=u(a),n.push(y);var f=new l(c+r._crypto_secretbox_macbytes()|0),E=f.address;if(n.push(E),0==(0|r._crypto_secretbox_easy(E,s,c,0,o,y))){var x=p(f,_);return v(n),x}g(n,"invalid usage")} +crypto_secretbox_keygen = function nt(e){var t=[];i(e);var a=new l(0|r._crypto_secretbox_keybytes()),_=a.address;t.push(_),r._crypto_secretbox_keygen(_);var n=p(a,e);return v(t),n} +crypto_secretbox_open_detached = function st(e,t,a,_,n){var s=[];i(n);var c=u(e=m(s,e,"ciphertext")),o=e.length;s.push(c),t=m(s,t,"mac");var h,y=0|r._crypto_secretbox_macbytes();t.length!==y&&b(s,"invalid mac length"),h=u(t),s.push(h),a=m(s,a,"nonce");var d,f=0|r._crypto_secretbox_noncebytes();a.length!==f&&b(s,"invalid nonce length"),d=u(a),s.push(d),_=m(s,_,"key");var E,x=0|r._crypto_secretbox_keybytes();_.length!==x&&b(s,"invalid key length"),E=u(_),s.push(E);var k=new l(0|o),S=k.address;if(s.push(S),0==(0|r._crypto_secretbox_open_detached(S,c,h,o,0,d,E))){var T=p(k,n);return v(s),T}g(s,"wrong secret key for the given ciphertext")} +crypto_secretbox_open_easy = function ct(e,t,a,_){var n=[];i(_),e=m(n,e,"ciphertext");var s,c=r._crypto_secretbox_macbytes(),o=e.length;o>>0;return v([]),t} +randombytes_stir = function zt(e){i(e),r._randombytes_stir()} +randombytes_uniform = function jt(e,t){var a=[];i(t),f(a,e,"upper_bound"),("number"!=typeof e||(0|e)!==e||e<0)&&b(a,"upper_bound must be an unsigned integer");var _=r._randombytes_uniform(e)>>>0;return v(a),_} +sodium_version_string = function qt(){var e=r._sodium_version_string(),t=r.UTF8ToString(e);return v([]),t} +SODIUM_LIBRARY_VERSION_MAJOR = 10 +SODIUM_LIBRARY_VERSION_MINOR = 3 +crypto_aead_chacha20poly1305_ABYTES = 16 +crypto_aead_chacha20poly1305_IETF_ABYTES = 16 +crypto_aead_chacha20poly1305_IETF_KEYBYTES = 32 +crypto_aead_chacha20poly1305_IETF_MESSAGEBYTES_MAX = -17 +crypto_aead_chacha20poly1305_IETF_NPUBBYTES = 12 +crypto_aead_chacha20poly1305_IETF_NSECBYTES = 0 +crypto_aead_chacha20poly1305_KEYBYTES = 32 +crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX = -17 +crypto_aead_chacha20poly1305_NPUBBYTES = 8 +crypto_aead_chacha20poly1305_NSECBYTES = 0 +crypto_aead_chacha20poly1305_ietf_ABYTES = 16 +crypto_aead_chacha20poly1305_ietf_KEYBYTES = 32 +crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX = -17 +crypto_aead_chacha20poly1305_ietf_NPUBBYTES = 12 +crypto_aead_chacha20poly1305_ietf_NSECBYTES = 0 +crypto_aead_xchacha20poly1305_IETF_ABYTES = 16 +crypto_aead_xchacha20poly1305_IETF_KEYBYTES = 32 +crypto_aead_xchacha20poly1305_IETF_MESSAGEBYTES_MAX = -17 +crypto_aead_xchacha20poly1305_IETF_NPUBBYTES = 24 +crypto_aead_xchacha20poly1305_IETF_NSECBYTES = 0 +crypto_aead_xchacha20poly1305_ietf_ABYTES = 16 +crypto_aead_xchacha20poly1305_ietf_KEYBYTES = 32 +crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX = -17 +crypto_aead_xchacha20poly1305_ietf_NPUBBYTES = 24 +crypto_aead_xchacha20poly1305_ietf_NSECBYTES = 0 +crypto_auth_BYTES = 32 +crypto_auth_KEYBYTES = 32 +crypto_auth_hmacsha256_BYTES = 32 +crypto_auth_hmacsha256_KEYBYTES = 32 +crypto_auth_hmacsha512256_BYTES = 32 +crypto_auth_hmacsha512256_KEYBYTES = 32 +crypto_auth_hmacsha512_BYTES = 64 +crypto_auth_hmacsha512_KEYBYTES = 32 +crypto_box_BEFORENMBYTES = 32 +crypto_box_MACBYTES = 16 +crypto_box_MESSAGEBYTES_MAX = -17 +crypto_box_NONCEBYTES = 24 +crypto_box_PUBLICKEYBYTES = 32 +crypto_box_SEALBYTES = 48 +crypto_box_SECRETKEYBYTES = 32 +crypto_box_SEEDBYTES = 32 +crypto_box_curve25519xchacha20poly1305_BEFORENMBYTES = 32 +crypto_box_curve25519xchacha20poly1305_MACBYTES = 16 +crypto_box_curve25519xchacha20poly1305_MESSAGEBYTES_MAX = -17 +crypto_box_curve25519xchacha20poly1305_NONCEBYTES = 24 +crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES = 32 +crypto_box_curve25519xchacha20poly1305_SEALBYTES = 48 +crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES = 32 +crypto_box_curve25519xchacha20poly1305_SEEDBYTES = 32 +crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32 +crypto_box_curve25519xsalsa20poly1305_MACBYTES = 16 +crypto_box_curve25519xsalsa20poly1305_MESSAGEBYTES_MAX = -17 +crypto_box_curve25519xsalsa20poly1305_NONCEBYTES = 24 +crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32 +crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32 +crypto_box_curve25519xsalsa20poly1305_SEEDBYTES = 32 +crypto_core_ed25519_BYTES = 32 +crypto_core_ed25519_HASHBYTES = 64 +crypto_core_ed25519_NONREDUCEDSCALARBYTES = 64 +crypto_core_ed25519_SCALARBYTES = 32 +crypto_core_ed25519_UNIFORMBYTES = 32 +crypto_core_hchacha20_CONSTBYTES = 16 +crypto_core_hchacha20_INPUTBYTES = 16 +crypto_core_hchacha20_KEYBYTES = 32 +crypto_core_hchacha20_OUTPUTBYTES = 32 +crypto_core_hsalsa20_CONSTBYTES = 16 +crypto_core_hsalsa20_INPUTBYTES = 16 +crypto_core_hsalsa20_KEYBYTES = 32 +crypto_core_hsalsa20_OUTPUTBYTES = 32 +crypto_core_ristretto255_BYTES = 32 +crypto_core_ristretto255_HASHBYTES = 64 +crypto_core_ristretto255_NONREDUCEDSCALARBYTES = 64 +crypto_core_ristretto255_SCALARBYTES = 32 +crypto_core_salsa2012_CONSTBYTES = 16 +crypto_core_salsa2012_INPUTBYTES = 16 +crypto_core_salsa2012_KEYBYTES = 32 +crypto_core_salsa2012_OUTPUTBYTES = 64 +crypto_core_salsa20_CONSTBYTES = 16 +crypto_core_salsa20_INPUTBYTES = 16 +crypto_core_salsa20_KEYBYTES = 32 +crypto_core_salsa20_OUTPUTBYTES = 64 +crypto_generichash_BYTES = 32 +crypto_generichash_BYTES_MAX = 64 +crypto_generichash_BYTES_MIN = 16 +crypto_generichash_KEYBYTES = 32 +crypto_generichash_KEYBYTES_MAX = 64 +crypto_generichash_KEYBYTES_MIN = 16 +crypto_generichash_blake2b_BYTES = 32 +crypto_generichash_blake2b_BYTES_MAX = 64 +crypto_generichash_blake2b_BYTES_MIN = 16 +crypto_generichash_blake2b_KEYBYTES = 32 +crypto_generichash_blake2b_KEYBYTES_MAX = 64 +crypto_generichash_blake2b_KEYBYTES_MIN = 16 +crypto_generichash_blake2b_PERSONALBYTES = 16 +crypto_generichash_blake2b_SALTBYTES = 16 +crypto_hash_BYTES = 64 +crypto_hash_sha256_BYTES = 32 +crypto_hash_sha512_BYTES = 64 +crypto_kdf_BYTES_MAX = 64 +crypto_kdf_BYTES_MIN = 16 +crypto_kdf_CONTEXTBYTES = 8 +crypto_kdf_KEYBYTES = 32 +crypto_kdf_blake2b_BYTES_MAX = 64 +crypto_kdf_blake2b_BYTES_MIN = 16 +crypto_kdf_blake2b_CONTEXTBYTES = 8 +crypto_kdf_blake2b_KEYBYTES = 32 +crypto_kx_PUBLICKEYBYTES = 32 +crypto_kx_SECRETKEYBYTES = 32 +crypto_kx_SEEDBYTES = 32 +crypto_kx_SESSIONKEYBYTES = 32 +crypto_onetimeauth_BYTES = 16 +crypto_onetimeauth_KEYBYTES = 32 +crypto_onetimeauth_poly1305_BYTES = 16 +crypto_onetimeauth_poly1305_KEYBYTES = 32 +crypto_pwhash_ALG_ARGON2I13 = 1 +crypto_pwhash_ALG_ARGON2ID13 = 2 +crypto_pwhash_ALG_DEFAULT = 2 +crypto_pwhash_BYTES_MAX = -1 +crypto_pwhash_BYTES_MIN = 16 +crypto_pwhash_MEMLIMIT_INTERACTIVE = 67108864 +crypto_pwhash_MEMLIMIT_MAX = -2147483648 +crypto_pwhash_MEMLIMIT_MIN = 8192 +crypto_pwhash_MEMLIMIT_MODERATE = 268435456 +crypto_pwhash_MEMLIMIT_SENSITIVE = 1073741824 +crypto_pwhash_OPSLIMIT_INTERACTIVE = 2 +crypto_pwhash_OPSLIMIT_MAX = -1 +crypto_pwhash_OPSLIMIT_MIN = 1 +crypto_pwhash_OPSLIMIT_MODERATE = 3 +crypto_pwhash_OPSLIMIT_SENSITIVE = 4 +crypto_pwhash_PASSWD_MAX = -1 +crypto_pwhash_PASSWD_MIN = 0 +crypto_pwhash_SALTBYTES = 16 +crypto_pwhash_STRBYTES = 128 +crypto_pwhash_argon2i_BYTES_MAX = -1 +crypto_pwhash_argon2i_BYTES_MIN = 16 +crypto_pwhash_argon2i_SALTBYTES = 16 +crypto_pwhash_argon2i_STRBYTES = 128 +crypto_pwhash_argon2id_BYTES_MAX = -1 +crypto_pwhash_argon2id_BYTES_MIN = 16 +crypto_pwhash_argon2id_SALTBYTES = 16 +crypto_pwhash_argon2id_STRBYTES = 128 +crypto_pwhash_scryptsalsa208sha256_BYTES_MAX = -1 +crypto_pwhash_scryptsalsa208sha256_BYTES_MIN = 16 +crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE = 16777216 +crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX = -1 +crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN = 16777216 +crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE = 1073741824 +crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE = 524288 +crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX = -1 +crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN = 32768 +crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE = 33554432 +crypto_pwhash_scryptsalsa208sha256_SALTBYTES = 32 +crypto_pwhash_scryptsalsa208sha256_STRBYTES = 102 +crypto_scalarmult_BYTES = 32 +crypto_scalarmult_SCALARBYTES = 32 +crypto_scalarmult_curve25519_BYTES = 32 +crypto_scalarmult_curve25519_SCALARBYTES = 32 +crypto_scalarmult_ed25519_BYTES = 32 +crypto_scalarmult_ed25519_SCALARBYTES = 32 +crypto_scalarmult_ristretto255_BYTES = 32 +crypto_scalarmult_ristretto255_SCALARBYTES = 32 +crypto_secretbox_KEYBYTES = 32 +crypto_secretbox_MACBYTES = 16 +crypto_secretbox_MESSAGEBYTES_MAX = -17 +crypto_secretbox_NONCEBYTES = 24 +crypto_secretbox_xchacha20poly1305_KEYBYTES = 32 +crypto_secretbox_xchacha20poly1305_MACBYTES = 16 +crypto_secretbox_xchacha20poly1305_MESSAGEBYTES_MAX = -17 +crypto_secretbox_xchacha20poly1305_NONCEBYTES = 24 +crypto_secretbox_xsalsa20poly1305_KEYBYTES = 32 +crypto_secretbox_xsalsa20poly1305_MACBYTES = 16 +crypto_secretbox_xsalsa20poly1305_MESSAGEBYTES_MAX = -17 +crypto_secretbox_xsalsa20poly1305_NONCEBYTES = 24 +crypto_secretstream_xchacha20poly1305_ABYTES = 17 +crypto_secretstream_xchacha20poly1305_HEADERBYTES = 24 +crypto_secretstream_xchacha20poly1305_KEYBYTES = 32 +crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX = -18 +crypto_secretstream_xchacha20poly1305_TAG_FINAL = 3 +crypto_secretstream_xchacha20poly1305_TAG_MESSAGE = 0 +crypto_secretstream_xchacha20poly1305_TAG_PUSH = 1 +crypto_secretstream_xchacha20poly1305_TAG_REKEY = 2 +crypto_shorthash_BYTES = 8 +crypto_shorthash_KEYBYTES = 16 +crypto_shorthash_siphash24_BYTES = 8 +crypto_shorthash_siphash24_KEYBYTES = 16 +crypto_shorthash_siphashx24_BYTES = 16 +crypto_shorthash_siphashx24_KEYBYTES = 16 +crypto_sign_BYTES = 64 +crypto_sign_MESSAGEBYTES_MAX = -65 +crypto_sign_PUBLICKEYBYTES = 32 +crypto_sign_SECRETKEYBYTES = 64 +crypto_sign_SEEDBYTES = 32 +crypto_sign_ed25519_BYTES = 64 +crypto_sign_ed25519_MESSAGEBYTES_MAX = -65 +crypto_sign_ed25519_PUBLICKEYBYTES = 32 +crypto_sign_ed25519_SECRETKEYBYTES = 64 +crypto_sign_ed25519_SEEDBYTES = 32 +crypto_stream_KEYBYTES = 32 +crypto_stream_MESSAGEBYTES_MAX = -1 +crypto_stream_NONCEBYTES = 24 +crypto_stream_chacha20_IETF_KEYBYTES = 32 +crypto_stream_chacha20_IETF_MESSAGEBYTES_MAX = -1 +crypto_stream_chacha20_IETF_NONCEBYTES = 12 +crypto_stream_chacha20_KEYBYTES = 32 +crypto_stream_chacha20_MESSAGEBYTES_MAX = -1 +crypto_stream_chacha20_NONCEBYTES = 8 +crypto_stream_chacha20_ietf_KEYBYTES = 32 +crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX = -1 +crypto_stream_chacha20_ietf_NONCEBYTES = 12 +crypto_stream_salsa2012_KEYBYTES = 32 +crypto_stream_salsa2012_MESSAGEBYTES_MAX = -1 +crypto_stream_salsa2012_NONCEBYTES = 8 +crypto_stream_salsa208_KEYBYTES = 32 +crypto_stream_salsa208_MESSAGEBYTES_MAX = -1 +crypto_stream_salsa208_NONCEBYTES = 8 +crypto_stream_salsa20_KEYBYTES = 32 +crypto_stream_salsa20_MESSAGEBYTES_MAX = -1 +crypto_stream_salsa20_NONCEBYTES = 8 +crypto_stream_xchacha20_KEYBYTES = 32 +crypto_stream_xchacha20_MESSAGEBYTES_MAX = -1 +crypto_stream_xchacha20_NONCEBYTES = 24 +crypto_stream_xsalsa20_KEYBYTES = 32 +crypto_stream_xsalsa20_MESSAGEBYTES_MAX = -1 +crypto_stream_xsalsa20_NONCEBYTES = 24 +crypto_verify_16_BYTES = 16 +crypto_verify_32_BYTES = 32 +crypto_verify_64_BYTES = 64 +SODIUM_VERSION_STRING = "1.0.18" +crypto_pwhash_STRPREFIX = "$argon2id$" +crypto_pwhash_scryptsalsa208sha256_STRPREFIX = "$7$" diff --git a/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStreamTest.kt b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStreamTest.kt index 41837de..31a1706 100644 --- a/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStreamTest.kt +++ b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStreamTest.kt @@ -1,76 +1,89 @@ -//package com.ionspin.kotlin.crypto.secretstream -// -//import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint -//import com.ionspin.kotlin.crypto.Initializer -//import com.ionspin.kotlin.crypto.util.encodeToUByteArray -//import com.ionspin.kotlin.crypto.util.testBlocking -//import debug.test.Crypto -//import kotlin.math.exp -//import kotlin.test.Test -//import kotlin.test.assertTrue -// -///** -// * Created by Ugljesa Jovanovic -// * ugljesa.jovanovic@ionspin.com -// * on 15-Aug-2020 -// */ -//class SecretStreamTest { -// @Test -// fun testSecretStream() = testBlocking { -// Initializer.initializeWithCallback { -// assertTrue { -// val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " + -// "only one tip for the future, sunscreen would be it.").encodeToUByteArray() -// -// val additionalData = ubyteArrayOf( -// 0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U -// ) -// val key = ubyteArrayOf( -// 0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U, -// 0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU, -// 0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U, -// 0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU, -// ) -// -// val nonce = ubyteArrayOf( -// 0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U, -// 0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU, -// 0x50U, 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U, -// ) -// -// val expected = ubyteArrayOf( -// 0xbdU, 0x6dU, 0x17U, 0x9dU, 0x3eU, 0x83U, 0xd4U, 0x3bU, -// 0x95U, 0x76U, 0x57U, 0x94U, 0x93U, 0xc0U, 0xe9U, 0x39U, -// 0x57U, 0x2aU, 0x17U, 0x00U, 0x25U, 0x2bU, 0xfaU, 0xccU, -// 0xbeU, 0xd2U, 0x90U, 0x2cU, 0x21U, 0x39U, 0x6cU, 0xbbU, -// 0x73U, 0x1cU, 0x7fU, 0x1bU, 0x0bU, 0x4aU, 0xa6U, 0x44U, -// 0x0bU, 0xf3U, 0xa8U, 0x2fU, 0x4eU, 0xdaU, 0x7eU, 0x39U, -// 0xaeU, 0x64U, 0xc6U, 0x70U, 0x8cU, 0x54U, 0xc2U, 0x16U, -// 0xcbU, 0x96U, 0xb7U, 0x2eU, 0x12U, 0x13U, 0xb4U, 0x52U, -// 0x2fU, 0x8cU, 0x9bU, 0xa4U, 0x0dU, 0xb5U, 0xd9U, 0x45U, -// 0xb1U, 0x1bU, 0x69U, 0xb9U, 0x82U, 0xc1U, 0xbbU, 0x9eU, -// 0x3fU, 0x3fU, 0xacU, 0x2bU, 0xc3U, 0x69U, 0x48U, 0x8fU, -// 0x76U, 0xb2U, 0x38U, 0x35U, 0x65U, 0xd3U, 0xffU, 0xf9U, -// 0x21U, 0xf9U, 0x66U, 0x4cU, 0x97U, 0x63U, 0x7dU, 0xa9U, -// 0x76U, 0x88U, 0x12U, 0xf6U, 0x15U, 0xc6U, 0x8bU, 0x13U, -// 0xb5U, 0x2eU, 0xc0U, 0x87U, 0x59U, 0x24U, 0xc1U, 0xc7U, -// 0x98U, 0x79U, 0x47U, 0xdeU, 0xafU, 0xd8U, 0x78U, 0x0aU, -// 0xcfU, 0x49U -// ) -// message.hexColumsPrint() -// val crypto = Crypto() -// val stateAndHeader = crypto.crypto_secretstream_xchacha20poly1305_init_push(key) -// val encrypted = -// crypto.crypto_secretstream_xchacha20poly1305_push(stateAndHeader.state, message, ubyteArrayOf(), 0U) -// encrypted.hexColumsPrint() -// val decryptState = crypto.crypto_secretstream_xchacha20poly1305_init_pull(stateAndHeader.header, key) -// val decrypted = -// crypto.crypto_secretstream_xchacha20poly1305_pull(decryptState, encrypted, ubyteArrayOf()) -// decrypted.hexColumsPrint() -// decrypted.contentEquals(message) -// -// } -// } -// } -// -//} +package com.ionspin.kotlin.crypto.secretstream + +import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint +import com.ionspin.kotlin.crypto.LibsodiumInitializer +import com.ionspin.kotlin.crypto.util.encodeToUByteArray +import com.ionspin.kotlin.crypto.util.testBlocking +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertTrue + +/** + * Created by Ugljesa Jovanovic + * ugljesa.jovanovic@ionspin.com + * on 15-Aug-2020 + */ +class SecretStreamTest { + + @BeforeTest + fun initialize() = testBlocking { + LibsodiumInitializer.initialize() + } + + @Test + fun testSecretStream() = testBlocking { + val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " + + "only one tip for the future, sunscreen would be it.").encodeToUByteArray() + + val additionalData = ubyteArrayOf( + 0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U + ) + val key = ubyteArrayOf( + 0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U, + 0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU, + 0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U, + 0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU, + ) + + val nonce = ubyteArrayOf( + 0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U, + 0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU, + 0x50U, 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U, + ) + + val expected = ubyteArrayOf( + 0xbdU, 0x6dU, 0x17U, 0x9dU, 0x3eU, 0x83U, 0xd4U, 0x3bU, + 0x95U, 0x76U, 0x57U, 0x94U, 0x93U, 0xc0U, 0xe9U, 0x39U, + 0x57U, 0x2aU, 0x17U, 0x00U, 0x25U, 0x2bU, 0xfaU, 0xccU, + 0xbeU, 0xd2U, 0x90U, 0x2cU, 0x21U, 0x39U, 0x6cU, 0xbbU, + 0x73U, 0x1cU, 0x7fU, 0x1bU, 0x0bU, 0x4aU, 0xa6U, 0x44U, + 0x0bU, 0xf3U, 0xa8U, 0x2fU, 0x4eU, 0xdaU, 0x7eU, 0x39U, + 0xaeU, 0x64U, 0xc6U, 0x70U, 0x8cU, 0x54U, 0xc2U, 0x16U, + 0xcbU, 0x96U, 0xb7U, 0x2eU, 0x12U, 0x13U, 0xb4U, 0x52U, + 0x2fU, 0x8cU, 0x9bU, 0xa4U, 0x0dU, 0xb5U, 0xd9U, 0x45U, + 0xb1U, 0x1bU, 0x69U, 0xb9U, 0x82U, 0xc1U, 0xbbU, 0x9eU, + 0x3fU, 0x3fU, 0xacU, 0x2bU, 0xc3U, 0x69U, 0x48U, 0x8fU, + 0x76U, 0xb2U, 0x38U, 0x35U, 0x65U, 0xd3U, 0xffU, 0xf9U, + 0x21U, 0xf9U, 0x66U, 0x4cU, 0x97U, 0x63U, 0x7dU, 0xa9U, + 0x76U, 0x88U, 0x12U, 0xf6U, 0x15U, 0xc6U, 0x8bU, 0x13U, + 0xb5U, 0x2eU, 0xc0U, 0x87U, 0x59U, 0x24U, 0xc1U, 0xc7U, + 0x98U, 0x79U, 0x47U, 0xdeU, 0xafU, 0xd8U, 0x78U, 0x0aU, + 0xcfU, 0x49U + ) + message.hexColumsPrint() + println("---- init enc ----") + val stateAndHeader = SecretStream.xChaCha20Poly1305InitPush(key) + println("---- encrypt ----") + val encrypted = + SecretStream.xChaCha20Poly1305Push(stateAndHeader.state, message, ubyteArrayOf(), 0U) + encrypted.hexColumsPrint() + println("---- init dec ----") + val decryptState = SecretStream.xChaCha20Poly1305InitPull(key, stateAndHeader.header) + println("---- decrypt ----") + val decrypted = + SecretStream.xChaCha20Poly1305Pull(decryptState.state, encrypted, ubyteArrayOf()) + decrypted.decryptedData.hexColumsPrint() + assertTrue { + decrypted.decryptedData.contentEquals(message) + + } + + + } + + +} + +expect fun modifyState(state: SecretStreamState, forceNonce: UByteArray) + + diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt index cb5c6b6..646e0ad 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt @@ -45,12 +45,12 @@ interface JsSodiumInterface { //XChaCha20Poly1305 //encrypt - fun crypto_secretstream_xchacha20poly1305_init_push(header: Uint8Array) : dynamic + fun crypto_secretstream_xchacha20poly1305_init_push(key: Uint8Array) : dynamic fun crypto_secretstream_xchacha20poly1305_push(state: dynamic, message: Uint8Array, additionalData: Uint8Array, tag: UByte) : Uint8Array //decrypt fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : dynamic - fun crypto_secretstream_xchacha20poly1305_pull(state: dynamic, ciphertext: Uint8Array, additionalData: Uint8Array) : Uint8Array + fun crypto_secretstream_xchacha20poly1305_pull(state: dynamic, ciphertext: Uint8Array, additionalData: Uint8Array) : dynamic //util fun memzero(array: Uint8Array) diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt index 8d9974e..b1daf7e 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt @@ -1,10 +1,16 @@ package com.ionspin.kotlin.crypto.secretstream +import com.ionspin.kotlin.crypto.getSodium +import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray +import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array +import org.khronos.webgl.Uint8Array + actual typealias SecretStreamState = Any actual object SecretStream { actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader { - TODO("not implemented yet") + val state = getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array()) + return SecretStreamStateAndHeader(state.state, (state.header as Uint8Array).toUByteArray()) } actual fun xChaCha20Poly1305Push( @@ -13,14 +19,17 @@ actual object SecretStream { additionalData: UByteArray, tag: UByte ): UByteArray { - TODO("not implemented yet") + return getSodium().crypto_secretstream_xchacha20poly1305_push( + state, message.toUInt8Array(), additionalData.toUInt8Array(), tag + ).toUByteArray() } actual fun xChaCha20Poly1305InitPull( key: UByteArray, header: UByteArray ): SecretStreamStateAndHeader { - TODO("not implemented yet") + val state = getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(), key.toUInt8Array()) + return SecretStreamStateAndHeader(state, header) } actual fun xChaCha20Poly1305Pull( @@ -28,7 +37,11 @@ actual object SecretStream { ciphertext: UByteArray, additionalData: UByteArray ): DecryptedDataAndTag { - TODO("not implemented yet") + val dataAndTag = getSodium().crypto_secretstream_xchacha20poly1305_pull( + state, ciphertext.toUInt8Array(), additionalData.toUInt8Array() + ) + return DecryptedDataAndTag((dataAndTag.message as Uint8Array).toUByteArray(), dataAndTag.tag) + } } diff --git a/multiplatform-crypto-libsodium-bindings/src/jsTest/kotlin/com/ionspin/kotlin/crypto/secretstream/modifyState.kt b/multiplatform-crypto-libsodium-bindings/src/jsTest/kotlin/com/ionspin/kotlin/crypto/secretstream/modifyState.kt new file mode 100644 index 0000000..27130e3 --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/jsTest/kotlin/com/ionspin/kotlin/crypto/secretstream/modifyState.kt @@ -0,0 +1,4 @@ +package com.ionspin.kotlin.crypto.secretstream + +actual fun modifyState(state: SecretStreamState, forceNonce: UByteArray) { +} diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmTest/kotlin/com/ionspin/kotlin/crypto/secretstream/modifyState.kt b/multiplatform-crypto-libsodium-bindings/src/jvmTest/kotlin/com/ionspin/kotlin/crypto/secretstream/modifyState.kt new file mode 100644 index 0000000..46ec50b --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/jvmTest/kotlin/com/ionspin/kotlin/crypto/secretstream/modifyState.kt @@ -0,0 +1,6 @@ +package com.ionspin.kotlin.crypto.secretstream + +actual fun modifyState(state: SecretStreamState, forceNonce: UByteArray) { + state.nonce = forceNonce.sliceArray(12 until 24).asByteArray() + println("Nonce modified ${state.nonce}") +} diff --git a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt index 11e15ba..af9f2e5 100644 --- a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt @@ -1,13 +1,12 @@ package com.ionspin.kotlin.crypto.secretstream import com.ionspin.kotlin.crypto.util.toPtr -import kotlinx.cinterop.UByteVar import kotlinx.cinterop.convert import kotlinx.cinterop.pin import kotlinx.cinterop.pointed import kotlinx.cinterop.ptr import kotlinx.cinterop.reinterpret -import kotlinx.cinterop.toCPointer +import libsodium.crypto_secretstream_xchacha20poly1305_ABYTES import libsodium.crypto_secretstream_xchacha20poly1305_headerbytes import libsodium.crypto_secretstream_xchacha20poly1305_init_pull import libsodium.crypto_secretstream_xchacha20poly1305_init_push @@ -42,23 +41,28 @@ actual object SecretStream { additionalData: UByteArray, tag: UByte ): UByteArray { - val ciphertext = UByteArray(message.size) + val ciphertext = UByteArray(message.size + crypto_secretstream_xchacha20poly1305_ABYTES.toInt()) { 0U } val ciphertextPinned = ciphertext.pin() val messagePinned = message.pin() - val additionalDataPinned = additionalData.pin() + val additionalDataPinned = if (additionalData.isNotEmpty()) { + additionalData.pin() + } else { + null + } crypto_secretstream_xchacha20poly1305_push( state.ptr, ciphertextPinned.toPtr(), null, messagePinned.toPtr(), message.size.convert(), - additionalDataPinned.toPtr(), + additionalDataPinned?.toPtr(), additionalData.size.convert(), tag ) + ciphertextPinned.unpin() messagePinned.unpin() - additionalDataPinned.unpin() + additionalDataPinned?.unpin() return ciphertext } @@ -85,27 +89,33 @@ actual object SecretStream { ciphertext: UByteArray, additionalData: UByteArray ): DecryptedDataAndTag { - val message = UByteArray(ciphertext.size) + val message = UByteArray(ciphertext.size - crypto_secretstream_xchacha20poly1305_ABYTES.toInt()) val messagePinned = message.pin() val ciphertextPinned = ciphertext.pin() - val additionalDataPinned = additionalData.pin() + val additionalDataPinned = if (additionalData.isNotEmpty()) { + additionalData.pin() + } else { + null + } val tag = UByteArray(1) { 0U } val tagPinned = tag.pin() - crypto_secretstream_xchacha20poly1305_pull( + val validTag = crypto_secretstream_xchacha20poly1305_pull( state.ptr, messagePinned.toPtr(), null, tagPinned.toPtr(), ciphertextPinned.toPtr(), ciphertext.size.convert(), - additionalDataPinned.toPtr(), - additionalData.size.convert(), - - ) + additionalDataPinned?.toPtr(), + additionalData.size.convert() + ) ciphertextPinned.unpin() messagePinned.unpin() - additionalDataPinned.unpin() + additionalDataPinned?.unpin() tagPinned.unpin() + if (validTag != 0) { + throw RuntimeException("Invalid tag") + } return DecryptedDataAndTag(message, tag[0]) } diff --git a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/util/ConversionUtil.kt b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/util/ConversionUtil.kt index 7a0d892..8964c2b 100644 --- a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/util/ConversionUtil.kt +++ b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/util/ConversionUtil.kt @@ -4,10 +4,11 @@ import kotlinx.cinterop.CPointer import kotlinx.cinterop.Pinned import kotlinx.cinterop.UByteVar import kotlinx.cinterop.addressOf +import kotlinx.cinterop.toCPointer /** * Created by Ugljesa Jovanovic * ugljesa.jovanovic@ionspin.com * on 27-Aug-2020 */ -fun Pinned.toPtr() : CPointer = addressOf(0) +fun Pinned.toPtr() : CPointer? = addressOf(0) diff --git a/multiplatform-crypto-libsodium-bindings/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/secretstream/modifyState.kt b/multiplatform-crypto-libsodium-bindings/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/secretstream/modifyState.kt new file mode 100644 index 0000000..27130e3 --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/secretstream/modifyState.kt @@ -0,0 +1,4 @@ +package com.ionspin.kotlin.crypto.secretstream + +actual fun modifyState(state: SecretStreamState, forceNonce: UByteArray) { +}