LCOV - code coverage report
Current view: top level - plugins/wireguard - wireguard_key.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 34 48 70.8 %
Date: 2023-07-05 22:20:52 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2020 Doc.ai and/or its affiliates.
       3             :  * Licensed under the Apache License, Version 2.0 (the "License");
       4             :  * you may not use this file except in compliance with the License.
       5             :  * You may obtain a copy of the License at:
       6             :  *
       7             :  *     http://www.apache.org/licenses/LICENSE-2.0
       8             :  *
       9             :  * Unless required by applicable law or agreed to in writing, software
      10             :  * distributed under the License is distributed on an "AS IS" BASIS,
      11             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             :  * See the License for the specific language governing permissions and
      13             :  * limitations under the License.
      14             :  */
      15             : 
      16             : #include <wireguard/wireguard_key.h>
      17             : #include <openssl/evp.h>
      18             : 
      19             : bool
      20        1128 : curve25519_gen_shared (u8 shared_key[CURVE25519_KEY_SIZE],
      21             :                        const u8 secret_key[CURVE25519_KEY_SIZE],
      22             :                        const u8 basepoint[CURVE25519_KEY_SIZE])
      23             : {
      24             : 
      25             :   bool ret;
      26             :   EVP_PKEY_CTX *ctx;
      27             :   size_t key_len;
      28             : 
      29        1128 :   EVP_PKEY *peerkey = NULL;
      30             :   EVP_PKEY *pkey =
      31        1128 :     EVP_PKEY_new_raw_private_key (EVP_PKEY_X25519, NULL, secret_key,
      32             :                                   CURVE25519_KEY_SIZE);
      33             : 
      34        1128 :   ret = true;
      35             : 
      36        1128 :   ctx = EVP_PKEY_CTX_new (pkey, NULL);
      37        1128 :   if (EVP_PKEY_derive_init (ctx) <= 0)
      38             :     {
      39           0 :       ret = false;
      40           0 :       goto out;
      41             :     }
      42             : 
      43             :   peerkey =
      44        1128 :     EVP_PKEY_new_raw_public_key (EVP_PKEY_X25519, NULL, basepoint,
      45             :                                  CURVE25519_KEY_SIZE);
      46        1128 :   if (EVP_PKEY_derive_set_peer (ctx, peerkey) <= 0)
      47             :     {
      48           0 :       ret = false;
      49           0 :       goto out;
      50             :     }
      51             : 
      52        1128 :   key_len = CURVE25519_KEY_SIZE;
      53        1128 :   if (EVP_PKEY_derive (ctx, shared_key, &key_len) <= 0)
      54             :     {
      55           0 :       ret = false;
      56             :     }
      57             : 
      58        1128 : out:
      59        1128 :   EVP_PKEY_CTX_free (ctx);
      60        1128 :   EVP_PKEY_free (pkey);
      61        1128 :   EVP_PKEY_free (peerkey);
      62        1128 :   return ret;
      63             : }
      64             : 
      65             : bool
      66         296 : curve25519_gen_public (u8 public_key[CURVE25519_KEY_SIZE],
      67             :                        const u8 secret_key[CURVE25519_KEY_SIZE])
      68             : {
      69             :   size_t pub_len;
      70             :   EVP_PKEY *pkey =
      71         296 :     EVP_PKEY_new_raw_private_key (EVP_PKEY_X25519, NULL, secret_key,
      72             :                                   CURVE25519_KEY_SIZE);
      73         296 :   pub_len = CURVE25519_KEY_SIZE;
      74         296 :   if (!EVP_PKEY_get_raw_public_key (pkey, public_key, &pub_len))
      75             :     {
      76           0 :       EVP_PKEY_free (pkey);
      77           0 :       return false;
      78             :     }
      79         296 :   EVP_PKEY_free (pkey);
      80         296 :   return true;
      81             : }
      82             : 
      83             : bool
      84         214 : curve25519_gen_secret (u8 secret_key[CURVE25519_KEY_SIZE])
      85             : {
      86             :   size_t secret_len;
      87         214 :   EVP_PKEY *pkey = NULL;
      88         214 :   EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id (EVP_PKEY_X25519, NULL);
      89         214 :   EVP_PKEY_keygen_init (pctx);
      90         214 :   EVP_PKEY_keygen (pctx, &pkey);
      91         214 :   EVP_PKEY_CTX_free (pctx);
      92             : 
      93         214 :   secret_len = CURVE25519_KEY_SIZE;
      94         214 :   if (!EVP_PKEY_get_raw_private_key (pkey, secret_key, &secret_len))
      95             :     {
      96           0 :       EVP_PKEY_free (pkey);
      97           0 :       return false;
      98             :     }
      99         214 :   EVP_PKEY_free (pkey);
     100         214 :   return true;
     101             : }
     102             : 
     103             : bool
     104          72 : key_to_base64 (const u8 * src, size_t src_len, u8 * out)
     105             : {
     106          72 :   if (!EVP_EncodeBlock (out, src, src_len))
     107           0 :     return false;
     108          72 :   return true;
     109             : }
     110             : 
     111             : bool
     112           0 : key_from_base64 (const u8 * src, size_t src_len, u8 * out)
     113             : {
     114           0 :   if (EVP_DecodeBlock (out, src, src_len - 1) <= 0)
     115           0 :     return false;
     116           0 :   return true;
     117             : }
     118             : 
     119             : /*
     120             :  * fd.io coding-style-patch-verification: ON
     121             :  *
     122             :  * Local Variables:
     123             :  * eval: (c-set-style "gnu")
     124             :  * End:
     125             :  */

Generated by: LCOV version 1.14