Pass SIM/AKA crypto helper to constructor of message
[strongswan.git] / src / libsimaka / simaka_crypto.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "simaka_crypto.h"
17
18 #include <daemon.h>
19
20 /** length of the k_encr key */
21 #define KENCR_LEN 16
22 /** length of the k_auth key */
23 #define KAUTH_LEN 16
24 /** length of the MSK */
25 #define MSK_LEN 64
26 /** length of the EMSK */
27 #define EMSK_LEN 64
28
29 typedef struct private_simaka_crypto_t private_simaka_crypto_t;
30
31 /**
32 * Private data of an simaka_crypto_t object.
33 */
34 struct private_simaka_crypto_t {
35
36 /**
37 * Public simaka_crypto_t interface.
38 */
39 simaka_crypto_t public;
40
41 /**
42 * signer to create/verify AT_MAC
43 */
44 signer_t *signer;
45
46 /**
47 * crypter to encrypt/decrypt AT_ENCR_DATA
48 */
49 crypter_t *crypter;
50
51 /**
52 * hasher used in key derivation
53 */
54 hasher_t *hasher;
55
56 /**
57 * PRF function used in key derivation
58 */
59 prf_t *prf;
60
61 /**
62 * Random number generator to generate nonces
63 */
64 rng_t *rng;
65
66 /**
67 * Have k_encr/k_auth been derived?
68 */
69 bool derived;
70 };
71
72 /**
73 * Implementation of simaka_crypto_t.get_signer
74 */
75 static signer_t* get_signer(private_simaka_crypto_t *this)
76 {
77 return this->derived ? this->signer : NULL;
78 }
79
80 /**
81 * Implementation of simaka_crypto_t.get_crypter
82 */
83 static crypter_t* get_crypter(private_simaka_crypto_t *this)
84 {
85 return this->derived ? this->crypter : NULL;
86 }
87
88 /**
89 * Implementation of simaka_crypto_t.get_rng
90 */
91 static rng_t* get_rng(private_simaka_crypto_t *this)
92 {
93 return this->rng;
94 }
95
96 /**
97 * Implementation of simaka_crypto_t.derive_keys_full
98 */
99 static chunk_t derive_keys_full(private_simaka_crypto_t *this,
100 identification_t *id, chunk_t data)
101 {
102
103 char mk[HASH_SIZE_SHA1], k_encr[KENCR_LEN], k_auth[KAUTH_LEN];
104 chunk_t str, msk;
105 int i;
106
107 /* For SIM: MK = SHA1(Identity|n*Kc|NONCE_MT|Version List|Selected Version)
108 * For AKA: MK = SHA1(Identity|IK|CK) */
109 this->hasher->get_hash(this->hasher, id->get_encoding(id), NULL);
110 this->hasher->get_hash(this->hasher, data, mk);
111 DBG3(DBG_IKE, "MK %b", mk, HASH_SIZE_SHA1);
112
113 /* K_encr | K_auth | MSK | EMSK = prf() | prf() | prf() | prf() */
114 this->prf->set_key(this->prf, chunk_create(mk, HASH_SIZE_SHA1));
115 str = chunk_alloca(this->prf->get_block_size(this->prf) * 3);
116 for (i = 0; i < 3; i++)
117 {
118 this->prf->get_bytes(this->prf, chunk_empty, str.ptr + str.len / 3 * i);
119 }
120
121 memcpy(k_encr, str.ptr, KENCR_LEN);
122 str = chunk_skip(str, KENCR_LEN);
123 memcpy(k_auth, str.ptr, KAUTH_LEN);
124 str = chunk_skip(str, KAUTH_LEN);
125
126 this->signer->set_key(this->signer, chunk_create(k_auth, KAUTH_LEN));
127 this->crypter->set_key(this->crypter, chunk_create(k_encr, KENCR_LEN));
128
129 msk = chunk_clone(chunk_create(str.ptr, MSK_LEN));
130 DBG3(DBG_IKE, "K_encr %b\nK_auth %b\nMSK %B",
131 k_encr, KENCR_LEN, k_auth, KAUTH_LEN, &msk);
132
133 this->derived = TRUE;
134 return msk;
135 }
136
137 /**
138 * Implementation of simaka_crypto_t.destroy.
139 */
140 static void destroy(private_simaka_crypto_t *this)
141 {
142 DESTROY_IF(this->rng);
143 DESTROY_IF(this->hasher);
144 DESTROY_IF(this->prf);
145 DESTROY_IF(this->signer);
146 DESTROY_IF(this->crypter);
147 free(this);
148 }
149
150 /**
151 * See header
152 */
153 simaka_crypto_t *simaka_crypto_create()
154 {
155 private_simaka_crypto_t *this = malloc_thing(private_simaka_crypto_t);
156
157 this->public.get_signer = (signer_t*(*)(simaka_crypto_t*))get_signer;
158 this->public.get_crypter = (crypter_t*(*)(simaka_crypto_t*))get_crypter;
159 this->public.get_rng = (rng_t*(*)(simaka_crypto_t*))get_rng;
160 this->public.derive_keys_full = (chunk_t(*)(simaka_crypto_t*, identification_t *id, chunk_t data))derive_keys_full;
161 this->public.destroy = (void(*)(simaka_crypto_t*))destroy;
162
163 this->derived = FALSE;
164 this->rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
165 this->hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
166 this->prf = lib->crypto->create_prf(lib->crypto, PRF_FIPS_SHA1_160);
167 this->signer = lib->crypto->create_signer(lib->crypto, AUTH_HMAC_SHA1_128);
168 this->crypter = lib->crypto->create_crypter(lib->crypto, ENCR_AES_CBC, 16);
169 if (!this->rng || !this->hasher || !this->prf ||
170 !this->signer || !this->crypter)
171 {
172 DBG1(DBG_IKE, "unable to use EAP-SIM, missing algorithms");
173 destroy(this);
174 return NULL;
175 }
176 return &this->public;
177 }
178