├── .gitignore ├── Public API Docs ├── img │ ├── gh.png │ ├── carat.png │ └── dash.png ├── docsets │ ├── .tgz │ └── .docset │ │ └── Contents │ │ ├── Resources │ │ ├── docSet.dsidx │ │ └── Documents │ │ │ ├── img │ │ │ ├── gh.png │ │ │ ├── carat.png │ │ │ └── dash.png │ │ │ └── js │ │ │ └── jazzy.js │ │ └── Info.plist └── js │ └── jazzy.js ├── libsignal-protocol-swift ├── libsignal-protocol-c │ ├── curve25519 │ │ ├── ed25519 │ │ │ ├── d.h │ │ │ ├── d2.h │ │ │ ├── sqrtm1.h │ │ │ ├── ge_p2_0.c │ │ │ ├── nacl_includes │ │ │ │ ├── crypto_int32.h │ │ │ │ ├── crypto_int64.h │ │ │ │ ├── crypto_uint32.h │ │ │ │ ├── crypto_uint64.h │ │ │ │ ├── crypto_verify_32.h │ │ │ │ ├── crypto_sign.h │ │ │ │ └── crypto_sign_edwards25519sha512batch.h │ │ │ ├── ge_p3_0.c │ │ │ ├── ge_precomp_0.c │ │ │ ├── additions │ │ │ │ ├── sc_clamp.c │ │ │ │ ├── compare.h │ │ │ │ ├── crypto_hash_sha512.h │ │ │ │ ├── zeroize.h │ │ │ │ ├── fe_isequal.c │ │ │ │ ├── ge_neg.c │ │ │ │ ├── utility.h │ │ │ │ ├── fe_isreduced.c │ │ │ │ ├── generalized │ │ │ │ │ ├── ge_p3_add.c │ │ │ │ │ ├── point_isreduced.c │ │ │ │ │ ├── gen_crypto_additions.h │ │ │ │ │ ├── sc_isreduced.c │ │ │ │ │ ├── gen_constants.h │ │ │ │ │ ├── gen_veddsa.h │ │ │ │ │ ├── gen_labelset.h │ │ │ │ │ ├── gen_x.h │ │ │ │ │ └── gen_eddsa.h │ │ │ │ ├── zeroize.c │ │ │ │ ├── ge_isneutral.c │ │ │ │ ├── fe_montx_to_edy.c │ │ │ │ ├── ge_scalarmult_cofactor.c │ │ │ │ ├── keygen.h │ │ │ │ ├── fe_mont_rhs.c │ │ │ │ ├── ge_p3_to_montx.c │ │ │ │ ├── sc_cmov.c │ │ │ │ ├── xeddsa.h │ │ │ │ ├── keygen.c │ │ │ │ ├── utility.c │ │ │ │ ├── curve_sigs.h │ │ │ │ ├── compare.c │ │ │ │ ├── sc_neg.c │ │ │ │ ├── open_modified.c │ │ │ │ ├── crypto_additions.h │ │ │ │ ├── sign_modified.c │ │ │ │ ├── fe_sqrt.c │ │ │ │ ├── ge_montx_to_p3.c │ │ │ │ ├── elligator.c │ │ │ │ ├── xeddsa.c │ │ │ │ ├── curve_sigs.c │ │ │ │ └── ge_scalarmult.c │ │ │ ├── ge_p2_dbl.c │ │ │ ├── ge_add.c │ │ │ ├── ge_madd.c │ │ │ ├── ge_msub.c │ │ │ ├── ge_sub.c │ │ │ ├── ge_p3_dbl.c │ │ │ ├── fe_pow22523.c │ │ │ ├── fe_invert.c │ │ │ ├── ge_p3_to_p2.c │ │ │ ├── ge_p1p1_to_p2.c │ │ │ ├── fe_0.c │ │ │ ├── fe_1.c │ │ │ ├── ge_p1p1_to_p3.c │ │ │ ├── ge_tobytes.c │ │ │ ├── ge_p3_tobytes.c │ │ │ ├── ge_p3_to_cached.c │ │ │ ├── fe_isnegative.c │ │ │ ├── sc.h │ │ │ ├── fe_isnonzero.c │ │ │ ├── fe_copy.c │ │ │ ├── tests │ │ │ │ ├── internal_slow_tests.h │ │ │ │ └── internal_fast_tests.h │ │ │ ├── sign.c │ │ │ ├── fe_neg.c │ │ │ ├── ge_frombytes.c │ │ │ ├── open.c │ │ │ ├── fe_add.c │ │ │ ├── fe_sub.c │ │ │ ├── fe_cmov.c │ │ │ ├── ge_p2_dbl.h │ │ │ ├── nacl_sha512 │ │ │ │ └── hash.c │ │ │ ├── fe.h │ │ │ ├── ge_madd.h │ │ │ ├── ge_msub.h │ │ │ ├── ge_add.h │ │ │ ├── ge_sub.h │ │ │ ├── base2.h │ │ │ ├── fe_frombytes.c │ │ │ ├── ge_double_scalarmult.c │ │ │ ├── ge_scalarmult_base.c │ │ │ ├── ge.h │ │ │ └── fe_tobytes.c │ │ └── curve25519-donna.h │ ├── signal_utarray.h │ ├── hkdf.h │ ├── session_builder_internal.h │ ├── sender_key.h │ ├── sender_key_record.h │ ├── sender_key_state.h │ ├── vpool.h │ ├── session_builder.h │ ├── session_record.h │ ├── group_session_builder.h │ ├── device_consistency.h │ ├── session_pre_key.h │ ├── signal_protocol_internal.h │ ├── signal_protocol_types.h │ └── group_cipher.h ├── Setup │ ├── setup.h │ └── setup.c ├── SignalProtocol.h ├── Additional │ └── Info.plist ├── StoreWrappers │ └── KeyStoreWrapper.swift ├── Stores │ ├── SenderKeyStore.swift │ ├── PreKeyStore.swift │ ├── SignedPreKeyStore.swift │ ├── IdentityKeyStore.swift │ ├── SessionStore.swift │ └── SignalStore.swift ├── Misc │ ├── DataExtensions.swift │ ├── SignalAddress.swift │ ├── SignalError.swift │ ├── SignalSenderKeyName.swift │ ├── KeyPair.swift │ └── CiphertextMessage.swift └── Sessions │ ├── SessionBuilder.swift │ ├── SessionPreKey.swift │ ├── SessionPreKeyBundle.swift │ └── SessionSignedPreKey.swift ├── libsignal-protocol-swift.xcodeproj ├── xcuserdata │ └── User.xcuserdatad │ │ ├── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist │ │ └── xcschemes │ │ └── xcschememanagement.plist └── project.xcworkspace │ └── contents.xcworkspacedata ├── libsignal-protocol-swiftTests ├── Info.plist ├── TestImplementation │ ├── TestPreKeyStore.swift │ ├── TestSignedPrekeyStore.swift │ ├── TestSenderKeyStore.swift │ ├── TestIdentityStore.swift │ └── TestSessionStore.swift ├── GroupCipherTests.swift ├── TestHelper.swift └── FingerprintTests.swift └── libsignal-protocol-swift.podspec /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | build -------------------------------------------------------------------------------- /Public API Docs/img/gh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/christophhagen/libsignal-protocol-swift/HEAD/Public API Docs/img/gh.png -------------------------------------------------------------------------------- /Public API Docs/docsets/.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/christophhagen/libsignal-protocol-swift/HEAD/Public API Docs/docsets/.tgz -------------------------------------------------------------------------------- /Public API Docs/img/carat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/christophhagen/libsignal-protocol-swift/HEAD/Public API Docs/img/carat.png -------------------------------------------------------------------------------- /Public API Docs/img/dash.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/christophhagen/libsignal-protocol-swift/HEAD/Public API Docs/img/dash.png -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/d.h: -------------------------------------------------------------------------------- 1 | -10913610,13857413,-15372611,6949391,114729,-8787816,-6275908,-3247719,-18696448,-12055116 2 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/d2.h: -------------------------------------------------------------------------------- 1 | -21827239,-5839606,-30745221,13898782,229458,15978800,-12551817,-6495438,29715968,9444199 2 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/sqrtm1.h: -------------------------------------------------------------------------------- 1 | -32595792,-7943725,9377950,3500415,12389472,-272473,-25146209,-2005654,326686,11406482 2 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_p2_0.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | void ge_p2_0(ge_p2 *h) 4 | { 5 | fe_0(h->X); 6 | fe_1(h->Y); 7 | fe_1(h->Z); 8 | } 9 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/nacl_includes/crypto_int32.h: -------------------------------------------------------------------------------- 1 | #ifndef crypto_int32_h 2 | #define crypto_int32_h 3 | 4 | typedef int crypto_int32; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /Public API Docs/docsets/.docset/Contents/Resources/docSet.dsidx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/christophhagen/libsignal-protocol-swift/HEAD/Public API Docs/docsets/.docset/Contents/Resources/docSet.dsidx -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/nacl_includes/crypto_int64.h: -------------------------------------------------------------------------------- 1 | #ifndef crypto_int64_h 2 | #define crypto_int64_h 3 | 4 | typedef long long crypto_int64; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /libsignal-protocol-swift.xcodeproj/xcuserdata/User.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/nacl_includes/crypto_uint32.h: -------------------------------------------------------------------------------- 1 | #ifndef crypto_uint32_h 2 | #define crypto_uint32_h 3 | 4 | typedef unsigned int crypto_uint32; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /Public API Docs/docsets/.docset/Contents/Resources/Documents/img/gh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/christophhagen/libsignal-protocol-swift/HEAD/Public API Docs/docsets/.docset/Contents/Resources/Documents/img/gh.png -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/nacl_includes/crypto_uint64.h: -------------------------------------------------------------------------------- 1 | #ifndef crypto_uint64_h 2 | #define crypto_uint64_h 3 | 4 | typedef unsigned long long crypto_uint64; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /Public API Docs/docsets/.docset/Contents/Resources/Documents/img/carat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/christophhagen/libsignal-protocol-swift/HEAD/Public API Docs/docsets/.docset/Contents/Resources/Documents/img/carat.png -------------------------------------------------------------------------------- /Public API Docs/docsets/.docset/Contents/Resources/Documents/img/dash.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/christophhagen/libsignal-protocol-swift/HEAD/Public API Docs/docsets/.docset/Contents/Resources/Documents/img/dash.png -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_p3_0.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | void ge_p3_0(ge_p3 *h) 4 | { 5 | fe_0(h->X); 6 | fe_1(h->Y); 7 | fe_1(h->Z); 8 | fe_0(h->T); 9 | } 10 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_precomp_0.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | void ge_precomp_0(ge_precomp *h) 4 | { 5 | fe_1(h->yplusx); 6 | fe_1(h->yminusx); 7 | fe_0(h->xy2d); 8 | } 9 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/sc_clamp.c: -------------------------------------------------------------------------------- 1 | #include "crypto_additions.h" 2 | 3 | void sc_clamp(unsigned char* a) 4 | { 5 | a[0] &= 248; 6 | a[31] &= 127; 7 | a[31] |= 64; 8 | } 9 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/curve25519-donna.h: -------------------------------------------------------------------------------- 1 | #ifndef CURVE25519_DONNA_H 2 | #define CURVE25519_DONNA_H 3 | 4 | extern int curve25519_donna(uint8_t *, const uint8_t *, const uint8_t *); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/compare.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMPARE_H__ 2 | #define __COMPARE_H__ 3 | 4 | int crypto_verify_32_ref(const unsigned char *b1, const unsigned char *b2); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_p2_dbl.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | /* 4 | r = 2 * p 5 | */ 6 | 7 | void ge_p2_dbl(ge_p1p1 *r,const ge_p2 *p) 8 | { 9 | fe t0; 10 | #include "ge_p2_dbl.h" 11 | } 12 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_add.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | /* 4 | r = p + q 5 | */ 6 | 7 | void ge_add(ge_p1p1 *r,const ge_p3 *p,const ge_cached *q) 8 | { 9 | fe t0; 10 | #include "ge_add.h" 11 | } 12 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_madd.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | /* 4 | r = p + q 5 | */ 6 | 7 | void ge_madd(ge_p1p1 *r,const ge_p3 *p,const ge_precomp *q) 8 | { 9 | fe t0; 10 | #include "ge_madd.h" 11 | } 12 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_msub.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | /* 4 | r = p - q 5 | */ 6 | 7 | void ge_msub(ge_p1p1 *r,const ge_p3 *p,const ge_precomp *q) 8 | { 9 | fe t0; 10 | #include "ge_msub.h" 11 | } 12 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_sub.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | /* 4 | r = p - q 5 | */ 6 | 7 | void ge_sub(ge_p1p1 *r,const ge_p3 *p,const ge_cached *q) 8 | { 9 | fe t0; 10 | #include "ge_sub.h" 11 | } 12 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_p3_dbl.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | /* 4 | r = 2 * p 5 | */ 6 | 7 | void ge_p3_dbl(ge_p1p1 *r,const ge_p3 *p) 8 | { 9 | ge_p2 q; 10 | ge_p3_to_p2(&q,p); 11 | ge_p2_dbl(r,&q); 12 | } 13 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_pow22523.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | void fe_pow22523(fe out,const fe z) 4 | { 5 | fe t0; 6 | fe t1; 7 | fe t2; 8 | int i; 9 | 10 | #include "pow22523.h" 11 | 12 | return; 13 | } 14 | -------------------------------------------------------------------------------- /libsignal-protocol-swift.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/crypto_hash_sha512.h: -------------------------------------------------------------------------------- 1 | #ifndef crypto_hash_sha512_H 2 | #define crypto_hash_sha512_H 3 | 4 | extern int crypto_hash_sha512(unsigned char *,const unsigned char *,unsigned long long); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_invert.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | void fe_invert(fe out,const fe z) 4 | { 5 | fe t0; 6 | fe t1; 7 | fe t2; 8 | fe t3; 9 | int i; 10 | 11 | #include "pow225521.h" 12 | 13 | return; 14 | } 15 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_p3_to_p2.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | /* 4 | r = p 5 | */ 6 | 7 | extern void ge_p3_to_p2(ge_p2 *r,const ge_p3 *p) 8 | { 9 | fe_copy(r->X,p->X); 10 | fe_copy(r->Y,p->Y); 11 | fe_copy(r->Z,p->Z); 12 | } 13 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_p1p1_to_p2.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | /* 4 | r = p 5 | */ 6 | 7 | extern void ge_p1p1_to_p2(ge_p2 *r,const ge_p1p1 *p) 8 | { 9 | fe_mul(r->X,p->X,p->T); 10 | fe_mul(r->Y,p->Y,p->Z); 11 | fe_mul(r->Z,p->Z,p->T); 12 | } 13 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/zeroize.h: -------------------------------------------------------------------------------- 1 | #ifndef __ZEROIZE_H__ 2 | #define __ZEROIZE_H__ 3 | 4 | #include 5 | 6 | #define ZEROIZE_STACK_SIZE 1024 7 | 8 | void zeroize(unsigned char* b, size_t len); 9 | 10 | void zeroize_stack(void); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_0.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | /* 4 | h = 0 5 | */ 6 | 7 | void fe_0(fe h) 8 | { 9 | h[0] = 0; 10 | h[1] = 0; 11 | h[2] = 0; 12 | h[3] = 0; 13 | h[4] = 0; 14 | h[5] = 0; 15 | h[6] = 0; 16 | h[7] = 0; 17 | h[8] = 0; 18 | h[9] = 0; 19 | } 20 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_1.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | /* 4 | h = 1 5 | */ 6 | 7 | void fe_1(fe h) 8 | { 9 | h[0] = 1; 10 | h[1] = 0; 11 | h[2] = 0; 12 | h[3] = 0; 13 | h[4] = 0; 14 | h[5] = 0; 15 | h[6] = 0; 16 | h[7] = 0; 17 | h[8] = 0; 18 | h[9] = 0; 19 | } 20 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_p1p1_to_p3.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | /* 4 | r = p 5 | */ 6 | 7 | extern void ge_p1p1_to_p3(ge_p3 *r,const ge_p1p1 *p) 8 | { 9 | fe_mul(r->X,p->X,p->T); 10 | fe_mul(r->Y,p->Y,p->Z); 11 | fe_mul(r->Z,p->Z,p->T); 12 | fe_mul(r->T,p->X,p->Y); 13 | } 14 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/fe_isequal.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | #include "crypto_verify_32.h" 3 | 4 | /* 5 | return 1 if f == g 6 | return 0 if f != g 7 | */ 8 | 9 | int fe_isequal(const fe f, const fe g) 10 | { 11 | fe h; 12 | fe_sub(h, f, g); 13 | return 1 ^ (1 & (fe_isnonzero(h) >> 8)); 14 | } 15 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/ge_neg.c: -------------------------------------------------------------------------------- 1 | #include "crypto_additions.h" 2 | #include "ge.h" 3 | 4 | /* 5 | return r = -p 6 | */ 7 | 8 | 9 | void ge_neg(ge_p3* r, const ge_p3 *p) 10 | { 11 | fe_neg(r->X, p->X); 12 | fe_copy(r->Y, p->Y); 13 | fe_copy(r->Z, p->Z); 14 | fe_neg(r->T, p->T); 15 | } 16 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_tobytes.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | void ge_tobytes(unsigned char *s,const ge_p2 *h) 4 | { 5 | fe recip; 6 | fe x; 7 | fe y; 8 | 9 | fe_invert(recip,h->Z); 10 | fe_mul(x,h->X,recip); 11 | fe_mul(y,h->Y,recip); 12 | fe_tobytes(s,y); 13 | s[31] ^= fe_isnegative(x) << 7; 14 | } 15 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_p3_tobytes.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | void ge_p3_tobytes(unsigned char *s,const ge_p3 *h) 4 | { 5 | fe recip; 6 | fe x; 7 | fe y; 8 | 9 | fe_invert(recip,h->Z); 10 | fe_mul(x,h->X,recip); 11 | fe_mul(y,h->Y,recip); 12 | fe_tobytes(s,y); 13 | s[31] ^= fe_isnegative(x) << 7; 14 | } 15 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/utility.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __UTILITY_H__ 3 | #define __UTILITY_H__ 4 | 5 | #include "fe.h" 6 | 7 | void print_vector(const char* name, const unsigned char* v); 8 | void print_bytes(const char* name, const unsigned char* v, int numbytes); 9 | void print_fe(const char* name, const fe in); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/fe_isreduced.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | #include "crypto_verify_32.h" 3 | 4 | int fe_isreduced(const unsigned char* s) 5 | { 6 | fe f; 7 | unsigned char strict[32]; 8 | 9 | fe_frombytes(f, s); 10 | fe_tobytes(strict, f); 11 | if (crypto_verify_32(strict, s) != 0) 12 | return 0; 13 | return 1; 14 | } 15 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/generalized/ge_p3_add.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | /* 4 | r = p + q 5 | */ 6 | 7 | void ge_p3_add(ge_p3 *r, const ge_p3 *p, const ge_p3 *q) 8 | { 9 | ge_cached p_cached; 10 | ge_p1p1 r_p1p1; 11 | 12 | ge_p3_to_cached(&p_cached, p); 13 | ge_add(&r_p1p1, q, &p_cached); 14 | ge_p1p1_to_p3(r, &r_p1p1); 15 | } 16 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/generalized/point_isreduced.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "fe.h" 3 | #include "crypto_additions.h" 4 | 5 | int point_isreduced(const unsigned char* p) 6 | { 7 | unsigned char strict[32]; 8 | 9 | memmove(strict, p, 32); 10 | strict[31] &= 0x7F; /* mask off sign bit */ 11 | return fe_isreduced(strict); 12 | } 13 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_p3_to_cached.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | /* 4 | r = p 5 | */ 6 | 7 | static const fe d2 = { 8 | #include "d2.h" 9 | } ; 10 | 11 | extern void ge_p3_to_cached(ge_cached *r,const ge_p3 *p) 12 | { 13 | fe_add(r->YplusX,p->Y,p->X); 14 | fe_sub(r->YminusX,p->Y,p->X); 15 | fe_copy(r->Z,p->Z); 16 | fe_mul(r->T2d,p->T,d2); 17 | } 18 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_isnegative.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | /* 4 | return 1 if f is in {1,3,5,...,q-2} 5 | return 0 if f is in {0,2,4,...,q-1} 6 | 7 | Preconditions: 8 | |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. 9 | */ 10 | 11 | int fe_isnegative(const fe f) 12 | { 13 | unsigned char s[32]; 14 | fe_tobytes(s,f); 15 | return s[0] & 1; 16 | } 17 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Setup/setup.h: -------------------------------------------------------------------------------- 1 | // 2 | // setup.h 3 | // libsignal-protocol-swift iOS 4 | // 5 | // Created by User on 15.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | #ifndef setup_h 10 | #define setup_h 11 | 12 | void *signal_setup(void); 13 | 14 | void signal_destroy(void *global_context); 15 | 16 | int setup_crypto_provider(void *context); 17 | 18 | #endif /* setup_h */ 19 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/zeroize.c: -------------------------------------------------------------------------------- 1 | #include "zeroize.h" 2 | 3 | void zeroize(unsigned char* b, size_t len) 4 | { 5 | size_t count = 0; 6 | volatile unsigned char *p = b; 7 | 8 | for (count = 0; count < len; count++) 9 | p[count] = 0; 10 | } 11 | 12 | void zeroize_stack() 13 | { 14 | unsigned char m[ZEROIZE_STACK_SIZE]; 15 | zeroize(m, ZEROIZE_STACK_SIZE); 16 | } 17 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/ge_isneutral.c: -------------------------------------------------------------------------------- 1 | #include "crypto_additions.h" 2 | #include "ge.h" 3 | 4 | /* 5 | return 1 if p is the neutral point 6 | return 0 otherwise 7 | */ 8 | 9 | int ge_isneutral(const ge_p3 *p) 10 | { 11 | fe zero; 12 | fe_0(zero); 13 | 14 | /* Check if p == neutral element == (0, 1) */ 15 | return (fe_isequal(p->X, zero) & fe_isequal(p->Y, p->Z)); 16 | } 17 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/generalized/gen_crypto_additions.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __GEN_CRYPTO_ADDITIONS__ 3 | #define __GEN_CRYPTO_ADDITIONS__ 4 | 5 | #include "crypto_uint32.h" 6 | #include "fe.h" 7 | #include "ge.h" 8 | 9 | int sc_isreduced(const unsigned char* s); 10 | 11 | int point_isreduced(const unsigned char* p); 12 | 13 | void ge_p3_add(ge_p3 *r, const ge_p3 *p, const ge_p3 *q); 14 | 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/fe_montx_to_edy.c: -------------------------------------------------------------------------------- 1 | 2 | #include "fe.h" 3 | #include "crypto_additions.h" 4 | 5 | void fe_montx_to_edy(fe y, const fe u) 6 | { 7 | /* 8 | y = (u - 1) / (u + 1) 9 | 10 | NOTE: u=-1 is converted to y=0 since fe_invert is mod-exp 11 | */ 12 | fe one, um1, up1; 13 | 14 | fe_1(one); 15 | fe_sub(um1, u, one); 16 | fe_add(up1, u, one); 17 | fe_invert(up1, up1); 18 | fe_mul(y, um1, up1); 19 | } 20 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/generalized/sc_isreduced.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "fe.h" 3 | #include "sc.h" 4 | #include "crypto_additions.h" 5 | #include "crypto_verify_32.h" 6 | 7 | int sc_isreduced(const unsigned char* s) 8 | { 9 | unsigned char strict[64]; 10 | 11 | memset(strict, 0, 64); 12 | memmove(strict, s, 32); 13 | sc_reduce(strict); 14 | if (crypto_verify_32(strict, s) != 0) 15 | return 0; 16 | return 1; 17 | } 18 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/signal_utarray.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGNAL_UTARRAY_H 2 | #define SIGNAL_UTARRAY_H 3 | 4 | #include "signal_protocol.h" 5 | 6 | #define oom() do { \ 7 | result = SG_ERR_NOMEM; \ 8 | goto complete; \ 9 | } while(0) 10 | 11 | #include "utarray.h" 12 | 13 | #endif /* SIGNAL_UTARRAY_H */ 14 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/ge_scalarmult_cofactor.c: -------------------------------------------------------------------------------- 1 | #include "crypto_additions.h" 2 | #include "ge.h" 3 | 4 | /* 5 | return 8 * p 6 | */ 7 | 8 | void ge_scalarmult_cofactor(ge_p3 *q, const ge_p3 *p) 9 | { 10 | ge_p1p1 p1p1; 11 | ge_p2 p2; 12 | 13 | ge_p3_dbl(&p1p1, p); 14 | ge_p1p1_to_p2(&p2, &p1p1); 15 | 16 | ge_p2_dbl(&p1p1, &p2); 17 | ge_p1p1_to_p2(&p2, &p1p1); 18 | 19 | ge_p2_dbl(&p1p1, &p2); 20 | ge_p1p1_to_p3(q, &p1p1); 21 | } 22 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/sc.h: -------------------------------------------------------------------------------- 1 | #ifndef SC_H 2 | #define SC_H 3 | 4 | /* 5 | The set of scalars is \Z/l 6 | where l = 2^252 + 27742317777372353535851937790883648493. 7 | */ 8 | 9 | #define sc_reduce crypto_sign_ed25519_ref10_sc_reduce 10 | #define sc_muladd crypto_sign_ed25519_ref10_sc_muladd 11 | 12 | extern void sc_reduce(unsigned char *); 13 | extern void sc_muladd(unsigned char *,const unsigned char *,const unsigned char *,const unsigned char *); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/keygen.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __KEYGEN_H__ 3 | #define __KEYGEN_H__ 4 | 5 | /* Sets and clears bits to make a random 32 bytes into a private key */ 6 | void sc_clamp(unsigned char* a); 7 | 8 | /* The private key should be 32 random bytes "clamped" by sc_clamp() */ 9 | void curve25519_keygen(unsigned char* curve25519_pubkey_out, /* 32 bytes */ 10 | const unsigned char* curve25519_privkey_in); /* 32 bytes */ 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/SignalProtocol.h: -------------------------------------------------------------------------------- 1 | // 2 | // libsignal_protocol_swift.h 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 14.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | @import Foundation; 10 | 11 | //! Project version number for libsignal_protocol_swift. 12 | FOUNDATION_EXPORT double libsignal_protocol_swiftVersionNumber; 13 | 14 | //! Project version string for libsignal_protocol_swift. 15 | FOUNDATION_EXPORT const unsigned char libsignal_protocol_swiftVersionString[]; 16 | 17 | #include "setup.h" 18 | 19 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/generalized/gen_constants.h: -------------------------------------------------------------------------------- 1 | #ifndef _GEN_CONSTANTS_H__ 2 | #define _GEN_CONSTANTS_H__ 3 | 4 | #define LABELSETMAXLEN 512 5 | #define LABELMAXLEN 128 6 | #define BUFLEN 1024 7 | #define BLOCKLEN 128 /* SHA512 */ 8 | #define HASHLEN 64 /* SHA512 */ 9 | #define POINTLEN 32 10 | #define SCALARLEN 32 11 | #define RANDLEN 32 12 | #define SIGNATURELEN 64 13 | #define VRFSIGNATURELEN 96 14 | #define VRFOUTPUTLEN 32 15 | #define MSTART 2048 16 | #define MSGMAXLEN 1048576 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/fe_mont_rhs.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | void fe_mont_rhs(fe v2, fe u) { 4 | fe A, one; 5 | fe u2, Au, inner; 6 | 7 | fe_1(one); 8 | fe_0(A); 9 | A[0] = 486662; /* A = 486662 */ 10 | 11 | fe_sq(u2, u); /* u^2 */ 12 | fe_mul(Au, A, u); /* Au */ 13 | fe_add(inner, u2, Au); /* u^2 + Au */ 14 | fe_add(inner, inner, one); /* u^2 + Au + 1 */ 15 | fe_mul(v2, u, inner); /* u(u^2 + Au + 1) */ 16 | } 17 | 18 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/ge_p3_to_montx.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | #include "crypto_additions.h" 3 | 4 | void ge_p3_to_montx(fe u, const ge_p3 *ed) 5 | { 6 | /* 7 | u = (y + 1) / (1 - y) 8 | or 9 | u = (y + z) / (z - y) 10 | 11 | NOTE: y=1 is converted to u=0 since fe_invert is mod-exp 12 | */ 13 | 14 | fe y_plus_one, one_minus_y, inv_one_minus_y; 15 | 16 | fe_add(y_plus_one, ed->Y, ed->Z); 17 | fe_sub(one_minus_y, ed->Z, ed->Y); 18 | fe_invert(inv_one_minus_y, one_minus_y); 19 | fe_mul(u, y_plus_one, inv_one_minus_y); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/sc_cmov.c: -------------------------------------------------------------------------------- 1 | #include "crypto_additions.h" 2 | 3 | /* 4 | Replace (f,g) with (g,g) if b == 1; 5 | replace (f,g) with (f,g) if b == 0. 6 | 7 | Preconditions: b in {0,1}. 8 | */ 9 | 10 | void sc_cmov(unsigned char* f, const unsigned char* g, unsigned char b) 11 | { 12 | int count=32; 13 | unsigned char x[32]; 14 | for (count=0; count < 32; count++) 15 | x[count] = f[count] ^ g[count]; 16 | b = -b; 17 | for (count=0; count < 32; count++) 18 | x[count] &= b; 19 | for (count=0; count < 32; count++) 20 | f[count] = f[count] ^ x[count]; 21 | } 22 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_isnonzero.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | #include "crypto_verify_32.h" 3 | 4 | /* 5 | return nonzero if f == 0 6 | return 0 if f != 0 7 | 8 | Preconditions: 9 | |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. 10 | */ 11 | 12 | /* TREVOR'S COMMENT 13 | * 14 | * I think the above comment is wrong. Instead: 15 | * 16 | * return 0 if f == 0 17 | * return -1 if f != 0 18 | * 19 | * */ 20 | 21 | static const unsigned char zero[32]; 22 | 23 | int fe_isnonzero(const fe f) 24 | { 25 | unsigned char s[32]; 26 | fe_tobytes(s,f); 27 | return crypto_verify_32(s,zero); 28 | } 29 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_copy.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | /* 4 | h = f 5 | */ 6 | 7 | void fe_copy(fe h,const fe f) 8 | { 9 | crypto_int32 f0 = f[0]; 10 | crypto_int32 f1 = f[1]; 11 | crypto_int32 f2 = f[2]; 12 | crypto_int32 f3 = f[3]; 13 | crypto_int32 f4 = f[4]; 14 | crypto_int32 f5 = f[5]; 15 | crypto_int32 f6 = f[6]; 16 | crypto_int32 f7 = f[7]; 17 | crypto_int32 f8 = f[8]; 18 | crypto_int32 f9 = f[9]; 19 | h[0] = f0; 20 | h[1] = f1; 21 | h[2] = f2; 22 | h[3] = f3; 23 | h[4] = f4; 24 | h[5] = f5; 25 | h[6] = f6; 26 | h[7] = f7; 27 | h[8] = f8; 28 | h[9] = f9; 29 | } 30 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/tests/internal_slow_tests.h: -------------------------------------------------------------------------------- 1 | #ifndef __INTERNAL_SLOW_TESTS_H__ 2 | #define __INTERNAL_SLOW_TESTS_H__ 3 | 4 | /* silent = 0 : prints info+error messages to stdout, abort() on test failure 5 | * silent = 1 : returns 0 for success, anything else for failure 6 | * iterations : hardcoded known-good values are at 10000, so run at least this many 7 | */ 8 | 9 | int curvesigs_slow_test(int silent, int iterations); 10 | int xeddsa_slow_test(int silent, int iterations); 11 | int xeddsa_to_curvesigs_slow_test(int silent, int iterations); 12 | int generalized_xveddsa_slow_test(int silent, int iterations); 13 | 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /Public API Docs/docsets/.docset/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleIdentifier 6 | com.jazzy. 7 | CFBundleName 8 | 9 | DocSetPlatformFamily 10 | 11 | isDashDocset 12 | 13 | dashIndexFilePath 14 | index.html 15 | isJavaScriptEnabled 16 | 17 | DashDocSetFamily 18 | dashtoc 19 | 20 | 21 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/tests/internal_fast_tests.h: -------------------------------------------------------------------------------- 1 | #ifndef __INTERNAL_FAST_TESTS_H__ 2 | #define __INTERNAL_FAST_TESTS_H__ 3 | 4 | /* silent = 0 : prints info+error messages to stdout, abort() on test failure 5 | * silent = 1 : returns 0 for success, anything else for failure 6 | */ 7 | 8 | int sha512_fast_test(int silent); 9 | int strict_fast_test(int silent); 10 | int elligator_fast_test(int silent); 11 | int curvesigs_fast_test(int silent); 12 | int xeddsa_fast_test(int silent); 13 | int vxeddsa_fast_test(int silent); 14 | int generalized_xeddsa_fast_test(int silent); 15 | int generalized_xveddsa_fast_test(int silent); 16 | 17 | int all_fast_tests(int silent); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/nacl_includes/crypto_verify_32.h: -------------------------------------------------------------------------------- 1 | #ifndef crypto_verify_32_H 2 | #define crypto_verify_32_H 3 | 4 | #define crypto_verify_32_ref_BYTES 32 5 | #ifdef __cplusplus 6 | #include 7 | extern "C" { 8 | #endif 9 | extern int crypto_verify_32_ref(const unsigned char *,const unsigned char *); 10 | #ifdef __cplusplus 11 | } 12 | #endif 13 | 14 | #define crypto_verify_32 crypto_verify_32_ref 15 | #define crypto_verify_32_BYTES crypto_verify_32_ref_BYTES 16 | #define crypto_verify_32_IMPLEMENTATION "crypto_verify/32/ref" 17 | #ifndef crypto_verify_32_ref_VERSION 18 | #define crypto_verify_32_ref_VERSION "-" 19 | #endif 20 | #define crypto_verify_32_VERSION crypto_verify_32_ref_VERSION 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/xeddsa.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __XEDDSA_H__ 3 | #define __XEDDSA_H__ 4 | 5 | /* returns 0 on success */ 6 | int xed25519_sign(unsigned char* signature_out, /* 64 bytes */ 7 | const unsigned char* curve25519_privkey, /* 32 bytes */ 8 | const unsigned char* msg, const unsigned long msg_len, /* <= 256 bytes */ 9 | const unsigned char* random); /* 64 bytes */ 10 | 11 | /* returns 0 on success */ 12 | int xed25519_verify(const unsigned char* signature, /* 64 bytes */ 13 | const unsigned char* curve25519_pubkey, /* 32 bytes */ 14 | const unsigned char* msg, const unsigned long msg_len); /* <= 256 bytes */ 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/keygen.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | #include "keygen.h" 3 | #include "crypto_additions.h" 4 | 5 | void curve25519_keygen(unsigned char* curve25519_pubkey_out, 6 | const unsigned char* curve25519_privkey_in) 7 | { 8 | /* Perform a fixed-base multiplication of the Edwards base point, 9 | (which is efficient due to precalculated tables), then convert 10 | to the Curve25519 montgomery-format public key. 11 | 12 | NOTE: y=1 is converted to u=0 since fe_invert is mod-exp 13 | */ 14 | 15 | ge_p3 ed; /* Ed25519 pubkey point */ 16 | fe u; 17 | 18 | ge_scalarmult_base(&ed, curve25519_privkey_in); 19 | ge_p3_to_montx(u, &ed); 20 | fe_tobytes(curve25519_pubkey_out, u); 21 | } 22 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/utility.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "utility.h" 4 | 5 | void print_vector(const char* name, const unsigned char* v) 6 | { 7 | int count; 8 | printf("%s = \n", name); 9 | for (count = 0; count < 32; count++) 10 | printf("%02x ", v[count]); 11 | printf("\n"); 12 | } 13 | 14 | void print_bytes(const char* name, const unsigned char* v, int numbytes) 15 | { 16 | int count; 17 | printf("%s = \n", name); 18 | for (count = 0; count < numbytes; count++) 19 | printf("%02x ", v[count]); 20 | printf("\n"); 21 | } 22 | 23 | void print_fe(const char* name, const fe in) 24 | { 25 | unsigned char bytes[32]; 26 | fe_tobytes(bytes, in); 27 | print_vector(name, bytes); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/curve_sigs.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __CURVE_SIGS_H__ 3 | #define __CURVE_SIGS_H__ 4 | 5 | /* returns 0 on success */ 6 | int curve25519_sign(unsigned char* signature_out, /* 64 bytes */ 7 | const unsigned char* curve25519_privkey, /* 32 bytes */ 8 | const unsigned char* msg, const unsigned long msg_len, /* <= 256 bytes */ 9 | const unsigned char* random); /* 64 bytes */ 10 | 11 | /* returns 0 on success */ 12 | int curve25519_verify(const unsigned char* signature, /* 64 bytes */ 13 | const unsigned char* curve25519_pubkey, /* 32 bytes */ 14 | const unsigned char* msg, const unsigned long msg_len); /* <= 256 bytes */ 15 | 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /libsignal-protocol-swiftTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /libsignal-protocol-swiftTests/TestImplementation/TestPreKeyStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TestPreKeyStore.swift 3 | // libsignal-protocol-swiftTests 4 | // 5 | // Created by User on 16.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalProtocol 11 | 12 | class TestPreKeyStore: PreKeyStore { 13 | 14 | var keys = [UInt32 : Data]() 15 | 16 | func load(preKey: UInt32) -> Data? { 17 | return keys[preKey] 18 | } 19 | 20 | func store(preKey: Data, for id: UInt32) -> Bool { 21 | keys[id] = preKey 22 | return true 23 | } 24 | 25 | func contains(preKey: UInt32) -> Bool { 26 | return keys[preKey] != nil 27 | } 28 | 29 | func remove(preKey: UInt32) -> Bool { 30 | keys[preKey] = nil 31 | return true 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/hkdf.h: -------------------------------------------------------------------------------- 1 | #ifndef HKDF_H 2 | #define HKDF_H 3 | 4 | #include 5 | #include 6 | #include "signal_protocol_types.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | int hkdf_create(hkdf_context **context, int message_version, signal_context *global_context); 13 | 14 | ssize_t hkdf_derive_secrets(hkdf_context *context, 15 | uint8_t **output, 16 | const uint8_t *input_key_material, size_t input_key_material_len, 17 | const uint8_t *salt, size_t salt_len, 18 | const uint8_t *info, size_t info_len, 19 | size_t output_len); 20 | 21 | int hkdf_compare(const hkdf_context *context1, const hkdf_context *context2); 22 | 23 | void hkdf_destroy(signal_type_base *type); 24 | 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | 29 | #endif /* HKDF_H */ 30 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/nacl_includes/crypto_sign.h: -------------------------------------------------------------------------------- 1 | #ifndef crypto_sign_H 2 | #define crypto_sign_H 3 | 4 | #include "crypto_sign_edwards25519sha512batch.h" 5 | 6 | #define crypto_sign crypto_sign_edwards25519sha512batch 7 | #define crypto_sign_open crypto_sign_edwards25519sha512batch_open 8 | #define crypto_sign_keypair crypto_sign_edwards25519sha512batch_keypair 9 | #define crypto_sign_BYTES crypto_sign_edwards25519sha512batch_BYTES 10 | #define crypto_sign_PUBLICKEYBYTES crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES 11 | #define crypto_sign_SECRETKEYBYTES crypto_sign_edwards25519sha512batch_SECRETKEYBYTES 12 | #define crypto_sign_PRIMITIVE "edwards25519sha512batch" 13 | #define crypto_sign_IMPLEMENTATION crypto_sign_edwards25519sha512batch_IMPLEMENTATION 14 | #define crypto_sign_VERSION crypto_sign_edwards25519sha512batch_VERSION 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Additional/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | NSPrincipalClass 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /libsignal-protocol-swiftTests/TestImplementation/TestSignedPrekeyStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TestSignedPrekeyStore.swift 3 | // libsignal-protocol-swiftTests 4 | // 5 | // Created by User on 16.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalProtocol 11 | 12 | final class TestSignedPrekeyStore: SignedPreKeyStore { 13 | 14 | var keys = [UInt32 : Data]() 15 | 16 | func load(signedPreKey: UInt32) -> Data? { 17 | return keys[signedPreKey] 18 | } 19 | 20 | func store(signedPreKey: Data, for id: UInt32) -> Bool { 21 | keys[id] = signedPreKey 22 | return true 23 | } 24 | 25 | func contains(signedPreKey: UInt32) -> Bool { 26 | return keys[signedPreKey] != nil 27 | } 28 | 29 | func remove(signedPreKey: UInt32) -> Bool { 30 | keys[signedPreKey] = nil 31 | return true 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/compare.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "compare.h" 3 | 4 | /* Const-time comparison from SUPERCOP, but here it's only used for 5 | signature verification, so doesn't need to be const-time. But 6 | copied the nacl version anyways. */ 7 | int crypto_verify_32_ref(const unsigned char *x, const unsigned char *y) 8 | { 9 | unsigned int differentbits = 0; 10 | #define F(i) differentbits |= x[i] ^ y[i]; 11 | F(0) 12 | F(1) 13 | F(2) 14 | F(3) 15 | F(4) 16 | F(5) 17 | F(6) 18 | F(7) 19 | F(8) 20 | F(9) 21 | F(10) 22 | F(11) 23 | F(12) 24 | F(13) 25 | F(14) 26 | F(15) 27 | F(16) 28 | F(17) 29 | F(18) 30 | F(19) 31 | F(20) 32 | F(21) 33 | F(22) 34 | F(23) 35 | F(24) 36 | F(25) 37 | F(26) 38 | F(27) 39 | F(28) 40 | F(29) 41 | F(30) 42 | F(31) 43 | return (1 & ((differentbits - 1) >> 8)) - 1; 44 | } 45 | -------------------------------------------------------------------------------- /libsignal-protocol-swiftTests/TestImplementation/TestSenderKeyStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TestSenderKeyStore.swift 3 | // libsignal-protocol-swiftTests 4 | // 5 | // Created by User on 16.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalProtocol 11 | 12 | final class TestSenderKeyStore: SenderKeyStore { 13 | 14 | var keys = [SignalSenderKeyName: Data]() 15 | 16 | var records = [SignalSenderKeyName: Data]() 17 | 18 | func store(senderKey: Data, for address: SignalSenderKeyName, userRecord: Data?) -> Bool { 19 | keys[address] = senderKey 20 | records[address] = userRecord 21 | return true 22 | } 23 | 24 | func loadSenderKey(for address: SignalSenderKeyName) -> (senderKey: Data, userRecord: Data?)? { 25 | guard let key = keys[address] else { 26 | return nil 27 | } 28 | return (key, records[address]) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/StoreWrappers/KeyStoreWrapper.swift: -------------------------------------------------------------------------------- 1 | // 2 | // KeyWrapper.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 17.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | 12 | protocol KeyStoreWrapper { 13 | 14 | associatedtype StoreType 15 | 16 | static var delegates: [Int: StoreType] { get set } 17 | 18 | static func setStore(in context: OpaquePointer, delegate: StoreType, userData: UnsafeMutablePointer) throws 19 | } 20 | 21 | extension KeyStoreWrapper { 22 | 23 | static func removeDelegate(for id: Int) { 24 | delegates[id] = nil 25 | } 26 | 27 | static func delegate(for pointer: UnsafeMutableRawPointer?) -> StoreType? { 28 | guard let ptr = pointer else { 29 | return nil 30 | } 31 | let typedPtr = ptr.assumingMemoryBound(to: Int.self) 32 | return delegates[typedPtr.pointee] 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/sign.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "crypto_sign.h" 3 | #include "crypto_hash_sha512.h" 4 | #include "ge.h" 5 | #include "sc.h" 6 | 7 | int crypto_sign( 8 | unsigned char *sm,unsigned long long *smlen, 9 | const unsigned char *m,unsigned long long mlen, 10 | const unsigned char *sk 11 | ) 12 | { 13 | unsigned char pk[32]; 14 | unsigned char az[64]; 15 | unsigned char nonce[64]; 16 | unsigned char hram[64]; 17 | ge_p3 R; 18 | 19 | memmove(pk,sk + 32,32); 20 | 21 | crypto_hash_sha512(az,sk,32); 22 | az[0] &= 248; 23 | az[31] &= 63; 24 | az[31] |= 64; 25 | 26 | *smlen = mlen + 64; 27 | memmove(sm + 64,m,mlen); 28 | memmove(sm + 32,az + 32,32); 29 | crypto_hash_sha512(nonce,sm + 32,mlen + 32); 30 | memmove(sm + 32,pk,32); 31 | 32 | sc_reduce(nonce); 33 | ge_scalarmult_base(&R,nonce); 34 | ge_p3_tobytes(sm,&R); 35 | 36 | crypto_hash_sha512(hram,sm,mlen + 64); 37 | sc_reduce(hram); 38 | sc_muladd(sm + 32,hram,az,nonce); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /libsignal-protocol-swiftTests/TestImplementation/TestIdentityStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TestIdentityStore.swift 3 | // libsignal-protocol-swiftTests 4 | // 5 | // Created by User on 16.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalProtocol 11 | 12 | class TestIdentityStore: IdentityKeyStore { 13 | 14 | var keys = [SignalAddress : Data]() 15 | 16 | var identity: KeyPair? 17 | 18 | var registrationId: UInt32? 19 | 20 | func identityKeyPair() -> KeyPair? { 21 | return identity 22 | } 23 | 24 | func localRegistrationId() -> UInt32? { 25 | return registrationId 26 | } 27 | 28 | func save(identity: Data?, for address: SignalAddress) -> Bool { 29 | keys[address] = identity 30 | return true 31 | } 32 | 33 | func isTrusted(identity: Data, for address: SignalAddress) -> Bool? { 34 | guard let savedIdentity = keys[address] else { 35 | return true 36 | } 37 | return savedIdentity == identity 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_neg.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | /* 4 | h = -f 5 | 6 | Preconditions: 7 | |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. 8 | 9 | Postconditions: 10 | |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. 11 | */ 12 | 13 | void fe_neg(fe h,const fe f) 14 | { 15 | crypto_int32 f0 = f[0]; 16 | crypto_int32 f1 = f[1]; 17 | crypto_int32 f2 = f[2]; 18 | crypto_int32 f3 = f[3]; 19 | crypto_int32 f4 = f[4]; 20 | crypto_int32 f5 = f[5]; 21 | crypto_int32 f6 = f[6]; 22 | crypto_int32 f7 = f[7]; 23 | crypto_int32 f8 = f[8]; 24 | crypto_int32 f9 = f[9]; 25 | crypto_int32 h0 = -f0; 26 | crypto_int32 h1 = -f1; 27 | crypto_int32 h2 = -f2; 28 | crypto_int32 h3 = -f3; 29 | crypto_int32 h4 = -f4; 30 | crypto_int32 h5 = -f5; 31 | crypto_int32 h6 = -f6; 32 | crypto_int32 h7 = -f7; 33 | crypto_int32 h8 = -f8; 34 | crypto_int32 h9 = -f9; 35 | h[0] = h0; 36 | h[1] = h1; 37 | h[2] = h2; 38 | h[3] = h3; 39 | h[4] = h4; 40 | h[5] = h5; 41 | h[6] = h6; 42 | h[7] = h7; 43 | h[8] = h8; 44 | h[9] = h9; 45 | } 46 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/generalized/gen_veddsa.h: -------------------------------------------------------------------------------- 1 | #ifndef __GEN_VEDDSA_H__ 2 | #define __GEN_VEDDSA_H__ 3 | 4 | int generalized_veddsa_25519_sign( 5 | unsigned char* signature_out, 6 | const unsigned char* eddsa_25519_pubkey_bytes, 7 | const unsigned char* eddsa_25519_privkey_scalar, 8 | const unsigned char* msg, 9 | const unsigned long msg_len, 10 | const unsigned char* random, 11 | const unsigned char* customization_label, 12 | const unsigned long customization_label_len); 13 | 14 | int generalized_veddsa_25519_verify( 15 | unsigned char* vrf_out, 16 | const unsigned char* signature, 17 | const unsigned char* eddsa_25519_pubkey_bytes, 18 | const unsigned char* msg, 19 | const unsigned long msg_len, 20 | const unsigned char* customization_label, 21 | const unsigned long customization_label_len); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/sc_neg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "crypto_additions.h" 3 | #include "sc.h" 4 | 5 | /* l = order of base point = 2^252 + 27742317777372353535851937790883648493 */ 6 | 7 | /* 8 | static unsigned char l[32] = {0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 9 | 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 10 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 11 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x10}; 12 | */ 13 | 14 | static unsigned char lminus1[32] = {0xec, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 15 | 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 16 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 17 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10}; 18 | 19 | /* b = -a (mod l) */ 20 | void sc_neg(unsigned char *b, const unsigned char *a) 21 | { 22 | unsigned char zero[32]; 23 | memset(zero, 0, 32); 24 | sc_muladd(b, lminus1, a, zero); /* b = (-1)a + 0 (mod l) */ 25 | } 26 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/generalized/gen_labelset.h: -------------------------------------------------------------------------------- 1 | #ifndef __GEN_LABELSET_H__ 2 | #define __GEN_LABELSET_H__ 3 | 4 | extern const unsigned char B_bytes[]; 5 | 6 | unsigned char* buffer_add(unsigned char* bufptr, const unsigned char* bufend, 7 | const unsigned char* in, const unsigned long in_len); 8 | 9 | unsigned char* buffer_pad(const unsigned char* buf, unsigned char* bufptr, const unsigned char* bufend); 10 | 11 | 12 | int labelset_new(unsigned char* labelset, unsigned long* labelset_len, const unsigned long labelset_maxlen, 13 | const unsigned char* protocol_name, const unsigned char protocol_name_len, 14 | const unsigned char* customization_label, const unsigned char customization_label_len); 15 | 16 | int labelset_add(unsigned char* labelset, unsigned long* labelset_len, const unsigned long labelset_maxlen, 17 | const unsigned char* label, const unsigned char label_len); 18 | 19 | int labelset_validate(const unsigned char* labelset, const unsigned long labelset_len); 20 | 21 | int labelset_is_empty(const unsigned char* labelset, const unsigned long labelset_len); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/open_modified.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "crypto_sign.h" 3 | #include "crypto_hash_sha512.h" 4 | #include "crypto_verify_32.h" 5 | #include "ge.h" 6 | #include "sc.h" 7 | #include "crypto_additions.h" 8 | 9 | int crypto_sign_open_modified( 10 | unsigned char *m, 11 | const unsigned char *sm,unsigned long long smlen, 12 | const unsigned char *pk 13 | ) 14 | { 15 | unsigned char pkcopy[32]; 16 | unsigned char rcopy[32]; 17 | unsigned char scopy[32]; 18 | unsigned char h[64]; 19 | unsigned char rcheck[32]; 20 | ge_p3 A; 21 | ge_p2 R; 22 | 23 | if (smlen < 64) goto badsig; 24 | if (sm[63] & 224) goto badsig; /* strict parsing of s */ 25 | if (ge_frombytes_negate_vartime(&A,pk) != 0) goto badsig; 26 | 27 | memmove(pkcopy,pk,32); 28 | memmove(rcopy,sm,32); 29 | memmove(scopy,sm + 32,32); 30 | 31 | memmove(m,sm,smlen); 32 | memmove(m + 32,pkcopy,32); 33 | crypto_hash_sha512(h,m,smlen); 34 | sc_reduce(h); 35 | 36 | ge_double_scalarmult_vartime(&R,h,&A,scopy); 37 | ge_tobytes(rcheck,&R); 38 | 39 | if (crypto_verify_32(rcheck,rcopy) == 0) { 40 | return 0; 41 | } 42 | 43 | badsig: 44 | return -1; 45 | } 46 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_frombytes.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | static const fe d = { 4 | #include "d.h" 5 | } ; 6 | 7 | static const fe sqrtm1 = { 8 | #include "sqrtm1.h" 9 | } ; 10 | 11 | int ge_frombytes_negate_vartime(ge_p3 *h,const unsigned char *s) 12 | { 13 | fe u; 14 | fe v; 15 | fe v3; 16 | fe vxx; 17 | fe check; 18 | 19 | fe_frombytes(h->Y,s); 20 | fe_1(h->Z); 21 | fe_sq(u,h->Y); 22 | fe_mul(v,u,d); 23 | fe_sub(u,u,h->Z); /* u = y^2-1 */ 24 | fe_add(v,v,h->Z); /* v = dy^2+1 */ 25 | 26 | fe_sq(v3,v); 27 | fe_mul(v3,v3,v); /* v3 = v^3 */ 28 | fe_sq(h->X,v3); 29 | fe_mul(h->X,h->X,v); 30 | fe_mul(h->X,h->X,u); /* x = uv^7 */ 31 | 32 | fe_pow22523(h->X,h->X); /* x = (uv^7)^((q-5)/8) */ 33 | fe_mul(h->X,h->X,v3); 34 | fe_mul(h->X,h->X,u); /* x = uv^3(uv^7)^((q-5)/8) */ 35 | 36 | fe_sq(vxx,h->X); 37 | fe_mul(vxx,vxx,v); 38 | fe_sub(check,vxx,u); /* vx^2-u */ 39 | if (fe_isnonzero(check)) { 40 | fe_add(check,vxx,u); /* vx^2+u */ 41 | if (fe_isnonzero(check)) return -1; 42 | fe_mul(h->X,h->X,sqrtm1); 43 | } 44 | 45 | if (fe_isnegative(h->X) == (s[31] >> 7)) 46 | fe_neg(h->X,h->X); 47 | 48 | fe_mul(h->T,h->X,h->Y); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/open.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "crypto_sign.h" 3 | #include "crypto_hash_sha512.h" 4 | #include "crypto_verify_32.h" 5 | #include "ge.h" 6 | #include "sc.h" 7 | 8 | int crypto_sign_open( 9 | unsigned char *m,unsigned long long *mlen, 10 | const unsigned char *sm,unsigned long long smlen, 11 | const unsigned char *pk 12 | ) 13 | { 14 | unsigned char pkcopy[32]; 15 | unsigned char rcopy[32]; 16 | unsigned char scopy[32]; 17 | unsigned char h[64]; 18 | unsigned char rcheck[32]; 19 | ge_p3 A; 20 | ge_p2 R; 21 | 22 | if (smlen < 64) goto badsig; 23 | if (sm[63] & 224) goto badsig; 24 | if (ge_frombytes_negate_vartime(&A,pk) != 0) goto badsig; 25 | 26 | memmove(pkcopy,pk,32); 27 | memmove(rcopy,sm,32); 28 | memmove(scopy,sm + 32,32); 29 | 30 | memmove(m,sm,smlen); 31 | memmove(m + 32,pkcopy,32); 32 | crypto_hash_sha512(h,m,smlen); 33 | sc_reduce(h); 34 | 35 | ge_double_scalarmult_vartime(&R,h,&A,scopy); 36 | ge_tobytes(rcheck,&R); 37 | if (crypto_verify_32(rcheck,rcopy) == 0) { 38 | memmove(m,m + 64,smlen - 64); 39 | memset(m + smlen - 64,0,64); 40 | *mlen = smlen - 64; 41 | return 0; 42 | } 43 | 44 | badsig: 45 | *mlen = -1; 46 | memset(m,0,smlen); 47 | return -1; 48 | } 49 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/session_builder_internal.h: -------------------------------------------------------------------------------- 1 | #ifndef SESSION_BUILDER_INTERNAL_H 2 | #define SESSION_BUILDER_INTERNAL_H 3 | 4 | #include 5 | #include "signal_protocol_types.h" 6 | 7 | /** 8 | * Build a new session from a received pre_key_signal_message. 9 | * 10 | * After a session is constructed in this way, the embedded signal_message 11 | * can be decrypted. 12 | * 13 | * @param message The received pre_key_signal_message. 14 | * @param unsigned_pre_key_id set to the unsigned pre key ID, if available. 15 | * Return value indicates whether or not this value is available. 16 | * @retval 0 Success, no unsigned pre key value available 17 | * @retval 1 Success, an unsigned pre key is available 18 | * @retval SG_ERR_INVALID_KEY_ID when there is no local pre_key_record that 19 | * corresponds to the PreKey ID in the message. 20 | * @retval SG_ERR_INVALID_KEY when the message is formatted incorrectly. 21 | * @retval SG_ERR_UNTRUSTED_IDENTITY when the identity key of the sender is untrusted. 22 | */ 23 | int session_builder_process_pre_key_signal_message(session_builder *builder, 24 | session_record *record, pre_key_signal_message *message, uint32_t *unsigned_pre_key_id); 25 | 26 | #endif /* SESSION_BUILDER_INTERNAL_H */ 27 | -------------------------------------------------------------------------------- /libsignal-protocol-swift.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |spec| 2 | spec.name = 'libsignal-protocol-swift' 3 | spec.summary = 'A Swift wrapper framework for libsignal-protocol-c' 4 | spec.license = 'GPLv3' 5 | 6 | spec.version = '0.2.2' 7 | spec.source = { 8 | :git => 'https://github.com/christophhagen/libsignal-protocol-swift.git', 9 | :tag => spec.version 10 | } 11 | spec.swift_version = '4.0' 12 | spec.module_name = 'SignalProtocol' 13 | 14 | spec.authors = { 'Christoph Hagen' => 'christoph@spacemasters.eu' } 15 | spec.homepage = 'https://github.com/christophhagen/libsignal-protocol-swift' 16 | 17 | spec.ios.deployment_target = '9.0' 18 | spec.osx.deployment_target = '10.9' 19 | spec.tvos.deployment_target = '9.0' 20 | spec.watchos.deployment_target = '4.0' 21 | 22 | spec.source_files = 'libsignal-protocol-swift/**/*.{swift,c,h}' 23 | spec.public_header_files = '' 24 | spec.private_header_files = 'libsignal-protocol-swift/**/*.h' 25 | 26 | spec.pod_target_xcconfig = { 27 | 'SWIFT_INCLUDE_PATHS' => '${BUILT_PRODUCTS_DIR}/SignalModuleMap, ${BUILT_PRODUCTS_DIR}/CommonCryptoModuleMap' 28 | } 29 | spec.resources = '$(BUILT_PRODUCTS_DIR)/**/*.modulemap' 30 | spec.preserve_paths = '$(BUILT_PRODUCTS_DIR)/**/*.modulemap' 31 | 32 | end 33 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/sender_key.h: -------------------------------------------------------------------------------- 1 | #ifndef SENDER_KEY 2 | #define SENDER_KEY 3 | 4 | #include 5 | #include 6 | #include "signal_protocol_types.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | int sender_message_key_create(sender_message_key **key, 13 | uint32_t iteration, signal_buffer *seed, 14 | signal_context *global_context); 15 | uint32_t sender_message_key_get_iteration(sender_message_key *key); 16 | signal_buffer *sender_message_key_get_iv(sender_message_key *key); 17 | signal_buffer *sender_message_key_get_cipher_key(sender_message_key *key); 18 | signal_buffer *sender_message_key_get_seed(sender_message_key *key); 19 | void sender_message_key_destroy(signal_type_base *type); 20 | 21 | int sender_chain_key_create(sender_chain_key **key, 22 | uint32_t iteration, signal_buffer *chain_key, 23 | signal_context *global_context); 24 | uint32_t sender_chain_key_get_iteration(sender_chain_key *key); 25 | int sender_chain_key_create_message_key(sender_chain_key *key, sender_message_key **message_key); 26 | int sender_chain_key_create_next(sender_chain_key *key, sender_chain_key **next_key); 27 | signal_buffer *sender_chain_key_get_seed(sender_chain_key *key); 28 | void sender_chain_key_destroy(signal_type_base *type); 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | 34 | #endif /* SENDER_KEY */ 35 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_add.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | /* 4 | h = f + g 5 | Can overlap h with f or g. 6 | 7 | Preconditions: 8 | |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. 9 | |g| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. 10 | 11 | Postconditions: 12 | |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. 13 | */ 14 | 15 | void fe_add(fe h,const fe f,const fe g) 16 | { 17 | crypto_int32 f0 = f[0]; 18 | crypto_int32 f1 = f[1]; 19 | crypto_int32 f2 = f[2]; 20 | crypto_int32 f3 = f[3]; 21 | crypto_int32 f4 = f[4]; 22 | crypto_int32 f5 = f[5]; 23 | crypto_int32 f6 = f[6]; 24 | crypto_int32 f7 = f[7]; 25 | crypto_int32 f8 = f[8]; 26 | crypto_int32 f9 = f[9]; 27 | crypto_int32 g0 = g[0]; 28 | crypto_int32 g1 = g[1]; 29 | crypto_int32 g2 = g[2]; 30 | crypto_int32 g3 = g[3]; 31 | crypto_int32 g4 = g[4]; 32 | crypto_int32 g5 = g[5]; 33 | crypto_int32 g6 = g[6]; 34 | crypto_int32 g7 = g[7]; 35 | crypto_int32 g8 = g[8]; 36 | crypto_int32 g9 = g[9]; 37 | crypto_int32 h0 = f0 + g0; 38 | crypto_int32 h1 = f1 + g1; 39 | crypto_int32 h2 = f2 + g2; 40 | crypto_int32 h3 = f3 + g3; 41 | crypto_int32 h4 = f4 + g4; 42 | crypto_int32 h5 = f5 + g5; 43 | crypto_int32 h6 = f6 + g6; 44 | crypto_int32 h7 = f7 + g7; 45 | crypto_int32 h8 = f8 + g8; 46 | crypto_int32 h9 = f9 + g9; 47 | h[0] = h0; 48 | h[1] = h1; 49 | h[2] = h2; 50 | h[3] = h3; 51 | h[4] = h4; 52 | h[5] = h5; 53 | h[6] = h6; 54 | h[7] = h7; 55 | h[8] = h8; 56 | h[9] = h9; 57 | } 58 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_sub.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | /* 4 | h = f - g 5 | Can overlap h with f or g. 6 | 7 | Preconditions: 8 | |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. 9 | |g| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. 10 | 11 | Postconditions: 12 | |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. 13 | */ 14 | 15 | void fe_sub(fe h,const fe f,const fe g) 16 | { 17 | crypto_int32 f0 = f[0]; 18 | crypto_int32 f1 = f[1]; 19 | crypto_int32 f2 = f[2]; 20 | crypto_int32 f3 = f[3]; 21 | crypto_int32 f4 = f[4]; 22 | crypto_int32 f5 = f[5]; 23 | crypto_int32 f6 = f[6]; 24 | crypto_int32 f7 = f[7]; 25 | crypto_int32 f8 = f[8]; 26 | crypto_int32 f9 = f[9]; 27 | crypto_int32 g0 = g[0]; 28 | crypto_int32 g1 = g[1]; 29 | crypto_int32 g2 = g[2]; 30 | crypto_int32 g3 = g[3]; 31 | crypto_int32 g4 = g[4]; 32 | crypto_int32 g5 = g[5]; 33 | crypto_int32 g6 = g[6]; 34 | crypto_int32 g7 = g[7]; 35 | crypto_int32 g8 = g[8]; 36 | crypto_int32 g9 = g[9]; 37 | crypto_int32 h0 = f0 - g0; 38 | crypto_int32 h1 = f1 - g1; 39 | crypto_int32 h2 = f2 - g2; 40 | crypto_int32 h3 = f3 - g3; 41 | crypto_int32 h4 = f4 - g4; 42 | crypto_int32 h5 = f5 - g5; 43 | crypto_int32 h6 = f6 - g6; 44 | crypto_int32 h7 = f7 - g7; 45 | crypto_int32 h8 = f8 - g8; 46 | crypto_int32 h9 = f9 - g9; 47 | h[0] = h0; 48 | h[1] = h1; 49 | h[2] = h2; 50 | h[3] = h3; 51 | h[4] = h4; 52 | h[5] = h5; 53 | h[6] = h6; 54 | h[7] = h7; 55 | h[8] = h8; 56 | h[9] = h9; 57 | } 58 | -------------------------------------------------------------------------------- /libsignal-protocol-swift.xcodeproj/xcuserdata/User.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SignalModuleMap.xcscheme 8 | 9 | orderHint 10 | 4 11 | 12 | libsignal-protocol-swift 2.xcscheme 13 | 14 | orderHint 15 | 1 16 | 17 | libsignal-protocol-swift 4.xcscheme 18 | 19 | orderHint 20 | 2 21 | 22 | libsignal-protocol-swift 6.xcscheme 23 | 24 | orderHint 25 | 3 26 | 27 | libsignal-protocol-swift iOS.xcscheme 28 | 29 | orderHint 30 | 0 31 | 32 | libsignal-protocol-swift macOS.xcscheme 33 | 34 | orderHint 35 | 1 36 | 37 | libsignal-protocol-swift tvOS.xcscheme 38 | 39 | orderHint 40 | 2 41 | 42 | libsignal-protocol-swift watchOS.xcscheme 43 | 44 | orderHint 45 | 3 46 | 47 | libsignal-protocol-swift.xcscheme 48 | 49 | orderHint 50 | 0 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Public API Docs/js/jazzy.js: -------------------------------------------------------------------------------- 1 | window.jazzy = {'docset': false} 2 | if (typeof window.dash != 'undefined') { 3 | document.documentElement.className += ' dash' 4 | window.jazzy.docset = true 5 | } 6 | if (navigator.userAgent.match(/xcode/i)) { 7 | document.documentElement.className += ' xcode' 8 | window.jazzy.docset = true 9 | } 10 | 11 | // On doc load, toggle the URL hash discussion if present 12 | $(document).ready(function() { 13 | if (!window.jazzy.docset) { 14 | var linkToHash = $('a[href="' + window.location.hash +'"]'); 15 | linkToHash.trigger("click"); 16 | } 17 | }); 18 | 19 | // On token click, toggle its discussion and animate token.marginLeft 20 | $(".token").click(function(event) { 21 | if (window.jazzy.docset) { 22 | return; 23 | } 24 | var link = $(this); 25 | var animationDuration = 300; 26 | var tokenOffset = "15px"; 27 | var original = link.css('marginLeft') == tokenOffset; 28 | link.animate({'margin-left':original ? "0px" : tokenOffset}, animationDuration); 29 | $content = link.parent().parent().next(); 30 | $content.slideToggle(animationDuration); 31 | 32 | // Keeps the document from jumping to the hash. 33 | var href = $(this).attr('href'); 34 | if (history.pushState) { 35 | history.pushState({}, '', href); 36 | } else { 37 | location.hash = href; 38 | } 39 | event.preventDefault(); 40 | }); 41 | 42 | // Dumb down quotes within code blocks that delimit strings instead of quotations 43 | // https://github.com/realm/jazzy/issues/714 44 | $("code q").replaceWith(function () { 45 | return ["\"", $(this).contents(), "\""]; 46 | }); 47 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/crypto_additions.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __CRYPTO_ADDITIONS__ 3 | #define __CRYPTO_ADDITIONS__ 4 | 5 | #include "crypto_uint32.h" 6 | #include "fe.h" 7 | #include "ge.h" 8 | 9 | #define MAX_MSG_LEN 256 10 | 11 | void sc_neg(unsigned char *b, const unsigned char *a); 12 | void sc_cmov(unsigned char* f, const unsigned char* g, unsigned char b); 13 | 14 | int fe_isequal(const fe f, const fe g); 15 | int fe_isreduced(const unsigned char* s); 16 | void fe_mont_rhs(fe v2, const fe u); 17 | void fe_montx_to_edy(fe y, const fe u); 18 | void fe_sqrt(fe b, const fe a); 19 | 20 | int ge_isneutral(const ge_p3* q); 21 | void ge_neg(ge_p3* r, const ge_p3 *p); 22 | void ge_montx_to_p3(ge_p3* p, const fe u, const unsigned char ed_sign_bit); 23 | void ge_p3_to_montx(fe u, const ge_p3 *p); 24 | void ge_scalarmult(ge_p3 *h, const unsigned char *a, const ge_p3 *A); 25 | void ge_scalarmult_cofactor(ge_p3 *q, const ge_p3 *p); 26 | 27 | void elligator(fe u, const fe r); 28 | void hash_to_point(ge_p3* p, const unsigned char* msg, const unsigned long in_len); 29 | 30 | int crypto_sign_modified( 31 | unsigned char *sm, 32 | const unsigned char *m,unsigned long long mlen, 33 | const unsigned char *sk, /* Curve/Ed25519 private key */ 34 | const unsigned char *pk, /* Ed25519 public key */ 35 | const unsigned char *random /* 64 bytes random to hash into nonce */ 36 | ); 37 | 38 | int crypto_sign_open_modified( 39 | unsigned char *m, 40 | const unsigned char *sm,unsigned long long smlen, 41 | const unsigned char *pk 42 | ); 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_cmov.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | /* 4 | Replace (f,g) with (g,g) if b == 1; 5 | replace (f,g) with (f,g) if b == 0. 6 | 7 | Preconditions: b in {0,1}. 8 | */ 9 | 10 | void fe_cmov(fe f,const fe g,unsigned int b) 11 | { 12 | crypto_int32 f0 = f[0]; 13 | crypto_int32 f1 = f[1]; 14 | crypto_int32 f2 = f[2]; 15 | crypto_int32 f3 = f[3]; 16 | crypto_int32 f4 = f[4]; 17 | crypto_int32 f5 = f[5]; 18 | crypto_int32 f6 = f[6]; 19 | crypto_int32 f7 = f[7]; 20 | crypto_int32 f8 = f[8]; 21 | crypto_int32 f9 = f[9]; 22 | crypto_int32 g0 = g[0]; 23 | crypto_int32 g1 = g[1]; 24 | crypto_int32 g2 = g[2]; 25 | crypto_int32 g3 = g[3]; 26 | crypto_int32 g4 = g[4]; 27 | crypto_int32 g5 = g[5]; 28 | crypto_int32 g6 = g[6]; 29 | crypto_int32 g7 = g[7]; 30 | crypto_int32 g8 = g[8]; 31 | crypto_int32 g9 = g[9]; 32 | crypto_int32 x0 = f0 ^ g0; 33 | crypto_int32 x1 = f1 ^ g1; 34 | crypto_int32 x2 = f2 ^ g2; 35 | crypto_int32 x3 = f3 ^ g3; 36 | crypto_int32 x4 = f4 ^ g4; 37 | crypto_int32 x5 = f5 ^ g5; 38 | crypto_int32 x6 = f6 ^ g6; 39 | crypto_int32 x7 = f7 ^ g7; 40 | crypto_int32 x8 = f8 ^ g8; 41 | crypto_int32 x9 = f9 ^ g9; 42 | b = -b; 43 | x0 &= b; 44 | x1 &= b; 45 | x2 &= b; 46 | x3 &= b; 47 | x4 &= b; 48 | x5 &= b; 49 | x6 &= b; 50 | x7 &= b; 51 | x8 &= b; 52 | x9 &= b; 53 | f[0] = f0 ^ x0; 54 | f[1] = f1 ^ x1; 55 | f[2] = f2 ^ x2; 56 | f[3] = f3 ^ x3; 57 | f[4] = f4 ^ x4; 58 | f[5] = f5 ^ x5; 59 | f[6] = f6 ^ x6; 60 | f[7] = f7 ^ x7; 61 | f[8] = f8 ^ x8; 62 | f[9] = f9 ^ x9; 63 | } 64 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/sign_modified.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "crypto_sign.h" 3 | #include "crypto_hash_sha512.h" 4 | #include "ge.h" 5 | #include "sc.h" 6 | #include "zeroize.h" 7 | #include "crypto_additions.h" 8 | 9 | /* NEW: Compare to pristine crypto_sign() 10 | Uses explicit private key for nonce derivation and as scalar, 11 | instead of deriving both from a master key. 12 | */ 13 | int crypto_sign_modified( 14 | unsigned char *sm, 15 | const unsigned char *m,unsigned long long mlen, 16 | const unsigned char *sk, const unsigned char* pk, 17 | const unsigned char* random 18 | ) 19 | { 20 | unsigned char nonce[64]; 21 | unsigned char hram[64]; 22 | ge_p3 R; 23 | int count=0; 24 | 25 | memmove(sm + 64,m,mlen); 26 | memmove(sm + 32,sk,32); /* NEW: Use privkey directly for nonce derivation */ 27 | 28 | /* NEW : add prefix to separate hash uses - see .h */ 29 | sm[0] = 0xFE; 30 | for (count = 1; count < 32; count++) 31 | sm[count] = 0xFF; 32 | 33 | /* NEW: add suffix of random data */ 34 | memmove(sm + mlen + 64, random, 64); 35 | 36 | crypto_hash_sha512(nonce,sm,mlen + 128); 37 | memmove(sm + 32,pk,32); 38 | 39 | sc_reduce(nonce); 40 | 41 | ge_scalarmult_base(&R,nonce); 42 | ge_p3_tobytes(sm,&R); 43 | 44 | crypto_hash_sha512(hram,sm,mlen + 64); 45 | sc_reduce(hram); 46 | sc_muladd(sm + 32,hram,sk,nonce); /* NEW: Use privkey directly */ 47 | 48 | /* Erase any traces of private scalar or 49 | nonce left in the stack from sc_muladd */ 50 | zeroize_stack(); 51 | zeroize(nonce, 64); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Public API Docs/docsets/.docset/Contents/Resources/Documents/js/jazzy.js: -------------------------------------------------------------------------------- 1 | window.jazzy = {'docset': false} 2 | if (typeof window.dash != 'undefined') { 3 | document.documentElement.className += ' dash' 4 | window.jazzy.docset = true 5 | } 6 | if (navigator.userAgent.match(/xcode/i)) { 7 | document.documentElement.className += ' xcode' 8 | window.jazzy.docset = true 9 | } 10 | 11 | // On doc load, toggle the URL hash discussion if present 12 | $(document).ready(function() { 13 | if (!window.jazzy.docset) { 14 | var linkToHash = $('a[href="' + window.location.hash +'"]'); 15 | linkToHash.trigger("click"); 16 | } 17 | }); 18 | 19 | // On token click, toggle its discussion and animate token.marginLeft 20 | $(".token").click(function(event) { 21 | if (window.jazzy.docset) { 22 | return; 23 | } 24 | var link = $(this); 25 | var animationDuration = 300; 26 | var tokenOffset = "15px"; 27 | var original = link.css('marginLeft') == tokenOffset; 28 | link.animate({'margin-left':original ? "0px" : tokenOffset}, animationDuration); 29 | $content = link.parent().parent().next(); 30 | $content.slideToggle(animationDuration); 31 | 32 | // Keeps the document from jumping to the hash. 33 | var href = $(this).attr('href'); 34 | if (history.pushState) { 35 | history.pushState({}, '', href); 36 | } else { 37 | location.hash = href; 38 | } 39 | event.preventDefault(); 40 | }); 41 | 42 | // Dumb down quotes within code blocks that delimit strings instead of quotations 43 | // https://github.com/realm/jazzy/issues/714 44 | $("code q").replaceWith(function () { 45 | return ["\"", $(this).contents(), "\""]; 46 | }); 47 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/fe_sqrt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "fe.h" 3 | #include "crypto_additions.h" 4 | 5 | /* sqrt(-1) */ 6 | static unsigned char i_bytes[32] = { 7 | 0xb0, 0xa0, 0x0e, 0x4a, 0x27, 0x1b, 0xee, 0xc4, 8 | 0x78, 0xe4, 0x2f, 0xad, 0x06, 0x18, 0x43, 0x2f, 9 | 0xa7, 0xd7, 0xfb, 0x3d, 0x99, 0x00, 0x4d, 0x2b, 10 | 0x0b, 0xdf, 0xc1, 0x4f, 0x80, 0x24, 0x83, 0x2b 11 | }; 12 | 13 | /* Preconditions: a is square or zero */ 14 | 15 | void fe_sqrt(fe out, const fe a) 16 | { 17 | fe exp, b, b2, bi, i; 18 | #ifndef NDEBUG 19 | fe legendre, zero, one; 20 | #endif 21 | 22 | fe_frombytes(i, i_bytes); 23 | fe_pow22523(exp, a); /* b = a^(q-5)/8 */ 24 | 25 | /* PRECONDITION: legendre symbol == 1 (square) or 0 (a == zero) */ 26 | #ifndef NDEBUG 27 | fe_sq(legendre, exp); /* in^((q-5)/4) */ 28 | fe_sq(legendre, legendre); /* in^((q-5)/2) */ 29 | fe_mul(legendre, legendre, a); /* in^((q-3)/2) */ 30 | fe_mul(legendre, legendre, a); /* in^((q-1)/2) */ 31 | 32 | fe_0(zero); 33 | fe_1(one); 34 | assert(fe_isequal(legendre, zero) || fe_isequal(legendre, one)); 35 | #endif 36 | 37 | fe_mul(b, a, exp); /* b = a * a^(q-5)/8 */ 38 | fe_sq(b2, b); /* b^2 = a * a^(q-1)/4 */ 39 | 40 | /* note b^4 == a^2, so b^2 == a or -a 41 | * if b^2 != a, multiply it by sqrt(-1) */ 42 | fe_mul(bi, b, i); 43 | fe_cmov(b, bi, 1 ^ fe_isequal(b2, a)); 44 | fe_copy(out, b); 45 | 46 | /* PRECONDITION: out^2 == a */ 47 | #ifndef NDEBUG 48 | fe_sq(b2, out); 49 | assert(fe_isequal(a, b2)); 50 | #endif 51 | } 52 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/sender_key_record.h: -------------------------------------------------------------------------------- 1 | #ifndef SENDER_KEY_RECORD_H 2 | #define SENDER_KEY_RECORD_H 3 | 4 | #include "signal_protocol_types.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | int sender_key_record_create(sender_key_record **record, 11 | signal_context *global_context); 12 | int sender_key_record_serialize(signal_buffer **buffer, sender_key_record *record); 13 | int sender_key_record_deserialize(sender_key_record **record, const uint8_t *data, size_t len, signal_context *global_context); 14 | int sender_key_record_copy(sender_key_record **record, sender_key_record *other_state, signal_context *global_context); 15 | 16 | int sender_key_record_is_empty(sender_key_record *record); 17 | int sender_key_record_get_sender_key_state(sender_key_record *record, sender_key_state **state); 18 | int sender_key_record_get_sender_key_state_by_id(sender_key_record *record, sender_key_state **state, uint32_t key_id); 19 | int sender_key_record_add_sender_key_state(sender_key_record *record, 20 | uint32_t id, uint32_t iteration, signal_buffer *chain_key, ec_public_key *signature_key); 21 | int sender_key_record_set_sender_key_state(sender_key_record *record, 22 | uint32_t id, uint32_t iteration, signal_buffer *chain_key, ec_key_pair *signature_key_pair); 23 | 24 | signal_buffer *sender_key_record_get_user_record(const sender_key_record *record); 25 | void sender_key_record_set_user_record(sender_key_record *record, signal_buffer *user_record); 26 | 27 | void sender_key_record_destroy(signal_type_base *type); 28 | 29 | #ifdef __cplusplus 30 | } 31 | #endif 32 | 33 | #endif /* SENDER_KEY_RECORD_H */ 34 | -------------------------------------------------------------------------------- /libsignal-protocol-swiftTests/TestImplementation/TestSessionStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TestSessionStore.swift 3 | // libsignal-protocol-swiftTests 4 | // 5 | // Created by User on 16.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalProtocol 11 | 12 | final class TestSessionStore: SessionStore { 13 | 14 | private var sessions = [SignalAddress : Data]() 15 | 16 | private var records = [SignalAddress : Data]() 17 | 18 | func loadSession(for address: SignalAddress) -> (session: Data, userRecord: Data?)? { 19 | guard let session = sessions[address] else { 20 | return nil 21 | } 22 | return (session, records[address]) 23 | } 24 | 25 | func subDeviceSessions(for name: String) -> [Int32]? { 26 | return sessions.keys.filter({ $0.name == name }).map { $0.deviceId } 27 | } 28 | 29 | func store(session: Data, for address: SignalAddress, userRecord: Data?) -> Bool { 30 | sessions[address] = session 31 | records[address] = userRecord 32 | return true 33 | } 34 | 35 | func containsSession(for address: SignalAddress) -> Bool { 36 | return sessions[address] != nil 37 | } 38 | 39 | func deleteSession(for address: SignalAddress) -> Bool? { 40 | sessions[address] = nil 41 | records[address] = nil 42 | return true 43 | } 44 | 45 | func deleteAllSessions(for name: String) -> Int? { 46 | let matches = sessions.keys.filter({ $0.name == name }) 47 | for item in matches { 48 | sessions[item] = nil 49 | } 50 | return matches.count 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/sender_key_state.h: -------------------------------------------------------------------------------- 1 | #ifndef SENDER_KEY_STATE_H 2 | #define SENDER_KEY_STATE_H 3 | 4 | #include 5 | #include "signal_protocol_types.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | int sender_key_state_create(sender_key_state **state, 12 | uint32_t id, sender_chain_key *chain_key, 13 | ec_public_key *signature_public_key, ec_private_key *signature_private_key, 14 | signal_context *global_context); 15 | int sender_key_state_serialize(signal_buffer **buffer, sender_key_state *state); 16 | int sender_key_state_deserialize(sender_key_state **state, const uint8_t *data, size_t len, signal_context *global_context); 17 | int sender_key_state_copy(sender_key_state **state, sender_key_state *other_state, signal_context *global_context); 18 | 19 | uint32_t sender_key_state_get_key_id(sender_key_state *state); 20 | sender_chain_key *sender_key_state_get_chain_key(sender_key_state *state); 21 | void sender_key_state_set_chain_key(sender_key_state *state, sender_chain_key *chain_key); 22 | ec_public_key *sender_key_state_get_signing_key_public(sender_key_state *state); 23 | ec_private_key *sender_key_state_get_signing_key_private(sender_key_state *state); 24 | int sender_key_state_has_sender_message_key(sender_key_state *state, uint32_t iteration); 25 | int sender_key_state_add_sender_message_key(sender_key_state *state, sender_message_key *message_key); 26 | sender_message_key *sender_key_state_remove_sender_message_key(sender_key_state *state, uint32_t iteration); 27 | 28 | void sender_key_state_destroy(signal_type_base *type); 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | 34 | #endif /* SENDER_KEY_STATE_H */ 35 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_p2_dbl.h: -------------------------------------------------------------------------------- 1 | 2 | /* qhasm: enter ge_p2_dbl */ 3 | 4 | /* qhasm: fe X1 */ 5 | 6 | /* qhasm: fe Y1 */ 7 | 8 | /* qhasm: fe Z1 */ 9 | 10 | /* qhasm: fe A */ 11 | 12 | /* qhasm: fe AA */ 13 | 14 | /* qhasm: fe XX */ 15 | 16 | /* qhasm: fe YY */ 17 | 18 | /* qhasm: fe B */ 19 | 20 | /* qhasm: fe X3 */ 21 | 22 | /* qhasm: fe Y3 */ 23 | 24 | /* qhasm: fe Z3 */ 25 | 26 | /* qhasm: fe T3 */ 27 | 28 | /* qhasm: XX=X1^2 */ 29 | /* asm 1: fe_sq(>XX=fe#1,XX=r->X,X); */ 31 | fe_sq(r->X,p->X); 32 | 33 | /* qhasm: YY=Y1^2 */ 34 | /* asm 1: fe_sq(>YY=fe#3,YY=r->Z,Y); */ 36 | fe_sq(r->Z,p->Y); 37 | 38 | /* qhasm: B=2*Z1^2 */ 39 | /* asm 1: fe_sq2(>B=fe#4,B=r->T,Z); */ 41 | fe_sq2(r->T,p->Z); 42 | 43 | /* qhasm: A=X1+Y1 */ 44 | /* asm 1: fe_add(>A=fe#2,A=r->Y,X,Y); */ 46 | fe_add(r->Y,p->X,p->Y); 47 | 48 | /* qhasm: AA=A^2 */ 49 | /* asm 1: fe_sq(>AA=fe#5,AA=t0,Y); */ 51 | fe_sq(t0,r->Y); 52 | 53 | /* qhasm: Y3=YY+XX */ 54 | /* asm 1: fe_add(>Y3=fe#2,Y3=r->Y,Z,X); */ 56 | fe_add(r->Y,r->Z,r->X); 57 | 58 | /* qhasm: Z3=YY-XX */ 59 | /* asm 1: fe_sub(>Z3=fe#3,Z3=r->Z,Z,X); */ 61 | fe_sub(r->Z,r->Z,r->X); 62 | 63 | /* qhasm: X3=AA-Y3 */ 64 | /* asm 1: fe_sub(>X3=fe#1,X3=r->X,Y); */ 66 | fe_sub(r->X,t0,r->Y); 67 | 68 | /* qhasm: T3=B-Z3 */ 69 | /* asm 1: fe_sub(>T3=fe#4,T3=r->T,T,Z); */ 71 | fe_sub(r->T,r->T,r->Z); 72 | 73 | /* qhasm: return */ 74 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Stores/SenderKeyStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SenderKeyStore.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 15.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | The `SenderKeyStore`protocol must be adopted to provide the storage for sender keys. 14 | */ 15 | public protocol SenderKeyStore { 16 | 17 | /** 18 | Store a serialized sender key record for a given 19 | (groupId + senderId + deviceId) tuple. 20 | 21 | - parameter senderKey: The serialized record 22 | - parameter address: the (groupId + senderId + deviceId) tuple 23 | - parameter userRecord: Containing application specific 24 | data to be stored alongside the serialized record. If no such 25 | data exists, then this parameter will be nil. 26 | - returns: `true` on success, `false` on failure 27 | */ 28 | func store(senderKey: Data, for address: SignalSenderKeyName, userRecord: Data?) -> Bool 29 | 30 | /** 31 | Returns a copy of the sender key record corresponding to the 32 | (groupId + senderId + deviceId) tuple. 33 | 34 | - parameter address: the (groupId + senderId + deviceId) tuple 35 | - returns: The sender key and optional user record, or nil on failure 36 | */ 37 | func loadSenderKey(for address: SignalSenderKeyName) -> (senderKey: Data, userRecord: Data?)? 38 | 39 | /** 40 | Function called to perform cleanup when the data store context is being 41 | destroyed. 42 | */ 43 | func destroy() 44 | } 45 | 46 | public extension SenderKeyStore { 47 | 48 | /** 49 | Function called to perform cleanup when the data store context is being 50 | destroyed. 51 | */ 52 | func destroy() { 53 | // Empty implementation to make this function 'optional' 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Stores/PreKeyStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PreKeyStore.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 15.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | The `PreKeyStore`protocol must be adopted to provide the storage for pre keys. 14 | */ 15 | public protocol PreKeyStore { 16 | 17 | /** 18 | Load a local serialized PreKey record. 19 | - parameter preKey: The ID of the local serialized PreKey record 20 | - returns: The record, if found, or nil 21 | */ 22 | func load(preKey: UInt32) -> Data? 23 | 24 | /** 25 | Store a local serialized PreKey record. 26 | - parameter preKey: The serialized record 27 | - parameter id: The ID of the PreKey record to store. 28 | - returns: `true` on success, `false` on failure 29 | */ 30 | func store(preKey: Data, for id: UInt32) -> Bool 31 | 32 | /** 33 | Determine whether there is a committed PreKey record matching the 34 | provided ID. 35 | - parameter preKey: A PreKey record ID. 36 | - returns: `true` if the store has a record for the PreKey ID, `false` otherwise 37 | */ 38 | func contains(preKey: UInt32) -> Bool 39 | 40 | /** 41 | Delete a PreKey record from local storage. 42 | - parameter preKey: The ID of the PreKey record to remove. 43 | - returns: `true` on success, `false` on failure 44 | */ 45 | func remove(preKey: UInt32) -> Bool 46 | 47 | /** 48 | Function called to perform cleanup when the data store context is being 49 | destroyed. 50 | */ 51 | func destroy() 52 | } 53 | 54 | public extension PreKeyStore { 55 | 56 | /** 57 | Function called to perform cleanup when the data store context is being 58 | destroyed. 59 | */ 60 | func destroy() { 61 | // Empty implementation to make this function 'optional' 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/generalized/gen_x.h: -------------------------------------------------------------------------------- 1 | #ifndef __GEN_X_H 2 | #define __GEN_X_H 3 | 4 | int generalized_xeddsa_25519_sign(unsigned char* signature_out, /* 64 bytes */ 5 | const unsigned char* x25519_privkey_scalar, /* 32 bytes */ 6 | const unsigned char* msg, const unsigned long msg_len, 7 | const unsigned char* random, /* 32 bytes */ 8 | const unsigned char* customization_label, 9 | const unsigned long customization_label_len); 10 | 11 | int generalized_xeddsa_25519_verify( 12 | const unsigned char* signature, /* 64 bytes */ 13 | const unsigned char* x25519_pubkey_bytes, /* 32 bytes */ 14 | const unsigned char* msg, 15 | const unsigned long msg_len, 16 | const unsigned char* customization_label, 17 | const unsigned long customization_label_len); 18 | 19 | int generalized_xveddsa_25519_sign( 20 | unsigned char* signature_out, /* 96 bytes */ 21 | const unsigned char* x25519_privkey_scalar, /* 32 bytes */ 22 | const unsigned char* msg, 23 | const unsigned long msg_len, 24 | const unsigned char* random, /* 32 bytes */ 25 | const unsigned char* customization_label, 26 | const unsigned long customization_label_len); 27 | 28 | int generalized_xveddsa_25519_verify( 29 | unsigned char* vrf_out, /* 32 bytes */ 30 | const unsigned char* signature, /* 96 bytes */ 31 | const unsigned char* x25519_pubkey_bytes, /* 32 bytes */ 32 | const unsigned char* msg, 33 | const unsigned long msg_len, 34 | const unsigned char* customization_label, 35 | const unsigned long customization_label_len); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Stores/SignedPreKeyStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SignedPreKeyStore.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 15.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | The `SignedPreKeyStore`protocol must be adopted to provide the storage for signed pre keys. 14 | */ 15 | public protocol SignedPreKeyStore { 16 | 17 | 18 | /** 19 | Load a local serialized signed PreKey record. 20 | - parameter signedPreKey: The ID of the local signed PreKey record 21 | - returns: The record, if found, or nil 22 | */ 23 | func load(signedPreKey: UInt32) -> Data? 24 | 25 | /** 26 | Store a local serialized signed PreKey record. 27 | - parameter signedPreKey: The serialized record 28 | - parameter id: the Id of the signed PreKey record to store 29 | - returns: `true` on success, `false` on failure 30 | */ 31 | func store(signedPreKey: Data, for id: UInt32) -> Bool 32 | 33 | /** 34 | Determine whether there is a committed signed PreKey record matching 35 | the provided ID. 36 | - parameter singedPreKey: A signed PreKey record ID 37 | - returns: `true` if the store has a record for the signed PreKey ID, `false` otherwise 38 | */ 39 | func contains(signedPreKey: UInt32) -> Bool 40 | 41 | /** 42 | Delete a SignedPreKeyRecord from local storage. 43 | 44 | - parameter signedPreKey: The ID of the signed PreKey record to remove. 45 | - returns: `true` on success, `false` on failure 46 | */ 47 | func remove(signedPreKey: UInt32) -> Bool 48 | 49 | /** 50 | Function called to perform cleanup when the data store context is being 51 | destroyed. 52 | */ 53 | func destroy() 54 | } 55 | 56 | public extension SignedPreKeyStore { 57 | 58 | /** 59 | Function called to perform cleanup when the data store context is being 60 | destroyed. 61 | */ 62 | func destroy() { 63 | // Empty implementation to make this function 'optional' 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/nacl_sha512/hash.c: -------------------------------------------------------------------------------- 1 | /* 2 | 20080913 3 | D. J. Bernstein 4 | Public domain. 5 | */ 6 | 7 | #include 8 | typedef uint64_t uint64; 9 | 10 | extern int crypto_hashblocks_sha512(unsigned char *statebytes,const unsigned char *in,unsigned long long inlen); 11 | 12 | #define blocks crypto_hashblocks_sha512 13 | 14 | static const unsigned char iv[64] = { 15 | 0x6a,0x09,0xe6,0x67,0xf3,0xbc,0xc9,0x08, 16 | 0xbb,0x67,0xae,0x85,0x84,0xca,0xa7,0x3b, 17 | 0x3c,0x6e,0xf3,0x72,0xfe,0x94,0xf8,0x2b, 18 | 0xa5,0x4f,0xf5,0x3a,0x5f,0x1d,0x36,0xf1, 19 | 0x51,0x0e,0x52,0x7f,0xad,0xe6,0x82,0xd1, 20 | 0x9b,0x05,0x68,0x8c,0x2b,0x3e,0x6c,0x1f, 21 | 0x1f,0x83,0xd9,0xab,0xfb,0x41,0xbd,0x6b, 22 | 0x5b,0xe0,0xcd,0x19,0x13,0x7e,0x21,0x79 23 | } ; 24 | 25 | int crypto_hash_sha512(unsigned char *out,const unsigned char *in,unsigned long long inlen) 26 | { 27 | unsigned char h[64]; 28 | unsigned char padded[256]; 29 | unsigned long long i; 30 | unsigned long long bytes = inlen; 31 | 32 | for (i = 0;i < 64;++i) h[i] = iv[i]; 33 | 34 | blocks(h,in,inlen); 35 | in += inlen; 36 | inlen &= 127; 37 | in -= inlen; 38 | 39 | for (i = 0;i < inlen;++i) padded[i] = in[i]; 40 | padded[inlen] = 0x80; 41 | 42 | if (inlen < 112) { 43 | for (i = inlen + 1;i < 119;++i) padded[i] = 0; 44 | padded[119] = bytes >> 61; 45 | padded[120] = bytes >> 53; 46 | padded[121] = bytes >> 45; 47 | padded[122] = bytes >> 37; 48 | padded[123] = bytes >> 29; 49 | padded[124] = bytes >> 21; 50 | padded[125] = bytes >> 13; 51 | padded[126] = bytes >> 5; 52 | padded[127] = bytes << 3; 53 | blocks(h,padded,128); 54 | } else { 55 | for (i = inlen + 1;i < 247;++i) padded[i] = 0; 56 | padded[247] = bytes >> 61; 57 | padded[248] = bytes >> 53; 58 | padded[249] = bytes >> 45; 59 | padded[250] = bytes >> 37; 60 | padded[251] = bytes >> 29; 61 | padded[252] = bytes >> 21; 62 | padded[253] = bytes >> 13; 63 | padded[254] = bytes >> 5; 64 | padded[255] = bytes << 3; 65 | blocks(h,padded,256); 66 | } 67 | 68 | for (i = 0;i < 64;++i) out[i] = h[i]; 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe.h: -------------------------------------------------------------------------------- 1 | #ifndef FE_H 2 | #define FE_H 3 | 4 | #include "crypto_int32.h" 5 | 6 | typedef crypto_int32 fe[10]; 7 | 8 | /* 9 | fe means field element. 10 | Here the field is \Z/(2^255-19). 11 | An element t, entries t[0]...t[9], represents the integer 12 | t[0]+2^26 t[1]+2^51 t[2]+2^77 t[3]+2^102 t[4]+...+2^230 t[9]. 13 | Bounds on each t[i] vary depending on context. 14 | */ 15 | 16 | #define fe_frombytes crypto_sign_ed25519_ref10_fe_frombytes 17 | #define fe_tobytes crypto_sign_ed25519_ref10_fe_tobytes 18 | #define fe_copy crypto_sign_ed25519_ref10_fe_copy 19 | #define fe_isnonzero crypto_sign_ed25519_ref10_fe_isnonzero 20 | #define fe_isnegative crypto_sign_ed25519_ref10_fe_isnegative 21 | #define fe_0 crypto_sign_ed25519_ref10_fe_0 22 | #define fe_1 crypto_sign_ed25519_ref10_fe_1 23 | #define fe_cswap crypto_sign_ed25519_ref10_fe_cswap 24 | #define fe_cmov crypto_sign_ed25519_ref10_fe_cmov 25 | #define fe_add crypto_sign_ed25519_ref10_fe_add 26 | #define fe_sub crypto_sign_ed25519_ref10_fe_sub 27 | #define fe_neg crypto_sign_ed25519_ref10_fe_neg 28 | #define fe_mul crypto_sign_ed25519_ref10_fe_mul 29 | #define fe_sq crypto_sign_ed25519_ref10_fe_sq 30 | #define fe_sq2 crypto_sign_ed25519_ref10_fe_sq2 31 | #define fe_mul121666 crypto_sign_ed25519_ref10_fe_mul121666 32 | #define fe_invert crypto_sign_ed25519_ref10_fe_invert 33 | #define fe_pow22523 crypto_sign_ed25519_ref10_fe_pow22523 34 | 35 | extern void fe_frombytes(fe,const unsigned char *); 36 | extern void fe_tobytes(unsigned char *,const fe); 37 | 38 | extern void fe_copy(fe,const fe); 39 | extern int fe_isnonzero(const fe); 40 | extern int fe_isnegative(const fe); 41 | extern void fe_0(fe); 42 | extern void fe_1(fe); 43 | extern void fe_cswap(fe,fe,unsigned int); 44 | extern void fe_cmov(fe,const fe,unsigned int); 45 | 46 | extern void fe_add(fe,const fe,const fe); 47 | extern void fe_sub(fe,const fe,const fe); 48 | extern void fe_neg(fe,const fe); 49 | extern void fe_mul(fe,const fe,const fe); 50 | extern void fe_sq(fe,const fe); 51 | extern void fe_sq2(fe,const fe); 52 | extern void fe_mul121666(fe,const fe); 53 | extern void fe_invert(fe,const fe); 54 | extern void fe_pow22523(fe,const fe); 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/vpool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006, 2008 Alexey Vatchenko 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /* 18 | * VPool: implementation of pool of data with a variable size. 19 | */ 20 | #ifndef _VPOOL_H_ 21 | #define _VPOOL_H_ 22 | 23 | #include 24 | #include 25 | 26 | struct vpool { 27 | void *v_basebuf; /* pointer returned by (re|m)alloc() */ 28 | void *v_buf; /* actual data starts here */ 29 | size_t v_off; 30 | size_t v_size; 31 | 32 | size_t v_blksize; 33 | size_t v_limit; 34 | int v_lasterr; 35 | }; 36 | 37 | enum vpool_trunc {VPOOL_EXCLUDE, VPOOL_INCLUDE}; 38 | #define VPOOL_TAIL UINT_MAX 39 | 40 | void vpool_init(struct vpool *pool, size_t blksize, size_t limit); 41 | void vpool_final(struct vpool *pool); 42 | 43 | void vpool_reset(struct vpool *pool); 44 | void vpool_wipe(struct vpool *pool); 45 | 46 | void * vpool_insert(struct vpool *pool, 47 | size_t where, void *data, size_t datsize); 48 | void * vpool_expand(struct vpool *pool, size_t where, size_t size); 49 | 50 | int vpool_truncate(struct vpool *pool, 51 | size_t where, size_t size, enum vpool_trunc how); 52 | 53 | #define vpool_is_empty(pool) ((pool)->v_off == 0) 54 | #define vpool_get_buf(pool) ((pool)->v_buf) 55 | #define vpool_get_length(pool) ((pool)->v_off) 56 | #define vpool_get_error(pool) ((pool)->v_lasterr) 57 | 58 | void vpool_export(struct vpool *pool, void **buf, size_t *size); 59 | 60 | #endif /* !_VPOOL_H_ */ 61 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/nacl_includes/crypto_sign_edwards25519sha512batch.h: -------------------------------------------------------------------------------- 1 | #ifndef crypto_sign_edwards25519sha512batch_H 2 | #define crypto_sign_edwards25519sha512batch_H 3 | 4 | #define crypto_sign_edwards25519sha512batch_ref10_SECRETKEYBYTES 64 5 | #define crypto_sign_edwards25519sha512batch_ref10_PUBLICKEYBYTES 32 6 | #define crypto_sign_edwards25519sha512batch_ref10_BYTES 64 7 | #ifdef __cplusplus 8 | #include 9 | extern std::string crypto_sign_edwards25519sha512batch_ref10(const std::string &,const std::string &); 10 | extern std::string crypto_sign_edwards25519sha512batch_ref10_open(const std::string &,const std::string &); 11 | extern std::string crypto_sign_edwards25519sha512batch_ref10_keypair(std::string *); 12 | extern "C" { 13 | #endif 14 | extern int crypto_sign_edwards25519sha512batch_ref10(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *); 15 | extern int crypto_sign_edwards25519sha512batch_ref10_open(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *); 16 | extern int crypto_sign_edwards25519sha512batch_ref10_keypair(unsigned char *,unsigned char *); 17 | #ifdef __cplusplus 18 | } 19 | #endif 20 | 21 | #define crypto_sign_edwards25519sha512batch crypto_sign_edwards25519sha512batch_ref10 22 | #define crypto_sign_edwards25519sha512batch_open crypto_sign_edwards25519sha512batch_ref10_open 23 | #define crypto_sign_edwards25519sha512batch_keypair crypto_sign_edwards25519sha512batch_ref10_keypair 24 | #define crypto_sign_edwards25519sha512batch_BYTES crypto_sign_edwards25519sha512batch_ref10_BYTES 25 | #define crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES crypto_sign_edwards25519sha512batch_ref10_PUBLICKEYBYTES 26 | #define crypto_sign_edwards25519sha512batch_SECRETKEYBYTES crypto_sign_edwards25519sha512batch_ref10_SECRETKEYBYTES 27 | #define crypto_sign_edwards25519sha512batch_IMPLEMENTATION "crypto_sign/edwards25519sha512batch/ref10" 28 | #ifndef crypto_sign_edwards25519sha512batch_ref10_VERSION 29 | #define crypto_sign_edwards25519sha512batch_ref10_VERSION "-" 30 | #endif 31 | #define crypto_sign_edwards25519sha512batch_VERSION crypto_sign_edwards25519sha512batch_ref10_VERSION 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /libsignal-protocol-swiftTests/GroupCipherTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // GroupCipherTests.swift 3 | // libsignal-protocol-swiftTests 4 | // 5 | // Created by User on 19.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | import SignalProtocol 11 | 12 | private let groupSender = SignalSenderKeyName(groupId: "nihilist history reading group", 13 | sender: SignalAddress(name: "+14150001111", deviceId: 1)) 14 | 15 | class GroupCipherTests: XCTestCase { 16 | 17 | func testExample() { 18 | 19 | guard let aliceStore = setupStore(makeKeys: false), 20 | let bobStore = setupStore(makeKeys: false) else { 21 | XCTFail("Could not create store") 22 | return 23 | } 24 | 25 | let aliceBuilder = GroupSessionBuilder(in: aliceStore) 26 | let bobBuilder = GroupSessionBuilder(in: bobStore) 27 | 28 | let aliceCipher = GroupCipher(for: groupSender, in: aliceStore) 29 | let bobCipher = GroupCipher(for: groupSender, in: bobStore) 30 | 31 | let distributionMessage: CiphertextMessage 32 | do { 33 | distributionMessage = try aliceBuilder.createSession(for: groupSender) 34 | } catch { 35 | XCTFail("Could not create distribution message") 36 | return 37 | } 38 | 39 | do { 40 | try bobBuilder.process(senderKeyDistributionMessage: distributionMessage, from: groupSender) 41 | } catch { 42 | XCTFail("Could not process distribution message \(error)") 43 | return 44 | } 45 | 46 | let message = "smert ze smert".data(using: .utf8)! 47 | let encryptedMessage: CiphertextMessage 48 | do { 49 | encryptedMessage = try aliceCipher.encrypt(message) 50 | } catch { 51 | XCTFail("Could no encrypt message \(error)") 52 | return 53 | } 54 | 55 | let decryptedMessage: Data 56 | do { 57 | decryptedMessage = try bobCipher.decrypt(encryptedMessage) 58 | } catch { 59 | XCTFail("Could no decrypt message") 60 | return 61 | } 62 | 63 | XCTAssert(decryptedMessage == message, "Messages not equal") 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/ge_montx_to_p3.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | #include "ge.h" 3 | #include "assert.h" 4 | #include "crypto_additions.h" 5 | #include "utility.h" 6 | 7 | /* sqrt(-(A+2)) */ 8 | static unsigned char A_bytes[32] = { 9 | 0x06, 0x7e, 0x45, 0xff, 0xaa, 0x04, 0x6e, 0xcc, 10 | 0x82, 0x1a, 0x7d, 0x4b, 0xd1, 0xd3, 0xa1, 0xc5, 11 | 0x7e, 0x4f, 0xfc, 0x03, 0xdc, 0x08, 0x7b, 0xd2, 12 | 0xbb, 0x06, 0xa0, 0x60, 0xf4, 0xed, 0x26, 0x0f 13 | }; 14 | 15 | void ge_montx_to_p3(ge_p3* p, const fe u, const unsigned char ed_sign_bit) 16 | { 17 | fe x, y, A, v, v2, iv, nx; 18 | 19 | fe_frombytes(A, A_bytes); 20 | 21 | /* given u, recover edwards y */ 22 | /* given u, recover v */ 23 | /* given u and v, recover edwards x */ 24 | 25 | fe_montx_to_edy(y, u); /* y = (u - 1) / (u + 1) */ 26 | 27 | fe_mont_rhs(v2, u); /* v^2 = u(u^2 + Au + 1) */ 28 | fe_sqrt(v, v2); /* v = sqrt(v^2) */ 29 | 30 | fe_mul(x, u, A); /* x = u * sqrt(-(A+2)) */ 31 | fe_invert(iv, v); /* 1/v */ 32 | fe_mul(x, x, iv); /* x = (u/v) * sqrt(-(A+2)) */ 33 | 34 | fe_neg(nx, x); /* negate x to match sign bit */ 35 | fe_cmov(x, nx, fe_isnegative(x) ^ ed_sign_bit); 36 | 37 | fe_copy(p->X, x); 38 | fe_copy(p->Y, y); 39 | fe_1(p->Z); 40 | fe_mul(p->T, p->X, p->Y); 41 | 42 | /* POSTCONDITION: check that p->X and p->Y satisfy the Ed curve equation */ 43 | /* -x^2 + y^2 = 1 + dx^2y^2 */ 44 | #ifndef NDEBUG 45 | { 46 | fe one, d, x2, y2, x2y2, dx2y2; 47 | 48 | unsigned char dbytes[32] = { 49 | 0xa3, 0x78, 0x59, 0x13, 0xca, 0x4d, 0xeb, 0x75, 50 | 0xab, 0xd8, 0x41, 0x41, 0x4d, 0x0a, 0x70, 0x00, 51 | 0x98, 0xe8, 0x79, 0x77, 0x79, 0x40, 0xc7, 0x8c, 52 | 0x73, 0xfe, 0x6f, 0x2b, 0xee, 0x6c, 0x03, 0x52 53 | }; 54 | 55 | fe_frombytes(d, dbytes); 56 | fe_1(one); 57 | fe_sq(x2, p->X); /* x^2 */ 58 | fe_sq(y2, p->Y); /* y^2 */ 59 | 60 | fe_mul(dx2y2, x2, y2); /* x^2y^2 */ 61 | fe_mul(dx2y2, dx2y2, d); /* dx^2y^2 */ 62 | fe_add(dx2y2, dx2y2, one); /* dx^2y^2 + 1 */ 63 | 64 | fe_neg(x2y2, x2); /* -x^2 */ 65 | fe_add(x2y2, x2y2, y2); /* -x^2 + y^2 */ 66 | 67 | assert(fe_isequal(x2y2, dx2y2)); 68 | } 69 | #endif 70 | } 71 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_madd.h: -------------------------------------------------------------------------------- 1 | 2 | /* qhasm: enter ge_madd */ 3 | 4 | /* qhasm: fe X1 */ 5 | 6 | /* qhasm: fe Y1 */ 7 | 8 | /* qhasm: fe Z1 */ 9 | 10 | /* qhasm: fe T1 */ 11 | 12 | /* qhasm: fe ypx2 */ 13 | 14 | /* qhasm: fe ymx2 */ 15 | 16 | /* qhasm: fe xy2d2 */ 17 | 18 | /* qhasm: fe X3 */ 19 | 20 | /* qhasm: fe Y3 */ 21 | 22 | /* qhasm: fe Z3 */ 23 | 24 | /* qhasm: fe T3 */ 25 | 26 | /* qhasm: fe YpX1 */ 27 | 28 | /* qhasm: fe YmX1 */ 29 | 30 | /* qhasm: fe A */ 31 | 32 | /* qhasm: fe B */ 33 | 34 | /* qhasm: fe C */ 35 | 36 | /* qhasm: fe D */ 37 | 38 | /* qhasm: YpX1 = Y1+X1 */ 39 | /* asm 1: fe_add(>YpX1=fe#1,YpX1=r->X,Y,X); */ 41 | fe_add(r->X,p->Y,p->X); 42 | 43 | /* qhasm: YmX1 = Y1-X1 */ 44 | /* asm 1: fe_sub(>YmX1=fe#2,YmX1=r->Y,Y,X); */ 46 | fe_sub(r->Y,p->Y,p->X); 47 | 48 | /* qhasm: A = YpX1*ypx2 */ 49 | /* asm 1: fe_mul(>A=fe#3,A=r->Z,X,yplusx); */ 51 | fe_mul(r->Z,r->X,q->yplusx); 52 | 53 | /* qhasm: B = YmX1*ymx2 */ 54 | /* asm 1: fe_mul(>B=fe#2,B=r->Y,Y,yminusx); */ 56 | fe_mul(r->Y,r->Y,q->yminusx); 57 | 58 | /* qhasm: C = xy2d2*T1 */ 59 | /* asm 1: fe_mul(>C=fe#4,C=r->T,xy2d,T); */ 61 | fe_mul(r->T,q->xy2d,p->T); 62 | 63 | /* qhasm: D = 2*Z1 */ 64 | /* asm 1: fe_add(>D=fe#5,D=t0,Z,Z); */ 66 | fe_add(t0,p->Z,p->Z); 67 | 68 | /* qhasm: X3 = A-B */ 69 | /* asm 1: fe_sub(>X3=fe#1,X3=r->X,Z,Y); */ 71 | fe_sub(r->X,r->Z,r->Y); 72 | 73 | /* qhasm: Y3 = A+B */ 74 | /* asm 1: fe_add(>Y3=fe#2,Y3=r->Y,Z,Y); */ 76 | fe_add(r->Y,r->Z,r->Y); 77 | 78 | /* qhasm: Z3 = D+C */ 79 | /* asm 1: fe_add(>Z3=fe#3,Z3=r->Z,T); */ 81 | fe_add(r->Z,t0,r->T); 82 | 83 | /* qhasm: T3 = D-C */ 84 | /* asm 1: fe_sub(>T3=fe#4,T3=r->T,T); */ 86 | fe_sub(r->T,t0,r->T); 87 | 88 | /* qhasm: return */ 89 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_msub.h: -------------------------------------------------------------------------------- 1 | 2 | /* qhasm: enter ge_msub */ 3 | 4 | /* qhasm: fe X1 */ 5 | 6 | /* qhasm: fe Y1 */ 7 | 8 | /* qhasm: fe Z1 */ 9 | 10 | /* qhasm: fe T1 */ 11 | 12 | /* qhasm: fe ypx2 */ 13 | 14 | /* qhasm: fe ymx2 */ 15 | 16 | /* qhasm: fe xy2d2 */ 17 | 18 | /* qhasm: fe X3 */ 19 | 20 | /* qhasm: fe Y3 */ 21 | 22 | /* qhasm: fe Z3 */ 23 | 24 | /* qhasm: fe T3 */ 25 | 26 | /* qhasm: fe YpX1 */ 27 | 28 | /* qhasm: fe YmX1 */ 29 | 30 | /* qhasm: fe A */ 31 | 32 | /* qhasm: fe B */ 33 | 34 | /* qhasm: fe C */ 35 | 36 | /* qhasm: fe D */ 37 | 38 | /* qhasm: YpX1 = Y1+X1 */ 39 | /* asm 1: fe_add(>YpX1=fe#1,YpX1=r->X,Y,X); */ 41 | fe_add(r->X,p->Y,p->X); 42 | 43 | /* qhasm: YmX1 = Y1-X1 */ 44 | /* asm 1: fe_sub(>YmX1=fe#2,YmX1=r->Y,Y,X); */ 46 | fe_sub(r->Y,p->Y,p->X); 47 | 48 | /* qhasm: A = YpX1*ymx2 */ 49 | /* asm 1: fe_mul(>A=fe#3,A=r->Z,X,yminusx); */ 51 | fe_mul(r->Z,r->X,q->yminusx); 52 | 53 | /* qhasm: B = YmX1*ypx2 */ 54 | /* asm 1: fe_mul(>B=fe#2,B=r->Y,Y,yplusx); */ 56 | fe_mul(r->Y,r->Y,q->yplusx); 57 | 58 | /* qhasm: C = xy2d2*T1 */ 59 | /* asm 1: fe_mul(>C=fe#4,C=r->T,xy2d,T); */ 61 | fe_mul(r->T,q->xy2d,p->T); 62 | 63 | /* qhasm: D = 2*Z1 */ 64 | /* asm 1: fe_add(>D=fe#5,D=t0,Z,Z); */ 66 | fe_add(t0,p->Z,p->Z); 67 | 68 | /* qhasm: X3 = A-B */ 69 | /* asm 1: fe_sub(>X3=fe#1,X3=r->X,Z,Y); */ 71 | fe_sub(r->X,r->Z,r->Y); 72 | 73 | /* qhasm: Y3 = A+B */ 74 | /* asm 1: fe_add(>Y3=fe#2,Y3=r->Y,Z,Y); */ 76 | fe_add(r->Y,r->Z,r->Y); 77 | 78 | /* qhasm: Z3 = D-C */ 79 | /* asm 1: fe_sub(>Z3=fe#3,Z3=r->Z,T); */ 81 | fe_sub(r->Z,t0,r->T); 82 | 83 | /* qhasm: T3 = D+C */ 84 | /* asm 1: fe_add(>T3=fe#4,T3=r->T,T); */ 86 | fe_add(r->T,t0,r->T); 87 | 88 | /* qhasm: return */ 89 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/session_builder.h: -------------------------------------------------------------------------------- 1 | #ifndef SESSION_BUILDER_H 2 | #define SESSION_BUILDER_H 3 | 4 | #include 5 | #include "signal_protocol_types.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | /* 12 | * Session builder is responsible for setting up encrypted sessions. 13 | * Once a session has been established, session_cipher 14 | * can be used to encrypt/decrypt messages in that session. 15 | * 16 | * Sessions are built from one these different possible vectors: 17 | * - A session_pre_key_bundle retrieved from a server 18 | * - A pre_key_signal_message received from a client 19 | * 20 | * Sessions are constructed per Signal Protocol address 21 | * (recipient name + device ID tuple). Remote logical users are identified by 22 | * their recipient name, and each logical recipient can have multiple 23 | * physical devices. 24 | */ 25 | 26 | /** 27 | * Constructs a session builder. 28 | * 29 | * The store and global contexts must remain valid for the lifetime of the 30 | * session builder. 31 | * 32 | * When finished, free the returned instance by calling session_builder_free(). 33 | * 34 | * @param builder set to a freshly allocated session builder instance 35 | * @param store the signal_protocol_store_context to store all state information in 36 | * @param remote_address the address of the remote user to build a session with 37 | * @param global_context the global library context 38 | * @return 0 on success, or negative on failure 39 | */ 40 | int session_builder_create(session_builder **builder, 41 | signal_protocol_store_context *store, const signal_protocol_address *remote_address, 42 | signal_context *global_context); 43 | 44 | /** 45 | * Build a new session from a session_pre_key_bundle retrieved from a server. 46 | * 47 | * @param bundle A pre key bundle for the destination recipient, retrieved from a server. 48 | * @retval SG_SUCCESS Success 49 | * @retval SG_ERR_INVALID_KEY when the session_pre_key_bundle is badly formatted. 50 | * @retval SG_ERR_UNTRUSTED_IDENTITY when the sender's identity key is not trusted. 51 | */ 52 | int session_builder_process_pre_key_bundle(session_builder *builder, session_pre_key_bundle *bundle); 53 | 54 | void session_builder_free(session_builder *builder); 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif /* SESSION_BUILDER_H */ 61 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Misc/DataExtensions.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DataExtensions.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 15.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | extension Data { 13 | 14 | /** 15 | Allocate a signal buffer to assign to the input pointer and copy the data into it. 16 | - parameter pointer: The pointer to copy the data to. 17 | */ 18 | func copy(to pointer: UnsafeMutablePointer) { 19 | // Assign output pointer 20 | pointer.pointee = self.signalBuffer 21 | } 22 | 23 | /** 24 | Create data from a C pointer and length. 25 | - parameter pointer: The C pointer to the data. 26 | - parameter length: The number of bytes 27 | */ 28 | init(from pointer: UnsafePointer, length: Int) { 29 | let buffer = UnsafeBufferPointer(start: pointer, count: length) 30 | self.init(buffer: buffer) 31 | } 32 | 33 | /** 34 | Create data from a signal buffer. 35 | - parameter signalBuffer: The C pointer to the signal buffer. 36 | */ 37 | init(signalBuffer: OpaquePointer) { 38 | let length = signal_buffer_len(signalBuffer) 39 | let data = signal_buffer_data(signalBuffer)! 40 | self.init(from: data, length: length) 41 | } 42 | 43 | /// Convert the data to a signal buffer (needs to be freed manually) 44 | var signalBuffer: OpaquePointer { 45 | let buffer = signal_buffer_alloc(count)! 46 | let ptr = signal_buffer_data(buffer)! 47 | // Copy data 48 | self.withUnsafeBytes { ptr.assign(from: $0, count: self.count) } 49 | return buffer 50 | } 51 | 52 | /// Convert data to an `ec_public_key` pointer (needs to be freed manually) 53 | func publicKeyPointer() throws -> OpaquePointer { 54 | // Convert public key 55 | let buffer = self.signalBuffer 56 | defer { signal_buffer_free(buffer) } 57 | let length = signal_buffer_len(buffer) 58 | let data = signal_buffer_data(buffer)! 59 | var ptr: OpaquePointer? = nil 60 | let result = withUnsafeMutablePointer(to: &ptr) { 61 | curve_decode_point($0, data, length, Signal.context) 62 | } 63 | guard result == 0 else { throw SignalError(value: result) } 64 | return ptr! 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/session_record.h: -------------------------------------------------------------------------------- 1 | #ifndef SESSION_RECORD_H 2 | #define SESSION_RECORD_H 3 | 4 | #include 5 | #include 6 | #include "signal_protocol_types.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | int session_record_create(session_record **record, session_state *state, signal_context *global_context); 13 | int session_record_serialize(signal_buffer **buffer, const session_record *record); 14 | int session_record_deserialize(session_record **record, const uint8_t *data, size_t len, signal_context *global_context); 15 | int session_record_copy(session_record **record, session_record *other_record, signal_context *global_context); 16 | 17 | int session_record_has_session_state(session_record *record, uint32_t version, const ec_public_key *alice_base_key); 18 | session_state *session_record_get_state(session_record *record); 19 | void session_record_set_state(session_record *record, session_state *state); 20 | 21 | session_record_state_node *session_record_get_previous_states_head(const session_record *record); 22 | session_state *session_record_get_previous_states_element(const session_record_state_node *node); 23 | session_record_state_node *session_record_get_previous_states_next(const session_record_state_node *node); 24 | 25 | /** 26 | * Removes the specified node in the previous states list. 27 | * @param node the node to remove 28 | * @return the node immediately following the removed node, or null if at the end of the list 29 | */ 30 | session_record_state_node *session_record_get_previous_states_remove(session_record *record, session_record_state_node *node); 31 | 32 | int session_record_is_fresh(session_record *record); 33 | 34 | /** 35 | * Move the current session_state into the list of "previous" session states, 36 | * and replace the current session_state with a fresh reset instance. 37 | * 38 | * @return 0 on success, negative on failure 39 | */ 40 | int session_record_archive_current_state(session_record *record); 41 | 42 | int session_record_promote_state(session_record *record, session_state *promoted_state); 43 | 44 | signal_buffer *session_record_get_user_record(const session_record *record); 45 | void session_record_set_user_record(session_record *record, signal_buffer *user_record); 46 | 47 | void session_record_destroy(signal_type_base *type); 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | 53 | #endif /* SESSION_RECORD_H */ 54 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Sessions/SessionBuilder.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SessionBuilder.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 16.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | Session builder is responsible for setting up encrypted sessions. 14 | Once a session has been established, session_cipher 15 | can be used to encrypt/decrypt messages in that session. 16 | 17 | Sessions are built from one these different possible vectors: 18 | - A session_pre_key_bundle retrieved from a server 19 | - A pre_key_signal_message received from a client 20 | 21 | Sessions are constructed per Signal Protocol address 22 | (recipient name + device ID tuple). Remote logical users are identified by 23 | their recipient name, and each logical recipient can have multiple 24 | physical devices. 25 | */ 26 | public final class SessionBuilder { 27 | 28 | let remoteAddress: SignalAddress 29 | 30 | let store: SignalStore 31 | 32 | /** 33 | Constructs a session builder. 34 | - parameter remoteAddress: The address of the remote user to build a session with 35 | - parameter store: The store for the keys and state information 36 | */ 37 | public init(for remoteAddress: SignalAddress, in store: SignalStore) { 38 | self.remoteAddress = remoteAddress 39 | self.store = store 40 | } 41 | 42 | /** 43 | Build a new session from a session_pre_key_bundle retrieved from a server. 44 | 45 | - note: Possible errors: 46 | - `untrustedIdentity` if the sender's identity key is not trusted 47 | - parameter preKeyBundle: A pre key bundle for the destination recipient, retrieved from a server. 48 | - throws: Errors of type `signalError` 49 | */ 50 | public func process(preKeyBundle: SessionPreKeyBundle) throws { 51 | var builder: OpaquePointer? = nil 52 | var result = withUnsafeMutablePointer(to: &builder) { 53 | session_builder_create($0, store.storeContext, remoteAddress.signalAddress, Signal.context) 54 | } 55 | 56 | guard result == 0 else { throw SignalError(value: result) } 57 | defer { session_builder_free(builder) } 58 | 59 | let bundle = try preKeyBundle.pointer() 60 | defer { session_pre_key_bundle_destroy(bundle) } 61 | 62 | result = session_builder_process_pre_key_bundle(builder, bundle) 63 | guard result == 0 else { throw SignalError(value: result) } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_add.h: -------------------------------------------------------------------------------- 1 | 2 | /* qhasm: enter ge_add */ 3 | 4 | /* qhasm: fe X1 */ 5 | 6 | /* qhasm: fe Y1 */ 7 | 8 | /* qhasm: fe Z1 */ 9 | 10 | /* qhasm: fe Z2 */ 11 | 12 | /* qhasm: fe T1 */ 13 | 14 | /* qhasm: fe ZZ */ 15 | 16 | /* qhasm: fe YpX2 */ 17 | 18 | /* qhasm: fe YmX2 */ 19 | 20 | /* qhasm: fe T2d2 */ 21 | 22 | /* qhasm: fe X3 */ 23 | 24 | /* qhasm: fe Y3 */ 25 | 26 | /* qhasm: fe Z3 */ 27 | 28 | /* qhasm: fe T3 */ 29 | 30 | /* qhasm: fe YpX1 */ 31 | 32 | /* qhasm: fe YmX1 */ 33 | 34 | /* qhasm: fe A */ 35 | 36 | /* qhasm: fe B */ 37 | 38 | /* qhasm: fe C */ 39 | 40 | /* qhasm: fe D */ 41 | 42 | /* qhasm: YpX1 = Y1+X1 */ 43 | /* asm 1: fe_add(>YpX1=fe#1,YpX1=r->X,Y,X); */ 45 | fe_add(r->X,p->Y,p->X); 46 | 47 | /* qhasm: YmX1 = Y1-X1 */ 48 | /* asm 1: fe_sub(>YmX1=fe#2,YmX1=r->Y,Y,X); */ 50 | fe_sub(r->Y,p->Y,p->X); 51 | 52 | /* qhasm: A = YpX1*YpX2 */ 53 | /* asm 1: fe_mul(>A=fe#3,A=r->Z,X,YplusX); */ 55 | fe_mul(r->Z,r->X,q->YplusX); 56 | 57 | /* qhasm: B = YmX1*YmX2 */ 58 | /* asm 1: fe_mul(>B=fe#2,B=r->Y,Y,YminusX); */ 60 | fe_mul(r->Y,r->Y,q->YminusX); 61 | 62 | /* qhasm: C = T2d2*T1 */ 63 | /* asm 1: fe_mul(>C=fe#4,C=r->T,T2d,T); */ 65 | fe_mul(r->T,q->T2d,p->T); 66 | 67 | /* qhasm: ZZ = Z1*Z2 */ 68 | /* asm 1: fe_mul(>ZZ=fe#1,ZZ=r->X,Z,Z); */ 70 | fe_mul(r->X,p->Z,q->Z); 71 | 72 | /* qhasm: D = 2*ZZ */ 73 | /* asm 1: fe_add(>D=fe#5,D=t0,X,X); */ 75 | fe_add(t0,r->X,r->X); 76 | 77 | /* qhasm: X3 = A-B */ 78 | /* asm 1: fe_sub(>X3=fe#1,X3=r->X,Z,Y); */ 80 | fe_sub(r->X,r->Z,r->Y); 81 | 82 | /* qhasm: Y3 = A+B */ 83 | /* asm 1: fe_add(>Y3=fe#2,Y3=r->Y,Z,Y); */ 85 | fe_add(r->Y,r->Z,r->Y); 86 | 87 | /* qhasm: Z3 = D+C */ 88 | /* asm 1: fe_add(>Z3=fe#3,Z3=r->Z,T); */ 90 | fe_add(r->Z,t0,r->T); 91 | 92 | /* qhasm: T3 = D-C */ 93 | /* asm 1: fe_sub(>T3=fe#4,T3=r->T,T); */ 95 | fe_sub(r->T,t0,r->T); 96 | 97 | /* qhasm: return */ 98 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_sub.h: -------------------------------------------------------------------------------- 1 | 2 | /* qhasm: enter ge_sub */ 3 | 4 | /* qhasm: fe X1 */ 5 | 6 | /* qhasm: fe Y1 */ 7 | 8 | /* qhasm: fe Z1 */ 9 | 10 | /* qhasm: fe Z2 */ 11 | 12 | /* qhasm: fe T1 */ 13 | 14 | /* qhasm: fe ZZ */ 15 | 16 | /* qhasm: fe YpX2 */ 17 | 18 | /* qhasm: fe YmX2 */ 19 | 20 | /* qhasm: fe T2d2 */ 21 | 22 | /* qhasm: fe X3 */ 23 | 24 | /* qhasm: fe Y3 */ 25 | 26 | /* qhasm: fe Z3 */ 27 | 28 | /* qhasm: fe T3 */ 29 | 30 | /* qhasm: fe YpX1 */ 31 | 32 | /* qhasm: fe YmX1 */ 33 | 34 | /* qhasm: fe A */ 35 | 36 | /* qhasm: fe B */ 37 | 38 | /* qhasm: fe C */ 39 | 40 | /* qhasm: fe D */ 41 | 42 | /* qhasm: YpX1 = Y1+X1 */ 43 | /* asm 1: fe_add(>YpX1=fe#1,YpX1=r->X,Y,X); */ 45 | fe_add(r->X,p->Y,p->X); 46 | 47 | /* qhasm: YmX1 = Y1-X1 */ 48 | /* asm 1: fe_sub(>YmX1=fe#2,YmX1=r->Y,Y,X); */ 50 | fe_sub(r->Y,p->Y,p->X); 51 | 52 | /* qhasm: A = YpX1*YmX2 */ 53 | /* asm 1: fe_mul(>A=fe#3,A=r->Z,X,YminusX); */ 55 | fe_mul(r->Z,r->X,q->YminusX); 56 | 57 | /* qhasm: B = YmX1*YpX2 */ 58 | /* asm 1: fe_mul(>B=fe#2,B=r->Y,Y,YplusX); */ 60 | fe_mul(r->Y,r->Y,q->YplusX); 61 | 62 | /* qhasm: C = T2d2*T1 */ 63 | /* asm 1: fe_mul(>C=fe#4,C=r->T,T2d,T); */ 65 | fe_mul(r->T,q->T2d,p->T); 66 | 67 | /* qhasm: ZZ = Z1*Z2 */ 68 | /* asm 1: fe_mul(>ZZ=fe#1,ZZ=r->X,Z,Z); */ 70 | fe_mul(r->X,p->Z,q->Z); 71 | 72 | /* qhasm: D = 2*ZZ */ 73 | /* asm 1: fe_add(>D=fe#5,D=t0,X,X); */ 75 | fe_add(t0,r->X,r->X); 76 | 77 | /* qhasm: X3 = A-B */ 78 | /* asm 1: fe_sub(>X3=fe#1,X3=r->X,Z,Y); */ 80 | fe_sub(r->X,r->Z,r->Y); 81 | 82 | /* qhasm: Y3 = A+B */ 83 | /* asm 1: fe_add(>Y3=fe#2,Y3=r->Y,Z,Y); */ 85 | fe_add(r->Y,r->Z,r->Y); 86 | 87 | /* qhasm: Z3 = D-C */ 88 | /* asm 1: fe_sub(>Z3=fe#3,Z3=r->Z,T); */ 90 | fe_sub(r->Z,t0,r->T); 91 | 92 | /* qhasm: T3 = D+C */ 93 | /* asm 1: fe_add(>T3=fe#4,T3=r->T,T); */ 95 | fe_add(r->T,t0,r->T); 96 | 97 | /* qhasm: return */ 98 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/base2.h: -------------------------------------------------------------------------------- 1 | { 2 | { 25967493,-14356035,29566456,3660896,-12694345,4014787,27544626,-11754271,-6079156,2047605 }, 3 | { -12545711,934262,-2722910,3049990,-727428,9406986,12720692,5043384,19500929,-15469378 }, 4 | { -8738181,4489570,9688441,-14785194,10184609,-12363380,29287919,11864899,-24514362,-4438546 }, 5 | }, 6 | { 7 | { 15636291,-9688557,24204773,-7912398,616977,-16685262,27787600,-14772189,28944400,-1550024 }, 8 | { 16568933,4717097,-11556148,-1102322,15682896,-11807043,16354577,-11775962,7689662,11199574 }, 9 | { 30464156,-5976125,-11779434,-15670865,23220365,15915852,7512774,10017326,-17749093,-9920357 }, 10 | }, 11 | { 12 | { 10861363,11473154,27284546,1981175,-30064349,12577861,32867885,14515107,-15438304,10819380 }, 13 | { 4708026,6336745,20377586,9066809,-11272109,6594696,-25653668,12483688,-12668491,5581306 }, 14 | { 19563160,16186464,-29386857,4097519,10237984,-4348115,28542350,13850243,-23678021,-15815942 }, 15 | }, 16 | { 17 | { 5153746,9909285,1723747,-2777874,30523605,5516873,19480852,5230134,-23952439,-15175766 }, 18 | { -30269007,-3463509,7665486,10083793,28475525,1649722,20654025,16520125,30598449,7715701 }, 19 | { 28881845,14381568,9657904,3680757,-20181635,7843316,-31400660,1370708,29794553,-1409300 }, 20 | }, 21 | { 22 | { -22518993,-6692182,14201702,-8745502,-23510406,8844726,18474211,-1361450,-13062696,13821877 }, 23 | { -6455177,-7839871,3374702,-4740862,-27098617,-10571707,31655028,-7212327,18853322,-14220951 }, 24 | { 4566830,-12963868,-28974889,-12240689,-7602672,-2830569,-8514358,-10431137,2207753,-3209784 }, 25 | }, 26 | { 27 | { -25154831,-4185821,29681144,7868801,-6854661,-9423865,-12437364,-663000,-31111463,-16132436 }, 28 | { 25576264,-2703214,7349804,-11814844,16472782,9300885,3844789,15725684,171356,6466918 }, 29 | { 23103977,13316479,9739013,-16149481,817875,-15038942,8965339,-14088058,-30714912,16193877 }, 30 | }, 31 | { 32 | { -33521811,3180713,-2394130,14003687,-16903474,-16270840,17238398,4729455,-18074513,9256800 }, 33 | { -25182317,-4174131,32336398,5036987,-21236817,11360617,22616405,9761698,-19827198,630305 }, 34 | { -13720693,2639453,-24237460,-7406481,9494427,-5774029,-6554551,-15960994,-2449256,-14291300 }, 35 | }, 36 | { 37 | { -3151181,-5046075,9282714,6866145,-31907062,-863023,-18940575,15033784,25105118,-7894876 }, 38 | { -24326370,15950226,-31801215,-14592823,-11662737,-5090925,1573892,-2625887,2198790,-15804619 }, 39 | { -3099351,10324967,-2241613,7453183,-5446979,-2735503,-13812022,-16236442,-32461234,-12290683 }, 40 | }, 41 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/elligator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "fe.h" 3 | #include "ge.h" 4 | #include "crypto_uint32.h" 5 | #include "crypto_hash_sha512.h" 6 | #include "crypto_additions.h" 7 | 8 | unsigned int legendre_is_nonsquare(fe in) 9 | { 10 | fe temp; 11 | unsigned char bytes[32]; 12 | fe_pow22523(temp, in); /* temp = in^((q-5)/8) */ 13 | fe_sq(temp, temp); /* in^((q-5)/4) */ 14 | fe_sq(temp, temp); /* in^((q-5)/2) */ 15 | fe_mul(temp, temp, in); /* in^((q-3)/2) */ 16 | fe_mul(temp, temp, in); /* in^((q-1)/2) */ 17 | 18 | /* temp is now the Legendre symbol: 19 | * 1 = square 20 | * 0 = input is zero 21 | * -1 = nonsquare 22 | */ 23 | fe_tobytes(bytes, temp); 24 | return 1 & bytes[31]; 25 | } 26 | 27 | void elligator(fe u, const fe r) 28 | { 29 | /* r = input 30 | * x = -A/(1+2r^2) # 2 is nonsquare 31 | * e = (x^3 + Ax^2 + x)^((q-1)/2) # legendre symbol 32 | * if e == 1 (square) or e == 0 (because x == 0 and 2r^2 + 1 == 0) 33 | * u = x 34 | * if e == -1 (nonsquare) 35 | * u = -x - A 36 | */ 37 | fe A, one, twor2, twor2plus1, twor2plus1inv; 38 | fe x, e, Atemp, uneg; 39 | unsigned int nonsquare; 40 | 41 | fe_1(one); 42 | fe_0(A); 43 | A[0] = 486662; /* A = 486662 */ 44 | 45 | fe_sq2(twor2, r); /* 2r^2 */ 46 | fe_add(twor2plus1, twor2, one); /* 1+2r^2 */ 47 | fe_invert(twor2plus1inv, twor2plus1); /* 1/(1+2r^2) */ 48 | fe_mul(x, twor2plus1inv, A); /* A/(1+2r^2) */ 49 | fe_neg(x, x); /* x = -A/(1+2r^2) */ 50 | 51 | fe_mont_rhs(e, x); /* e = x^3 + Ax^2 + x */ 52 | nonsquare = legendre_is_nonsquare(e); 53 | 54 | fe_0(Atemp); 55 | fe_cmov(Atemp, A, nonsquare); /* 0, or A if nonsquare */ 56 | fe_add(u, x, Atemp); /* x, or x+A if nonsquare */ 57 | fe_neg(uneg, u); /* -x, or -x-A if nonsquare */ 58 | fe_cmov(u, uneg, nonsquare); /* x, or -x-A if nonsquare */ 59 | } 60 | 61 | void hash_to_point(ge_p3* p, const unsigned char* in, const unsigned long in_len) 62 | { 63 | unsigned char hash[64]; 64 | fe h, u; 65 | unsigned char sign_bit; 66 | ge_p3 p3; 67 | 68 | crypto_hash_sha512(hash, in, in_len); 69 | 70 | /* take the high bit as Edwards sign bit */ 71 | sign_bit = (hash[31] & 0x80) >> 7; 72 | hash[31] &= 0x7F; 73 | fe_frombytes(h, hash); 74 | elligator(u, h); 75 | 76 | ge_montx_to_p3(&p3, u, sign_bit); 77 | ge_scalarmult_cofactor(p, &p3); 78 | } 79 | 80 | 81 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Sessions/SessionPreKey.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SessionPreKey.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 15.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | A session pre key is uploaded to the server and 14 | used as part of the pre key bundle to establish new sessions. 15 | */ 16 | public struct SessionPreKey { 17 | 18 | /// The pre key id 19 | public let id: UInt32 20 | 21 | /// The key pair of the pre key 22 | public let keyPair: KeyPair 23 | 24 | /** 25 | Create a pre key from serialized data 26 | - parameter pointer: The serialized data 27 | - throws: Errors of type `SignalError` 28 | */ 29 | public init(from data: Data) throws { 30 | var preKey: OpaquePointer? = nil 31 | let result = data.withUnsafeBytes { dPtr in 32 | withUnsafeMutablePointer(to: &preKey) { 33 | session_pre_key_deserialize($0, dPtr, data.count, Signal.context) 34 | } 35 | } 36 | guard result == 0 else { throw SignalError(value: result) } 37 | defer { session_pre_key_destroy(preKey) } 38 | try self.init(pointer: preKey!) 39 | } 40 | 41 | /** 42 | Create a pre key from a pointer 43 | - parameter pointer: The `session_pre_key` pointer 44 | - throws: Errors of type `SignalError` 45 | */ 46 | init(pointer: OpaquePointer) throws { 47 | let ptr = session_pre_key_get_key_pair(pointer)! 48 | 49 | self.keyPair = try KeyPair(pointer: ptr) 50 | self.id = session_pre_key_get_id(pointer) 51 | } 52 | 53 | /// Serialize the session pre key 54 | public func data() throws -> Data { 55 | let keyPairPtr = try keyPair.pointer() 56 | defer { ec_public_key_destroy(keyPairPtr) } 57 | 58 | // Create pre key 59 | var preKey: OpaquePointer? = nil 60 | var result = withUnsafeMutablePointer(to: &preKey) { 61 | session_pre_key_create($0, self.id, keyPairPtr) 62 | } 63 | guard result == 0 else { throw SignalError(value: result) } 64 | defer { session_pre_key_destroy(preKey) } 65 | 66 | // Serialize pre key 67 | var preKeyRecord: OpaquePointer? = nil 68 | result = withUnsafeMutablePointer(to: &preKeyRecord) { 69 | session_pre_key_serialize($0, preKey) 70 | } 71 | guard result == 0 else { throw SignalError(value: result) } 72 | defer { signal_buffer_free(preKeyRecord) } 73 | 74 | // Return serialized data 75 | return Data(signalBuffer: preKeyRecord!) 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_frombytes.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | #include "crypto_int64.h" 3 | #include "crypto_uint64.h" 4 | 5 | static crypto_uint64 load_3(const unsigned char *in) 6 | { 7 | crypto_uint64 result; 8 | result = (crypto_uint64) in[0]; 9 | result |= ((crypto_uint64) in[1]) << 8; 10 | result |= ((crypto_uint64) in[2]) << 16; 11 | return result; 12 | } 13 | 14 | static crypto_uint64 load_4(const unsigned char *in) 15 | { 16 | crypto_uint64 result; 17 | result = (crypto_uint64) in[0]; 18 | result |= ((crypto_uint64) in[1]) << 8; 19 | result |= ((crypto_uint64) in[2]) << 16; 20 | result |= ((crypto_uint64) in[3]) << 24; 21 | return result; 22 | } 23 | 24 | /* 25 | Ignores top bit of h. 26 | */ 27 | 28 | void fe_frombytes(fe h,const unsigned char *s) 29 | { 30 | crypto_int64 h0 = load_4(s); 31 | crypto_int64 h1 = load_3(s + 4) << 6; 32 | crypto_int64 h2 = load_3(s + 7) << 5; 33 | crypto_int64 h3 = load_3(s + 10) << 3; 34 | crypto_int64 h4 = load_3(s + 13) << 2; 35 | crypto_int64 h5 = load_4(s + 16); 36 | crypto_int64 h6 = load_3(s + 20) << 7; 37 | crypto_int64 h7 = load_3(s + 23) << 5; 38 | crypto_int64 h8 = load_3(s + 26) << 4; 39 | crypto_int64 h9 = (load_3(s + 29) & 8388607) << 2; 40 | crypto_int64 carry0; 41 | crypto_int64 carry1; 42 | crypto_int64 carry2; 43 | crypto_int64 carry3; 44 | crypto_int64 carry4; 45 | crypto_int64 carry5; 46 | crypto_int64 carry6; 47 | crypto_int64 carry7; 48 | crypto_int64 carry8; 49 | crypto_int64 carry9; 50 | 51 | carry9 = (h9 + (crypto_int64) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= carry9 << 25; 52 | carry1 = (h1 + (crypto_int64) (1<<24)) >> 25; h2 += carry1; h1 -= carry1 << 25; 53 | carry3 = (h3 + (crypto_int64) (1<<24)) >> 25; h4 += carry3; h3 -= carry3 << 25; 54 | carry5 = (h5 + (crypto_int64) (1<<24)) >> 25; h6 += carry5; h5 -= carry5 << 25; 55 | carry7 = (h7 + (crypto_int64) (1<<24)) >> 25; h8 += carry7; h7 -= carry7 << 25; 56 | 57 | carry0 = (h0 + (crypto_int64) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26; 58 | carry2 = (h2 + (crypto_int64) (1<<25)) >> 26; h3 += carry2; h2 -= carry2 << 26; 59 | carry4 = (h4 + (crypto_int64) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26; 60 | carry6 = (h6 + (crypto_int64) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26; 61 | carry8 = (h8 + (crypto_int64) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26; 62 | 63 | h[0] = (crypto_int32) h0; 64 | h[1] = (crypto_int32) h1; 65 | h[2] = (crypto_int32) h2; 66 | h[3] = (crypto_int32) h3; 67 | h[4] = (crypto_int32) h4; 68 | h[5] = (crypto_int32) h5; 69 | h[6] = (crypto_int32) h6; 70 | h[7] = (crypto_int32) h7; 71 | h[8] = (crypto_int32) h8; 72 | h[9] = (crypto_int32) h9; 73 | } 74 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Stores/IdentityKeyStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // IdentityKeyStore.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 14.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | The `IdentityKeyStore`protocol must be adopted to provide the storage for identity keys. 14 | */ 15 | public protocol IdentityKeyStore { 16 | 17 | /** 18 | Get the local client's identity key pair. 19 | - returns: The identity key pair on success, nil on failure 20 | */ 21 | func identityKeyPair() -> KeyPair? 22 | 23 | /** 24 | Return the local client's registration ID. 25 | 26 | Clients should maintain a registration ID, a random number 27 | between 1 and 16380 that's generated once at install time. 28 | 29 | - returns: The registration id on success, nil on failure 30 | */ 31 | func localRegistrationId() -> UInt32? 32 | 33 | /** 34 | Save a remote client's identity key 35 | 36 | Store a remote client's identity key as trusted. 37 | The value of `identity` may be nil. In this case remove the key data 38 | from the identity store, but retain any metadata that may be kept 39 | alongside it. 40 | 41 | - parameter identity: The remote client's identity key, may be nil 42 | - parameter address: The address of the remote client 43 | - returns: `true` on success, `false` on failure 44 | */ 45 | func save(identity: Data?, for address: SignalAddress) -> Bool 46 | 47 | /** 48 | Verify a remote client's identity key. 49 | 50 | Determine whether a remote client's identity is trusted. Convention is that the Signal protocol is 'trust on first use.' This means that an identity key is considered 'trusted' if there is no entry for the recipient in the local store, or if it matches the saved key for a recipient in the local store. Only if it mismatches an entry in the local store is it considered 'untrusted.' 51 | 52 | - parameter address: The address of the remote client 53 | - parameter identity: The identity key to verify 54 | - returns: `true` if trusted, `false` if untrusted, nil on failure 55 | */ 56 | func isTrusted(identity: Data, for address: SignalAddress) -> Bool? 57 | 58 | /** 59 | Function called to perform cleanup when the data store context is being 60 | destroyed. 61 | */ 62 | func destroy() 63 | 64 | } 65 | 66 | public extension IdentityKeyStore { 67 | 68 | /** 69 | Function called to perform cleanup when the data store context is being 70 | destroyed. 71 | */ 72 | func destroy() { 73 | // Empty implementation to make this function 'optional' 74 | } 75 | } 76 | 77 | 78 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/xeddsa.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ge.h" 3 | #include "crypto_additions.h" 4 | #include "zeroize.h" 5 | #include "xeddsa.h" 6 | #include "crypto_verify_32.h" 7 | 8 | int xed25519_sign(unsigned char* signature_out, 9 | const unsigned char* curve25519_privkey, 10 | const unsigned char* msg, const unsigned long msg_len, 11 | const unsigned char* random) 12 | { 13 | unsigned char a[32], aneg[32]; 14 | unsigned char A[32]; 15 | ge_p3 ed_pubkey_point; 16 | unsigned char *sigbuf; /* working buffer */ 17 | unsigned char sign_bit = 0; 18 | 19 | if ((sigbuf = malloc(msg_len + 128)) == 0) { 20 | memset(signature_out, 0, 64); 21 | return -1; 22 | } 23 | 24 | /* Convert the Curve25519 privkey to an Ed25519 public key */ 25 | ge_scalarmult_base(&ed_pubkey_point, curve25519_privkey); 26 | ge_p3_tobytes(A, &ed_pubkey_point); 27 | 28 | /* Force Edwards sign bit to zero */ 29 | sign_bit = (A[31] & 0x80) >> 7; 30 | memcpy(a, curve25519_privkey, 32); 31 | sc_neg(aneg, a); 32 | sc_cmov(a, aneg, sign_bit); 33 | A[31] &= 0x7F; 34 | 35 | /* Perform an Ed25519 signature with explicit private key */ 36 | crypto_sign_modified(sigbuf, msg, msg_len, a, A, random); 37 | memmove(signature_out, sigbuf, 64); 38 | 39 | zeroize(a, 32); 40 | zeroize(aneg, 32); 41 | free(sigbuf); 42 | return 0; 43 | } 44 | 45 | int xed25519_verify(const unsigned char* signature, 46 | const unsigned char* curve25519_pubkey, 47 | const unsigned char* msg, const unsigned long msg_len) 48 | { 49 | fe u; 50 | fe y; 51 | unsigned char ed_pubkey[32]; 52 | unsigned char verifybuf[MAX_MSG_LEN + 64]; /* working buffer */ 53 | unsigned char verifybuf2[MAX_MSG_LEN + 64]; /* working buffer #2 */ 54 | 55 | if (msg_len > MAX_MSG_LEN) { 56 | return -1; 57 | } 58 | 59 | /* Convert the Curve25519 public key into an Ed25519 public key. 60 | 61 | y = (u - 1) / (u + 1) 62 | 63 | NOTE: u=-1 is converted to y=0 since fe_invert is mod-exp 64 | */ 65 | if (!fe_isreduced(curve25519_pubkey)) 66 | return -1; 67 | fe_frombytes(u, curve25519_pubkey); 68 | fe_montx_to_edy(y, u); 69 | fe_tobytes(ed_pubkey, y); 70 | 71 | memmove(verifybuf, signature, 64); 72 | memmove(verifybuf+64, msg, msg_len); 73 | 74 | /* Then perform a normal Ed25519 verification, return 0 on success */ 75 | /* The below call has a strange API: */ 76 | /* verifybuf = R || S || message */ 77 | /* verifybuf2 = internal to next call gets a copy of verifybuf, S gets 78 | replaced with pubkey for hashing */ 79 | return crypto_sign_open_modified(verifybuf2, verifybuf, 64 + msg_len, ed_pubkey); 80 | } 81 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Misc/SignalAddress.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SignalAddress.swift 3 | // libsignal-protocol-swift iOS 4 | // 5 | // Created by User on 15.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | A SignalAddress identifies the device of a user. 14 | */ 15 | public final class SignalAddress { 16 | 17 | /// The name or identifier of the user 18 | public let name: String 19 | 20 | /// The id of the user's device 21 | public let deviceId: Int32 22 | 23 | /// Internal pointer to the C name 24 | private let namePointer: UnsafeMutablePointer 25 | 26 | /// Internal pointer to the C struct 27 | private let address: UnsafeMutablePointer 28 | 29 | var signalAddress: UnsafePointer { 30 | return UnsafePointer(address) 31 | } 32 | 33 | /** 34 | Create a signal address. 35 | - parameter name: The name/id of the user 36 | - parameter deviceId: The id of the device 37 | */ 38 | public init(name: String, deviceId: Int32) { 39 | self.name = name 40 | self.deviceId = deviceId 41 | let count = name.utf8.count 42 | // From: https://github.com/christophhagen/libsignal-protocol-swift/issues/2 43 | self.namePointer = UnsafeMutablePointer(mutating: (name as NSString).utf8String!) 44 | self.address = UnsafeMutablePointer.allocate(capacity: 1) 45 | address.pointee = signal_protocol_address(name: namePointer, name_len: count, device_id: deviceId) 46 | } 47 | 48 | convenience init?(from address: UnsafePointer?) { 49 | guard let add = address?.pointee else { 50 | return nil 51 | } 52 | self.init(from: add) 53 | } 54 | 55 | convenience init?(from address: signal_protocol_address) { 56 | guard let namePtr = address.name else { 57 | return nil 58 | } 59 | self.init(name: String(cString: namePtr), deviceId: address.device_id) 60 | } 61 | 62 | deinit { 63 | signalAddress.deallocate() 64 | } 65 | } 66 | 67 | extension SignalAddress: Equatable { 68 | 69 | /** 70 | Compare two signal addresses for equality 71 | - parameter lhs: The first address 72 | - parameter rhs: The second address 73 | - returns: `true` if the addresses match 74 | */ 75 | public static func == (lhs: SignalAddress, rhs: SignalAddress) -> Bool { 76 | return lhs.name == rhs.name && lhs.deviceId == rhs.deviceId 77 | } 78 | } 79 | 80 | extension SignalAddress: Hashable { 81 | 82 | /// A hash of the address 83 | public var hashValue: Int { 84 | return name.hashValue &+ deviceId.hashValue 85 | } 86 | } 87 | 88 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/group_session_builder.h: -------------------------------------------------------------------------------- 1 | #ifndef GROUP_SESSION_BUILDER_H 2 | #define GROUP_SESSION_BUILDER_H 3 | 4 | #include "signal_protocol_types.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | /* 11 | * Group session builder is responsible for setting up group sender key encrypted sessions. 12 | * 13 | * Once a session has been established, group_cipher can be used to 14 | * encrypt/decrypt messages in that session. 15 | *

16 | * The built sessions are unidirectional: they can be used either for sending 17 | * or for receiving, but not both. 18 | * 19 | * Sessions are constructed per (groupId + senderId + deviceId) tuple. Remote logical users 20 | * are identified by their senderId, and each logical recipientId can have multiple physical 21 | * devices. 22 | */ 23 | 24 | /** 25 | * Constructs a group session builder. 26 | * 27 | * The store and global contexts must remain valid for the lifetime of the 28 | * session builder. 29 | * 30 | * When finished, free the returned instance by calling group_session_builder_free(). 31 | * 32 | * @param builder set to a freshly allocated group session builder instance 33 | * @param store the signal_protocol_store_context to store all state information in 34 | * @param global_context the global library context 35 | * @return 0 on success, or negative on failure 36 | */ 37 | int group_session_builder_create(group_session_builder **builder, 38 | signal_protocol_store_context *store, signal_context *global_context); 39 | 40 | /** 41 | * Construct a group session for receiving messages from senderKeyName. 42 | * 43 | * @param sender_key_name the (groupId, senderId, deviceId) tuple associated 44 | * with the sender_key_distribution_message 45 | * @param distribution_message a received sender_key_distribution_message 46 | * @return 0 on success, or negative on failure 47 | */ 48 | int group_session_builder_process_session(group_session_builder *builder, 49 | const signal_protocol_sender_key_name *sender_key_name, 50 | sender_key_distribution_message *distribution_message); 51 | 52 | /** 53 | * Construct a group session for sending messages. 54 | * 55 | * @param distribution_message a distribution message to be allocated and populated 56 | * @param sender_key_name the (groupId, senderId, deviceId) tuple. In this 57 | * case, the sender should be the caller 58 | * @return 0 on success, or negative on failure 59 | */ 60 | int group_session_builder_create_session(group_session_builder *builder, 61 | sender_key_distribution_message **distribution_message, 62 | const signal_protocol_sender_key_name *sender_key_name); 63 | 64 | void group_session_builder_free(group_session_builder *builder); 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif /* GROUP_SESSION_BUILDER_H */ 71 | -------------------------------------------------------------------------------- /libsignal-protocol-swiftTests/TestHelper.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TestHelper.swift 3 | // libsignal-protocol-swiftTests 4 | // 5 | // Created by User on 19.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import SignalProtocol 10 | 11 | func setupStore(makeKeys: Bool) -> SignalStore? { 12 | 13 | let store: SignalStore 14 | do { 15 | store = try SignalStore( 16 | identityKeyStore: TestIdentityStore(), 17 | preKeyStore: TestPreKeyStore(), 18 | sessionStore: TestSessionStore(), 19 | signedPreKeyStore: TestSignedPrekeyStore(), 20 | senderKeyStore: TestSenderKeyStore()) 21 | } catch { 22 | print("Failed to create store: \(error)") 23 | return nil 24 | } 25 | 26 | let identity: KeyPair 27 | do { identity = try Signal.generateIdentityKeyPair() 28 | } catch { 29 | print("Could not create identity key: \(error)") 30 | return nil 31 | } 32 | 33 | let registrationId: UInt32 34 | do { 35 | registrationId = try Signal.generateRegistrationId() 36 | } catch { 37 | print("Could not create registration id: \(error)") 38 | return nil 39 | } 40 | 41 | // Store identity key pair and registration id 42 | (store.identityKeyStore as! TestIdentityStore).identity = identity 43 | (store.identityKeyStore as! TestIdentityStore).registrationId = registrationId 44 | 45 | if makeKeys { 46 | let preKeys: [SessionPreKey] 47 | do { 48 | preKeys = try Signal.generatePreKeys(start: 1, count: 100) 49 | } catch { 50 | print("Could not create pre keys: \(error)") 51 | return nil 52 | } 53 | 54 | // Store keys 55 | for key in preKeys { 56 | do { 57 | let stored = store.preKeyStore.store(preKey: try key.data(), for: key.id) 58 | guard stored else { 59 | print("Could not store pre key") 60 | return nil 61 | } 62 | } catch { 63 | print("Could not serialize pre key: \(error)") 64 | return nil 65 | } 66 | } 67 | 68 | let signedPreKey: SessionSignedPreKey 69 | do { 70 | signedPreKey = try Signal.generate(signedPreKey: 1, identity: identity, timestamp: 0) 71 | } catch { 72 | print("Could not create signed pre key: \(error)") 73 | return nil 74 | } 75 | 76 | // Store signed pre key 77 | do { 78 | (store.signedPreKeyStore as! TestSignedPrekeyStore).keys[signedPreKey.id] = try signedPreKey.data() 79 | } catch { 80 | print("Could not serialize signed pre key: \(error)") 81 | } 82 | } 83 | /// Setup finished 84 | return store 85 | } 86 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_double_scalarmult.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | 3 | static void slide(signed char *r,const unsigned char *a) 4 | { 5 | int i; 6 | int b; 7 | int k; 8 | 9 | for (i = 0;i < 256;++i) 10 | r[i] = 1 & (a[i >> 3] >> (i & 7)); 11 | 12 | for (i = 0;i < 256;++i) 13 | if (r[i]) { 14 | for (b = 1;b <= 6 && i + b < 256;++b) { 15 | if (r[i + b]) { 16 | if (r[i] + (r[i + b] << b) <= 15) { 17 | r[i] += r[i + b] << b; r[i + b] = 0; 18 | } else if (r[i] - (r[i + b] << b) >= -15) { 19 | r[i] -= r[i + b] << b; 20 | for (k = i + b;k < 256;++k) { 21 | if (!r[k]) { 22 | r[k] = 1; 23 | break; 24 | } 25 | r[k] = 0; 26 | } 27 | } else 28 | break; 29 | } 30 | } 31 | } 32 | 33 | } 34 | 35 | static ge_precomp Bi[8] = { 36 | #include "base2.h" 37 | } ; 38 | 39 | /* 40 | r = a * A + b * B 41 | where a = a[0]+256*a[1]+...+256^31 a[31]. 42 | and b = b[0]+256*b[1]+...+256^31 b[31]. 43 | B is the Ed25519 base point (x,4/5) with x positive. 44 | */ 45 | 46 | void ge_double_scalarmult_vartime(ge_p2 *r,const unsigned char *a,const ge_p3 *A,const unsigned char *b) 47 | { 48 | signed char aslide[256]; 49 | signed char bslide[256]; 50 | ge_cached Ai[8]; /* A,3A,5A,7A,9A,11A,13A,15A */ 51 | ge_p1p1 t; 52 | ge_p3 u; 53 | ge_p3 A2; 54 | int i; 55 | 56 | slide(aslide,a); 57 | slide(bslide,b); 58 | 59 | ge_p3_to_cached(&Ai[0],A); 60 | ge_p3_dbl(&t,A); ge_p1p1_to_p3(&A2,&t); 61 | ge_add(&t,&A2,&Ai[0]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[1],&u); 62 | ge_add(&t,&A2,&Ai[1]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[2],&u); 63 | ge_add(&t,&A2,&Ai[2]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[3],&u); 64 | ge_add(&t,&A2,&Ai[3]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[4],&u); 65 | ge_add(&t,&A2,&Ai[4]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[5],&u); 66 | ge_add(&t,&A2,&Ai[5]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[6],&u); 67 | ge_add(&t,&A2,&Ai[6]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[7],&u); 68 | 69 | ge_p2_0(r); 70 | 71 | for (i = 255;i >= 0;--i) { 72 | if (aslide[i] || bslide[i]) break; 73 | } 74 | 75 | for (;i >= 0;--i) { 76 | ge_p2_dbl(&t,r); 77 | 78 | if (aslide[i] > 0) { 79 | ge_p1p1_to_p3(&u,&t); 80 | ge_add(&t,&u,&Ai[aslide[i]/2]); 81 | } else if (aslide[i] < 0) { 82 | ge_p1p1_to_p3(&u,&t); 83 | ge_sub(&t,&u,&Ai[(-aslide[i])/2]); 84 | } 85 | 86 | if (bslide[i] > 0) { 87 | ge_p1p1_to_p3(&u,&t); 88 | ge_madd(&t,&u,&Bi[bslide[i]/2]); 89 | } else if (bslide[i] < 0) { 90 | ge_p1p1_to_p3(&u,&t); 91 | ge_msub(&t,&u,&Bi[(-bslide[i])/2]); 92 | } 93 | 94 | ge_p1p1_to_p2(r,&t); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Setup/setup.c: -------------------------------------------------------------------------------- 1 | // 2 | // setup.c 3 | // libsignal-protocol-swift iOS 4 | // 5 | // Created by User on 15.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | #include "setup.h" 10 | #include "signal_protocol.h" 11 | 12 | #include 13 | 14 | pthread_mutex_t global_mutex; 15 | pthread_mutexattr_t global_mutex_attr; 16 | 17 | signal_protocol_store_context* setup_store_context(signal_context *global_context); 18 | int set_locking(signal_context *global_context); 19 | void test_log(int level, const char *message, size_t len, void *user_data); 20 | 21 | void *signal_setup(void) { 22 | signal_context *global_context; 23 | int result = signal_context_create(&global_context, 0); 24 | if (result != 0) { 25 | return 0; 26 | } 27 | 28 | result = set_locking(global_context); 29 | if (result != 0) { 30 | signal_context_destroy(global_context); 31 | return 0; 32 | } 33 | 34 | result = setup_crypto_provider(global_context); 35 | if (result != 0) { 36 | signal_context_destroy(global_context); 37 | pthread_mutex_destroy(&global_mutex); 38 | pthread_mutexattr_destroy(&global_mutex_attr); 39 | return 0; 40 | } 41 | 42 | signal_context_set_log_function(global_context, test_log); 43 | 44 | return (void*) global_context; 45 | } 46 | 47 | void signal_destroy(void *global_context) { 48 | 49 | signal_context_destroy((signal_context*) global_context); 50 | 51 | pthread_mutex_destroy(&global_mutex); 52 | pthread_mutexattr_destroy(&global_mutex_attr); 53 | } 54 | 55 | // MARK: Locking functions 56 | 57 | void test_lock(void *user_data) { 58 | pthread_mutex_lock(&global_mutex); 59 | } 60 | 61 | void test_unlock(void *user_data) { 62 | pthread_mutex_unlock(&global_mutex); 63 | } 64 | 65 | int set_locking(signal_context *global_context) { 66 | pthread_mutexattr_init(&global_mutex_attr); 67 | pthread_mutexattr_settype(&global_mutex_attr, PTHREAD_MUTEX_RECURSIVE); 68 | pthread_mutex_init(&global_mutex, &global_mutex_attr); 69 | 70 | return signal_context_set_locking_functions(global_context, test_lock, test_unlock); 71 | } 72 | 73 | void test_log(int level, const char *message, size_t len, void *user_data) { 74 | switch(level) { 75 | case SG_LOG_ERROR: 76 | fprintf(stderr, "[ERROR] %s\n", message); 77 | break; 78 | case SG_LOG_WARNING: 79 | fprintf(stderr, "[WARNING] %s\n", message); 80 | break; 81 | case SG_LOG_NOTICE: 82 | fprintf(stderr, "[NOTICE] %s\n", message); 83 | break; 84 | case SG_LOG_INFO: 85 | fprintf(stderr, "[INFO] %s\n", message); 86 | break; 87 | case SG_LOG_DEBUG: 88 | fprintf(stderr, "[DEBUG] %s\n", message); 89 | break; 90 | default: 91 | fprintf(stderr, "[%d] %s\n", level, message); 92 | break; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/generalized/gen_eddsa.h: -------------------------------------------------------------------------------- 1 | #ifndef __GEN_EDDSA_H__ 2 | #define __GEN_EDDSA_H__ 3 | 4 | #include "ge.h" 5 | 6 | /* B: base point 7 | R: commitment (point), 8 | r: private nonce (scalar) 9 | K: encoded public key 10 | k: private key (scalar) 11 | Z: 32-bytes random 12 | M: buffer containing message, message starts at M_start, continues for M_len 13 | 14 | r = hash(B || labelset || Z || pad1 || k || pad2 || labelset || K || extra || M) (mod q) 15 | */ 16 | int generalized_commit(unsigned char* R_bytes, unsigned char* r_scalar, 17 | const unsigned char* labelset, const unsigned long labelset_len, 18 | const unsigned char* extra, const unsigned long extra_len, 19 | const unsigned char* K_bytes, const unsigned char* k_scalar, 20 | const unsigned char* Z, 21 | unsigned char* M_buf, const unsigned long M_start, const unsigned long M_len); 22 | 23 | /* if is_labelset_empty(labelset): 24 | return hash(R || K || M) (mod q) 25 | else: 26 | return hash(B || labelset || R || labelset || K || extra || M) (mod q) 27 | */ 28 | int generalized_challenge(unsigned char* h_scalar, 29 | const unsigned char* labelset, const unsigned long labelset_len, 30 | const unsigned char* extra, const unsigned long extra_len, 31 | const unsigned char* R_bytes, 32 | const unsigned char* K_bytes, 33 | unsigned char* M_buf, const unsigned long M_start, const unsigned long M_len); 34 | 35 | /* return r + kh (mod q) */ 36 | int generalized_prove(unsigned char* out_scalar, 37 | const unsigned char* r_scalar, 38 | const unsigned char* k_scalar, 39 | const unsigned char* h_scalar); 40 | 41 | /* R = B^s / K^h */ 42 | int generalized_solve_commitment(unsigned char* R_bytes_out, ge_p3* K_point_out, 43 | const ge_p3* B_point, const unsigned char* s_scalar, 44 | const unsigned char* K_bytes, const unsigned char* h_scalar); 45 | 46 | 47 | int generalized_eddsa_25519_sign( 48 | unsigned char* signature_out, 49 | const unsigned char* eddsa_25519_pubkey_bytes, 50 | const unsigned char* eddsa_25519_privkey_scalar, 51 | const unsigned char* msg, 52 | const unsigned long msg_len, 53 | const unsigned char* random, 54 | const unsigned char* customization_label, 55 | const unsigned long customization_label_len); 56 | 57 | int generalized_eddsa_25519_verify( 58 | const unsigned char* signature, 59 | const unsigned char* eddsa_25519_pubkey, 60 | const unsigned char* msg, 61 | const unsigned long msg_len, 62 | const unsigned char* customization_label, 63 | const unsigned long customization_label_len); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Misc/SignalError.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SignalError.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 16.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | /** 12 | Errors thrown by the framework. 13 | - note: Most errors correspond directly to 14 | the error codes of `libsignal-protocol-c`. 15 | */ 16 | public enum SignalError: Int32, Error { 17 | 18 | // MARK: libsignal-protocol-c errors 19 | 20 | /// Allocation of memory failed 21 | case noMemory = -12 22 | 23 | /// An argument to the function was invalid/missing 24 | case invalidArgument = -22 25 | 26 | /// Unspecified error 27 | case unknownError = -1000 28 | 29 | /// The message has already been received 30 | case duplicateMessage = -1001 31 | 32 | /// The key is invalid 33 | case invalidKey = -1002 34 | 35 | /// The key id is invalid 36 | case invalidKeyId = -1003 37 | 38 | /// The MAC of a message is invalid 39 | case invalidMac = -1004 40 | 41 | /// The message is malformed or corrupt 42 | case invalidMessage = -1005 43 | 44 | /// The version does not match 45 | case invalidVersion = -1006 46 | 47 | /// The message version is no longer supported 48 | case legacyMessage = -1007 49 | 50 | /// There is no session saved 51 | case noSession = -1008 52 | 53 | //case staleKeyExchange = -1009 54 | 55 | /// The identity of the sender is untrusted 56 | case untrustedIdentity = -1010 57 | 58 | /// The VRF signature is invalid 59 | case incalidVrfSignature = -1011 60 | 61 | /// The (de)serialization failed 62 | case invalidProtoBuf = -1100 63 | 64 | /// The fingerprint versions don't match 65 | case fpVersionMismatch = -1200 66 | 67 | /// The identities of the fingerprints don't match 68 | case fpIdentityMismatch = -1201 69 | 70 | // MARK: libsignal-protocol-swift errors 71 | 72 | /// There is no delegate registered 73 | case noDelegate = -2001 74 | 75 | /// No identity key or registration id in the identity key store 76 | case noData = -2002 77 | 78 | /// A Swift SignalAddress could not be created 79 | case noSignalAddress = -2003 80 | 81 | /// Retrieving data from a key store failed 82 | case notSuccessful = -2004 83 | 84 | /// An identity key could not be checked 85 | case isTrustedFailed = -2005 86 | 87 | /** 88 | Create a SignalError from a `libsignal-protocol-c` result. 89 | - note: For unknown error codes the error will be set to `unknownError` 90 | - parameter value: The result code of an operation. 91 | */ 92 | public init(value: Int32) { 93 | if let status = SignalError(rawValue: Int32(value)) { 94 | self = status 95 | } else { 96 | print("Unknown error: \(value)") 97 | self = .unknownError 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Stores/SessionStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SessionStore.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 15.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | The `SessionStore`protocol must be adopted to provide the storage for sessions. 14 | */ 15 | public protocol SessionStore { 16 | 17 | /** 18 | Returns a copy of the serialized session record corresponding to the 19 | provided recipient ID + device ID tuple. 20 | 21 | - parameter address: The address of the remote client 22 | - returns: The session and optional user record, or nil on failure 23 | */ 24 | func loadSession(for address: SignalAddress) -> (session: Data, userRecord: Data?)? 25 | 26 | /** 27 | Returns all known devices with active sessions for a recipient 28 | - parameter name: The name of the remote client 29 | - returns: The ids of all active devices 30 | */ 31 | func subDeviceSessions(for name: String) -> [Int32]? 32 | 33 | /** 34 | Commit to storage the session record for a given 35 | recipient ID + device ID tuple. 36 | 37 | - parameter session: The serialized session record 38 | - parameter userRecord: Application specific data to be stored alongside the serialized session record for the remote client. If no such data exists, then this parameter will be nil. 39 | - parameter address: The address of the remote client 40 | - returns: `true` on sucess, `false` on failure. 41 | */ 42 | func store(session: Data, for address: SignalAddress, userRecord: Data?) -> Bool 43 | 44 | /** 45 | Determine whether there is a committed session record for a 46 | recipient ID + device ID tuple. 47 | 48 | - parameter address: The address of the remote client 49 | - returns: `true` if a session record exists, `false` otherwise. 50 | */ 51 | func containsSession(for address: SignalAddress) -> Bool 52 | 53 | /** 54 | Remove a session record for a recipient ID + device ID tuple. 55 | 56 | - parameter address: The address of the remote client 57 | - returns: `true` if a session was deleted, `false` if a session was not deleted, nil on error 58 | */ 59 | func deleteSession(for address: SignalAddress) -> Bool? 60 | 61 | /** 62 | Remove the session records corresponding to all devices of a recipient Id. 63 | 64 | - parameter name: The name of the remote client 65 | - returns: The number of deleted sessions on success, nil on failure 66 | */ 67 | func deleteAllSessions(for name: String) -> Int? 68 | 69 | /** 70 | Function called to perform cleanup when the data store context is being 71 | destroyed. 72 | */ 73 | func destroy() 74 | } 75 | 76 | public extension SessionStore { 77 | 78 | /** 79 | Function called to perform cleanup when the data store context is being 80 | destroyed. 81 | */ 82 | func destroy() { 83 | // Empty implementation to make this function 'optional' 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge_scalarmult_base.c: -------------------------------------------------------------------------------- 1 | #include "ge.h" 2 | #include "crypto_uint32.h" 3 | 4 | static unsigned char equal(signed char b,signed char c) 5 | { 6 | unsigned char ub = b; 7 | unsigned char uc = c; 8 | unsigned char x = ub ^ uc; /* 0: yes; 1..255: no */ 9 | crypto_uint32 y = x; /* 0: yes; 1..255: no */ 10 | y -= 1; /* 4294967295: yes; 0..254: no */ 11 | y >>= 31; /* 1: yes; 0: no */ 12 | return y; 13 | } 14 | 15 | static unsigned char negative(signed char b) 16 | { 17 | unsigned long long x = b; /* 18446744073709551361..18446744073709551615: yes; 0..255: no */ 18 | x >>= 63; /* 1: yes; 0: no */ 19 | return x; 20 | } 21 | 22 | static void cmov(ge_precomp *t,ge_precomp *u,unsigned char b) 23 | { 24 | fe_cmov(t->yplusx,u->yplusx,b); 25 | fe_cmov(t->yminusx,u->yminusx,b); 26 | fe_cmov(t->xy2d,u->xy2d,b); 27 | } 28 | 29 | /* base[i][j] = (j+1)*256^i*B */ 30 | static ge_precomp base[32][8] = { 31 | #include "base.h" 32 | } ; 33 | 34 | static void select(ge_precomp *t,int pos,signed char b) 35 | { 36 | ge_precomp minust; 37 | unsigned char bnegative = negative(b); 38 | unsigned char babs = b - (((-bnegative) & b) << 1); 39 | 40 | ge_precomp_0(t); 41 | cmov(t,&base[pos][0],equal(babs,1)); 42 | cmov(t,&base[pos][1],equal(babs,2)); 43 | cmov(t,&base[pos][2],equal(babs,3)); 44 | cmov(t,&base[pos][3],equal(babs,4)); 45 | cmov(t,&base[pos][4],equal(babs,5)); 46 | cmov(t,&base[pos][5],equal(babs,6)); 47 | cmov(t,&base[pos][6],equal(babs,7)); 48 | cmov(t,&base[pos][7],equal(babs,8)); 49 | fe_copy(minust.yplusx,t->yminusx); 50 | fe_copy(minust.yminusx,t->yplusx); 51 | fe_neg(minust.xy2d,t->xy2d); 52 | cmov(t,&minust,bnegative); 53 | } 54 | 55 | /* 56 | h = a * B 57 | where a = a[0]+256*a[1]+...+256^31 a[31] 58 | B is the Ed25519 base point (x,4/5) with x positive. 59 | 60 | Preconditions: 61 | a[31] <= 127 62 | */ 63 | 64 | void ge_scalarmult_base(ge_p3 *h,const unsigned char *a) 65 | { 66 | signed char e[64]; 67 | signed char carry; 68 | ge_p1p1 r; 69 | ge_p2 s; 70 | ge_precomp t; 71 | int i; 72 | 73 | for (i = 0;i < 32;++i) { 74 | e[2 * i + 0] = (a[i] >> 0) & 15; 75 | e[2 * i + 1] = (a[i] >> 4) & 15; 76 | } 77 | /* each e[i] is between 0 and 15 */ 78 | /* e[63] is between 0 and 7 */ 79 | 80 | carry = 0; 81 | for (i = 0;i < 63;++i) { 82 | e[i] += carry; 83 | carry = e[i] + 8; 84 | carry >>= 4; 85 | e[i] -= carry << 4; 86 | } 87 | e[63] += carry; 88 | /* each e[i] is between -8 and 8 */ 89 | 90 | ge_p3_0(h); 91 | for (i = 1;i < 64;i += 2) { 92 | select(&t,i / 2,e[i]); 93 | ge_madd(&r,h,&t); ge_p1p1_to_p3(h,&r); 94 | } 95 | 96 | ge_p3_dbl(&r,h); ge_p1p1_to_p2(&s,&r); 97 | ge_p2_dbl(&r,&s); ge_p1p1_to_p2(&s,&r); 98 | ge_p2_dbl(&r,&s); ge_p1p1_to_p2(&s,&r); 99 | ge_p2_dbl(&r,&s); ge_p1p1_to_p3(h,&r); 100 | 101 | for (i = 0;i < 64;i += 2) { 102 | select(&t,i / 2,e[i]); 103 | ge_madd(&r,h,&t); ge_p1p1_to_p3(h,&r); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Misc/SignalSenderKeyName.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SignalSenderKeyName.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 15.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | The identifier of a party in a group conversation setting. 14 | */ 15 | public final class SignalSenderKeyName { 16 | 17 | /// The group identifier 18 | public let groupId: String 19 | 20 | /// The sender/recipient 21 | public let sender: SignalAddress 22 | 23 | private let groupPointer: UnsafeMutablePointer 24 | 25 | private let address: UnsafeMutablePointer 26 | 27 | var pointer: UnsafePointer { 28 | return UnsafePointer(address) 29 | } 30 | 31 | /** 32 | Create a sender key name. 33 | - parameter groupId: the identifier of the group 34 | - parameter sender: The sender of the messages 35 | */ 36 | public init(groupId: String, sender: SignalAddress) { 37 | self.groupId = groupId 38 | self.sender = sender 39 | let count = groupId.utf8.count 40 | self.groupPointer = UnsafeMutablePointer.allocate(capacity: count) 41 | groupPointer.assign(from: groupId, count: count) 42 | self.address = UnsafeMutablePointer.allocate(capacity: 1) 43 | 44 | address.pointee = signal_protocol_sender_key_name(group_id: groupPointer, group_id_len: count, sender: sender.signalAddress.pointee) 45 | } 46 | 47 | /** 48 | Create an a sender key name from a pointer to a libsignal-protocol-c address. 49 | - parameter address: The pointer to the address 50 | */ 51 | convenience init?(from address: UnsafePointer?) { 52 | guard let add = address?.pointee else { 53 | return nil 54 | } 55 | self.init(from: add) 56 | } 57 | 58 | /** 59 | Create an a sender key name from a libsignal-protocol-c address. 60 | - parameter address: The libsignal-protocol-c address 61 | */ 62 | convenience init?(from address: signal_protocol_sender_key_name) { 63 | 64 | guard let groupPtr = address.group_id, 65 | let sender = SignalAddress(from: address.sender) else { 66 | return nil 67 | } 68 | self.init(groupId: String(cString: groupPtr), sender: sender) 69 | } 70 | } 71 | 72 | extension SignalSenderKeyName: Equatable { 73 | 74 | /** 75 | Compare two sender key names for equality 76 | - parameter lhs: The first address 77 | - parameter rhs: The second address 78 | - returns: `true` if the sender key names match 79 | */ 80 | public static func == (lhs: SignalSenderKeyName, rhs: SignalSenderKeyName) -> Bool { 81 | return lhs.groupId == rhs.groupId && lhs.sender == rhs.sender 82 | } 83 | 84 | } 85 | 86 | extension SignalSenderKeyName: Hashable { 87 | 88 | /// The hash of the sender key name 89 | public var hashValue: Int { 90 | return groupId.hashValue &+ sender.hashValue 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/device_consistency.h: -------------------------------------------------------------------------------- 1 | #ifndef DEVICE_CONSISTENCY_H 2 | #define DEVICE_CONSISTENCY_H 3 | 4 | #include "signal_protocol_types.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | int device_consistency_signature_create(device_consistency_signature **signature, 11 | const uint8_t *signature_data, size_t signature_len, 12 | const uint8_t *vrf_output_data, size_t vrf_output_len); 13 | 14 | signal_buffer *device_consistency_signature_get_signature(const device_consistency_signature *signature); 15 | signal_buffer *device_consistency_signature_get_vrf_output(const device_consistency_signature *signature); 16 | 17 | void device_consistency_signature_destroy(signal_type_base *type); 18 | 19 | int device_consistency_commitment_create(device_consistency_commitment **commitment, 20 | uint32_t generation, ec_public_key_list *identity_key_list, 21 | signal_context *global_context); 22 | 23 | uint32_t device_consistency_commitment_get_generation(const device_consistency_commitment *commitment); 24 | signal_buffer *device_consistency_commitment_get_serialized(const device_consistency_commitment *commitment); 25 | 26 | void device_consistency_commitment_destroy(signal_type_base *type); 27 | 28 | int device_consistency_message_create_from_pair(device_consistency_message **message, 29 | device_consistency_commitment *commitment, 30 | ec_key_pair *identity_key_pair, 31 | signal_context *global_context); 32 | int device_consistency_message_create_from_serialized(device_consistency_message **message, 33 | device_consistency_commitment *commitment, 34 | const uint8_t *serialized_data, size_t serialized_len, 35 | ec_public_key *identity_key, 36 | signal_context *global_context); 37 | 38 | signal_buffer *device_consistency_message_get_serialized(const device_consistency_message *message); 39 | device_consistency_signature *device_consistency_message_get_signature(const device_consistency_message *message); 40 | uint32_t device_consistency_signature_get_generation(const device_consistency_message *message); 41 | 42 | void device_consistency_message_destroy(signal_type_base *type); 43 | 44 | int device_consistency_code_generate_for(device_consistency_commitment *commitment, 45 | device_consistency_signature_list *signatures, 46 | char **code_string, 47 | signal_context *global_context); 48 | 49 | device_consistency_signature_list *device_consistency_signature_list_alloc(void); 50 | device_consistency_signature_list *device_consistency_signature_list_copy(const device_consistency_signature_list *list); 51 | int device_consistency_signature_list_push_back(device_consistency_signature_list *list, device_consistency_signature *value); 52 | unsigned int device_consistency_signature_list_size(const device_consistency_signature_list *list); 53 | device_consistency_signature *device_consistency_signature_list_at(const device_consistency_signature_list *list, unsigned int index); 54 | void device_consistency_signature_list_free(device_consistency_signature_list *list); 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif /* DEVICE_CONSISTENCY_H */ 61 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/ge.h: -------------------------------------------------------------------------------- 1 | #ifndef GE_H 2 | #define GE_H 3 | 4 | /* 5 | ge means group element. 6 | 7 | Here the group is the set of pairs (x,y) of field elements (see fe.h) 8 | satisfying -x^2 + y^2 = 1 + d x^2y^2 9 | where d = -121665/121666. 10 | 11 | Representations: 12 | ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z 13 | ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT 14 | ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T 15 | ge_precomp (Duif): (y+x,y-x,2dxy) 16 | */ 17 | 18 | #include "fe.h" 19 | 20 | typedef struct { 21 | fe X; 22 | fe Y; 23 | fe Z; 24 | } ge_p2; 25 | 26 | typedef struct { 27 | fe X; 28 | fe Y; 29 | fe Z; 30 | fe T; 31 | } ge_p3; 32 | 33 | typedef struct { 34 | fe X; 35 | fe Y; 36 | fe Z; 37 | fe T; 38 | } ge_p1p1; 39 | 40 | typedef struct { 41 | fe yplusx; 42 | fe yminusx; 43 | fe xy2d; 44 | } ge_precomp; 45 | 46 | typedef struct { 47 | fe YplusX; 48 | fe YminusX; 49 | fe Z; 50 | fe T2d; 51 | } ge_cached; 52 | 53 | #define ge_frombytes_negate_vartime crypto_sign_ed25519_ref10_ge_frombytes_negate_vartime 54 | #define ge_tobytes crypto_sign_ed25519_ref10_ge_tobytes 55 | #define ge_p3_tobytes crypto_sign_ed25519_ref10_ge_p3_tobytes 56 | 57 | #define ge_p2_0 crypto_sign_ed25519_ref10_ge_p2_0 58 | #define ge_p3_0 crypto_sign_ed25519_ref10_ge_p3_0 59 | #define ge_precomp_0 crypto_sign_ed25519_ref10_ge_precomp_0 60 | #define ge_p3_to_p2 crypto_sign_ed25519_ref10_ge_p3_to_p2 61 | #define ge_p3_to_cached crypto_sign_ed25519_ref10_ge_p3_to_cached 62 | #define ge_p1p1_to_p2 crypto_sign_ed25519_ref10_ge_p1p1_to_p2 63 | #define ge_p1p1_to_p3 crypto_sign_ed25519_ref10_ge_p1p1_to_p3 64 | #define ge_p2_dbl crypto_sign_ed25519_ref10_ge_p2_dbl 65 | #define ge_p3_dbl crypto_sign_ed25519_ref10_ge_p3_dbl 66 | 67 | #define ge_madd crypto_sign_ed25519_ref10_ge_madd 68 | #define ge_msub crypto_sign_ed25519_ref10_ge_msub 69 | #define ge_add crypto_sign_ed25519_ref10_ge_add 70 | #define ge_sub crypto_sign_ed25519_ref10_ge_sub 71 | #define ge_scalarmult_base crypto_sign_ed25519_ref10_ge_scalarmult_base 72 | #define ge_double_scalarmult_vartime crypto_sign_ed25519_ref10_ge_double_scalarmult_vartime 73 | 74 | extern void ge_tobytes(unsigned char *,const ge_p2 *); 75 | extern void ge_p3_tobytes(unsigned char *,const ge_p3 *); 76 | extern int ge_frombytes_negate_vartime(ge_p3 *,const unsigned char *); 77 | 78 | extern void ge_p2_0(ge_p2 *); 79 | extern void ge_p3_0(ge_p3 *); 80 | extern void ge_precomp_0(ge_precomp *); 81 | extern void ge_p3_to_p2(ge_p2 *,const ge_p3 *); 82 | extern void ge_p3_to_cached(ge_cached *,const ge_p3 *); 83 | extern void ge_p1p1_to_p2(ge_p2 *,const ge_p1p1 *); 84 | extern void ge_p1p1_to_p3(ge_p3 *,const ge_p1p1 *); 85 | extern void ge_p2_dbl(ge_p1p1 *,const ge_p2 *); 86 | extern void ge_p3_dbl(ge_p1p1 *,const ge_p3 *); 87 | 88 | extern void ge_madd(ge_p1p1 *,const ge_p3 *,const ge_precomp *); 89 | extern void ge_msub(ge_p1p1 *,const ge_p3 *,const ge_precomp *); 90 | extern void ge_add(ge_p1p1 *,const ge_p3 *,const ge_cached *); 91 | extern void ge_sub(ge_p1p1 *,const ge_p3 *,const ge_cached *); 92 | extern void ge_scalarmult_base(ge_p3 *,const unsigned char *); 93 | extern void ge_double_scalarmult_vartime(ge_p2 *,const unsigned char *,const ge_p3 *,const unsigned char *); 94 | 95 | #endif 96 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/session_pre_key.h: -------------------------------------------------------------------------------- 1 | #ifndef SESSION_PRE_KEY_H 2 | #define SESSION_PRE_KEY_H 3 | 4 | #include 5 | #include 6 | #include "signal_protocol_types.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #define PRE_KEY_MEDIUM_MAX_VALUE 0xFFFFFF 13 | 14 | /*------------------------------------------------------------------------*/ 15 | 16 | int session_pre_key_create(session_pre_key **pre_key, uint32_t id, ec_key_pair *key_pair); 17 | int session_pre_key_serialize(signal_buffer **buffer, const session_pre_key *pre_key); 18 | int session_pre_key_deserialize(session_pre_key **pre_key, const uint8_t *data, size_t len, signal_context *global_context); 19 | 20 | uint32_t session_pre_key_get_id(const session_pre_key *pre_key); 21 | ec_key_pair *session_pre_key_get_key_pair(const session_pre_key *pre_key); 22 | 23 | void session_pre_key_destroy(signal_type_base *type); 24 | 25 | /*------------------------------------------------------------------------*/ 26 | 27 | int session_signed_pre_key_create(session_signed_pre_key **pre_key, 28 | uint32_t id, uint64_t timestamp, ec_key_pair *key_pair, 29 | const uint8_t *signature, size_t signature_len); 30 | int session_signed_pre_key_serialize(signal_buffer **buffer, const session_signed_pre_key *pre_key); 31 | int session_signed_pre_key_deserialize(session_signed_pre_key **pre_key, const uint8_t *data, size_t len, signal_context *global_context); 32 | 33 | uint32_t session_signed_pre_key_get_id(const session_signed_pre_key *pre_key); 34 | uint64_t session_signed_pre_key_get_timestamp(const session_signed_pre_key *pre_key); 35 | ec_key_pair *session_signed_pre_key_get_key_pair(const session_signed_pre_key *pre_key); 36 | const uint8_t *session_signed_pre_key_get_signature(const session_signed_pre_key *pre_key); 37 | size_t session_signed_pre_key_get_signature_len(const session_signed_pre_key *pre_key); 38 | 39 | void session_signed_pre_key_destroy(signal_type_base *type); 40 | 41 | /*------------------------------------------------------------------------*/ 42 | 43 | int session_pre_key_bundle_create(session_pre_key_bundle **bundle, 44 | uint32_t registration_id, int device_id, uint32_t pre_key_id, 45 | ec_public_key *pre_key_public, 46 | uint32_t signed_pre_key_id, ec_public_key *signed_pre_key_public, 47 | const uint8_t *signed_pre_key_signature_data, size_t signed_pre_key_signature_len, 48 | ec_public_key *identity_key); 49 | 50 | uint32_t session_pre_key_bundle_get_registration_id(const session_pre_key_bundle *bundle); 51 | int session_pre_key_bundle_get_device_id(const session_pre_key_bundle *bundle); 52 | uint32_t session_pre_key_bundle_get_pre_key_id(const session_pre_key_bundle *bundle); 53 | ec_public_key *session_pre_key_bundle_get_pre_key(const session_pre_key_bundle *bundle); 54 | uint32_t session_pre_key_bundle_get_signed_pre_key_id(const session_pre_key_bundle *bundle); 55 | ec_public_key *session_pre_key_bundle_get_signed_pre_key(const session_pre_key_bundle *bundle); 56 | signal_buffer *session_pre_key_bundle_get_signed_pre_key_signature(const session_pre_key_bundle *bundle); 57 | ec_public_key *session_pre_key_bundle_get_identity_key(const session_pre_key_bundle *bundle); 58 | 59 | void session_pre_key_bundle_destroy(signal_type_base *type); 60 | 61 | #ifdef __cplusplus 62 | } 63 | #endif 64 | 65 | #endif /* SESSION_PRE_KEY_H */ 66 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Sessions/SessionPreKeyBundle.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SessionPreKeyBundle.swift 3 | // libsignal-protocol-swift iOS 4 | // 5 | // Created by User on 16.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | A session pre key bundle can be used to establish a new session. 14 | */ 15 | public struct SessionPreKeyBundle { 16 | 17 | /// The registration id of the remote party 18 | let registrationId: UInt32 19 | 20 | /// The device id of the remote party 21 | let deviceId: Int32 22 | 23 | /// The pre key id 24 | let preKeyId: UInt32 25 | 26 | /// The pre key public data 27 | let preKey: Data 28 | 29 | /// The signed pre key id 30 | let signedPreKeyId: UInt32 31 | 32 | /// The signed pre key public data 33 | let signedPreKey: Data 34 | 35 | /// The signature of the signed pre key 36 | let signature: Data 37 | 38 | /// The identity public key of the remote party 39 | let identityKey: Data 40 | 41 | /** 42 | Create a pre key bundle from the components 43 | - parameter registrationId: The registration id of the remote party 44 | - parameter deviceId: The device id of the remote party 45 | - parameter preKeyId: The pre key id 46 | - parameter preKey: The pre key public data 47 | - parameter signedPreKeyId: The signed pre key id 48 | - parameter signedPreKey: The signed pre key public data 49 | - parameter signature: The signature of the signed pre key 50 | - parameter identityKey: The identity public key of the remote party 51 | */ 52 | public init(registrationId: UInt32, 53 | deviceId: Int32, 54 | preKeyId: UInt32, 55 | preKey: Data, 56 | signedPreKeyId: UInt32, 57 | signedPreKey: Data, 58 | signature: Data, 59 | identityKey: Data) { 60 | self.registrationId = registrationId 61 | self.deviceId = deviceId 62 | self.preKeyId = preKeyId 63 | self.preKey = preKey 64 | self.signedPreKeyId = signedPreKeyId 65 | self.signedPreKey = signedPreKey 66 | self.signature = signature 67 | self.identityKey = identityKey 68 | } 69 | 70 | /// Convert the bundle to a `session_pre_key_bundle` pointer (needs to be freed) 71 | func pointer() throws -> OpaquePointer { 72 | 73 | // Convert pre key 74 | let preKeyPtr = try preKey.publicKeyPointer() 75 | defer { signal_type_unref(preKeyPtr) } 76 | 77 | // Convert signed pre key 78 | let signedPreKeyPtr = try signedPreKey.publicKeyPointer() 79 | defer { signal_type_unref(signedPreKeyPtr) } 80 | 81 | // Convert identity key 82 | let identityKeyPtr = try identityKey.publicKeyPointer() 83 | defer { signal_type_unref(identityKeyPtr) } 84 | 85 | var bundlePtr: OpaquePointer? = nil 86 | let result = signature.withUnsafeBytes { ptr in 87 | withUnsafeMutablePointer(to: &bundlePtr) { bPtr in 88 | session_pre_key_bundle_create( bPtr, registrationId, deviceId, preKeyId, preKeyPtr, signedPreKeyId, signedPreKeyPtr, ptr, signature.count, identityKeyPtr) 89 | } 90 | } 91 | guard result == 0 else { throw SignalError(value: result) } 92 | return bundlePtr! 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Sessions/SessionSignedPreKey.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SessionSignedPreKey.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 16.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | A signed pre key. 14 | */ 15 | public struct SessionSignedPreKey { 16 | 17 | /// The id of the signed pre key 18 | public let id: UInt32 19 | 20 | /// The creation timestamp of the key 21 | public let timestamp: UInt64 22 | 23 | /// The signature of the public key signed by the identity key 24 | public let signature: Data 25 | 26 | /// The key pair of the signed pre key 27 | public let keyPair: KeyPair 28 | 29 | /** 30 | Create a signed pre key from serialized data. 31 | - parameter data: The serialized data 32 | - throws: Errors of type `SignalError` 33 | */ 34 | public init(from data: Data) throws { 35 | var signedPreKey: OpaquePointer? = nil 36 | let result = data.withUnsafeBytes { dPtr in 37 | withUnsafeMutablePointer(to: &signedPreKey) { 38 | session_signed_pre_key_deserialize($0, dPtr, data.count, Signal.context) 39 | } 40 | } 41 | guard result == 0 else { throw SignalError(value: result) } 42 | defer { session_signed_pre_key_destroy(signedPreKey) } 43 | try self.init(pointer: signedPreKey!) 44 | } 45 | 46 | /** 47 | Create a key pair from a pointer. 48 | - parameter pointer: A `session_signed_pre_key` pointer 49 | - throws: Errors of type `SignalError` 50 | */ 51 | init(pointer: OpaquePointer) throws { 52 | 53 | let ptr = session_signed_pre_key_get_key_pair(pointer)! 54 | self.keyPair = try KeyPair(pointer: ptr) 55 | 56 | let signatureData = session_signed_pre_key_get_signature(pointer) 57 | let signatureLength = session_signed_pre_key_get_signature_len(pointer) 58 | guard signatureData != nil else { throw SignalError.unknownError } 59 | self.signature = Data(from: signatureData!, length: signatureLength) 60 | self.timestamp = session_signed_pre_key_get_timestamp(pointer) 61 | self.id = session_signed_pre_key_get_id(pointer) 62 | } 63 | 64 | /// Serialize the session signed pre key 65 | public func data() throws -> Data { 66 | let keyPairPtr = try keyPair.pointer() 67 | defer { ec_key_pair_destroy(keyPairPtr) } 68 | 69 | // Create pre key 70 | var signedPreKey: OpaquePointer? = nil 71 | var result = signature.withUnsafeBytes { ptr in 72 | withUnsafeMutablePointer(to: &signedPreKey) { 73 | return session_signed_pre_key_create($0, id, timestamp, keyPairPtr, ptr, signature.count) 74 | } 75 | } 76 | guard result == 0 else { throw SignalError(value: result) } 77 | defer { session_signed_pre_key_destroy(signedPreKey) } 78 | 79 | // Serialize pre key 80 | var signedPreKeyRecord: OpaquePointer? = nil 81 | result = withUnsafeMutablePointer(to: &signedPreKeyRecord) { 82 | session_signed_pre_key_serialize($0, signedPreKey) 83 | } 84 | guard result == 0 else { throw SignalError(value: result) } 85 | defer { signal_buffer_free(signedPreKeyRecord) } 86 | 87 | // Return serialized data 88 | return Data(signalBuffer: signedPreKeyRecord!) 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Misc/KeyPair.swift: -------------------------------------------------------------------------------- 1 | // 2 | // KeyPair.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 16.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | A key pair consists of an elliptic curve public key and corresponding private key. 14 | */ 15 | public struct KeyPair { 16 | 17 | /// The public key data 18 | public let publicKey: Data 19 | 20 | /// The private key data 21 | public let privateKey: Data 22 | 23 | /** 24 | Create a key pair from the components. 25 | - parameter publicKey: The public key data 26 | - parameter privateKey: The private key data 27 | */ 28 | public init(publicKey: Data, privateKey: Data) { 29 | self.publicKey = publicKey 30 | self.privateKey = privateKey 31 | } 32 | 33 | /** 34 | Create a key pair from a pointer. 35 | - parameter pointer: The ec_key_pair pointer. 36 | - throws: Errors of type `SignalError` 37 | */ 38 | init(pointer: OpaquePointer) throws { 39 | let pubKey = ec_key_pair_get_public(pointer)! 40 | let privKey = ec_key_pair_get_private(pointer)! 41 | 42 | var pubBuffer: OpaquePointer? = nil 43 | var result = withUnsafeMutablePointer(to: &pubBuffer) { 44 | ec_public_key_serialize($0, pubKey) 45 | } 46 | guard result == 0 else { throw SignalError(value: result) } 47 | 48 | var privBuffer: OpaquePointer? = nil 49 | result = withUnsafeMutablePointer(to: &privBuffer) { 50 | ec_private_key_serialize($0, privKey) 51 | } 52 | guard result == 0 else { throw SignalError(value: result) } 53 | 54 | self.publicKey = Data(signalBuffer: pubBuffer!) 55 | self.privateKey = Data(signalBuffer: privBuffer!) 56 | signal_buffer_free(pubBuffer) 57 | signal_buffer_free(privBuffer) 58 | } 59 | 60 | /// Convert to a pointer (must be freed through ec_key_pair_destroy) 61 | func pointer() throws -> OpaquePointer { 62 | // Convert public key 63 | let publicBuffer = publicKey.signalBuffer 64 | defer { signal_buffer_free(publicBuffer) } 65 | let pubLength = signal_buffer_len(publicBuffer) 66 | let pubData = signal_buffer_data(publicBuffer)! 67 | var pubKey: OpaquePointer? = nil 68 | var result = withUnsafeMutablePointer(to: &pubKey) { 69 | curve_decode_point($0, pubData, pubLength, Signal.context) 70 | } 71 | guard result == 0 else { throw SignalError(value: result) } 72 | 73 | // Convert private key 74 | let privateBuffer = privateKey.signalBuffer 75 | defer { signal_buffer_free(privateBuffer) } 76 | let privLength = signal_buffer_len(privateBuffer) 77 | let privData = signal_buffer_data(privateBuffer)! 78 | var privKey: OpaquePointer? = nil 79 | result = withUnsafeMutablePointer(to: &privKey) { 80 | curve_decode_private_point($0, privData, privLength, Signal.context) 81 | } 82 | guard result == 0 else { throw SignalError(value: result) } 83 | 84 | // Create key pair 85 | var keyPair: OpaquePointer? = nil 86 | result = withUnsafeMutablePointer(to: &keyPair) { 87 | ec_key_pair_create($0, pubKey, privKey) 88 | } 89 | guard result == 0 else { throw SignalError(value: result) } 90 | return keyPair! 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Misc/CiphertextMessage.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CiphertextMessage.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 16.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | A `CiphertextMessage` is an encrypted message ready for delivery. It can be one 14 | of several types. 15 | */ 16 | public struct CiphertextMessage { 17 | 18 | /// The message type of an encrypted message 19 | public enum MessageType: UInt8 { 20 | 21 | /// A signal message 22 | case signal = 2 23 | 24 | /// A pre key signal message to establish a session 25 | case preKey = 3 26 | 27 | /// A sender key message 28 | case senderKey = 4 29 | 30 | /// A sender key distribution message to esatblish a group session 31 | case distribution = 5 32 | 33 | /// Unknown message type 34 | case unknown 35 | 36 | /** 37 | Create a ciphertext message type from an Int32 value. 38 | - note: If the type value does not correspond to a recognised 39 | type, then the type will be `unknown` 40 | - parameter value: The raw value of the type 41 | */ 42 | init(_ value: Int32) { 43 | switch value { 44 | case 2...5: self = MessageType(rawValue: UInt8(value))! 45 | default: self = .unknown 46 | } 47 | } 48 | 49 | /** 50 | Create a ciphertext message type from a UInt8 value. 51 | - note: If the type value does not correspond to a recognised 52 | type, then the type will be `unknown` 53 | - parameter value: The raw value of the type 54 | */ 55 | init(_ value: UInt8) { 56 | switch value { 57 | case 2...5: self = MessageType(rawValue: value)! 58 | default: self = .unknown 59 | } 60 | } 61 | } 62 | 63 | /// The type of the message 64 | public let type: MessageType 65 | 66 | /// The message data 67 | public let message: Data 68 | 69 | /** 70 | Create a CiphertextMessage from type and data. 71 | - parameter type: The message type 72 | - parameter data: The encrypted data 73 | */ 74 | public init(type: MessageType, message: Data) { 75 | self.type = type 76 | self.message = message 77 | } 78 | 79 | /** 80 | Create a message from a `ciphertext_message` pointer 81 | */ 82 | init(pointer: OpaquePointer) { 83 | let messageType = ciphertext_message_get_type(pointer) 84 | self.type = MessageType(messageType) 85 | let ptr = ciphertext_message_get_serialized(pointer) 86 | self.message = Data(signalBuffer: ptr!) 87 | } 88 | 89 | /** 90 | Create a message from serialized data. 91 | - parameter data: The serialized message, containing the message type at the first byte. 92 | */ 93 | public init(from data: Data) { 94 | guard data.count > 0 else { 95 | self.type = .unknown 96 | self.message = Data() 97 | return 98 | } 99 | self.type = MessageType(data[0]) 100 | self.message = data.advanced(by: 1) 101 | } 102 | 103 | /// Serialize the message by prepending the message type to the message data 104 | public var data: Data { 105 | return [type.rawValue] + message 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/curve_sigs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "ge.h" 4 | #include "curve_sigs.h" 5 | #include "crypto_sign.h" 6 | #include "crypto_additions.h" 7 | 8 | int curve25519_sign(unsigned char* signature_out, 9 | const unsigned char* curve25519_privkey, 10 | const unsigned char* msg, const unsigned long msg_len, 11 | const unsigned char* random) 12 | { 13 | ge_p3 ed_pubkey_point; /* Ed25519 pubkey point */ 14 | unsigned char ed_pubkey[32]; /* Ed25519 encoded pubkey */ 15 | unsigned char *sigbuf; /* working buffer */ 16 | unsigned char sign_bit = 0; 17 | 18 | if ((sigbuf = malloc(msg_len + 128)) == 0) { 19 | memset(signature_out, 0, 64); 20 | return -1; 21 | } 22 | 23 | /* Convert the Curve25519 privkey to an Ed25519 public key */ 24 | ge_scalarmult_base(&ed_pubkey_point, curve25519_privkey); 25 | ge_p3_tobytes(ed_pubkey, &ed_pubkey_point); 26 | sign_bit = ed_pubkey[31] & 0x80; 27 | 28 | /* Perform an Ed25519 signature with explicit private key */ 29 | crypto_sign_modified(sigbuf, msg, msg_len, curve25519_privkey, 30 | ed_pubkey, random); 31 | memmove(signature_out, sigbuf, 64); 32 | 33 | /* Encode the sign bit into signature (in unused high bit of S) */ 34 | signature_out[63] &= 0x7F; /* bit should be zero already, but just in case */ 35 | signature_out[63] |= sign_bit; 36 | 37 | free(sigbuf); 38 | return 0; 39 | } 40 | 41 | int curve25519_verify(const unsigned char* signature, 42 | const unsigned char* curve25519_pubkey, 43 | const unsigned char* msg, const unsigned long msg_len) 44 | { 45 | fe u; 46 | fe y; 47 | unsigned char ed_pubkey[32]; 48 | unsigned char *verifybuf = NULL; /* working buffer */ 49 | unsigned char *verifybuf2 = NULL; /* working buffer #2 */ 50 | int result; 51 | 52 | if ((verifybuf = malloc(msg_len + 64)) == 0) { 53 | result = -1; 54 | goto err; 55 | } 56 | 57 | if ((verifybuf2 = malloc(msg_len + 64)) == 0) { 58 | result = -1; 59 | goto err; 60 | } 61 | 62 | /* Convert the Curve25519 public key into an Ed25519 public key. In 63 | particular, convert Curve25519's "montgomery" x-coordinate (u) into an 64 | Ed25519 "edwards" y-coordinate: 65 | 66 | y = (u - 1) / (u + 1) 67 | 68 | NOTE: u=-1 is converted to y=0 since fe_invert is mod-exp 69 | 70 | Then move the sign bit into the pubkey from the signature. 71 | */ 72 | fe_frombytes(u, curve25519_pubkey); 73 | fe_montx_to_edy(y, u); 74 | fe_tobytes(ed_pubkey, y); 75 | 76 | /* Copy the sign bit, and remove it from signature */ 77 | ed_pubkey[31] &= 0x7F; /* bit should be zero already, but just in case */ 78 | ed_pubkey[31] |= (signature[63] & 0x80); 79 | memmove(verifybuf, signature, 64); 80 | verifybuf[63] &= 0x7F; 81 | 82 | memmove(verifybuf+64, msg, msg_len); 83 | 84 | /* Then perform a normal Ed25519 verification, return 0 on success */ 85 | /* The below call has a strange API: */ 86 | /* verifybuf = R || S || message */ 87 | /* verifybuf2 = internal to next call gets a copy of verifybuf, S gets 88 | replaced with pubkey for hashing */ 89 | result = crypto_sign_open_modified(verifybuf2, verifybuf, 64 + msg_len, ed_pubkey); 90 | 91 | err: 92 | 93 | if (verifybuf != NULL) { 94 | free(verifybuf); 95 | } 96 | 97 | if (verifybuf2 != NULL) { 98 | free(verifybuf2); 99 | } 100 | 101 | return result; 102 | } 103 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/fe_tobytes.c: -------------------------------------------------------------------------------- 1 | #include "fe.h" 2 | 3 | /* 4 | Preconditions: 5 | |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. 6 | 7 | Write p=2^255-19; q=floor(h/p). 8 | Basic claim: q = floor(2^(-255)(h + 19 2^(-25)h9 + 2^(-1))). 9 | 10 | Proof: 11 | Have |h|<=p so |q|<=1 so |19^2 2^(-255) q|<1/4. 12 | Also have |h-2^230 h9|<2^231 so |19 2^(-255)(h-2^230 h9)|<1/4. 13 | 14 | Write y=2^(-1)-19^2 2^(-255)q-19 2^(-255)(h-2^230 h9). 15 | Then 0> 25; 53 | q = (h0 + q) >> 26; 54 | q = (h1 + q) >> 25; 55 | q = (h2 + q) >> 26; 56 | q = (h3 + q) >> 25; 57 | q = (h4 + q) >> 26; 58 | q = (h5 + q) >> 25; 59 | q = (h6 + q) >> 26; 60 | q = (h7 + q) >> 25; 61 | q = (h8 + q) >> 26; 62 | q = (h9 + q) >> 25; 63 | 64 | /* Goal: Output h-(2^255-19)q, which is between 0 and 2^255-20. */ 65 | h0 += 19 * q; 66 | /* Goal: Output h-2^255 q, which is between 0 and 2^255-20. */ 67 | 68 | carry0 = h0 >> 26; h1 += carry0; h0 -= carry0 << 26; 69 | carry1 = h1 >> 25; h2 += carry1; h1 -= carry1 << 25; 70 | carry2 = h2 >> 26; h3 += carry2; h2 -= carry2 << 26; 71 | carry3 = h3 >> 25; h4 += carry3; h3 -= carry3 << 25; 72 | carry4 = h4 >> 26; h5 += carry4; h4 -= carry4 << 26; 73 | carry5 = h5 >> 25; h6 += carry5; h5 -= carry5 << 25; 74 | carry6 = h6 >> 26; h7 += carry6; h6 -= carry6 << 26; 75 | carry7 = h7 >> 25; h8 += carry7; h7 -= carry7 << 25; 76 | carry8 = h8 >> 26; h9 += carry8; h8 -= carry8 << 26; 77 | carry9 = h9 >> 25; h9 -= carry9 << 25; 78 | /* h10 = carry9 */ 79 | 80 | /* 81 | Goal: Output h0+...+2^255 h10-2^255 q, which is between 0 and 2^255-20. 82 | Have h0+...+2^230 h9 between 0 and 2^255-1; 83 | evidently 2^255 h10-2^255 q = 0. 84 | Goal: Output h0+...+2^230 h9. 85 | */ 86 | 87 | s[0] = h0 >> 0; 88 | s[1] = h0 >> 8; 89 | s[2] = h0 >> 16; 90 | s[3] = (h0 >> 24) | (h1 << 2); 91 | s[4] = h1 >> 6; 92 | s[5] = h1 >> 14; 93 | s[6] = (h1 >> 22) | (h2 << 3); 94 | s[7] = h2 >> 5; 95 | s[8] = h2 >> 13; 96 | s[9] = (h2 >> 21) | (h3 << 5); 97 | s[10] = h3 >> 3; 98 | s[11] = h3 >> 11; 99 | s[12] = (h3 >> 19) | (h4 << 6); 100 | s[13] = h4 >> 2; 101 | s[14] = h4 >> 10; 102 | s[15] = h4 >> 18; 103 | s[16] = h5 >> 0; 104 | s[17] = h5 >> 8; 105 | s[18] = h5 >> 16; 106 | s[19] = (h5 >> 24) | (h6 << 1); 107 | s[20] = h6 >> 7; 108 | s[21] = h6 >> 15; 109 | s[22] = (h6 >> 23) | (h7 << 3); 110 | s[23] = h7 >> 5; 111 | s[24] = h7 >> 13; 112 | s[25] = (h7 >> 21) | (h8 << 4); 113 | s[26] = h8 >> 4; 114 | s[27] = h8 >> 12; 115 | s[28] = (h8 >> 20) | (h9 << 6); 116 | s[29] = h9 >> 2; 117 | s[30] = h9 >> 10; 118 | s[31] = h9 >> 18; 119 | } 120 | -------------------------------------------------------------------------------- /libsignal-protocol-swiftTests/FingerprintTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // FingerprintTests.swift 3 | // libsignal-protocol-swiftTests 4 | // 5 | // Created by User on 19.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | import SignalProtocol 11 | 12 | class FingerprintTests: XCTestCase { 13 | 14 | func testFingerprint() { 15 | 16 | let alice = try! Signal.generateIdentityKeyPair().publicKey 17 | let bob = try! Signal.generateIdentityKeyPair().publicKey 18 | 19 | let aliceAddress = "+14152222222" 20 | let bobAddress = "+14153333333" 21 | 22 | let iterations = 1024 23 | 24 | let aliceFingerprint: Fingerprint 25 | do { 26 | aliceFingerprint = try Fingerprint( 27 | iterations: iterations, 28 | localIdentifier: aliceAddress, localIdentity: alice, 29 | remoteIdentifier: bobAddress, remoteIdentity: bob) 30 | } catch { 31 | XCTFail("Could not create fingerprint for alice: \(error)") 32 | return 33 | } 34 | 35 | let bobFingerprint: Fingerprint 36 | do { 37 | bobFingerprint = try Fingerprint( 38 | iterations: iterations, 39 | localIdentifier: bobAddress, localIdentity: bob, 40 | remoteIdentifier: aliceAddress, remoteIdentity: alice) 41 | } catch { 42 | XCTFail("Could not create fingerprint for bob: \(error)") 43 | return 44 | } 45 | 46 | do { 47 | let equal = try aliceFingerprint.matches(scannable: bobFingerprint) 48 | XCTAssert(equal, "Fingerprints don't match") 49 | } catch { 50 | XCTFail("Could not match fingerprints: \(error)") 51 | return 52 | } 53 | } 54 | 55 | func testFingerprintLists() { 56 | let aliceKeys = [try! Signal.generateIdentityKeyPair().publicKey, 57 | try! Signal.generateIdentityKeyPair().publicKey, 58 | try! Signal.generateIdentityKeyPair().publicKey] 59 | 60 | let bobKeys = [try! Signal.generateIdentityKeyPair().publicKey, 61 | try! Signal.generateIdentityKeyPair().publicKey, 62 | try! Signal.generateIdentityKeyPair().publicKey] 63 | 64 | let aliceAddress = "+14152222222" 65 | let bobAddress = "+14153333333" 66 | 67 | let iterations = 1024 68 | 69 | let aliceFingerprint: Fingerprint 70 | do { 71 | aliceFingerprint = try Fingerprint( 72 | iterations: iterations, 73 | localIdentifier: aliceAddress, localIdentityList: aliceKeys, 74 | remoteIdentifier: bobAddress, remoteIdentityList: bobKeys) 75 | } catch { 76 | XCTFail("Could not create fingerprint for alice: \(error)") 77 | return 78 | } 79 | 80 | let bobFingerprint: Fingerprint 81 | do { 82 | bobFingerprint = try Fingerprint( 83 | iterations: iterations, 84 | localIdentifier: bobAddress, localIdentityList: bobKeys, 85 | remoteIdentifier: aliceAddress, remoteIdentityList: aliceKeys) 86 | } catch { 87 | XCTFail("Could not create fingerprint for bob: \(error)") 88 | return 89 | } 90 | 91 | do { 92 | let equal = try aliceFingerprint.matches(scannable: bobFingerprint) 93 | XCTAssert(equal, "Fingerprints don't match") 94 | let equal2 = try bobFingerprint.matches(scannable: aliceFingerprint) 95 | XCTAssert(equal2, "Fingerprints don't match") 96 | } catch { 97 | XCTFail("Could not match fingerprints: \(error)") 98 | return 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/curve25519/ed25519/additions/ge_scalarmult.c: -------------------------------------------------------------------------------- 1 | #include "crypto_uint32.h" 2 | #include "ge.h" 3 | #include "crypto_additions.h" 4 | 5 | static unsigned char equal(signed char b,signed char c) 6 | { 7 | unsigned char ub = b; 8 | unsigned char uc = c; 9 | unsigned char x = ub ^ uc; /* 0: yes; 1..255: no */ 10 | crypto_uint32 y = x; /* 0: yes; 1..255: no */ 11 | y -= 1; /* 4294967295: yes; 0..254: no */ 12 | y >>= 31; /* 1: yes; 0: no */ 13 | return y; 14 | } 15 | 16 | static unsigned char negative(signed char b) 17 | { 18 | unsigned long long x = b; /* 18446744073709551361..18446744073709551615: yes; 0..255: no */ 19 | x >>= 63; /* 1: yes; 0: no */ 20 | return x; 21 | } 22 | 23 | static void cmov(ge_cached *t,const ge_cached *u,unsigned char b) 24 | { 25 | fe_cmov(t->YplusX,u->YplusX,b); 26 | fe_cmov(t->YminusX,u->YminusX,b); 27 | fe_cmov(t->Z,u->Z,b); 28 | fe_cmov(t->T2d,u->T2d,b); 29 | } 30 | 31 | static void select(ge_cached *t,const ge_cached *pre, signed char b) 32 | { 33 | ge_cached minust; 34 | unsigned char bnegative = negative(b); 35 | unsigned char babs = b - (((-bnegative) & b) << 1); 36 | 37 | fe_1(t->YplusX); 38 | fe_1(t->YminusX); 39 | fe_1(t->Z); 40 | fe_0(t->T2d); 41 | 42 | cmov(t,pre+0,equal(babs,1)); 43 | cmov(t,pre+1,equal(babs,2)); 44 | cmov(t,pre+2,equal(babs,3)); 45 | cmov(t,pre+3,equal(babs,4)); 46 | cmov(t,pre+4,equal(babs,5)); 47 | cmov(t,pre+5,equal(babs,6)); 48 | cmov(t,pre+6,equal(babs,7)); 49 | cmov(t,pre+7,equal(babs,8)); 50 | fe_copy(minust.YplusX,t->YminusX); 51 | fe_copy(minust.YminusX,t->YplusX); 52 | fe_copy(minust.Z,t->Z); 53 | fe_neg(minust.T2d,t->T2d); 54 | cmov(t,&minust,bnegative); 55 | } 56 | 57 | /* 58 | h = a * B 59 | where a = a[0]+256*a[1]+...+256^31 a[31] 60 | B is the Ed25519 base point (x,4/5) with x positive. 61 | 62 | Preconditions: 63 | a[31] <= 127 64 | */ 65 | 66 | void ge_scalarmult(ge_p3 *h, const unsigned char *a, const ge_p3 *A) 67 | { 68 | signed char e[64]; 69 | signed char carry; 70 | ge_p1p1 r; 71 | ge_p2 s; 72 | ge_p3 t0, t1, t2; 73 | ge_cached t, pre[8]; 74 | int i; 75 | 76 | for (i = 0;i < 32;++i) { 77 | e[2 * i + 0] = (a[i] >> 0) & 15; 78 | e[2 * i + 1] = (a[i] >> 4) & 15; 79 | } 80 | /* each e[i] is between 0 and 15 */ 81 | /* e[63] is between 0 and 7 */ 82 | 83 | carry = 0; 84 | for (i = 0;i < 63;++i) { 85 | e[i] += carry; 86 | carry = e[i] + 8; 87 | carry >>= 4; 88 | e[i] -= carry << 4; 89 | } 90 | e[63] += carry; 91 | /* each e[i] is between -8 and 8 */ 92 | 93 | // Precomputation: 94 | ge_p3_to_cached(pre+0, A); // A 95 | 96 | ge_p3_dbl(&r, A); 97 | ge_p1p1_to_p3(&t0, &r); 98 | ge_p3_to_cached(pre+1, &t0); // 2A 99 | 100 | ge_add(&r, A, pre+1); 101 | ge_p1p1_to_p3(&t1, &r); 102 | ge_p3_to_cached(pre+2, &t1); // 3A 103 | 104 | ge_p3_dbl(&r, &t0); 105 | ge_p1p1_to_p3(&t0, &r); 106 | ge_p3_to_cached(pre+3, &t0); // 4A 107 | 108 | ge_add(&r, A, pre+3); 109 | ge_p1p1_to_p3(&t2, &r); 110 | ge_p3_to_cached(pre+4, &t2); // 5A 111 | 112 | ge_p3_dbl(&r, &t1); 113 | ge_p1p1_to_p3(&t1, &r); 114 | ge_p3_to_cached(pre+5, &t1); // 6A 115 | 116 | ge_add(&r, A, pre+5); 117 | ge_p1p1_to_p3(&t1, &r); 118 | ge_p3_to_cached(pre+6, &t1); // 7A 119 | 120 | ge_p3_dbl(&r, &t0); 121 | ge_p1p1_to_p3(&t0, &r); 122 | ge_p3_to_cached(pre+7, &t0); // 8A 123 | 124 | ge_p3_0(h); 125 | 126 | for (i = 63;i > 0; i--) { 127 | select(&t,pre,e[i]); 128 | ge_add(&r, h, &t); 129 | ge_p1p1_to_p2(&s,&r); 130 | 131 | ge_p2_dbl(&r,&s); ge_p1p1_to_p2(&s,&r); 132 | ge_p2_dbl(&r,&s); ge_p1p1_to_p2(&s,&r); 133 | ge_p2_dbl(&r,&s); ge_p1p1_to_p2(&s,&r); 134 | ge_p2_dbl(&r,&s); ge_p1p1_to_p3(h,&r); 135 | 136 | } 137 | select(&t,pre,e[0]); 138 | ge_add(&r, h, &t); 139 | ge_p1p1_to_p3(h,&r); 140 | } 141 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/signal_protocol_internal.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGNAL_PROTOCOL_INTERNAL_H 2 | #define SIGNAL_PROTOCOL_INTERNAL_H 3 | 4 | #include 5 | #include "LocalStorageProtocol.pb-c.h" 6 | #include "signal_protocol.h" 7 | 8 | struct signal_type_base { 9 | unsigned int ref_count; 10 | void (*destroy)(signal_type_base *instance); 11 | }; 12 | 13 | void signal_type_init(signal_type_base *instance, 14 | void (*destroy_func)(signal_type_base *instance)); 15 | 16 | #define SIGNAL_INIT(instance, destroy_func) signal_type_init((signal_type_base *)instance, destroy_func) 17 | 18 | struct signal_buffer { 19 | size_t len; 20 | uint8_t data[]; 21 | }; 22 | 23 | struct signal_context { 24 | signal_crypto_provider crypto_provider; 25 | void (*lock)(void *user_data); 26 | void (*unlock)(void *user_data); 27 | void (*log)(int level, const char *message, size_t len, void *user_data); 28 | void *user_data; 29 | }; 30 | 31 | int signal_crypto_random(signal_context *context, uint8_t *data, size_t len); 32 | 33 | int signal_hmac_sha256_init(signal_context *context, void **hmac_context, const uint8_t *key, size_t key_len); 34 | int signal_hmac_sha256_update(signal_context *context, void *hmac_context, const uint8_t *data, size_t data_len); 35 | int signal_hmac_sha256_final(signal_context *context, void *hmac_context, signal_buffer **output); 36 | void signal_hmac_sha256_cleanup(signal_context *context, void *hmac_context); 37 | 38 | int signal_sha512_digest_init(signal_context *context, void **digest_context); 39 | int signal_sha512_digest_update(signal_context *context, void *digest_context, const uint8_t *data, size_t data_len); 40 | int signal_sha512_digest_final(signal_context *context, void *digest_context, signal_buffer **output); 41 | void signal_sha512_digest_cleanup(signal_context *context, void *digest_context); 42 | 43 | 44 | int signal_encrypt(signal_context *context, 45 | signal_buffer **output, 46 | int cipher, 47 | const uint8_t *key, size_t key_len, 48 | const uint8_t *iv, size_t iv_len, 49 | const uint8_t *plaintext, size_t plaintext_len); 50 | 51 | int signal_decrypt(signal_context *context, 52 | signal_buffer **output, 53 | int cipher, 54 | const uint8_t *key, size_t key_len, 55 | const uint8_t *iv, size_t iv_len, 56 | const uint8_t *ciphertext, size_t ciphertext_len); 57 | 58 | void signal_lock(signal_context *context); 59 | void signal_unlock(signal_context *context); 60 | void signal_log(signal_context *context, int level, const char *format, ...); 61 | void signal_explicit_bzero(void *v, size_t n); 62 | int signal_constant_memcmp(const void *s1, const void *s2, size_t n); 63 | 64 | /*------------------------------------------------------------------------*/ 65 | 66 | /* 67 | * Functions used for internal protocol buffers serialization support. 68 | */ 69 | 70 | int ec_public_key_serialize_protobuf(ProtobufCBinaryData *buffer, const ec_public_key *key); 71 | int ec_private_key_serialize_protobuf(ProtobufCBinaryData *buffer, const ec_private_key *key); 72 | 73 | int ratchet_chain_key_get_key_protobuf(const ratchet_chain_key *chain_key, ProtobufCBinaryData *buffer); 74 | int ratchet_root_key_get_key_protobuf(const ratchet_root_key *root_key, ProtobufCBinaryData *buffer); 75 | 76 | int session_state_serialize_prepare(session_state *state, Textsecure__SessionStructure *session_structure); 77 | void session_state_serialize_prepare_free(Textsecure__SessionStructure *session_structure); 78 | int session_state_deserialize_protobuf(session_state **state, Textsecure__SessionStructure *session_structure, signal_context *global_context); 79 | 80 | int sender_key_state_serialize_prepare(sender_key_state *state, Textsecure__SenderKeyStateStructure *state_structure); 81 | void sender_key_state_serialize_prepare_free(Textsecure__SenderKeyStateStructure *state_structure); 82 | int sender_key_state_deserialize_protobuf(sender_key_state **state, Textsecure__SenderKeyStateStructure *state_structure, signal_context *global_context); 83 | 84 | void signal_protocol_str_serialize_protobuf(ProtobufCBinaryData *buffer, const char *str); 85 | char *signal_protocol_str_deserialize_protobuf(ProtobufCBinaryData *buffer); 86 | 87 | #endif /* SIGNAL_PROTOCOL_INTERNAL_H */ 88 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/Stores/SignalStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SignalStore.swift 3 | // libsignal-protocol-swift 4 | // 5 | // Created by User on 15.02.18. 6 | // Copyright © 2018 User. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import SignalModule 11 | 12 | /** 13 | A `SignalStore` provides access to all required data stores for a client. 14 | */ 15 | public final class SignalStore { 16 | 17 | /// Count of all active stores 18 | private static var instanceCount = 0 19 | 20 | /// Pointer to store, used to identify delegate in callbacks 21 | private let instanceId: UnsafeMutablePointer 22 | 23 | /// The store context pointer of the instance 24 | let storeContext: OpaquePointer 25 | 26 | /// The delegate that handles the identity key store operations 27 | public let identityKeyStore: IdentityKeyStore 28 | 29 | /// The delegate that handles the pre key store operations 30 | public let preKeyStore: PreKeyStore 31 | 32 | /// The delegate that handles the session store operations 33 | public let sessionStore: SessionStore 34 | 35 | /// The delegate that handles the signed pre key store operations 36 | public let signedPreKeyStore: SignedPreKeyStore 37 | 38 | /// The delegate that handles the sender key store operations, optional 39 | public let senderKeyStore: SenderKeyStore? 40 | 41 | /** 42 | Create a `SignalStore` with the necessary delegates. 43 | - parameter identityKeyStore: The identity key store for this instance 44 | - parameter preKeyStore: The pre key store for this instance 45 | - parameter sessionStore: The session store for this instance 46 | - parameter signedPreKeyStore: The signed pre key store for this instance 47 | - parameter senderKeyStore: The (optional) sender key store for this instance, only needed for group messages 48 | - throws: Errors of type `SignalError` 49 | */ 50 | public init(identityKeyStore: IdentityKeyStore, 51 | preKeyStore: PreKeyStore, 52 | sessionStore: SessionStore, 53 | signedPreKeyStore: SignedPreKeyStore, 54 | senderKeyStore: SenderKeyStore?) throws { 55 | 56 | var store: OpaquePointer? = nil 57 | let result = withUnsafeMutablePointer(to: &store) { 58 | signal_protocol_store_context_create($0, Signal.context) 59 | } 60 | guard result == 0 else { throw SignalError(value: result) } 61 | 62 | self.instanceId = UnsafeMutablePointer.allocate(capacity: 1) 63 | self.instanceId.pointee = SignalStore.instanceCount 64 | SignalStore.instanceCount += 1 65 | 66 | self.storeContext = store! 67 | self.identityKeyStore = identityKeyStore 68 | self.preKeyStore = preKeyStore 69 | self.senderKeyStore = senderKeyStore 70 | self.sessionStore = sessionStore 71 | self.signedPreKeyStore = signedPreKeyStore 72 | 73 | try registerDelegates() 74 | } 75 | 76 | /// Add delegates to wrapper classes 77 | private func registerDelegates() throws { 78 | try IdentityKeyStoreWrapper.setStore(in: storeContext, delegate: identityKeyStore, userData: instanceId) 79 | try PreKeyStoreWrapper.setStore(in: storeContext, delegate: preKeyStore, userData: instanceId) 80 | try SessionStoreWrapper.setStore(in: storeContext, delegate: sessionStore, userData: instanceId) 81 | try SignedPreKeyStoreWrapper.setStore(in: storeContext, delegate: signedPreKeyStore, userData: instanceId) 82 | if senderKeyStore != nil { 83 | try SenderKeyStoreWrapper.setStore(in: storeContext, delegate: senderKeyStore!, userData: instanceId) 84 | } 85 | } 86 | 87 | /// Remove delegates from wrapper classes 88 | private func unregisterDelegates() { 89 | let id = instanceId.pointee 90 | 91 | IdentityKeyStoreWrapper.removeDelegate(for: id) 92 | PreKeyStoreWrapper.removeDelegate(for: id) 93 | SenderKeyStoreWrapper.removeDelegate(for: id) 94 | SessionStoreWrapper.removeDelegate(for: id) 95 | SignedPreKeyStoreWrapper.removeDelegate(for: id) 96 | } 97 | 98 | deinit { 99 | unregisterDelegates() 100 | self.instanceId.deallocate() 101 | signal_protocol_store_context_destroy(storeContext) 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/signal_protocol_types.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGNAL_PROTOCOL_TYPES_H 2 | #define SIGNAL_PROTOCOL_TYPES_H 3 | 4 | #include 5 | #include 6 | 7 | #ifndef _WINDOWS 8 | #include 9 | #else 10 | #include 11 | typedef SSIZE_T ssize_t; 12 | #endif 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | /* 19 | * Base library types 20 | */ 21 | typedef struct signal_type_base signal_type_base; 22 | typedef struct signal_buffer signal_buffer; 23 | typedef struct signal_buffer_list signal_buffer_list; 24 | typedef struct signal_int_list signal_int_list; 25 | 26 | /* 27 | * Global context for the Signal Protocol library 28 | */ 29 | typedef struct signal_context signal_context; 30 | 31 | /* 32 | * Context for the Signal Protocol data store implementation 33 | */ 34 | typedef struct signal_protocol_store_context signal_protocol_store_context; 35 | 36 | /* 37 | * Address of an Signal Protocol message recipient 38 | */ 39 | typedef struct signal_protocol_address { 40 | const char *name; 41 | size_t name_len; 42 | int32_t device_id; 43 | } signal_protocol_address; 44 | 45 | /* 46 | * A representation of a (group + sender + device) tuple 47 | */ 48 | typedef struct signal_protocol_sender_key_name { 49 | const char *group_id; 50 | size_t group_id_len; 51 | signal_protocol_address sender; 52 | } signal_protocol_sender_key_name; 53 | 54 | /* 55 | * Curve key types 56 | */ 57 | typedef struct ec_public_key ec_public_key; 58 | typedef struct ec_private_key ec_private_key; 59 | typedef struct ec_key_pair ec_key_pair; 60 | typedef struct ec_public_key_list ec_public_key_list; 61 | 62 | /* 63 | * HKDF types 64 | */ 65 | typedef struct hkdf_context hkdf_context; 66 | 67 | /* 68 | * Key helper types 69 | */ 70 | typedef struct signal_protocol_key_helper_pre_key_list_node signal_protocol_key_helper_pre_key_list_node; 71 | 72 | /* 73 | * Protocol types 74 | */ 75 | typedef struct ciphertext_message ciphertext_message; 76 | typedef struct signal_message signal_message; 77 | typedef struct pre_key_signal_message pre_key_signal_message; 78 | typedef struct sender_key_message sender_key_message; 79 | typedef struct sender_key_distribution_message sender_key_distribution_message; 80 | 81 | /* 82 | * Ratchet types 83 | */ 84 | #define RATCHET_CIPHER_KEY_LENGTH 32 85 | #define RATCHET_MAC_KEY_LENGTH 32 86 | #define RATCHET_IV_LENGTH 16 87 | 88 | typedef struct ratchet_chain_key ratchet_chain_key; 89 | typedef struct ratchet_root_key ratchet_root_key; 90 | typedef struct ratchet_identity_key_pair ratchet_identity_key_pair; 91 | 92 | typedef struct ratchet_message_keys { 93 | uint8_t cipher_key[RATCHET_CIPHER_KEY_LENGTH]; 94 | uint8_t mac_key[RATCHET_MAC_KEY_LENGTH]; 95 | uint8_t iv[RATCHET_IV_LENGTH]; 96 | uint32_t counter; 97 | } ratchet_message_keys; 98 | 99 | /* 100 | * Session types 101 | */ 102 | typedef struct session_pre_key session_pre_key; 103 | typedef struct session_signed_pre_key session_signed_pre_key; 104 | typedef struct session_pre_key_bundle session_pre_key_bundle; 105 | typedef struct session_builder session_builder; 106 | typedef struct session_record session_record; 107 | typedef struct session_record_state_node session_record_state_node; 108 | typedef struct session_state session_state; 109 | typedef struct session_cipher session_cipher; 110 | 111 | /* 112 | * Group types 113 | */ 114 | typedef struct sender_message_key sender_message_key; 115 | typedef struct sender_chain_key sender_chain_key; 116 | typedef struct sender_key_state sender_key_state; 117 | typedef struct sender_key_record sender_key_record; 118 | typedef struct group_session_builder group_session_builder; 119 | typedef struct group_cipher group_cipher; 120 | 121 | /* 122 | * Fingerprint types 123 | */ 124 | typedef struct fingerprint fingerprint; 125 | typedef struct displayable_fingerprint displayable_fingerprint; 126 | typedef struct scannable_fingerprint scannable_fingerprint; 127 | typedef struct fingerprint_generator fingerprint_generator; 128 | 129 | /* 130 | * Device consistency types 131 | */ 132 | typedef struct device_consistency_signature device_consistency_signature; 133 | typedef struct device_consistency_commitment device_consistency_commitment; 134 | typedef struct device_consistency_message device_consistency_message; 135 | typedef struct device_consistency_signature_list device_consistency_signature_list; 136 | 137 | #ifdef __cplusplus 138 | } 139 | #endif 140 | 141 | #endif /* SIGNAL_PROTOCOL_TYPES_H */ 142 | -------------------------------------------------------------------------------- /libsignal-protocol-swift/libsignal-protocol-c/group_cipher.h: -------------------------------------------------------------------------------- 1 | #ifndef GROUP_CIPHER_H 2 | #define GROUP_CIPHER_H 3 | 4 | #include 5 | #include 6 | #include "signal_protocol_types.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | /* 13 | * The main entry point for Signal Protocol group encrypt/decrypt operations. 14 | * 15 | * Once a session has been established with group_session_builder and a 16 | * sender_key_distribution_message has been distributed to each member of 17 | * the group, this class can be used for all subsequent encrypt/decrypt 18 | * operations within that session (i.e. until group membership changes). 19 | */ 20 | 21 | /** 22 | * Construct a group cipher for encrypt/decrypt operations. 23 | * 24 | * The store and global contexts must remain valid for the lifetime of the 25 | * group cipher. 26 | * 27 | * When finished, free the returned instance by calling group_cipher_free(). 28 | * 29 | * @param cipher set to a freshly allocated group cipher instance 30 | * @param store the signal_protocol_store_context to store all state information in 31 | * @param sender_key_id the sender that messages will be encrypted to or decrypted from 32 | * @param global_context the global library context 33 | * @return 0 on success, or negative on failure 34 | */ 35 | int group_cipher_create(group_cipher **cipher, 36 | signal_protocol_store_context *store, const signal_protocol_sender_key_name *sender_key_id, 37 | signal_context *global_context); 38 | 39 | /** 40 | * Set the optional user data pointer for the group cipher. 41 | * 42 | * This is to give callback functions a way of accessing app specific 43 | * context information for this cipher. 44 | */ 45 | void group_cipher_set_user_data(group_cipher *cipher, void *user_data); 46 | 47 | /** 48 | * Get the optional user data pointer for the group cipher. 49 | * 50 | * This is to give callback functions a way of accessing app specific 51 | * context information for this cipher. 52 | */ 53 | void *group_cipher_get_user_data(group_cipher *cipher); 54 | 55 | /** 56 | * Set the callback function that is called during the decrypt process. 57 | * 58 | * The callback function is called from within group_cipher_decrypt() after 59 | * decryption is complete but before the updated session state has been 60 | * committed to the session store. If the callback function returns a 61 | * negative value, then the decrypt function will immediately fail with 62 | * an error. 63 | * 64 | * This a callback allows some implementations to store the committed plaintext 65 | * to their local message store first, in case they are concerned with a crash 66 | * or write error happening between the time the session state is updated but 67 | * before they're able to successfully store the plaintext to disk. 68 | * 69 | * @param callback the callback function to set 70 | */ 71 | void group_cipher_set_decryption_callback(group_cipher *cipher, 72 | int (*callback)(group_cipher *cipher, signal_buffer *plaintext, void *decrypt_context)); 73 | 74 | /** 75 | * Encrypt a message. 76 | * 77 | * @param padded_plaintext The plaintext message bytes, optionally padded to a constant multiple. 78 | * @param padded_plaintext_len The length of the data pointed to by padded_message 79 | * @param encrypted_message Set to a ciphertext message encrypted to the group+sender+device tuple. 80 | * 81 | * @retval SG_SUCCESS Success 82 | * @retval SG_ERR_NO_SESSION if there is no established session for this contact. 83 | * @retval SG_ERR_INVALID_KEY if there is no valid private key for this session. 84 | */ 85 | int group_cipher_encrypt(group_cipher *cipher, 86 | const uint8_t *padded_plaintext, size_t padded_plaintext_len, 87 | ciphertext_message **encrypted_message); 88 | 89 | /** 90 | * Decrypt a message. 91 | * 92 | * @param ciphertext The sender_key_message to decrypt. 93 | * @param decrypt_context Optional context pointer associated with the 94 | * ciphertext, which is passed to the decryption callback function 95 | * @param plaintext Set to a newly allocated buffer containing the plaintext. 96 | * 97 | * @retval SG_SUCCESS Success 98 | * @retval SG_ERR_INVALID_MESSAGE if the input is not valid ciphertext. 99 | * @retval SG_ERR_DUPLICATE_MESSAGE if the input is a message that has already been received. 100 | * @retval SG_ERR_LEGACY_MESSAGE if the input is a message formatted by a protocol version that 101 | * is no longer supported. 102 | * @retval SG_ERR_NO_SESSION if there is no established session for this contact. 103 | */ 104 | int group_cipher_decrypt(group_cipher *cipher, 105 | sender_key_message *ciphertext, void *decrypt_context, 106 | signal_buffer **plaintext); 107 | 108 | void group_cipher_free(group_cipher *cipher); 109 | 110 | #ifdef __cplusplus 111 | } 112 | #endif 113 | 114 | #endif /* GROUP_CIPHER_H */ 115 | --------------------------------------------------------------------------------