mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
957c61cbbf
This change upgrades to GCC 12.3 and GNU binutils 2.42. The GNU linker appears to have changed things so that only a single de-duplicated str table is present in the binary, and it gets placed wherever the linker wants, regardless of what the linker script says. To cope with that we need to stop using .ident to embed licenses. As such, this change does significant work to revamp how third party licenses are defined in the codebase, using `.section .notice,"aR",@progbits`. This new GCC 12.3 toolchain has support for GNU indirect functions. It lets us support __target_clones__ for the first time. This is used for optimizing the performance of libc string functions such as strlen and friends so far on x86, by ensuring AVX systems favor a second codepath that uses VEX encoding. It shaves some latency off certain operations. It's a useful feature to have for scientific computing for the reasons explained by the test/libcxx/openmp_test.cc example which compiles for fifteen different microarchitectures. Thanks to the upgrades, it's now also possible to use newer instruction sets, such as AVX512FP16, VNNI. Cosmo now uses the %gs register on x86 by default for TLS. Doing it is helpful for any program that links `cosmo_dlopen()`. Such programs had to recompile their binaries at startup to change the TLS instructions. That's not great, since it means every page in the executable needs to be faulted. The work of rewriting TLS-related x86 opcodes, is moved to fixupobj.com instead. This is great news for MacOS x86 users, since we previously needed to morph the binary every time for that platform but now that's no longer necessary. The only platforms where we need fixup of TLS x86 opcodes at runtime are now Windows, OpenBSD, and NetBSD. On Windows we morph TLS to point deeper into the TIB, based on a TlsAlloc assignment, and on OpenBSD/NetBSD we morph %gs back into %fs since the kernels do not allow us to specify a value for the %gs register. OpenBSD users are now required to use APE Loader to run Cosmo binaries and assimilation is no longer possible. OpenBSD kernel needs to change to allow programs to specify a value for the %gs register, or it needs to stop marking executable pages loaded by the kernel as mimmutable(). This release fixes __constructor__, .ctor, .init_array, and lastly the .preinit_array so they behave the exact same way as glibc. We no longer use hex constants to define math.h symbols like M_PI.
217 lines
9.2 KiB
C
217 lines
9.2 KiB
C
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*-│
|
|
│ vi: set et ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi │
|
|
╚──────────────────────────────────────────────────────────────────────────────╝
|
|
│ │
|
|
│ Argon2 reference source code package - reference C implementations │
|
|
│ │
|
|
│ Copyright 2015 │
|
|
│ Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves │
|
|
│ │
|
|
│ You may use this work under the terms of a Creative Commons CC0 1.0 │
|
|
│ License/Waiver or the Apache Public License 2.0, at your option. The │
|
|
│ terms of these licenses can be found at: │
|
|
│ │
|
|
│ - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0 │
|
|
│ - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0 │
|
|
│ │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/log/libfatal.internal.h"
|
|
#include "third_party/argon2/argon2.h"
|
|
#include "third_party/argon2/blake2-impl.h"
|
|
#include "third_party/argon2/blake2.h"
|
|
#include "third_party/argon2/blamka-round-ref.h"
|
|
#include "third_party/argon2/core.h"
|
|
|
|
/*
|
|
* Argon2 reference source code package - reference C implementations
|
|
*
|
|
* Copyright 2015
|
|
* Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
|
|
*
|
|
* You may use this work under the terms of a Creative Commons CC0 1.0
|
|
* License/Waiver or the Apache Public License 2.0, at your option. The terms of
|
|
* these licenses can be found at:
|
|
*
|
|
* - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0
|
|
* - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* You should have received a copy of both of these licenses along with this
|
|
* software. If not, they may be obtained at the above URLs.
|
|
*/
|
|
|
|
/*
|
|
* i fills a new memory block and optionally XORs the old block over the new one.
|
|
* @next_block must be initialized.
|
|
* @param prev_block Pointer to the previous block
|
|
* @param ref_block Pointer to the reference block
|
|
* @param next_block Pointer to the block to be constructed
|
|
* @param with_xor Whether to XOR into the new block (1) or just overwrite (0)
|
|
* @pre all block pointers must be valid
|
|
*/
|
|
static optimizespeed void fill_block(const block *prev_block,
|
|
const block *ref_block,
|
|
block *next_block,
|
|
int with_xor) {
|
|
block blockR, block_tmp;
|
|
unsigned i;
|
|
|
|
if (with_xor) {
|
|
for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
|
|
block_tmp.v[i] = (blockR.v[i] = prev_block->v[i] ^ ref_block->v[i]) ^ next_block->v[i];
|
|
}
|
|
} else {
|
|
for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
|
|
block_tmp.v[i] = blockR.v[i] = prev_block->v[i] ^ ref_block->v[i];
|
|
}
|
|
}
|
|
|
|
/* Apply Blake2 on columns of 64-bit words: (0,1,...,15) , then
|
|
(16,17,..31)... finally (112,113,...127) */
|
|
for (i = 0; i < 8; ++i) {
|
|
BLAKE2_ROUND_NOMSG(
|
|
blockR.v[16 * i], blockR.v[16 * i + 1], blockR.v[16 * i + 2],
|
|
blockR.v[16 * i + 3], blockR.v[16 * i + 4], blockR.v[16 * i + 5],
|
|
blockR.v[16 * i + 6], blockR.v[16 * i + 7], blockR.v[16 * i + 8],
|
|
blockR.v[16 * i + 9], blockR.v[16 * i + 10], blockR.v[16 * i + 11],
|
|
blockR.v[16 * i + 12], blockR.v[16 * i + 13], blockR.v[16 * i + 14],
|
|
blockR.v[16 * i + 15]);
|
|
}
|
|
|
|
/* Apply Blake2 on rows of 64-bit words: (0,1,16,17,...112,113), then
|
|
(2,3,18,19,...,114,115).. finally (14,15,30,31,...,126,127) */
|
|
for (i = 0; i < 8; i++) {
|
|
BLAKE2_ROUND_NOMSG(
|
|
blockR.v[2 * i], blockR.v[2 * i + 1], blockR.v[2 * i + 16],
|
|
blockR.v[2 * i + 17], blockR.v[2 * i + 32], blockR.v[2 * i + 33],
|
|
blockR.v[2 * i + 48], blockR.v[2 * i + 49], blockR.v[2 * i + 64],
|
|
blockR.v[2 * i + 65], blockR.v[2 * i + 80], blockR.v[2 * i + 81],
|
|
blockR.v[2 * i + 96], blockR.v[2 * i + 97], blockR.v[2 * i + 112],
|
|
blockR.v[2 * i + 113]);
|
|
}
|
|
|
|
for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
|
|
next_block->v[i] = block_tmp.v[i] ^ blockR.v[i];
|
|
}
|
|
}
|
|
|
|
static void next_addresses(block *address_block, block *input_block,
|
|
const block *zero_block) {
|
|
input_block->v[6]++;
|
|
fill_block(zero_block, input_block, address_block, 0);
|
|
fill_block(zero_block, address_block, address_block, 0);
|
|
}
|
|
|
|
/**
|
|
* Function that fills the segment using previous segments also from
|
|
* other threads.
|
|
*
|
|
* @param context current context
|
|
* @param instance Pointer to the current instance
|
|
* @param position Current position
|
|
* @pre all block pointers must be valid
|
|
*/
|
|
void fill_segment(const argon2_instance_t *instance,
|
|
argon2_position_t position) {
|
|
block *ref_block = NULL, *curr_block = NULL;
|
|
block address_block, input_block, zero_block;
|
|
uint64_t pseudo_rand, ref_index, ref_lane;
|
|
uint32_t prev_offset, curr_offset;
|
|
uint32_t starting_index;
|
|
uint32_t i;
|
|
int data_independent_addressing;
|
|
|
|
if (instance == NULL) {
|
|
return;
|
|
}
|
|
|
|
data_independent_addressing =
|
|
(instance->type == Argon2_i) ||
|
|
(instance->type == Argon2_id && (position.pass == 0) &&
|
|
(position.slice < ARGON2_SYNC_POINTS / 2));
|
|
|
|
if (data_independent_addressing) {
|
|
init_block_value(&zero_block, 0);
|
|
init_block_value(&input_block, 0);
|
|
|
|
input_block.v[0] = position.pass;
|
|
input_block.v[1] = position.lane;
|
|
input_block.v[2] = position.slice;
|
|
input_block.v[3] = instance->memory_blocks;
|
|
input_block.v[4] = instance->passes;
|
|
input_block.v[5] = instance->type;
|
|
}
|
|
|
|
starting_index = 0;
|
|
|
|
if ((0 == position.pass) && (0 == position.slice)) {
|
|
starting_index = 2; /* we have already generated the first two blocks */
|
|
|
|
/* Don't forget to generate the first block of addresses: */
|
|
if (data_independent_addressing) {
|
|
next_addresses(&address_block, &input_block, &zero_block);
|
|
}
|
|
}
|
|
|
|
/* Offset of the current block */
|
|
curr_offset = position.lane * instance->lane_length +
|
|
position.slice * instance->segment_length + starting_index;
|
|
|
|
if (0 == curr_offset % instance->lane_length) {
|
|
/* Last block in this lane */
|
|
prev_offset = curr_offset + instance->lane_length - 1;
|
|
} else {
|
|
/* Previous block */
|
|
prev_offset = curr_offset - 1;
|
|
}
|
|
|
|
for (i = starting_index; i < instance->segment_length;
|
|
++i, ++curr_offset, ++prev_offset) {
|
|
/*1.1 Rotating prev_offset if needed */
|
|
if (curr_offset % instance->lane_length == 1) {
|
|
prev_offset = curr_offset - 1;
|
|
}
|
|
|
|
/* 1.2 Computing the index of the reference block */
|
|
/* 1.2.1 Taking pseudo-random value from the previous block */
|
|
if (data_independent_addressing) {
|
|
if (i % ARGON2_ADDRESSES_IN_BLOCK == 0) {
|
|
next_addresses(&address_block, &input_block, &zero_block);
|
|
}
|
|
pseudo_rand = address_block.v[i % ARGON2_ADDRESSES_IN_BLOCK];
|
|
} else {
|
|
pseudo_rand = instance->memory[prev_offset].v[0];
|
|
}
|
|
|
|
/* 1.2.2 Computing the lane of the reference block */
|
|
ref_lane = ((pseudo_rand >> 32)) % instance->lanes;
|
|
|
|
if ((position.pass == 0) && (position.slice == 0)) {
|
|
/* Can not reference other lanes yet */
|
|
ref_lane = position.lane;
|
|
}
|
|
|
|
/* 1.2.3 Computing the number of possible reference block within the
|
|
* lane.
|
|
*/
|
|
position.index = i;
|
|
ref_index = index_alpha(instance, &position, pseudo_rand & 0xFFFFFFFF,
|
|
ref_lane == position.lane);
|
|
|
|
/* 2 Creating a new block */
|
|
ref_block =
|
|
instance->memory + instance->lane_length * ref_lane + ref_index;
|
|
curr_block = instance->memory + curr_offset;
|
|
if (ARGON2_VERSION_10 == instance->version) {
|
|
/* version 1.2.1 and earlier: overwrite, not XOR */
|
|
fill_block(instance->memory + prev_offset, ref_block, curr_block, 0);
|
|
} else {
|
|
if(0 == position.pass) {
|
|
fill_block(instance->memory + prev_offset, ref_block,
|
|
curr_block, 0);
|
|
} else {
|
|
fill_block(instance->memory + prev_offset, ref_block,
|
|
curr_block, 1);
|
|
}
|
|
}
|
|
}
|
|
}
|