int pubnub_encrypt_buffered(char const *cipher_key, pubnub_bymebl_t msg, char *base64_str, size_t *n, pubnub_bymebl_t buffer) { uint8_t const iv[] = "0123456789012345"; uint8_t key[33]; cipher_hash(cipher_key, key); if (-1 == pbaes256_encrypt(msg, key, iv, &buffer)) { return -1; } return pbbase64_encode_std(buffer, base64_str, n); }
int pubnub_decrypt_buffered(char const *cipher_key, char const *base64_str, pubnub_bymebl_t *data, pubnub_bymebl_t *buffer) { if (0 == pbbase64_decode_std_str(base64_str, buffer)) { uint8_t const iv[] = "0123456789012345"; uint8_t key[33]; cipher_hash(cipher_key, key); buffer->ptr[buffer->size] = '\0'; return pbaes256_decrypt(*buffer, key, iv, data); } return -1; }
int pubnub_encrypt(char const *cipher_key, pubnub_bymebl_t msg, char *base64_str, size_t *n) { pubnub_bymebl_t encrypted; uint8_t const iv[] = "0123456789012345"; uint8_t key[33]; int result; cipher_hash(cipher_key, key); encrypted = pbaes256_encrypt_alloc(msg, key, iv); if (NULL == encrypted.ptr) { return -1; } result = pbbase64_encode_std(encrypted, base64_str, n); free(encrypted.ptr); return result; }
pubnub_bymebl_t pubnub_decrypt_alloc(char const *cipher_key, char const *base64_str) { pubnub_bymebl_t decoded; uint8_t iv[] = "0123456789012345"; uint8_t key[33]; cipher_hash(cipher_key, key); decoded = pbbase64_decode_alloc_std_str(base64_str); if (decoded.ptr != NULL) { pubnub_bymebl_t result; decoded.ptr[decoded.size] = '\0'; result = pbaes256_decrypt_alloc(decoded, key, iv); free(decoded.ptr); return result; } return decoded; }
remote_t remote_new(lob_t key, uint8_t *token) { uint8_t hash[32]; remote_t remote; if(!key || key->body_len != uECC_BYTES+1) return LOG("invalid key %d != %d",(key)?key->body_len:0,uECC_BYTES+1); if(!(remote = malloc(sizeof(struct remote_struct)))) return NULL; memset(remote,0,sizeof (struct remote_struct)); // copy in key and make ephemeral ones uECC_decompress(key->body,remote->key); uECC_make_key(remote->ekey, remote->esecret); uECC_compress(remote->ekey, remote->ecomp); if(token) { cipher_hash(remote->ecomp,16,hash); memcpy(token,hash,16); } // generate a random seq starting point for message IV's e3x_rand((uint8_t*)&(remote->seq),4); return remote; }
remote_t remote_new(lob_t key, uint8_t *token) { uint8_t hash[32]; remote_t remote; if(!key) return LOG("missing key"); if(key->body_len != crypto_box_PUBLICKEYBYTES) return LOG("invalid key %d != %d",key->body_len,crypto_box_PUBLICKEYBYTES); if(!(remote = malloc(sizeof(struct remote_struct)))) return NULL; memset(remote,0,sizeof (struct remote_struct)); // copy in key and make ephemeral ones memcpy(remote->key,key->body,key->body_len); crypto_box_keypair(remote->ekey,remote->esecret); // set token if wanted if(token) { cipher_hash(remote->ekey,16,hash); memcpy(token,hash,16); } return remote; }