dedup

deduplicating backup program
git clone git://git.2f30.org/dedup
Log | Files | Refs | README | LICENSE

key.c (970B)


      1 #include <assert.h>
      2 #include <unistd.h>
      3 
      4 #include <sodium.h>
      5 
      6 #include "config.h"
      7 #include "misc.h"
      8 
      9 int
     10 keygen(unsigned char *key, size_t n)
     11 {
     12 	assert(KEYSIZE == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
     13 	if (n != KEYSIZE) {
     14 		seterr("invalid key size");
     15 		return -1;
     16 	}
     17 	if (sodium_init() < 0) {
     18 		seterr("sodium_init: failed");
     19 		return -1;
     20 	}
     21 	crypto_aead_xchacha20poly1305_ietf_keygen(key);
     22 	return 0;
     23 }
     24 
     25 int
     26 writekey(int fd, unsigned char *key, size_t n)
     27 {
     28 	assert(KEYSIZE == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
     29 	if (n != KEYSIZE) {
     30 		seterr("invalid key size");
     31 		return -1;
     32 	}
     33 	if (xwrite(fd, key, n) != n) {
     34 		seterr("failed to write key");
     35 		return -1;
     36 	}
     37 	return 0;
     38 }
     39 
     40 int
     41 readkey(int fd, unsigned char *key, size_t n)
     42 {
     43 	assert(KEYSIZE == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
     44 	if (n != KEYSIZE) {
     45 		seterr("invalid key size");
     46 		return -1;
     47 	}
     48 	if (xread(fd, key, n) != n) {
     49 		seterr("failed to read key");
     50 		return -1;
     51 	}
     52 	return 0;
     53 }