+/*
+ * base64 - libubox base64 functions
+ *
+ * Copyright (C) 2015 Felix Fietkau <nbd@openwrt.org>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
/* $OpenBSD: base64.c,v 1.7 2013/12/31 02:32:56 tedu Exp $ */
/*
characters followed by one "=" padding character.
*/
-int b64_ntop(const void *_src, size_t srclength,
- void *dest, size_t targsize)
+int b64_encode(const void *_src, size_t srclength,
+ void *dest, size_t targsize)
{
const unsigned char *src = _src;
char *target = dest;
it returns the number of data bytes stored at the target, or -1 on error.
*/
-int b64_pton(const void *_src, void *dest, size_t targsize)
+int b64_decode(const void *_src, void *dest, size_t targsize)
{
const char *src = _src;
unsigned char *target = dest;
return (-1);
}
+ /* Null-terminate if we have room left */
+ if (tarindex < targsize)
+ target[tarindex] = 0;
+
return (tarindex);
}
#ifndef __BASE64_H
#define __BASE64_H
-int b64_ntop(const void *src, size_t src_len,
- void *dest, size_t dest_len);
+int b64_encode(const void *src, size_t src_len,
+ void *dest, size_t dest_len);
-int b64_pton(const void *src, void *dest, size_t dest_len);
+int b64_decode(const void *src, void *dest, size_t dest_len);
+#define B64_ENCODE_LEN(_len) ((((_len) + 2) / 3) * 4 + 1)
#define B64_DECODE_LEN(_len) (((_len) / 4) * 3 + 1)
-#define B64_ENCODE_LEN(_len) ((((_len) / 3) + 1) * 4 + 1)
#endif
get_base64_file(const char *file, void *dest, int size, void *buf, int buflen)
{
get_file(file, buf, buflen - 1);
- return b64_pton(buf, dest, size) == size;
+ return b64_decode(buf, dest, size) == size;
}
static void write_file(const char *name, const uint8_t *fingerprint,
munmap(m, mlen);
close(mfd);
- if (b64_ntop(&sig, sizeof(sig), buf, sizeof(buf)) < 0)
+ if (b64_encode(&sig, sizeof(sig), buf, sizeof(buf)) < 0)
return 1;
write_file(sigfile, sig.fingerprint, "signed by key", buf);
sha512_add(&s, skey.seckey, sizeof(skey.seckey));
memcpy(skey.checksum, sha512_final_get(&s), sizeof(skey.checksum));
- if (b64_ntop(&skey, sizeof(skey), buf, sizeof(buf)) < 0)
+ if (b64_encode(&skey, sizeof(skey), buf, sizeof(buf)) < 0)
return 1;
write_file(seckeyfile, skey.fingerprint, "public key", buf);
memcpy(pkey.fingerprint, skey.fingerprint, sizeof(pkey.fingerprint));
memcpy(pkey.pubkey, skey.seckey + 32, sizeof(pkey.pubkey));
- if (b64_ntop(&pkey, sizeof(pkey), buf, sizeof(buf)) < 0)
+ if (b64_encode(&pkey, sizeof(pkey), buf, sizeof(buf)) < 0)
return 1;
write_file(pubkeyfile, pkey.fingerprint, "private key", buf);