LCOV - code coverage report
Current view: top level - lib/src/utils/crypto - ffi.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 20 29 69.0 %
Date: 2024-07-12 20:20:16 Functions: 0 0 -

          Line data    Source code
       1             : /*
       2             :  *   Famedly Matrix SDK
       3             :  *   Copyright (C) 2019, 2020, 2021 Famedly GmbH
       4             :  *
       5             :  *   This program is free software: you can redistribute it and/or modify
       6             :  *   it under the terms of the GNU Affero General Public License as
       7             :  *   published by the Free Software Foundation, either version 3 of the
       8             :  *   License, or (at your option) any later version.
       9             :  *
      10             :  *   This program is distributed in the hope that it will be useful,
      11             :  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      13             :  *   GNU Affero General Public License for more details.
      14             :  *
      15             :  *   You should have received a copy of the GNU Affero General Public License
      16             :  *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
      17             :  */
      18             : 
      19             : import 'dart:ffi';
      20             : import 'dart:io';
      21             : 
      22          27 : final libcrypto = () {
      23           9 :   if (Platform.isIOS) {
      24           0 :     return DynamicLibrary.process();
      25           9 :   } else if (Platform.isAndroid) {
      26           0 :     return DynamicLibrary.open('libcrypto.so');
      27           9 :   } else if (Platform.isWindows) {
      28           0 :     return DynamicLibrary.open('libcrypto.dll');
      29           9 :   } else if (Platform.isMacOS) {
      30             :     try {
      31           0 :       return DynamicLibrary.open('libcrypto.3.dylib');
      32             :     } catch (_) {
      33           0 :       return DynamicLibrary.open('libcrypto.1.1.dylib');
      34             :     }
      35             :   } else {
      36             :     try {
      37           9 :       return DynamicLibrary.open('libcrypto.so.3');
      38             :     } catch (_) {
      39           0 :       return DynamicLibrary.open('libcrypto.so.1.1');
      40             :     }
      41             :   }
      42           9 : }();
      43             : 
      44           6 : final PKCS5_PBKDF2_HMAC = libcrypto.lookupFunction<
      45             :     IntPtr Function(
      46             :         Pointer<Uint8> pass,
      47             :         IntPtr passlen,
      48             :         Pointer<Uint8> salt,
      49             :         IntPtr saltlen,
      50             :         IntPtr iter,
      51             :         Pointer<NativeType> digest,
      52             :         IntPtr keylen,
      53             :         Pointer<Uint8> out),
      54             :     int Function(
      55             :         Pointer<Uint8> pass,
      56             :         int passlen,
      57             :         Pointer<Uint8> salt,
      58             :         int saltlen,
      59             :         int iter,
      60             :         Pointer<NativeType> digest,
      61             :         int keylen,
      62             :         Pointer<Uint8> out)>('PKCS5_PBKDF2_HMAC');
      63             : 
      64           0 : final EVP_sha1 = libcrypto.lookupFunction<Pointer<NativeType> Function(),
      65             :     Pointer<NativeType> Function()>('EVP_sha1');
      66             : 
      67           6 : final EVP_sha256 = libcrypto.lookupFunction<Pointer<NativeType> Function(),
      68             :     Pointer<NativeType> Function()>('EVP_sha256');
      69             : 
      70           6 : final EVP_sha512 = libcrypto.lookupFunction<Pointer<NativeType> Function(),
      71             :     Pointer<NativeType> Function()>('EVP_sha512');
      72             : 
      73           0 : final EVP_aes_128_ctr = libcrypto.lookupFunction<Pointer<NativeType> Function(),
      74             :     Pointer<NativeType> Function()>('EVP_aes_128_ctr');
      75             : 
      76          27 : final EVP_aes_256_ctr = libcrypto.lookupFunction<Pointer<NativeType> Function(),
      77             :     Pointer<NativeType> Function()>('EVP_aes_256_ctr');
      78             : 
      79          27 : final EVP_CIPHER_CTX_new = libcrypto.lookupFunction<
      80             :     Pointer<NativeType> Function(),
      81             :     Pointer<NativeType> Function()>('EVP_CIPHER_CTX_new');
      82             : 
      83          27 : final EVP_EncryptInit_ex = libcrypto.lookupFunction<
      84             :     Pointer<NativeType> Function(
      85             :         Pointer<NativeType> ctx,
      86             :         Pointer<NativeType> alg,
      87             :         Pointer<NativeType> some,
      88             :         Pointer<Uint8> key,
      89             :         Pointer<Uint8> iv),
      90             :     Pointer<NativeType> Function(
      91             :         Pointer<NativeType> ctx,
      92             :         Pointer<NativeType> alg,
      93             :         Pointer<NativeType> some,
      94             :         Pointer<Uint8> key,
      95             :         Pointer<Uint8> iv)>('EVP_EncryptInit_ex');
      96             : 
      97          27 : final EVP_EncryptUpdate = libcrypto.lookupFunction<
      98             :     Pointer<NativeType> Function(Pointer<NativeType> ctx, Pointer<Uint8> output,
      99             :         Pointer<IntPtr> outputLen, Pointer<Uint8> input, IntPtr inputLen),
     100             :     Pointer<NativeType> Function(
     101             :         Pointer<NativeType> ctx,
     102             :         Pointer<Uint8> output,
     103             :         Pointer<IntPtr> outputLen,
     104             :         Pointer<Uint8> input,
     105             :         int inputLen)>('EVP_EncryptUpdate');
     106             : 
     107          27 : final EVP_EncryptFinal_ex = libcrypto.lookupFunction<
     108             :     Pointer<NativeType> Function(
     109             :         Pointer<NativeType> ctx, Pointer<Uint8> data, Pointer<IntPtr> len),
     110             :     Pointer<NativeType> Function(Pointer<NativeType> ctx, Pointer<Uint8> data,
     111             :         Pointer<IntPtr> len)>('EVP_EncryptFinal_ex');
     112             : 
     113          27 : final EVP_CIPHER_CTX_free = libcrypto.lookupFunction<
     114             :     Pointer<NativeType> Function(Pointer<NativeType> ctx),
     115             :     Pointer<NativeType> Function(
     116             :         Pointer<NativeType> ctx)>('EVP_CIPHER_CTX_free');
     117             : 
     118           6 : final EVP_Digest = libcrypto.lookupFunction<
     119             :     IntPtr Function(
     120             :         Pointer<Uint8> data,
     121             :         IntPtr len,
     122             :         Pointer<Uint8> hash,
     123             :         Pointer<IntPtr> hsize,
     124             :         Pointer<NativeType> alg,
     125             :         Pointer<NativeType> engine),
     126             :     int Function(
     127             :         Pointer<Uint8> data,
     128             :         int len,
     129             :         Pointer<Uint8> hash,
     130             :         Pointer<IntPtr> hsize,
     131             :         Pointer<NativeType> alg,
     132             :         Pointer<NativeType> engine)>('EVP_Digest');
     133             : 
     134           6 : final EVP_MD_size = () {
     135             :   // EVP_MD_size was renamed to EVP_MD_get_size in Openssl3.0.
     136             :   // There is an alias macro, but those don't exist in libraries.
     137             :   // Try loading the new name first, then fall back to the old one if not found.
     138             :   try {
     139           2 :     return libcrypto.lookupFunction<IntPtr Function(Pointer<NativeType> ctx),
     140             :         int Function(Pointer<NativeType> ctx)>('EVP_MD_get_size');
     141             :   } catch (e) {
     142           0 :     return libcrypto.lookupFunction<IntPtr Function(Pointer<NativeType> ctx),
     143             :         int Function(Pointer<NativeType> ctx)>('EVP_MD_size');
     144             :   }
     145           2 : }();

Generated by: LCOV version 1.14