<commit_before>/*
   TigerKDF common functions between reference and optimized C versions.

   Written in 2014 by <NAME> <<EMAIL>>

   To the extent possible under law, the author(s) have dedicated all copyright
   and related and neighboring rights to this software to the public domain
   worldwide. This software is distributed without any warranty.

   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/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pbkdf2.h"
#include "tigerkdf.h"
#include "tigerkdf-impl.h"

// Print the state.
void printState(char *message, uint32_t state[8]) {
    puts(message);
    for(uint32_t i = 0; i < 8; i++) {
        printf("%u ", state[i]);
    }
    printf("\n");
}

// Print a value out in hex - from Catena.
void printHex(char *message, uint8_t *x, int len) {
    puts(message);
    for(uint32_t i = 0; i < len; i++) {
        if(i != 0 && i % 8 == 0) {
            puts("");
        }
        printf("%02x ", x[i]);
    }
    printf("     %d (octets)\n\n", len);
}

// Prevents compiler optimizing out memset() -- from blake2-impl.h
static inline void secure_zero_memory(void *v, size_t n) {
    volatile uint8_t *p = ( volatile uint8_t * )v;
    while( n-- ) *p++ = 0;
}

// Verify that parameters are valid for password hashing.
static bool verifyParameters(uint32_t hashSize, uint32_t passwordSize, uint32_t saltSize, uint32_t memSize,
        uint32_t multiplies, uint8_t startGarlic, uint8_t stopGarlic, uint32_t dataSize, uint32_t blockSize,
        uint32_t subBlockSize, uint32_t parallelism, uint32_t repetitions) {
    if(subBlockSize == 0) {
        subBlockSize = blockSize;
    }
    if(hashSize > blockSize || hashSize < 12 || (hashSize & 0x3) || passwordSize > 1024 ||
            passwordSize == 0 || saltSize > 1024  || saltSize == 0 || memSize == 0 ||
            memSize > 1 << 30 || multiplies*(uint64_t)blockSize/1024 > 1 << 30 ||
            startGarlic > stopGarlic || stopGarlic > 30 || dataSize > 1024 || blockSize > 1 << 30 ||
            blockSize & 0x1f || subBlockSize > blockSize || subBlockSize & 0x1f ||
            subBlockSize*(blockSize/subBlockSize) != blockSize ||
            ((uint64_t)memSize << 10) < 4*(uint64_t)blockSize*parallelism || parallelism == 0 ||
            parallelism > 1 << 20 || repetitions == 0 || repetitions > 1 << 30) {
        return false;
    }
    uint64_t totalSize = (uint64_t)memSize << (10 + stopGarlic);
    if(totalSize >> (10 + stopGarlic) != memSize || totalSize > 1LL << 50 || totalSize/blockSize > 1 << 30) {
        return false;
    }
    // numSubBlocks has to be a power of 2 so we can use a simple mask to select a random-ish one
    uint32_t numSubBlocks = blockSize/subBlockSize;
    while((numSubBlocks & 1) == 0) {
        numSubBlocks >>= 1;
    }
    if(numSubBlocks != 1) {
        return false;
    }
    return true;
}
<commit_msg>Check that multiplies <= 8<commit_after>/*
   TigerKDF common functions between reference and optimized C versions.

   Written in 2014 by <NAME> <<EMAIL>>

   To the extent possible under law, the author(s) have dedicated all copyright
   and related and neighboring rights to this software to the public domain
   worldwide. This software is distributed without any warranty.

   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/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pbkdf2.h"
#include "tigerkdf.h"
#include "tigerkdf-impl.h"

// Print the state.
void printState(char *message, uint32_t state[8]) {
    puts(message);
    for(uint32_t i = 0; i < 8; i++) {
        printf("%u ", state[i]);
    }
    printf("\n");
}

// Print a value out in hex - from Catena.
void printHex(char *message, uint8_t *x, int len) {
    puts(message);
    for(uint32_t i = 0; i < len; i++) {
        if(i != 0 && i % 8 == 0) {
            puts("");
        }
        printf("%02x ", x[i]);
    }
    printf("     %d (octets)\n\n", len);
}

// Prevents compiler optimizing out memset() -- from blake2-impl.h
static inline void secure_zero_memory(void *v, size_t n) {
    volatile uint8_t *p = ( volatile uint8_t * )v;
    while( n-- ) *p++ = 0;
}

// Verify that parameters are valid for password hashing.
static bool verifyParameters(uint32_t hashSize, uint32_t passwordSize, uint32_t saltSize, uint32_t memSize,
        uint32_t multiplies, uint8_t startGarlic, uint8_t stopGarlic, uint32_t dataSize, uint32_t blockSize,
        uint32_t subBlockSize, uint32_t parallelism, uint32_t repetitions) {
    if(subBlockSize == 0) {
        subBlockSize = blockSize;
    }
    if(hashSize > blockSize || hashSize < 12 || (hashSize & 0x3) || passwordSize > 1024 ||
            passwordSize == 0 || saltSize > 1024  || saltSize == 0 || memSize == 0 ||
            memSize > 1 << 30 || multiplies > 8 || startGarlic > stopGarlic || stopGarlic > 30 ||
            dataSize > 1024 || blockSize > 1 << 30 || blockSize & 0x1f || subBlockSize > blockSize ||
            subBlockSize & 0x1f || subBlockSize*(blockSize/subBlockSize) != blockSize ||
            ((uint64_t)memSize << 10) < 4*(uint64_t)blockSize*parallelism || parallelism == 0 ||
            parallelism > 1 << 20 || repetitions == 0 || repetitions > 1 << 30) {
        return false;
    }
    uint64_t totalSize = (uint64_t)memSize << (10 + stopGarlic);
    if(totalSize >> (10 + stopGarlic) != memSize || totalSize > 1LL << 50 || totalSize/blockSize > 1 << 30) {
        return false;
    }
    // numSubBlocks has to be a power of 2 so we can use a simple mask to select a random-ish one
    uint32_t numSubBlocks = blockSize/subBlockSize;
    while((numSubBlocks & 1) == 0) {
        numSubBlocks >>= 1;
    }
    if(numSubBlocks != 1) {
        return false;
    }
    return true;
}

{'fraction_non_alphanumeric': 0.1267605633802817, 'fraction_numerical': 0.041415157612340714, 'mean_word_length': 2.8483870967741933, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '463167', 'n_tokens_mistral': 2127, 'n_tokens_neox': 1917, 'n_words': 600}