blob: 4113aada0ff98a0d2f2f41dbe4cf713fcf6db64a [file] [log] [blame]
Ericc3fa0072021-05-19 17:45:38 +02001/*
2 * (C) 2021 by sysmocom s.f.m.c. GmbH
3 *
4 * Author: Eric Wild <ewild@sysmocom.de>
5 *
6 * All Rights Reserved
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
Ericc3fa0072021-05-19 17:45:38 +020020 */
21
22#include <stdint.h>
23#include <string.h>
24
Pau Espin Pedrol88955fb2023-01-18 18:54:00 +010025#include "config.h"
Ericc3fa0072021-05-19 17:45:38 +020026#if (USE_GNUTLS)
27#include <gnutls/gnutls.h>
28#include <gnutls/crypto.h>
29#define HMAC_FUNC(k,lk,s,sl,out) gnutls_hmac_fast(GNUTLS_MAC_SHA256,k,lk,s,sl,out)
30#else
31#include <osmocom/crypt/kdf.h>
32#define HMAC_FUNC(k,lk,s,sl,out) hmac_sha256(k,lk,s,sl,out)
33#endif
34
35#include <osmocom/core/bit32gen.h>
36#include <osmocom/crypt/kdf.h>
37
38#include "kdf/common.h"
39#include "kdf/sha256.h"
40
41
42#if (USE_GNUTLS)
43/* gnutls < 3.3.0 requires global init.
44 * gnutls >= 3.3.0 does it automatic.
45 * It doesn't hurt calling it twice,
46 * as long it's not done at the same time (threads).
47 */
48__attribute__((constructor))
49static void on_dso_load_gnutls(void)
50{
51 if (!gnutls_check_version("3.3.0"))
52 gnutls_global_init();
53}
54
55__attribute__((destructor))
56static void on_dso_unload_gnutls(void)
57{
58 if (!gnutls_check_version("3.3.0"))
59 gnutls_global_deinit();
60}
61#endif
62
63/*
64 * This file uses the generic key derivation function defined in 3GPP TS 33.220 Annex B
65 *
66 * The S parameter always consists of concatenated values FC | P0 | L0 | Pi | Li | ...
67 * with Pi = Parameter number i and Li = Length of Pi (two octets)
68 *
69 * FC is either a single octet or two octets 0xff | FC
70 * FC values ranges depend on the specification parts that use the KDF,
71 * they are defined in 3GPP TS 33.220 Annex B.2.2
72 *
73 */
74
75/*! \addtogroup kdf
76 * @{
77 * key derivation functions
78 *
79 * \file kdf.c */
80
81/* 3GPP TS 33.102 B.5 */
82void osmo_kdf_kc128(const uint8_t* ck, const uint8_t* ik, uint8_t* kc128) {
83 uint8_t k[16*2];
84 uint8_t s[1];
85 uint8_t out_tmp256[32];
86 memcpy (&k[0], ck, 16);
87 memcpy (&k[16], ik, 16);
88
89 s[0] = 0x32; // yeah, really just one FC byte..
90
91 HMAC_FUNC(k, 32, s, 1, out_tmp256);
92 memcpy(kc128, out_tmp256, 16);
93}
94
95/* 3GPP TS 33.401 A.2 */
96void osmo_kdf_kasme(const uint8_t *ck, const uint8_t *ik, const uint8_t* plmn_id,
97 const uint8_t *sqn, const uint8_t *ak, uint8_t *kasme)
98{
99 uint8_t s[14];
100 uint8_t k[16*2];
101 int i;
102
103 memcpy(&k[0], ck, 16);
104 memcpy(&k[16], ik, 16);
105
106 s[0] = 0x10;
107 memcpy(&s[1], plmn_id, 3);
108 s[4] = 0x00;
109 s[5] = 0x03;
110
111 for (i = 0; i < 6; i++)
112 s[6+i] = sqn[i] ^ ak[i];
113 s[12] = 0x00;
114 s[13] = 0x06;
115
116 HMAC_FUNC(k, 32, s, 14, kasme);
117}
118
119/* 3GPP TS 33.401 A.3 */
120void osmo_kdf_enb(const uint8_t *kasme, uint32_t ul_count, uint8_t *kenb)
121{
122 uint8_t s[7];
123
124 s[0] = 0x11;
125 osmo_store32be(ul_count, &s[1]);
126 s[5] = 0x00;
127 s[6] = 0x04;
128
129 HMAC_FUNC(kasme, 32, s, 7, kenb);
130}
131
132/* 3GPP TS 33.401 A.4 */
133void osmo_kdf_nh(const uint8_t *kasme, const uint8_t *sync_input, uint8_t *nh)
134{
135 uint8_t s[35];
136
137 s[0] = 0x12;
138 memcpy(s+1, sync_input, 32);
139 s[33] = 0x00;
140 s[34] = 0x20;
141
142 HMAC_FUNC(kasme, 32, s, 35, nh);
143}
144
145/* 3GPP TS 33.401 A.7 */
146void osmo_kdf_nas(uint8_t algo_type, uint8_t algo_id, const uint8_t *kasme, uint8_t *knas)
147{
148 uint8_t s[7];
149 uint8_t out[32];
150
151 s[0] = 0x15;
152 s[1] = algo_type;
153 s[2] = 0x00;
154 s[3] = 0x01;
155 s[4] = algo_id;
156 s[5] = 0x00;
157 s[6] = 0x01;
158
159 HMAC_FUNC(kasme, 32, s, 7, out);
160 memcpy(knas, out+16, 16);
161}
162
163/*! @} */