stun

simple point to point tunnel
git clone git://git.2f30.org/stun
Log | Files | Refs | README

crypto.c (1594B)


      1 #include <string.h>
      2 
      3 #include <sodium.h>
      4 
      5 #include "warp.h"
      6 
      7 static unsigned char salt[crypto_pwhash_SALTBYTES];
      8 static unsigned char key[crypto_aead_chacha20poly1305_IETF_KEYBYTES];
      9 
     10 void
     11 cryptoinit(void)
     12 {
     13 	if (sodium_init() == -1)
     14 		fatalx("failed to initialize crypto engine");
     15 }
     16 
     17 void
     18 derivekey(char *pw)
     19 {
     20 	memset(salt, 0, sizeof(salt)); /* XXX */
     21 	if (crypto_pwhash(key, sizeof(key), pw, strlen(pw), salt,
     22 	                  crypto_pwhash_OPSLIMIT_INTERACTIVE,
     23 	                  crypto_pwhash_MEMLIMIT_INTERACTIVE,
     24 	                  crypto_pwhash_ALG_DEFAULT) != 0)
     25 		fatalx("failed to derive key");
     26 }
     27 
     28 size_t
     29 cryptononcelen(void)
     30 {
     31 	return crypto_aead_chacha20poly1305_IETF_NPUBBYTES;
     32 }
     33 
     34 size_t
     35 cryptotaglen(void)
     36 {
     37 	return crypto_aead_chacha20poly1305_IETF_ABYTES;
     38 }
     39 
     40 int cryptoseal(unsigned char *c, unsigned long long *clen,
     41                const unsigned char *m, unsigned long long mlen,
     42                const unsigned char *ad, unsigned long long adlen,
     43                const unsigned char *npub)
     44 {
     45 	return crypto_aead_chacha20poly1305_ietf_encrypt(c, clen, m, mlen,
     46 	                                                 ad, adlen, NULL,
     47 	                                                 npub, key);
     48 }
     49 
     50 int cryptoopen(unsigned char *m, unsigned long long *mlen,
     51                const unsigned char *c, unsigned long long clen,
     52                const unsigned char *ad, unsigned long long adlen,
     53                const unsigned char *npub)
     54 {
     55 	return crypto_aead_chacha20poly1305_ietf_decrypt(m, mlen, NULL, c, clen,
     56 	                                                 ad, adlen, npub, key);
     57 }