File Makefile.in changed (mode: 100644) (index fd1e2fa..56db202) |
... |
... |
nf2fac: nf2fac.c $(OBJS) totp.h key.h protocol.h util.h conf.h $(DEP) |
34 |
34 |
|
|
35 |
35 |
.PHONY: clean |
.PHONY: clean |
36 |
36 |
clean: |
clean: |
37 |
|
@rm -fv $(TARGETS) $(OBJS) |
|
|
37 |
|
@rm -fv test $(TARGETS) $(OBJS) |
38 |
38 |
@-rm -f $(PRJ)-*.rpm $(PRJ)-*-*-*.tgz $(PRJ)-*.tar.gz |
@-rm -f $(PRJ)-*.rpm $(PRJ)-*-*-*.tgz $(PRJ)-*.tar.gz |
39 |
39 |
|
|
40 |
40 |
.PHONY: check |
.PHONY: check |
41 |
41 |
check: |
check: |
42 |
42 |
cppcheck --enable=all -I. . |
cppcheck --enable=all -I. . |
43 |
43 |
|
|
|
44 |
|
test: test.c $(OBJS) $(DEP) |
|
45 |
|
gcc $(CFLAGS) test.c -o test $(OBJS) $(LIBS) |
|
46 |
|
@./test |
|
47 |
|
|
44 |
48 |
install: all |
install: all |
45 |
49 |
@mkdir -p $(I_USR_SBIN) |
@mkdir -p $(I_USR_SBIN) |
46 |
50 |
@cp -vd nf2fad $(I_USR_SBIN) |
@cp -vd nf2fad $(I_USR_SBIN) |
File totp.c changed (mode: 100644) (index e74860e..222ef77) |
... |
... |
int totp_base64_generate(char *out, const unsigned char digits) |
46 |
46 |
} |
} |
47 |
47 |
|
|
48 |
48 |
/* |
/* |
49 |
|
* Returns |
|
|
49 |
|
* Returns the index of the char |
50 |
50 |
*/ |
*/ |
51 |
51 |
unsigned char totp_char_to_index(const char c) |
unsigned char totp_char_to_index(const char c) |
52 |
52 |
{ |
{ |
|
... |
... |
unsigned char totp_char_to_index(const char c) |
55 |
55 |
|
|
56 |
56 |
q = toupper(c); |
q = toupper(c); |
57 |
57 |
x = strchr(totp_base32_tab, q); |
x = strchr(totp_base32_tab, q); |
58 |
|
if (x == NULL) |
|
|
58 |
|
if (x == NULL) { |
|
59 |
|
snprintf(error, sizeof(error), "invalid base32 char"); |
59 |
60 |
return 0xFF; |
return 0xFF; |
|
61 |
|
} |
60 |
62 |
|
|
61 |
63 |
return x - totp_base32_tab; |
return x - totp_base32_tab; |
62 |
64 |
} |
} |
|
... |
... |
static unsigned char totp_hex2bin(const char *s) |
131 |
133 |
* Compute a code based on a key, a timestamp/30 and number of digits |
* Compute a code based on a key, a timestamp/30 and number of digits |
132 |
134 |
* Returns -1 on error (probably the key is invalid), 0 on success (@out |
* Returns -1 on error (probably the key is invalid), 0 on success (@out |
133 |
135 |
* will contain the digits) |
* will contain the digits) |
|
136 |
|
* Use maximum 10 digits. |
134 |
137 |
*/ |
*/ |
135 |
138 |
int totp_compute(char *out, const char *key, unsigned int tc, |
int totp_compute(char *out, const char *key, unsigned int tc, |
136 |
139 |
unsigned char digits) |
unsigned char digits) |
137 |
140 |
{ |
{ |
138 |
|
unsigned char key_bin[64], key_len, btc[8], i, *p, o; |
|
|
141 |
|
unsigned char key_bin[64], key_len, btc[8], i, *p, o, number_len; |
139 |
142 |
int r; |
int r; |
140 |
|
char stc[17], format[8]; |
|
|
143 |
|
char stc[17], number[11]; |
141 |
144 |
unsigned int v; |
unsigned int v; |
142 |
145 |
unsigned long long z; |
unsigned long long z; |
143 |
146 |
|
|
144 |
147 |
//printf("DEBUG: %s: key=%s tc=%u\n", __func__, key, tc); |
//printf("DEBUG: %s: key=%s tc=%u\n", __func__, key, tc); |
145 |
148 |
|
|
|
149 |
|
if (digits > 10) { |
|
150 |
|
snprintf(error, sizeof(error), "too many digits"); |
|
151 |
|
return -1; |
|
152 |
|
} |
|
153 |
|
|
146 |
154 |
r = totp_base32_decode(key_bin, sizeof(key_bin), key); |
r = totp_base32_decode(key_bin, sizeof(key_bin), key); |
147 |
155 |
if (r == -1) |
if (r == -1) |
148 |
156 |
return -1; |
return -1; |
|
... |
... |
int totp_compute(char *out, const char *key, unsigned int tc, |
164 |
172 |
for (i = 0; i < digits; i++) |
for (i = 0; i < digits; i++) |
165 |
173 |
z *= 10; |
z *= 10; |
166 |
174 |
|
|
167 |
|
sprintf(format, "%%0%hhuu", digits); |
|
168 |
|
sprintf(out, format, v % z); |
|
|
175 |
|
number_len = snprintf(number, sizeof(number), "%llu", v % z); |
|
176 |
|
|
|
177 |
|
// Prepending zeros |
|
178 |
|
for (i = 0; i < digits - number_len; i++) |
|
179 |
|
out[i] = '0'; |
|
180 |
|
out[i] = '\0'; |
|
181 |
|
|
|
182 |
|
strcat(out, number); |
169 |
183 |
|
|
170 |
184 |
return 0; |
return 0; |
171 |
185 |
} |
} |