adding liblte sources

This commit is contained in:
Andre Puschmann 2017-05-18 12:05:07 +02:00
parent 8c172bfee0
commit c42fe3488d
10 changed files with 37592 additions and 0 deletions

9
liblte/CMakeLists.txt Normal file
View File

@ -0,0 +1,9 @@
include_directories(hdr)
add_library(lte SHARED
src/liblte_common.cc
src/liblte_rrc.cc
src/liblte_mme.cc
src/liblte_security.cc
)
target_link_libraries(lte ${POLAR_LIBRARIES})
INSTALL(TARGETS lte DESTINATION ${LIBRARY_DIR})

239
liblte/hdr/liblte_common.h Normal file
View File

@ -0,0 +1,239 @@
/*******************************************************************************
Copyright 2012-2014 Ben Wojtowicz
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************
File: liblte_common.h
Description: Contains all the common definitions for the LTE library.
Revision History
---------- ------------- --------------------------------------------
02/26/2012 Ben Wojtowicz Created file.
07/21/2013 Ben Wojtowicz Added a common message structure.
06/15/2014 Ben Wojtowicz Split LIBLTE_MSG_STRUCT into bit and byte
aligned messages.
08/03/2014 Ben Wojtowicz Commonized value_2_bits and bits_2_value.
11/29/2014 Ben Wojtowicz Added liblte prefix to value_2_bits and
bits_2_value.
*******************************************************************************/
#ifndef __LIBLTE_COMMON_H__
#define __LIBLTE_COMMON_H__
/*******************************************************************************
INCLUDES
*******************************************************************************/
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
/*******************************************************************************
DEFINES
*******************************************************************************/
// FIXME: This was chosen arbitrarily
#define LIBLTE_ASN1_OID_MAXSUBIDS 128
#define LIBLTE_MAX_MSG_SIZE_BITS 102048
#define LIBLTE_MAX_MSG_SIZE_BYTES 12756
#define LIBLTE_MSG_HEADER_OFFSET 1024
/*******************************************************************************
TYPEDEFS
*******************************************************************************/
typedef int8_t int8;
typedef uint8_t uint8;
typedef int16_t int16;
typedef uint16_t uint16;
typedef int32_t int32;
typedef uint32_t uint32;
typedef int64_t int64;
typedef uint64_t uint64;
typedef enum{
LIBLTE_SUCCESS = 0,
LIBLTE_ERROR_INVALID_INPUTS,
LIBLTE_ERROR_ENCODE_FAIL,
LIBLTE_ERROR_DECODE_FAIL,
LIBLTE_ERROR_INVALID_CRC,
LIBLTE_ERROR_N_ITEMS
}LIBLTE_ERROR_ENUM;
static const char liblte_error_text[LIBLTE_ERROR_N_ITEMS][64] = {
"Invalid inputs",
"Encode failure",
"Decode failure",
};
typedef void* LIBLTE_ASN1_OPEN_TYPE_STRUCT;
typedef struct {
uint32_t numids; // number of subidentifiers
uint32_t subid[LIBLTE_ASN1_OID_MAXSUBIDS]; // subidentifier values
} LIBLTE_ASN1_OID_STRUCT;
typedef struct{
bool data_valid;
bool data;
}LIBLTE_BOOL_MSG_STRUCT;
typedef struct{
uint32 N_bits;
uint8 msg[LIBLTE_MAX_MSG_SIZE_BITS];
}LIBLTE_SIMPLE_BIT_MSG_STRUCT;
typedef struct{
uint32 N_bytes;
uint8 msg[LIBLTE_MAX_MSG_SIZE_BYTES];
}LIBLTE_SIMPLE_BYTE_MSG_STRUCT;
struct LIBLTE_BYTE_MSG_STRUCT{
uint32 N_bytes;
uint8 buffer[LIBLTE_MAX_MSG_SIZE_BYTES];
uint8 *msg;
LIBLTE_BYTE_MSG_STRUCT():N_bytes(0)
{
msg = &buffer[LIBLTE_MSG_HEADER_OFFSET];
}
LIBLTE_BYTE_MSG_STRUCT(const LIBLTE_BYTE_MSG_STRUCT& buf)
{
N_bytes = buf.N_bytes;
memcpy(msg, buf.msg, N_bytes);
}
LIBLTE_BYTE_MSG_STRUCT & operator= (const LIBLTE_BYTE_MSG_STRUCT & buf)
{
N_bytes = buf.N_bytes;
memcpy(msg, buf.msg, N_bytes);
}
uint32 get_headroom()
{
return msg-buffer;
}
void reset()
{
N_bytes = 0;
msg = &buffer[LIBLTE_MSG_HEADER_OFFSET];
}
};
struct LIBLTE_BIT_MSG_STRUCT{
uint32 N_bits;
uint8 buffer[LIBLTE_MAX_MSG_SIZE_BITS];
uint8 *msg;
LIBLTE_BIT_MSG_STRUCT():N_bits(0)
{
msg = &buffer[LIBLTE_MSG_HEADER_OFFSET];
while( (uint64_t)(msg) % 8 > 0) {
msg++;
}
}
LIBLTE_BIT_MSG_STRUCT(const LIBLTE_BIT_MSG_STRUCT& buf){
N_bits = buf.N_bits;
memcpy(msg, buf.msg, N_bits);
}
LIBLTE_BIT_MSG_STRUCT & operator= (const LIBLTE_BIT_MSG_STRUCT & buf){
N_bits = buf.N_bits;
memcpy(msg, buf.msg, N_bits);
}
uint32 get_headroom()
{
return msg-buffer;
}
void reset()
{
N_bits = 0;
msg = &buffer[LIBLTE_MSG_HEADER_OFFSET];
while( (uint64_t)(msg) % 8 > 0) {
msg++;
}
}
};
/*******************************************************************************
DECLARATIONS
*******************************************************************************/
/*********************************************************************
Name: liblte_value_2_bits
Description: Converts a value to a bit string
*********************************************************************/
void liblte_value_2_bits(uint32 value,
uint8 **bits,
uint32 N_bits);
/*********************************************************************
Name: liblte_bits_2_value
Description: Converts a bit string to a value
*********************************************************************/
uint32 liblte_bits_2_value(uint8 **bits,
uint32 N_bits);
/*********************************************************************
Name: liblte_pack
Description: Pack a bit array into a byte array
*********************************************************************/
void liblte_pack(LIBLTE_BIT_MSG_STRUCT *bits,
LIBLTE_BYTE_MSG_STRUCT *bytes);
/*********************************************************************
Name: liblte_unpack
Description: Unpack a byte array into a bit array
*********************************************************************/
void liblte_unpack(LIBLTE_BYTE_MSG_STRUCT *bytes,
LIBLTE_BIT_MSG_STRUCT *bits);
/*********************************************************************
Name: liblte_pack
Description: Pack a bit array into a byte array
*********************************************************************/
void liblte_pack(uint8_t *bits, uint32_t n_bits, uint8_t *bytes);
/*********************************************************************
Name: liblte_unpack
Description: Unpack a byte array into a bit array
*********************************************************************/
void liblte_unpack(uint8_t *bytes, uint32_t n_bytes, uint8_t *bits);
/*********************************************************************
Name: liblte_align_up
Description: Aligns a pointer to a multibyte boundary
*********************************************************************/
void liblte_align_up(uint8_t **ptr, uint32_t align);
/*********************************************************************
Name: liblte_align_up_zero
Description: Aligns a pointer to a multibyte boundary and zeros
bytes skipped
*********************************************************************/
void liblte_align_up_zero(uint8_t **ptr, uint32_t align);
#endif /* __LIBLTE_COMMON_H__ */

4027
liblte/hdr/liblte_mme.h Normal file

File diff suppressed because it is too large Load Diff

6921
liblte/hdr/liblte_rrc.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,270 @@
/*******************************************************************************
Copyright 2014 Ben Wojtowicz
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************
File: liblte_security.h
Description: Contains all the definitions for the LTE security algorithm
library.
Revision History
---------- ------------- --------------------------------------------
08/03/2014 Ben Wojtowicz Created file.
09/03/2014 Ben Wojtowicz Added key generation and EIA2.
*******************************************************************************/
#ifndef __LIBLTE_SECURITY_H__
#define __LIBLTE_SECURITY_H__
/*******************************************************************************
INCLUDES
*******************************************************************************/
#include "liblte_common.h"
/*******************************************************************************
DEFINES
*******************************************************************************/
/*******************************************************************************
TYPEDEFS
*******************************************************************************/
/*******************************************************************************
DECLARATIONS
*******************************************************************************/
/*********************************************************************
Name: liblte_security_generate_k_asme
Description: Generate the security key Kasme.
Document Reference: 33.401 v10.0.0 Annex A.2
*********************************************************************/
// Defines
// Enums
// Structs
// Functions
LIBLTE_ERROR_ENUM liblte_security_generate_k_asme(uint8 *ck,
uint8 *ik,
uint8 *ak,
uint8 *sqn,
uint16 mcc,
uint16 mnc,
uint8 *k_asme);
/*********************************************************************
Name: liblte_security_generate_k_enb
Description: Generate the security key Kenb.
Document Reference: 33.401 v10.0.0 Annex A.2
*********************************************************************/
// Defines
// Enums
// Structs
// Functions
LIBLTE_ERROR_ENUM liblte_security_generate_k_enb(uint8 *k_asme,
uint32 nas_count,
uint8 *k_enb);
/*********************************************************************
Name: liblte_security_generate_k_nas
Description: Generate the NAS security keys KNASenc and KNASint.
Document Reference: 33.401 v10.0.0 Annex A.2
*********************************************************************/
// Defines
// Enums
typedef enum{
LIBLTE_SECURITY_CIPHERING_ALGORITHM_ID_EEA0 = 0,
LIBLTE_SECURITY_CIPHERING_ALGORITHM_ID_128_EEA1,
LIBLTE_SECURITY_CIPHERING_ALGORITHM_ID_128_EEA2,
LIBLTE_SECURITY_CIPHERING_ALGORITHM_ID_N_ITEMS,
}LIBLTE_SECURITY_CIPHERING_ALGORITHM_ID_ENUM;
static const char liblte_security_ciphering_algorithm_id_text[LIBLTE_SECURITY_CIPHERING_ALGORITHM_ID_N_ITEMS][20] = {"EEA0",
"128-EEA1",
"128-EEA2"};
typedef enum{
LIBLTE_SECURITY_INTEGRITY_ALGORITHM_ID_EIA0 = 0,
LIBLTE_SECURITY_INTEGRITY_ALGORITHM_ID_128_EIA1,
LIBLTE_SECURITY_INTEGRITY_ALGORITHM_ID_128_EIA2,
LIBLTE_SECURITY_INTEGRITY_ALGORITHM_ID_N_ITEMS,
}LIBLTE_SECURITY_INTEGRITY_ALGORITHM_ID_ENUM;
static const char liblte_security_integrity_algorithm_id_text[LIBLTE_SECURITY_INTEGRITY_ALGORITHM_ID_N_ITEMS][20] = {"EIA0",
"128-EIA1",
"128-EIA2"};
// Structs
// Functions
LIBLTE_ERROR_ENUM liblte_security_generate_k_nas(uint8 *k_asme,
LIBLTE_SECURITY_CIPHERING_ALGORITHM_ID_ENUM enc_alg_id,
LIBLTE_SECURITY_INTEGRITY_ALGORITHM_ID_ENUM int_alg_id,
uint8 *k_nas_enc,
uint8 *k_nas_int);
/*********************************************************************
Name: liblte_security_generate_k_rrc
Description: Generate the RRC security keys KRRCenc and KRRCint.
Document Reference: 33.401 v10.0.0 Annex A.2
*********************************************************************/
// Defines
// Enums
// Structs
// Functions
LIBLTE_ERROR_ENUM liblte_security_generate_k_rrc(uint8 *k_enb,
LIBLTE_SECURITY_CIPHERING_ALGORITHM_ID_ENUM enc_alg_id,
LIBLTE_SECURITY_INTEGRITY_ALGORITHM_ID_ENUM int_alg_id,
uint8 *k_rrc_enc,
uint8 *k_rrc_int);
/*********************************************************************
Name: liblte_security_generate_k_up
Description: Generate the user plane security keys KUPenc and
KUPint.
Document Reference: 33.401 v10.0.0 Annex A.2
*********************************************************************/
// Defines
// Enums
// Structs
// Functions
LIBLTE_ERROR_ENUM liblte_security_generate_k_up(uint8 *k_enb,
LIBLTE_SECURITY_CIPHERING_ALGORITHM_ID_ENUM enc_alg_id,
LIBLTE_SECURITY_INTEGRITY_ALGORITHM_ID_ENUM int_alg_id,
uint8 *k_up_enc,
uint8 *k_up_int);
/*********************************************************************
Name: liblte_security_128_eia2
Description: 128-bit integrity algorithm EIA2.
Document Reference: 33.401 v10.0.0 Annex B.2.3
33.102 v10.0.0 Section 6.5.4
RFC4493
*********************************************************************/
// Defines
#define LIBLTE_SECURITY_DIRECTION_UPLINK 0
#define LIBLTE_SECURITY_DIRECTION_DOWNLINK 1
// Enums
// Structs
// Functions
LIBLTE_ERROR_ENUM liblte_security_128_eia2(uint8 *key,
uint32 count,
uint8 bearer,
uint8 direction,
uint8 *msg,
uint32 msg_len,
uint8 *mac);
LIBLTE_ERROR_ENUM liblte_security_128_eia2(uint8 *key,
uint32 count,
uint8 bearer,
uint8 direction,
LIBLTE_BIT_MSG_STRUCT *msg,
uint8 *mac);
/*********************************************************************
Name: liblte_security_milenage_f1
Description: Milenage security function F1. Computes network
authentication code MAC-A from key K, random
challenge RAND, sequence number SQN, and
authentication management field AMF.
Document Reference: 35.206 v10.0.0 Annex 3
*********************************************************************/
// Defines
// Enums
// Structs
// Functions
LIBLTE_ERROR_ENUM liblte_security_milenage_f1(uint8 *k,
uint8 *op,
uint8 *rand,
uint8 *sqn,
uint8 *amf,
uint8 *mac_a);
/*********************************************************************
Name: liblte_security_milenage_f1_star
Description: Milenage security function F1*. Computes resynch
authentication code MAC-S from key K, random
challenge RAND, sequence number SQN, and
authentication management field AMF.
Document Reference: 35.206 v10.0.0 Annex 3
*********************************************************************/
// Defines
// Enums
// Structs
// Functions
LIBLTE_ERROR_ENUM liblte_security_milenage_f1_star(uint8 *k,
uint8 *op,
uint8 *rand,
uint8 *sqn,
uint8 *amf,
uint8 *mac_s);
/*********************************************************************
Name: liblte_security_milenage_f2345
Description: Milenage security functions F2, F3, F4, and F5.
Computes response RES, confidentiality key CK,
integrity key IK, and anonymity key AK from random
challenge RAND.
Document Reference: 35.206 v10.0.0 Annex 3
*********************************************************************/
// Defines
// Enums
// Structs
// Functions
LIBLTE_ERROR_ENUM liblte_security_milenage_f2345(uint8 *k,
uint8 *op,
uint8 *rand,
uint8 *res,
uint8 *ck,
uint8 *ik,
uint8 *ak);
/*********************************************************************
Name: liblte_security_milenage_f5_star
Description: Milenage security function F5*. Computes resynch
anonymity key AK from key K and random challenge
RAND.
Document Reference: 35.206 v10.0.0 Annex 3
*********************************************************************/
// Defines
// Enums
// Structs
// Functions
LIBLTE_ERROR_ENUM liblte_security_milenage_f5_star(uint8 *k,
uint8 *op,
uint8 *rand,
uint8 *ak);
#endif /* __LIBLTE_SECURITY_H__ */

53
liblte/hdr/liblte_ssl.h Normal file
View File

@ -0,0 +1,53 @@
#ifndef __LIBLTE_SSL_H__
#define __LIBLTE_SSL_H__
#ifdef HAVE_POLARSSL
#include "polarssl/sha256.h"
#include "polarssl/aes.h"
void sha256(const unsigned char *key, size_t keylen,
const unsigned char *input, size_t ilen,
unsigned char output[32], int is224 )
{
sha256_hmac(key, keylen, input, ilen, output, is224);
}
#endif // HAVE_POLARSSL
#ifdef HAVE_MBEDTLS
#include "mbedtls/md.h"
#include "mbedtls/aes.h"
typedef mbedtls_aes_context aes_context;
#define AES_ENCRYPT 1
#define AES_DECRYPT 0
int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize )
{
return mbedtls_aes_setkey_enc(ctx, key, keysize);
}
int aes_crypt_ecb( aes_context *ctx,
int mode,
const unsigned char input[16],
unsigned char output[16] )
{
return mbedtls_aes_crypt_ecb(ctx, mode, input, output);
}
void sha256(const unsigned char *key, size_t keylen,
const unsigned char *input, size_t ilen,
unsigned char output[32], int is224 )
{
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
key, keylen,
input, ilen,
output );
}
#endif // HAVE_MBEDTLS
#endif // __LIBLTE_SSL_H__

198
liblte/src/liblte_common.cc Normal file
View File

@ -0,0 +1,198 @@
/*******************************************************************************
Copyright 2014 Ben Wojtowicz
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************
File: liblte_common.cc
Description: Contains all the implementations for the LTE common library.
Revision History
---------- ------------- --------------------------------------------
08/03/2014 Ben Wojtowicz Created file.
11/29/2014 Ben Wojtowicz Added liblte prefix to value_2_bits and
bits_2_value.
*******************************************************************************/
/*******************************************************************************
INCLUDES
*******************************************************************************/
#include "liblte_common.h"
/*******************************************************************************
DEFINES
*******************************************************************************/
/*******************************************************************************
TYPEDEFS
*******************************************************************************/
/*******************************************************************************
GLOBAL VARIABLES
*******************************************************************************/
/*******************************************************************************
FUNCTIONS
*******************************************************************************/
/*********************************************************************
Name: liblte_value_2_bits
Description: Converts a value to a bit string
*********************************************************************/
void liblte_value_2_bits(uint32 value,
uint8 **bits,
uint32 N_bits)
{
uint32 i;
for(i=0; i<N_bits; i++)
{
(*bits)[i] = (value >> (N_bits-i-1)) & 0x1;
}
*bits += N_bits;
}
/*********************************************************************
Name: liblte_bits_2_value
Description: Converts a bit string to a value
*********************************************************************/
uint32 liblte_bits_2_value(uint8 **bits,
uint32 N_bits)
{
uint32 value = 0;
uint32 i;
for(i=0; i<N_bits; i++)
{
value |= (*bits)[i] << (N_bits-i-1);
}
*bits += N_bits;
return(value);
}
/*********************************************************************
Name: liblte_pack
Description: Pack a bit array into a byte array
*********************************************************************/
void liblte_pack(LIBLTE_BIT_MSG_STRUCT *bits,
LIBLTE_BYTE_MSG_STRUCT *bytes)
{
uint8_t* bit_ptr = bits->msg;
uint32_t i;
for(i=0; i<bits->N_bits/8; i++)
{
bytes->msg[i] = liblte_bits_2_value(&bit_ptr, 8);
}
bytes->N_bytes = bits->N_bits/8;
if(bits->N_bits%8 > 0)
{
bytes->msg[bytes->N_bytes] = liblte_bits_2_value(&bit_ptr, bits->N_bits%8);
bytes->N_bytes++;
}
}
/*********************************************************************
Name: liblte_unpack
Description: Unpack a byte array into a bit array
*********************************************************************/
void liblte_unpack(LIBLTE_BYTE_MSG_STRUCT *bytes,
LIBLTE_BIT_MSG_STRUCT *bits)
{
uint8_t *bit_ptr = bits->msg;
uint32_t i;
for(i=0; i<bytes->N_bytes; i++)
{
liblte_value_2_bits(bytes->msg[i], &bit_ptr, 8);
}
bits->N_bits = bytes->N_bytes*8;
}
/*********************************************************************
Name: liblte_pack
Description: Pack a bit array into a byte array
*********************************************************************/
void liblte_pack(uint8_t *bits, uint32_t n_bits, uint8_t *bytes)
{
uint8_t* bit_ptr = bits;
uint32_t i;
for(i=0; i<n_bits/8; i++)
{
bytes[i] = liblte_bits_2_value(&bit_ptr, 8);
}
if(n_bits%8 > 0)
{
bytes[n_bits/8] = liblte_bits_2_value(&bit_ptr, n_bits%8);
}
}
/*********************************************************************
Name: liblte_unpack
Description: Unpack a byte array into a bit array
*********************************************************************/
void liblte_unpack(uint8_t *bytes, uint32_t n_bytes, uint8_t *bits)
{
uint8_t *bit_ptr = bits;
uint32_t i;
for(i=0; i<n_bytes; i++)
{
liblte_value_2_bits(bytes[i], &bit_ptr, 8);
}
}
/*********************************************************************
Name: liblte_align_up
Description: Aligns a pointer to a multibyte boundary
*********************************************************************/
void liblte_align_up(uint8_t **ptr, uint32_t align)
{
while( (uint64_t)(*ptr) % align > 0)
{
(*ptr)++;
}
}
/*********************************************************************
Name: liblte_align_up_zero
Description: Aligns a pointer to a multibyte boundary and zeros
bytes skipped
*********************************************************************/
void liblte_align_up_zero(uint8_t **ptr, uint32_t align)
{
while( (uint64_t)(*ptr) % align > 0)
{
**ptr = 0;
(*ptr)++;
}
}

10954
liblte/src/liblte_mme.cc Normal file

File diff suppressed because it is too large Load Diff

13676
liblte/src/liblte_rrc.cc Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff