ASCON Suite
ascon-pbkdf2-hmac.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/hmac.h>
25 #include <ascon/utility.h>
26 #include "core/ascon-util.h"
27 #include <string.h>
28 
29 /* Implementation of the "F" function from RFC 8018, section 5.2 */
30 static void ascon_pbkdf2_hmac_f
31  (ascon_hmac_state_t *state, unsigned char *T, unsigned char *U,
32  const unsigned char *password, size_t passwordlen,
33  const unsigned char *salt, size_t saltlen,
34  unsigned long count, unsigned long blocknum)
35 {
36  unsigned char b[4];
37  be_store_word32(b, blocknum);
38  ascon_hmac_init(state, password, passwordlen);
39  ascon_hmac_update(state, salt, saltlen);
40  ascon_hmac_update(state, b, sizeof(b));
41  ascon_hmac_finalize(state, password, passwordlen, T);
42  if (count > 1) {
43  ascon_hmac_reinit(state, password, passwordlen);
45  ascon_hmac_finalize(state, password, passwordlen, U);
47  while (count > 2) {
48  ascon_hmac_reinit(state, password, passwordlen);
50  ascon_hmac_finalize(state, password, passwordlen, U);
52  --count;
53  }
54  }
56 }
57 
59  (unsigned char *out, size_t outlen,
60  const unsigned char *password, size_t passwordlen,
61  const unsigned char *salt, size_t saltlen, unsigned long count)
62 {
64  unsigned char U[ASCON_HMAC_SIZE];
65  unsigned long blocknum = 1;
66  while (outlen > 0) {
67  if (outlen >= ASCON_HMAC_SIZE) {
68  ascon_pbkdf2_hmac_f(&state, out, U, password, passwordlen,
69  salt, saltlen, count, blocknum);
70  out += ASCON_HMAC_SIZE;
71  outlen -= ASCON_HMAC_SIZE;
72  } else {
73  unsigned char T[ASCON_HMAC_SIZE];
74  ascon_pbkdf2_hmac_f(&state, T, U, password, passwordlen,
75  salt, saltlen, count, blocknum);
76  memcpy(out, T, outlen);
77  ascon_clean(T, sizeof(T));
78  break;
79  }
80  ++blocknum;
81  }
82  ascon_clean(U, sizeof(U));
83 }
void ascon_pbkdf2_hmac(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 (legacy HMAC version).
#define lw_xor_block(dest, src, len)
Definition: ascon-util.h:183
#define be_store_word32(ptr, x)
Definition: ascon-util.h:75
Hashed Message Authentication Code (HMAC) based on ASCON-HASH.
void ascon_hmac_update(ascon_hmac_state_t *state, const unsigned char *in, size_t inlen)
Updates an incremental ASCON-HMAC state with more input data.
#define ASCON_HMAC_SIZE
Default size of the output for ASCON-HMAC.
Definition: hmac.h:53
void ascon_hmac_reinit(ascon_hmac_state_t *state, const unsigned char *key, size_t keylen)
Re-initializes an incremental HMAC state using ASCON-HASH.
void ascon_hmac_init(ascon_hmac_state_t *state, const unsigned char *key, size_t keylen)
Initializes an incremental HMAC state using ASCON-HASH.
void ascon_hmac_finalize(ascon_hmac_state_t *state, const unsigned char *key, size_t keylen, unsigned char *out)
Finalizes an incremental ASCON-HMAC state.
void ascon_hmac_free(ascon_hmac_state_t *state)
Frees the ASCON-HMAC state and destroys any sensitive material.
Password-based key derivation function based on ASCON.
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
State information for the ASCON-HMAC incremental mode.
Definition: hmac.h:64
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