Skip to content

Commit

Permalink
addtl changes to slow-hash and mlocker - these may be unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Shapiro committed Oct 18, 2018
1 parent 387d431 commit 0ad95ab
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 50 deletions.
48 changes: 47 additions & 1 deletion changes when upgrading monero src.txt
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,54 @@ and replace get_page_size with

===========

important for 13-word support:

mnemonics/electrum-words.cpp

- return result.checksum() % crypto::ElectrumWords::seed_length;
+ return result.checksum() % word_list.size();
+ return result.checksum() % word_list.size();


=========


for emscripten: (Note this might not actually make a difference - remove if confirmed not to)

crypto/slow-hash.c

-extern int aesb_single_round(const uint8_t *in, uint8_t*out, const uint8_t *expandedKey);
-extern int aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *expandedKey);
+extern void aesb_single_round(const uint8_t *in, uint8_t*out, const uint8_t *expandedKey);
+extern void aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *expandedKey);

-extern int aesb_single_round(const uint8_t *in, uint8_t*out, const uint8_t *expandedKey);
-extern int aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *expandedKey);
+extern void aesb_single_round(const uint8_t *in, uint8_t*out, const uint8_t *expandedKey);
+extern void aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *expandedKey);


=========

Note: this may not be necessary but might simplify emscripten compilation (remove this entry if confirmed not to)


epee/src/mlocker.cpp

-#define HAVE_MLOCK 1
+// #define HAVE_MLOCK 1



- CRITICAL_REGION_LOCAL(mutex());
- if (page_size == 0)
- page_size = query_page_size();
- return page_size;
+ return 4096;
+ // CRITICAL_REGION_LOCAL(mutex());
+ // if (page_size == 0)
+ // page_size = query_page_size();
+ // return page_size;



and comment out body of other function implementations
8 changes: 4 additions & 4 deletions crypto/slow-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
#define INIT_SIZE_BLK 8
#define INIT_SIZE_BYTE (INIT_SIZE_BLK * AES_BLOCK_SIZE)

extern int aesb_single_round(const uint8_t *in, uint8_t*out, const uint8_t *expandedKey);
extern int aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *expandedKey);
extern void aesb_single_round(const uint8_t *in, uint8_t*out, const uint8_t *expandedKey);
extern void aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *expandedKey);

#define VARIANT1_1(p) \
do if (variant == 1) \
Expand Down Expand Up @@ -1408,8 +1408,8 @@ static void (*const extra_hashes[4])(const void *, size_t, char *) = {
hash_extra_blake, hash_extra_groestl, hash_extra_jh, hash_extra_skein
};

extern int aesb_single_round(const uint8_t *in, uint8_t*out, const uint8_t *expandedKey);
extern int aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *expandedKey);
extern void aesb_single_round(const uint8_t *in, uint8_t*out, const uint8_t *expandedKey);
extern void aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *expandedKey);

static size_t e2i(const uint8_t* a, size_t count) { return (*((uint64_t*)a) / AES_BLOCK_SIZE) & (count - 1); }

Expand Down
93 changes: 48 additions & 45 deletions epee/src/mlocker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#if defined __GNUC__ && !defined _WIN32
#define HAVE_MLOCK 1
// #define HAVE_MLOCK 1
#endif

#include <unistd.h>
Expand Down Expand Up @@ -95,20 +95,21 @@ namespace epee

size_t mlocker::get_page_size()
{
CRITICAL_REGION_LOCAL(mutex());
if (page_size == 0)
page_size = query_page_size();
return page_size;
return 4096;
// CRITICAL_REGION_LOCAL(mutex());
// if (page_size == 0)
// page_size = query_page_size();
// return page_size;
}

mlocker::mlocker(void *ptr, size_t len): ptr(ptr), len(len)
{
lock(ptr, len);
// lock(ptr, len);
}

mlocker::~mlocker()
{
unlock(ptr, len);
// unlock(ptr, len);
}

void mlocker::lock(void *ptr, size_t len)
Expand All @@ -117,66 +118,68 @@ namespace epee
if (page_size == 0)
return;

CRITICAL_REGION_LOCAL(mutex());
const size_t first = ((uintptr_t)ptr) / page_size;
const size_t last = (((uintptr_t)ptr) + len - 1) / page_size;
for (size_t page = first; page <= last; ++page)
lock_page(page);
++num_locked_objects;
// CRITICAL_REGION_LOCAL(mutex());
// const size_t first = ((uintptr_t)ptr) / page_size;
// const size_t last = (((uintptr_t)ptr) + len - 1) / page_size;
// for (size_t page = first; page <= last; ++page)
// lock_page(page);
// ++num_locked_objects;
}

void mlocker::unlock(void *ptr, size_t len)
{
size_t page_size = get_page_size();
if (page_size == 0)
return;
CRITICAL_REGION_LOCAL(mutex());
const size_t first = ((uintptr_t)ptr) / page_size;
const size_t last = (((uintptr_t)ptr) + len - 1) / page_size;
for (size_t page = first; page <= last; ++page)
unlock_page(page);
--num_locked_objects;
// CRITICAL_REGION_LOCAL(mutex());
// const size_t first = ((uintptr_t)ptr) / page_size;
// const size_t last = (((uintptr_t)ptr) + len - 1) / page_size;
// for (size_t page = first; page <= last; ++page)
// unlock_page(page);
// --num_locked_objects;
}

size_t mlocker::get_num_locked_pages()
{
CRITICAL_REGION_LOCAL(mutex());
return map().size();
return 0;
// CRITICAL_REGION_LOCAL(mutex());
// return map().size();
}

size_t mlocker::get_num_locked_objects()
{
CRITICAL_REGION_LOCAL(mutex());
return num_locked_objects;
return 0;
// CRITICAL_REGION_LOCAL(mutex());
// return num_locked_objects;
}

void mlocker::lock_page(size_t page)
{
std::pair<std::map<size_t, unsigned int>::iterator, bool> p = map().insert(std::make_pair(page, 1));
if (p.second)
{
do_lock((void*)(page * page_size), page_size);
}
else
{
++p.first->second;
}
// std::pair<std::map<size_t, unsigned int>::iterator, bool> p = map().insert(std::make_pair(page, 1));
// if (p.second)
// {
// do_lock((void*)(page * page_size), page_size);
// }
// else
// {
// ++p.first->second;
// }
}

void mlocker::unlock_page(size_t page)
{
std::map<size_t, unsigned int>::iterator i = map().find(page);
if (i == map().end())
{
MERROR("Attempt to unlock unlocked page at " << (void*)(page * page_size));
}
else
{
if (!--i->second)
{
map().erase(i);
do_unlock((void*)(page * page_size), page_size);
}
}
// std::map<size_t, unsigned int>::iterator i = map().find(page);
// if (i == map().end())
// {
// MERROR("Attempt to unlock unlocked page at " << (void*)(page * page_size));
// }
// else
// {
// if (!--i->second)
// {
// map().erase(i);
// do_unlock((void*)(page * page_size), page_size);
// }
// }
}
}

0 comments on commit 0ad95ab

Please sign in to comment.