Coverage Report

Created: 2024-06-28 06:39

/src/nettle-with-mini-gmp/hkdf.c
Line
Count
Source
1
/* hkdf.c
2
3
   Copyright (C) 2017 Red Hat, Inc.
4
5
   Author: Nikos Mavrogiannopoulos
6
7
   This file is part of GNU Nettle.
8
9
   GNU Nettle is free software: you can redistribute it and/or
10
   modify it under the terms of either:
11
12
     * the GNU Lesser General Public License as published by the Free
13
       Software Foundation; either version 3 of the License, or (at your
14
       option) any later version.
15
16
   or
17
18
     * the GNU General Public License as published by the Free
19
       Software Foundation; either version 2 of the License, or (at your
20
       option) any later version.
21
22
   or both in parallel, as here.
23
24
   GNU Nettle is distributed in the hope that it will be useful,
25
   but WITHOUT ANY WARRANTY; without even the implied warranty of
26
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27
   General Public License for more details.
28
29
   You should have received copies of the GNU General Public License and
30
   the GNU Lesser General Public License along with this program.  If
31
   not, see http://www.gnu.org/licenses/.
32
 */
33
34
/* Functions for the HKDF handling.
35
 */
36
37
#if HAVE_CONFIG_H
38
# include "config.h"
39
#endif
40
41
#include "hkdf.h"
42
43
/* hkdf_extract: Outputs a PRK of digest_size
44
 */
45
void
46
hkdf_extract(void *mac_ctx,
47
       nettle_hash_update_func *update,
48
       nettle_hash_digest_func *digest,
49
       size_t digest_size,
50
       size_t secret_size, const uint8_t *secret,
51
       uint8_t *dst)
52
1.02k
{
53
1.02k
  update(mac_ctx, secret_size, secret);
54
1.02k
  digest(mac_ctx, digest_size, dst);
55
1.02k
}
56
57
/* hkdf_expand: Outputs an arbitrary key of size specified by length
58
 */
59
void
60
hkdf_expand(void *mac_ctx,
61
      nettle_hash_update_func *update,
62
      nettle_hash_digest_func *digest,
63
      size_t digest_size,
64
      size_t info_size, const uint8_t *info,
65
      size_t length, uint8_t *dst)
66
1.02k
{
67
1.02k
  uint8_t i = 1;
68
69
1.02k
  if (!length)
70
42
    return;
71
72
118k
  for (;; dst += digest_size, length -= digest_size, i++)
73
119k
    {
74
119k
      update(mac_ctx, info_size, info);
75
119k
      update(mac_ctx, 1, &i);
76
119k
      if (length <= digest_size)
77
985
  break;
78
79
118k
      digest(mac_ctx, digest_size, dst);
80
118k
      update(mac_ctx, digest_size, dst);
81
118k
    }
82
83
985
  digest(mac_ctx, length, dst);
84
985
}