Commit a29ae31525cbdd420aea28105f73b962db9ef635
Use libsodium base64
cel committed on 5/25/2020, 11:20:08 PMParent: 52ec6488b4f1eb3227dae48eea2c00c8a85ce2e2
Files changed
Makefile | changed |
sbotc.c | changed |
base64.c | deleted |
base64.h | deleted |
Makefile | ||
---|---|---|
@@ -13,9 +13,9 @@ | ||
13 | 13 … | endif |
14 | 14 … | |
15 | 15 … | all: $(BIN) |
16 | 16 … | |
17 | -$(BIN): $(BIN).c base64.c jsmn.c | |
17 … | +$(BIN): $(BIN).c jsmn.c | |
18 | 18 … | |
19 | 19 … | install: all |
20 | 20 … | @mkdir -vp $(DESTDIR)$(BINDIR) $(DESTDIR)$(MANDIR)/man1 |
21 | 21 … | @cp -vf $(BIN) $(DESTDIR)$(BINDIR) |
sbotc.c | ||
---|---|---|
@@ -31,9 +31,8 @@ | ||
31 | 31 … | |
32 | 32 … | |
33 | 33 … | |
34 | 34 … | |
35 | - | |
36 | 35 … | |
37 | 36 … | |
38 | 37 … | |
39 | 38 … | |
@@ -385,18 +384,24 @@ | ||
385 | 384 … | if (*key_str == '@') key_str++; |
386 | 385 … | size_t len = strlen(key_str); |
387 | 386 … | if (len == 52 && strcmp(key_str+44, ".ed25519") == 0) {} |
388 | 387 … | else if (len != 44) { errno = EMSGSIZE; return -1; } |
389 | - return base64_decode(key_str, 44, key, 32); | |
388 … | + return sodium_base642bin( | |
389 … | + (unsigned char *const)key, 32, | |
390 … | + (const char *const)key_str, 44, | |
391 … | + NULL, NULL, NULL, sodium_base64_VARIANT_ORIGINAL); | |
390 | 392 … | } |
391 | 393 … | |
392 | 394 … | static int seckey_decode(const char *key_str, unsigned char key[64]) { |
393 | 395 … | if (!key_str) { errno = EPROTO; return -1; } |
394 | 396 … | if (!*key_str) { errno = EPROTO; return -1; } |
395 | 397 … | if (*key_str == '@') key_str++; |
396 | 398 … | size_t len = strlen(key_str); |
397 | 399 … | if (len > 8 && memcmp(key_str + len - 8, ".ed25519", 8) == 0) len -= 8; |
398 | - return base64_decode(key_str, len, key, 64); | |
400 … | + return sodium_base642bin( | |
401 … | + (unsigned char *const)key, 64, | |
402 … | + (const char *const)key_str, len, | |
403 … | + NULL, NULL, NULL, sodium_base64_VARIANT_ORIGINAL); | |
399 | 404 … | } |
400 | 405 … | |
401 | 406 … | static jsmntok_t *json_lookup(const char *buf, jsmntok_t *tok, const char *prop, size_t prop_len) { |
402 | 407 … | jsmntok_t *end = tok + tok->size + 1; |
@@ -508,9 +513,12 @@ | ||
508 | 513 … | if (key_len < 0) err(1, "unable to read private key"); |
509 | 514 … | |
510 | 515 … | if (key_len > 8 && memcmp(pk_b64 + key_len - 8, ".ed25519", 8) == 0) |
511 | 516 … | key_len -= 8; |
512 | - rc = base64_decode(pk_b64, key_len, pk, 64); | |
517 … | + rc = sodium_base642bin( | |
518 … | + (unsigned char *const)pk, 64, | |
519 … | + (const char *const)pk_b64, key_len, | |
520 … | + NULL, NULL, NULL, sodium_base64_VARIANT_ORIGINAL); | |
513 | 521 … | if (rc < 0) err(1, "unable to decode private key"); |
514 | 522 … | } |
515 | 523 … | |
516 | 524 … | static void increment_nonce(uint8_t nonce[24]) { |
@@ -907,10 +915,9 @@ | ||
907 | 915 … | |
908 | 916 … | rc = crypto_hash_sha256_final(&hash_state, hash); |
909 | 917 … | if (rc < 0) errx(1, "hash finalize failed"); |
910 | 918 … | |
911 | - rc = base64_encode(hash, 32, id+1, sizeof(id)-1); | |
912 | - if (rc < 0) err(1, "encoding hash failed"); | |
919 … | + (void)sodium_bin2base64(id+1, sizeof(id)-1, hash, 32, sodium_base64_VARIANT_ORIGINAL); | |
913 | 920 … | strcpy(id + 45, ".sha256\n"); |
914 | 921 … | rc = write_all(outfd, id, sizeof(id)-1); |
915 | 922 … | if (rc < 0) err(1, "writing hash failed"); |
916 | 923 … |
base64.c | ||
---|---|---|
@@ -1,118 +1,0 @@ | ||
1 | -/* | |
2 | - | |
3 | - This code is public domain software. | |
4 | - | |
5 | -*/ | |
6 | - | |
7 | - | |
8 | - | |
9 | - | |
10 | - | |
11 | - | |
12 | - | |
13 | - | |
14 | -// single base64 character conversion | |
15 | -// | |
16 | -static int POS(char c) | |
17 | -{ | |
18 | - if (c>='A' && c<='Z') return c - 'A'; | |
19 | - if (c>='a' && c<='z') return c - 'a' + 26; | |
20 | - if (c>='0' && c<='9') return c - '0' + 52; | |
21 | - if (c == '+') return 62; | |
22 | - if (c == '/') return 63; | |
23 | - if (c == '=') return -1; | |
24 | - return -2; | |
25 | -} | |
26 | - | |
27 | -// base64 decoding | |
28 | -// | |
29 | -// s: base64 string | |
30 | -// str_len size of the base64 string | |
31 | -// data: output buffer for decoded data | |
32 | -// data_len expected size of decoded data | |
33 | -// return: 0 on success, -1 on failure | |
34 | -// | |
35 | -int base64_decode(const char* s, size_t str_len, void *data, size_t data_len) | |
36 | -{ | |
37 | - const char *p, *str_end; | |
38 | - unsigned char *q, *end; | |
39 | - int n[4] = { 0, 0, 0, 0 }; | |
40 | - | |
41 | - if (str_len % 4) { errno = EBADMSG; return -1; } | |
42 | - q = (unsigned char*) data; | |
43 | - end = q + data_len; | |
44 | - str_end = s + str_len; | |
45 | - | |
46 | - for (p = s; p < str_end; ) { | |
47 | - n[0] = POS(*p++); | |
48 | - n[1] = POS(*p++); | |
49 | - n[2] = POS(*p++); | |
50 | - n[3] = POS(*p++); | |
51 | - | |
52 | - if (n[0] == -2 || n[1] == -2 || n[2] == -2 || n[3] == -2) | |
53 | - { errno = EBADMSG; return -1; } | |
54 | - | |
55 | - if (n[0] == -1 || n[1] == -1) | |
56 | - { errno = EBADMSG; return -1; } | |
57 | - | |
58 | - if (n[2] == -1 && n[3] != -1) | |
59 | - { errno = EBADMSG; return -1; } | |
60 | - | |
61 | - if (q >= end) { errno = EMSGSIZE; return -1; } | |
62 | - q[0] = (n[0] << 2) + (n[1] >> 4); | |
63 | - if (n[2] != -1) { | |
64 | - if (q+1 >= end) { errno = EMSGSIZE; return -1; } | |
65 | - q[1] = ((n[1] & 15) << 4) + (n[2] >> 2); | |
66 | - } | |
67 | - if (n[3] != -1) { | |
68 | - if (q+2 >= end) { errno = EMSGSIZE; return -1; } | |
69 | - q[2] = ((n[2] & 3) << 6) + n[3]; | |
70 | - } | |
71 | - q += 3; | |
72 | - } | |
73 | - | |
74 | - return 0; | |
75 | -} | |
76 | - | |
77 | -int base64_encode(const void* buf, size_t size, char *str, size_t out_size) { | |
78 | - static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
79 | - | |
80 | - char* p = str; | |
81 | - const unsigned char* q = (const unsigned char*) buf; | |
82 | - size_t i = 0; | |
83 | - | |
84 | - if ((size+3)*4/3 + 1 > out_size) { | |
85 | - errno = EMSGSIZE; | |
86 | - return -1; | |
87 | - } | |
88 | - | |
89 | - while (i < size) { | |
90 | - int c = q[i++]; | |
91 | - c *= 256; | |
92 | - if (i < size) | |
93 | - c += q[i]; | |
94 | - i++; | |
95 | - | |
96 | - c *= 256; | |
97 | - if (i < size) | |
98 | - c += q[i]; | |
99 | - i++; | |
100 | - | |
101 | - *p++ = base64[(c & 0x00fc0000) >> 18]; | |
102 | - *p++ = base64[(c & 0x0003f000) >> 12]; | |
103 | - | |
104 | - if (i > size + 1) | |
105 | - *p++ = '='; | |
106 | - else | |
107 | - *p++ = base64[(c & 0x00000fc0) >> 6]; | |
108 | - | |
109 | - if (i > size) | |
110 | - *p++ = '='; | |
111 | - else | |
112 | - *p++ = base64[c & 0x0000003f]; | |
113 | - } | |
114 | - | |
115 | - *p = 0; | |
116 | - | |
117 | - return 0; | |
118 | -} |
Built with git-ssb-web