Paraslash Audio Streaming | |
About News Download Documentation Development |
Libgrcypt-based encryption/decryption routines. More...
#include <regex.h>
#include <gcrypt.h>
#include "para.h"
#include "error.h"
#include "string.h"
#include "crypt.h"
#include "crypt_backend.h"
#include "fd.h"
#include "base64.h"
Macros | |
#define | dump_buffer(a, b, c) |
Empty. More... | |
#define | RSA_PUBKEY_SEXP "(public-key (rsa (n %m) (e %m)))" |
S-expression for the public part of an RSA key. More... | |
#define | RSA_PRIVKEY_SEXP "(private-key (rsa (n %m) (e %m) (d %m) (p %m) (q %m) (u %m)))" |
S-expression for a private RSA key. More... | |
#define | RSA_DECRYPT_SEXP "(enc-val(flags oaep)(rsa(a %m)))" |
S-expression for decryption. More... | |
Enumerations | |
enum | asn1_types { ASN1_TYPE_INTEGER = 0x2, ASN1_TYPE_BIT_STRING = 0x03, ASN1_TYPE_SEQUENCE = 0x30 } |
ASN Types and their code. More... | |
Functions | |
void | hash_function (const char *data, unsigned long len, unsigned char *hash) |
Compute the hash of the given input data. More... | |
void | hash2_function (const char *data, unsigned long len, unsigned char *hash) |
Compute the hash2 of the given input data. More... | |
void | get_random_bytes_or_die (unsigned char *buf, int num) |
Fill a buffer with random content. More... | |
void | crypt_init (void) |
Initialize the crypto backend. More... | |
void | crypt_shutdown (void) |
Allocate all resources of the crypto backend. More... | |
int | apc_get_pubkey (const char *key_file, struct asymmetric_key **result) |
Read an asymmetric key from a file. More... | |
void | apc_free_pubkey (struct asymmetric_key *key) |
Deallocate a public key. More... | |
int | apc_priv_decrypt (const char *key_file, unsigned char **outbuf, unsigned char *inbuf, int inlen) |
Decrypt a buffer using a private key. More... | |
int | apc_pub_encrypt (struct asymmetric_key *pub, unsigned char *inbuf, unsigned len, unsigned char **outbuf) |
Encrypt a buffer using asymmetric keys. More... | |
struct stream_cipher * | sc_new (const unsigned char *data, int len) |
Allocate and initialize an aes_ctr128 stream cipher structure. More... | |
void | sc_free (struct stream_cipher *sc) |
Deallocate a stream cipher structure. More... | |
void | sc_crypt (struct stream_cipher *sc, struct iovec *src, struct iovec *dst) |
Encrypt or decrypt a buffer using a stream cipher. More... | |
Libgrcypt-based encryption/decryption routines.
#define dump_buffer | ( | a, | |
b, | |||
c | |||
) |
Empty.
Define GCRYPT_DEBUG to dump buffers.
#define RSA_PUBKEY_SEXP "(public-key (rsa (n %m) (e %m)))" |
S-expression for the public part of an RSA key.
#define RSA_PRIVKEY_SEXP "(private-key (rsa (n %m) (e %m) (d %m) (p %m) (q %m) (u %m)))" |
S-expression for a private RSA key.
#define RSA_DECRYPT_SEXP "(enc-val(flags oaep)(rsa(a %m)))" |
S-expression for decryption.
enum asn1_types |
void hash_function | ( | const char * | data, |
unsigned long | len, | ||
unsigned char * | hash | ||
) |
Compute the hash of the given input data.
data | Pointer to the data to compute the hash value from. |
len | The length of data in bytes. |
hash | Result pointer. |
hash must point to an area at least HASH_SIZE
bytes large.
References HASH_SIZE.
void hash2_function | ( | const char * | data, |
unsigned long | len, | ||
unsigned char * | hash | ||
) |
Compute the hash2 of the given input data.
data | Pointer to the data to compute the hash value from. |
len | The length of data in bytes. |
hash | Result pointer. |
hash must point to an area at least HASH2_SIZE
bytes large.
References HASH2_SIZE.
void get_random_bytes_or_die | ( | unsigned char * | buf, |
int | num | ||
) |
Fill a buffer with random content.
buf | The buffer to fill. |
num | The size of buf in bytes. |
This function puts num cryptographically strong pseudo-random bytes into buf. If it can not guarantee an unpredictable byte sequence (for example because the PRNG has not been seeded with enough randomness) the function logs an error message and calls exit().
Referenced by crypt_init().
void crypt_init | ( | void | ) |
Initialize the crypto backend.
This function initializes the crypto library and seeds the pseudo random number generator used by random() with a random seed obtained from the crypto implementation. On errors, an error message is logged and the function calls exit().
References get_random_bytes_or_die(), and PARA_EMERG_LOG.
void crypt_shutdown | ( | void | ) |
Allocate all resources of the crypto backend.
int apc_get_pubkey | ( | const char * | key_file, |
struct asymmetric_key ** | result | ||
) |
Read an asymmetric key from a file.
key_file | The file containing the key. |
result | The key structure is returned here. |
References decode_public_key(), and PARA_DEBUG_LOG.
void apc_free_pubkey | ( | struct asymmetric_key * | key | ) |
Deallocate a public key.
key | Pointer to the key structure to free. |
This should be called for keys obtained by apc_get_pubkey() if the key is no longer needed.
int apc_priv_decrypt | ( | const char * | key_file, |
unsigned char ** | outbuf, | ||
unsigned char * | inbuf, | ||
int | inlen | ||
) |
Decrypt a buffer using a private key.
key_file | Full path of the key. |
outbuf | The output buffer is allocated by the callee. |
inbuf | The encrypted input buffer. |
inlen | The length of inbuf. |
The outbuf must be large enough to hold at least 512 bytes.
int apc_pub_encrypt | ( | struct asymmetric_key * | pub, |
unsigned char * | inbuf, | ||
unsigned | len, | ||
unsigned char ** | outbuf | ||
) |
Encrypt a buffer using asymmetric keys.
pub | The public key. |
inbuf | The input buffer. |
len | The length of inbuf. |
outbuf | The output buffer will be allocated by the callee. |
struct stream_cipher* sc_new | ( | const unsigned char * | data, |
int | len | ||
) |
Allocate and initialize an aes_ctr128 stream cipher structure.
data | The key. |
len | The size of the key. |
References AES_CRT128_BLOCK_SIZE, and alloc().
void sc_free | ( | struct stream_cipher * | sc | ) |
Deallocate a stream cipher structure.
sc | A stream cipher previously obtained by sc_new(). |
void sc_crypt | ( | struct stream_cipher * | sc, |
struct iovec * | src, | ||
struct iovec * | dst | ||
) |
Encrypt or decrypt a buffer using a stream cipher.
sc | Crypto key. |
src | The source buffer and length. |
dst | The destination buffer and length, filled out by the function. |
It is up to the implementation to decide whether the crypt operation is performed in place. The caller can tell by looking if the buffers given by src and dst coincide after the call. If (and only if) the crypt operation was not performed in place, the function allocated a new buffer for the result, so dst->iov_base is different from src->iov_base. In this case, the destination buffer must be freed by the caller when it is no longer needed.