Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
53 | 0 | update(mac_ctx, secret_size, secret); |
54 | 0 | digest(mac_ctx, digest_size, dst); |
55 | 0 | } |
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 | 0 | { |
67 | 0 | uint8_t i = 1; |
68 | |
|
69 | 0 | if (!length) |
70 | 0 | return; |
71 | | |
72 | 0 | for (;; dst += digest_size, length -= digest_size, i++) |
73 | 0 | { |
74 | 0 | update(mac_ctx, info_size, info); |
75 | 0 | update(mac_ctx, 1, &i); |
76 | 0 | if (length <= digest_size) |
77 | 0 | break; |
78 | | |
79 | 0 | digest(mac_ctx, digest_size, dst); |
80 | 0 | update(mac_ctx, digest_size, dst); |
81 | 0 | } |
82 | |
|
83 | 0 | digest(mac_ctx, length, dst); |
84 | 0 | } |