49 #define NOISE_KEY_VERSION 1
54 #define NOISE_KEY_SALT_LEN 16
59 #define NOISE_KEY_ITERATIONS 20000
65 #define NOISE_ENC_KEY_OVERHEAD 128
87 static int noise_load_file
102 file = fopen(filename,
"rb");
108 if (fseek(file, 0L, SEEK_END) >= 0) {
109 off_t length = ftell(file);
114 if (fseek(file, 0L, SEEK_SET) < 0) {
118 pbuf->
size = (size_t)length;
122 pbuf->
data = (uint8_t *)malloc(pbuf->
size);
129 size = fread(pbuf->
data, 1, pbuf->
size, file);
130 if (!size && ferror(file)) {
182 int err = noise_load_file(filename, &pbuf);
186 noise_load_free(&pbuf);
283 int err = noise_load_file(filename, &pbuf);
287 noise_load_free(&pbuf);
377 const void *passphrase,
size_t passphrase_len)
380 int err = noise_load_file(filename, &pbuf);
384 (key, &pbuf, passphrase, passphrase_len);
385 noise_load_free(&pbuf);
401 static int noise_parse_protect_name
402 (
const char *name,
int *cipher_id,
int *hash_id)
407 end = strchr(name,
'_');
414 end = strchr(name,
'_');
421 if (strcmp(name,
"PBKDF2") != 0)
452 const void *passphrase,
size_t passphrase_len)
457 uint8_t key_data[40];
458 int cipher_id, hash_id;
467 if (!pbuf || !passphrase)
486 err = noise_parse_protect_name
488 &cipher_id, &hash_id);
502 memset(&buf, 0,
sizeof(buf));
506 (hash, (
const uint8_t *)passphrase, passphrase_len,
510 key_data,
sizeof(key_data));
517 (cipher, (((uint64_t)(key_data[32])) << 56) |
518 (((uint64_t)(key_data[33])) << 48) |
519 (((uint64_t)(key_data[34])) << 40) |
520 (((uint64_t)(key_data[35])) << 32) |
521 (((uint64_t)(key_data[36])) << 24) |
522 (((uint64_t)(key_data[37])) << 16) |
523 (((uint64_t)(key_data[38])) << 8) |
524 ((uint64_t)(key_data[39])));
555 typedef int (*NoiseWriteFunc)(
NoiseProtobuf *pbuf,
int tag,
const void *obj);
568 static int noise_save_to_file
569 (
const void *obj,
const char *filename, NoiseWriteFunc func)
578 if (!obj || !filename)
583 err = (*func)(&pbuf, 0, obj);
591 pbuf.data = (uint8_t *)malloc(size);
597 err = (*func)(&pbuf, 0, obj);
606 file = fopen(filename,
"wb");
608 if (fwrite(data, 1, size, file) != size)
639 return noise_save_to_file
680 return noise_save_to_file
728 const void *passphrase,
size_t passphrase_len,
729 const char *protect_name)
733 int cipher_id, hash_id;
738 if (!key || !filename || !passphrase || !protect_name)
740 err = noise_parse_protect_name(protect_name, &cipher_id, &hash_id);
752 size += strlen(protect_name) + NOISE_ENC_KEY_OVERHEAD;
755 pbuf.
data = (uint8_t *)malloc(size);
762 (key, &pbuf, passphrase, passphrase_len, protect_name);
764 (pbuf.
size - pbuf.
posn) > NOISE_MAX_PAYLOAD_LEN) {
770 file = fopen(filename,
"wb");
773 if (fwrite(pbuf.
data + pbuf.
posn, 1, len, file) != len)
809 const void *passphrase,
size_t passphrase_len,
810 const char *protect_name)
814 uint8_t salt[NOISE_KEY_SALT_LEN];
815 uint8_t key_data[40];
816 int cipher_id, hash_id;
825 if (!key || !pbuf || !passphrase || !protect_name)
827 err = noise_parse_protect_name(protect_name, &cipher_id, &hash_id);
852 if (pcopy.
posn < mac_len) {
855 pcopy.
posn -= mac_len;
872 (enc_key, protect_name, strlen(protect_name));
876 (enc_key, NOISE_KEY_ITERATIONS);
886 (hash, (
const uint8_t *)passphrase, passphrase_len,
887 salt,
sizeof(salt), NOISE_KEY_ITERATIONS,
888 key_data,
sizeof(key_data));
895 (cipher, (((uint64_t)(key_data[32])) << 56) |
896 (((uint64_t)(key_data[33])) << 48) |
897 (((uint64_t)(key_data[34])) << 40) |
898 (((uint64_t)(key_data[35])) << 32) |
899 (((uint64_t)(key_data[36])) << 24) |
900 (((uint64_t)(key_data[37])) << 16) |
901 (((uint64_t)(key_data[38])) << 8) |
902 ((uint64_t)(key_data[39])));
#define NOISE_MAX_PAYLOAD_LEN
Maximum payload length for Noise packets.
const char * Noise_EncryptedPrivateKey_get_algorithm(const Noise_EncryptedPrivateKey *obj)
int Noise_Certificate_write(NoiseProtobuf *pbuf, int tag, const Noise_Certificate *obj)
int noise_save_certificate_to_buffer(const Noise_Certificate *cert, NoiseProtobuf *pbuf)
Saves a certificate to a protobuf.
int noise_load_certificate_chain_from_buffer(Noise_CertificateChain **chain, NoiseProtobuf *pbuf)
Loads a certificate chain from a protobuf.
int Noise_EncryptedPrivateKey_has_salt(const Noise_EncryptedPrivateKey *obj)
int Noise_Certificate_free(Noise_Certificate *obj)
int noise_cipherstate_init_key(NoiseCipherState *state, const uint8_t *key, size_t key_len)
Initializes the key on a CipherState object.
int noise_protobuf_prepare_input(NoiseProtobuf *pbuf, const uint8_t *data, size_t size)
uint32_t Noise_EncryptedPrivateKey_get_iterations(const Noise_EncryptedPrivateKey *obj)
void noise_clean(void *data, size_t size)
Cleans a block of memory to destroy its contents.
int noise_hashstate_pbkdf2(NoiseHashState *state, const uint8_t *passphrase, size_t passphrase_len, const uint8_t *salt, size_t salt_len, size_t iterations, uint8_t *output, size_t output_len)
Hashes a passphrase and salt using the PBKDF2 key derivation function.
const void * Noise_EncryptedPrivateKey_get_encrypted_data(const Noise_EncryptedPrivateKey *obj)
int Noise_EncryptedPrivateKey_write(NoiseProtobuf *pbuf, int tag, const Noise_EncryptedPrivateKey *obj)
#define NOISE_ERROR_INVALID_PARAM
Invalid parameter to function; e.g. a NULL value.
Internal structure of the NoiseHashState type.
int noise_save_private_key_to_file(const Noise_PrivateKey *key, const char *filename, const void *passphrase, size_t passphrase_len, const char *protect_name)
Saves a private key in encrypted form to a file.
#define NOISE_ERROR_NONE
Success, no error.
int noise_protobuf_read_at_end_element(const NoiseProtobuf *pbuf, size_t end_posn)
int noise_load_private_key_from_file(Noise_PrivateKey **key, const char *filename, const void *passphrase, size_t passphrase_len)
Loads a private key from a file.
int Noise_EncryptedPrivateKey_has_algorithm(const Noise_EncryptedPrivateKey *obj)
int noise_cipherstate_set_nonce(NoiseCipherState *state, uint64_t nonce)
Sets the nonce value for this cipherstate object.
#define noise_buffer_set_input(buffer, ptr, len)
Sets a NoiseBuffer object to point to an input memory region.
int noise_cipherstate_decrypt_with_ad(NoiseCipherState *state, const uint8_t *ad, size_t ad_len, NoiseBuffer *buffer)
Decrypts a block of data with this CipherState object.
int Noise_CertificateChain_write(NoiseProtobuf *pbuf, int tag, const Noise_CertificateChain *obj)
int Noise_PrivateKey_write(NoiseProtobuf *pbuf, int tag, const Noise_PrivateKey *obj)
int noise_cipherstate_free(NoiseCipherState *state)
Frees a CipherState object after destroying all sensitive material.
int noise_load_certificate_from_file(Noise_Certificate **cert, const char *filename)
Loads a certificate from a file.
int Noise_EncryptedPrivateKey_new(Noise_EncryptedPrivateKey **obj)
const void * Noise_EncryptedPrivateKey_get_salt(const Noise_EncryptedPrivateKey *obj)
int noise_load_certificate_from_buffer(Noise_Certificate **cert, NoiseProtobuf *pbuf)
Loads a certificate from a protobuf.
#define NOISE_HASH_CATEGORY
Category for hash algorithms.
Main header file to include the Noise protocol library definitions.
int noise_cipherstate_encrypt_with_ad(NoiseCipherState *state, const uint8_t *ad, size_t ad_len, NoiseBuffer *buffer)
Encrypts a block of data with this CipherState object.
int noise_hashstate_new_by_id(NoiseHashState **state, int id)
Creates a new HashState object by its algorithm identifier.
int noise_protobuf_peek_tag(const NoiseProtobuf *pbuf)
int noise_save_certificate_chain_to_buffer(const Noise_CertificateChain *chain, NoiseProtobuf *pbuf)
Saves a certificate chain to a protobuf.
size_t noise_cipherstate_get_mac_length(const NoiseCipherState *state)
Gets the length of packet MAC values for a CipherState object.
int Noise_EncryptedPrivateKey_free(Noise_EncryptedPrivateKey *obj)
#define NOISE_ERROR_NO_MEMORY
Insufficient memory to complete the operation.
#define NOISE_ERROR_UNKNOWN_NAME
Algorithm name is unknown.
#define NOISE_ERROR_INVALID_LENGTH
Invalid length specified for a key, packet, etc.
int Noise_CertificateChain_free(Noise_CertificateChain *obj)
uint32_t Noise_EncryptedPrivateKey_get_version(const Noise_EncryptedPrivateKey *obj)
int Noise_CertificateChain_insert_certs(Noise_CertificateChain *obj, size_t index, Noise_Certificate *value)
size_t Noise_EncryptedPrivateKey_get_size_salt(const Noise_EncryptedPrivateKey *obj)
int noise_protobuf_finish_output(NoiseProtobuf *pbuf, uint8_t **data, size_t *size)
int noise_name_to_id(int category, const char *name, size_t name_len)
Maps an algorithm name to the corresponding identifier.
int noise_save_private_key_to_buffer(const Noise_PrivateKey *key, NoiseProtobuf *pbuf, const void *passphrase, size_t passphrase_len, const char *protect_name)
Saves a private key in encrypted form to a protobuf.
#define NOISE_CIPHER_CATEGORY
Category for cipher algorithms.
int noise_randstate_generate_simple(uint8_t *buffer, size_t len)
Generates random data without first creating a RandState object.
size_t Noise_EncryptedPrivateKey_get_size_encrypted_data(const Noise_EncryptedPrivateKey *obj)
int noise_save_certificate_chain_to_file(const Noise_CertificateChain *chain, const char *filename)
Saves a certificate chain to a file.
void noise_free(void *ptr, size_t size)
Destroys the contents of a block of memory and free it.
int noise_load_private_key_from_buffer(Noise_PrivateKey **key, NoiseProtobuf *pbuf, const void *passphrase, size_t passphrase_len)
Loads a private key from a protobuf.
int Noise_EncryptedPrivateKey_set_iterations(Noise_EncryptedPrivateKey *obj, uint32_t value)
int noise_protobuf_finish_measure(NoiseProtobuf *pbuf, size_t *size)
int Noise_EncryptedPrivateKey_has_iterations(const Noise_EncryptedPrivateKey *obj)
Internal structure of the NoiseCipherState type.
int noise_hashstate_free(NoiseHashState *state)
Frees a HashState object after destroying all sensitive material.
#define NOISE_ERROR_SYSTEM
System error, with more information in errno.
int Noise_EncryptedPrivateKey_has_encrypted_data(const Noise_EncryptedPrivateKey *obj)
int noise_save_certificate_to_file(const Noise_Certificate *cert, const char *filename)
Saves a certificate to a file.
int Noise_CertificateChain_new(Noise_CertificateChain **obj)
int Noise_EncryptedPrivateKey_set_algorithm(Noise_EncryptedPrivateKey *obj, const char *value, size_t size)
int noise_cipherstate_new_by_id(NoiseCipherState **state, int id)
Creates a new CipherState object by its algorithm identifier.
int Noise_EncryptedPrivateKey_set_encrypted_data(Noise_EncryptedPrivateKey *obj, const void *value, size_t size)
int Noise_EncryptedPrivateKey_set_salt(Noise_EncryptedPrivateKey *obj, const void *value, size_t size)
int Noise_PrivateKey_read(NoiseProtobuf *pbuf, int tag, Noise_PrivateKey **obj)
int Noise_EncryptedPrivateKey_set_version(Noise_EncryptedPrivateKey *obj, uint32_t value)
#define NOISE_HASH_NONE
Hash identifier that indicates "no hash".
int noise_protobuf_read_end_element(NoiseProtobuf *pbuf, size_t end_posn)
#define noise_buffer_set_inout(buffer, ptr, len, max)
Sets a NoiseBuffer object to point to an input-output memory region.
int noise_protobuf_read_start_element(NoiseProtobuf *pbuf, int tag, size_t *end_posn)
int noise_load_certificate_chain_from_file(Noise_CertificateChain **chain, const char *filename)
Loads a certificate chain from a file.
size_t noise_cipherstate_get_key_length(const NoiseCipherState *state)
Gets the length of the encryption key for a CipherState object.
int Noise_Certificate_read(NoiseProtobuf *pbuf, int tag, Noise_Certificate **obj)
int Noise_CertificateChain_read(NoiseProtobuf *pbuf, int tag, Noise_CertificateChain **obj)
#define NOISE_CIPHER_NONE
Cipher identifier that indicates "no cipher".
int noise_protobuf_prepare_measure(NoiseProtobuf *pbuf, size_t max_size)
#define NOISE_ERROR_INVALID_FORMAT
Invalid format for packet or key file.
Type that defines a region of memory for a data buffer.
int Noise_EncryptedPrivateKey_read(NoiseProtobuf *pbuf, int tag, Noise_EncryptedPrivateKey **obj)