<commit_before>/*
 * Argon2 source code package
 *
 * Written by <NAME> and <NAME>, 2015
 *
 * This work is licensed under a Creative Commons CC0 1.0 License/Waiver.
 *
 * You should have received a copy of the CC0 Public Domain Dedication along
 * with
 * this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

/*For memory wiping*/
#ifdef _MSC_VER
#include <windows.h>
#include <winbase.h> //For SecureZeroMemory
#endif
#if defined __STDC_LIB_EXT1__
#define __STDC_WANT_LIB_EXT1__ 1
#endif
#define VC_GE_2005(version) (version >= 1400)

#include <inttypes.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "argon2.h"
#include "core.h"
#include "kat.h"

#include "thread.h"

#include "blake2/blake2.h"
#include "blake2/blake2-impl.h"

#if defined(__clang__)
#if __has_attribute(optnone)
#define NOT_OPTIMIZED __attribute__((optnone))
#endif
#elif defined(__GNUC__)
#define GCC_VERSION                                                            \
    (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#if GCC_VERSION >= 40400
#define NOT_OPTIMIZED __attribute__((optimize("O0")))
#endif
#endif
#ifndef NOT_OPTIMIZED
#define NOT_OPTIMIZED
#endif

/***************Instance and Position constructors**********/
void init_block_value(block *b, uint8_t in) { memset(b->v, in, sizeof(b->v)); }

void copy_block(block *dst, const block *src) {
    memcpy(dst->v, src->v, sizeof(uint64_t) * ARGON2_WORDS_IN_BLOCK);
}

void xor_block(block *dst, const block *src) {
    int i;

    for (i = 0; i < ARGON2_WORDS_IN_BLOCK; ++i) {
        dst->v[i] ^= src->v[i];
    }
}

/***************Memory allocators*****************/
int allocate_memory(block **memory, uint32_t m_cost) {
    if (memory != NULL) {
        size_t memory_size = sizeof(block) * m_cost;
        if (m_cost != 0 && memory_size / m_cost != sizeof(block)) {
            return ARGON2_MEMORY_ALLOCATION_ERROR;
        }

        *memory = (block *)malloc(memory_size);

        if (!*memory) {
            return ARGON2_MEMORY_ALLOCATION_ERROR;
        }

        return ARGON2_OK;
    } else
        return ARGON2_MEMORY_ALLOCATION_ERROR;
}

void NOT_OPTIMIZED secure_wipe_memory(void *v, size_t n) {
#if defined(_MSC_VER) && VC_GE_2005(_MSC_VER)
    SecureZeroMemory(v, n);
#elif defined memset_s
    memset_s(v, n);
#elif defined(__OpenBSD__)
    explicit_bzero(memory, size);
#else
    static void *(*const volatile memset_sec)(void *, int, size_t) = &memset;
    memset_sec(v, 0, n);
#endif
}

/*********Memory functions*/

void clear_memory(Argon2_instance_t *instance, bool clear) {
    if (instance->memory != NULL && clear) {
        secure_wipe_memory(instance->memory,
                           sizeof(block) * instance->memory_blocks);
    }
}

void free_memory(block *memory) {
    if (memory != NULL) {
        free(memory);
    }
}

void finalize(const Argon2_Context *context, Argon2_instance_t *instance) {
    if (context != NULL && instance != NULL) {
        block blockhash;
        copy_block(&blockhash, instance->memory + instance->lane_length - 1);

        // XOR the last blocks
        for (uint32_t l = 1; l < instance->lanes; ++l) {
            uint32_t last_block_in_lane =
                l * instance->lane_length + (instance->lane_length - 1);
            xor_block(&blockhash, instance->memory + last_block_in_lane);
        }

        // Hash the result
        blake2b_long(context->out, (uint8_t *)blockhash.v, context->outlen,
                     ARGON2_BLOCK_SIZE);
        secure_wipe_memory(blockhash.v,
                           ARGON2_BLOCK_SIZE); // clear the blockhash

        if (context->print) // Shall we print the output tag?
        {
            print_tag(context->out, context->outlen);
        }

        // Clear memory
        clear_memory(instance, context->clear_memory);

        // Deallocate the memory
        if (NULL != context->free_cbk) {
            context->free_cbk((uint8_t *)instance->memory,
                              instance->memory_blocks * sizeof(block));
        } else {
            free_memory(instance->memory);
        }
    }
}

uint32_t index_alpha(const Argon2_instance_t *instance,
                     const Argon2_position_t *position, uint32_t pseudo_rand,
                     bool same_lane) {
    /*
     * Pass 0:
     *      This lane : all already finished segments plus already constructed
     * blocks in this segment
     *      Other lanes : all already finished segments
     * Pass 1+:
     *      This lane : (SYNC_POINTS - 1) last segments plus already constructed
     * blocks in this segment
     *      Other lanes : (SYNC_POINTS - 1) last segments<commit_msg>minor tweaks<commit_after>/*
 * Argon2 source code package
 *
 * Written by <NAME> and <NAME>, 2015
 *
 * This work is licensed under a Creative Commons CC0 1.0 License/Waiver.
 *
 * You should have received a copy of the CC0 Public Domain Dedication along
 * with
 * this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

/*For memory wiping*/
#ifdef _MSC_VER
#include <windows.h>
#include <winbase.h> //For SecureZeroMemory
#endif
#if defined __STDC_LIB_EXT1__
#define __STDC_WANT_LIB_EXT1__ 1
#endif
#define VC_GE_2005(version) (version >= 1400)

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "argon2.h"
#include "core.h"
#include "kat.h"
#include "thread.h"
#include "blake2/blake2.h"
#include "blake2/blake2-impl.h"

#if defined(__clang__)
#if __has_attribute(optnone)
#define NOT_OPTIMIZED __attribute__((optnone))
#endif
#elif defined(__GNUC__)
#define GCC_VERSION                                                            \
    (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#if GCC_VERSION >= 40400
#define NOT_OPTIMIZED __attribute__((optimize("O0")))
#endif
#endif
#ifndef NOT_OPTIMIZED
#define NOT_OPTIMIZED
#endif

/***************Instance and Position constructors**********/
void init_block_value(block *b, uint8_t in) { 
    memset(b->v, in, sizeof(b->v));
}

void copy_block(block *dst, const block *src) {
    memcpy(dst->v, src->v, sizeof(uint64_t) * ARGON2_WORDS_IN_BLOCK);
}

void xor_block(block *dst, const block *src) {
    for (int i = 0; i < ARGON2_WORDS_IN_BLOCK; ++i) {
        dst->v[i] ^= src->v[i];
    }
}

/***************Memory allocators*****************/
int allocate_memory(block **memory, uint32_t m_cost) {
    if (memory != NULL) {
        size_t memory_size = sizeof(block) * m_cost;
        if (m_cost != 0 && memory_size / m_cost != sizeof(block)) {
            return ARGON2_MEMORY_ALLOCATION_ERROR;
        }

        *memory = (block *)malloc(memory_size);

        if (!*memory) {
            return ARGON2_MEMORY_ALLOCATION_ERROR;
        }

        return ARGON2_OK;
    } else
        return ARGON2_MEMORY_ALLOCATION_ERROR;
}

void NOT_OPTIMIZED secure_wipe_memory(void *v, size_t n) {
#if defined(_MSC_VER) && VC_GE_2005(_MSC_VER)
    SecureZeroMemory(v, n);
#elif defined memset_s
    memset_s(v, n);
#elif defined(__OpenBSD__)
    explicit_bzero(memory, size);
#else
    static void *(*const volatile memset_sec)(void *, int, size_t) = &memset;
    memset_sec(v, 0, n);
#endif
}

/*********Memory functions*/

void clear_memory(Argon2_instance_t *instance, bool clear) {
    if (instance->memory != NULL && clear) {
        secure_wipe_memory(instance->memory,
                           sizeof(block) * instance->memory_blocks);
    }
}

void free_memory(block *memory) {
    if (memory != NULL) {
        free(memory);
    }
}

void finalize(const Argon2_Context *context, Argon2_instance_t *instance) {
    if (context != NULL && instance != NULL) {
        block blockhash;
        copy_block(&blockhash, instance->memory + instance->lane_length - 1);

        // XOR the last blocks
        for (uint32_t l = 1; l < instance->lanes; ++l) {
            uint32_t last_block_in_lane =
                l * instance->lane_length + (instance->lane_length - 1);
            xor_block(&blockhash, instance->memory + last_block_in_lane);
        }

        // Hash the result
        blake2b_long(context->out, (uint8_t *)blockhash.v, context->outlen,
                     ARGON2_BLOCK_SIZE);
        secure_wipe_memory(blockhash.v, ARGON2_BLOCK_SIZE); // clear blockhash

        if (context->print) // Shall we print the output tag?
        {
            print_tag(context->out, context->outlen);
        }

        // Clear memory
        clear_memory(instance, context->clear_memory);

        // Deallocate the memory
        if (NULL != context->free_cbk) {
            context->free_cbk((uint8_t *)instance->memory,
                              instance->memory_blocks * sizeof(block));
        } else {
            free_memory(instance->memory);
        }
    }
}

uint32_t index_alpha(const Argon2_instance_t *instance,
                     const Argon2_position_t *position, uint32_t pseudo_rand,
                     bool same_lane) {
    /*
     * Pass 0:
     *      This lane : all already finished segments plus already constructed
     * blocks in this segment
     *      Other lanes : all already finished segments
     * Pass 1+:
     *      This lane : (SYNC_POINTS - 1) last segments plus already constructed
     * blocks in this segment
     *      Other lanes : (SYNC_POINTS - 1) last segments
{'fraction_non_alphanumeric': 0.12761640324647586, 'fraction_numerical': 0.01794105083297736, 'mean_word_length': 2.731075697211155, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4168810', 'n_tokens_mistral': 3216, 'n_tokens_neox': 3053, 'n_words': 738}