git ssb

8+

cel / sbotc



Commit a29ae31525cbdd420aea28105f73b962db9ef635

Use libsodium base64

cel committed on 5/25/2020, 11:20:08 PM
Parent: 52ec6488b4f1eb3227dae48eea2c00c8a85ce2e2

Files changed

Makefilechanged
sbotc.cchanged
base64.cdeleted
base64.hdeleted
MakefileView
@@ -13,9 +13,9 @@
1313 endif
1414
1515 all: $(BIN)
1616
17-$(BIN): $(BIN).c base64.c jsmn.c
17 +$(BIN): $(BIN).c jsmn.c
1818
1919 install: all
2020 @mkdir -vp $(DESTDIR)$(BINDIR) $(DESTDIR)$(MANDIR)/man1
2121 @cp -vf $(BIN) $(DESTDIR)$(BINDIR)
sbotc.cView
@@ -31,9 +31,8 @@
3131 #include <unistd.h>
3232
3333 #include <sodium.h>
3434
35-#include "base64.h"
3635 #include "jsmn.h"
3736
3837 #define BOXS_MAXLEN 4096
3938
@@ -385,18 +384,24 @@
385384 if (*key_str == '@') key_str++;
386385 size_t len = strlen(key_str);
387386 if (len == 52 && strcmp(key_str+44, ".ed25519") == 0) {}
388387 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);
390392 }
391393
392394 static int seckey_decode(const char *key_str, unsigned char key[64]) {
393395 if (!key_str) { errno = EPROTO; return -1; }
394396 if (!*key_str) { errno = EPROTO; return -1; }
395397 if (*key_str == '@') key_str++;
396398 size_t len = strlen(key_str);
397399 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);
399404 }
400405
401406 static jsmntok_t *json_lookup(const char *buf, jsmntok_t *tok, const char *prop, size_t prop_len) {
402407 jsmntok_t *end = tok + tok->size + 1;
@@ -508,9 +513,12 @@
508513 if (key_len < 0) err(1, "unable to read private key");
509514
510515 if (key_len > 8 && memcmp(pk_b64 + key_len - 8, ".ed25519", 8) == 0)
511516 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);
513521 if (rc < 0) err(1, "unable to decode private key");
514522 }
515523
516524 static void increment_nonce(uint8_t nonce[24]) {
@@ -907,10 +915,9 @@
907915
908916 rc = crypto_hash_sha256_final(&hash_state, hash);
909917 if (rc < 0) errx(1, "hash finalize failed");
910918
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);
913920 strcpy(id + 45, ".sha256\n");
914921 rc = write_all(outfd, id, sizeof(id)-1);
915922 if (rc < 0) err(1, "writing hash failed");
916923
base64.cView
@@ -1,118 +1,0 @@
1-/*
2-
3- This code is public domain software.
4-
5-*/
6-
7-#include "base64.h"
8-
9-#include <stdlib.h>
10-#include <string.h>
11-#include <errno.h>
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-}
base64.hView
@@ -1,6 +1,0 @@
1-#pragma once
2-
3-#include <stddef.h>
4-
5-int base64_encode(const void* buf, size_t size, char *str, size_t out_size);
6-int base64_decode(const char *s, size_t str_len, void *data, size_t data_len);

Built with git-ssb-web