adding crypto files
This commit is contained in:
1 parent
b0be7b356d
commit
e8af5b5c0e
17 files changed
+9723
No files matched your search
@@ -0,0 +1,263 @@
|
||||
/*
|
||||
* crypto.c
|
||||
*
|
||||
* Created on: Jun 25, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
//#include <nordic_common.h>
|
||||
//#include <app_error.h>
|
||||
//#include <pstorage.h>
|
||||
//#include <app_scheduler.h>
|
||||
#include <nrf_soc.h>
|
||||
#include <Nffs.h>
|
||||
|
||||
#include "crypto.h"
|
||||
|
||||
#define CRYPTO_INSTANCE 2 // Change this to force key regeneration on next run
|
||||
|
||||
crypto_keys_t crypto_keys;
|
||||
|
||||
static const uint8_t zeros64[64];
|
||||
//static pstorage_handle_t crypto_store_handle;
|
||||
//static volatile uint8_t crypto_storing;
|
||||
static uint8_t crypto_loadKeys(void);
|
||||
//static void crypto_pstorage_callback(pstorage_handle_t *p_handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len);
|
||||
|
||||
#define CRYPTO_KEYFILE "/adafruit/homekit/key"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t valid0;
|
||||
uint8_t instance;
|
||||
|
||||
// Local keys
|
||||
uint8_t srp_b[32];
|
||||
uint8_t srp_salt[16];
|
||||
uint8_t srp_v[384];
|
||||
uint8_t srp_B[384];
|
||||
uint8_t sign_secret[64];
|
||||
|
||||
// Client keys
|
||||
uint8_t clientname[36];
|
||||
uint8_t ltpk[32];
|
||||
|
||||
uint8_t valid1;
|
||||
uint8_t __padding__[1];
|
||||
} crypto_persistent_keys_t;
|
||||
|
||||
void crypto_init(void)
|
||||
{
|
||||
// uint32_t err_code;
|
||||
//
|
||||
// static const pstorage_module_param_t param =
|
||||
// {
|
||||
// .cb = crypto_pstorage_callback,
|
||||
// .block_size = sizeof(crypto_persistent_keys_t),
|
||||
// .block_count = 1
|
||||
// };
|
||||
// err_code = pstorage_register((pstorage_module_param_t*)¶m, &crypto_store_handle);
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
|
||||
if (!crypto_loadKeys())
|
||||
{
|
||||
srp_init();
|
||||
|
||||
crypto_sign_keypair(crypto_keys.sign.public, crypto_keys.sign.secret);
|
||||
|
||||
// Store for reuse
|
||||
crypto_scheduleStoreKeys();
|
||||
crypto_storeKeys();
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t crypto_loadKeys(void)
|
||||
{
|
||||
// uint32_t err_code;
|
||||
//
|
||||
// pstorage_handle_t handle;
|
||||
// err_code = pstorage_block_identifier_get(&crypto_store_handle, 0, &handle);
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
|
||||
// crypto_persistent_keys_t keys = {};
|
||||
// err_code = pstorage_load((uint8_t*)&keys, &handle, sizeof(keys), 0);
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
|
||||
NffsFile file(CRYPTO_KEYFILE, FS_ACCESS_WRITE | FS_ACCESS_READ);
|
||||
|
||||
if ( !file.exists() ) return 0;
|
||||
|
||||
crypto_persistent_keys_t keys = {};
|
||||
file.read(&keys, sizeof(keys));
|
||||
|
||||
if (keys.valid0 == 0x55 && keys.valid1 == 0xAA && keys.instance == CRYPTO_INSTANCE)
|
||||
{
|
||||
// Valid
|
||||
memcpy(srp.b, keys.srp_b, 32);
|
||||
memcpy(srp.salt, keys.srp_salt, 16);
|
||||
memcpy(srp.v, keys.srp_v, 384);
|
||||
memcpy(srp.B, keys.srp_B, 384);
|
||||
memcpy(crypto_keys.sign.secret, keys.sign_secret, 64);
|
||||
memcpy(crypto_keys.client.name, keys.clientname, 36);
|
||||
memcpy(crypto_keys.client.ltpk, keys.ltpk, 32);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void crypto_scheduleStoreKeys(void)
|
||||
{
|
||||
// crypto_storing = 1;
|
||||
}
|
||||
|
||||
void crypto_storeKeys(void)
|
||||
{
|
||||
// uint32_t err_code;
|
||||
|
||||
// if (crypto_storing)
|
||||
{
|
||||
// pstorage_handle_t handle;
|
||||
// err_code = pstorage_block_identifier_get(&crypto_store_handle, 0, &handle);
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
|
||||
crypto_persistent_keys_t keys = {};
|
||||
memcpy(keys.srp_b, srp.b, 32);
|
||||
memcpy(keys.srp_salt, srp.salt, 16);
|
||||
memcpy(keys.srp_v, srp.v, 384);
|
||||
memcpy(keys.srp_B, srp.B, 384);
|
||||
memcpy(keys.sign_secret, crypto_keys.sign.secret, 64);
|
||||
memcpy(keys.clientname, crypto_keys.client.name, 36);
|
||||
memcpy(keys.ltpk, crypto_keys.client.ltpk, 32);
|
||||
|
||||
keys.instance = CRYPTO_INSTANCE;
|
||||
keys.valid0 = 0x55;
|
||||
keys.valid1 = 0xAA;
|
||||
|
||||
Nffs.writeFile(CRYPTO_KEYFILE, &keys, sizeof(keys));
|
||||
|
||||
// err_code = pstorage_update(&handle, (uint8_t*)&keys, sizeof(keys), 0);
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
//
|
||||
// // Pump events until the store is done
|
||||
// while (crypto_storing)
|
||||
// {
|
||||
// err_code = sd_app_evt_wait();
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
// app_sched_execute();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
//static void crypto_pstorage_callback(pstorage_handle_t *p_handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len)
|
||||
//{
|
||||
// switch (op_code)
|
||||
// {
|
||||
// case PSTORAGE_UPDATE_OP_CODE:
|
||||
// crypto_storing = 0;
|
||||
// break;
|
||||
//
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
//}
|
||||
|
||||
uint8_t crypto_advertise(void)
|
||||
{
|
||||
// If we have a client name, we don't advertise, otherwise we do.
|
||||
if (crypto_keys.client.name[0])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t crypto_verifyAndDecrypt(const uint8_t* key, uint8_t* nonce, uint8_t* encrypted, uint8_t length, uint8_t* output_buf, uint8_t* mac)
|
||||
{
|
||||
uint8_t polykey[sizeof(zeros64)];
|
||||
crypto_stream_chacha20_xor(polykey, zeros64, sizeof(zeros64), nonce, key, 0);
|
||||
|
||||
uint8_t padding = (16 - length % 16) % 16;
|
||||
uint8_t message[length + padding + 16];
|
||||
memcpy(message, encrypted, length);
|
||||
memset(message + length, 0, padding + 16);
|
||||
message[length + padding + 8] = (uint8_t)length;
|
||||
message[length + padding + 9] = (uint8_t)(length >> 8);
|
||||
|
||||
if (crypto_onetimeauth_poly1305_verify(mac, message, sizeof(message), polykey) != 0)
|
||||
{
|
||||
// Fail
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
crypto_stream_chacha20_xor(output_buf, message, length, nonce, key, 1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void crypto_encryptAndSeal(const uint8_t* key, uint8_t* nonce, uint8_t* plain, uint16_t length, uint8_t* output_buf, uint8_t* output_mac)
|
||||
{
|
||||
uint8_t polykey[sizeof(zeros64)];
|
||||
crypto_stream_chacha20_xor(polykey, zeros64, sizeof(zeros64), nonce, key, 0);
|
||||
|
||||
uint8_t padding = (16 - length % 16) % 16;
|
||||
uint8_t message[length + padding + 16];
|
||||
|
||||
crypto_stream_chacha20_xor(message, plain, length, nonce, key, 1);
|
||||
memset(message + length, 0, padding + 16);
|
||||
message[length + padding + 8] = (uint8_t)length;
|
||||
message[length + padding + 9] = (uint8_t)(length >> 8);
|
||||
|
||||
crypto_onetimeauth_poly1305(output_mac, message, sizeof(message), polykey);
|
||||
|
||||
memcpy(output_buf, message, length);
|
||||
}
|
||||
|
||||
void crypto_sha512hmac(uint8_t* hash, uint8_t* salt, uint8_t salt_length, uint8_t* data, uint8_t data_length)
|
||||
{
|
||||
uint8_t message1[128 + data_length];
|
||||
uint8_t message2[128 + 64];
|
||||
|
||||
memset(message1, 0x36, 128);
|
||||
memset(message2, 0x5C, 128);
|
||||
for (unsigned i = salt_length; i--; )
|
||||
{
|
||||
message1[i] = 0x36 ^ salt[i];
|
||||
message2[i] = 0x5C ^ salt[i];
|
||||
}
|
||||
memcpy(message1 + 128, data, data_length);
|
||||
crypto_hash_sha512(message2 + 128, message1, sizeof(message1));
|
||||
crypto_hash_sha512(hash, message2, sizeof(message2));
|
||||
}
|
||||
|
||||
void crypto_hkdf(uint8_t* target, uint8_t* salt, uint8_t salt_length, uint8_t* info, uint8_t info_length, uint8_t* ikm, uint8_t ikm_length)
|
||||
{
|
||||
crypto_sha512hmac(target, salt, salt_length, ikm, ikm_length);
|
||||
crypto_sha512hmac(target, target, 64, info, info_length);
|
||||
}
|
||||
|
||||
void crypto_transportEncrypt(uint8_t* key, uint8_t* nonce, uint8_t* plaintext, uint16_t plength, uint8_t* ciphertext, uint16_t* clength)
|
||||
{
|
||||
crypto_encryptAndSeal(key, nonce, plaintext, plength, ciphertext, ciphertext + plength);
|
||||
for (unsigned i = 0; i < 8 && !++nonce[i]; i++)
|
||||
;
|
||||
*clength = plength + 16;
|
||||
}
|
||||
|
||||
uint8_t crypto_transportDecrypt(uint8_t* key, uint8_t* nonce, uint8_t* ciphertext, uint16_t clength, uint8_t* plaintext, uint16_t* plength)
|
||||
{
|
||||
uint8_t r = crypto_verifyAndDecrypt(key, nonce, ciphertext, clength - 16, plaintext, ciphertext + clength - 16);
|
||||
for (unsigned i = 0; i < 8 && !++nonce[i]; i++)
|
||||
;
|
||||
*plength = clength - 16;
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* crypto.h
|
||||
*
|
||||
* Created on: Jun 25, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#ifndef HOMEKIT_CRYPTO_H_
|
||||
#define HOMEKIT_CRYPTO_H_
|
||||
|
||||
//#define USE_UNACL_SCALARMULT 1
|
||||
#define USE_TWEETNACL_SCALARMULT 1
|
||||
|
||||
#if defined(USE_UNACL_SCALARMULT)
|
||||
#include "uNaCl/api.h"
|
||||
#endif
|
||||
#include "tweetnacl-modified/tweetnacl.h"
|
||||
#include "srp/srp.h"
|
||||
#include "random.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
struct
|
||||
{
|
||||
// Public key is the last 32-bytes of the secret key
|
||||
union
|
||||
{
|
||||
uint8_t secret[64];
|
||||
struct
|
||||
{
|
||||
uint8_t __ignore__[32];
|
||||
uint8_t public[32];
|
||||
};
|
||||
};
|
||||
} sign;
|
||||
|
||||
// Client we are paired with
|
||||
struct
|
||||
{
|
||||
uint8_t name[36];
|
||||
uint8_t ltpk[32];
|
||||
} client;
|
||||
} crypto_keys_t;
|
||||
|
||||
extern crypto_keys_t crypto_keys;
|
||||
|
||||
|
||||
extern void crypto_init(void);
|
||||
extern void crypto_scheduleStoreKeys(void);
|
||||
extern void crypto_storeKeys(void);
|
||||
extern uint8_t crypto_verifyAndDecrypt(const uint8_t* key, uint8_t* nonce, uint8_t* encrypted, uint8_t length, uint8_t* output_buf, uint8_t* mac);
|
||||
extern void crypto_encryptAndSeal(const uint8_t* key, uint8_t* nonce, uint8_t* plain, uint16_t length, uint8_t* output_buf, uint8_t* output_mac);
|
||||
extern void crypto_sha512hmac(uint8_t* hash, uint8_t* salt, uint8_t salt_length, uint8_t* data, uint8_t data_length);
|
||||
extern void crypto_hkdf(uint8_t* target, uint8_t* salt, uint8_t salt_length, uint8_t* info, uint8_t info_length, uint8_t* ikm, uint8_t ikm_length);
|
||||
extern void crypto_transportEncrypt(uint8_t* key, uint8_t* nonce, uint8_t* plaintext, uint16_t plength, uint8_t* ciphertext, uint16_t* clength);
|
||||
extern uint8_t crypto_transportDecrypt(uint8_t* key, uint8_t* nonce, uint8_t* ciphertext, uint16_t clength, uint8_t* plaintext, uint16_t* plength);
|
||||
extern uint8_t crypto_advertise(void);
|
||||
|
||||
#endif /* HOMEKIT_CRYPTO_H_ */
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* random.c
|
||||
*
|
||||
* Created on: Jun 10, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
//#include <softdevice_handler.h>
|
||||
|
||||
#include "random.h"
|
||||
|
||||
|
||||
void random_create(uint8_t* p_result, uint8_t length)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
while (length)
|
||||
{
|
||||
uint8_t available = 0;
|
||||
err_code = sd_rand_application_bytes_available_get(&available);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
if (available)
|
||||
{
|
||||
available = available < length ? available : length;
|
||||
err_code = sd_rand_application_vector_get(p_result, available);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
p_result += available;
|
||||
length -= available;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void randombytes(uint8_t* p_result, uint64_t length)
|
||||
{
|
||||
random_create(p_result, (uint8_t)length);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* random.h
|
||||
*
|
||||
* Created on: Jun 10, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#ifndef HOMEKIT_RANDOM_H_
|
||||
#define HOMEKIT_RANDOM_H_
|
||||
|
||||
extern void random_create(uint8_t* p_result, uint8_t length);
|
||||
extern void randombytes(uint8_t* p_result, uint64_t length);
|
||||
|
||||
#endif /* HOMEKIT_RANDOM_H_ */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,754 @@
|
||||
/**
|
||||
* \file bignum.h
|
||||
*
|
||||
* \brief Multi-precision integer library
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef POLARSSL_BIGNUM_H
|
||||
#define POLARSSL_BIGNUM_H
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#if defined(POLARSSL_FS_IO)
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
|
||||
#include <basetsd.h>
|
||||
#if (_MSC_VER <= 1200)
|
||||
typedef signed short int16_t;
|
||||
typedef unsigned short uint16_t;
|
||||
#else
|
||||
typedef INT16 int16_t;
|
||||
typedef UINT16 uint16_t;
|
||||
#endif
|
||||
typedef INT32 int32_t;
|
||||
typedef INT64 int64_t;
|
||||
typedef UINT32 uint32_t;
|
||||
typedef UINT64 uint64_t;
|
||||
#else
|
||||
#include <inttypes.h>
|
||||
#endif /* _MSC_VER && !EFIX64 && !EFI32 */
|
||||
|
||||
#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
|
||||
#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
|
||||
#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
|
||||
#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
|
||||
#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
|
||||
#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
|
||||
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
|
||||
#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
|
||||
|
||||
#define MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
|
||||
|
||||
/*
|
||||
* Maximum size MPIs are allowed to grow to in number of limbs.
|
||||
*/
|
||||
#define POLARSSL_MPI_MAX_LIMBS 10000
|
||||
|
||||
#if !defined(POLARSSL_MPI_WINDOW_SIZE)
|
||||
/*
|
||||
* Maximum window size used for modular exponentiation. Default: 6
|
||||
* Minimum value: 1. Maximum value: 6.
|
||||
*
|
||||
* Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
|
||||
* for the sliding window calculation. (So 64 by default)
|
||||
*
|
||||
* Reduction in size, reduces speed.
|
||||
*/
|
||||
#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
|
||||
#endif /* !POLARSSL_MPI_WINDOW_SIZE */
|
||||
|
||||
#if !defined(POLARSSL_MPI_MAX_SIZE)
|
||||
/*
|
||||
* Maximum size of MPIs allowed in bits and bytes for user-MPIs.
|
||||
* ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
|
||||
*
|
||||
* Note: Calculations can results temporarily in larger MPIs. So the number
|
||||
* of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
|
||||
*/
|
||||
#define POLARSSL_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
|
||||
#endif /* !POLARSSL_MPI_MAX_SIZE */
|
||||
|
||||
#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
|
||||
|
||||
/*
|
||||
* When reading from files with mpi_read_file() and writing to files with
|
||||
* mpi_write_file() the buffer should have space
|
||||
* for a (short) label, the MPI (in the provided radix), the newline
|
||||
* characters and the '\0'.
|
||||
*
|
||||
* By default we assume at least a 10 char label, a minimum radix of 10
|
||||
* (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
|
||||
* Autosized at compile time for at least a 10 char label, a minimum radix
|
||||
* of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
|
||||
*
|
||||
* This used to be statically sized to 1250 for a maximum of 4096 bit
|
||||
* numbers (1234 decimal chars).
|
||||
*
|
||||
* Calculate using the formula:
|
||||
* POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
|
||||
* LabelSize + 6
|
||||
*/
|
||||
#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
|
||||
#define LN_2_DIV_LN_10_SCALE100 332
|
||||
#define POLARSSL_MPI_RW_BUFFER_SIZE ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
|
||||
|
||||
/*
|
||||
* Define the base integer type, architecture-wise
|
||||
*/
|
||||
#if defined(POLARSSL_HAVE_INT8)
|
||||
typedef signed char t_sint;
|
||||
typedef unsigned char t_uint;
|
||||
typedef uint16_t t_udbl;
|
||||
#define POLARSSL_HAVE_UDBL
|
||||
#else
|
||||
#if defined(POLARSSL_HAVE_INT16)
|
||||
typedef int16_t t_sint;
|
||||
typedef uint16_t t_uint;
|
||||
typedef uint32_t t_udbl;
|
||||
#define POLARSSL_HAVE_UDBL
|
||||
#else
|
||||
/*
|
||||
* 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
|
||||
* by defining POLARSSL_HAVE_INT32 and undefining POLARSSL_HAVE_ASM
|
||||
*/
|
||||
#if ( ! defined(POLARSSL_HAVE_INT32) && \
|
||||
defined(_MSC_VER) && defined(_M_AMD64) )
|
||||
#define POLARSSL_HAVE_INT64
|
||||
typedef int64_t t_sint;
|
||||
typedef uint64_t t_uint;
|
||||
#else
|
||||
#if ( ! defined(POLARSSL_HAVE_INT32) && \
|
||||
defined(__GNUC__) && ( \
|
||||
defined(__amd64__) || defined(__x86_64__) || \
|
||||
defined(__ppc64__) || defined(__powerpc64__) || \
|
||||
defined(__ia64__) || defined(__alpha__) || \
|
||||
(defined(__sparc__) && defined(__arch64__)) || \
|
||||
defined(__s390x__) || defined(__mips64) ) )
|
||||
#define POLARSSL_HAVE_INT64
|
||||
typedef int64_t t_sint;
|
||||
typedef uint64_t t_uint;
|
||||
typedef unsigned int t_udbl __attribute__((mode(TI)));
|
||||
#define POLARSSL_HAVE_UDBL
|
||||
#else
|
||||
#define POLARSSL_HAVE_INT32
|
||||
typedef int32_t t_sint;
|
||||
typedef uint32_t t_uint;
|
||||
#if ( defined(_MSC_VER) && defined(_M_IX86) )
|
||||
typedef uint64_t t_udbl;
|
||||
#define POLARSSL_HAVE_UDBL
|
||||
#else
|
||||
#if defined( POLARSSL_HAVE_LONGLONG )
|
||||
typedef unsigned long long t_udbl;
|
||||
#define POLARSSL_HAVE_UDBL
|
||||
#endif
|
||||
#endif
|
||||
#endif /* !POLARSSL_HAVE_INT32 && __GNUC__ && 64-bit platform */
|
||||
#endif /* !POLARSSL_HAVE_INT32 && _MSC_VER && _M_AMD64 */
|
||||
#endif /* POLARSSL_HAVE_INT16 */
|
||||
#endif /* POLARSSL_HAVE_INT8 */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief MPI structure
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int s; /*!< integer sign */
|
||||
size_t n; /*!< total # of limbs */
|
||||
t_uint *p; /*!< pointer to limbs */
|
||||
}
|
||||
mpi;
|
||||
|
||||
/**
|
||||
* \brief Initialize one MPI (make internal references valid)
|
||||
* This just makes it ready to be set or freed,
|
||||
* but does not define a value for the MPI.
|
||||
*
|
||||
* \param X One MPI to initialize.
|
||||
*/
|
||||
void mpi_init( mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Unallocate one MPI
|
||||
*
|
||||
* \param X One MPI to unallocate.
|
||||
*/
|
||||
void mpi_free( mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Enlarge to the specified number of limbs
|
||||
*
|
||||
* \param X MPI to grow
|
||||
* \param nblimbs The target number of limbs
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_grow( mpi *X, size_t nblimbs );
|
||||
|
||||
/**
|
||||
* \brief Resize down, keeping at least the specified number of limbs
|
||||
*
|
||||
* \param X MPI to shrink
|
||||
* \param nblimbs The minimum number of limbs to keep
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_shrink( mpi *X, size_t nblimbs );
|
||||
|
||||
/**
|
||||
* \brief Copy the contents of Y into X
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param Y Source MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_copy( mpi *X, const mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Swap the contents of X and Y
|
||||
*
|
||||
* \param X First MPI value
|
||||
* \param Y Second MPI value
|
||||
*/
|
||||
void mpi_swap( mpi *X, mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Safe conditional assignement X = Y if assign is 1
|
||||
*
|
||||
* \param X MPI to conditionally assign to
|
||||
* \param Y Value to be assigned
|
||||
* \param assign 1: perform the assignment, 0: keep X's original value
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
*
|
||||
* \note This function is equivalent to
|
||||
* if( assign ) mpi_copy( X, Y );
|
||||
* except that it avoids leaking any information about whether
|
||||
* the assignment was done or not (the above code may leak
|
||||
* information through branch prediction and/or memory access
|
||||
* patterns analysis).
|
||||
*/
|
||||
int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign );
|
||||
|
||||
/**
|
||||
* \brief Safe conditional swap X <-> Y if swap is 1
|
||||
*
|
||||
* \param X First mpi value
|
||||
* \param Y Second mpi value
|
||||
* \param assign 1: perform the swap, 0: keep X and Y's original values
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
*
|
||||
* \note This function is equivalent to
|
||||
* if( assign ) mpi_swap( X, Y );
|
||||
* except that it avoids leaking any information about whether
|
||||
* the assignment was done or not (the above code may leak
|
||||
* information through branch prediction and/or memory access
|
||||
* patterns analysis).
|
||||
*/
|
||||
int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign );
|
||||
|
||||
/**
|
||||
* \brief Set value from integer
|
||||
*
|
||||
* \param X MPI to set
|
||||
* \param z Value to use
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_lset( mpi *X, t_sint z );
|
||||
|
||||
/**
|
||||
* \brief Get a specific bit from X
|
||||
*
|
||||
* \param X MPI to use
|
||||
* \param pos Zero-based index of the bit in X
|
||||
*
|
||||
* \return Either a 0 or a 1
|
||||
*/
|
||||
int mpi_get_bit( const mpi *X, size_t pos );
|
||||
|
||||
/**
|
||||
* \brief Set a bit of X to a specific value of 0 or 1
|
||||
*
|
||||
* \note Will grow X if necessary to set a bit to 1 in a not yet
|
||||
* existing limb. Will not grow if bit should be set to 0
|
||||
*
|
||||
* \param X MPI to use
|
||||
* \param pos Zero-based index of the bit in X
|
||||
* \param val The value to set the bit to (0 or 1)
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
|
||||
*/
|
||||
int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
|
||||
|
||||
/**
|
||||
* \brief Return the number of zero-bits before the least significant
|
||||
* '1' bit
|
||||
*
|
||||
* Note: Thus also the zero-based index of the least significant '1' bit
|
||||
*
|
||||
* \param X MPI to use
|
||||
*/
|
||||
size_t mpi_lsb( const mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Return the number of bits up to and including the most
|
||||
* significant '1' bit'
|
||||
*
|
||||
* Note: Thus also the one-based index of the most significant '1' bit
|
||||
*
|
||||
* \param X MPI to use
|
||||
*/
|
||||
size_t mpi_msb( const mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Return the total size in bytes
|
||||
*
|
||||
* \param X MPI to use
|
||||
*/
|
||||
size_t mpi_size( const mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Import from an ASCII string
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param radix Input numeric base
|
||||
* \param s Null-terminated string buffer
|
||||
*
|
||||
* \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
|
||||
*/
|
||||
int mpi_read_string( mpi *X, int radix, const char *s );
|
||||
|
||||
/**
|
||||
* \brief Export into an ASCII string
|
||||
*
|
||||
* \param X Source MPI
|
||||
* \param radix Output numeric base
|
||||
* \param s String buffer
|
||||
* \param slen String buffer size
|
||||
*
|
||||
* \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
|
||||
* *slen is always updated to reflect the amount
|
||||
* of data that has (or would have) been written.
|
||||
*
|
||||
* \note Call this function with *slen = 0 to obtain the
|
||||
* minimum required buffer size in *slen.
|
||||
*/
|
||||
int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
|
||||
|
||||
#if defined(POLARSSL_FS_IO)
|
||||
/**
|
||||
* \brief Read X from an opened file
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param radix Input numeric base
|
||||
* \param fin Input file handle
|
||||
*
|
||||
* \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
|
||||
* the file read buffer is too small or a
|
||||
* POLARSSL_ERR_MPI_XXX error code
|
||||
*/
|
||||
int mpi_read_file( mpi *X, int radix, FILE *fin );
|
||||
|
||||
/**
|
||||
* \brief Write X into an opened file, or stdout if fout is NULL
|
||||
*
|
||||
* \param p Prefix, can be NULL
|
||||
* \param X Source MPI
|
||||
* \param radix Output numeric base
|
||||
* \param fout Output file handle (can be NULL)
|
||||
*
|
||||
* \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
|
||||
*
|
||||
* \note Set fout == NULL to print X on the console.
|
||||
*/
|
||||
int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
|
||||
#endif /* POLARSSL_FS_IO */
|
||||
|
||||
/**
|
||||
* \brief Import X from unsigned binary data, big endian
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param buf Input buffer
|
||||
* \param buflen Input buffer size
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
|
||||
|
||||
/**
|
||||
* \brief Export X into unsigned binary data, big endian.
|
||||
* Always fills the whole buffer, which will start with zeros
|
||||
* if the number is smaller.
|
||||
*
|
||||
* \param X Source MPI
|
||||
* \param buf Output buffer
|
||||
* \param buflen Output buffer size
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
|
||||
*/
|
||||
int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
|
||||
|
||||
/**
|
||||
* \brief Left-shift: X <<= count
|
||||
*
|
||||
* \param X MPI to shift
|
||||
* \param count Amount to shift
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_shift_l( mpi *X, size_t count );
|
||||
|
||||
/**
|
||||
* \brief Right-shift: X >>= count
|
||||
*
|
||||
* \param X MPI to shift
|
||||
* \param count Amount to shift
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_shift_r( mpi *X, size_t count );
|
||||
|
||||
/**
|
||||
* \brief Compare unsigned values
|
||||
*
|
||||
* \param X Left-hand MPI
|
||||
* \param Y Right-hand MPI
|
||||
*
|
||||
* \return 1 if |X| is greater than |Y|,
|
||||
* -1 if |X| is lesser than |Y| or
|
||||
* 0 if |X| is equal to |Y|
|
||||
*/
|
||||
int mpi_cmp_abs( const mpi *X, const mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Compare signed values
|
||||
*
|
||||
* \param X Left-hand MPI
|
||||
* \param Y Right-hand MPI
|
||||
*
|
||||
* \return 1 if X is greater than Y,
|
||||
* -1 if X is lesser than Y or
|
||||
* 0 if X is equal to Y
|
||||
*/
|
||||
int mpi_cmp_mpi( const mpi *X, const mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Compare signed values
|
||||
*
|
||||
* \param X Left-hand MPI
|
||||
* \param z The integer value to compare to
|
||||
*
|
||||
* \return 1 if X is greater than z,
|
||||
* -1 if X is lesser than z or
|
||||
* 0 if X is equal to z
|
||||
*/
|
||||
int mpi_cmp_int( const mpi *X, t_sint z );
|
||||
|
||||
/**
|
||||
* \brief Unsigned addition: X = |A| + |B|
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Unsigned subtraction: X = |A| - |B|
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
|
||||
*/
|
||||
int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Signed addition: X = A + B
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Signed subtraction: X = A - B
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Signed addition: X = A + b
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param b The integer value to add
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_add_int( mpi *X, const mpi *A, t_sint b );
|
||||
|
||||
/**
|
||||
* \brief Signed subtraction: X = A - b
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param b The integer value to subtract
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
|
||||
|
||||
/**
|
||||
* \brief Baseline multiplication: X = A * B
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Baseline multiplication: X = A * b
|
||||
* Note: despite the functon signature, b is treated as a
|
||||
* t_uint. Negative values of b are treated as large positive
|
||||
* values.
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param b The integer value to multiply with
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
|
||||
|
||||
/**
|
||||
* \brief Division by mpi: A = Q * B + R
|
||||
*
|
||||
* \param Q Destination MPI for the quotient
|
||||
* \param R Destination MPI for the rest value
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
|
||||
*
|
||||
* \note Either Q or R can be NULL.
|
||||
*/
|
||||
int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Division by int: A = Q * b + R
|
||||
*
|
||||
* \param Q Destination MPI for the quotient
|
||||
* \param R Destination MPI for the rest value
|
||||
* \param A Left-hand MPI
|
||||
* \param b Integer to divide by
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
|
||||
*
|
||||
* \note Either Q or R can be NULL.
|
||||
*/
|
||||
int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
|
||||
|
||||
/**
|
||||
* \brief Modulo: R = A mod B
|
||||
*
|
||||
* \param R Destination MPI for the rest value
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
|
||||
* POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
|
||||
*/
|
||||
int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Modulo: r = A mod b
|
||||
*
|
||||
* \param r Destination t_uint
|
||||
* \param A Left-hand MPI
|
||||
* \param b Integer to divide by
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
|
||||
* POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
|
||||
*/
|
||||
int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
|
||||
|
||||
/**
|
||||
* \brief Sliding-window exponentiation: X = A^E mod N
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param E Exponent MPI
|
||||
* \param N Modular MPI
|
||||
* \param _RR Speed-up MPI used for recalculations
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
|
||||
* if E is negative
|
||||
*
|
||||
* \note _RR is used to avoid re-computing R*R mod N across
|
||||
* multiple calls, which speeds up things a bit. It can
|
||||
* be set to NULL if the extra performance is unneeded.
|
||||
*/
|
||||
int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
|
||||
|
||||
/**
|
||||
* \brief Fill an MPI X with size bytes of random
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param size Size in bytes
|
||||
* \param f_rng RNG function
|
||||
* \param p_rng RNG parameter
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_fill_random( mpi *X, size_t size,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng );
|
||||
|
||||
/**
|
||||
* \brief Greatest common divisor: G = gcd(A, B)
|
||||
*
|
||||
* \param G Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Modular inverse: X = A^-1 mod N
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param N Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
|
||||
POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
|
||||
*/
|
||||
int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
|
||||
|
||||
/**
|
||||
* \brief Miller-Rabin primality test
|
||||
*
|
||||
* \param X MPI to check
|
||||
* \param f_rng RNG function
|
||||
* \param p_rng RNG parameter
|
||||
*
|
||||
* \return 0 if successful (probably prime),
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
|
||||
*/
|
||||
int mpi_is_prime( mpi *X,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng );
|
||||
|
||||
/**
|
||||
* \brief Prime number generation
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param nbits Required size of X in bits
|
||||
* ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
|
||||
* \param dh_flag If 1, then (X-1)/2 will be prime too
|
||||
* \param f_rng RNG function
|
||||
* \param p_rng RNG parameter
|
||||
*
|
||||
* \return 0 if successful (probably prime),
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
|
||||
*/
|
||||
int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng );
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int mpi_self_test( int verbose );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* bignum.h */
|
||||
@@ -0,0 +1,870 @@
|
||||
/**
|
||||
* \file bn_mul.h
|
||||
*
|
||||
* \brief Multi-precision integer library
|
||||
*
|
||||
* Copyright (C) 2006-2010, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
/*
|
||||
* Multiply source vector [s] with b, add result
|
||||
* to destination vector [d] and set carry c.
|
||||
*
|
||||
* Currently supports:
|
||||
*
|
||||
* . IA-32 (386+) . AMD64 / EM64T
|
||||
* . IA-32 (SSE2) . Motorola 68000
|
||||
* . PowerPC, 32-bit . MicroBlaze
|
||||
* . PowerPC, 64-bit . TriCore
|
||||
* . SPARC v8 . ARM v3+
|
||||
* . Alpha . MIPS32
|
||||
* . C, longlong . C, generic
|
||||
*/
|
||||
#ifndef POLARSSL_BN_MUL_H
|
||||
#define POLARSSL_BN_MUL_H
|
||||
|
||||
#include "bignum.h"
|
||||
|
||||
#if defined(POLARSSL_HAVE_ASM)
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#if defined(__i386__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"movl %%ebx, %0 \n\t" \
|
||||
"movl %5, %%esi \n\t" \
|
||||
"movl %6, %%edi \n\t" \
|
||||
"movl %7, %%ecx \n\t" \
|
||||
"movl %8, %%ebx \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lodsl \n\t" \
|
||||
"mull %%ebx \n\t" \
|
||||
"addl %%ecx, %%eax \n\t" \
|
||||
"adcl $0, %%edx \n\t" \
|
||||
"addl (%%edi), %%eax \n\t" \
|
||||
"adcl $0, %%edx \n\t" \
|
||||
"movl %%edx, %%ecx \n\t" \
|
||||
"stosl \n\t"
|
||||
|
||||
#if defined(POLARSSL_HAVE_SSE2)
|
||||
|
||||
#define MULADDC_HUIT \
|
||||
"movd %%ecx, %%mm1 \n\t" \
|
||||
"movd %%ebx, %%mm0 \n\t" \
|
||||
"movd (%%edi), %%mm3 \n\t" \
|
||||
"paddq %%mm3, %%mm1 \n\t" \
|
||||
"movd (%%esi), %%mm2 \n\t" \
|
||||
"pmuludq %%mm0, %%mm2 \n\t" \
|
||||
"movd 4(%%esi), %%mm4 \n\t" \
|
||||
"pmuludq %%mm0, %%mm4 \n\t" \
|
||||
"movd 8(%%esi), %%mm6 \n\t" \
|
||||
"pmuludq %%mm0, %%mm6 \n\t" \
|
||||
"movd 12(%%esi), %%mm7 \n\t" \
|
||||
"pmuludq %%mm0, %%mm7 \n\t" \
|
||||
"paddq %%mm2, %%mm1 \n\t" \
|
||||
"movd 4(%%edi), %%mm3 \n\t" \
|
||||
"paddq %%mm4, %%mm3 \n\t" \
|
||||
"movd 8(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm6, %%mm5 \n\t" \
|
||||
"movd 12(%%edi), %%mm4 \n\t" \
|
||||
"paddq %%mm4, %%mm7 \n\t" \
|
||||
"movd %%mm1, (%%edi) \n\t" \
|
||||
"movd 16(%%esi), %%mm2 \n\t" \
|
||||
"pmuludq %%mm0, %%mm2 \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"movd 20(%%esi), %%mm4 \n\t" \
|
||||
"pmuludq %%mm0, %%mm4 \n\t" \
|
||||
"paddq %%mm3, %%mm1 \n\t" \
|
||||
"movd 24(%%esi), %%mm6 \n\t" \
|
||||
"pmuludq %%mm0, %%mm6 \n\t" \
|
||||
"movd %%mm1, 4(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"movd 28(%%esi), %%mm3 \n\t" \
|
||||
"pmuludq %%mm0, %%mm3 \n\t" \
|
||||
"paddq %%mm5, %%mm1 \n\t" \
|
||||
"movd 16(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm2 \n\t" \
|
||||
"movd %%mm1, 8(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm7, %%mm1 \n\t" \
|
||||
"movd 20(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm4 \n\t" \
|
||||
"movd %%mm1, 12(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm2, %%mm1 \n\t" \
|
||||
"movd 24(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm6 \n\t" \
|
||||
"movd %%mm1, 16(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm4, %%mm1 \n\t" \
|
||||
"movd 28(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm3 \n\t" \
|
||||
"movd %%mm1, 20(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm6, %%mm1 \n\t" \
|
||||
"movd %%mm1, 24(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm3, %%mm1 \n\t" \
|
||||
"movd %%mm1, 28(%%edi) \n\t" \
|
||||
"addl $32, %%edi \n\t" \
|
||||
"addl $32, %%esi \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"movd %%mm1, %%ecx \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"emms \n\t" \
|
||||
"movl %4, %%ebx \n\t" \
|
||||
"movl %%ecx, %1 \n\t" \
|
||||
"movl %%edi, %2 \n\t" \
|
||||
"movl %%esi, %3 \n\t" \
|
||||
: "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "eax", "ecx", "edx", "esi", "edi" \
|
||||
);
|
||||
|
||||
#else
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"movl %4, %%ebx \n\t" \
|
||||
"movl %%ecx, %1 \n\t" \
|
||||
"movl %%edi, %2 \n\t" \
|
||||
"movl %%esi, %3 \n\t" \
|
||||
: "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "eax", "ecx", "edx", "esi", "edi" \
|
||||
);
|
||||
#endif /* SSE2 */
|
||||
#endif /* i386 */
|
||||
|
||||
#if defined(__amd64__) || defined (__x86_64__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"movq %3, %%rsi \n\t" \
|
||||
"movq %4, %%rdi \n\t" \
|
||||
"movq %5, %%rcx \n\t" \
|
||||
"movq %6, %%rbx \n\t" \
|
||||
"xorq %%r8, %%r8 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"movq (%%rsi), %%rax \n\t" \
|
||||
"mulq %%rbx \n\t" \
|
||||
"addq $8, %%rsi \n\t" \
|
||||
"addq %%rcx, %%rax \n\t" \
|
||||
"movq %%r8, %%rcx \n\t" \
|
||||
"adcq $0, %%rdx \n\t" \
|
||||
"nop \n\t" \
|
||||
"addq %%rax, (%%rdi) \n\t" \
|
||||
"adcq %%rdx, %%rcx \n\t" \
|
||||
"addq $8, %%rdi \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"movq %%rcx, %0 \n\t" \
|
||||
"movq %%rdi, %1 \n\t" \
|
||||
"movq %%rsi, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "rax", "rcx", "rdx", "rbx", "rsi", "rdi", "r8" \
|
||||
);
|
||||
|
||||
#endif /* AMD64 */
|
||||
|
||||
#if defined(__mc68020__) || defined(__mcpu32__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"movl %3, %%a2 \n\t" \
|
||||
"movl %4, %%a3 \n\t" \
|
||||
"movl %5, %%d3 \n\t" \
|
||||
"movl %6, %%d2 \n\t" \
|
||||
"moveq #0, %%d0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"moveq #0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"addxl %%d4, %%d3 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"movl %%d3, %0 \n\t" \
|
||||
"movl %%a3, %1 \n\t" \
|
||||
"movl %%a2, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "d0", "d1", "d2", "d3", "d4", "a2", "a3" \
|
||||
);
|
||||
|
||||
#define MULADDC_HUIT \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t"
|
||||
|
||||
#endif /* MC68000 */
|
||||
|
||||
#if defined(__powerpc64__) || defined(__ppc64__)
|
||||
|
||||
#if defined(__MACH__) && defined(__APPLE__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld r3, %3 \n\t" \
|
||||
"ld r4, %4 \n\t" \
|
||||
"ld r5, %5 \n\t" \
|
||||
"ld r6, %6 \n\t" \
|
||||
"addi r3, r3, -8 \n\t" \
|
||||
"addi r4, r4, -8 \n\t" \
|
||||
"addic r5, r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldu r7, 8(r3) \n\t" \
|
||||
"mulld r8, r7, r6 \n\t" \
|
||||
"mulhdu r9, r7, r6 \n\t" \
|
||||
"adde r8, r8, r5 \n\t" \
|
||||
"ld r7, 8(r4) \n\t" \
|
||||
"addze r5, r9 \n\t" \
|
||||
"addc r8, r8, r7 \n\t" \
|
||||
"stdu r8, 8(r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze r5, r5 \n\t" \
|
||||
"addi r4, r4, 8 \n\t" \
|
||||
"addi r3, r3, 8 \n\t" \
|
||||
"std r5, %0 \n\t" \
|
||||
"std r4, %1 \n\t" \
|
||||
"std r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
|
||||
#else /* __MACH__ && __APPLE__ */
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld %%r3, %3 \n\t" \
|
||||
"ld %%r4, %4 \n\t" \
|
||||
"ld %%r5, %5 \n\t" \
|
||||
"ld %%r6, %6 \n\t" \
|
||||
"addi %%r3, %%r3, -8 \n\t" \
|
||||
"addi %%r4, %%r4, -8 \n\t" \
|
||||
"addic %%r5, %%r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldu %%r7, 8(%%r3) \n\t" \
|
||||
"mulld %%r8, %%r7, %%r6 \n\t" \
|
||||
"mulhdu %%r9, %%r7, %%r6 \n\t" \
|
||||
"adde %%r8, %%r8, %%r5 \n\t" \
|
||||
"ld %%r7, 8(%%r4) \n\t" \
|
||||
"addze %%r5, %%r9 \n\t" \
|
||||
"addc %%r8, %%r8, %%r7 \n\t" \
|
||||
"stdu %%r8, 8(%%r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze %%r5, %%r5 \n\t" \
|
||||
"addi %%r4, %%r4, 8 \n\t" \
|
||||
"addi %%r3, %%r3, 8 \n\t" \
|
||||
"std %%r5, %0 \n\t" \
|
||||
"std %%r4, %1 \n\t" \
|
||||
"std %%r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
#endif /* __MACH__ && __APPLE__ */
|
||||
|
||||
#elif defined(__powerpc__) || defined(__ppc__) /* end PPC64/begin PPC32 */
|
||||
|
||||
#if defined(__MACH__) && defined(__APPLE__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lwz r3, %3 \n\t" \
|
||||
"lwz r4, %4 \n\t" \
|
||||
"lwz r5, %5 \n\t" \
|
||||
"lwz r6, %6 \n\t" \
|
||||
"addi r3, r3, -4 \n\t" \
|
||||
"addi r4, r4, -4 \n\t" \
|
||||
"addic r5, r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lwzu r7, 4(r3) \n\t" \
|
||||
"mullw r8, r7, r6 \n\t" \
|
||||
"mulhwu r9, r7, r6 \n\t" \
|
||||
"adde r8, r8, r5 \n\t" \
|
||||
"lwz r7, 4(r4) \n\t" \
|
||||
"addze r5, r9 \n\t" \
|
||||
"addc r8, r8, r7 \n\t" \
|
||||
"stwu r8, 4(r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze r5, r5 \n\t" \
|
||||
"addi r4, r4, 4 \n\t" \
|
||||
"addi r3, r3, 4 \n\t" \
|
||||
"stw r5, %0 \n\t" \
|
||||
"stw r4, %1 \n\t" \
|
||||
"stw r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
#else /* __MACH__ && __APPLE__ */
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lwz %%r3, %3 \n\t" \
|
||||
"lwz %%r4, %4 \n\t" \
|
||||
"lwz %%r5, %5 \n\t" \
|
||||
"lwz %%r6, %6 \n\t" \
|
||||
"addi %%r3, %%r3, -4 \n\t" \
|
||||
"addi %%r4, %%r4, -4 \n\t" \
|
||||
"addic %%r5, %%r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lwzu %%r7, 4(%%r3) \n\t" \
|
||||
"mullw %%r8, %%r7, %%r6 \n\t" \
|
||||
"mulhwu %%r9, %%r7, %%r6 \n\t" \
|
||||
"adde %%r8, %%r8, %%r5 \n\t" \
|
||||
"lwz %%r7, 4(%%r4) \n\t" \
|
||||
"addze %%r5, %%r9 \n\t" \
|
||||
"addc %%r8, %%r8, %%r7 \n\t" \
|
||||
"stwu %%r8, 4(%%r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze %%r5, %%r5 \n\t" \
|
||||
"addi %%r4, %%r4, 4 \n\t" \
|
||||
"addi %%r3, %%r3, 4 \n\t" \
|
||||
"stw %%r5, %0 \n\t" \
|
||||
"stw %%r4, %1 \n\t" \
|
||||
"stw %%r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
#endif /* __MACH__ && __APPLE__ */
|
||||
|
||||
#endif /* PPC32 */
|
||||
|
||||
/*
|
||||
* The Sparc64 assembly is reported to be broken.
|
||||
* Disable it for now, until we're able to fix it.
|
||||
*/
|
||||
#if 0 && defined(__sparc__) && defined(__sparc64__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldx %3, %%o0 \n\t" \
|
||||
"ldx %4, %%o1 \n\t" \
|
||||
"ld %5, %%o2 \n\t" \
|
||||
"ld %6, %%o3 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ld [%%o0], %%o4 \n\t" \
|
||||
"inc 4, %%o0 \n\t" \
|
||||
"ld [%%o1], %%o5 \n\t" \
|
||||
"umul %%o3, %%o4, %%o4 \n\t" \
|
||||
"addcc %%o4, %%o2, %%o4 \n\t" \
|
||||
"rd %%y, %%g1 \n\t" \
|
||||
"addx %%g1, 0, %%g1 \n\t" \
|
||||
"addcc %%o4, %%o5, %%o4 \n\t" \
|
||||
"st %%o4, [%%o1] \n\t" \
|
||||
"addx %%g1, 0, %%o2 \n\t" \
|
||||
"inc 4, %%o1 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"st %%o2, %0 \n\t" \
|
||||
"stx %%o1, %1 \n\t" \
|
||||
"stx %%o0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "g1", "o0", "o1", "o2", "o3", "o4", \
|
||||
"o5" \
|
||||
);
|
||||
#endif /* SPARCv9 */
|
||||
|
||||
#if defined(__sparc__) && !defined(__sparc64__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld %3, %%o0 \n\t" \
|
||||
"ld %4, %%o1 \n\t" \
|
||||
"ld %5, %%o2 \n\t" \
|
||||
"ld %6, %%o3 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ld [%%o0], %%o4 \n\t" \
|
||||
"inc 4, %%o0 \n\t" \
|
||||
"ld [%%o1], %%o5 \n\t" \
|
||||
"umul %%o3, %%o4, %%o4 \n\t" \
|
||||
"addcc %%o4, %%o2, %%o4 \n\t" \
|
||||
"rd %%y, %%g1 \n\t" \
|
||||
"addx %%g1, 0, %%g1 \n\t" \
|
||||
"addcc %%o4, %%o5, %%o4 \n\t" \
|
||||
"st %%o4, [%%o1] \n\t" \
|
||||
"addx %%g1, 0, %%o2 \n\t" \
|
||||
"inc 4, %%o1 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"st %%o2, %0 \n\t" \
|
||||
"st %%o1, %1 \n\t" \
|
||||
"st %%o0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "g1", "o0", "o1", "o2", "o3", "o4", \
|
||||
"o5" \
|
||||
);
|
||||
|
||||
#endif /* SPARCv8 */
|
||||
|
||||
#if defined(__microblaze__) || defined(microblaze)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lwi r3, %3 \n\t" \
|
||||
"lwi r4, %4 \n\t" \
|
||||
"lwi r5, %5 \n\t" \
|
||||
"lwi r6, %6 \n\t" \
|
||||
"andi r7, r6, 0xffff \n\t" \
|
||||
"bsrli r6, r6, 16 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lhui r8, r3, 0 \n\t" \
|
||||
"addi r3, r3, 2 \n\t" \
|
||||
"lhui r9, r3, 0 \n\t" \
|
||||
"addi r3, r3, 2 \n\t" \
|
||||
"mul r10, r9, r6 \n\t" \
|
||||
"mul r11, r8, r7 \n\t" \
|
||||
"mul r12, r9, r7 \n\t" \
|
||||
"mul r13, r8, r6 \n\t" \
|
||||
"bsrli r8, r10, 16 \n\t" \
|
||||
"bsrli r9, r11, 16 \n\t" \
|
||||
"add r13, r13, r8 \n\t" \
|
||||
"add r13, r13, r9 \n\t" \
|
||||
"bslli r10, r10, 16 \n\t" \
|
||||
"bslli r11, r11, 16 \n\t" \
|
||||
"add r12, r12, r10 \n\t" \
|
||||
"addc r13, r13, r0 \n\t" \
|
||||
"add r12, r12, r11 \n\t" \
|
||||
"addc r13, r13, r0 \n\t" \
|
||||
"lwi r10, r4, 0 \n\t" \
|
||||
"add r12, r12, r10 \n\t" \
|
||||
"addc r13, r13, r0 \n\t" \
|
||||
"add r12, r12, r5 \n\t" \
|
||||
"addc r5, r13, r0 \n\t" \
|
||||
"swi r12, r4, 0 \n\t" \
|
||||
"addi r4, r4, 4 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"swi r5, %0 \n\t" \
|
||||
"swi r4, %1 \n\t" \
|
||||
"swi r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4" "r5", "r6", "r7", "r8", \
|
||||
"r9", "r10", "r11", "r12", "r13" \
|
||||
);
|
||||
|
||||
#endif /* MicroBlaze */
|
||||
|
||||
#if defined(__tricore__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld.a %%a2, %3 \n\t" \
|
||||
"ld.a %%a3, %4 \n\t" \
|
||||
"ld.w %%d4, %5 \n\t" \
|
||||
"ld.w %%d1, %6 \n\t" \
|
||||
"xor %%d5, %%d5 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ld.w %%d0, [%%a2+] \n\t" \
|
||||
"madd.u %%e2, %%e4, %%d0, %%d1 \n\t" \
|
||||
"ld.w %%d0, [%%a3] \n\t" \
|
||||
"addx %%d2, %%d2, %%d0 \n\t" \
|
||||
"addc %%d3, %%d3, 0 \n\t" \
|
||||
"mov %%d4, %%d3 \n\t" \
|
||||
"st.w [%%a3+], %%d2 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"st.w %0, %%d4 \n\t" \
|
||||
"st.a %1, %%a3 \n\t" \
|
||||
"st.a %2, %%a2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "d0", "d1", "e2", "d4", "a2", "a3" \
|
||||
);
|
||||
|
||||
#endif /* TriCore */
|
||||
|
||||
#if defined(__arm__)
|
||||
|
||||
#if defined(__thumb__) && !defined(__thumb2__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldr r0, %3 \n\t" \
|
||||
"ldr r1, %4 \n\t" \
|
||||
"ldr r2, %5 \n\t" \
|
||||
"ldr r3, %6 \n\t" \
|
||||
"lsr r7, r3, #16 \n\t" \
|
||||
"mov r9, r7 \n\t" \
|
||||
"lsl r7, r3, #16 \n\t" \
|
||||
"lsr r7, r7, #16 \n\t" \
|
||||
"mov r8, r7 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldmia r0!, {r6} \n\t" \
|
||||
"lsr r7, r6, #16 \n\t" \
|
||||
"lsl r6, r6, #16 \n\t" \
|
||||
"lsr r6, r6, #16 \n\t" \
|
||||
"mov r4, r8 \n\t" \
|
||||
"mul r4, r6 \n\t" \
|
||||
"mov r3, r9 \n\t" \
|
||||
"mul r6, r3 \n\t" \
|
||||
"mov r5, r9 \n\t" \
|
||||
"mul r5, r7 \n\t" \
|
||||
"mov r3, r8 \n\t" \
|
||||
"mul r7, r3 \n\t" \
|
||||
"lsr r3, r6, #16 \n\t" \
|
||||
"add r5, r5, r3 \n\t" \
|
||||
"lsr r3, r7, #16 \n\t" \
|
||||
"add r5, r5, r3 \n\t" \
|
||||
"add r4, r4, r2 \n\t" \
|
||||
"mov r2, #0 \n\t" \
|
||||
"adc r5, r2 \n\t" \
|
||||
"lsl r3, r6, #16 \n\t" \
|
||||
"add r4, r4, r3 \n\t" \
|
||||
"adc r5, r2 \n\t" \
|
||||
"lsl r3, r7, #16 \n\t" \
|
||||
"add r4, r4, r3 \n\t" \
|
||||
"adc r5, r2 \n\t" \
|
||||
"ldr r3, [r1] \n\t" \
|
||||
"add r4, r4, r3 \n\t" \
|
||||
"adc r2, r5 \n\t" \
|
||||
"stmia r1!, {r4} \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"str r2, %0 \n\t" \
|
||||
"str r1, %1 \n\t" \
|
||||
"str r0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r0", "r1", "r2", "r3", "r4", "r5", \
|
||||
"r6", "r7", "r8", "r9", "cc" \
|
||||
);
|
||||
|
||||
#else
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldr r0, %3 \n\t" \
|
||||
"ldr r1, %4 \n\t" \
|
||||
"ldr r2, %5 \n\t" \
|
||||
"ldr r3, %6 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldr r4, [r0], #4 \n\t" \
|
||||
"mov r5, #0 \n\t" \
|
||||
"ldr r6, [r1] \n\t" \
|
||||
"umlal r2, r5, r3, r4 \n\t" \
|
||||
"adds r7, r6, r2 \n\t" \
|
||||
"adc r2, r5, #0 \n\t" \
|
||||
"str r7, [r1], #4 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"str r2, %0 \n\t" \
|
||||
"str r1, %1 \n\t" \
|
||||
"str r0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r0", "r1", "r2", "r3", "r4", "r5", \
|
||||
"r6", "r7", "cc" \
|
||||
);
|
||||
|
||||
#endif /* Thumb */
|
||||
|
||||
#endif /* ARMv3 */
|
||||
|
||||
#if defined(__alpha__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldq $1, %3 \n\t" \
|
||||
"ldq $2, %4 \n\t" \
|
||||
"ldq $3, %5 \n\t" \
|
||||
"ldq $4, %6 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldq $6, 0($1) \n\t" \
|
||||
"addq $1, 8, $1 \n\t" \
|
||||
"mulq $6, $4, $7 \n\t" \
|
||||
"umulh $6, $4, $6 \n\t" \
|
||||
"addq $7, $3, $7 \n\t" \
|
||||
"cmpult $7, $3, $3 \n\t" \
|
||||
"ldq $5, 0($2) \n\t" \
|
||||
"addq $7, $5, $7 \n\t" \
|
||||
"cmpult $7, $5, $5 \n\t" \
|
||||
"stq $7, 0($2) \n\t" \
|
||||
"addq $2, 8, $2 \n\t" \
|
||||
"addq $6, $3, $3 \n\t" \
|
||||
"addq $5, $3, $3 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"stq $3, %0 \n\t" \
|
||||
"stq $2, %1 \n\t" \
|
||||
"stq $1, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "$1", "$2", "$3", "$4", "$5", "$6", "$7" \
|
||||
);
|
||||
#endif /* Alpha */
|
||||
|
||||
#if defined(__mips__) && !defined(__mips64)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lw $10, %3 \n\t" \
|
||||
"lw $11, %4 \n\t" \
|
||||
"lw $12, %5 \n\t" \
|
||||
"lw $13, %6 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lw $14, 0($10) \n\t" \
|
||||
"multu $13, $14 \n\t" \
|
||||
"addi $10, $10, 4 \n\t" \
|
||||
"mflo $14 \n\t" \
|
||||
"mfhi $9 \n\t" \
|
||||
"addu $14, $12, $14 \n\t" \
|
||||
"lw $15, 0($11) \n\t" \
|
||||
"sltu $12, $14, $12 \n\t" \
|
||||
"addu $15, $14, $15 \n\t" \
|
||||
"sltu $14, $15, $14 \n\t" \
|
||||
"addu $12, $12, $9 \n\t" \
|
||||
"sw $15, 0($11) \n\t" \
|
||||
"addu $12, $12, $14 \n\t" \
|
||||
"addi $11, $11, 4 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"sw $12, %0 \n\t" \
|
||||
"sw $11, %1 \n\t" \
|
||||
"sw $10, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "$9", "$10", "$11", "$12", "$13", "$14", "$15" \
|
||||
);
|
||||
|
||||
#endif /* MIPS */
|
||||
#endif /* GNUC */
|
||||
|
||||
#if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
__asm mov esi, s \
|
||||
__asm mov edi, d \
|
||||
__asm mov ecx, c \
|
||||
__asm mov ebx, b
|
||||
|
||||
#define MULADDC_CORE \
|
||||
__asm lodsd \
|
||||
__asm mul ebx \
|
||||
__asm add eax, ecx \
|
||||
__asm adc edx, 0 \
|
||||
__asm add eax, [edi] \
|
||||
__asm adc edx, 0 \
|
||||
__asm mov ecx, edx \
|
||||
__asm stosd
|
||||
|
||||
#if defined(POLARSSL_HAVE_SSE2)
|
||||
|
||||
#define EMIT __asm _emit
|
||||
|
||||
#define MULADDC_HUIT \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0xC9 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0xC3 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x1F \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x16 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x04 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x08 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x7E EMIT 0x0C \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xF8 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x5F EMIT 0x04 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xDC \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x08 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xEE \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x67 EMIT 0x0C \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xFC \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x0F \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x56 EMIT 0x10 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x14 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x18 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x04 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x5E EMIT 0x1C \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xD8 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCD \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x10 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xD5 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x08 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCF \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x14 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xE5 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x0C \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x18 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xF5 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x10 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCC \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x1C \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xDD \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x14 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCE \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x18 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x1C \
|
||||
EMIT 0x83 EMIT 0xC7 EMIT 0x20 \
|
||||
EMIT 0x83 EMIT 0xC6 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0xC9
|
||||
|
||||
#define MULADDC_STOP \
|
||||
EMIT 0x0F EMIT 0x77 \
|
||||
__asm mov c, ecx \
|
||||
__asm mov d, edi \
|
||||
__asm mov s, esi \
|
||||
|
||||
#else
|
||||
|
||||
#define MULADDC_STOP \
|
||||
__asm mov c, ecx \
|
||||
__asm mov d, edi \
|
||||
__asm mov s, esi \
|
||||
|
||||
#endif /* SSE2 */
|
||||
#endif /* MSVC */
|
||||
|
||||
#endif /* POLARSSL_HAVE_ASM */
|
||||
|
||||
#if !defined(MULADDC_CORE)
|
||||
#if defined(POLARSSL_HAVE_UDBL)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
{ \
|
||||
t_udbl r; \
|
||||
t_uint r0, r1;
|
||||
|
||||
#define MULADDC_CORE \
|
||||
r = *(s++) * (t_udbl) b; \
|
||||
r0 = (t_uint) r; \
|
||||
r1 = (t_uint)( r >> biL ); \
|
||||
r0 += c; r1 += (r0 < c); \
|
||||
r0 += *d; r1 += (r0 < *d); \
|
||||
c = r1; *(d++) = r0;
|
||||
|
||||
#define MULADDC_STOP \
|
||||
}
|
||||
|
||||
#else
|
||||
#define MULADDC_INIT \
|
||||
{ \
|
||||
t_uint s0, s1, b0, b1; \
|
||||
t_uint r0, r1, rx, ry; \
|
||||
b0 = ( b << biH ) >> biH; \
|
||||
b1 = ( b >> biH );
|
||||
|
||||
#define MULADDC_CORE \
|
||||
s0 = ( *s << biH ) >> biH; \
|
||||
s1 = ( *s >> biH ); s++; \
|
||||
rx = s0 * b1; r0 = s0 * b0; \
|
||||
ry = s1 * b0; r1 = s1 * b1; \
|
||||
r1 += ( rx >> biH ); \
|
||||
r1 += ( ry >> biH ); \
|
||||
rx <<= biH; ry <<= biH; \
|
||||
r0 += rx; r1 += (r0 < rx); \
|
||||
r0 += ry; r1 += (r0 < ry); \
|
||||
r0 += c; r1 += (r0 < c); \
|
||||
r0 += *d; r1 += (r0 < *d); \
|
||||
c = r1; *(d++) = r0;
|
||||
|
||||
#define MULADDC_STOP \
|
||||
}
|
||||
|
||||
#endif /* C (generic) */
|
||||
#endif /* C (longlong) */
|
||||
|
||||
#endif /* bn_mul.h */
|
||||
@@ -0,0 +1,495 @@
|
||||
/**
|
||||
* \file check_config.h
|
||||
*
|
||||
* \brief Consistency checks for configuration options
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/*
|
||||
* It is recommended to include this file from your config.h
|
||||
* in order to catch dependency issues early.
|
||||
*/
|
||||
|
||||
#ifndef POLARSSL_CHECK_CONFIG_H
|
||||
#define POLARSSL_CHECK_CONFIG_H
|
||||
|
||||
#if defined(POLARSSL_DEPRECATED_WARNING) && \
|
||||
!defined(__GNUC__) && !defined(__clang__)
|
||||
#error "POLARSSL_DEPRECATED_WARNING only works with GCC and Clang"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_NET_C) && !defined(POLARSSL_HAVE_IPV6)
|
||||
#if defined(POLARSSL_DEPRECATED_WARNING)
|
||||
#warning "Using POLARSSL_NET_C without POLARSSL_HAVE_IPV6 is deprecated"
|
||||
#endif
|
||||
#if defined(POLARSSL_DEPRECATED_REMOVED)
|
||||
#define POLARSSL_HAVE_IPV6
|
||||
#endif
|
||||
#endif /* POLARSSL_NET_C && !POLARSSL_HAVE_IPV6 */
|
||||
|
||||
#if defined(POLARSSL_AESNI_C) && !defined(POLARSSL_HAVE_ASM)
|
||||
#error "POLARSSL_AESNI_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_CERTS_C) && !defined(POLARSSL_PEM_PARSE_C)
|
||||
#error "POLARSSL_CERTS_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_CTR_DRBG_C) && !defined(POLARSSL_AES_C)
|
||||
#error "POLARSSL_CTR_DRBG_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_DHM_C) && !defined(POLARSSL_BIGNUM_C)
|
||||
#error "POLARSSL_DHM_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECDH_C) && !defined(POLARSSL_ECP_C)
|
||||
#error "POLARSSL_ECDH_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECDSA_C) && \
|
||||
( !defined(POLARSSL_ECP_C) || \
|
||||
!defined(POLARSSL_ASN1_PARSE_C) || \
|
||||
!defined(POLARSSL_ASN1_WRITE_C) )
|
||||
#error "POLARSSL_ECDSA_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECDSA_DETERMINISTIC) && !defined(POLARSSL_HMAC_DRBG_C)
|
||||
#error "POLARSSL_ECDSA_DETERMINISTIC defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECP_C) && ( !defined(POLARSSL_BIGNUM_C) || ( \
|
||||
!defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_BP256R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_BP384R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_BP512R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP192K1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP224K1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP256K1_ENABLED) ) )
|
||||
#error "POLARSSL_ECP_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ENTROPY_C) && (!defined(POLARSSL_SHA512_C) && \
|
||||
!defined(POLARSSL_SHA256_C))
|
||||
#error "POLARSSL_ENTROPY_C defined, but not all prerequisites"
|
||||
#endif
|
||||
#if defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_SHA512_C) && \
|
||||
defined(CTR_DRBG_ENTROPY_LEN) && (CTR_DRBG_ENTROPY_LEN > 64)
|
||||
#error "CTR_DRBG_ENTROPY_LEN value too high"
|
||||
#endif
|
||||
#if defined(POLARSSL_ENTROPY_C) && \
|
||||
( !defined(POLARSSL_SHA512_C) || defined(POLARSSL_ENTROPY_FORCE_SHA256) ) \
|
||||
&& defined(CTR_DRBG_ENTROPY_LEN) && (CTR_DRBG_ENTROPY_LEN > 32)
|
||||
#error "CTR_DRBG_ENTROPY_LEN value too high"
|
||||
#endif
|
||||
#if defined(POLARSSL_ENTROPY_C) && \
|
||||
defined(POLARSSL_ENTROPY_FORCE_SHA256) && !defined(POLARSSL_SHA256_C)
|
||||
#error "POLARSSL_ENTROPY_FORCE_SHA256 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_GCM_C) && ( \
|
||||
!defined(POLARSSL_AES_C) && !defined(POLARSSL_CAMELLIA_C) )
|
||||
#error "POLARSSL_GCM_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_HAVEGE_C) && !defined(POLARSSL_TIMING_C)
|
||||
#error "POLARSSL_HAVEGE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_HMAC_DRBG) && !defined(POLARSSL_MD_C)
|
||||
#error "POLARSSL_HMAC_DRBG_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) && \
|
||||
( !defined(POLARSSL_ECDH_C) || !defined(POLARSSL_X509_CRT_PARSE_C) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
|
||||
( !defined(POLARSSL_ECDH_C) || !defined(POLARSSL_X509_CRT_PARSE_C) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) && !defined(POLARSSL_DHM_C)
|
||||
#error "POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) && \
|
||||
!defined(POLARSSL_ECDH_C)
|
||||
#error "POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
|
||||
( !defined(POLARSSL_DHM_C) || !defined(POLARSSL_RSA_C) || \
|
||||
!defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_PKCS1_V15) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
|
||||
( !defined(POLARSSL_ECDH_C) || !defined(POLARSSL_RSA_C) || \
|
||||
!defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_PKCS1_V15) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
|
||||
( !defined(POLARSSL_ECDH_C) || !defined(POLARSSL_ECDSA_C) || \
|
||||
!defined(POLARSSL_X509_CRT_PARSE_C) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
|
||||
( !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
|
||||
!defined(POLARSSL_PKCS1_V15) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
|
||||
( !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
|
||||
!defined(POLARSSL_PKCS1_V15) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_MEMORY_C) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_MEMORY_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && \
|
||||
( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) )
|
||||
#error "POLARSSL_MEMORY_BUFFER_ALLOC_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PADLOCK_C) && !defined(POLARSSL_HAVE_ASM)
|
||||
#error "POLARSSL_PADLOCK_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PBKDF2_C) && !defined(POLARSSL_MD_C)
|
||||
#error "POLARSSL_PBKDF2_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PEM_PARSE_C) && !defined(POLARSSL_BASE64_C)
|
||||
#error "POLARSSL_PEM_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PEM_WRITE_C) && !defined(POLARSSL_BASE64_C)
|
||||
#error "POLARSSL_PEM_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PK_C) && \
|
||||
( !defined(POLARSSL_RSA_C) && !defined(POLARSSL_ECP_C) )
|
||||
#error "POLARSSL_PK_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PK_PARSE_C) && !defined(POLARSSL_PK_C)
|
||||
#error "POLARSSL_PK_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PK_WRITE_C) && !defined(POLARSSL_PK_C)
|
||||
#error "POLARSSL_PK_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PKCS11_C) && !defined(POLARSSL_PK_C)
|
||||
#error "POLARSSL_PKCS11_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_ALT) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_EXIT_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_MACRO) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_EXIT_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_MACRO) &&\
|
||||
( defined(POLARSSL_PLATFORM_STD_EXIT) ||\
|
||||
defined(POLARSSL_PLATFORM_EXIT_ALT) )
|
||||
#error "POLARSSL_PLATFORM_EXIT_MACRO and POLARSSL_PLATFORM_STD_EXIT/POLARSSL_PLATFORM_EXIT_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_FPRINTF_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_FPRINTF_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) &&\
|
||||
( defined(POLARSSL_PLATFORM_STD_FPRINTF) ||\
|
||||
defined(POLARSSL_PLATFORM_FPRINTF_ALT) )
|
||||
#error "POLARSSL_PLATFORM_FPRINTF_MACRO and POLARSSL_PLATFORM_STD_FPRINTF/POLARSSL_PLATFORM_FPRINTF_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FREE_MACRO) &&\
|
||||
( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) )
|
||||
#error "POLARSSL_PLATFORM_FREE_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FREE_MACRO) &&\
|
||||
defined(POLARSSL_PLATFORM_STD_FREE)
|
||||
#error "POLARSSL_PLATFORM_FREE_MACRO and POLARSSL_PLATFORM_STD_FREE cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FREE_MACRO) && !defined(POLARSSL_PLATFORM_MALLOC_MACRO)
|
||||
#error "POLARSSL_PLATFORM_MALLOC_MACRO must be defined if POLARSSL_PLATFORM_FREE_MACRO is"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) &&\
|
||||
( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) )
|
||||
#error "POLARSSL_PLATFORM_MALLOC_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) &&\
|
||||
defined(POLARSSL_PLATFORM_STD_MALLOC)
|
||||
#error "POLARSSL_PLATFORM_MALLOC_MACRO and POLARSSL_PLATFORM_STD_MALLOC cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) && !defined(POLARSSL_PLATFORM_FREE_MACRO)
|
||||
#error "POLARSSL_PLATFORM_FREE_MACRO must be defined if POLARSSL_PLATFORM_MALLOC_MACRO is"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_MEMORY) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_MEMORY defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_ALT) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_PRINTF_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_PRINTF_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) &&\
|
||||
( defined(POLARSSL_PLATFORM_STD_PRINTF) ||\
|
||||
defined(POLARSSL_PLATFORM_PRINTF_ALT) )
|
||||
#error "POLARSSL_PLATFORM_PRINTF_MACRO and POLARSSL_PLATFORM_STD_PRINTF/POLARSSL_PLATFORM_PRINTF_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_SNPRINTF_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) && ( defined(_WIN32)\
|
||||
&& !defined(EFIX64) && !defined(EFI32) )
|
||||
#error "POLARSSL_PLATFORM_SNPRINTF_ALT defined but not available on Windows"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_SNPRINTF_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) &&\
|
||||
( defined(POLARSSL_PLATFORM_STD_SNPRINTF) ||\
|
||||
defined(POLARSSL_PLATFORM_SNPRINTF_ALT) )
|
||||
#error "POLARSSL_PLATFORM_SNPRINTF_MACRO and POLARSSL_PLATFORM_STD_SNPRINTF/POLARSSL_PLATFORM_SNPRINTF_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_MEM_HDR) &&\
|
||||
!defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS)
|
||||
#error "POLARSSL_PLATFORM_STD_MEM_HDR defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_MALLOC) && !defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#error "POLARSSL_PLATFORM_STD_MALLOC defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_MALLOC) && !defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#error "POLARSSL_PLATFORM_STD_MALLOC defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_FREE) && !defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#error "POLARSSL_PLATFORM_STD_FREE defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_EXIT) &&\
|
||||
!defined(POLARSSL_PLATFORM_EXIT_ALT)
|
||||
#error "POLARSSL_PLATFORM_STD_EXIT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_FPRINTF) &&\
|
||||
!defined(POLARSSL_PLATFORM_FPRINTF_ALT)
|
||||
#error "POLARSSL_PLATFORM_STD_FPRINTF defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_PRINTF) &&\
|
||||
!defined(POLARSSL_PLATFORM_PRINTF_ALT)
|
||||
#error "POLARSSL_PLATFORM_STD_PRINTF defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_SNPRINTF) &&\
|
||||
!defined(POLARSSL_PLATFORM_SNPRINTF_ALT)
|
||||
#error "POLARSSL_PLATFORM_STD_SNPRINTF defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_RSA_C) && ( !defined(POLARSSL_BIGNUM_C) || \
|
||||
!defined(POLARSSL_OID_C) )
|
||||
#error "POLARSSL_RSA_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT) && \
|
||||
( !defined(POLARSSL_RSA_C) || !defined(POLARSSL_PKCS1_V21) )
|
||||
#error "POLARSSL_X509_RSASSA_PSS_SUPPORT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_PROTO_SSL3) && ( !defined(POLARSSL_MD5_C) || \
|
||||
!defined(POLARSSL_SHA1_C) )
|
||||
#error "POLARSSL_SSL_PROTO_SSL3 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_PROTO_TLS1) && ( !defined(POLARSSL_MD5_C) || \
|
||||
!defined(POLARSSL_SHA1_C) )
|
||||
#error "POLARSSL_SSL_PROTO_TLS1 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_PROTO_TLS1_1) && ( !defined(POLARSSL_MD5_C) || \
|
||||
!defined(POLARSSL_SHA1_C) )
|
||||
#error "POLARSSL_SSL_PROTO_TLS1_1 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_PROTO_TLS1_2) && ( !defined(POLARSSL_SHA1_C) && \
|
||||
!defined(POLARSSL_SHA256_C) && !defined(POLARSSL_SHA512_C) )
|
||||
#error "POLARSSL_SSL_PROTO_TLS1_2 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_CLI_C) && !defined(POLARSSL_SSL_TLS_C)
|
||||
#error "POLARSSL_SSL_CLI_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_TLS_C) && ( !defined(POLARSSL_CIPHER_C) || \
|
||||
!defined(POLARSSL_MD_C) )
|
||||
#error "POLARSSL_SSL_TLS_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_SRV_C) && !defined(POLARSSL_SSL_TLS_C)
|
||||
#error "POLARSSL_SSL_SRV_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_TLS_C) && (!defined(POLARSSL_SSL_PROTO_SSL3) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1) && !defined(POLARSSL_SSL_PROTO_TLS1_1) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_2))
|
||||
#error "POLARSSL_SSL_TLS_C defined, but no protocols are active"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_TLS_C) && (defined(POLARSSL_SSL_PROTO_SSL3) && \
|
||||
defined(POLARSSL_SSL_PROTO_TLS1_1) && !defined(POLARSSL_SSL_PROTO_TLS1))
|
||||
#error "Illegal protocol selection"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_TLS_C) && (defined(POLARSSL_SSL_PROTO_TLS1) && \
|
||||
defined(POLARSSL_SSL_PROTO_TLS1_2) && !defined(POLARSSL_SSL_PROTO_TLS1_1))
|
||||
#error "Illegal protocol selection"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_TLS_C) && (defined(POLARSSL_SSL_PROTO_SSL3) && \
|
||||
defined(POLARSSL_SSL_PROTO_TLS1_2) && (!defined(POLARSSL_SSL_PROTO_TLS1) || \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_1)))
|
||||
#error "Illegal protocol selection"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_1) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_2)
|
||||
#error "POLARSSL_SSL_ENCRYPT_THEN_MAC defined, but not all prerequsites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_1) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_2)
|
||||
#error "POLARSSL_SSL_EXTENDED_MASTER_SECRET defined, but not all prerequsites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_SESSION_TICKETS) && defined(POLARSSL_SSL_TLS_C) && \
|
||||
( !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C) || \
|
||||
!defined(POLARSSL_CIPHER_MODE_CBC) )
|
||||
#error "POLARSSL_SSL_SESSION_TICKETS_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) && \
|
||||
!defined(POLARSSL_SSL_PROTO_SSL3) && !defined(POLARSSL_SSL_PROTO_TLS1)
|
||||
#error "POLARSSL_SSL_CBC_RECORD_SPLITTING defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && \
|
||||
!defined(POLARSSL_X509_CRT_PARSE_C)
|
||||
#error "POLARSSL_SSL_SERVER_NAME_INDICATION defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_THREADING_PTHREAD)
|
||||
#if !defined(POLARSSL_THREADING_C) || defined(POLARSSL_THREADING_IMPL)
|
||||
#error "POLARSSL_THREADING_PTHREAD defined, but not all prerequisites"
|
||||
#endif
|
||||
#define POLARSSL_THREADING_IMPL
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_THREADING_ALT)
|
||||
#if !defined(POLARSSL_THREADING_C) || defined(POLARSSL_THREADING_IMPL)
|
||||
#error "POLARSSL_THREADING_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
#define POLARSSL_THREADING_IMPL
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_THREADING_C) && !defined(POLARSSL_THREADING_IMPL)
|
||||
#error "POLARSSL_THREADING_C defined, single threading implementation required"
|
||||
#endif
|
||||
#undef POLARSSL_THREADING_IMPL
|
||||
|
||||
#if defined(POLARSSL_VERSION_FEATURES) && !defined(POLARSSL_VERSION_C)
|
||||
#error "POLARSSL_VERSION_FEATURES defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_USE_C) && ( !defined(POLARSSL_BIGNUM_C) || \
|
||||
!defined(POLARSSL_OID_C) || !defined(POLARSSL_ASN1_PARSE_C) || \
|
||||
!defined(POLARSSL_PK_PARSE_C) )
|
||||
#error "POLARSSL_X509_USE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CREATE_C) && ( !defined(POLARSSL_BIGNUM_C) || \
|
||||
!defined(POLARSSL_OID_C) || !defined(POLARSSL_ASN1_WRITE_C) || \
|
||||
!defined(POLARSSL_PK_WRITE_C) )
|
||||
#error "POLARSSL_X509_CREATE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CRT_PARSE_C) && ( !defined(POLARSSL_X509_USE_C) )
|
||||
#error "POLARSSL_X509_CRT_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CRL_PARSE_C) && ( !defined(POLARSSL_X509_USE_C) )
|
||||
#error "POLARSSL_X509_CRL_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CSR_PARSE_C) && ( !defined(POLARSSL_X509_USE_C) )
|
||||
#error "POLARSSL_X509_CSR_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CRT_WRITE_C) && ( !defined(POLARSSL_X509_CREATE_C) )
|
||||
#error "POLARSSL_X509_CRT_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CSR_WRITE_C) && ( !defined(POLARSSL_X509_CREATE_C) )
|
||||
#error "POLARSSL_X509_CSR_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#endif /* POLARSSL_CHECK_CONFIG_H */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,734 @@
|
||||
/*
|
||||
* Buffer-based memory allocator
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
|
||||
#include "memory_buffer_alloc.h"
|
||||
|
||||
/* No need for the header guard as POLARSSL_MEMORY_BUFFER_ALLOC_C
|
||||
is dependent upon POLARSSL_PLATFORM_C */
|
||||
#include "platform.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#include <execinfo.h>
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
#include "polarssl/threading.h"
|
||||
#endif
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void polarssl_zeroize( void *v, size_t n ) {
|
||||
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
||||
}
|
||||
|
||||
#define MAGIC1 0xFF00AA55
|
||||
#define MAGIC2 0xEE119966
|
||||
#define MAX_BT 20
|
||||
|
||||
typedef struct _memory_header memory_header;
|
||||
struct _memory_header
|
||||
{
|
||||
size_t magic1;
|
||||
size_t size;
|
||||
size_t alloc;
|
||||
memory_header *prev;
|
||||
memory_header *next;
|
||||
memory_header *prev_free;
|
||||
memory_header *next_free;
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
char **trace;
|
||||
size_t trace_count;
|
||||
#endif
|
||||
size_t magic2;
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
memory_header *first;
|
||||
memory_header *first_free;
|
||||
int verify;
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
size_t malloc_count;
|
||||
size_t free_count;
|
||||
size_t total_used;
|
||||
size_t maximum_used;
|
||||
size_t header_count;
|
||||
size_t maximum_header_count;
|
||||
#endif
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
threading_mutex_t mutex;
|
||||
#endif
|
||||
}
|
||||
buffer_alloc_ctx;
|
||||
|
||||
static buffer_alloc_ctx heap;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
static void debug_header( memory_header *hdr )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
size_t i;
|
||||
#endif
|
||||
|
||||
polarssl_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
|
||||
"ALLOC(%zu), SIZE(%10zu)\n",
|
||||
(size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
|
||||
hdr->alloc, hdr->size );
|
||||
polarssl_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
|
||||
(size_t) hdr->prev_free, (size_t) hdr->next_free );
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
polarssl_fprintf( stderr, "TRACE: \n" );
|
||||
for( i = 0; i < hdr->trace_count; i++ )
|
||||
polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
|
||||
polarssl_fprintf( stderr, "\n" );
|
||||
#endif
|
||||
}
|
||||
|
||||
static void debug_chain()
|
||||
{
|
||||
memory_header *cur = heap.first;
|
||||
|
||||
polarssl_fprintf( stderr, "\nBlock list\n" );
|
||||
while( cur != NULL )
|
||||
{
|
||||
debug_header( cur );
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
polarssl_fprintf( stderr, "Free list\n" );
|
||||
cur = heap.first_free;
|
||||
|
||||
while( cur != NULL )
|
||||
{
|
||||
debug_header( cur );
|
||||
cur = cur->next_free;
|
||||
}
|
||||
}
|
||||
#endif /* POLARSSL_MEMORY_DEBUG */
|
||||
|
||||
static int verify_header( memory_header *hdr )
|
||||
{
|
||||
if( hdr->magic1 != MAGIC1 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->magic2 != MAGIC2 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->alloc > 1 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->prev != NULL && hdr->prev == hdr->next )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: prev == next\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int verify_chain()
|
||||
{
|
||||
memory_header *prv = heap.first, *cur = heap.first->next;
|
||||
|
||||
if( verify_header( heap.first ) != 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification of first header "
|
||||
"failed\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( heap.first->prev != NULL )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification failed: "
|
||||
"first->prev != NULL\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
while( cur != NULL )
|
||||
{
|
||||
if( verify_header( cur ) != 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification of header "
|
||||
"failed\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( cur->prev != prv )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification failed: "
|
||||
"cur->prev != prv\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
prv = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static void *buffer_alloc_malloc( size_t len )
|
||||
{
|
||||
memory_header *new, *cur = heap.first_free;
|
||||
unsigned char *p;
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
void *trace_buffer[MAX_BT];
|
||||
size_t trace_cnt;
|
||||
#endif
|
||||
|
||||
if( heap.buf == NULL || heap.first == NULL )
|
||||
return( NULL );
|
||||
|
||||
if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
|
||||
{
|
||||
len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
}
|
||||
|
||||
// Find block that fits
|
||||
//
|
||||
while( cur != NULL )
|
||||
{
|
||||
if( cur->size >= len )
|
||||
break;
|
||||
|
||||
cur = cur->next_free;
|
||||
}
|
||||
|
||||
if( cur == NULL )
|
||||
return( NULL );
|
||||
|
||||
if( cur->alloc != 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
|
||||
"data\n" );
|
||||
#endif
|
||||
polarssl_exit( 1 );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.malloc_count++;
|
||||
#endif
|
||||
|
||||
// Found location, split block if > memory_header + 4 room left
|
||||
//
|
||||
if( cur->size - len < sizeof(memory_header) +
|
||||
POLARSSL_MEMORY_ALIGN_MULTIPLE )
|
||||
{
|
||||
cur->alloc = 1;
|
||||
|
||||
// Remove from free_list
|
||||
//
|
||||
if( cur->prev_free != NULL )
|
||||
cur->prev_free->next_free = cur->next_free;
|
||||
else
|
||||
heap.first_free = cur->next_free;
|
||||
|
||||
if( cur->next_free != NULL )
|
||||
cur->next_free->prev_free = cur->prev_free;
|
||||
|
||||
cur->prev_free = NULL;
|
||||
cur->next_free = NULL;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.total_used += cur->size;
|
||||
if( heap.total_used > heap.maximum_used )
|
||||
heap.maximum_used = heap.total_used;
|
||||
#endif
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
trace_cnt = backtrace( trace_buffer, MAX_BT );
|
||||
cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
|
||||
cur->trace_count = trace_cnt;
|
||||
#endif
|
||||
|
||||
if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
|
||||
polarssl_exit( 1 );
|
||||
|
||||
return( ( (unsigned char *) cur ) + sizeof(memory_header) );
|
||||
}
|
||||
|
||||
p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
|
||||
new = (memory_header *) p;
|
||||
|
||||
new->size = cur->size - len - sizeof(memory_header);
|
||||
new->alloc = 0;
|
||||
new->prev = cur;
|
||||
new->next = cur->next;
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
new->trace = NULL;
|
||||
new->trace_count = 0;
|
||||
#endif
|
||||
new->magic1 = MAGIC1;
|
||||
new->magic2 = MAGIC2;
|
||||
|
||||
if( new->next != NULL )
|
||||
new->next->prev = new;
|
||||
|
||||
// Replace cur with new in free_list
|
||||
//
|
||||
new->prev_free = cur->prev_free;
|
||||
new->next_free = cur->next_free;
|
||||
if( new->prev_free != NULL )
|
||||
new->prev_free->next_free = new;
|
||||
else
|
||||
heap.first_free = new;
|
||||
|
||||
if( new->next_free != NULL )
|
||||
new->next_free->prev_free = new;
|
||||
|
||||
cur->alloc = 1;
|
||||
cur->size = len;
|
||||
cur->next = new;
|
||||
cur->prev_free = NULL;
|
||||
cur->next_free = NULL;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.header_count++;
|
||||
if( heap.header_count > heap.maximum_header_count )
|
||||
heap.maximum_header_count = heap.header_count;
|
||||
heap.total_used += cur->size;
|
||||
if( heap.total_used > heap.maximum_used )
|
||||
heap.maximum_used = heap.total_used;
|
||||
#endif
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
trace_cnt = backtrace( trace_buffer, MAX_BT );
|
||||
cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
|
||||
cur->trace_count = trace_cnt;
|
||||
#endif
|
||||
|
||||
if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
|
||||
polarssl_exit( 1 );
|
||||
|
||||
return( ( (unsigned char *) cur ) + sizeof(memory_header) );
|
||||
}
|
||||
|
||||
static void buffer_alloc_free( void *ptr )
|
||||
{
|
||||
memory_header *hdr, *old = NULL;
|
||||
unsigned char *p = (unsigned char *) ptr;
|
||||
|
||||
if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
|
||||
return;
|
||||
|
||||
if( p < heap.buf || p > heap.buf + heap.len )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
|
||||
"space\n" );
|
||||
#endif
|
||||
polarssl_exit( 1 );
|
||||
}
|
||||
|
||||
p -= sizeof(memory_header);
|
||||
hdr = (memory_header *) p;
|
||||
|
||||
if( verify_header( hdr ) != 0 )
|
||||
polarssl_exit( 1 );
|
||||
|
||||
if( hdr->alloc != 1 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
|
||||
"data\n" );
|
||||
#endif
|
||||
polarssl_exit( 1 );
|
||||
}
|
||||
|
||||
hdr->alloc = 0;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.free_count++;
|
||||
heap.total_used -= hdr->size;
|
||||
#endif
|
||||
|
||||
// Regroup with block before
|
||||
//
|
||||
if( hdr->prev != NULL && hdr->prev->alloc == 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.header_count--;
|
||||
#endif
|
||||
hdr->prev->size += sizeof(memory_header) + hdr->size;
|
||||
hdr->prev->next = hdr->next;
|
||||
old = hdr;
|
||||
hdr = hdr->prev;
|
||||
|
||||
if( hdr->next != NULL )
|
||||
hdr->next->prev = hdr;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
free( old->trace );
|
||||
#endif
|
||||
memset( old, 0, sizeof(memory_header) );
|
||||
}
|
||||
|
||||
// Regroup with block after
|
||||
//
|
||||
if( hdr->next != NULL && hdr->next->alloc == 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.header_count--;
|
||||
#endif
|
||||
hdr->size += sizeof(memory_header) + hdr->next->size;
|
||||
old = hdr->next;
|
||||
hdr->next = hdr->next->next;
|
||||
|
||||
if( hdr->prev_free != NULL || hdr->next_free != NULL )
|
||||
{
|
||||
if( hdr->prev_free != NULL )
|
||||
hdr->prev_free->next_free = hdr->next_free;
|
||||
else
|
||||
heap.first_free = hdr->next_free;
|
||||
|
||||
if( hdr->next_free != NULL )
|
||||
hdr->next_free->prev_free = hdr->prev_free;
|
||||
}
|
||||
|
||||
hdr->prev_free = old->prev_free;
|
||||
hdr->next_free = old->next_free;
|
||||
|
||||
if( hdr->prev_free != NULL )
|
||||
hdr->prev_free->next_free = hdr;
|
||||
else
|
||||
heap.first_free = hdr;
|
||||
|
||||
if( hdr->next_free != NULL )
|
||||
hdr->next_free->prev_free = hdr;
|
||||
|
||||
if( hdr->next != NULL )
|
||||
hdr->next->prev = hdr;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
free( old->trace );
|
||||
#endif
|
||||
memset( old, 0, sizeof(memory_header) );
|
||||
}
|
||||
|
||||
// Prepend to free_list if we have not merged
|
||||
// (Does not have to stay in same order as prev / next list)
|
||||
//
|
||||
if( old == NULL )
|
||||
{
|
||||
hdr->next_free = heap.first_free;
|
||||
if( heap.first_free != NULL )
|
||||
heap.first_free->prev_free = hdr;
|
||||
heap.first_free = hdr;
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
hdr->trace = NULL;
|
||||
hdr->trace_count = 0;
|
||||
#endif
|
||||
|
||||
if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
|
||||
polarssl_exit( 1 );
|
||||
}
|
||||
|
||||
void memory_buffer_set_verify( int verify )
|
||||
{
|
||||
heap.verify = verify;
|
||||
}
|
||||
|
||||
int memory_buffer_alloc_verify()
|
||||
{
|
||||
return verify_chain();
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
void memory_buffer_alloc_status()
|
||||
{
|
||||
polarssl_fprintf( stderr,
|
||||
"Current use: %zu blocks / %zu bytes, max: %zu blocks / "
|
||||
"%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
|
||||
heap.header_count, heap.total_used,
|
||||
heap.maximum_header_count, heap.maximum_used,
|
||||
heap.maximum_header_count * sizeof( memory_header )
|
||||
+ heap.maximum_used,
|
||||
heap.malloc_count, heap.free_count );
|
||||
|
||||
if( heap.first->next == NULL )
|
||||
polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
|
||||
else
|
||||
{
|
||||
polarssl_fprintf( stderr, "Memory currently allocated:\n" );
|
||||
debug_chain();
|
||||
}
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
|
||||
{
|
||||
*max_used = heap.maximum_used;
|
||||
*max_blocks = heap.maximum_header_count;
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_max_reset( void )
|
||||
{
|
||||
heap.maximum_used = 0;
|
||||
heap.maximum_header_count = 0;
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
|
||||
{
|
||||
*cur_used = heap.total_used;
|
||||
*cur_blocks = heap.header_count;
|
||||
}
|
||||
#endif /* POLARSSL_MEMORY_DEBUG */
|
||||
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
static void *buffer_alloc_malloc_mutexed( size_t len )
|
||||
{
|
||||
void *buf;
|
||||
polarssl_mutex_lock( &heap.mutex );
|
||||
buf = buffer_alloc_malloc( len );
|
||||
polarssl_mutex_unlock( &heap.mutex );
|
||||
return( buf );
|
||||
}
|
||||
|
||||
static void buffer_alloc_free_mutexed( void *ptr )
|
||||
{
|
||||
polarssl_mutex_lock( &heap.mutex );
|
||||
buffer_alloc_free( ptr );
|
||||
polarssl_mutex_unlock( &heap.mutex );
|
||||
}
|
||||
#endif /* POLARSSL_THREADING_C */
|
||||
|
||||
int memory_buffer_alloc_init( unsigned char *buf, size_t len )
|
||||
{
|
||||
memset( &heap, 0, sizeof(buffer_alloc_ctx) );
|
||||
memset( buf, 0, len );
|
||||
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
polarssl_mutex_init( &heap.mutex );
|
||||
platform_set_malloc_free( buffer_alloc_malloc_mutexed,
|
||||
buffer_alloc_free_mutexed );
|
||||
#else
|
||||
platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
|
||||
#endif
|
||||
|
||||
if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
|
||||
{
|
||||
/* Adjust len first since buf is used in the computation */
|
||||
len -= POLARSSL_MEMORY_ALIGN_MULTIPLE
|
||||
- (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
|
||||
- (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
}
|
||||
|
||||
heap.buf = buf;
|
||||
heap.len = len;
|
||||
|
||||
heap.first = (memory_header *) buf;
|
||||
heap.first->size = len - sizeof(memory_header);
|
||||
heap.first->magic1 = MAGIC1;
|
||||
heap.first->magic2 = MAGIC2;
|
||||
heap.first_free = heap.first;
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_free()
|
||||
{
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
polarssl_mutex_free( &heap.mutex );
|
||||
#endif
|
||||
polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
static int check_pointer( void *p )
|
||||
{
|
||||
if( p == NULL )
|
||||
return( -1 );
|
||||
|
||||
if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
|
||||
return( -1 );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int check_all_free( )
|
||||
{
|
||||
if(
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.total_used != 0 ||
|
||||
#endif
|
||||
heap.first != heap.first_free ||
|
||||
(void *) heap.first != (void *) heap.buf )
|
||||
{
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#define TEST_ASSERT( condition ) \
|
||||
if( ! (condition) ) \
|
||||
{ \
|
||||
if( verbose != 0 ) \
|
||||
polarssl_printf( "failed\n" ); \
|
||||
\
|
||||
ret = 1; \
|
||||
goto cleanup; \
|
||||
}
|
||||
|
||||
int memory_buffer_alloc_self_test( int verbose )
|
||||
{
|
||||
unsigned char buf[1024];
|
||||
unsigned char *p, *q, *r, *end;
|
||||
int ret = 0;
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " MBA test #1 (basic alloc-free cycle): " );
|
||||
|
||||
memory_buffer_alloc_init( buf, sizeof( buf ) );
|
||||
|
||||
p = polarssl_malloc( 1 );
|
||||
q = polarssl_malloc( 128 );
|
||||
r = polarssl_malloc( 16 );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 &&
|
||||
check_pointer( q ) == 0 &&
|
||||
check_pointer( r ) == 0 );
|
||||
|
||||
polarssl_free( r );
|
||||
polarssl_free( q );
|
||||
polarssl_free( p );
|
||||
|
||||
TEST_ASSERT( check_all_free( ) == 0 );
|
||||
|
||||
/* Memorize end to compare with the next test */
|
||||
end = heap.buf + heap.len;
|
||||
|
||||
memory_buffer_alloc_free( );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " MBA test #2 (buf not aligned): " );
|
||||
|
||||
memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
|
||||
|
||||
TEST_ASSERT( heap.buf + heap.len == end );
|
||||
|
||||
p = polarssl_malloc( 1 );
|
||||
q = polarssl_malloc( 128 );
|
||||
r = polarssl_malloc( 16 );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 &&
|
||||
check_pointer( q ) == 0 &&
|
||||
check_pointer( r ) == 0 );
|
||||
|
||||
polarssl_free( r );
|
||||
polarssl_free( q );
|
||||
polarssl_free( p );
|
||||
|
||||
TEST_ASSERT( check_all_free( ) == 0 );
|
||||
|
||||
memory_buffer_alloc_free( );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " MBA test #3 (full): " );
|
||||
|
||||
memory_buffer_alloc_init( buf, sizeof( buf ) );
|
||||
|
||||
p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 );
|
||||
TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
|
||||
|
||||
polarssl_free( p );
|
||||
|
||||
p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
|
||||
q = polarssl_malloc( 16 );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
|
||||
TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
|
||||
|
||||
polarssl_free( q );
|
||||
|
||||
TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
|
||||
|
||||
polarssl_free( p );
|
||||
|
||||
TEST_ASSERT( check_all_free( ) == 0 );
|
||||
|
||||
memory_buffer_alloc_free( );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
|
||||
cleanup:
|
||||
memory_buffer_alloc_free( );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_SELF_TEST */
|
||||
|
||||
#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */
|
||||
@@ -0,0 +1,149 @@
|
||||
/**
|
||||
* \file memory_buffer_alloc.h
|
||||
*
|
||||
* \brief Buffer-based memory allocator
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef POLARSSL_MEMORY_BUFFER_ALLOC_H
|
||||
#define POLARSSL_MEMORY_BUFFER_ALLOC_H
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* \name SECTION: Module settings
|
||||
*
|
||||
* The configuration options you can set for this module are in this section.
|
||||
* Either change them in config.h or define them on the compiler command line.
|
||||
* \{
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_MEMORY_ALIGN_MULTIPLE)
|
||||
#define POLARSSL_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */
|
||||
#endif
|
||||
|
||||
/* \} name SECTION: Module settings */
|
||||
|
||||
#define MEMORY_VERIFY_NONE 0
|
||||
#define MEMORY_VERIFY_ALLOC (1 << 0)
|
||||
#define MEMORY_VERIFY_FREE (1 << 1)
|
||||
#define MEMORY_VERIFY_ALWAYS (MEMORY_VERIFY_ALLOC | MEMORY_VERIFY_FREE)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Initialize use of stack-based memory allocator.
|
||||
* The stack-based allocator does memory management inside the
|
||||
* presented buffer and does not call malloc() and free().
|
||||
* It sets the global polarssl_malloc() and polarssl_free() pointers
|
||||
* to its own functions.
|
||||
* (Provided polarssl_malloc() and polarssl_free() are thread-safe if
|
||||
* POLARSSL_THREADING_C is defined)
|
||||
*
|
||||
* \note This code is not optimized and provides a straight-forward
|
||||
* implementation of a stack-based memory allocator.
|
||||
*
|
||||
* \param buf buffer to use as heap
|
||||
* \param len size of the buffer
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int memory_buffer_alloc_init( unsigned char *buf, size_t len );
|
||||
|
||||
/**
|
||||
* \brief Free the mutex for thread-safety and clear remaining memory
|
||||
*/
|
||||
void memory_buffer_alloc_free( void );
|
||||
|
||||
/**
|
||||
* \brief Determine when the allocator should automatically verify the state
|
||||
* of the entire chain of headers / meta-data.
|
||||
* (Default: MEMORY_VERIFY_NONE)
|
||||
*
|
||||
* \param verify One of MEMORY_VERIFY_NONE, MEMORY_VERIFY_ALLOC,
|
||||
* MEMORY_VERIFY_FREE or MEMORY_VERIFY_ALWAYS
|
||||
*/
|
||||
void memory_buffer_set_verify( int verify );
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
/**
|
||||
* \brief Print out the status of the allocated memory (primarily for use
|
||||
* after a program should have de-allocated all memory)
|
||||
* Prints out a list of 'still allocated' blocks and their stack
|
||||
* trace if POLARSSL_MEMORY_BACKTRACE is defined.
|
||||
*/
|
||||
void memory_buffer_alloc_status( void );
|
||||
|
||||
/**
|
||||
* \brief Get the peak heap usage so far
|
||||
*
|
||||
* \param max_used Peak number of bytes reauested by the application
|
||||
* \param max_blocks Peak number of blocks reauested by the application
|
||||
*/
|
||||
void memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks );
|
||||
|
||||
/**
|
||||
* \brief Reset peak statistics
|
||||
*/
|
||||
void memory_buffer_alloc_max_reset( void );
|
||||
|
||||
/**
|
||||
* \brief Get the current heap usage
|
||||
*
|
||||
* \param cur_used Number of bytes reauested by the application
|
||||
* \param cur_blocks Number of blocks reauested by the application
|
||||
*/
|
||||
void memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks );
|
||||
#endif /* POLARSSL_MEMORY_DEBUG */
|
||||
|
||||
/**
|
||||
* \brief Verifies that all headers in the memory buffer are correct
|
||||
* and contain sane values. Helps debug buffer-overflow errors.
|
||||
*
|
||||
* Prints out first failure if POLARSSL_MEMORY_DEBUG is defined.
|
||||
* Prints out full header information if POLARSSL_MEMORY_DEBUG_HEADERS
|
||||
* is defined. (Includes stack trace information for each block if
|
||||
* POLARSSL_MEMORY_BACKTRACE is defined as well).
|
||||
*
|
||||
* \returns 0 if verified, 1 otherwise
|
||||
*/
|
||||
int memory_buffer_alloc_verify( void );
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if a test failed
|
||||
*/
|
||||
int memory_buffer_alloc_self_test( int verbose );
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* memory_buffer_alloc.h */
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Platform abstraction layer
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#if !defined(POLARSSL_PLATFORM_STD_MALLOC)
|
||||
static void *platform_malloc_uninit( size_t len )
|
||||
{
|
||||
((void) len);
|
||||
return( NULL );
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_MALLOC platform_malloc_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_MALLOC */
|
||||
|
||||
#if !defined(POLARSSL_PLATFORM_STD_FREE)
|
||||
static void platform_free_uninit( void *ptr )
|
||||
{
|
||||
((void) ptr);
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_FREE platform_free_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_FREE */
|
||||
|
||||
void * (*polarssl_malloc)( size_t ) = POLARSSL_PLATFORM_STD_MALLOC;
|
||||
void (*polarssl_free)( void * ) = POLARSSL_PLATFORM_STD_FREE;
|
||||
|
||||
int platform_set_malloc_free( void * (*malloc_func)( size_t ),
|
||||
void (*free_func)( void * ) )
|
||||
{
|
||||
polarssl_malloc = malloc_func;
|
||||
polarssl_free = free_func;
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_PLATFORM_MEMORY */
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT)
|
||||
#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF)
|
||||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static int platform_snprintf_uninit( char * s, size_t n,
|
||||
const char * format, ... )
|
||||
{
|
||||
((void) s);
|
||||
((void) n);
|
||||
((void) format);
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_SNPRINTF */
|
||||
|
||||
int (*polarssl_snprintf)( char * s, size_t n,
|
||||
const char * format,
|
||||
... ) = POLARSSL_PLATFORM_STD_SNPRINTF;
|
||||
|
||||
int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
|
||||
const char * format,
|
||||
... ) )
|
||||
{
|
||||
polarssl_snprintf = snprintf_func;
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_ALT)
|
||||
#if !defined(POLARSSL_PLATFORM_STD_PRINTF)
|
||||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static int platform_printf_uninit( const char *format, ... )
|
||||
{
|
||||
((void) format);
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_PRINTF platform_printf_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_PRINTF */
|
||||
|
||||
int (*polarssl_printf)( const char *, ... ) = POLARSSL_PLATFORM_STD_PRINTF;
|
||||
|
||||
int platform_set_printf( int (*printf_func)( const char *, ... ) )
|
||||
{
|
||||
polarssl_printf = printf_func;
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_PLATFORM_PRINTF_ALT */
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_ALT)
|
||||
#if !defined(POLARSSL_PLATFORM_STD_FPRINTF)
|
||||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
|
||||
{
|
||||
((void) stream);
|
||||
((void) format);
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_FPRINTF platform_fprintf_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_FPRINTF */
|
||||
|
||||
int (*polarssl_fprintf)( FILE *, const char *, ... ) =
|
||||
POLARSSL_PLATFORM_STD_FPRINTF;
|
||||
|
||||
int platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
|
||||
{
|
||||
polarssl_fprintf = fprintf_func;
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_PLATFORM_FPRINTF_ALT */
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_ALT)
|
||||
#if !defined(POLARSSL_PLATFORM_STD_EXIT)
|
||||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static void platform_exit_uninit( int status )
|
||||
{
|
||||
((void) status);
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_EXIT platform_exit_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_EXIT */
|
||||
|
||||
void (*polarssl_exit)( int status ) = POLARSSL_PLATFORM_STD_EXIT;
|
||||
|
||||
int platform_set_exit( void (*exit_func)( int status ) )
|
||||
{
|
||||
polarssl_exit = exit_func;
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_PLATFORM_EXIT_ALT */
|
||||
|
||||
#endif /* POLARSSL_PLATFORM_C */
|
||||
@@ -0,0 +1,205 @@
|
||||
/**
|
||||
* \file platform.h
|
||||
*
|
||||
* \brief mbed TLS Platform abstraction layer
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef POLARSSL_PLATFORM_H
|
||||
#define POLARSSL_PLATFORM_H
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
/* Temporary compatibility hack for to keep MEMORY_C working */
|
||||
#if defined(POLARSSL_MEMORY_C) && !defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#define POLARSSL_PLATFORM_MEMORY
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \name SECTION: Module settings
|
||||
*
|
||||
* The configuration options you can set for this module are in this section.
|
||||
* Either change them in config.h or define them on the compiler command line.
|
||||
* \{
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS)
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF)
|
||||
#define POLARSSL_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
|
||||
#endif
|
||||
#if !defined(POLARSSL_PLATFORM_STD_PRINTF)
|
||||
#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use */
|
||||
#endif
|
||||
#if !defined(POLARSSL_PLATFORM_STD_FPRINTF)
|
||||
#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
|
||||
#endif
|
||||
#if !defined(POLARSSL_PLATFORM_STD_MALLOC)
|
||||
#define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use */
|
||||
#endif
|
||||
#if !defined(POLARSSL_PLATFORM_STD_FREE)
|
||||
#define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use */
|
||||
#endif
|
||||
#if !defined(POLARSSL_PLATFORM_STD_EXIT)
|
||||
#define POLARSSL_PLATFORM_STD_EXIT exit /**< Default free to use */
|
||||
#endif
|
||||
#else /* POLARSSL_PLATFORM_NO_STD_FUNCTIONS */
|
||||
#if defined(POLARSSL_PLATFORM_STD_MEM_HDR)
|
||||
#include POLARSSL_PLATFORM_STD_MEM_HDR
|
||||
#endif
|
||||
#endif /* POLARSSL_PLATFORM_NO_STD_FUNCTIONS */
|
||||
|
||||
/* \} name SECTION: Module settings */
|
||||
|
||||
/*
|
||||
* The function pointers for malloc and free
|
||||
*/
|
||||
#if defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#if defined(POLARSSL_PLATFORM_FREE_MACRO) && \
|
||||
defined(POLARSSL_PLATFORM_MALLOC_MACRO)
|
||||
#define polarssl_free POLARSSL_PLATFORM_FREE_MACRO
|
||||
#define polarssl_malloc POLARSSL_PLATFORM_MALLOC_MACRO
|
||||
#else
|
||||
/* For size_t */
|
||||
#include <stddef.h>
|
||||
extern void * (*polarssl_malloc)( size_t len );
|
||||
extern void (*polarssl_free)( void *ptr );
|
||||
|
||||
/**
|
||||
* \brief Set your own memory implementation function pointers
|
||||
*
|
||||
* \param malloc_func the malloc function implementation
|
||||
* \param free_func the free function implementation
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int platform_set_malloc_free( void * (*malloc_func)( size_t ),
|
||||
void (*free_func)( void * ) );
|
||||
#endif /* POLARSSL_PLATFORM_FREE_MACRO && POLARSSL_PLATFORM_MALLOC_MACRO */
|
||||
#else /* !POLARSSL_PLATFORM_MEMORY */
|
||||
#define polarssl_free free
|
||||
#define polarssl_malloc malloc
|
||||
#endif /* POLARSSL_PLATFORM_MEMORY && !POLARSSL_PLATFORM_{FREE,MALLOC}_MACRO */
|
||||
|
||||
/*
|
||||
* The function pointers for fprintf
|
||||
*/
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_ALT)
|
||||
/* We need FILE * */
|
||||
#include <stdio.h>
|
||||
extern int (*polarssl_fprintf)( FILE *stream, const char *format, ... );
|
||||
|
||||
/**
|
||||
* \brief Set your own fprintf function pointer
|
||||
*
|
||||
* \param fprintf_func the fprintf function implementation
|
||||
*
|
||||
* \return 0
|
||||
*/
|
||||
int platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
|
||||
... ) );
|
||||
#else
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO)
|
||||
#define polarssl_fprintf POLARSSL_PLATFORM_FPRINTF_MACRO
|
||||
#else
|
||||
#define polarssl_fprintf fprintf
|
||||
#endif /* POLARSSL_PLATFORM_FPRINTF_MACRO */
|
||||
#endif /* POLARSSL_PLATFORM_FPRINTF_ALT */
|
||||
|
||||
/*
|
||||
* The function pointers for printf
|
||||
*/
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_ALT)
|
||||
extern int (*polarssl_printf)( const char *format, ... );
|
||||
|
||||
/**
|
||||
* \brief Set your own printf function pointer
|
||||
*
|
||||
* \param printf_func the printf function implementation
|
||||
*
|
||||
* \return 0
|
||||
*/
|
||||
int platform_set_printf( int (*printf_func)( const char *, ... ) );
|
||||
#else /* !POLARSSL_PLATFORM_PRINTF_ALT */
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_MACRO)
|
||||
#define polarssl_printf POLARSSL_PLATFORM_PRINTF_MACRO
|
||||
#else
|
||||
#define polarssl_printf printf
|
||||
#endif /* POLARSSL_PLATFORM_PRINTF_MACRO */
|
||||
#endif /* POLARSSL_PLATFORM_PRINTF_ALT */
|
||||
|
||||
/*
|
||||
* The function pointers for snprintf
|
||||
*/
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT)
|
||||
extern int (*polarssl_snprintf)( char * s, size_t n, const char * format, ... );
|
||||
|
||||
/**
|
||||
* \brief Set your own snprintf function pointer
|
||||
*
|
||||
* \param snprintf_func the snprintf function implementation
|
||||
*
|
||||
* \return 0
|
||||
*/
|
||||
int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
|
||||
const char * format, ... ) );
|
||||
#else /* POLARSSL_PLATFORM_SNPRINTF_ALT */
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO)
|
||||
#define polarssl_snprintf POLARSSL_PLATFORM_SNPRINTF_MACRO
|
||||
#else
|
||||
#define polarssl_snprintf snprintf
|
||||
#endif /* POLARSSL_PLATFORM_SNPRINTF_MACRO */
|
||||
#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */
|
||||
|
||||
/*
|
||||
* The function pointers for exit
|
||||
*/
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_ALT)
|
||||
extern void (*polarssl_exit)( int status );
|
||||
|
||||
/**
|
||||
* \brief Set your own exit function pointer
|
||||
*
|
||||
* \param exit_func the exit function implementation
|
||||
*
|
||||
* \return 0
|
||||
*/
|
||||
int platform_set_exit( void (*exit_func)( int status ) );
|
||||
#else
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_MACRO)
|
||||
#define polarssl_exit POLARSSL_PLATFORM_EXIT_MACRO
|
||||
#else
|
||||
#define polarssl_exit exit
|
||||
#endif /* POLARSSL_PLATFORM_EXIT_MACRO */
|
||||
#endif /* POLARSSL_PLATFORM_EXIT_ALT */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* platform.h */
|
||||
@@ -0,0 +1,328 @@
|
||||
/*
|
||||
* srp.c
|
||||
*
|
||||
* SRP algorithm (see http://srp.stanford.edu)
|
||||
* Uses bits of the ARM mbed TSL library (https://tls.mbed.org).
|
||||
* Notes: You can probably do better here since N and G are known at compile time.
|
||||
* This library uses a massive amount of RAM during calculations.
|
||||
* The SRP handshake takes 30+ seconds.
|
||||
*
|
||||
* Created on: Jun 10, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
//#include <app_error.h>
|
||||
|
||||
//#include "homekit/homekit-config.h"
|
||||
#include "bignum.h"
|
||||
#include "memory_buffer_alloc.h"
|
||||
#include "../random.h"
|
||||
#include "srp.h"
|
||||
#include "../tweetnacl-modified/tweetnacl.h"
|
||||
//#include "homekit/pairing.h"
|
||||
|
||||
#define E(V) (((V) << 24) | ((V) >> 24) | (((V) >> 8) & 0x0000FF00) | (((V) << 8) & 0x00FF0000))
|
||||
static const uint32_t srp_N[] =
|
||||
{
|
||||
E(0xFFFFFFFF), E(0xFFFFFFFF), E(0xC90FDAA2), E(0x2168C234), E(0xC4C6628B), E(0x80DC1CD1), E(0x29024E08),
|
||||
E(0x8A67CC74), E(0x020BBEA6), E(0x3B139B22), E(0x514A0879), E(0x8E3404DD), E(0xEF9519B3), E(0xCD3A431B),
|
||||
E(0x302B0A6D), E(0xF25F1437), E(0x4FE1356D), E(0x6D51C245), E(0xE485B576), E(0x625E7EC6), E(0xF44C42E9),
|
||||
E(0xA637ED6B), E(0x0BFF5CB6), E(0xF406B7ED), E(0xEE386BFB), E(0x5A899FA5), E(0xAE9F2411), E(0x7C4B1FE6),
|
||||
E(0x49286651), E(0xECE45B3D), E(0xC2007CB8), E(0xA163BF05), E(0x98DA4836), E(0x1C55D39A), E(0x69163FA8),
|
||||
E(0xFD24CF5F), E(0x83655D23), E(0xDCA3AD96), E(0x1C62F356), E(0x208552BB), E(0x9ED52907), E(0x7096966D),
|
||||
E(0x670C354E), E(0x4ABC9804), E(0xF1746C08), E(0xCA18217C), E(0x32905E46), E(0x2E36CE3B), E(0xE39E772C),
|
||||
E(0x180E8603), E(0x9B2783A2), E(0xEC07A28F), E(0xB5C55DF0), E(0x6F4C52C9), E(0xDE2BCBF6), E(0x95581718),
|
||||
E(0x3995497C), E(0xEA956AE5), E(0x15D22618), E(0x98FA0510), E(0x15728E5A), E(0x8AAAC42D), E(0xAD33170D),
|
||||
E(0x04507A33), E(0xA85521AB), E(0xDF1CBA64), E(0xECFB8504), E(0x58DBEF0A), E(0x8AEA7157), E(0x5D060C7D),
|
||||
E(0xB3970F85), E(0xA6E1E4C7), E(0xABF5AE8C), E(0xDB0933D7), E(0x1E8C94E0), E(0x4A25619D), E(0xCEE3D226),
|
||||
E(0x1AD2EE6B), E(0xF12FFA06), E(0xD98A0864), E(0xD8760273), E(0x3EC86A64), E(0x521F2B18), E(0x177B200C),
|
||||
E(0xBBE11757), E(0x7A615D6C), E(0x770988C0), E(0xBAD946E2), E(0x08E24FA0), E(0x74E5AB31), E(0x43DB5BFC),
|
||||
E(0xE0FD108E), E(0x4B82D120), E(0xA93AD2CA), E(0xFFFFFFFF), E(0xFFFFFFFF)
|
||||
};
|
||||
#undef E
|
||||
static const uint32_t srp_N_sizeof = 384;
|
||||
static const uint8_t srp_G = 5;
|
||||
// Pre-compute hashes where we can
|
||||
static const uint8_t srp_N_G_hash[] =
|
||||
{
|
||||
0xA9, 0xC2, 0xE2, 0x55, 0x9B, 0xF0, 0xEB, 0xB5, 0x3F, 0x0C, 0xBB, 0xF6, 0x22, 0x82, 0x90, 0x6B,
|
||||
0xED, 0xE7, 0xF2, 0x18, 0x2F, 0x00, 0x67, 0x82, 0x11, 0xFB, 0xD5, 0xBD, 0xE5, 0xB2, 0x85, 0x03,
|
||||
0x3A, 0x49, 0x93, 0x50, 0x3B, 0x87, 0x39, 0x7F, 0x9B, 0xE5, 0xEC, 0x02, 0x08, 0x0F, 0xED, 0xBC,
|
||||
0x08, 0x35, 0x58, 0x7A, 0xD0, 0x39, 0x06, 0x08, 0x79, 0xB8, 0x62, 0x1E, 0x8C, 0x36, 0x59, 0xE0
|
||||
};
|
||||
static const uint8_t srp_N_hash_srp_G_hash[] =
|
||||
{
|
||||
0xB3, 0xD6, 0x3E, 0xF6, 0xAA, 0xFB, 0x2E, 0x97, 0x96, 0xDA, 0xE0, 0x06, 0xFE, 0x60, 0xF2, 0x0E,
|
||||
0x53, 0x26, 0xFE, 0x1E, 0x1C, 0x52, 0xA5, 0x02, 0x1E, 0xB2, 0x17, 0x47, 0xCA, 0x33, 0xDF, 0xEA,
|
||||
0x8F, 0xF2, 0x03, 0xBD, 0x45, 0xA2, 0x54, 0x43, 0x95, 0xD7, 0x3D, 0x19, 0x89, 0x9C, 0x17, 0x4A,
|
||||
0x68, 0x3C, 0x9E, 0x78, 0x32, 0x96, 0xDD, 0x16, 0x93, 0xEB, 0xC7, 0x1C, 0xF5, 0xA5, 0x3D, 0xA3
|
||||
};
|
||||
|
||||
static const uint8_t pincode[21] = "Pair-Setup:" HOMEKIT_CONFIG_PINCODE;
|
||||
|
||||
srp_keys_t srp;
|
||||
|
||||
static void MPI_ERROR_CHECK(int CODE)
|
||||
{
|
||||
if (CODE != 0)
|
||||
{
|
||||
APP_ERROR_CHECK(NRF_ERROR_INTERNAL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void srp_init(void)
|
||||
{
|
||||
int err_code;
|
||||
|
||||
// The MPI library uses a ridiculous amount of memory. We use the stack allocator
|
||||
// so we don't tie this memory up except when we absolutely need to.
|
||||
uint8_t memory[11 * 1024];
|
||||
memory_buffer_alloc_init(memory, sizeof(memory));
|
||||
|
||||
// Generate salt
|
||||
random_create(srp.salt, sizeof(srp.salt));
|
||||
|
||||
// Generate 'b' - a random value
|
||||
random_create(srp.b, 32);
|
||||
|
||||
// Calculate 'x' = H(s | H(I | ":" | P))
|
||||
mpi x;
|
||||
mpi_init(&x);
|
||||
{
|
||||
uint8_t message[sizeof(srp.salt) + 64];
|
||||
memcpy(message, srp.salt, sizeof(srp.salt));
|
||||
crypto_hash_sha512(message + sizeof(srp.salt), pincode, sizeof(pincode));
|
||||
crypto_hash_sha512(message, message, sizeof(message));
|
||||
err_code = mpi_read_binary(&x, message, 64);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
// Calculate 'v' = g ^ x mod N
|
||||
mpi g;
|
||||
mpi_init(&g);
|
||||
err_code = mpi_lset(&g, srp_G);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
mpi n;
|
||||
mpi_init(&n);
|
||||
err_code = mpi_read_binary(&n, (uint8_t*)srp_N, srp_N_sizeof);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
mpi tmp;
|
||||
mpi_init(&tmp);
|
||||
mpi v;
|
||||
mpi_init(&v);
|
||||
err_code = mpi_exp_mod(&v, &g, &x, &n, &tmp);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
// Calculate 'k'
|
||||
mpi k;
|
||||
mpi_init(&k);
|
||||
err_code = mpi_read_binary(&k, srp_N_G_hash, sizeof(srp_N_G_hash));
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
// Calculate 'B' = k*v + g^b % N
|
||||
mpi B;
|
||||
mpi_init(&B);
|
||||
err_code = mpi_mul_mpi(&B, &k, &v);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
err_code = mpi_write_binary(&v, srp.v, sizeof(srp.v));
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
mpi_free(&v);
|
||||
|
||||
mpi b;
|
||||
mpi_init(&b);
|
||||
err_code = mpi_read_binary(&b, srp.b, sizeof(srp.b));
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
err_code = mpi_exp_mod(&x, &g, &b, &n, &tmp);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
err_code = mpi_add_abs(&B, &B, &x);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
err_code = mpi_mod_mpi(&B, &B, &n);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
err_code = mpi_write_binary(&B, srp.B, sizeof(srp.B));
|
||||
|
||||
mpi_free(&b);
|
||||
mpi_free(&B);
|
||||
mpi_free(&k);
|
||||
mpi_free(&tmp);
|
||||
mpi_free(&n);
|
||||
mpi_free(&g);
|
||||
mpi_free(&x);
|
||||
|
||||
memory_buffer_alloc_free();
|
||||
}
|
||||
|
||||
void srp_start(void)
|
||||
{
|
||||
srp.clientM1 = 0;
|
||||
srp.serverM1 = 0;
|
||||
}
|
||||
|
||||
uint8_t srp_setA(uint8_t* abuf, uint16_t length, moretime_t moretime)
|
||||
{
|
||||
int err_code;
|
||||
|
||||
// The MPI library uses a ridiculous amount of memory. We use the stack allocator
|
||||
// so we don't tie this memory up except when we absolutely need to.
|
||||
uint8_t memory[11 * 1024];
|
||||
memory_buffer_alloc_init(memory, sizeof(memory));
|
||||
|
||||
// getK
|
||||
{
|
||||
mpi s;
|
||||
mpi_init(&s);
|
||||
|
||||
mpi n;
|
||||
mpi_init(&n);
|
||||
err_code = mpi_read_binary(&n, (uint8_t*)srp_N, srp_N_sizeof);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
{
|
||||
// u = H(A | B)
|
||||
mpi u;
|
||||
mpi_init(&u);
|
||||
{
|
||||
uint8_t message[length + sizeof(srp.B)];
|
||||
memcpy(message, abuf, length);
|
||||
memcpy(message + length, srp.B, sizeof(srp.B));
|
||||
crypto_hash_sha512(message, message, sizeof(message));
|
||||
|
||||
err_code = mpi_read_binary(&u, message, 64);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
mpi v;
|
||||
mpi_init(&v);
|
||||
err_code = mpi_read_binary(&v, srp.v, sizeof(srp.v));
|
||||
|
||||
// getS = (A * v^u mod N)^b mod N
|
||||
err_code = mpi_exp_mod(&s, &v, &u, &n, NULL);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
mpi_free(&v);
|
||||
mpi_free(&u);
|
||||
}
|
||||
|
||||
// These calculations take a long time. To avoid the connection dying we ask for more time now.
|
||||
if (moretime)
|
||||
{
|
||||
moretime();
|
||||
}
|
||||
|
||||
{
|
||||
mpi a;
|
||||
mpi_init(&a);
|
||||
err_code = mpi_read_binary(&a, abuf, length);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
err_code = mpi_mul_mpi(&s, &s, &a);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
mpi_free(&a);
|
||||
|
||||
mpi b;
|
||||
mpi_init(&b);
|
||||
err_code = mpi_read_binary(&b, srp.b, sizeof(srp.b));
|
||||
|
||||
err_code = mpi_exp_mod(&s, &s, &b, &n, NULL);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
mpi_free(&b);
|
||||
}
|
||||
|
||||
mpi_free(&n);
|
||||
|
||||
uint8_t sbuf[384];
|
||||
err_code = mpi_write_binary(&s, sbuf, sizeof(sbuf));
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
mpi_free(&s);
|
||||
|
||||
crypto_hash_sha512(srp.K, sbuf, sizeof(sbuf));
|
||||
}
|
||||
|
||||
memory_buffer_alloc_free();
|
||||
|
||||
// getM1 - username s abuf srp.B K
|
||||
{
|
||||
uint8_t message[sizeof(srp_N_hash_srp_G_hash) + 64 + sizeof(srp.salt) + length + 384 + sizeof(srp.K)];
|
||||
memcpy(message, srp_N_hash_srp_G_hash, sizeof(srp_N_hash_srp_G_hash));
|
||||
crypto_hash_sha512(message + sizeof(srp_N_hash_srp_G_hash), pincode, 10); // First 10 chars only - not the PIN part
|
||||
memcpy(message + sizeof(srp_N_hash_srp_G_hash) + 64, srp.salt, sizeof(srp.salt));
|
||||
memcpy(message + sizeof(srp_N_hash_srp_G_hash) + 64 + sizeof(srp.salt), abuf, length);
|
||||
memcpy(message + sizeof(srp_N_hash_srp_G_hash) + 64 + sizeof(srp.salt) + length, srp.B, sizeof(srp.B));
|
||||
memcpy(message + sizeof(srp_N_hash_srp_G_hash) + 64 + sizeof(srp.salt) + length + 384, srp.K, sizeof(srp.K));
|
||||
srp.serverM1 = 1;
|
||||
if (srp.clientM1)
|
||||
{
|
||||
uint8_t hash[64];
|
||||
crypto_hash_sha512(hash, message, sizeof(message));
|
||||
if (memcmp(hash, srp.M1, sizeof(srp.M1)) != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
crypto_hash_sha512(srp.M1, message, sizeof(message));
|
||||
}
|
||||
}
|
||||
|
||||
// getM2
|
||||
{
|
||||
uint8_t message[length + sizeof(srp.M1) + sizeof(srp.K)];
|
||||
memcpy(message, abuf, length);
|
||||
memcpy(message + length, srp.M1, sizeof(srp.M1));
|
||||
memcpy(message + length + sizeof(srp.M1), srp.K, sizeof(srp.K));
|
||||
crypto_hash_sha512(srp.M2, message, sizeof(message));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t* srp_getSalt(void)
|
||||
{
|
||||
return srp.salt;
|
||||
}
|
||||
|
||||
uint8_t* srp_getB(void)
|
||||
{
|
||||
return srp.B;
|
||||
}
|
||||
|
||||
uint8_t srp_checkM1(uint8_t* m1, uint16_t length)
|
||||
{
|
||||
srp.clientM1 = 1;
|
||||
if (length != sizeof(srp.M1))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (srp.serverM1)
|
||||
{
|
||||
if (memcmp(srp.M1, m1, sizeof(srp.M1)) != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(srp.M1, m1, sizeof(srp.M1));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t* srp_getM2(void)
|
||||
{
|
||||
return srp.M2;
|
||||
}
|
||||
|
||||
uint8_t* srp_getK(void)
|
||||
{
|
||||
return srp.K;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* srp.h
|
||||
*
|
||||
* Created on: Jun 10, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#ifndef HOMEKIT_SRP_SRP_H_
|
||||
#define HOMEKIT_SRP_SRP_H_
|
||||
|
||||
#define BIGNUM_BYTES 384
|
||||
#define BIGNUM_WORDS (BIGNUM_BYTES / 4)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t b[32];
|
||||
uint8_t salt[16];
|
||||
uint8_t v[384];
|
||||
uint8_t B[384];
|
||||
|
||||
uint8_t K[64];
|
||||
uint8_t M1[64];
|
||||
uint8_t M2[64];
|
||||
|
||||
uint8_t clientM1:1;
|
||||
uint8_t serverM1:1;
|
||||
} srp_keys_t;
|
||||
|
||||
extern srp_keys_t srp;
|
||||
|
||||
typedef void (*moretime_t)(void);
|
||||
|
||||
extern void srp_init(void);
|
||||
extern void srp_start(void);
|
||||
extern uint8_t srp_setA(uint8_t* a, uint16_t length, moretime_t moretime);
|
||||
extern uint8_t srp_checkM1(uint8_t* m1, uint16_t length);
|
||||
extern uint8_t* srp_getSalt(void);
|
||||
extern uint8_t* srp_getB(void);
|
||||
extern uint8_t* srp_getM2(void);
|
||||
extern uint8_t* srp_getK(void);
|
||||
|
||||
#endif /* HOMEKIT_SRP_SRP_H_ */
|
||||
@@ -0,0 +1,841 @@
|
||||
/*
|
||||
* tweetnacl.c
|
||||
*
|
||||
* Modified version of the amazing NaCL in 100 Tweets: http://tweetnacl.cr.yp.to/software.html
|
||||
*
|
||||
* 1. Replaced salsa20 with chacha20
|
||||
* 2. Improved performance, especially around multiply routines. No assembly yet (it would probably help a great deal).
|
||||
*
|
||||
* Created on: Jun 21, 2015
|
||||
* Modified: tim
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../crypto.h"
|
||||
|
||||
#define FOR(i,n) for (i = 0;i < n;++i)
|
||||
|
||||
typedef unsigned char u8;
|
||||
typedef unsigned short u16;
|
||||
typedef short i16;
|
||||
typedef unsigned long u32;
|
||||
typedef unsigned long long u64;
|
||||
typedef long i32;
|
||||
typedef long long i64;
|
||||
typedef i64 gf[16];
|
||||
extern void randombytes(u8 *,u64);
|
||||
|
||||
static const u8
|
||||
_0[16],
|
||||
_9[32] = {9};
|
||||
static const gf
|
||||
gf0,
|
||||
gf1 = {1},
|
||||
_121665 = {0xDB41,1},
|
||||
D = {0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203},
|
||||
D2 = {0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406},
|
||||
X = {0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169},
|
||||
Y = {0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666},
|
||||
I = {0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83};
|
||||
|
||||
static inline u32 L32(u32 x,int c) { return (x << c) | ((x&0xffffffff) >> (32 - c)); }
|
||||
|
||||
static u32 ld32(const u8 *x)
|
||||
{
|
||||
u32 u = x[3];
|
||||
u = (u<<8)|x[2];
|
||||
u = (u<<8)|x[1];
|
||||
return (u<<8)|x[0];
|
||||
}
|
||||
|
||||
static u64 dl64(const u8 *x)
|
||||
{
|
||||
u64 i,u=0;
|
||||
FOR(i,8) u=(u<<8)|x[i];
|
||||
return u;
|
||||
}
|
||||
|
||||
static void st32(u8 *x,u32 u)
|
||||
{
|
||||
int i;
|
||||
FOR(i,4) { x[i] = u; u >>= 8; }
|
||||
}
|
||||
|
||||
static void ts64(u8 *x,u64 u)
|
||||
{
|
||||
int i;
|
||||
for (i = 7;i >= 0;--i) { x[i] = u; u >>= 8; }
|
||||
}
|
||||
|
||||
static inline int vn(const u8 *x,const u8 *y,int n)
|
||||
{
|
||||
return memcmp(x,y,n);
|
||||
}
|
||||
|
||||
int inline crypto_verify_16(const u8 *x,const u8 *y)
|
||||
{
|
||||
return vn(x,y,16);
|
||||
}
|
||||
|
||||
int inline crypto_verify_32(const u8 *x,const u8 *y)
|
||||
{
|
||||
return vn(x,y,32);
|
||||
}
|
||||
|
||||
static void crypto_core_chacha20(u8 *out,const u8 *in,const u8 *k,const u8 *c)
|
||||
{
|
||||
u32 x[16],y[16],t[4];
|
||||
int i,j,m;
|
||||
|
||||
FOR(i,4) {
|
||||
x[i] = ld32(c+4*i);
|
||||
x[4+i] = ld32(k+4*i);
|
||||
x[8+i] = ld32(k+16+4*i);
|
||||
x[12+(2+i)%4] = ld32(in+4*i);
|
||||
}
|
||||
|
||||
FOR(i,16) y[i] = x[i];
|
||||
|
||||
FOR(i,10) {
|
||||
FOR(j,4) {
|
||||
FOR(m,4) t[m] = x[(j+4*m)%16];
|
||||
t[3] = L32(t[3] ^ (t[0] += t[1]), 16);
|
||||
t[1] = L32(t[1] ^ (t[2] += t[3]), 12);
|
||||
t[3] = L32(t[3] ^ (t[0] += t[1]), 8);
|
||||
t[1] = L32(t[1] ^ (t[2] += t[3]), 7);
|
||||
FOR(m,4) x[(j+4*m)%16] = t[m];
|
||||
}
|
||||
FOR(j,4) {
|
||||
FOR(m,4) t[m] = x[(4*m+(j+m)%4)%16];
|
||||
t[3] = L32(t[3] ^ (t[0] += t[1]), 16);
|
||||
t[1] = L32(t[1] ^ (t[2] += t[3]), 12);
|
||||
t[3] = L32(t[3] ^ (t[0] += t[1]), 8);
|
||||
t[1] = L32(t[1] ^ (t[2] += t[3]), 7);
|
||||
FOR(m,4) x[(4*m+(j+m)%4)%16] = t[m];
|
||||
}
|
||||
}
|
||||
FOR(i,16) st32(out + 4 * i,x[i] + y[i]);
|
||||
}
|
||||
|
||||
static const u8 sigma[16] = "expand 32-byte k";
|
||||
|
||||
int crypto_stream_chacha20_xor(u8 *c,const u8 *m,u64 b,const u8 *n,const u8 *k,const u8 v)
|
||||
{
|
||||
u8 z[16],x[64];
|
||||
u32 u,i;
|
||||
if (!b) return 0;
|
||||
FOR(i,16) z[i] = 0;
|
||||
FOR(i,8) z[i] = n[i];
|
||||
z[8] = v;
|
||||
while (b >= 64) {
|
||||
crypto_core_chacha20(x,z,k,sigma);
|
||||
FOR(i,64) c[i] = (m?m[i]:0) ^ x[i];
|
||||
u = 1;
|
||||
for (i = 8;i < 16;++i) {
|
||||
u += (u32) z[i];
|
||||
z[i] = u;
|
||||
u >>= 8;
|
||||
}
|
||||
b -= 64;
|
||||
c += 64;
|
||||
if (m) m += 64;
|
||||
}
|
||||
if (b) {
|
||||
crypto_core_chacha20(x,z,k,sigma);
|
||||
FOR(i,b) c[i] = (m?m[i]:0) ^ x[i];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void add1305(u32 *h,const u32 *c)
|
||||
{
|
||||
u32 j,u = 0;
|
||||
FOR(j,17) {
|
||||
u += h[j] + c[j];
|
||||
h[j] = u & 255;
|
||||
u >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
static const u32 minusp[17] = {
|
||||
5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 252
|
||||
} ;
|
||||
|
||||
int crypto_onetimeauth_poly1305(u8 *out,const u8 *m,u32 n,const u8 *k)
|
||||
{
|
||||
u32 s,i,j,u,x[17],r[17],h[17],c[17],g[17];
|
||||
|
||||
FOR(j,17) r[j]=h[j]=0;
|
||||
FOR(j,16) r[j]=k[j];
|
||||
r[3]&=15;
|
||||
r[4]&=252;
|
||||
r[7]&=15;
|
||||
r[8]&=252;
|
||||
r[11]&=15;
|
||||
r[12]&=252;
|
||||
r[15]&=15;
|
||||
|
||||
while (n > 0) {
|
||||
FOR(j,17) c[j] = 0;
|
||||
for (j = 0;(j < 16) && (j < n);++j) c[j] = m[j];
|
||||
c[j] = 1;
|
||||
m += j; n -= j;
|
||||
add1305(h,c);
|
||||
FOR(i,17) {
|
||||
x[i] = 0;
|
||||
for(j=0;j<=i;j++) x[i] += h[j] * r[i - j];
|
||||
for(;j<17;j++) x[i] += h[j] * 320 * r[i + 17 - j];
|
||||
}
|
||||
FOR(i,17) h[i] = x[i];
|
||||
u = 0;
|
||||
FOR(j,16) {
|
||||
u += h[j];
|
||||
h[j] = u & 255;
|
||||
u >>= 8;
|
||||
}
|
||||
u += h[16]; h[16] = u & 3;
|
||||
u = 5 * (u >> 2);
|
||||
FOR(j,16) {
|
||||
u += h[j];
|
||||
h[j] = u & 255;
|
||||
u >>= 8;
|
||||
}
|
||||
u += h[16]; h[16] = u;
|
||||
}
|
||||
|
||||
FOR(j,17) g[j] = h[j];
|
||||
add1305(h,minusp);
|
||||
s = -(h[16] >> 7);
|
||||
FOR(j,17) h[j] ^= s & (g[j] ^ h[j]);
|
||||
|
||||
FOR(j,16) c[j] = k[j + 16];
|
||||
c[16] = 0;
|
||||
add1305(h,c);
|
||||
FOR(j,16) out[j] = h[j];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int crypto_onetimeauth_poly1305_verify(const u8 *h,const u8 *m,u32 n,const u8 *k)
|
||||
{
|
||||
u8 x[16];
|
||||
crypto_onetimeauth_poly1305(x,m,n,k);
|
||||
return crypto_verify_16(h,x);
|
||||
}
|
||||
|
||||
static inline void set25519(gf r, const gf a)
|
||||
{
|
||||
int i;
|
||||
FOR(i,16) r[i]=a[i];
|
||||
}
|
||||
|
||||
//#define MUL38(V) ((v)*38)
|
||||
#define MUL38(V) ((((((V) << 3) + (V)) << 1) + (V)) << 1)
|
||||
|
||||
static void car25519(gf o)
|
||||
{
|
||||
i64 c=0;
|
||||
unsigned i;
|
||||
FOR(i,16) {
|
||||
i64 v=o[i]+c;
|
||||
o[i]=v&0xFFFF;
|
||||
c=v>>16;
|
||||
}
|
||||
while (c) {
|
||||
c=MUL38(c);
|
||||
for(i = 0; c && i < 16; i++) {
|
||||
i64 v=o[i]+c;
|
||||
o[i]=v&0xFFFF;
|
||||
c=v>>16;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void sel25519(gf p,gf q)
|
||||
{
|
||||
gf t;
|
||||
memcpy(t,p,sizeof(t));
|
||||
memcpy(p,q,sizeof(t));
|
||||
memcpy(q,t,sizeof(t));
|
||||
}
|
||||
|
||||
static void pack25519(u8 *o,const gf n)
|
||||
{
|
||||
int i,j,b;
|
||||
gf m,t;
|
||||
FOR(i,16) t[i]=n[i];
|
||||
car25519(t);
|
||||
FOR(j,2) {
|
||||
m[0]=t[0]-0xffed;
|
||||
for(i=1;i<15;i++) {
|
||||
m[i]=t[i]-0xffff-((m[i-1]>>16)&1);
|
||||
m[i-1]&=0xffff;
|
||||
}
|
||||
m[15]=t[15]-0x7fff-((m[14]>>16)&1);
|
||||
b=(m[15]>>16)&1;
|
||||
m[14]&=0xffff;
|
||||
if (!b) sel25519(t,m);
|
||||
}
|
||||
FOR(i,16) {
|
||||
o[2*i]=t[i]&0xff;
|
||||
o[2*i+1]=t[i]>>8;
|
||||
}
|
||||
}
|
||||
|
||||
static int neq25519(const gf a, const gf b)
|
||||
{
|
||||
u8 c[32],d[32];
|
||||
pack25519(c,a);
|
||||
pack25519(d,b);
|
||||
return crypto_verify_32(c,d);
|
||||
}
|
||||
|
||||
static u8 par25519(const gf a)
|
||||
{
|
||||
u8 d[32];
|
||||
pack25519(d,a);
|
||||
return d[0]&1;
|
||||
}
|
||||
|
||||
static void unpack25519(gf o, const u8 *n)
|
||||
{
|
||||
unsigned i;
|
||||
FOR(i,16) o[i]=(i64)(n[2*i]|((u32)n[2*i+1]<<8));
|
||||
o[15]&=0x7fff;
|
||||
}
|
||||
|
||||
static inline void A(gf o,const gf a,const gf b)
|
||||
{
|
||||
unsigned i;
|
||||
FOR(i,16) o[i]=a[i]+b[i];
|
||||
}
|
||||
|
||||
static inline void Z(gf o,const gf a,const gf b)
|
||||
{
|
||||
unsigned i;
|
||||
FOR(i,16) o[i]=a[i]-b[i];
|
||||
}
|
||||
|
||||
static inline void CS(u16 s[16], const gf o)
|
||||
{
|
||||
i64 c=0;
|
||||
unsigned i;
|
||||
FOR(i,16) {
|
||||
i64 v=o[i]+c;
|
||||
s[i]=(u16)v;
|
||||
c=v>>16;
|
||||
}
|
||||
while (c) {
|
||||
c=MUL38(c);
|
||||
for(i = 0; c && i < 16; i++) {
|
||||
i64 v=s[i]+c;
|
||||
s[i]=(u16)v;
|
||||
c=v>>16;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(USE_UNACL_SCALARMULT)
|
||||
|
||||
extern void fe25519_mul(u16 o[16], u16 x[16], u16 y[16]);
|
||||
extern void fe25519_square(u16 o[16], u16 x[16]);
|
||||
|
||||
static void M(gf o, const gf a, const gf b)
|
||||
{
|
||||
unsigned i;
|
||||
u16 as[16];
|
||||
u16 bs[16];
|
||||
u16 os[16];
|
||||
|
||||
CS(as, a);
|
||||
CS(bs, b);
|
||||
|
||||
fe25519_mul(os, as, bs);
|
||||
|
||||
FOR(i,16) o[i] = os[i];
|
||||
}
|
||||
|
||||
static void S(gf o, const gf a)
|
||||
{
|
||||
unsigned i;
|
||||
u16 as[16];
|
||||
u16 os[16];
|
||||
|
||||
CS(as, a);
|
||||
|
||||
fe25519_square(os, as);
|
||||
|
||||
FOR(i,16) o[i] = os[i];
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static void M(gf o,const gf a,const gf b)
|
||||
{
|
||||
unsigned i,j;
|
||||
u16 as[16];
|
||||
u16 bs[16];
|
||||
|
||||
CS(as, a);
|
||||
CS(bs, b);
|
||||
|
||||
i64 t[31],v;
|
||||
FOR(i,31) t[i]=0;
|
||||
i64* pt = &t[15];
|
||||
for (u16* asp = &as[15]; asp >= as; asp--, pt--) {
|
||||
u32 asi = *asp;
|
||||
i64* ppt = pt + 15;
|
||||
for (u16* bsp = &bs[15]; bsp >= bs; bsp--, ppt--) {
|
||||
v=*ppt;
|
||||
v+=(i64)(asi * (u32)*bsp);
|
||||
*ppt=v;
|
||||
}
|
||||
}
|
||||
FOR(i,15) { v=t[i+16]; v=MUL38(v); o[i]=t[i]+v; }
|
||||
o[15]=t[15];
|
||||
}
|
||||
|
||||
static void S(gf o,const gf a)
|
||||
{
|
||||
unsigned i,j;
|
||||
u16 as[16];
|
||||
|
||||
CS(as, a);
|
||||
|
||||
i64 t[31],v;
|
||||
FOR(i,31) t[i]=0;
|
||||
FOR(i,16) {
|
||||
u32 ai = (u32)as[i];
|
||||
t[i<<1]+=(u32)(ai*ai);
|
||||
for(j=i+1;j<16;j++) {
|
||||
v=t[i+j];
|
||||
v+=((i64)(ai*(u32)as[j]))<<1;
|
||||
t[i+j]=v;
|
||||
}
|
||||
}
|
||||
FOR(i,15) { v=t[i+16]; v=MUL38(v); o[i]=t[i]+v; }
|
||||
o[15]=t[15];
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static void inv25519(gf o,const gf i)
|
||||
{
|
||||
gf c;
|
||||
int a;
|
||||
FOR(a,16) c[a]=i[a];
|
||||
for(a=249;a;a--) {
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
}
|
||||
S(c,c);
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
S(c,c);
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
FOR(a,16) o[a]=c[a];
|
||||
}
|
||||
|
||||
static void pow2523(gf o,const gf i)
|
||||
{
|
||||
gf c;
|
||||
int a;
|
||||
FOR(a,16) c[a]=i[a];
|
||||
for(a=249;a;a--) {
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
}
|
||||
S(c,c);
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
FOR(a,16) o[a]=c[a];
|
||||
}
|
||||
|
||||
#if defined(USE_TWEETNACL_SCALARMULT)
|
||||
|
||||
int crypto_scalarmult_curve25519(u8 *q,const u8 *n,const u8 *p)
|
||||
{
|
||||
u8 z[32],r;
|
||||
i64 x[80];
|
||||
int i,j;
|
||||
gf a,b,c,d,e,f;
|
||||
FOR(i,31) z[i]=n[i];
|
||||
z[31]=(n[31]&127)|64;
|
||||
z[0]&=248;
|
||||
unpack25519(x,p);
|
||||
FOR(i,16) {
|
||||
b[i]=x[i];
|
||||
d[i]=a[i]=c[i]=0;
|
||||
}
|
||||
a[0]=d[0]=1;
|
||||
for(i=254;i>=0;--i) {
|
||||
r=(z[i>>3]>>(i&7))&1;
|
||||
if(r) {
|
||||
sel25519(a,b);
|
||||
sel25519(c,d);
|
||||
}
|
||||
A(e,a,c);
|
||||
Z(a,a,c);
|
||||
A(c,b,d);
|
||||
Z(b,b,d);
|
||||
S(d,e);
|
||||
S(f,a);
|
||||
M(a,c,a);
|
||||
M(c,b,e);
|
||||
A(e,a,c);
|
||||
Z(a,a,c);
|
||||
S(b,a);
|
||||
Z(c,d,f);
|
||||
M(a,c,_121665);
|
||||
A(a,a,d);
|
||||
M(c,c,a);
|
||||
M(a,d,f);
|
||||
M(d,b,x);
|
||||
S(b,e);
|
||||
if(r) {
|
||||
sel25519(a,b);
|
||||
sel25519(c,d);
|
||||
}
|
||||
}
|
||||
FOR(i,16) {
|
||||
x[i+16]=a[i];
|
||||
x[i+32]=c[i];
|
||||
x[i+48]=b[i];
|
||||
x[i+64]=d[i];
|
||||
}
|
||||
inv25519(x+32,x+32);
|
||||
M(x+16,x+16,x+32);
|
||||
pack25519(q,x+16);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int crypto_scalarmult_curve25519_base(u8 *q,const u8 *n)
|
||||
{
|
||||
return crypto_scalarmult_curve25519(q,n,_9);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static u64 R(u64 x,int c) { return (x >> c) | (x << (64 - c)); }
|
||||
static u64 Ch(u64 x,u64 y,u64 z) { return (x & y) ^ (~x & z); }
|
||||
static u64 Maj(u64 x,u64 y,u64 z) { return (x & y) ^ (x & z) ^ (y & z); }
|
||||
static u64 Sigma0(u64 x) { return R(x,28) ^ R(x,34) ^ R(x,39); }
|
||||
static u64 Sigma1(u64 x) { return R(x,14) ^ R(x,18) ^ R(x,41); }
|
||||
static u64 sigma0(u64 x) { return R(x, 1) ^ R(x, 8) ^ (x >> 7); }
|
||||
static u64 sigma1(u64 x) { return R(x,19) ^ R(x,61) ^ (x >> 6); }
|
||||
|
||||
static const u64 K[80] =
|
||||
{
|
||||
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
|
||||
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
|
||||
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
|
||||
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
|
||||
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
|
||||
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
|
||||
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
|
||||
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
|
||||
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
|
||||
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
|
||||
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
|
||||
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
|
||||
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
|
||||
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
|
||||
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
|
||||
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
|
||||
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
|
||||
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
|
||||
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
|
||||
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
|
||||
};
|
||||
|
||||
int crypto_hashblocks_sha512(u8 *x,const u8 *m,u64 n)
|
||||
{
|
||||
u64 z[8],b[8],a[8],w[16],t;
|
||||
int i,j;
|
||||
|
||||
FOR(i,8) z[i] = a[i] = dl64(x + 8 * i);
|
||||
|
||||
while (n >= 128) {
|
||||
FOR(i,16) w[i] = dl64(m + 8 * i);
|
||||
|
||||
FOR(i,80) {
|
||||
FOR(j,8) b[j] = a[j];
|
||||
t = a[7] + Sigma1(a[4]) + Ch(a[4],a[5],a[6]) + K[i] + w[i%16];
|
||||
b[7] = t + Sigma0(a[0]) + Maj(a[0],a[1],a[2]);
|
||||
b[3] += t;
|
||||
FOR(j,8) a[(j+1)%8] = b[j];
|
||||
if (i%16 == 15)
|
||||
FOR(j,16)
|
||||
w[j] += w[(j+9)%16] + sigma0(w[(j+1)%16]) + sigma1(w[(j+14)%16]);
|
||||
}
|
||||
|
||||
FOR(i,8) { a[i] += z[i]; z[i] = a[i]; }
|
||||
|
||||
m += 128;
|
||||
n -= 128;
|
||||
}
|
||||
|
||||
FOR(i,8) ts64(x+8*i,z[i]);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
static const u8 iv[64] = {
|
||||
0x6a,0x09,0xe6,0x67,0xf3,0xbc,0xc9,0x08,
|
||||
0xbb,0x67,0xae,0x85,0x84,0xca,0xa7,0x3b,
|
||||
0x3c,0x6e,0xf3,0x72,0xfe,0x94,0xf8,0x2b,
|
||||
0xa5,0x4f,0xf5,0x3a,0x5f,0x1d,0x36,0xf1,
|
||||
0x51,0x0e,0x52,0x7f,0xad,0xe6,0x82,0xd1,
|
||||
0x9b,0x05,0x68,0x8c,0x2b,0x3e,0x6c,0x1f,
|
||||
0x1f,0x83,0xd9,0xab,0xfb,0x41,0xbd,0x6b,
|
||||
0x5b,0xe0,0xcd,0x19,0x13,0x7e,0x21,0x79
|
||||
} ;
|
||||
|
||||
int crypto_hash_sha512(u8 *out,const u8 *m,u64 n)
|
||||
{
|
||||
u8 h[64],x[256];
|
||||
u64 i,b = n;
|
||||
|
||||
FOR(i,64) h[i] = iv[i];
|
||||
|
||||
crypto_hashblocks_sha512(h,m,n);
|
||||
m += n;
|
||||
n &= 127;
|
||||
m -= n;
|
||||
|
||||
FOR(i,256) x[i] = 0;
|
||||
FOR(i,n) x[i] = m[i];
|
||||
x[n] = 128;
|
||||
|
||||
n = 256-128*(n<112);
|
||||
x[n-9] = b >> 61;
|
||||
ts64(x+n-8,b<<3);
|
||||
crypto_hashblocks_sha512(h,x,n);
|
||||
|
||||
FOR(i,64) out[i] = h[i];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void add(gf p[4],gf q[4])
|
||||
{
|
||||
gf a,b,c,d,t,e,f,g,h;
|
||||
|
||||
Z(a, p[1], p[0]);
|
||||
Z(t, q[1], q[0]);
|
||||
M(a, a, t);
|
||||
A(b, p[0], p[1]);
|
||||
A(t, q[0], q[1]);
|
||||
M(b, b, t);
|
||||
M(c, p[3], q[3]);
|
||||
M(c, c, D2);
|
||||
M(d, p[2], q[2]);
|
||||
A(d, d, d);
|
||||
Z(e, b, a);
|
||||
Z(f, d, c);
|
||||
A(g, d, c);
|
||||
A(h, b, a);
|
||||
M(p[0], e, f);
|
||||
M(p[1], h, g);
|
||||
M(p[2], g, f);
|
||||
M(p[3], e, h);
|
||||
}
|
||||
|
||||
static void pack(u8 *r,gf p[4])
|
||||
{
|
||||
gf tx, ty, zi;
|
||||
inv25519(zi, p[2]);
|
||||
M(tx, p[0], zi);
|
||||
M(ty, p[1], zi);
|
||||
pack25519(r, ty);
|
||||
r[31] ^= par25519(tx) << 7;
|
||||
}
|
||||
|
||||
static void scalarmult(gf p[4],gf q[4],const u8 *s)
|
||||
{
|
||||
set25519(p[0],gf0);
|
||||
set25519(p[1],gf1);
|
||||
set25519(p[2],gf1);
|
||||
set25519(p[3],gf0);
|
||||
for (const u8* sp = &s[31]; sp >= s; --sp) {
|
||||
const u8 si = *sp;
|
||||
for (u8 j = 0x80; j; j>>=1) {
|
||||
if (si & j) {
|
||||
add(p,q);
|
||||
add(q,q);
|
||||
} else {
|
||||
add(q,p);
|
||||
add(p,p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void scalarbase(gf p[4],const u8 *s)
|
||||
{
|
||||
gf q[4];
|
||||
set25519(q[0],X);
|
||||
set25519(q[1],Y);
|
||||
set25519(q[2],gf1);
|
||||
M(q[3],X,Y);
|
||||
scalarmult(p,q,s);
|
||||
}
|
||||
|
||||
int crypto_sign_ed25519_keypair(u8 *pk, u8 *sk)
|
||||
{
|
||||
u8 d[64];
|
||||
gf p[4];
|
||||
int i;
|
||||
|
||||
randombytes(sk, 32);
|
||||
crypto_hash_sha512(d, sk, 32);
|
||||
d[0] &= 248;
|
||||
d[31] &= 127;
|
||||
d[31] |= 64;
|
||||
|
||||
scalarbase(p,d);
|
||||
pack(pk,p);
|
||||
|
||||
FOR(i,32) sk[32 + i] = pk[i];
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const u64 L[32] = {0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10};
|
||||
|
||||
static void modL(u8 *r,i64 x[64])
|
||||
{
|
||||
i64 carry,i,j;
|
||||
for (i = 63;i >= 32;--i) {
|
||||
carry = 0;
|
||||
for (j = i - 32;j < i - 12;++j) {
|
||||
x[j] += carry - 16 * x[i] * L[j - (i - 32)];
|
||||
carry = (x[j] + 128) >> 8;
|
||||
x[j] -= carry << 8;
|
||||
}
|
||||
x[j] += carry;
|
||||
x[i] = 0;
|
||||
}
|
||||
carry = 0;
|
||||
FOR(j,32) {
|
||||
x[j] += carry - (x[31] >> 4) * L[j];
|
||||
carry = x[j] >> 8;
|
||||
x[j] &= 255;
|
||||
}
|
||||
FOR(j,32) x[j] -= carry * L[j];
|
||||
FOR(i,32) {
|
||||
x[i+1] += x[i] >> 8;
|
||||
r[i] = x[i] & 255;
|
||||
}
|
||||
}
|
||||
|
||||
static void reduce(u8 *r)
|
||||
{
|
||||
i64 x[64],i;
|
||||
FOR(i,64) x[i] = (u64) r[i];
|
||||
FOR(i,64) r[i] = 0;
|
||||
modL(r,x);
|
||||
}
|
||||
|
||||
int crypto_sign_ed25519(u8 *sm,u64 *smlen,const u8 *m,u64 n,const u8 *sk)
|
||||
{
|
||||
u8 d[64],h[64],r[64];
|
||||
i64 i,j,x[64];
|
||||
gf p[4];
|
||||
|
||||
crypto_hash_sha512(d, sk, 32);
|
||||
d[0] &= 248;
|
||||
d[31] &= 127;
|
||||
d[31] |= 64;
|
||||
|
||||
*smlen = n+64;
|
||||
FOR(i,n) sm[64 + i] = m[i];
|
||||
FOR(i,32) sm[32 + i] = d[32 + i];
|
||||
|
||||
crypto_hash_sha512(r, sm+32, n+32);
|
||||
reduce(r);
|
||||
scalarbase(p,r);
|
||||
pack(sm,p);
|
||||
|
||||
FOR(i,32) sm[i+32] = sk[i+32];
|
||||
crypto_hash_sha512(h,sm,n + 64);
|
||||
reduce(h);
|
||||
|
||||
FOR(i,64) x[i] = 0;
|
||||
FOR(i,32) x[i] = (u64) r[i];
|
||||
FOR(i,32) FOR(j,32) x[i+j] += h[i] * (u64) d[j];
|
||||
modL(sm + 32,x);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int unpackneg(gf r[4],const u8 p[32])
|
||||
{
|
||||
gf t, chk, num, den, den2, den4, den6;
|
||||
set25519(r[2],gf1);
|
||||
unpack25519(r[1],p);
|
||||
S(num,r[1]);
|
||||
M(den,num,D);
|
||||
Z(num,num,r[2]);
|
||||
A(den,r[2],den);
|
||||
|
||||
S(den2,den);
|
||||
S(den4,den2);
|
||||
M(den6,den4,den2);
|
||||
M(t,den6,num);
|
||||
M(t,t,den);
|
||||
|
||||
pow2523(t,t);
|
||||
M(t,t,num);
|
||||
M(t,t,den);
|
||||
M(t,t,den);
|
||||
M(r[0],t,den);
|
||||
|
||||
S(chk,r[0]);
|
||||
M(chk,chk,den);
|
||||
if (neq25519(chk, num)) M(r[0],r[0],I);
|
||||
|
||||
S(chk,r[0]);
|
||||
M(chk,chk,den);
|
||||
if (neq25519(chk, num)) return -1;
|
||||
|
||||
if (par25519(r[0]) == (p[31]>>7)) Z(r[0],gf0,r[0]);
|
||||
|
||||
M(r[3],r[0],r[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int crypto_sign_ed25519_open(u8 *m,u64 *mlen,const u8 *sm,u64 n,const u8 *pk)
|
||||
{
|
||||
int i;
|
||||
u8 t[32],h[64];
|
||||
gf p[4],q[4];
|
||||
|
||||
*mlen = -1;
|
||||
if (n < 64) return -1;
|
||||
|
||||
if (unpackneg(q,pk)) return -1;
|
||||
|
||||
FOR(i,n) m[i] = sm[i];
|
||||
FOR(i,32) m[i+32] = pk[i];
|
||||
crypto_hash_sha512(h,m,n);
|
||||
reduce(h);
|
||||
scalarmult(p,q,h);
|
||||
|
||||
scalarbase(q,sm + 32);
|
||||
add(p,q);
|
||||
pack(t,p);
|
||||
|
||||
n -= 64;
|
||||
if (crypto_verify_32(sm, t)) {
|
||||
FOR(i,n) m[i] = 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
FOR(i,n) m[i] = sm[i + 64];
|
||||
*mlen = n;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* tweetnacl.h
|
||||
*
|
||||
* Created on: Jun 21, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#ifndef HOMEKIT_TWEETNACL_TWEETNACL_H_
|
||||
#define HOMEKIT_TWEETNACL_TWEETNACL_H_
|
||||
|
||||
#define crypto_sign_keypair crypto_sign_ed25519_keypair
|
||||
#define crypto_sign_open crypto_sign_ed25519_open
|
||||
#define crypto_sign crypto_sign_ed25519
|
||||
#define crypto_scalarmult_base crypto_scalarmult_curve25519_base
|
||||
#define crypto_scalarmult crypto_scalarmult_curve25519
|
||||
|
||||
extern int crypto_hashblocks_sha512(unsigned char *,const unsigned char *,unsigned long long);
|
||||
extern int crypto_hash_sha512(unsigned char *,const unsigned char *,unsigned long long);
|
||||
|
||||
extern int crypto_onetimeauth_poly1305(unsigned char *,const unsigned char *,unsigned long,const unsigned char *);
|
||||
extern int crypto_onetimeauth_poly1305_verify(const unsigned char *,const unsigned char *,unsigned long,const unsigned char *);
|
||||
|
||||
extern int crypto_sign_ed25519(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
|
||||
extern int crypto_sign_ed25519_open(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
|
||||
extern int crypto_sign_ed25519_keypair(unsigned char *,unsigned char *);
|
||||
|
||||
extern int crypto_stream_chacha20_xor(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char);
|
||||
|
||||
extern int crypto_verify_16(const unsigned char *,const unsigned char *);
|
||||
extern int crypto_verify_32(const unsigned char *,const unsigned char *);
|
||||
|
||||
#if defined(USE_TWEETNACL_SCALARMULT)
|
||||
extern int crypto_scalarmult_curve25519(unsigned char *,const unsigned char *,const unsigned char *);
|
||||
extern int crypto_scalarmult_curve25519_base(unsigned char *,const unsigned char *);
|
||||
#endif
|
||||
|
||||
#endif /* HOMEKIT_TWEETNACL_TWEETNACL_H_ */
|
||||
Reference in new issue
Block a user