0fe82738208b7777353218d17075d3fbb86c5288
[cfe_generate_password.git] / cfe_generate_password.c
1 /*
2   Generate Broadcom CFE seeds and passwords for many popular modem/router devices
3
4   Copyright 2015 TJ <hacker@iam.tj>
5   Licenced on the terms of the GNU General Public Licence version 3
6
7   To build:
8
9     gcc -o cfe_gen_pass cfe_generate_password.c
10
11   Or:
12
13     make
14
15   To use:
16
17     ./cfe_gen_pass [options]
18
19   This tool can generate passwords for use with many devices that contain
20   Broadcom Common Firmware Environment (CFE) bootbase which has a debug mode
21   that is enabled using the "ATEN 1 XXXXXXXX" command, where XXXXXXXX is an
22   eight digit hexadecimal 'password'.
23
24   It is NOT necessary to have the device generate a 'seed' using "ATSE [MODEL-ID]"
25   because this tool can generate the seed from the device's first (base) MAC address
26   *provided* the "ATSE" command has NOT been executed since the device last booted.
27
28   Access to the device's console via a serial UART port is required to enter the password.
29
30   So, for a device with base MAC address (reported by the CFE during boot) E.g:
31
32     CFE version 1.0.38-112.118 for BCM963268 (32bit,SP,BE)
33     ...
34     Base MAC Address                  : ec:43:f6:46:c0:80
35     ...
36     *** Press any key to stop auto run (1 seconds) ***
37     CFE>
38
39   Using this tool do:
40
41     ./cfe_gen_pass -s ec:43:f6:46:c0:80 -p
42
43     MAC address: ec:43:f6:46:c0:80 Timestamp: 000000 Seed: 00000046c080 Password: 10f0a563
44
45   And on the device do:
46
47     CFE> ATEN 1 10f0a563
48     OK
49     *** command status = 0
50
51   The tool can accept a timestamp as 6 hexadecimal characters (useful for testing the algorithm):
52
53     ./cfe_gen_pass -t 0FF020 -s ec:43:f6:46:c0:80 -p
54
55     MAC address: ec:43:f6:46:c0:80 Timestamp: 0FF020 Seed: 0FF02046c080 Password: 110f65a3
56  */
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <stdarg.h>
61 #include <string.h>
62
63 static const float VERSION = 1.0f;
64 static const size_t TIMESTAMP_SIZE = 6;
65 static const size_t SEED_SIZE = 12;
66 static const size_t PASSWORD_SIZE = 8;
67 static const size_t MESSAGE_SIZE = 128;
68
69 static void
70 pr_usage()
71 {
72   fprintf(stderr, "%s\n",
73     "Usage:\n"
74     "  -v                   show version\n"
75     "  -s 00:01:02:03:04:05 create seed from MAC address\n"
76     "  -t 000000            millisecond timestamp since boot\n"
77     "  -p [SEED]            generate password (with optional seed)\n\n"
78     "  E.g. -s 01:02:03:04:05 \n"
79     "       -s 01:02:03:04:05 -p\n"
80     "       -p 000000030405\n"
81   );
82 }
83
84 static void
85 pr_error_exit(unsigned int usage, const char *error, ...)
86 {
87  va_list args;
88  char error_message[MESSAGE_SIZE];
89
90  if (!error) return;
91
92  va_start(args, error);
93  (void) vsnprintf(error_message, MESSAGE_SIZE, error, args);
94  va_end(args);
95  fprintf(stderr, "Error: %s\n", error_message);
96
97  if (usage) pr_usage();
98
99  exit(EXIT_FAILURE);
100 }
101
102 static const unsigned int passwords[8] = {
103   0x10F0A563,
104   0x887852B1,
105   0xC43C2958,
106   0x621E14AC,
107   0x310F0A56,
108   0x1887852B,
109   0x8C43C295,
110   0xC621E14A
111 };
112
113 static unsigned int
114 generate_seed(char *mac, char *timestamp, char *seed)
115 {
116   unsigned int result = 0;
117   if (mac && strlen(mac) == 17) {
118     size_t i;
119     char *mac_ptr = mac + 9;
120     size_t ts_len = strlen(timestamp);
121     for (i = 0; i <= SEED_SIZE; ++i) {
122       /* if no timestamp assume CFE get_time() returned 0 and CFE g_pw_timestamp == 0x00000000 */
123       if (i < 6)
124           seed[i] = ts_len ? timestamp[i] : '0';
125       else {
126         if (*mac_ptr == ':' || *mac_ptr == '-')
127           ++mac_ptr;
128         seed[i] = *mac_ptr++;
129       }
130     }
131    result = 1;
132   } else
133    pr_error_exit(0, "MAC-ADDR should be 17 characters, e.g: 00:01:02:03:04:05");
134
135   return result;
136 }
137
138 static unsigned int
139 generate_pass(char *seed, char *password)
140 {
141   unsigned int result = 0;
142
143   if (seed && strlen(seed) == 12) {
144     unsigned int timestamp, byte, key, pass;
145     timestamp = byte = 0;
146     if(! sscanf(seed, "%06x", &timestamp))
147       pr_error_exit(1, "unable to parse seed's timestamp");
148     if (! sscanf(&seed[10], "%02x", &byte))
149       pr_error_exit(1, "unable to parse seed's MAC address");
150     key = byte & 0x07;
151     pass = (passwords[key] + timestamp) ^ timestamp;
152     snprintf(password, PASSWORD_SIZE + 1,  "%08x", pass);
153     result = 1;
154   } else
155     pr_error_exit(0, "Seed should be %d hex characters", SEED_SIZE);
156
157   return result;
158 }
159
160 int
161 main(int argc, char **argv, char **env)
162 {
163   int result = 0;
164
165   if (argc == 1) {
166     pr_usage();
167   }
168   else {
169     unsigned int arg;
170     char *MAC_ADDR = NULL;
171     char timestamp[TIMESTAMP_SIZE + 1];
172     char seed[SEED_SIZE + 1];
173     char password[PASSWORD_SIZE + 1];
174     unsigned int opt_seed, opt_pass, opt_ts;
175     seed[0] = password[0] = 0;
176     seed[SEED_SIZE] = password[PASSWORD_SIZE] = timestamp[TIMESTAMP_SIZE] = 0;
177     opt_seed = opt_pass = opt_ts = 0;
178     strncpy(timestamp, "000000", TIMESTAMP_SIZE);
179
180     for (arg = 1; arg < (unsigned) argc; ++arg) {
181       size_t arg_len = strlen(argv[arg]);
182
183       if (argv[arg][0] == '-') {
184         switch (argv[arg][1]) {
185           case 's':
186             opt_seed = 1;
187             break;
188           case 'p':
189             opt_pass = 1;
190             break;
191           case 't':
192             opt_ts = 1;
193             break;
194           case 'v':
195             fprintf(stderr, "Version: %0.2f\n", VERSION);
196         }
197       } else if (opt_seed == 1) {
198         MAC_ADDR = argv[arg];
199         ++opt_seed;
200       } else if (opt_pass == 1 && opt_seed == 0) {
201         if (arg_len != SEED_SIZE)
202           pr_error_exit(1, "seed length must be %d characters", SEED_SIZE);
203
204         strncpy(seed, argv[arg], SEED_SIZE);
205         ++opt_pass;
206       } else if (opt_ts == 1) {
207         if (arg_len != TIMESTAMP_SIZE)
208           pr_error_exit(1, "timestamp length must be %d characters", TIMESTAMP_SIZE);
209
210         strncpy(timestamp, argv[arg], TIMESTAMP_SIZE);
211         ++opt_ts;
212       }
213     }
214     if (! opt_seed && ! opt_pass)
215       pr_usage();
216     else if (opt_seed && opt_seed != 2)
217       pr_error_exit(1, "seed requires MAC-ADDRESS");
218     else if (! opt_seed && opt_pass && opt_pass != 2)
219       pr_error_exit(1, "password on its own requires a pre-generated seed");
220     else if (opt_seed && opt_pass && opt_pass != 1)
221       pr_error_exit(1, "generating seed and password; cannot also accept pre-generated seed");
222     else if (opt_ts == 1)
223       pr_error_exit(0, "missing timestamp; assuming 000000");
224
225
226     if (opt_seed)
227       if (! generate_seed(MAC_ADDR, timestamp, seed))
228         pr_error_exit(1, "unable to generate seed; aborting");
229     if (opt_pass)
230       if (! generate_pass(seed, password))
231         pr_error_exit(0, "unable to generate password");
232
233     printf("MAC address: %s Timestamp: %s Seed: %s Password: %s\n", MAC_ADDR, timestamp, seed, password);
234   }
235
236   return result;
237 }
238