1 /*
2  * Common code for the OpenBSD-SoftRAID format.
3  *
4  * This file takes replicated but common code, shared between the CPU
5  * and the GPU formats, and places it into one common location.
6  */
7 
8 #include "misc.h"
9 #include "common.h"
10 #include "openbsdsoftraid_common.h"
11 
openbsdsoftraid_valid(char * ciphertext,struct fmt_main * self,int is_cpu)12 int openbsdsoftraid_valid(char* ciphertext, struct fmt_main *self, int is_cpu)
13 {
14 	char *ctcopy;
15 	char *keeptr;
16 	char *p;
17 	int kdf_type;
18 
19 	if (strncmp(ciphertext, FORMAT_TAG, FORMAT_TAG_LEN) != 0)
20 		return 0;
21 
22 	ctcopy = strdup(ciphertext);
23 	keeptr = ctcopy;
24 	ctcopy += FORMAT_TAG_LEN;
25 
26 	if ((p = strtokm(ctcopy, "$")) == NULL)
27 		goto err;
28 	if (!isdec(p)) /* iterations */
29 		goto err;
30 	if ((p = strtokm(NULL, "$")) == NULL)
31 		goto err;
32 	if (strlen(p) != 2 * 128) /* salt */
33 		goto err;
34 	if (!ishexlc(p))
35 		goto err;
36 	if ((p = strtokm(NULL, "$")) == NULL)
37 		goto err;
38 	if (strlen(p) != 2 * 32 * 64) /* masked keys */
39 		goto err;
40 	if (!ishexlc(p))
41 		goto err;
42 	if ((p = strtokm(NULL, "$")) == NULL)
43 		goto err;
44 	if (strlen(p) != 2 * BINARY_SIZE) /* HMAC-SHA1 */
45 		goto err;
46 	if (!ishexlc(p))
47 		goto err;
48 	if ((p = strtokm(NULL, "$")) != NULL) { /* kdf type */
49 		if (strlen(p) != 1)
50 			goto err;
51 		if (!isdec(p))
52 			goto err;
53 		kdf_type = atoi(p);
54 		if (kdf_type != 1 && kdf_type != 3)
55 			goto err;
56 		if (!is_cpu && kdf_type != 1)
57 			goto err;
58 	}
59 
60 	MEM_FREE(keeptr);
61 	return 1;
62 
63 err:
64 	MEM_FREE(keeptr);
65 	return 0;
66 }
67 
openbsdsoftraid_get_salt(char * ciphertext)68 void *openbsdsoftraid_get_salt(char *ciphertext)
69 {
70 	static struct custom_salt cs;
71 	char *ctcopy = strdup(ciphertext);
72 	char *keeptr = ctcopy;
73 	int i;
74 	char *p;
75 
76 	memset(&cs, 0, sizeof(cs));
77 	ctcopy += FORMAT_TAG_LEN;
78 	p = strtokm(ctcopy, "$"); /* iterations */
79 	cs.num_iterations = atoi(p);
80 	p = strtokm(NULL, "$");   /* salt */
81 	for (i = 0; i < OPENBSD_SOFTRAID_SALTLENGTH ; i++)
82 		cs.salt[i] = atoi16[ARCH_INDEX(p[i * 2])] * 16
83 			+ atoi16[ARCH_INDEX(p[i * 2 + 1])];
84 	p = strtokm(NULL, "$");   /* masked keys */
85 	for (i = 0; i < OPENBSD_SOFTRAID_KEYLENGTH * OPENBSD_SOFTRAID_KEYS; i++)
86 		cs.masked_keys[i] = atoi16[ARCH_INDEX(p[i * 2])] * 16
87 			+ atoi16[ARCH_INDEX(p[i * 2 + 1])];
88 	p = strtokm(NULL, "$");   /* binary hash */
89 	p = strtokm(NULL, "$");   /* kdf type */
90 	if (p)
91 		cs.kdf_type = atoi(p);
92 	else
93 		cs.kdf_type = 1;
94 
95 	MEM_FREE(keeptr);
96 
97 	return (void *)&cs;
98 }
99 
openbsdsoftraid_get_binary(char * ciphertext)100 void *openbsdsoftraid_get_binary(char *ciphertext)
101 {
102 	static union {
103 		unsigned char c[BINARY_SIZE];
104 		ARCH_WORD dummy;
105 	} buf;
106 	unsigned char *out = buf.c;
107 	char *p, *cc = NULL;
108 	int i;
109 
110 	p = strrchr(ciphertext, '$') + 1;
111 
112 	if (strlen(p) == 1) { // hack, last field is kdf type
113 		cc = strdup(ciphertext);
114 		cc[strlen(ciphertext) - 2] = 0;
115 		p = strrchr(cc, '$') + 1;
116 
117 	}
118 	for (i = 0; i < BINARY_SIZE; i++) {
119 		out[i] = (atoi16[ARCH_INDEX(*p)] << 4) |
120 			atoi16[ARCH_INDEX(p[1])];
121 		p += 2;
122 	}
123 	if (cc)
124 		MEM_FREE(cc);
125 
126 	return out;
127 }
128 
129 /* Report kdf type as tunable cost */
openbsdsoftraid_get_kdf_type(void * salt)130 unsigned int openbsdsoftraid_get_kdf_type(void *salt)
131 {
132 	return ((struct custom_salt*)salt)->kdf_type;
133 }
134 
135 /* Report iteration count as tunable cost */
openbsdsoftraid_get_iteration_count(void * salt)136 unsigned int openbsdsoftraid_get_iteration_count(void *salt)
137 {
138 	return ((struct custom_salt*)salt)->num_iterations;
139 }
140