md5_crypt.c
Go to the documentation of this file.
1 /**
2  * @file md5_crypt.c
3  * @brief Unix crypt using MD5
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "kdf/md5_crypt.h"
37 #include "hash/md5.h"
38 
39 //Check crypto library configuration
40 #if (MD5_CRYPT_SUPPORT == ENABLED)
41 
42 //Base64 encoding table
43 static const char_t base64EncTable[64] =
44 {
45  '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
46  'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
47  'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
48  'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
49 };
50 
51 
52 /**
53  * @brief MD5-crypt algorithm
54  * @param[in] password NULL-terminated password
55  * @param[in] salt NULL-terminated salt string
56  * @param[out] output Output string
57  * @param[out] outputLen Length of the output string (optional parameter)
58  * @return Error code
59  **/
60 
61 error_t md5Crypt(const char_t *password, const char_t *salt, char_t *output,
62  size_t *outputLen)
63 {
64  uint_t i;
65  size_t j;
66  size_t n;
67  char_t *p;
68  size_t saltLen;
69  size_t passwordLen;
70  uint8_t digest[MD5_DIGEST_SIZE];
71 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
72  Md5Context *md5Context;
73 #else
74  Md5Context md5Context[2];
75 #endif
76 
77  //Check parameters
78  if(password == NULL || salt == NULL || output == NULL)
80 
81  //Skip the salt prefix, if any
82  if(osStrncmp(salt, "$1$", 3) == 0)
83  {
84  salt += 3;
85  }
86 
87  //Retrieve the length of the salt string
88  saltLen = osStrlen(salt);
89  //The salt string can be up to 16 characters
90  saltLen = MIN(saltLen, MD5_CRYPT_MAX_SALT_LEN);
91 
92  //Retrieve the length of the password string
93  passwordLen = osStrlen(password);
94 
95 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
96  //Allocate a memory buffer to hold the hash contexts
97  md5Context = cryptoAllocMem(2 * sizeof(Md5Context));
98  //Failed to allocate memory?
99  if(md5Context == NULL)
100  return ERROR_OUT_OF_MEMORY;
101 #endif
102 
103  //Start digest A
104  md5Init(&md5Context[0]);
105  //The password string is added to digest A
106  md5Update(&md5Context[0], password, passwordLen);
107  //The salt prefix is added to digest A
108  md5Update(&md5Context[0], "$1$", 3);
109  //The salt string is added to digest A
110  md5Update(&md5Context[0], salt, saltLen);
111 
112  //Start digest B
113  md5Init(&md5Context[1]);
114  //Add the password to digest B
115  md5Update(&md5Context[1], password, passwordLen);
116  //Add the salt string to digest B
117  md5Update(&md5Context[1], salt, saltLen);
118  //Add the password again to digest B
119  md5Update(&md5Context[1], password, passwordLen);
120  //Finish digest B
121  md5Final(&md5Context[1], digest);
122 
123  //For each block of 64 bytes in the password string, add digest B to digest
124  //A. For the remaining N bytes of the password string add the first N bytes
125  //of digest B to digest A
126  for(j = 0; j < passwordLen; j += n)
127  {
128  n = MIN(passwordLen - j, MD5_DIGEST_SIZE);
129  md5Update(&md5Context[0], digest, n);
130  }
131 
132  //Process each bit of the binary representation of the length of the password
133  //string up to and including the highest 1-digit, starting from to lowest bit
134  //position
135  for(n = passwordLen; n > 0; n >>= 1)
136  {
137  //Check the value of the current bit
138  if((n & 1) != 0)
139  {
140  //For a 1-digit add 0 to digest A
141  md5Update(&md5Context[0], "", 1);
142  }
143  else
144  {
145  //For a 0-digit add the first character of the key
146  md5Update(&md5Context[0], password, 1);
147  }
148  }
149 
150  //Finish digest A
151  md5Final(&md5Context[0], digest);
152 
153  //Apply 1000 rounds of calculation
154  for(i = 0; i < MD5_CRYPT_ROUNDS; i++)
155  {
156  //Start digest C
157  md5Init(&md5Context[0]);
158 
159  //Odd or even round?
160  if((i & 1) != 0)
161  {
162  //For odd round numbers add the password
163  md5Update(&md5Context[0], password, passwordLen);
164  }
165  else
166  {
167  //For even round numbers add the last digest
168  md5Update(&md5Context[0], digest, MD5_DIGEST_SIZE);
169  }
170 
171  //Round number not divisible by 3?
172  if(i % 3 != 0)
173  {
174  //For all round numbers not divisible by 3 add the salt
175  md5Update(&md5Context[0], salt, saltLen);
176  }
177 
178  //Round number not divisible by 7?
179  if(i % 7 != 0)
180  {
181  //For all round numbers not divisible by 7 add the password
182  md5Update(&md5Context[0], password, passwordLen);
183  }
184 
185  //Odd or even round?
186  if((i & 1) != 0)
187  {
188  //For odd round numbers add the last digest
189  md5Update(&md5Context[0], digest, MD5_DIGEST_SIZE);
190  }
191  else
192  {
193  //For even round numbers add the password
194  md5Update(&md5Context[0], password, passwordLen);
195  }
196 
197  //Finish intermediate digest
198  md5Final(&md5Context[0], digest);
199  }
200 
201  //The output string is an ASCII string that begins with the salt prefix
202  osStrcpy(output, "$1$");
203  n = 3;
204 
205  //The salt string truncated to 16 characters
206  saltLen = MIN(saltLen, MD5_CRYPT_MAX_SALT_LEN);
207 
208  //Append the salt string
209  osStrncpy(output + n, salt, saltLen);
210  n += saltLen;
211 
212  //Append a '$' character
213  output[n++] = '$';
214 
215  //Append the base-64 encoded final C digest
216  n += md5CryptEncodeBase64(digest, output + n);
217 
218 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
219  //Release hash context
220  cryptoFreeMem(md5Context);
221 #endif
222 
223  //Length of the output string (excluding the terminating NULL)
224  if(outputLen != NULL)
225  {
226  *outputLen = n;
227  }
228 
229  //Successful processing
230  return NO_ERROR;
231 }
232 
233 
234 /**
235  * @brief base-64 encoding algorithm
236  * @param[in] input MD5 digest to encode
237  * @param[out] output NULL-terminated string encoded with base-64 algorithm
238  * @return Length of the base-64 string
239  **/
240 
241 size_t md5CryptEncodeBase64(const uint8_t *input, uint8_t *output)
242 {
243  uint32_t value;
244  uint_t i;
245  uint_t j;
246 
247  //Encode the MD5 digest using base-64
248  for(i = 0, j = 0; i < 5; i++)
249  {
250  //Extract a group of three bytes from the digest
251  value = input[i] << 16;
252  value |= input[i + 6] << 8;
253  value |= (i < 4) ? input[i + 12] : input[5];
254 
255  //Each group produces four characters as output
256  output[j++] = base64EncTable[value & 0x3F];
257  output[j++] = base64EncTable[(value >> 6) & 0x3F];
258  output[j++] = base64EncTable[(value >> 12) & 0x3F];
259  output[j++] = base64EncTable[(value >> 18) & 0x3F];
260  }
261 
262  //For the last group there are not enough bytes left in the digest and
263  //the value zero is used in its place
264  value = input[11];
265 
266  //The last group produces two characters as output
267  output[j++] = base64EncTable[value & 0x3F];
268  output[j++] = base64EncTable[(value >> 6) & 0x3F];
269 
270  //Properly terminate the string with a NULL character
271  output[j] = '\0';
272 
273  //Return the length of the base-64 string
274  return j;
275 }
276 
277 #endif
278 
size_t md5CryptEncodeBase64(const uint8_t *input, uint8_t *output)
base-64 encoding algorithm
Definition: md5_crypt.c:241
uint8_t p
Definition: ndp.h:300
void md5Final(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest.
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
#define MD5_CRYPT_MAX_SALT_LEN
Definition: md5_crypt.h:41
#define osStrlen(s)
Definition: os_port.h:165
void md5Init(Md5Context *context)
Initialize MD5 message digest context.
#define MD5_CRYPT_ROUNDS
Definition: md5_crypt.h:38
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
General definitions for cryptographic algorithms.
MD5 algorithm context.
Definition: md5.h:62
#define MIN(a, b)
Definition: os_port.h:63
#define MD5_DIGEST_SIZE
Definition: md5.h:45
Unix crypt using MD5.
char char_t
Definition: compiler_port.h:48
uint8_t n
MD5 (Message-Digest Algorithm)
#define cryptoFreeMem(p)
Definition: crypto.h:791
#define osStrncpy(s1, s2, length)
Definition: os_port.h:213
uint8_t value[]
Definition: tcp.h:369
#define cryptoAllocMem(size)
Definition: crypto.h:786
#define osStrncmp(s1, s2, length)
Definition: os_port.h:177
unsigned int uint_t
Definition: compiler_port.h:50
#define osStrcpy(s1, s2)
Definition: os_port.h:207
void md5Update(Md5Context *context, const void *data, size_t length)
Update the MD5 context with a portion of the message being hashed.
@ NO_ERROR
Success.
Definition: error.h:44
error_t md5Crypt(const char_t *password, const char_t *salt, char_t *output, size_t *outputLen)
MD5-crypt algorithm.
Definition: md5_crypt.c:61