inc/usage.h
SUBDIRS = . test
+
+format:
+ VERSION_CONTROL=none indent -linux -brf $(totp_SOURCES)
#include<totp.h>
#include<usage.h>
-int args(int, char**);
+int args(int, char **);
#endif
#include<stdio.h>
#include<stdlib.h>
-int from_base32(unsigned char**, ssize_t*);
+int from_base32(unsigned char **, ssize_t *);
#endif
#include<base32.h>
#include<totp.h>
-int main(int,char**);
+int main(int, char **);
#endif
extern time_t now;
-int totp(unsigned char*, ssize_t, uint32_t*);
+int totp(unsigned char *, ssize_t, uint32_t *);
#endif
(specifications->manifest (list "autoconf"
"automake"
"coreutils"
- "gawk"
+ "gawk"
"gcc-toolchain"
- "gdb"
+ "gdb"
"grep"
+ "indent"
"make"
- "openssl"
+ "openssl"
"sed"
- "valgrind"))
+ "valgrind"))
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"time", required_argument, 0, 't'},
- {0,0,0,0}
+ {0, 0, 0, 0}
};
time_t now = 0;
struct tm tm;
int c;
- while(1) {
+ while (1) {
int option_index = 0;
- if((c = getopt_long(argc, argv, "ht:", long_options, &option_index)) == -1) { break; }
-
- switch(c) {
- case 0:
- if(long_options[option_index].flag != 0) { break; }
-
- fprintf(stderr, "option %s", long_options[option_index].name);
- if(optarg) {
- fprintf(stderr, " with arg %s", optarg);
- }
- fprintf(stderr, "\n");
- return -1;
-
- break;
- case 'h':
- usage();
- return -1;
- case 't':
- memset(&tm, 0, sizeof(tm));
-
- if(strptime(optarg, "%F %T %z", &tm) != NULL) {
- now = mktime(&tm);
- }
+ if ((c =
+ getopt_long(argc, argv, "ht:", long_options,
+ &option_index)) == -1) {
+ break;
+ }
+ switch (c) {
+ case 0:
+ if (long_options[option_index].flag != 0) {
break;
- case '?':
- default:
- return -1;
+ }
+
+ fprintf(stderr, "option %s",
+ long_options[option_index].name);
+ if (optarg) {
+ fprintf(stderr, " with arg %s", optarg);
+ }
+ fprintf(stderr, "\n");
+ return -1;
+
+ break;
+ case 'h':
+ usage();
+ return -1;
+ case 't':
+ memset(&tm, 0, sizeof(tm));
+
+ if (strptime(optarg, "%F %T %z", &tm) != NULL) {
+ now = mktime(&tm);
+ }
+
+ break;
+ case '?':
+ default:
+ return -1;
}
}
char c, to_set;
size_t i, j, offset, len;
- if(NULL == raw || NULL == *raw) {
+ if (NULL == raw || NULL == *raw) {
fprintf(stderr, "invalid buffer\n");
return -1;
}
- if(NULL == n || *n <= 0) {
+ if (NULL == n || *n <= 0) {
fprintf(stderr, "invalid secret size\n");
return -1;
}
- len = ((*n)*5)>>3;
- if(len == 0) {
+ len = ((*n) * 5) >> 3;
+ if (len == 0) {
fprintf(stderr, "invalid secret size\n");
return -1;
}
-
+
buf = calloc(len, sizeof(char));
- if(NULL == buf) {
+ if (NULL == buf) {
perror("calloc");
return -1;
}
offset = 0;
p = *raw;
j = 0;
- for(i = 0; i < *n; i++) {
+ for (i = 0; i < *n; i++) {
c = p[i];
- if(c >= 'A' && c <= 'Z') {
+ if (c >= 'A' && c <= 'Z') {
to_set = c - 65;
- } else if(c >= '2' && c <= '7') {
+ } else if (c >= '2' && c <= '7') {
to_set = c - 24;
- } else if(c == '=') {
+ } else if (c == '=') {
continue;
} else {
fprintf(stderr, "invalid base32 character %c\n", c);
// No need to mask this value since it will always be <31
buf[j] += (to_set << 3) >> offset;
- if(((offset + 5) > 7) && (j + 1 < len)) {
+ if (((offset + 5) > 7) && (j + 1 < len)) {
j++;
buf[j] += ((to_set << (8 - (5 - (8 - offset)))) & 0xff);
}
buf = NULL;
now = 0;
- if(args(argc, argv)<0) { return EXIT_FAILURE; }
+ if (args(argc, argv) < 0) {
+ return EXIT_FAILURE;
+ }
- if((n = getline((char **)&buf, &len, stdin)) <= 0) {
+ if ((n = getline((char **)&buf, &len, stdin)) <= 0) {
perror("getline failed");
fprintf(stderr, "No secret provided\n");
goto clean;
}
-
// Trim trailing '\n'
n--;
- if(from_base32(&buf, &n) < 0) { goto clean; }
- if(totp((unsigned char *)buf, n, &code) < 0) { goto clean; }
+ if (from_base32(&buf, &n) < 0) {
+ goto clean;
+ }
+
+ if (totp((unsigned char *)buf, n, &code) < 0) {
+ goto clean;
+ }
fprintf(stdout, "%.6u\n", code % 1000000);
free(buf);
return EXIT_SUCCESS;
- clean:
- if(buf != NULL) { free(buf); }
- return EXIT_FAILURE;
+ clean:
+ if (buf != NULL) {
+ free(buf);
+ }
+ return EXIT_FAILURE;
}
size_t offset;
unsigned int hash_len = SHA_DIGEST_LENGTH;
- if(NULL == key) { return -1; }
- if(n <= 0) { return -1; }
+ if (NULL == key) {
+ return -1;
+ }
+
+ if (n <= 0) {
+ return -1;
+ }
- if(now == 0) {
+ if (now == 0) {
now = time(NULL);
}
now = floor(now / 30);
// Guarantee that the data buffer is in big-endian order
- for(size_t i = 0; i<sizeof(time_t); i++) {
- data[sizeof(time_t) - (1 + i)] = now >> (8*i) & 0xff;
+ for (size_t i = 0; i < sizeof(time_t); i++) {
+ data[sizeof(time_t) - (1 + i)] = now >> (8 * i) & 0xff;
}
- if(NULL == HMAC(EVP_sha1(), key, n, data, sizeof(time_t), hash, &hash_len)) {
+ if (NULL ==
+ HMAC(EVP_sha1(), key, n, data, sizeof(time_t), hash, &hash_len)) {
fprintf(stderr, "hashing failed\n");
return -1;
}
offset = hash[SHA_DIGEST_LENGTH - 1] & 0xf;
- (*code) = ((hash[offset] & 0x7f) << 24)
- | ((hash[offset+1] & 0xff) << 16)
- | ((hash[offset+2] & 0xff) << 8)
- | (hash[offset+3] & 0xff);
+ (*code) = ((hash[offset] & 0x7f) << 24)
+ | ((hash[offset + 1] & 0xff) << 16)
+ | ((hash[offset + 2] & 0xff) << 8)
+ | (hash[offset + 3] & 0xff);
return 1;
}
fprintf(stderr, "Usage:\n");
fprintf(stderr, "\t[SECRET] | totp\n");
fprintf(stderr, "\n");
- fprintf(stderr, "Pass a base32 encoded secrets to stdin and generate a totp code at the current time.\n");
+ fprintf(stderr,
+ "Pass a base32 encoded secrets to stdin and generate a totp code at the current time.\n");
}