taskrambler  0.1.9
Web server and task management solution.
hash_pw.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <sys/types.h>
24 
25 #include <openssl/evp.h>
26 #include <openssl/rand.h>
27 
28 #include "class.h"
29 #include "auth/storage.h"
30 #include "utils/memory.h"
31 #include "commons.h"
32 
33 /*
34  * I have to hash the passwords, maybe this will move in
35  * a separate class in future, but now everything is done
36  * here
37  */
38 #define PBKDF2_ITERATIONS 2048
39 
40 /*
41  * base64 decode via openssl...
42  * I do not need this i think, but I keep it...maybe I have
43  * use for it later.
44  *
45 #include <openssl/bio.h>
46 #include <openssl/evp.h>
47 
48 #define B64_SALT "q36MilkD6Ezlt6+G394aPYWrSwAdEhdnK8k="
49 
50 BIO_METHOD * BIO_f_base64(void);
51 
52 void
53 base64decode(char * data) {
54  BIO * bio,
55  * b64;
56  FILE * b64_salt = fmemopen(B64_SALT, sizeof(B64_SALT)-1, "r");
57 
58  b64 = BIO_new(BIO_f_base64());
59  bio = BIO_new_fp(b64_salt, BIO_NOCLOSE);
60  bio = BIO_push(b64, bio);
61 
62  BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
63 
64  if (SALT_SIZE != BIO_read(bio, data, SALT_SIZE)) {
65  return -1;
66  }
67 
68  BIO_free_all(bio);
69  fclose(b64_salt);
70 }
71 */
72 
73 int
75  const char * password,
76  const size_t npassword,
77  unsigned char * hash,
78  unsigned char ** salt)
79 {
80  if (NULL == *salt) {
81  *salt = memCalloc(SALT_SIZE, sizeof(unsigned char));
82  if (0 > RAND_pseudo_bytes(*salt, SALT_SIZE)) {
83  MEM_FREE(*salt);
84  return FALSE;
85  }
86  }
87 
88  if (0 == PKCS5_PBKDF2_HMAC(
89  password,
90  npassword,
91  *salt,
92  SALT_SIZE,
94  EVP_sha512(),
95  HASH_SIZE,
96  hash)) {
97  MEM_FREE(*salt);
98  return FALSE;
99  }
100 
101  return TRUE;
102 }
103 
104 // vim: set ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
#define SALT_SIZE
Definition: auth/storage.h:34
#define FALSE
Definition: commons.h:28
#define TRUE
Definition: commons.h:27
#define PBKDF2_ITERATIONS
Definition: hash_pw.c:38
void * memCalloc(size_t, size_t)
Definition: memory.c:818
int hash_pw(const char *password, const size_t npassword, unsigned char *hash, unsigned char **salt)
Definition: hash_pw.c:74
#define HASH_SIZE
Definition: auth/storage.h:35