"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/lib/crypto_backend/argon2/core.c" (13 Jan 2022, 19968 Bytes) of package /linux/misc/cryptsetup-2.4.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "core.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.4.1_vs_2.4.2.

    1 /*
    2  * Argon2 reference source code package - reference C implementations
    3  *
    4  * Copyright 2015
    5  * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
    6  *
    7  * You may use this work under the terms of a Creative Commons CC0 1.0
    8  * License/Waiver or the Apache Public License 2.0, at your option. The terms of
    9  * these licenses can be found at:
   10  *
   11  * - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0
   12  * - Apache 2.0        : https://www.apache.org/licenses/LICENSE-2.0
   13  *
   14  * You should have received a copy of both of these licenses along with this
   15  * software. If not, they may be obtained at the above URLs.
   16  */
   17 
   18 /*For memory wiping*/
   19 #ifdef _MSC_VER
   20 #include <windows.h>
   21 #include <winbase.h> /* For SecureZeroMemory */
   22 #endif
   23 #if defined __STDC_LIB_EXT1__
   24 #define __STDC_WANT_LIB_EXT1__ 1
   25 #endif
   26 #define VC_GE_2005(version) (version >= 1400)
   27 
   28 #include <stdio.h>
   29 #include <stdlib.h>
   30 #include <string.h>
   31 
   32 #include "core.h"
   33 #include "thread.h"
   34 #include "blake2/blake2.h"
   35 #include "blake2/blake2-impl.h"
   36 
   37 #ifdef GENKAT
   38 #include "genkat.h"
   39 #endif
   40 
   41 #if defined(__clang__)
   42 #if __has_attribute(optnone)
   43 #define NOT_OPTIMIZED __attribute__((optnone))
   44 #endif
   45 #elif defined(__GNUC__)
   46 #define GCC_VERSION                                                            \
   47     (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
   48 #if GCC_VERSION >= 40400
   49 #define NOT_OPTIMIZED __attribute__((optimize("O0")))
   50 #endif
   51 #endif
   52 #ifndef NOT_OPTIMIZED
   53 #define NOT_OPTIMIZED
   54 #endif
   55 
   56 /***************Instance and Position constructors**********/
   57 void init_block_value(block *b, uint8_t in) { memset(b->v, in, sizeof(b->v)); }
   58 
   59 void copy_block(block *dst, const block *src) {
   60     memcpy(dst->v, src->v, sizeof(uint64_t) * ARGON2_QWORDS_IN_BLOCK);
   61 }
   62 
   63 void xor_block(block *dst, const block *src) {
   64     int i;
   65     for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
   66         dst->v[i] ^= src->v[i];
   67     }
   68 }
   69 
   70 static void load_block(block *dst, const void *input) {
   71     unsigned i;
   72     for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
   73         dst->v[i] = load64((const uint8_t *)input + i * sizeof(dst->v[i]));
   74     }
   75 }
   76 
   77 static void store_block(void *output, const block *src) {
   78     unsigned i;
   79     for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
   80         store64((uint8_t *)output + i * sizeof(src->v[i]), src->v[i]);
   81     }
   82 }
   83 
   84 /***************Memory functions*****************/
   85 
   86 int allocate_memory(const argon2_context *context, uint8_t **memory,
   87                     size_t num, size_t size) {
   88     size_t memory_size = num*size;
   89     if (memory == NULL) {
   90         return ARGON2_MEMORY_ALLOCATION_ERROR;
   91     }
   92 
   93     /* 1. Check for multiplication overflow */
   94     if (size != 0 && memory_size / size != num) {
   95         return ARGON2_MEMORY_ALLOCATION_ERROR;
   96     }
   97 
   98     /* 2. Try to allocate with appropriate allocator */
   99     if (context->allocate_cbk) {
  100         (context->allocate_cbk)(memory, memory_size);
  101     } else {
  102         *memory = malloc(memory_size);
  103     }
  104 
  105     if (*memory == NULL) {
  106         return ARGON2_MEMORY_ALLOCATION_ERROR;
  107     }
  108 
  109     return ARGON2_OK;
  110 }
  111 
  112 void free_memory(const argon2_context *context, uint8_t *memory,
  113                  size_t num, size_t size) {
  114     size_t memory_size = num*size;
  115     clear_internal_memory(memory, memory_size);
  116     if (context->free_cbk) {
  117         (context->free_cbk)(memory, memory_size);
  118     } else {
  119         free(memory);
  120     }
  121 }
  122 
  123 #if defined(_MSC_VER) && VC_GE_2005(_MSC_VER)
  124 void secure_wipe_memory(void *v, size_t n) {
  125     SecureZeroMemory(v, n);
  126 }
  127 #elif defined memset_s
  128 void secure_wipe_memory(void *v, size_t n) {
  129     memset_s(v, n, 0, n);
  130 }
  131 #elif defined(HAVE_EXPLICIT_BZERO)
  132 void secure_wipe_memory(void *v, size_t n) {
  133     explicit_bzero(v, n);
  134 }
  135 #else
  136 void NOT_OPTIMIZED secure_wipe_memory(void *v, size_t n) {
  137     static void *(*const volatile memset_sec)(void *, int, size_t) = &memset;
  138     memset_sec(v, 0, n);
  139 }
  140 #endif
  141 
  142 /* Memory clear flag defaults to true. */
  143 int FLAG_clear_internal_memory = 1;
  144 void clear_internal_memory(void *v, size_t n) {
  145   if (FLAG_clear_internal_memory && v) {
  146     secure_wipe_memory(v, n);
  147   }
  148 }
  149 
  150 void finalize(const argon2_context *context, argon2_instance_t *instance) {
  151     if (context != NULL && instance != NULL) {
  152         block blockhash;
  153         uint32_t l;
  154 
  155         copy_block(&blockhash, instance->memory + instance->lane_length - 1);
  156 
  157         /* XOR the last blocks */
  158         for (l = 1; l < instance->lanes; ++l) {
  159             uint32_t last_block_in_lane =
  160                 l * instance->lane_length + (instance->lane_length - 1);
  161             xor_block(&blockhash, instance->memory + last_block_in_lane);
  162         }
  163 
  164         /* Hash the result */
  165         {
  166             uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
  167             store_block(blockhash_bytes, &blockhash);
  168             blake2b_long(context->out, context->outlen, blockhash_bytes,
  169                          ARGON2_BLOCK_SIZE);
  170             /* clear blockhash and blockhash_bytes */
  171             clear_internal_memory(blockhash.v, ARGON2_BLOCK_SIZE);
  172             clear_internal_memory(blockhash_bytes, ARGON2_BLOCK_SIZE);
  173         }
  174 
  175 #ifdef GENKAT
  176         print_tag(context->out, context->outlen);
  177 #endif
  178 
  179         free_memory(context, (uint8_t *)instance->memory,
  180                     instance->memory_blocks, sizeof(block));
  181     }
  182 }
  183 
  184 uint32_t index_alpha(const argon2_instance_t *instance,
  185                      const argon2_position_t *position, uint32_t pseudo_rand,
  186                      int same_lane) {
  187     /*
  188      * Pass 0:
  189      *      This lane : all already finished segments plus already constructed
  190      * blocks in this segment
  191      *      Other lanes : all already finished segments
  192      * Pass 1+:
  193      *      This lane : (SYNC_POINTS - 1) last segments plus already constructed
  194      * blocks in this segment
  195      *      Other lanes : (SYNC_POINTS - 1) last segments
  196      */
  197     uint32_t reference_area_size;
  198     uint64_t relative_position;
  199     uint32_t start_position, absolute_position;
  200 
  201     if (0 == position->pass) {
  202         /* First pass */
  203         if (0 == position->slice) {
  204             /* First slice */
  205             reference_area_size =
  206                 position->index - 1; /* all but the previous */
  207         } else {
  208             if (same_lane) {
  209                 /* The same lane => add current segment */
  210                 reference_area_size =
  211                     position->slice * instance->segment_length +
  212                     position->index - 1;
  213             } else {
  214                 reference_area_size =
  215                     position->slice * instance->segment_length +
  216                     ((position->index == 0) ? (-1) : 0);
  217             }
  218         }
  219     } else {
  220         /* Second pass */
  221         if (same_lane) {
  222             reference_area_size = instance->lane_length -
  223                                   instance->segment_length + position->index -
  224                                   1;
  225         } else {
  226             reference_area_size = instance->lane_length -
  227                                   instance->segment_length +
  228                                   ((position->index == 0) ? (-1) : 0);
  229         }
  230     }
  231 
  232     /* 1.2.4. Mapping pseudo_rand to 0..<reference_area_size-1> and produce
  233      * relative position */
  234     relative_position = pseudo_rand;
  235     relative_position = relative_position * relative_position >> 32;
  236     relative_position = reference_area_size - 1 -
  237                         (reference_area_size * relative_position >> 32);
  238 
  239     /* 1.2.5 Computing starting position */
  240     start_position = 0;
  241 
  242     if (0 != position->pass) {
  243         start_position = (position->slice == ARGON2_SYNC_POINTS - 1)
  244                              ? 0
  245                              : (position->slice + 1) * instance->segment_length;
  246     }
  247 
  248     /* 1.2.6. Computing absolute position */
  249     absolute_position = (start_position + relative_position) %
  250                         instance->lane_length; /* absolute position */
  251     return absolute_position;
  252 }
  253 
  254 /* Single-threaded version for p=1 case */
  255 static int fill_memory_blocks_st(argon2_instance_t *instance) {
  256     uint32_t r, s, l;
  257 
  258     for (r = 0; r < instance->passes; ++r) {
  259         for (s = 0; s < ARGON2_SYNC_POINTS; ++s) {
  260             for (l = 0; l < instance->lanes; ++l) {
  261                 argon2_position_t position = {r, l, (uint8_t)s, 0};
  262                 fill_segment(instance, position);
  263             }
  264         }
  265 #ifdef GENKAT
  266         internal_kat(instance, r); /* Print all memory blocks */
  267 #endif
  268     }
  269     return ARGON2_OK;
  270 }
  271 
  272 #if !defined(ARGON2_NO_THREADS)
  273 
  274 #ifdef _WIN32
  275 static unsigned __stdcall fill_segment_thr(void *thread_data)
  276 #else
  277 static void *fill_segment_thr(void *thread_data)
  278 #endif
  279 {
  280     argon2_thread_data *my_data = thread_data;
  281     fill_segment(my_data->instance_ptr, my_data->pos);
  282     argon2_thread_exit();
  283     return 0;
  284 }
  285 
  286 /* Multi-threaded version for p > 1 case */
  287 static int fill_memory_blocks_mt(argon2_instance_t *instance) {
  288     uint32_t r, s;
  289     argon2_thread_handle_t *thread = NULL;
  290     argon2_thread_data *thr_data = NULL;
  291     int rc = ARGON2_OK;
  292 
  293     /* 1. Allocating space for threads */
  294     thread = calloc(instance->lanes, sizeof(argon2_thread_handle_t));
  295     if (thread == NULL) {
  296         rc = ARGON2_MEMORY_ALLOCATION_ERROR;
  297         goto fail;
  298     }
  299 
  300     thr_data = calloc(instance->lanes, sizeof(argon2_thread_data));
  301     if (thr_data == NULL) {
  302         rc = ARGON2_MEMORY_ALLOCATION_ERROR;
  303         goto fail;
  304     }
  305 
  306     for (r = 0; r < instance->passes; ++r) {
  307         for (s = 0; s < ARGON2_SYNC_POINTS; ++s) {
  308             uint32_t l, ll;
  309 
  310             /* 2. Calling threads */
  311             for (l = 0; l < instance->lanes; ++l) {
  312                 argon2_position_t position;
  313 
  314                 /* 2.1 Join a thread if limit is exceeded */
  315                 if (l >= instance->threads) {
  316                     if (argon2_thread_join(thread[l - instance->threads])) {
  317                         rc = ARGON2_THREAD_FAIL;
  318                         goto fail;
  319                     }
  320                 }
  321 
  322                 /* 2.2 Create thread */
  323                 position.pass = r;
  324                 position.lane = l;
  325                 position.slice = (uint8_t)s;
  326                 position.index = 0;
  327                 thr_data[l].instance_ptr =
  328                     instance; /* preparing the thread input */
  329                 memcpy(&(thr_data[l].pos), &position,
  330                        sizeof(argon2_position_t));
  331                 if (argon2_thread_create(&thread[l], &fill_segment_thr,
  332                                          (void *)&thr_data[l])) {
  333                     /* Wait for already running threads */
  334                     for (ll = 0; ll < l; ++ll)
  335                         argon2_thread_join(thread[ll]);
  336                     rc = ARGON2_THREAD_FAIL;
  337                     goto fail;
  338                 }
  339 
  340                 /* fill_segment(instance, position); */
  341                 /*Non-thread equivalent of the lines above */
  342             }
  343 
  344             /* 3. Joining remaining threads */
  345             for (l = instance->lanes - instance->threads; l < instance->lanes;
  346                  ++l) {
  347                 if (argon2_thread_join(thread[l])) {
  348                     rc = ARGON2_THREAD_FAIL;
  349                     goto fail;
  350                 }
  351             }
  352         }
  353 
  354 #ifdef GENKAT
  355         internal_kat(instance, r); /* Print all memory blocks */
  356 #endif
  357     }
  358 
  359 fail:
  360     if (thread != NULL) {
  361         free(thread);
  362     }
  363     if (thr_data != NULL) {
  364         free(thr_data);
  365     }
  366     return rc;
  367 }
  368 
  369 #endif /* ARGON2_NO_THREADS */
  370 
  371 int fill_memory_blocks(argon2_instance_t *instance) {
  372     if (instance == NULL || instance->lanes == 0) {
  373         return ARGON2_INCORRECT_PARAMETER;
  374     }
  375 #if defined(ARGON2_NO_THREADS)
  376     return fill_memory_blocks_st(instance);
  377 #else
  378     return instance->threads == 1 ?
  379             fill_memory_blocks_st(instance) : fill_memory_blocks_mt(instance);
  380 #endif
  381 }
  382 
  383 int validate_inputs(const argon2_context *context) {
  384     if (NULL == context) {
  385         return ARGON2_INCORRECT_PARAMETER;
  386     }
  387 
  388     if (NULL == context->out) {
  389         return ARGON2_OUTPUT_PTR_NULL;
  390     }
  391 
  392     /* Validate output length */
  393     if (ARGON2_MIN_OUTLEN > context->outlen) {
  394         return ARGON2_OUTPUT_TOO_SHORT;
  395     }
  396 
  397     if (ARGON2_MAX_OUTLEN < context->outlen) {
  398         return ARGON2_OUTPUT_TOO_LONG;
  399     }
  400 
  401     /* Validate password (required param) */
  402     if (NULL == context->pwd) {
  403         if (0 != context->pwdlen) {
  404             return ARGON2_PWD_PTR_MISMATCH;
  405         }
  406     }
  407 #if ARGON2_MIN_PWD_LENGTH > 0 /* cryptsetup: fix gcc warning */
  408     if (ARGON2_MIN_PWD_LENGTH > context->pwdlen) {
  409       return ARGON2_PWD_TOO_SHORT;
  410     }
  411 #endif
  412     if (ARGON2_MAX_PWD_LENGTH < context->pwdlen) {
  413         return ARGON2_PWD_TOO_LONG;
  414     }
  415 
  416     /* Validate salt (required param) */
  417     if (NULL == context->salt) {
  418         if (0 != context->saltlen) {
  419             return ARGON2_SALT_PTR_MISMATCH;
  420         }
  421     }
  422 
  423     if (ARGON2_MIN_SALT_LENGTH > context->saltlen) {
  424         return ARGON2_SALT_TOO_SHORT;
  425     }
  426 
  427     if (ARGON2_MAX_SALT_LENGTH < context->saltlen) {
  428         return ARGON2_SALT_TOO_LONG;
  429     }
  430 
  431     /* Validate secret (optional param) */
  432     if (NULL == context->secret) {
  433         if (0 != context->secretlen) {
  434             return ARGON2_SECRET_PTR_MISMATCH;
  435         }
  436     } else {
  437 #if ARGON2_MIN_SECRET > 0 /* cryptsetup: fix gcc warning */
  438         if (ARGON2_MIN_SECRET > context->secretlen) {
  439             return ARGON2_SECRET_TOO_SHORT;
  440         }
  441 #endif
  442         if (ARGON2_MAX_SECRET < context->secretlen) {
  443             return ARGON2_SECRET_TOO_LONG;
  444         }
  445     }
  446 
  447     /* Validate associated data (optional param) */
  448     if (NULL == context->ad) {
  449         if (0 != context->adlen) {
  450             return ARGON2_AD_PTR_MISMATCH;
  451         }
  452     } else {
  453 #if ARGON2_MIN_AD_LENGTH > 0 /* cryptsetup: fix gcc warning */
  454         if (ARGON2_MIN_AD_LENGTH > context->adlen) {
  455             return ARGON2_AD_TOO_SHORT;
  456         }
  457 #endif
  458         if (ARGON2_MAX_AD_LENGTH < context->adlen) {
  459             return ARGON2_AD_TOO_LONG;
  460         }
  461     }
  462 
  463     /* Validate memory cost */
  464     if (ARGON2_MIN_MEMORY > context->m_cost) {
  465         return ARGON2_MEMORY_TOO_LITTLE;
  466     }
  467 #if 0 /* UINT32_MAX, cryptsetup: fix gcc warning */
  468     if (ARGON2_MAX_MEMORY < context->m_cost) {
  469         return ARGON2_MEMORY_TOO_MUCH;
  470     }
  471 #endif
  472     if (context->m_cost < 8 * context->lanes) {
  473         return ARGON2_MEMORY_TOO_LITTLE;
  474     }
  475 
  476     /* Validate time cost */
  477     if (ARGON2_MIN_TIME > context->t_cost) {
  478         return ARGON2_TIME_TOO_SMALL;
  479     }
  480 
  481     if (ARGON2_MAX_TIME < context->t_cost) {
  482         return ARGON2_TIME_TOO_LARGE;
  483     }
  484 
  485     /* Validate lanes */
  486     if (ARGON2_MIN_LANES > context->lanes) {
  487         return ARGON2_LANES_TOO_FEW;
  488     }
  489 
  490     if (ARGON2_MAX_LANES < context->lanes) {
  491         return ARGON2_LANES_TOO_MANY;
  492     }
  493 
  494     /* Validate threads */
  495     if (ARGON2_MIN_THREADS > context->threads) {
  496         return ARGON2_THREADS_TOO_FEW;
  497     }
  498 
  499     if (ARGON2_MAX_THREADS < context->threads) {
  500         return ARGON2_THREADS_TOO_MANY;
  501     }
  502 
  503     if (NULL != context->allocate_cbk && NULL == context->free_cbk) {
  504         return ARGON2_FREE_MEMORY_CBK_NULL;
  505     }
  506 
  507     if (NULL == context->allocate_cbk && NULL != context->free_cbk) {
  508         return ARGON2_ALLOCATE_MEMORY_CBK_NULL;
  509     }
  510 
  511     return ARGON2_OK;
  512 }
  513 
  514 void fill_first_blocks(uint8_t *blockhash, const argon2_instance_t *instance) {
  515     uint32_t l;
  516     /* Make the first and second block in each lane as G(H0||0||i) or
  517        G(H0||1||i) */
  518     uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
  519     for (l = 0; l < instance->lanes; ++l) {
  520 
  521         store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0);
  522         store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH + 4, l);
  523         blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
  524                      ARGON2_PREHASH_SEED_LENGTH);
  525         load_block(&instance->memory[l * instance->lane_length + 0],
  526                    blockhash_bytes);
  527 
  528         store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 1);
  529         blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
  530                      ARGON2_PREHASH_SEED_LENGTH);
  531         load_block(&instance->memory[l * instance->lane_length + 1],
  532                    blockhash_bytes);
  533     }
  534     clear_internal_memory(blockhash_bytes, ARGON2_BLOCK_SIZE);
  535 }
  536 
  537 void initial_hash(uint8_t *blockhash, argon2_context *context,
  538                   argon2_type type) {
  539     blake2b_state BlakeHash;
  540     uint8_t value[sizeof(uint32_t)];
  541 
  542     if (NULL == context || NULL == blockhash) {
  543         return;
  544     }
  545 
  546     blake2b_init(&BlakeHash, ARGON2_PREHASH_DIGEST_LENGTH);
  547 
  548     store32(&value, context->lanes);
  549     blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  550 
  551     store32(&value, context->outlen);
  552     blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  553 
  554     store32(&value, context->m_cost);
  555     blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  556 
  557     store32(&value, context->t_cost);
  558     blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  559 
  560     store32(&value, context->version);
  561     blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  562 
  563     store32(&value, (uint32_t)type);
  564     blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  565 
  566     store32(&value, context->pwdlen);
  567     blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  568 
  569     if (context->pwd != NULL) {
  570         blake2b_update(&BlakeHash, (const uint8_t *)context->pwd,
  571                        context->pwdlen);
  572 
  573         if (context->flags & ARGON2_FLAG_CLEAR_PASSWORD) {
  574             secure_wipe_memory(context->pwd, context->pwdlen);
  575             context->pwdlen = 0;
  576         }
  577     }
  578 
  579     store32(&value, context->saltlen);
  580     blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  581 
  582     if (context->salt != NULL) {
  583         blake2b_update(&BlakeHash, (const uint8_t *)context->salt,
  584                        context->saltlen);
  585     }
  586 
  587     store32(&value, context->secretlen);
  588     blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  589 
  590     if (context->secret != NULL) {
  591         blake2b_update(&BlakeHash, (const uint8_t *)context->secret,
  592                        context->secretlen);
  593 
  594         if (context->flags & ARGON2_FLAG_CLEAR_SECRET) {
  595             secure_wipe_memory(context->secret, context->secretlen);
  596             context->secretlen = 0;
  597         }
  598     }
  599 
  600     store32(&value, context->adlen);
  601     blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  602 
  603     if (context->ad != NULL) {
  604         blake2b_update(&BlakeHash, (const uint8_t *)context->ad,
  605                        context->adlen);
  606     }
  607 
  608     blake2b_final(&BlakeHash, blockhash, ARGON2_PREHASH_DIGEST_LENGTH);
  609 }
  610 
  611 int initialize(argon2_instance_t *instance, argon2_context *context) {
  612     uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH];
  613     int result = ARGON2_OK;
  614 
  615     if (instance == NULL || context == NULL)
  616         return ARGON2_INCORRECT_PARAMETER;
  617     instance->context_ptr = context;
  618 
  619     /* 1. Memory allocation */
  620     result = allocate_memory(context, (uint8_t **)&(instance->memory),
  621                              instance->memory_blocks, sizeof(block));
  622     if (result != ARGON2_OK) {
  623         return result;
  624     }
  625 
  626     /* 2. Initial hashing */
  627     /* H_0 + 8 extra bytes to produce the first blocks */
  628     /* uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH]; */
  629     /* Hashing all inputs */
  630     initial_hash(blockhash, context, instance->type);
  631     /* Zeroing 8 extra bytes */
  632     clear_internal_memory(blockhash + ARGON2_PREHASH_DIGEST_LENGTH,
  633                           ARGON2_PREHASH_SEED_LENGTH -
  634                               ARGON2_PREHASH_DIGEST_LENGTH);
  635 
  636 #ifdef GENKAT
  637     initial_kat(blockhash, context, instance->type);
  638 #endif
  639 
  640     /* 3. Creating first blocks, we always have at least two blocks in a slice
  641      */
  642     fill_first_blocks(blockhash, instance);
  643     /* Clearing the hash */
  644     clear_internal_memory(blockhash, ARGON2_PREHASH_SEED_LENGTH);
  645 
  646     return ARGON2_OK;
  647 }