Coverage Report

Created: 2026-03-31 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/lib/nettle/prf.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2017 Red Hat, Inc.
3
 *
4
 * Author: Nikos Mavrogiannopoulos
5
 *
6
 * This file is part of GNUTLS.
7
 *
8
 * The GNUTLS library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public License
10
 * as published by the Free Software Foundation; either version 2.1 of
11
 * the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
20
 *
21
 */
22
23
#include "gnutls_int.h"
24
#include "int/tls1-prf.h"
25
#include <nettle/hmac.h>
26
27
/*-
28
 * _gnutls_prf_raw:
29
 * @mac: the MAC algorithm to use, set to %GNUTLS_MAC_MD5_SHA1 for the TLS1.0 mac
30
 * @master_size: length of the @master variable.
31
 * @master: the master secret used in PRF computation
32
 * @label_size: length of the @label variable.
33
 * @label: label used in PRF computation, typically a short string.
34
 * @seed_size: length of the @seed variable.
35
 * @seed: optional extra data to seed the PRF with.
36
 * @outsize: size of pre-allocated output buffer to hold the output.
37
 * @out: pre-allocated buffer to hold the generated data.
38
 *
39
 * Apply the TLS Pseudo-Random-Function (PRF) on the master secret
40
 * and the provided data.
41
 *
42
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
43
 -*/
44
int _gnutls_prf_raw(gnutls_mac_algorithm_t mac, size_t master_size,
45
        const void *master, size_t label_size, const char *label,
46
        size_t seed_size, const uint8_t *seed, size_t outsize,
47
        char *out)
48
0
{
49
0
  int ret;
50
51
0
  switch (mac) {
52
0
  case GNUTLS_MAC_MD5_SHA1:
53
0
    tls10_prf(master_size, (uint8_t *)master, label_size, label,
54
0
        seed_size, seed, outsize, (uint8_t *)out);
55
0
    return 0;
56
0
  case GNUTLS_MAC_SHA256: {
57
0
    struct hmac_sha256_ctx ctx;
58
0
    hmac_sha256_set_key(&ctx, master_size, (uint8_t *)master);
59
60
0
    ret = tls12_prf(&ctx,
61
0
        (nettle_hash_update_func *)hmac_sha256_update,
62
0
        (nettle_hash_digest_func *)hmac_sha256_digest,
63
0
        SHA256_DIGEST_SIZE, label_size, label,
64
0
        seed_size, seed, outsize, (uint8_t *)out);
65
66
0
    if (unlikely(ret != 1))
67
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
68
0
    break;
69
0
  }
70
0
  case GNUTLS_MAC_SHA384: {
71
0
    struct hmac_sha384_ctx ctx;
72
0
    hmac_sha384_set_key(&ctx, master_size, master);
73
74
0
    ret = tls12_prf(&ctx,
75
0
        (nettle_hash_update_func *)hmac_sha384_update,
76
0
        (nettle_hash_digest_func *)hmac_sha384_digest,
77
0
        SHA384_DIGEST_SIZE, label_size, label,
78
0
        seed_size, seed, outsize, (uint8_t *)out);
79
80
0
    if (unlikely(ret != 1))
81
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
82
0
    break;
83
0
  }
84
0
#if ENABLE_GOST
85
0
  case GNUTLS_MAC_STREEBOG_256: {
86
0
    struct hmac_streebog256_ctx ctx;
87
0
    hmac_streebog256_set_key(&ctx, master_size, master);
88
89
0
    ret = tls12_prf(
90
0
      &ctx,
91
0
      (nettle_hash_update_func *)hmac_streebog256_update,
92
0
      (nettle_hash_digest_func *)hmac_streebog256_digest,
93
0
      STREEBOG256_DIGEST_SIZE, label_size, label, seed_size,
94
0
      seed, outsize, (uint8_t *)out);
95
96
0
    if (unlikely(ret != 1))
97
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
98
0
    break;
99
0
  }
100
0
  case GNUTLS_MAC_STREEBOG_512: {
101
0
    struct hmac_streebog512_ctx ctx;
102
0
    hmac_streebog512_set_key(&ctx, master_size, master);
103
104
0
    ret = tls12_prf(
105
0
      &ctx,
106
0
      (nettle_hash_update_func *)hmac_streebog512_update,
107
0
      (nettle_hash_digest_func *)hmac_streebog512_digest,
108
0
      STREEBOG512_DIGEST_SIZE, label_size, label, seed_size,
109
0
      seed, outsize, (uint8_t *)out);
110
111
0
    if (unlikely(ret != 1))
112
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
113
0
    break;
114
0
  }
115
0
#endif
116
0
  default:
117
0
    gnutls_assert();
118
0
    _gnutls_debug_log("unhandled PRF %s\n",
119
0
          gnutls_mac_get_name(mac));
120
0
    return GNUTLS_E_INVALID_REQUEST;
121
0
  }
122
123
0
  return 0;
124
0
}