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
);
91 key_array_t g_key_array
;
93 bool parse_key(char **pstr
, struct crypto_key_t
*key
)
99 /* CRYPTO_KEY: 32 hex characters
100 * CRYPTO_USBOTP: usbotp(vid:pid) where vid and pid are hex numbers */
105 for(int j
= 0; j
< 16; j
++)
108 if(convxdigit(str
[2 * j
], &a
) || convxdigit(str
[2 * j
+ 1], &b
))
110 key
->u
.key
[j
] = (a
<< 4) | b
;
114 key
->method
= CRYPTO_KEY
;
119 const char *prefix
= "usbotp(";
120 if(strlen(str
) < strlen(prefix
))
122 if(strncmp(str
, prefix
, strlen(prefix
)) != 0)
124 str
+= strlen(prefix
);
126 long vid
= strtol(str
, &str
, 16);
127 if(vid
< 0 || vid
> 0xffff)
132 long pid
= strtol(str
, &str
, 16);
133 if(pid
< 0 || pid
> 0xffff)
138 key
->method
= CRYPTO_USBOTP
;
139 key
->u
.vid_pid
= vid
<< 16 | pid
;
144 void add_keys(key_array_t ka
, int kac
)
146 key_array_t new_ka
= xmalloc((g_nr_keys
+ kac
) * sizeof(struct crypto_key_t
));
147 memcpy(new_ka
, g_key_array
, g_nr_keys
* sizeof(struct crypto_key_t
));
148 memcpy(new_ka
+ g_nr_keys
, ka
, kac
* sizeof(struct crypto_key_t
));
150 g_key_array
= new_ka
;
161 bool add_keys_from_file(const char *key_file
)
164 FILE *fd
= fopen(key_file
, "r");
168 perror("cannot open key file");
171 fseek(fd
, 0, SEEK_END
);
173 fseek(fd
, 0, SEEK_SET
);
174 char *buf
= xmalloc(size
+ 1);
175 if(fread(buf
, 1, size
, fd
) != (size_t)size
)
178 perror("Cannot read key file");
186 printf("Parsing key file '%s'...\n", key_file
);
190 struct crypto_key_t k
;
192 if(!parse_key(&p
, &k
))
195 printf("invalid key file\n");
204 /* request at least one space character before next key, or end of file */
205 if(*p
!= 0 && !isspace(*p
))
208 printf("invalid key file\n");
211 /* skip whitespace */
221 void print_hex(byte
*data
, int len
, bool newline
)
223 for(int i
= 0; i
< len
; i
++)
224 printf("%02X ", data
[i
]);
229 void print_key(struct crypto_key_t
*key
, bool newline
)
234 print_hex(key
->u
.key
, 16, false);
237 printf("USB-OTP(%04x:%04x)", key
->u
.vid_pid
>> 16, key
->u
.vid_pid
& 0xffff);
247 char OFF
[] = { 0x1b, 0x5b, 0x31, 0x3b, '0', '0', 0x6d, '\0' };
249 char GREY
[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '0', 0x6d, '\0' };
250 char RED
[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '1', 0x6d, '\0' };
251 char GREEN
[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '2', 0x6d, '\0' };
252 char YELLOW
[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' };
253 char BLUE
[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' };
255 static bool g_color_enable
= true;
257 void enable_color(bool enable
)
259 g_color_enable
= enable
;
262 void color(color_t c
)
265 printf("%s", (char *)c
);