LCOV - code coverage report
Current view: top level - lib - entropy.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 12 16 75.0 %
Date: 2016-09-14 01:02:56 Functions: 3 3 100.0 %
Branches: 3 12 25.0 %

           Branch data     Line data    Source code
       1                 :            : /* Copyright (c) 2008, 2009, 2010, 2011, 2013 Nicira, Inc.
       2                 :            :  *
       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 <config.h>
      17                 :            : 
      18                 :            : #include "entropy.h"
      19                 :            : 
      20                 :            : #include <errno.h>
      21                 :            : #include <fcntl.h>
      22                 :            : #include <unistd.h>
      23                 :            : #ifdef _WIN32
      24                 :            : #include <Wincrypt.h>
      25                 :            : #endif
      26                 :            : #include "util.h"
      27                 :            : #include "socket-util.h"
      28                 :            : #include "openvswitch/vlog.h"
      29                 :            : 
      30                 :      53956 : VLOG_DEFINE_THIS_MODULE(entropy);
      31                 :            : 
      32                 :            : static const char urandom[] = "/dev/urandom";
      33                 :            : 
      34                 :            : /* Initializes 'buffer' with 'n' bytes of high-quality random numbers.  Returns
      35                 :            :  * 0 if successful, otherwise a positive errno value or EOF on error. */
      36                 :            : int
      37                 :      27350 : get_entropy(void *buffer, size_t n)
      38                 :            : {
      39                 :            : #ifndef _WIN32
      40                 :            :     size_t bytes_read;
      41                 :            :     int error;
      42                 :            :     int fd;
      43                 :            : 
      44                 :      27350 :     fd = open(urandom, O_RDONLY);
      45         [ -  + ]:      27350 :     if (fd < 0) {
      46         [ #  # ]:          0 :         VLOG_ERR("%s: open failed (%s)", urandom, ovs_strerror(errno));
      47         [ #  # ]:          0 :         return errno ? errno : EINVAL;
      48                 :            :     }
      49                 :            : 
      50                 :      27350 :     error = read_fully(fd, buffer, n, &bytes_read);
      51                 :      27350 :     close(fd);
      52                 :            : 
      53         [ -  + ]:      27350 :     if (error) {
      54         [ #  # ]:          0 :         VLOG_ERR("%s: read error (%s)", urandom, ovs_retval_to_string(error));
      55                 :            :     }
      56                 :            : #else
      57                 :            :     int error = 0;
      58                 :            :     HCRYPTPROV   crypt_prov = 0;
      59                 :            : 
      60                 :            :     CryptAcquireContext(&crypt_prov, NULL, NULL,
      61                 :            :                         PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
      62                 :            :     if (!CryptGenRandom(crypt_prov, n, buffer)) {
      63                 :            :         VLOG_ERR("CryptGenRandom: read error (%s)", ovs_lasterror_to_string());
      64                 :            :         error = EINVAL;
      65                 :            :     }
      66                 :            : 
      67                 :            :     CryptReleaseContext(crypt_prov, 0);
      68                 :            : #endif
      69                 :      27350 :     return error;
      70                 :            : }
      71                 :            : 
      72                 :            : /* Initializes 'buffer' with 'n' bytes of high-quality random numbers.  Exits
      73                 :            :  * if an error occurs. */
      74                 :            : void
      75                 :      27350 : get_entropy_or_die(void *buffer, size_t n)
      76                 :            : {
      77                 :      27350 :     int error = get_entropy(buffer, n);
      78         [ -  + ]:      27350 :     if (error) {
      79                 :          0 :         VLOG_FATAL("%s: read error (%s)",
      80                 :            :                    urandom, ovs_retval_to_string(error));
      81                 :            :     }
      82                 :      27350 : }

Generated by: LCOV version 1.12