1 /*
2 * This file is part of John the Ripper password cracker,
3 * Copyright (c) 2012 magnum
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted.
7 *
8 * There's ABSOLUTELY NO WARRANTY, express or implied.
9 */
10
11 #include "formats.h"
12 #include "johnswap.h"
13 #include "rawSHA256_common.h"
14 #include "misc.h"
15
16 /* ------- Check if the ciphertext if a valid SHA2 hash ------- */
valid_cisco(char * ciphertext)17 static int valid_cisco(char *ciphertext)
18 {
19 char *p, *q;
20
21 p = ciphertext;
22 if (!strncmp(p, CISCO_TAG, CISCO_TAG_LEN))
23 p += CISCO_TAG_LEN;
24
25 q = p;
26 while (atoi64[ARCH_INDEX(*q)] != 0x7F && q - p <= CISCO_CIPHERTEXT_LENGTH)
27 q++;
28 return !*q && q - p == CISCO_CIPHERTEXT_LENGTH;
29 }
30
valid_hex(char * ciphertext)31 static int valid_hex(char *ciphertext)
32 {
33 char *p, *q;
34
35 p = ciphertext;
36 if (!strncmp(p, HEX_TAG, HEX_TAG_LEN))
37 p += HEX_TAG_LEN;
38
39 q = p;
40 while (atoi16[ARCH_INDEX(*q)] != 0x7F && q - p <= HEX_CIPHERTEXT_LENGTH)
41 q++;
42 return !*q && q - p == HEX_CIPHERTEXT_LENGTH;
43 }
44
sha256_common_valid(char * ciphertext,struct fmt_main * self)45 int sha256_common_valid(char *ciphertext, struct fmt_main *self)
46 {
47 return (valid_hex(ciphertext) || valid_cisco(ciphertext));
48 }
49
50 /* ------- Binary ------- */
sha256_common_binary(char * ciphertext)51 void * sha256_common_binary(char *ciphertext)
52 {
53 static unsigned char * out;
54 char *p;
55 int i;
56
57 if (!out) out = mem_calloc_tiny(DIGEST_SIZE, MEM_ALIGN_WORD);
58
59 p = ciphertext + HEX_TAG_LEN;
60 for (i = 0; i < DIGEST_SIZE; i++) {
61 out[i] =
62 (atoi16[ARCH_INDEX(*p)] << 4) |
63 atoi16[ARCH_INDEX(p[1])];
64 p += 2;
65 }
66
67 #ifdef SIMD_COEF_32
68 alter_endianity (out, DIGEST_SIZE);
69 #endif
70 return out;
71 }
72
sha256_common_binary_BE(char * ciphertext)73 void *sha256_common_binary_BE(char *ciphertext)
74 {
75 static unsigned char * out;
76 char *p;
77 int i;
78
79 if (!out) out = mem_calloc_tiny(DIGEST_SIZE, MEM_ALIGN_WORD);
80
81 p = ciphertext + HEX_TAG_LEN;
82 for (i = 0; i < DIGEST_SIZE; i++) {
83 out[i] =
84 (atoi16[ARCH_INDEX(*p)] << 4) |
85 atoi16[ARCH_INDEX(p[1])];
86 p += 2;
87 }
88 alter_endianity (out, DIGEST_SIZE);
89 return out;
90 }
91
92 /* ------- Prepare ------- */
93 /* Convert Cisco hashes to hex ones, so .pot entries are compatible */
sha256_common_prepare(char * split_fields[10],struct fmt_main * self)94 char * sha256_common_prepare(char *split_fields[10], struct fmt_main *self)
95 {
96 static char * out;
97 char *o, *p = split_fields[1];
98
99 if (!out) out = mem_calloc_tiny(HEX_TAG_LEN + HEX_CIPHERTEXT_LENGTH + 1,
100 MEM_ALIGN_WORD);
101
102 if (!valid_cisco(p))
103 return p;
104
105 if (!strncmp(p, CISCO_TAG, CISCO_TAG_LEN))
106 p += CISCO_TAG_LEN;
107
108 strcpy(out, HEX_TAG);
109 o = out + HEX_TAG_LEN;
110
111 while(*p) {
112 unsigned int ch, b;
113
114 // Get 1st byte of input (1st and 2nd)
115 ch = *p++;
116 b = ((atoi64[ch] << 2) & 252) +
117 (atoi64[ARCH_INDEX(*p)] >> 4 & 0x03);
118 *o++ = itoa16[b >> 4];
119 *o++ = itoa16[b & 0x0f];
120
121 // Get 2nd byte of input (2nd and 3rd)
122 ch = *p++;
123 b = ((atoi64[ch] << 4) & 240) +
124 (atoi64[ARCH_INDEX(*p)] >> 2 & 0x0f);
125 *o++ = itoa16[b >> 4];
126 *o++ = itoa16[b & 0x0f];
127
128 if (!p[1])
129 return out;
130
131 // Get 3rd byte of input (3rd and 4th)
132 ch = *p++;
133 b = ((atoi64[ch] << 6) & 192) +
134 (atoi64[ARCH_INDEX(*p++)] & 0x3f);
135 *o++ = itoa16[b >> 4];
136 *o++ = itoa16[b & 0x0f];
137 }
138 error_msg("Error in prepare()");
139 }
140
141 /* ------- Split ------- */
sha256_common_split(char * ciphertext,int index,struct fmt_main * self)142 char * sha256_common_split(char *ciphertext, int index, struct fmt_main *self)
143 {
144 static char * out;
145
146 if (!out) out = mem_calloc_tiny(HEX_TAG_LEN + HEX_CIPHERTEXT_LENGTH + 1,
147 MEM_ALIGN_WORD);
148
149 if (!strncmp(ciphertext, HEX_TAG, HEX_TAG_LEN))
150 ciphertext += HEX_TAG_LEN;
151
152 memcpy(out, HEX_TAG, HEX_TAG_LEN);
153 memcpylwr(out + HEX_TAG_LEN, ciphertext, HEX_CIPHERTEXT_LENGTH + 1);
154 return out;
155 }
156