#include "system.h"
#include "cipher.h"
+#include "logger.h"
+#include "xalloc.h"
static struct {
- const char *name,
- enum gcry_cipher_algos algo,
- enum gcry_cipher_modes mode,
- int nid,
+ const char *name;
+ int algo;
+ int mode;
+ int nid;
} ciphertable[] = {
{"none", GCRY_CIPHER_NONE, GCRY_CIPHER_MODE_NONE, 0},
{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;
for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
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;
for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
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;
for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
gcry_error_t err;
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;
}
}
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);
else
cipher->blklen = 0;
- cipher->key = xmalloc(cipher->keylen, cipher->blklen);
+ cipher->key = xmalloc(cipher->keylen + cipher->blklen);
return true;
}
gcry_create_nonce(cipher->key, cipher->keylen + cipher->blklen);
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;
}
if(cipher->blklen == 1) {
*outlen = inlen;
return true;
+ }
reqlen = ((inlen + 1) / cipher->blklen) * cipher->blklen;
- if(reqlen > outlen)
+ if(reqlen > *outlen)
return false;
// add padding
bool cipher_encrypt(cipher_t *cipher, void *indata, size_t inlen, void *outdata, size_t *outlen) {
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"));
return false;
}
bool cipher_decrypt(cipher_t *cipher, void *indata, size_t inlen, void *outdata, size_t *outlen) {
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"));
return false;
}
void cipher_reset(cipher_t *cipher) {
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) {
#include "system.h"
#include "digest.h"
+#include "logger.h"
static struct {
- const char *name,
- enum gcry_md_algos algo,
- int nid,
+ const char *name;
+ int algo;
+ int nid;
} digesttable[] = {
{"none", GCRY_MD_NONE, 0},
{"sha1", GCRY_MD_SHA1, 64},
{"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;
for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
return false;
}
-static bool digesttonid(enum gcry_md_algos algo, int *nid) {
+static bool digesttonid(int algo, int *nid) {
int i;
for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
static bool digest_open(digest_t *digest, int algo) {
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;
}
- return digest_open(digest, algo, mode);
+ return digest_open(digest, algo);
}
bool digest_open_sha1(digest_t *digest) {
}
bool digest_create(digest_t *digest, void *indata, size_t inlen, void *outdata) {
- gcry_error_t err;
-
- if((err = gcry_md_hash_buffer(digest->algo, outdata, indata, inlen))) {
- logger(LOG_ERR, _("Error while creating digest!"));
- return false;
- }
-
- *outlen = digest->len;
+ gcry_md_hash_buffer(digest->algo, outdata, indata, inlen);
return true;
}
bool digest_verify(digest_t *digest, void *indata, size_t inlen, void *cmpdata) {
- gcry_error_t err;
char outdata[digest->len];
- if((err = gcry_md_hash_buffer(digest->algo, outdata, indata, inlen))) {
- logger(LOG_ERR, _("Error while creating digest!"));
- return false;
- }
-
+ gcry_md_hash_buffer(digest->algo, outdata, indata, inlen);
return !memcmp(indata, outdata, digest->len);
}
#include <gcrypt.h>
typedef struct digest {
- enum gcry_md_algos algo;
+ int algo;
int nid;
uint16_t len;
} digest_t;
-bool digest_open_by_name(struct digest_t *, const char *);
-bool digest_open_by_nid(struct digest_t *, int);
-bool digest_open_sha1(struct digest_t *);
-bool digest_create(struct digest_t *, void *indata, size_t inlen, void *outdata, size_t *outlen);
-bool digest_verify(struct digest_t *, void *indata, size_t inlen, void *digestdata, size_t digestlen);
-int digest_get_nid(struct digest_t *);
+bool digest_open_by_name(struct digest *, const char *);
+bool digest_open_by_nid(struct digest *, int);
+bool digest_open_sha1(struct digest *);
+bool digest_create(struct digest *, void *indata, size_t inlen, void *outdata);
+bool digest_verify(struct digest *, void *indata, size_t inlen, void *digestdata);
+int digest_get_nid(struct digest *);
#endif