ASCON Suite
ascon-pbkdf2.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include <ascon/pbkdf2.h>
24 #include <ascon/xof.h>
25 #include <ascon/utility.h>
26 #include "core/ascon-util.h"
27 #include "core/ascon-util-snp.h"
28 #include <string.h>
29 
30 /* Determine if we need to explicitly free the XOF state between iterations */
31 #if defined(ASCON_BACKEND_SLICED64) || defined(ASCON_BACKEND_SLICED32) || \
32  defined(ASCON_BACKEND_DIRECT_XOR)
33 #define ASCON_PBKDF2_FREE_STATE 0
34 #else
35 #define ASCON_PBKDF2_FREE_STATE 1
36 #endif
37 
38 /*
39  * Implementation of the "F" function from RFC 8018, section 5.2
40  *
41  * Note: Instead of HMAC like in RFC 8018, we use the following PRF:
42  *
43  * PRF(P, X) = ASCON-cXOF(X, 256, "PBKDF2", P)
44  */
45 static void ascon_pbkdf2_f
46  (ascon_xof_state_t *state, unsigned char *T, unsigned char *U,
47  const unsigned char *salt, size_t saltlen,
48  unsigned long count, unsigned long blocknum)
49 {
50  ascon_xof_state_t state2;
51  unsigned char b[4];
52  be_store_word32(b, blocknum);
53  ascon_xof_copy(&state2, state);
54  ascon_xof_absorb(&state2, salt, saltlen);
55  ascon_xof_absorb(&state2, b, sizeof(b));
57 #if ASCON_PBKDF2_FREE_STATE
58  ascon_xof_free(&state2);
59 #endif
60  if (count > 1) {
61  ascon_xof_copy(&state2, state);
64 #if ASCON_PBKDF2_FREE_STATE
65  ascon_xof_free(&state2);
66 #endif
68  while (count > 2) {
69  ascon_xof_copy(&state2, state);
72  ascon_xof_free(&state2);
74  --count;
75  }
76  }
77 #if !ASCON_PBKDF2_FREE_STATE
78  ascon_xof_free(&state2);
79 #endif
80 }
81 
83  (unsigned char *out, size_t outlen,
84  const unsigned char *password, size_t passwordlen,
85  const unsigned char *salt, size_t saltlen, unsigned long count)
86 {
88  unsigned char U[ASCON_PBKDF2_SIZE];
89  unsigned long blocknum = 1;
91  (&state, "PBKDF2", password, passwordlen, ASCON_PBKDF2_SIZE);
92  while (outlen > 0) {
93  if (outlen >= ASCON_PBKDF2_SIZE) {
94  ascon_pbkdf2_f(&state, out, U, salt, saltlen, count, blocknum);
95  out += ASCON_PBKDF2_SIZE;
96  outlen -= ASCON_PBKDF2_SIZE;
97  } else {
98  unsigned char T[ASCON_PBKDF2_SIZE];
99  ascon_pbkdf2_f(&state, T, U, salt, saltlen, count, blocknum);
100  memcpy(out, T, outlen);
101  ascon_clean(T, sizeof(T));
102  break;
103  }
104  ++blocknum;
105  }
107  ascon_clean(U, sizeof(U));
108 }
void ascon_pbkdf2(unsigned char *out, size_t outlen, const unsigned char *password, size_t passwordlen, const unsigned char *salt, size_t saltlen, unsigned long count)
Derives key material using ASCON-PBKDF2.
Definition: ascon-pbkdf2.c:83
#define lw_xor_block(dest, src, len)
Definition: ascon-util.h:183
#define be_store_word32(ptr, x)
Definition: ascon-util.h:75
Password-based key derivation function based on ASCON.
#define ASCON_PBKDF2_SIZE
Default output block size for ASCON-PBKDF2. Key material is generated in blocks of this size.
Definition: pbkdf2.h:43
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
State information for ASCON-XOF incremental mode.
Definition: xof.h:61
System utilities of use to applications that use ASCON.
void ascon_clean(void *buf, unsigned size)
Cleans a buffer that contains sensitive material.
Definition: ascon-clean.c:38
ASCON-XOF and ASCON-XOFA extensible output functions (XOF's).
void ascon_xof_free(ascon_xof_state_t *state)
Frees the ASCON-XOF state and destroys any sensitive material.
Definition: ascon-xof.c:218
void ascon_xof_absorb(ascon_xof_state_t *state, const unsigned char *in, size_t inlen)
Absorbs more input data into an ASCON-XOF state.
Definition: ascon-xof.c:229
void ascon_xof_init_custom(ascon_xof_state_t *state, const char *function_name, const unsigned char *custom, size_t customlen, size_t outlen)
Initializes the state for an incremental ASCON-XOF operation, with a named function,...
Definition: ascon-xof.c:146
void ascon_xof_copy(ascon_xof_state_t *dest, const ascon_xof_state_t *src)
Clones a copy of an ASCON-XOF state.
Definition: ascon-xof.c:344
void ascon_xof_squeeze(ascon_xof_state_t *state, unsigned char *out, size_t outlen)
Squeezes output data from an ASCON-XOF state.
Definition: ascon-xof.c:279