/* * Famedly Matrix SDK * Copyright (C) 2019, 2020, 2021 Famedly GmbH * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ import 'dart:ffi'; import 'dart:io'; final libcrypto = () { if (Platform.isIOS) { return DynamicLibrary.process(); } else if (Platform.isAndroid) { return DynamicLibrary.open('libcrypto.so'); } else if (Platform.isWindows) { return DynamicLibrary.open('libcrypto.dll'); } else if (Platform.isMacOS) { try { return DynamicLibrary.open('libcrypto.3.dylib'); } catch (_) { return DynamicLibrary.open('libcrypto.1.1.dylib'); } } else { try { return DynamicLibrary.open('libcrypto.so.3'); } catch (_) { return DynamicLibrary.open('libcrypto.so.1.1'); } } }(); final PKCS5_PBKDF2_HMAC = libcrypto.lookupFunction< IntPtr Function( Pointer pass, IntPtr passlen, Pointer salt, IntPtr saltlen, IntPtr iter, Pointer digest, IntPtr keylen, Pointer out, ), int Function( Pointer pass, int passlen, Pointer salt, int saltlen, int iter, Pointer digest, int keylen, Pointer out, )>('PKCS5_PBKDF2_HMAC'); final EVP_sha1 = libcrypto.lookupFunction Function(), Pointer Function()>('EVP_sha1'); final EVP_sha256 = libcrypto.lookupFunction Function(), Pointer Function()>('EVP_sha256'); final EVP_sha512 = libcrypto.lookupFunction Function(), Pointer Function()>('EVP_sha512'); final EVP_aes_128_ctr = libcrypto.lookupFunction Function(), Pointer Function()>('EVP_aes_128_ctr'); final EVP_aes_256_ctr = libcrypto.lookupFunction Function(), Pointer Function()>('EVP_aes_256_ctr'); final EVP_CIPHER_CTX_new = libcrypto.lookupFunction< Pointer Function(), Pointer Function()>('EVP_CIPHER_CTX_new'); final EVP_EncryptInit_ex = libcrypto.lookupFunction< Pointer Function( Pointer ctx, Pointer alg, Pointer some, Pointer key, Pointer iv, ), Pointer Function( Pointer ctx, Pointer alg, Pointer some, Pointer key, Pointer iv, )>('EVP_EncryptInit_ex'); final EVP_EncryptUpdate = libcrypto.lookupFunction< Pointer Function( Pointer ctx, Pointer output, Pointer outputLen, Pointer input, IntPtr inputLen, ), Pointer Function( Pointer ctx, Pointer output, Pointer outputLen, Pointer input, int inputLen, )>('EVP_EncryptUpdate'); final EVP_EncryptFinal_ex = libcrypto.lookupFunction< Pointer Function( Pointer ctx, Pointer data, Pointer len, ), Pointer Function( Pointer ctx, Pointer data, Pointer len, )>('EVP_EncryptFinal_ex'); final EVP_CIPHER_CTX_free = libcrypto.lookupFunction< Pointer Function(Pointer ctx), Pointer Function( Pointer ctx, )>('EVP_CIPHER_CTX_free'); final EVP_Digest = libcrypto.lookupFunction< IntPtr Function( Pointer data, IntPtr len, Pointer hash, Pointer hsize, Pointer alg, Pointer engine, ), int Function( Pointer data, int len, Pointer hash, Pointer hsize, Pointer alg, Pointer engine, )>('EVP_Digest'); final EVP_MD_size = () { // EVP_MD_size was renamed to EVP_MD_get_size in Openssl3.0. // There is an alias macro, but those don't exist in libraries. // Try loading the new name first, then fall back to the old one if not found. try { return libcrypto.lookupFunction ctx), int Function(Pointer ctx)>('EVP_MD_get_size'); } catch (e) { return libcrypto.lookupFunction ctx), int Function(Pointer ctx)>('EVP_MD_size'); } }();