1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
* Calculate IKEv2 prf and keying material, for libreswan
*
* Copyright (C) 2007 Michael C. Richardson <mcr@xelerance.com>
* Copyright (C) 2010 Paul Wouters <paul@xelerance.com>
* Copyright (C) 2013-2019 D. Hugh Redelmeier <hugh@mimosa.com>
* Copyright (C) 2015-2019 Andrew Cagney <cagney@gnu.org>
* Copyright (C) 2019 Paul Wouters <pwouters@redhat.com>
* Copyright (C) 2020 Nupur Agrawal <nupur202000@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* This code was developed with the support of Redhat corporation.
*
*/
#include "ike_alg.h"
#include "ike_alg_prf_ikev2_ops.h"
#include "lswlog.h"
#include "crypt_prf.h"
#include "crypt_symkey.h"
#include "fips_mode.h"
/*
* IKEv2 - RFC4306 2.14 SKEYSEED - calculation.
*/
static PK11SymKey *prfplus(const struct prf_desc *prf_desc,
PK11SymKey *key,
PK11SymKey *seed,
size_t required_keymat,
struct logger *logger)
{
uint8_t count = 1;
/* T1(prfplus) = prf(KEY, SEED|1) */
PK11SymKey *concatenated;
{
struct crypt_prf *prf = crypt_prf_init_symkey("prf+0", prf_desc,
"key", key,
logger);
crypt_prf_update_symkey(prf, "seed", seed);
crypt_prf_update_byte(prf, "1++", count++);
concatenated = crypt_prf_final_symkey(&prf);
}
/* make a copy to keep things easy */
PK11SymKey *old_t = symkey_addref(logger, "old_t[1]", concatenated);
while (sizeof_symkey(concatenated) < required_keymat) {
/* Tn = prf(KEY, Tn-1|SEED|n) */
struct crypt_prf *prf = crypt_prf_init_symkey("prf+N", prf_desc,
"key", key, logger);
crypt_prf_update_symkey(prf, "old_t", old_t);
crypt_prf_update_symkey(prf, "seed", seed);
crypt_prf_update_byte(prf, "N++", count++);
PK11SymKey *new_t = crypt_prf_final_symkey(&prf);
append_symkey_symkey(&concatenated, new_t, logger);
symkey_delref(logger, "old_t[N]", &old_t);
old_t = new_t;
}
symkey_delref(logger, "old_t[final]", &old_t);
/* truncate the result to match the length with required_keymat */
PK11SymKey *result = key_from_symkey_bytes("result", concatenated,
0, required_keymat,
HERE, logger);
symkey_delref(logger, "concatenated", &concatenated);
return result;
}
/*
* SKEYSEED = prf(Ni | Nr, g^ir)
*
*
*/
static PK11SymKey *ike_sa_skeyseed(const struct prf_desc *prf_desc,
const chunk_t Ni, const chunk_t Nr,
PK11SymKey *dh_secret,
struct logger *logger)
{
/*
* 2.14. Generating Keying Material for the IKE SA
*
* Ni and Nr are the nonces, stripped of any headers. For
* historical backward-compatibility reasons, there are two PRFs that
* are treated specially in this calculation. If the negotiated PRF is
* AES-XCBC-PRF-128 [AESXCBCPRF128] or AES-CMAC-PRF-128 [AESCMACPRF128],
* only the first 64 bits of Ni and the first 64 bits of Nr are used in
* calculating SKEYSEED, but all the bits are used for input to the prf+
* function.
*/
chunk_t key;
const char *key_name;
switch (prf_desc->common.id[IKEv2_ALG_ID]) {
case IKEv2_PRF_AES128_CMAC:
case IKEv2_PRF_AES128_XCBC:
{
chunk_t Ni64 = chunk2(Ni.ptr, BYTES_FOR_BITS(64));
chunk_t Nr64 = chunk2(Nr.ptr, BYTES_FOR_BITS(64));
key = clone_hunk_hunk(Ni64, Nr64, "key = Ni|Nr");
key_name = "Ni[0:63] | Nr[0:63]";
break;
}
default:
key = clone_hunk_hunk(Ni, Nr, "key = Ni|Nr");
key_name = "Ni | Nr";
break;
}
struct crypt_prf *prf = crypt_prf_init_hunk("SKEYSEED = prf(Ni | Nr, g^ir)",
prf_desc,
key_name, key,
logger);
free_chunk_content(&key);
if (prf == NULL) {
llog_pexpect(logger, HERE,
"failed to create IKEv2 PRF for computing SKEYSEED = prf(Ni | Nr, g^ir)");
return NULL;
}
/* seed = g^ir */
crypt_prf_update_symkey(prf, "g^ir", dh_secret);
/* generate */
return crypt_prf_final_symkey(&prf);
}
/*
* SKEYSEED = prf(SK_d (old), g^ir (new) | Ni | Nr)
*/
static PK11SymKey *ike_sa_rekey_skeyseed(const struct prf_desc *prf_desc,
PK11SymKey *SK_d_old,
PK11SymKey *new_dh_secret,
const chunk_t Ni, const chunk_t Nr,
struct logger *logger)
{
/* key = SK_d (old) */
struct crypt_prf *prf = crypt_prf_init_symkey("ike sa rekey skeyseed", prf_desc,
"SK_d (old)", SK_d_old,
logger);
if (prf == NULL) {
llog_pexpect(logger, HERE,
"failed to create IKEv2 PRF for computing SKEYSEED = prf(SK_d (old), g^ir (new) | Ni | Nr)");
return NULL;
}
/* seed: g^ir (new) | Ni | Nr) */
crypt_prf_update_symkey(prf, "g^ir (new)", new_dh_secret);
crypt_prf_update_hunk(prf, "Ni", Ni);
crypt_prf_update_hunk(prf, "Nr", Nr);
/* generate */
return crypt_prf_final_symkey(&prf);
}
/*
* SKEYSEED = prf(SK_d (old), "Resumption" | Ni | Nr)
*/
static PK11SymKey *ike_sa_resume_skeyseed(const struct prf_desc *prf_desc,
PK11SymKey *SK_d_old,
const chunk_t Ni, const chunk_t Nr,
struct logger *logger)
{
/* key = SK_d (old) */
struct crypt_prf *prf = crypt_prf_init_symkey("ike sa session resume skeyseed", prf_desc,
"SK_d (old)", SK_d_old,
logger);
if (prf == NULL) {
llog_pexpect(logger, HERE,
"failed to create IKEv2 PRF for computing SKEYSEED = prf(SK_d (old), 'Resumption' | Ni | Nr)");
return NULL;
}
/* seed: 'resumption' | Ni | Nr) */
static const char sr_str[] = "Resumption";
crypt_prf_update_bytes(prf,sr_str, sr_str, sizeof(sr_str) - 1);
crypt_prf_update_hunk(prf, "Ni", Ni);
crypt_prf_update_hunk(prf, "Nr", Nr);
/* generate */
return crypt_prf_final_symkey(&prf);
}
/*
* Compute: prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
*/
static PK11SymKey *ike_sa_keymat(const struct prf_desc *prf_desc,
PK11SymKey *skeyseed,
const chunk_t Ni, const chunk_t Nr,
shunk_t SPIi, shunk_t SPIr,
size_t required_bytes,
struct logger *logger)
{
PK11SymKey *data = symkey_from_hunk("data=Ni", Ni, logger);
append_symkey_hunk("data+=Nr", &data, Nr, logger);
append_symkey_hunk("data+=SPIi", &data, SPIi, logger);
append_symkey_hunk("data+=SPIr", &data, SPIr, logger);
PK11SymKey *result = prfplus(prf_desc,
skeyseed, data,
required_bytes,
logger);
symkey_delref(logger, "data", &data);
return result;
}
/*
* Compute: prf+(SK_d, [ g^ir (new) | ] Ni | Nr)
*/
static PK11SymKey *child_sa_keymat(const struct prf_desc *prf_desc,
PK11SymKey *SK_d,
PK11SymKey *new_dh_secret,
const chunk_t Ni, const chunk_t Nr,
size_t required_bytes,
struct logger *logger)
{
if (required_bytes == 0) {
/*
* For instance esp=null-none. Caller should
* interpret NULL to mean empty (NSS doesn't create
* zero length keys).
*/
ldbg(logger, "no CHILD SA KEMAT is required");
return NULL;
}
PK11SymKey *data;
if (new_dh_secret == NULL) {
data = symkey_from_hunk("data=Ni", Ni, logger);
append_symkey_hunk("data+=Nr", &data, Nr, logger);
} else {
/* make a local "readonly copy" and manipulate that */
data = symkey_addref(logger, "new_dh_secret", new_dh_secret);
append_symkey_hunk("data+=Ni", &data, Ni, logger);
append_symkey_hunk("data+=Nr", &data, Nr, logger);
}
PK11SymKey *result = prfplus(prf_desc,
SK_d, data,
required_bytes,
logger);
symkey_delref(logger, "data", &data);
return result;
}
static struct crypt_mac psk_auth(const struct prf_desc *prf_desc,
shunk_t pss,
chunk_t first_packet, chunk_t nonce,
const struct crypt_mac *id_hash,
chunk_t intermediate_packet,
struct logger *logger)
{
/* calculate inner prf */
PK11SymKey *prf_psk;
{
struct crypt_prf *prf =
crypt_prf_init_hunk("<prf-psk> = prf(<psk>,\"Key Pad for IKEv2\")",
prf_desc, "shared secret", pss, logger);
if (prf == NULL) {
if (is_fips_mode()) {
llog_passert(logger, HERE,
"FIPS: failure creating %s PRF context for digesting PSK",
prf_desc->common.fqn);
}
llog_pexpect(logger, HERE, "failure creating %s PRF context for digesting PSK",
prf_desc->common.fqn);
return empty_mac;
}
static const char psk_key_pad_str[] = "Key Pad for IKEv2"; /* RFC 4306 2:15 */
crypt_prf_update_bytes(prf, psk_key_pad_str, /* name */
psk_key_pad_str,
sizeof(psk_key_pad_str) - 1);
prf_psk = crypt_prf_final_symkey(&prf);
}
/* calculate outer prf */
struct crypt_mac signed_octets;
{
struct crypt_prf *prf =
crypt_prf_init_symkey("<signed-octets> = prf(<prf-psk>, <msg octets>)",
prf_desc, "<prf-psk>", prf_psk, logger);
/*
* For the responder, the octets to be signed start
* with the first octet of the first SPI in the header
* of the second message and end with the last octet
* of the last payload in the second message.
* Appended to this (for purposes of computing the
* signature) are the initiator's nonce Ni (just the
* value, not the payload containing it), and the
* value prf(SK_pr,IDr') where IDr' is the responder's
* ID payload excluding the fixed header. Note that
* neither the nonce Ni nor the value prf(SK_pr,IDr')
* are transmitted.
*/
crypt_prf_update_hunk(prf, "first-packet", first_packet);
crypt_prf_update_hunk(prf, "nonce", nonce);
crypt_prf_update_hunk(prf, "hash", *id_hash);
crypt_prf_update_hunk(prf,"IntAuth", intermediate_packet);
signed_octets = crypt_prf_final_mac(&prf, NULL);
}
symkey_delref(logger, "prf-psk", &prf_psk);
return signed_octets;
}
static struct crypt_mac psk_resume(const struct prf_desc *prf_desc,
PK11SymKey *SK_px,
chunk_t first_packet,
struct logger *logger)
{
struct crypt_prf *prf =
crypt_prf_init_symkey("<signed-octets> = prf(SK_px, <message octets>)",
prf_desc, "<SK_px>", SK_px, logger);
if (prf == NULL) {
llog_pexpect(logger, HERE,
"failed to create IKEv2 PRF for computing signed-octets = prf(SK_px, <message octets>");
return empty_mac;
}
crypt_prf_update_hunk(prf, "first-packet", first_packet);
return crypt_prf_final_mac(&prf, NULL);
}
const struct prf_ikev2_ops ike_alg_prf_ikev2_mac_ops = {
.backend = "native",
.prfplus = prfplus,
.ike_sa_skeyseed = ike_sa_skeyseed,
.ike_sa_rekey_skeyseed = ike_sa_rekey_skeyseed,
.ike_sa_resume_skeyseed = ike_sa_resume_skeyseed,
.ike_sa_keymat = ike_sa_keymat,
.child_sa_keymat = child_sa_keymat,
.psk_auth = psk_auth,
.psk_resume = psk_resume,
};
|