Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/scratch_impl.h
/********************************************************************** | /********************************************************************** | ||||
* Copyright (c) 2017 Andrew Poelstra * | * Copyright (c) 2017 Andrew Poelstra * | ||||
* Distributed under the MIT software license, see the accompanying * | * Distributed under the MIT software license, see the accompanying * | ||||
* file COPYING or http://www.opensource.org/licenses/mit-license.php.* | * file COPYING or http://www.opensource.org/licenses/mit-license.php.* | ||||
**********************************************************************/ | **********************************************************************/ | ||||
#ifndef _SECP256K1_SCRATCH_IMPL_H_ | #ifndef _SECP256K1_SCRATCH_IMPL_H_ | ||||
#define _SECP256K1_SCRATCH_IMPL_H_ | #define _SECP256K1_SCRATCH_IMPL_H_ | ||||
#include "util.h" | #include "util.h" | ||||
#include "scratch.h" | #include "scratch.h" | ||||
static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t size) { | static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t size) { | ||||
const size_t base_alloc = ((sizeof(secp256k1_scratch) + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT; | const size_t base_alloc = ROUND_TO_ALIGN(sizeof(secp256k1_scratch)); | ||||
void *alloc = checked_malloc(error_callback, base_alloc + size); | void *alloc = checked_malloc(error_callback, base_alloc + size); | ||||
secp256k1_scratch* ret = (secp256k1_scratch *)alloc; | secp256k1_scratch* ret = (secp256k1_scratch *)alloc; | ||||
if (ret != NULL) { | if (ret != NULL) { | ||||
memset(ret, 0, sizeof(*ret)); | memset(ret, 0, sizeof(*ret)); | ||||
memcpy(ret->magic, "scratch", 8); | memcpy(ret->magic, "scratch", 8); | ||||
ret->data = (void *) ((char *) alloc + base_alloc); | ret->data = (void *) ((char *) alloc + base_alloc); | ||||
ret->max_size = size; | ret->max_size = size; | ||||
} | } | ||||
Show All 32 Lines | static void secp256k1_scratch_apply_checkpoint(const secp256k1_callback* error_callback, secp256k1_scratch* scratch, size_t checkpoint) { | ||||
scratch->alloc_size = checkpoint; | scratch->alloc_size = checkpoint; | ||||
} | } | ||||
static size_t secp256k1_scratch_max_allocation(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch, size_t objects) { | static size_t secp256k1_scratch_max_allocation(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch, size_t objects) { | ||||
if (memcmp(scratch->magic, "scratch", 8) != 0) { | if (memcmp(scratch->magic, "scratch", 8) != 0) { | ||||
secp256k1_callback_call(error_callback, "invalid scratch space"); | secp256k1_callback_call(error_callback, "invalid scratch space"); | ||||
return 0; | return 0; | ||||
} | } | ||||
/* Ensure that multiplication will not wrap around */ | |||||
if (ALIGNMENT > 1 && objects > SIZE_MAX/(ALIGNMENT - 1)) { | |||||
return 0; | |||||
} | |||||
if (scratch->max_size - scratch->alloc_size <= objects * (ALIGNMENT - 1)) { | if (scratch->max_size - scratch->alloc_size <= objects * (ALIGNMENT - 1)) { | ||||
return 0; | return 0; | ||||
} | } | ||||
return scratch->max_size - scratch->alloc_size - objects * (ALIGNMENT - 1); | return scratch->max_size - scratch->alloc_size - objects * (ALIGNMENT - 1); | ||||
} | } | ||||
static void *secp256k1_scratch_alloc(const secp256k1_callback* error_callback, secp256k1_scratch* scratch, size_t size) { | static void *secp256k1_scratch_alloc(const secp256k1_callback* error_callback, secp256k1_scratch* scratch, size_t size) { | ||||
void *ret; | void *ret; | ||||
size = ROUND_TO_ALIGN(size); | size_t rounded_size; | ||||
rounded_size = ROUND_TO_ALIGN(size); | |||||
/* Check that rounding did not wrap around */ | |||||
if (rounded_size < size) { | |||||
return NULL; | |||||
} | |||||
size = rounded_size; | |||||
if (memcmp(scratch->magic, "scratch", 8) != 0) { | if (memcmp(scratch->magic, "scratch", 8) != 0) { | ||||
secp256k1_callback_call(error_callback, "invalid scratch space"); | secp256k1_callback_call(error_callback, "invalid scratch space"); | ||||
return NULL; | return NULL; | ||||
} | } | ||||
if (size > scratch->max_size - scratch->alloc_size) { | if (size > scratch->max_size - scratch->alloc_size) { | ||||
return NULL; | return NULL; | ||||
Show All 9 Lines |