Make sure the crypto wrapper functions can actually be compiled.
This commit is contained in:
parent
e8689a4753
commit
19413a8048
3 changed files with 48 additions and 43 deletions
33
src/cipher.c
33
src/cipher.c
|
@ -22,12 +22,14 @@
|
||||||
#include "system.h"
|
#include "system.h"
|
||||||
|
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
|
#include "logger.h"
|
||||||
|
#include "xalloc.h"
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
const char *name,
|
const char *name;
|
||||||
enum gcry_cipher_algos algo,
|
int algo;
|
||||||
enum gcry_cipher_modes mode,
|
int mode;
|
||||||
int nid,
|
int nid;
|
||||||
} ciphertable[] = {
|
} ciphertable[] = {
|
||||||
{"none", GCRY_CIPHER_NONE, GCRY_CIPHER_MODE_NONE, 0},
|
{"none", GCRY_CIPHER_NONE, GCRY_CIPHER_MODE_NONE, 0},
|
||||||
|
|
||||||
|
@ -52,7 +54,7 @@ static struct {
|
||||||
{NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, 428},
|
{NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, 428},
|
||||||
};
|
};
|
||||||
|
|
||||||
static bool nametocipher(const char *name, enum gcry_cipher_algos *algo, enum gcry_cipher_modes *mode) {
|
static bool nametocipher(const char *name, int *algo, int *mode) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
|
for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
|
||||||
|
@ -66,7 +68,7 @@ static bool nametocipher(const char *name, enum gcry_cipher_algos *algo, enum gc
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool nidtocipher(int nid, enum gcry_cipher_algos *algo, enum gcry_cipher_modes *mode) {
|
static bool nidtocipher(int nid, int *algo, int *mode) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
|
for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
|
||||||
|
@ -80,7 +82,7 @@ static bool nidtocipher(int nid, enum gcry_cipher_algos *algo, enum gcry_cipher_
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ciphertonid(enum gcry_cipher_algos algo, enum gcry_cipher_modes mode, int *nid) {
|
static bool ciphertonid(int algo, int mode, int *nid) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
|
for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
|
||||||
|
@ -97,7 +99,7 @@ static bool cipher_open(cipher_t *cipher, int algo, int mode) {
|
||||||
gcry_error_t err;
|
gcry_error_t err;
|
||||||
|
|
||||||
if(!ciphertonid(algo, mode, &cipher->nid)) {
|
if(!ciphertonid(algo, mode, &cipher->nid)) {
|
||||||
logger(LOG_DEBUG< _("Cipher %d mode %d has no corresponding nid!"), algo, mode);
|
logger(LOG_DEBUG, _("Cipher %d mode %d has no corresponding nid!"), algo, mode);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,11 +109,11 @@ static bool cipher_open(cipher_t *cipher, int algo, int mode) {
|
||||||
}
|
}
|
||||||
|
|
||||||
cipher->keylen = gcry_cipher_get_algo_keylen(algo);
|
cipher->keylen = gcry_cipher_get_algo_keylen(algo);
|
||||||
if(mode == GCRY_MODE_ECB || mode == GCRY_MODE_CBC)
|
if(mode == GCRY_CIPHER_MODE_ECB || mode == GCRY_CIPHER_MODE_CBC)
|
||||||
cipher->blklen = gcry_cipher_get_algo_blklen(algo);
|
cipher->blklen = gcry_cipher_get_algo_blklen(algo);
|
||||||
else
|
else
|
||||||
cipher->blklen = 0;
|
cipher->blklen = 0;
|
||||||
cipher->key = xmalloc(cipher->keylen, cipher->blklen);
|
cipher->key = xmalloc(cipher->keylen + cipher->blklen);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -158,7 +160,7 @@ bool cipher_regenerate_key(cipher_t *cipher) {
|
||||||
gcry_create_nonce(cipher->key, cipher->keylen + cipher->blklen);
|
gcry_create_nonce(cipher->key, cipher->keylen + cipher->blklen);
|
||||||
|
|
||||||
gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
|
gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
|
||||||
gcry_cipher_setiv(cipher->handle, cipher->key + keylen, cipher->blklen);
|
gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -169,9 +171,10 @@ bool cipher_add_padding(cipher_t *cipher, void *indata, size_t inlen, size_t *ou
|
||||||
if(cipher->blklen == 1) {
|
if(cipher->blklen == 1) {
|
||||||
*outlen = inlen;
|
*outlen = inlen;
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
reqlen = ((inlen + 1) / cipher->blklen) * cipher->blklen;
|
reqlen = ((inlen + 1) / cipher->blklen) * cipher->blklen;
|
||||||
if(reqlen > outlen)
|
if(reqlen > *outlen)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// add padding
|
// add padding
|
||||||
|
@ -200,7 +203,7 @@ bool cipher_remove_padding(cipher_t *cipher, void *indata, size_t inlen, size_t
|
||||||
bool cipher_encrypt(cipher_t *cipher, void *indata, size_t inlen, void *outdata, size_t *outlen) {
|
bool cipher_encrypt(cipher_t *cipher, void *indata, size_t inlen, void *outdata, size_t *outlen) {
|
||||||
gcry_error_t err;
|
gcry_error_t err;
|
||||||
|
|
||||||
if((err = gcry_cipher_encrypt(cipher->handle, oudata, inlen, indata, inlen))) {
|
if((err = gcry_cipher_encrypt(cipher->handle, outdata, inlen, indata, inlen))) {
|
||||||
logger(LOG_ERR, _("Error while encrypting"));
|
logger(LOG_ERR, _("Error while encrypting"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -211,7 +214,7 @@ bool cipher_encrypt(cipher_t *cipher, void *indata, size_t inlen, void *outdata,
|
||||||
bool cipher_decrypt(cipher_t *cipher, void *indata, size_t inlen, void *outdata, size_t *outlen) {
|
bool cipher_decrypt(cipher_t *cipher, void *indata, size_t inlen, void *outdata, size_t *outlen) {
|
||||||
gcry_error_t err;
|
gcry_error_t err;
|
||||||
|
|
||||||
if((err = gcry_cipher_decrypt(cipher->handle, oudata, inlen, indata, inlen))) {
|
if((err = gcry_cipher_decrypt(cipher->handle, outdata, inlen, indata, inlen))) {
|
||||||
logger(LOG_ERR, _("Error while encrypting"));
|
logger(LOG_ERR, _("Error while encrypting"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -221,7 +224,7 @@ bool cipher_decrypt(cipher_t *cipher, void *indata, size_t inlen, void *outdata,
|
||||||
|
|
||||||
void cipher_reset(cipher_t *cipher) {
|
void cipher_reset(cipher_t *cipher) {
|
||||||
gcry_cipher_reset(cipher->handle);
|
gcry_cipher_reset(cipher->handle);
|
||||||
gcry_cipher_setiv(cipher->handle, cipher->key + keylen, cipher->blklen);
|
gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cipher_get_nid(cipher_t *cipher) {
|
int cipher_get_nid(cipher_t *cipher) {
|
||||||
|
|
44
src/digest.c
44
src/digest.c
|
@ -22,11 +22,12 @@
|
||||||
#include "system.h"
|
#include "system.h"
|
||||||
|
|
||||||
#include "digest.h"
|
#include "digest.h"
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
const char *name,
|
const char *name;
|
||||||
enum gcry_md_algos algo,
|
int algo;
|
||||||
int nid,
|
int nid;
|
||||||
} digesttable[] = {
|
} digesttable[] = {
|
||||||
{"none", GCRY_MD_NONE, 0},
|
{"none", GCRY_MD_NONE, 0},
|
||||||
{"sha1", GCRY_MD_SHA1, 64},
|
{"sha1", GCRY_MD_SHA1, 64},
|
||||||
|
@ -35,7 +36,20 @@ static struct {
|
||||||
{"sha512", GCRY_MD_SHA512, 674},
|
{"sha512", GCRY_MD_SHA512, 674},
|
||||||
};
|
};
|
||||||
|
|
||||||
static bool nidtodigest(int nid, enum gcry_md_algos *algo) {
|
static bool nametodigest(const char *name, int *algo) {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
|
||||||
|
if(digesttable[i].name && !strcasecmp(name, digesttable[i].name)) {
|
||||||
|
*algo = digesttable[i].algo;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool nidtodigest(int nid, int *algo) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
|
for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
|
||||||
|
@ -48,7 +62,7 @@ static bool nidtodigest(int nid, enum gcry_md_algos *algo) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool digesttonid(enum gcry_md_algos algo, int *nid) {
|
static bool digesttonid(int algo, int *nid) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
|
for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
|
||||||
|
@ -63,7 +77,7 @@ static bool digesttonid(enum gcry_md_algos algo, int *nid) {
|
||||||
|
|
||||||
static bool digest_open(digest_t *digest, int algo) {
|
static bool digest_open(digest_t *digest, int algo) {
|
||||||
if(!digesttonid(algo, &digest->nid)) {
|
if(!digesttonid(algo, &digest->nid)) {
|
||||||
logger(LOG_DEBUG< _("Digest %d has no corresponding nid!"), algo);
|
logger(LOG_DEBUG, _("Digest %d has no corresponding nid!"), algo);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +105,7 @@ bool digest_open_by_nid(digest_t *digest, int nid) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return digest_open(digest, algo, mode);
|
return digest_open(digest, algo);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool digest_open_sha1(digest_t *digest) {
|
bool digest_open_sha1(digest_t *digest) {
|
||||||
|
@ -102,26 +116,14 @@ void digest_close(digest_t *digest) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool digest_create(digest_t *digest, void *indata, size_t inlen, void *outdata) {
|
bool digest_create(digest_t *digest, void *indata, size_t inlen, void *outdata) {
|
||||||
gcry_error_t err;
|
gcry_md_hash_buffer(digest->algo, outdata, indata, inlen);
|
||||||
|
|
||||||
if((err = gcry_md_hash_buffer(digest->algo, outdata, indata, inlen))) {
|
|
||||||
logger(LOG_ERR, _("Error while creating digest!"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
*outlen = digest->len;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool digest_verify(digest_t *digest, void *indata, size_t inlen, void *cmpdata) {
|
bool digest_verify(digest_t *digest, void *indata, size_t inlen, void *cmpdata) {
|
||||||
gcry_error_t err;
|
|
||||||
char outdata[digest->len];
|
char outdata[digest->len];
|
||||||
|
|
||||||
if((err = gcry_md_hash_buffer(digest->algo, outdata, indata, inlen))) {
|
gcry_md_hash_buffer(digest->algo, outdata, indata, inlen);
|
||||||
logger(LOG_ERR, _("Error while creating digest!"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return !memcmp(indata, outdata, digest->len);
|
return !memcmp(indata, outdata, digest->len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
src/digest.h
14
src/digest.h
|
@ -25,16 +25,16 @@
|
||||||
#include <gcrypt.h>
|
#include <gcrypt.h>
|
||||||
|
|
||||||
typedef struct digest {
|
typedef struct digest {
|
||||||
enum gcry_md_algos algo;
|
int algo;
|
||||||
int nid;
|
int nid;
|
||||||
uint16_t len;
|
uint16_t len;
|
||||||
} digest_t;
|
} digest_t;
|
||||||
|
|
||||||
bool digest_open_by_name(struct digest_t *, const char *);
|
bool digest_open_by_name(struct digest *, const char *);
|
||||||
bool digest_open_by_nid(struct digest_t *, int);
|
bool digest_open_by_nid(struct digest *, int);
|
||||||
bool digest_open_sha1(struct digest_t *);
|
bool digest_open_sha1(struct digest *);
|
||||||
bool digest_create(struct digest_t *, void *indata, size_t inlen, void *outdata, size_t *outlen);
|
bool digest_create(struct digest *, void *indata, size_t inlen, void *outdata);
|
||||||
bool digest_verify(struct digest_t *, void *indata, size_t inlen, void *digestdata, size_t digestlen);
|
bool digest_verify(struct digest *, void *indata, size_t inlen, void *digestdata);
|
||||||
int digest_get_nid(struct digest_t *);
|
int digest_get_nid(struct digest *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue