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-11-12 07:37:08 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             :     ),
      55             :     int Function(
      56             :       Pointer<Uint8> pass,
      57             :       int passlen,
      58             :       Pointer<Uint8> salt,
      59             :       int saltlen,
      60             :       int iter,
      61             :       Pointer<NativeType> digest,
      62             :       int keylen,
      63             :       Pointer<Uint8> out,
      64             :     )>('PKCS5_PBKDF2_HMAC');
      65             : 
      66           0 : final EVP_sha1 = libcrypto.lookupFunction<Pointer<NativeType> Function(),
      67             :     Pointer<NativeType> Function()>('EVP_sha1');
      68             : 
      69           6 : final EVP_sha256 = libcrypto.lookupFunction<Pointer<NativeType> Function(),
      70             :     Pointer<NativeType> Function()>('EVP_sha256');
      71             : 
      72           6 : final EVP_sha512 = libcrypto.lookupFunction<Pointer<NativeType> Function(),
      73             :     Pointer<NativeType> Function()>('EVP_sha512');
      74             : 
      75           0 : final EVP_aes_128_ctr = libcrypto.lookupFunction<Pointer<NativeType> Function(),
      76             :     Pointer<NativeType> Function()>('EVP_aes_128_ctr');
      77             : 
      78          27 : final EVP_aes_256_ctr = libcrypto.lookupFunction<Pointer<NativeType> Function(),
      79             :     Pointer<NativeType> Function()>('EVP_aes_256_ctr');
      80             : 
      81          27 : final EVP_CIPHER_CTX_new = libcrypto.lookupFunction<
      82             :     Pointer<NativeType> Function(),
      83             :     Pointer<NativeType> Function()>('EVP_CIPHER_CTX_new');
      84             : 
      85          27 : final EVP_EncryptInit_ex = libcrypto.lookupFunction<
      86             :     Pointer<NativeType> Function(
      87             :       Pointer<NativeType> ctx,
      88             :       Pointer<NativeType> alg,
      89             :       Pointer<NativeType> some,
      90             :       Pointer<Uint8> key,
      91             :       Pointer<Uint8> iv,
      92             :     ),
      93             :     Pointer<NativeType> Function(
      94             :       Pointer<NativeType> ctx,
      95             :       Pointer<NativeType> alg,
      96             :       Pointer<NativeType> some,
      97             :       Pointer<Uint8> key,
      98             :       Pointer<Uint8> iv,
      99             :     )>('EVP_EncryptInit_ex');
     100             : 
     101          27 : final EVP_EncryptUpdate = libcrypto.lookupFunction<
     102             :     Pointer<NativeType> Function(
     103             :       Pointer<NativeType> ctx,
     104             :       Pointer<Uint8> output,
     105             :       Pointer<IntPtr> outputLen,
     106             :       Pointer<Uint8> input,
     107             :       IntPtr inputLen,
     108             :     ),
     109             :     Pointer<NativeType> Function(
     110             :       Pointer<NativeType> ctx,
     111             :       Pointer<Uint8> output,
     112             :       Pointer<IntPtr> outputLen,
     113             :       Pointer<Uint8> input,
     114             :       int inputLen,
     115             :     )>('EVP_EncryptUpdate');
     116             : 
     117          27 : final EVP_EncryptFinal_ex = libcrypto.lookupFunction<
     118             :     Pointer<NativeType> Function(
     119             :       Pointer<NativeType> ctx,
     120             :       Pointer<Uint8> data,
     121             :       Pointer<IntPtr> len,
     122             :     ),
     123             :     Pointer<NativeType> Function(
     124             :       Pointer<NativeType> ctx,
     125             :       Pointer<Uint8> data,
     126             :       Pointer<IntPtr> len,
     127             :     )>('EVP_EncryptFinal_ex');
     128             : 
     129          27 : final EVP_CIPHER_CTX_free = libcrypto.lookupFunction<
     130             :     Pointer<NativeType> Function(Pointer<NativeType> ctx),
     131             :     Pointer<NativeType> Function(
     132             :       Pointer<NativeType> ctx,
     133             :     )>('EVP_CIPHER_CTX_free');
     134             : 
     135           6 : final EVP_Digest = libcrypto.lookupFunction<
     136             :     IntPtr Function(
     137             :       Pointer<Uint8> data,
     138             :       IntPtr len,
     139             :       Pointer<Uint8> hash,
     140             :       Pointer<IntPtr> hsize,
     141             :       Pointer<NativeType> alg,
     142             :       Pointer<NativeType> engine,
     143             :     ),
     144             :     int Function(
     145             :       Pointer<Uint8> data,
     146             :       int len,
     147             :       Pointer<Uint8> hash,
     148             :       Pointer<IntPtr> hsize,
     149             :       Pointer<NativeType> alg,
     150             :       Pointer<NativeType> engine,
     151             :     )>('EVP_Digest');
     152             : 
     153           6 : final EVP_MD_size = () {
     154             :   // EVP_MD_size was renamed to EVP_MD_get_size in Openssl3.0.
     155             :   // There is an alias macro, but those don't exist in libraries.
     156             :   // Try loading the new name first, then fall back to the old one if not found.
     157             :   try {
     158           2 :     return libcrypto.lookupFunction<IntPtr Function(Pointer<NativeType> ctx),
     159             :         int Function(Pointer<NativeType> ctx)>('EVP_MD_get_size');
     160             :   } catch (e) {
     161           0 :     return libcrypto.lookupFunction<IntPtr Function(Pointer<NativeType> ctx),
     162             :         int Function(Pointer<NativeType> ctx)>('EVP_MD_size');
     163             :   }
     164           2 : }();

Generated by: LCOV version 1.14