Lightweight Cryptography Primitives
 All Data Structures Files Functions Variables Typedefs Macros Pages
internal-pbkdf2.h
1 /*
2  * Copyright (C) 2021 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 /*
24  * The contents of this header file expand out to the full implementation of
25  * PBKDF2 for a specific underlying hash algorithm. We expect a number of
26  * macros to be defined before this file is included to configure the
27  * underlying PBKDF2 variant:
28  *
29  * PBKDF2_ALG_NAME Name of the PBKDF2 algorithm; e.g. ascon_pbkdf2
30  * PBKDF2_HMAC_SIZE Size of the output for the HMAC algorithm.
31  * PBKDF2_HMAC_STATE Type for the HMAC state; e.g. ascon_hmac_state_t
32  * PBKDF2_HMAC_INIT Name of the HMAC initialization function.
33  * PBKDF2_HMAC_UPDATE Name of the HMAC update function.
34  * PBKDF2_HMAC_FINALIZE Name of the HMAC finalization function.
35  */
36 #if defined(PBKDF2_ALG_NAME)
37 
38 #define PBKDF2_CONCAT_INNER(name,suffix) name##suffix
39 #define PBKDF2_CONCAT(name,suffix) PBKDF2_CONCAT_INNER(name,suffix)
40 
41 /* Implementation of the "F" function from RFC 8018, section 5.2 */
42 static void PBKDF2_CONCAT(PBKDF2_ALG_NAME,_f)
43  (PBKDF2_HMAC_STATE *state, unsigned char *T, unsigned char *U,
44  const unsigned char *password, size_t passwordlen,
45  const unsigned char *salt, size_t saltlen,
46  unsigned long count, unsigned long blocknum)
47 {
48  unsigned char b[4];
49  be_store_word32(b, blocknum);
50  PBKDF2_HMAC_INIT(state, password, passwordlen);
51  PBKDF2_HMAC_UPDATE(state, salt, saltlen);
52  PBKDF2_HMAC_UPDATE(state, b, sizeof(b));
53  PBKDF2_HMAC_FINALIZE(state, password, passwordlen, T);
54  if (count > 1) {
55  PBKDF2_HMAC_INIT(state, password, passwordlen);
56  PBKDF2_HMAC_UPDATE(state, T, PBKDF2_HMAC_SIZE);
57  PBKDF2_HMAC_FINALIZE(state, password, passwordlen, U);
58  lw_xor_block(T, U, PBKDF2_HMAC_SIZE);
59  while (count > 2) {
60  PBKDF2_HMAC_INIT(state, password, passwordlen);
61  PBKDF2_HMAC_UPDATE(state, U, PBKDF2_HMAC_SIZE);
62  PBKDF2_HMAC_FINALIZE(state, password, passwordlen, U);
63  lw_xor_block(T, U, PBKDF2_HMAC_SIZE);
64  --count;
65  }
66  }
67 }
68 
69 void PBKDF2_ALG_NAME
70  (unsigned char *out, size_t outlen,
71  const unsigned char *password, size_t passwordlen,
72  const unsigned char *salt, size_t saltlen, unsigned long count)
73 {
74  PBKDF2_HMAC_STATE state;
75  unsigned char U[PBKDF2_HMAC_SIZE];
76  unsigned long blocknum = 1;
77  while (outlen > 0) {
78  if (outlen >= PBKDF2_HMAC_SIZE) {
79  PBKDF2_CONCAT(PBKDF2_ALG_NAME,_f)
80  (&state, out, U, password, passwordlen,
81  salt, saltlen, count, blocknum);
82  out += PBKDF2_HMAC_SIZE;
83  outlen -= PBKDF2_HMAC_SIZE;
84  } else {
85  unsigned char T[PBKDF2_HMAC_SIZE];
86  PBKDF2_CONCAT(PBKDF2_ALG_NAME,_f)
87  (&state, T, U, password, passwordlen,
88  salt, saltlen, count, blocknum);
89  memcpy(out, T, outlen);
90  aead_clean(T, sizeof(T));
91  break;
92  }
93  ++blocknum;
94  }
95  aead_clean(&state, sizeof(state));
96  aead_clean(U, sizeof(U));
97 }
98 
99 #endif /* PBKDF2_ALG_NAME */
100 
101 /* Now undefine everything so that we can include this file again for
102  * another variant on the PBKDF2 algorithm */
103 #undef PBKDF2_ALG_NAME
104 #undef PBKDF2_HMAC_SIZE
105 #undef PBKDF2_HMAC_STATE
106 #undef PBKDF2_HMAC_INIT
107 #undef PBKDF2_HMAC_UPDATE
108 #undef PBKDF2_HMAC_FINALIZE