Coverage Report

Created: 2026-11-30 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/strongswan/src/libtls/tls_prf.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2010 Martin Willi
3
 *
4
 * Copyright (C) secunet Security Networks AG
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License as published by the
8
 * Free Software Foundation; either version 2 of the License, or (at your
9
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
10
 *
11
 * This program is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
 * for more details.
15
 */
16
17
#include "tls_prf.h"
18
19
typedef struct private_tls_prf12_t private_tls_prf12_t;
20
21
#include <library.h>
22
23
/**
24
 * Private data of an tls_prf_t object.
25
 */
26
struct private_tls_prf12_t {
27
28
  /**
29
   * Public tls_prf_t interface.
30
   */
31
  tls_prf_t public;
32
33
  /**
34
   * Underlying primitive PRF
35
   */
36
  prf_t *prf;
37
};
38
39
METHOD(tls_prf_t, set_key12, bool,
40
  private_tls_prf12_t *this, chunk_t key)
41
100
{
42
100
  return this->prf->set_key(this->prf, key);
43
100
}
44
45
/**
46
 * The P_hash function as in TLS 1.0/1.2
47
 */
48
static bool p_hash(prf_t *prf, char *label, chunk_t seed, size_t block_size,
49
           size_t bytes, char *out)
50
252
{
51
252
  char buf[block_size], abuf[block_size];
52
252
  chunk_t a;
53
54
  /* seed = label + seed */
55
252
  seed = chunk_cata("cc", chunk_create(label, strlen(label)), seed);
56
  /* A(0) = seed */
57
252
  a = seed;
58
59
252
  while (TRUE)
60
1.01k
  {
61
    /* A(i) = HMAC_hash(secret, A(i-1)) */
62
1.01k
    if (!prf->get_bytes(prf, a, abuf))
63
0
    {
64
0
      return FALSE;
65
0
    }
66
1.01k
    a = chunk_from_thing(abuf);
67
    /* HMAC_hash(secret, A(i) + seed) */
68
1.01k
    if (!prf->get_bytes(prf, a, NULL) ||
69
1.01k
      !prf->get_bytes(prf, seed, buf))
70
0
    {
71
0
      return FALSE;
72
0
    }
73
74
1.01k
    if (bytes <= block_size)
75
252
    {
76
252
      memcpy(out, buf, bytes);
77
252
      break;
78
252
    }
79
758
    memcpy(out, buf, block_size);
80
758
    out += block_size;
81
758
    bytes -= block_size;
82
758
  }
83
252
  return TRUE;
84
252
}
85
86
METHOD(tls_prf_t, get_bytes12, bool,
87
  private_tls_prf12_t *this, char *label, chunk_t seed,
88
  size_t bytes, char *out)
89
100
{
90
100
  return p_hash(this->prf, label, seed, this->prf->get_block_size(this->prf),
91
100
          bytes, out);
92
100
}
93
94
METHOD(tls_prf_t, destroy12, void,
95
  private_tls_prf12_t *this)
96
1.63k
{
97
1.63k
  this->prf->destroy(this->prf);
98
1.63k
  free(this);
99
1.63k
}
100
101
/**
102
 * See header
103
 */
104
tls_prf_t *tls_prf_create_12(pseudo_random_function_t prf)
105
1.63k
{
106
1.63k
  private_tls_prf12_t *this;
107
108
1.63k
  INIT(this,
109
1.63k
    .public = {
110
1.63k
      .set_key = _set_key12,
111
1.63k
      .get_bytes = _get_bytes12,
112
1.63k
      .destroy = _destroy12,
113
1.63k
    },
114
1.63k
    .prf = lib->crypto->create_prf(lib->crypto, prf),
115
1.63k
  );
116
1.63k
  if (!this->prf)
117
1
  {
118
1
    free(this);
119
1
    return NULL;
120
1
  }
121
1.63k
  return &this->public;
122
1.63k
}
123
124
125
typedef struct private_tls_prf10_t private_tls_prf10_t;
126
127
/**
128
 * Private data of an tls_prf_t object.
129
 */
130
struct private_tls_prf10_t {
131
132
  /**
133
   * Public tls_prf_t interface.
134
   */
135
  tls_prf_t public;
136
137
  /**
138
   * Underlying MD5 PRF
139
   */
140
  prf_t *md5;
141
142
  /**
143
   * Underlying SHA1 PRF
144
   */
145
  prf_t *sha1;
146
};
147
148
METHOD(tls_prf_t, set_key10, bool,
149
  private_tls_prf10_t *this, chunk_t key)
150
76
{
151
76
  size_t len = key.len / 2 + key.len % 2;
152
153
76
  return this->md5->set_key(this->md5, chunk_create(key.ptr, len)) &&
154
76
       this->sha1->set_key(this->sha1, chunk_create(key.ptr + key.len - len,
155
76
                            len));
156
76
}
157
158
METHOD(tls_prf_t, get_bytes10, bool,
159
  private_tls_prf10_t *this, char *label, chunk_t seed,
160
  size_t bytes, char *out)
161
76
{
162
76
  char buf[bytes];
163
164
76
  if (!p_hash(this->md5, label, seed, this->md5->get_block_size(this->md5),
165
76
        bytes, out) ||
166
76
    !p_hash(this->sha1, label, seed, this->sha1->get_block_size(this->sha1),
167
76
        bytes, buf))
168
0
  {
169
0
    return FALSE;
170
0
  }
171
76
  memxor(out, buf, bytes);
172
76
  return TRUE;
173
76
}
174
175
METHOD(tls_prf_t, destroy10, void,
176
  private_tls_prf10_t *this)
177
1.05k
{
178
1.05k
  DESTROY_IF(this->md5);
179
1.05k
  DESTROY_IF(this->sha1);
180
1.05k
  free(this);
181
1.05k
}
182
183
/**
184
 * See header
185
 */
186
tls_prf_t *tls_prf_create_10()
187
1.05k
{
188
1.05k
  private_tls_prf10_t *this;
189
190
1.05k
  INIT(this,
191
1.05k
    .public = {
192
1.05k
      .set_key = _set_key10,
193
1.05k
      .get_bytes = _get_bytes10,
194
1.05k
      .destroy = _destroy10,
195
1.05k
    },
196
1.05k
    .md5 = lib->crypto->create_prf(lib->crypto, PRF_HMAC_MD5),
197
1.05k
    .sha1 = lib->crypto->create_prf(lib->crypto, PRF_HMAC_SHA1),
198
1.05k
  );
199
1.05k
  if (!this->md5 || !this->sha1)
200
0
  {
201
0
    destroy10(this);
202
0
    return NULL;
203
0
  }
204
1.05k
  return &this->public;
205
1.05k
}