sandbox: refactor string-based option-unchanged tests to use a macro
[tor.git] / src / common / crypto_format.c
blobbe669c8d2b301d6230ff1e3988e8661ffcf2c1f9
1 /* Copyright (c) 2012-2013, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /* Formatting and parsing code for crypto-related data structures. */
6 #include "orconfig.h"
7 #ifdef HAVE_SYS_STAT_H
8 #include <sys/stat.h>
9 #endif
10 #include "crypto.h"
11 #include "crypto_curve25519.h"
12 #include "util.h"
13 #include "torlog.h"
15 int
16 curve25519_public_to_base64(char *output,
17 const curve25519_public_key_t *pkey)
19 char buf[128];
20 base64_encode(buf, sizeof(buf),
21 (const char*)pkey->public_key, CURVE25519_PUBKEY_LEN);
22 buf[CURVE25519_BASE64_PADDED_LEN] = '\0';
23 memcpy(output, buf, CURVE25519_BASE64_PADDED_LEN+1);
24 return 0;
27 int
28 curve25519_public_from_base64(curve25519_public_key_t *pkey,
29 const char *input)
31 size_t len = strlen(input);
32 if (len == CURVE25519_BASE64_PADDED_LEN - 1) {
33 /* not padded */
34 return digest256_from_base64((char*)pkey->public_key, input);
35 } else if (len == CURVE25519_BASE64_PADDED_LEN) {
36 char buf[128];
37 if (base64_decode(buf, sizeof(buf), input, len) != CURVE25519_PUBKEY_LEN)
38 return -1;
39 memcpy(pkey->public_key, buf, CURVE25519_PUBKEY_LEN);
40 return 0;
41 } else {
42 return -1;