2020-11-30 11:43:50 -08:00
|
|
|
/**
|
2022-04-29 00:28:44 -07:00
|
|
|
* Copyright 2013-2022 Software Radio Systems Limited
|
2019-04-23 01:53:11 -07:00
|
|
|
*
|
2021-04-22 01:59:40 -07:00
|
|
|
* This file is part of srsRAN.
|
2019-04-23 01:53:11 -07:00
|
|
|
*
|
2021-04-22 01:59:40 -07:00
|
|
|
* srsRAN is free software: you can redistribute it and/or modify
|
2021-03-28 14:12:42 -07:00
|
|
|
* 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.
|
|
|
|
*
|
2021-04-22 01:59:40 -07:00
|
|
|
* srsRAN is distributed in the hope that it will be useful,
|
2021-03-28 14:12:42 -07:00
|
|
|
* 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.
|
2019-04-23 01:53:11 -07:00
|
|
|
*
|
2021-03-28 14:12:42 -07:00
|
|
|
* A copy of the GNU Affero General Public License can be found in
|
|
|
|
* the LICENSE file in the top-level directory of this distribution
|
|
|
|
* and at http://www.gnu.org/licenses/.
|
2019-04-23 01:53:11 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
#ifndef SRSRAN_SSL_H
|
|
|
|
#define SRSRAN_SSL_H
|
2017-05-18 03:05:07 -07:00
|
|
|
|
|
|
|
#ifdef HAVE_POLARSSL
|
|
|
|
|
|
|
|
#include "polarssl/aes.h"
|
2019-07-10 06:27:48 -07:00
|
|
|
#include "polarssl/sha256.h"
|
2017-05-18 03:05:07 -07:00
|
|
|
|
2021-07-27 08:52:02 -07:00
|
|
|
inline void sha256(const unsigned char* key,
|
|
|
|
size_t keylen,
|
|
|
|
const unsigned char* input,
|
|
|
|
size_t ilen,
|
|
|
|
unsigned char output[32],
|
|
|
|
int is224)
|
2017-05-18 03:05:07 -07:00
|
|
|
{
|
|
|
|
sha256_hmac(key, keylen, input, ilen, output, is224);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAVE_POLARSSL
|
|
|
|
|
|
|
|
#ifdef HAVE_MBEDTLS
|
|
|
|
|
|
|
|
#include "mbedtls/aes.h"
|
2019-07-10 06:27:48 -07:00
|
|
|
#include "mbedtls/md.h"
|
2017-05-18 03:05:07 -07:00
|
|
|
|
|
|
|
typedef mbedtls_aes_context aes_context;
|
|
|
|
|
2019-07-10 06:27:48 -07:00
|
|
|
#define AES_ENCRYPT 1
|
|
|
|
#define AES_DECRYPT 0
|
2017-05-18 03:05:07 -07:00
|
|
|
|
2021-07-27 08:52:02 -07:00
|
|
|
inline int aes_setkey_enc(aes_context* ctx, const unsigned char* key, unsigned int keysize)
|
2017-05-18 03:05:07 -07:00
|
|
|
{
|
|
|
|
return mbedtls_aes_setkey_enc(ctx, key, keysize);
|
|
|
|
}
|
|
|
|
|
2021-07-27 08:52:02 -07:00
|
|
|
inline int aes_crypt_ecb(aes_context* ctx, int mode, const unsigned char input[16], unsigned char output[16])
|
2017-05-18 03:05:07 -07:00
|
|
|
{
|
|
|
|
return mbedtls_aes_crypt_ecb(ctx, mode, input, output);
|
|
|
|
}
|
|
|
|
|
2021-07-27 08:52:02 -07:00
|
|
|
inline int aes_crypt_ctr(aes_context* ctx,
|
|
|
|
size_t length,
|
|
|
|
size_t* nc_off,
|
|
|
|
unsigned char nonce_counter[16],
|
|
|
|
unsigned char stream_block[16],
|
|
|
|
const unsigned char* input,
|
|
|
|
unsigned char* output)
|
2017-11-30 09:09:12 -08:00
|
|
|
{
|
2019-07-10 06:27:48 -07:00
|
|
|
return mbedtls_aes_crypt_ctr(ctx, length, nc_off, nonce_counter, stream_block, input, output);
|
2017-11-30 09:09:12 -08:00
|
|
|
}
|
|
|
|
|
2021-07-27 08:52:02 -07:00
|
|
|
inline void sha256(const unsigned char* key,
|
|
|
|
size_t keylen,
|
|
|
|
const unsigned char* input,
|
|
|
|
size_t ilen,
|
|
|
|
unsigned char output[32],
|
|
|
|
int is224)
|
2017-05-18 03:05:07 -07:00
|
|
|
{
|
2019-07-10 06:27:48 -07:00
|
|
|
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), key, keylen, input, ilen, output);
|
2017-05-18 03:05:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAVE_MBEDTLS
|
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
#endif // SRSRAN_SSL_H
|