1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2010 Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
34 void *memdup(const void *p
, size_t len
)
36 void *cpy
= xmalloc(len
);
41 void generate_random_data(void *buf
, size_t sz
)
44 unsigned char* p
= (unsigned char*)buf
;
49 void *xmalloc(size_t s
)
52 if(!r
) bugp("malloc");
56 int convxdigit(char digit
, byte
*val
)
58 if(digit
>= '0' && digit
<= '9')
63 else if(digit
>= 'A' && digit
<= 'F')
65 *val
= digit
- 'A' + 10;
68 else if(digit
>= 'a' && digit
<= 'f')
70 *val
= digit
- 'a' + 10;
77 /* helper function to augment an array, free old array */
78 void *augment_array(void *arr
, size_t elem_sz
, size_t cnt
, void *aug
, size_t aug_cnt
)
80 void *p
= xmalloc(elem_sz
* (cnt
+ aug_cnt
));
81 memcpy(p
, arr
, elem_sz
* cnt
);
82 memcpy(p
+ elem_sz
* cnt
, aug
, elem_sz
* aug_cnt
);
87 void augment_array_ex(void **arr
, size_t elem_sz
, int *cnt
, int *capacity
,
88 void *aug
, int aug_cnt
)
90 /* if capacity is not large enough, double it */
91 if(*cnt
+ aug_cnt
> *capacity
)
95 while(*cnt
+ aug_cnt
> *capacity
)
97 void *p
= xmalloc(elem_sz
* (*capacity
));
98 memcpy(p
, *arr
, elem_sz
* (*cnt
));
103 memcpy(*arr
+ elem_sz
* (*cnt
), aug
, elem_sz
* aug_cnt
);
111 key_array_t g_key_array
;
113 bool parse_key(char **pstr
, struct crypto_key_t
*key
)
119 /* CRYPTO_KEY: 32 hex characters
120 * CRYPTO_USBOTP: usbotp(vid:pid) where vid and pid are hex numbers */
125 for(int j
= 0; j
< 16; j
++)
128 if(convxdigit(str
[2 * j
], &a
) || convxdigit(str
[2 * j
+ 1], &b
))
130 key
->u
.key
[j
] = (a
<< 4) | b
;
134 key
->method
= CRYPTO_KEY
;
139 const char *prefix
= "usbotp(";
140 if(strlen(str
) < strlen(prefix
))
142 if(strncmp(str
, prefix
, strlen(prefix
)) != 0)
144 str
+= strlen(prefix
);
146 long vid
= strtol(str
, &str
, 16);
147 if(vid
< 0 || vid
> 0xffff)
152 long pid
= strtol(str
, &str
, 16);
153 if(pid
< 0 || pid
> 0xffff)
158 key
->method
= CRYPTO_USBOTP
;
159 key
->u
.vid_pid
= vid
<< 16 | pid
;
164 void add_keys(key_array_t ka
, int kac
)
166 key_array_t new_ka
= xmalloc((g_nr_keys
+ kac
) * sizeof(struct crypto_key_t
));
167 memcpy(new_ka
, g_key_array
, g_nr_keys
* sizeof(struct crypto_key_t
));
168 memcpy(new_ka
+ g_nr_keys
, ka
, kac
* sizeof(struct crypto_key_t
));
170 g_key_array
= new_ka
;
181 bool add_keys_from_file(const char *key_file
)
184 FILE *fd
= fopen(key_file
, "r");
188 perror("cannot open key file");
191 fseek(fd
, 0, SEEK_END
);
193 fseek(fd
, 0, SEEK_SET
);
194 char *buf
= xmalloc(size
+ 1);
195 if(fread(buf
, 1, size
, fd
) != (size_t)size
)
198 perror("Cannot read key file");
206 printf("Parsing key file '%s'...\n", key_file
);
210 struct crypto_key_t k
;
212 if(!parse_key(&p
, &k
))
215 printf("invalid key file\n");
224 /* request at least one space character before next key, or end of file */
225 if(*p
!= 0 && !isspace(*p
))
228 printf("invalid key file\n");
231 /* skip whitespace */
241 void print_hex(byte
*data
, int len
, bool newline
)
243 for(int i
= 0; i
< len
; i
++)
244 printf("%02X ", data
[i
]);
249 void print_key(struct crypto_key_t
*key
, bool newline
)
254 print_hex(key
->u
.key
, 16, false);
257 printf("USB-OTP(%04x:%04x)", key
->u
.vid_pid
>> 16, key
->u
.vid_pid
& 0xffff);
263 print_hex(&key
->u
.xor_key
[0].key
[0], 64, false);
264 print_hex(&key
->u
.xor_key
[1].key
[0], 64, false);
273 char OFF
[] = { 0x1b, 0x5b, 0x31, 0x3b, '0', '0', 0x6d, '\0' };
275 char GREY
[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '0', 0x6d, '\0' };
276 char RED
[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '1', 0x6d, '\0' };
277 char GREEN
[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '2', 0x6d, '\0' };
278 char YELLOW
[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' };
279 char BLUE
[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' };
281 static bool g_color_enable
= true;
283 void enable_color(bool enable
)
285 g_color_enable
= enable
;
288 void color(color_t c
)
291 printf("%s", (char *)c
);