Skip to content

Commit

Permalink
Add v2.local CLI example tools
Browse files Browse the repository at this point in the history
  • Loading branch information
minus7 committed Dec 18, 2018
1 parent 98890b7 commit bce7cf2
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 26 deletions.
51 changes: 27 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.10)
project(libpaseto
VERSION 0.99
LANGUAGES C)
VERSION 0.99
LANGUAGES C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS OFF)
Expand All @@ -13,40 +13,43 @@ include_directories(${SODIUM_INCLUDE_DIRS})

include_directories(include)
set(PASETO_SOURCES
src/paseto_v2_local.c
src/paseto_v2_public.c
src/paseto.c
src/helpers.c
src/helpers.h
include/paseto.h)
src/paseto_v2_local.c
src/paseto_v2_public.c
src/paseto.c
src/helpers.c
src/helpers.h
include/paseto.h)

CONFIGURE_FILE("paseto.pc.in" "paseto.pc" @ONLY)
configure_file("paseto.pc.in" "paseto.pc" @ONLY)


# shared library build
add_library(paseto SHARED ${PASETO_SOURCES})
target_link_libraries(paseto ${SODIUM_LIBRARIES})
install(TARGETS paseto
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
install(FILES include/paseto.h
DESTINATION include)
DESTINATION include)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/paseto.pc"
DESTINATION lib/pkgconfig)
DESTINATION lib/pkgconfig)

# test executable
add_executable(pasetotest
test/main.c
test/helpers.h
test/helpers.c
test/test.h
test/test.c
test/v2vectors.h
test/v2vectors.c
test/v2publicvectors.c)
test/main.c
test/helpers.h
test/helpers.c
test/test.h
test/test.c
test/v2vectors.h
test/v2vectors.c
test/v2publicvectors.c)
target_compile_definitions(pasetotest PRIVATE _POSIX_C_SOURCE=200809L)
target_link_libraries(pasetotest ${SODIUM_LIBRARIES} paseto)

# example executable
add_executable(example example.c)
target_link_libraries(example ${SODIUM_LIBRARIES} paseto)
# examples
set(EXAMPLES example paseto-v2-local-encrypt paseto-v2-local-decrypt)
foreach(EXAMPLE ${EXAMPLES})
add_executable(${EXAMPLE} examples/${EXAMPLE}.c)
target_link_libraries(${EXAMPLE} ${SODIUM_LIBRARIES} paseto)
endforeach()
File renamed without changes.
42 changes: 42 additions & 0 deletions examples/paseto-v2-local-decrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "paseto.h"
#include <stdio.h>
#include <stdlib.h>

// Loads v2.local-encrypted data from stdin, decrypts it using the hex-encoded
// key in the PASETO_V2_LOCAL_SECRET environment variable and writes decrypted
// data to stdout. The footer is not written to stdout.


static const size_t BUF_SIZE = 64*1024;


int main() {
if (!paseto_init()) {
fprintf(stderr, "Failed to initialize libpaseto\n");
exit(-1);
}

uint8_t key[paseto_v2_LOCAL_KEYBYTES];
if (!paseto_v2_local_load_key_base64(key,
getenv("PASETO_V2_LOCAL_SECRET"))) {
perror("Failed to load key");
exit(-1);
}
char *encrypted = malloc(BUF_SIZE);
if (!encrypted) {
perror("Failed to allocate input buffer");
exit(-1);
}
size_t encrypted_len = fread(encrypted, 1, BUF_SIZE - 1, stdin);
encrypted[encrypted_len] = '\0';
size_t message_len;
uint8_t *message = paseto_v2_local_decrypt(
encrypted, &message_len, key, NULL, NULL);
free(encrypted);
if (!message) {
perror("paseto_v2_local_decrypt failed");
exit(-1);
}
fwrite(message, 1, message_len, stdout);
paseto_free(message);
}
40 changes: 40 additions & 0 deletions examples/paseto-v2-local-encrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "paseto.h"
#include <stdio.h>
#include <stdlib.h>

// Loads data from stdin, encrypts it using the hex-encoded key in the
// PASETO_V2_LOCAL_SECRET environment variable and writes encrypted data to
// stdout. A footer can not be specified.


static const size_t BUF_SIZE = 64*1024;


int main() {
if (!paseto_init()) {
fprintf(stderr, "Failed to initialize libpaseto\n");
exit(-1);
}

uint8_t key[paseto_v2_LOCAL_KEYBYTES];
if (!paseto_v2_local_load_key_base64(key,
getenv("PASETO_V2_LOCAL_SECRET"))) {
perror("Failed to load key");
exit(-1);
}
uint8_t *message = malloc(BUF_SIZE);
if (!message) {
perror("Failed to allocate input buffer");
exit(-1);
}
size_t message_len = fread(message, 1, BUF_SIZE - 1, stdin);
char *encrypted = paseto_v2_local_encrypt(
message, message_len, key, NULL, 0);
free(message);
if (!encrypted) {
perror("paseto_v2_local_encrypt failed");
exit(-1);
}
puts(encrypted);
paseto_free(encrypted);
}
6 changes: 4 additions & 2 deletions include/paseto.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ char *paseto_v2_local_encrypt(
* is not NULL and the encoded message contains a non-zero-length footer, it
* will be set to a pointer to the decoded footer. It is the callers
* responsibility to free it.
* Returns a pointer to the decrypted message. `message_len` is set ti its
* length. It is the callers responsibility to free it using `paseto_free`.
* Returns a pointer to the decrypted message. `message_len` is set to its
* length. For convenience, the message is terminated by a NULL byte. This NULL
* byte is *not* included in `message_len`. It is the callers responsibility to
* free it using `paseto_free`.
* Returns NULL on error and sets errno.
* Returns NULL on verification failure.
*/
Expand Down
1 change: 1 addition & 0 deletions src/paseto_v2_local.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ uint8_t *paseto_v2_local_decrypt(
return NULL;
}

// include a null terminator for convenience
message[internal_message_len] = '\0';

free(pa.base);
Expand Down

0 comments on commit bce7cf2

Please sign in to comment.