Subversion Repositories MLServ

Rev

Rev 6 | Rev 8 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6 Rev 7
Line 1... Line 1...
1
/* $Id: crypt.c 6 2024-09-25 00:20:21Z nishi $ */
1
/* $Id: crypt.c 7 2024-09-25 00:56:55Z nishi $ */
2
 
2
 
3
#include "cm_crypt.h"
3
#include "cm_crypt.h"
4
 
4
 
5
#include <unistd.h>
5
#include <unistd.h>
6
#include <stdio.h>
6
#include <stdio.h>
Line 9... Line 9...
9
 
9
 
10
#ifdef __linux__
10
#ifdef __linux__
11
#include <crypt.h>
11
#include <crypt.h>
12
#endif
12
#endif
13
 
13
 
14
const char* cm_crypt_methods[] = {"DES", "MD5", "Blowfish", "", "", "SHA-256", "SHA-512"};
14
const char* cm_crypt_methods[] = {"DES",    "MD5", "Blowfish", "", "", "SHA-256", "SHA-512",
-
 
15
#ifdef __NetBSD__
-
 
16
				  "Argon2",
-
 
17
#endif
-
 
18
				  NULL};
15
 
19
 
16
char* cm_crypt_spec(int num, const char* string, const char* salt) {
20
char* cm_crypt_spec(int num, const char* string, const char* salt) {
17
	char* r = NULL;
21
	char* r = NULL;
18
	if(num == C_DES) {
22
	if(num == C_DES) {
19
		r = crypt(string, salt);
23
		r = crypt(string, salt);
20
	} else if(num == C_MD5 || num == C_SHA256 || num == C_SHA512) {
24
	} else if(num == C_MD5 || num == C_SHA256 || num == C_SHA512) {
21
		char* buffer = malloc(3 + strlen(salt) + 1);
25
		char* buffer = malloc(3 + strlen(salt) + 1 + 1);
22
		buffer[3 + strlen(salt)] = 0;
26
		buffer[3 + strlen(salt) + 1] = 0;
23
		sprintf(buffer, "$%d$%s", num, salt);
27
		sprintf(buffer, "$%d$%s$", num, salt);
24
 
28
 
25
		r = crypt(string, buffer);
29
		r = crypt(string, buffer);
26
 
30
 
27
		free(buffer);
31
		free(buffer);
28
	} else if(num == C_BLOWFISH) {
32
	} else if(num == C_BLOWFISH) {
29
		char* buffer = malloc(4 + 3 + strlen(salt) + 1);
33
		char* buffer = malloc(4 + 3 + strlen(salt) + 1 + 1);
30
		buffer[4 + 3 + strlen(salt)] = 0;
34
		buffer[4 + 3 + strlen(salt) + 1] = 0;
31
		sprintf(buffer, "$%da$08$%s", num, salt);
35
		sprintf(buffer, "$%da$08$%s$", num, salt);
-
 
36
 
-
 
37
		r = crypt(string, buffer);
-
 
38
 
-
 
39
		free(buffer);
-
 
40
	} else if(num == C_ARGON2) {
-
 
41
		if(strlen(salt) != 16) return NULL;
-
 
42
		char* buffer = malloc(1 + 8 + 1 + 4 + 1 + 16 + 1 + 1 + 7);
-
 
43
		buffer[1 + 8 + 1 + 4 + 1 + 16 + 1 + 7] = 0;
-
 
44
		sprintf(buffer, "$argon2id$v=19$m=4096$%s$", salt);
32
 
45
 
33
		r = crypt(string, buffer);
46
		r = crypt(string, buffer);
34
 
47
 
35
		free(buffer);
48
		free(buffer);
36
	}
49
	}
Line 43... Line 56...
43
	fflush(stdout);
56
	fflush(stdout);
44
 
57
 
45
	if(cm_crypt_spec(num, "random", "randomrandomrandomrandom") != NULL) {
58
	if(cm_crypt_spec(num, "random", "randomrandomrandomrandom") != NULL) {
46
		printf("works\n");
59
		printf("works\n");
47
		return 1;
60
		return 1;
48
	} else {
-
 
49
		printf("does not work\n");
-
 
50
		return 0;
-
 
51
	}
61
	}
-
 
62
 
-
 
63
#ifdef __NetBSD__
-
 
64
	if(cm_crypt_spec(num, "random", "abcdabcdabcdabcd") != NULL) {
-
 
65
		printf("works\n");
-
 
66
		return 1;
-
 
67
	}
-
 
68
#endif
-
 
69
 
-
 
70
	printf("does not work\n");
-
 
71
 
-
 
72
	return 0;
52
}
73
}
53
 
74
 
54
int cm_chosen_crypt;
75
int cm_chosen_crypt;
55
 
76
 
56
int cm_crypt_init(void) {
77
int cm_crypt_init(void) {