gsasl  2.2.1
crypto.c
Go to the documentation of this file.
1 /* crypto.c --- Simple crypto wrappers for applications.
2  * Copyright (C) 2002-2024 Simon Josefsson
3  *
4  * This file is part of GNU SASL Library.
5  *
6  * GNU SASL Library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GNU SASL Library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License License along with GNU SASL Library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include <config.h>
24 #include "internal.h"
25 #include "mechtools.h"
26 
27 #include "gc.h"
28 
38 int
39 gsasl_nonce (char *data, size_t datalen)
40 {
41  return gc_nonce (data, datalen);
42 }
43 
54 int
55 gsasl_random (char *data, size_t datalen)
56 {
57  return gc_random (data, datalen);
58 }
59 
72 size_t
74 {
75  switch (hash)
76  {
77  case GSASL_HASH_SHA1:
78  return GSASL_HASH_SHA1_SIZE;
79  case GSASL_HASH_SHA256:
81  }
82 
83  return 0;
84 }
85 
103 int
105  const char *salted_password,
106  char *client_key,
107  char *server_key, char *stored_key)
108 {
109  int res;
110  size_t hashlen = gsasl_hash_length (hash);
111 
112  /* ClientKey */
113 #define CLIENT_KEY "Client Key"
114  res = _gsasl_hmac (hash, salted_password, hashlen,
115  CLIENT_KEY, strlen (CLIENT_KEY), client_key);
116  if (res != GSASL_OK)
117  return res;
118 
119  /* StoredKey */
120  res = _gsasl_hash (hash, client_key, hashlen, stored_key);
121  if (res != GSASL_OK)
122  return res;
123 
124  /* ServerKey */
125 #define SERVER_KEY "Server Key"
126  res = _gsasl_hmac (hash, salted_password, hashlen,
127  SERVER_KEY, strlen (SERVER_KEY), server_key);
128  if (res != GSASL_OK)
129  return res;
130 
131  return GSASL_OK;
132 }
133 
155 int
157  const char *password,
158  unsigned int iteration_count,
159  const char *salt,
160  size_t saltlen,
161  char *salted_password,
162  char *client_key,
163  char *server_key, char *stored_key)
164 {
165  int res;
166  char *preppass;
167 
168  res = gsasl_saslprep (password, GSASL_ALLOW_UNASSIGNED, &preppass, NULL);
169  if (res != GSASL_OK)
170  return res;
171 
172  res = _gsasl_pbkdf2 (hash, preppass, strlen (preppass),
173  salt, saltlen, iteration_count, salted_password, 0);
174  free (preppass);
175  if (res != GSASL_OK)
176  return res;
177 
178  return gsasl_scram_secrets_from_salted_password (hash, salted_password,
179  client_key, server_key,
180  stored_key);
181 }
int gsasl_random(char *data, size_t datalen)
Definition: crypto.c:55
size_t gsasl_hash_length(Gsasl_hash hash)
Definition: crypto.c:73
#define SERVER_KEY
int gsasl_scram_secrets_from_salted_password(Gsasl_hash hash, const char *salted_password, char *client_key, char *server_key, char *stored_key)
Definition: crypto.c:104
int gsasl_nonce(char *data, size_t datalen)
Definition: crypto.c:39
int gsasl_scram_secrets_from_password(Gsasl_hash hash, const char *password, unsigned int iteration_count, const char *salt, size_t saltlen, char *salted_password, char *client_key, char *server_key, char *stored_key)
Definition: crypto.c:156
#define CLIENT_KEY
@ GSASL_ALLOW_UNASSIGNED
Definition: gsasl.h:332
Gsasl_hash
Definition: gsasl.h:428
@ GSASL_HASH_SHA1
Definition: gsasl.h:430
@ GSASL_HASH_SHA256
Definition: gsasl.h:431
@ GSASL_OK
Definition: gsasl.h:129
@ GSASL_HASH_SHA1_SIZE
Definition: gsasl.h:450
@ GSASL_HASH_SHA256_SIZE
Definition: gsasl.h:451
_GSASL_API int gsasl_saslprep(const char *in, Gsasl_saslprep_flags flags, char **out, int *stringpreprc)
int _gsasl_hmac(Gsasl_hash hash, const char *key, size_t keylen, const char *in, size_t inlen, char *outhash)
Definition: mechtools.c:329
int _gsasl_pbkdf2(Gsasl_hash hash, const char *password, size_t passwordlen, const char *salt, size_t saltlen, unsigned int c, char *dk, size_t dklen)
Definition: mechtools.c:368
int _gsasl_hash(Gsasl_hash hash, const char *in, size_t inlen, char *outhash)
Definition: mechtools.c:296