Fix packet authentication.
This wasn't working at all, since we didn't do HMAC but just a plain hash. Also, verification of packets failed because it was checking the whole packet, not the packet minus the HMAC.
This commit is contained in:
parent
10d609b1f0
commit
c845bc109c
6 changed files with 86 additions and 37 deletions
|
|
@ -87,6 +87,7 @@ static bool digest_open(digest_t *digest, int algo, int maclength) {
|
|||
digest->maclength = maclength;
|
||||
|
||||
digest->algo = algo;
|
||||
digest->hmac = NULL;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -118,24 +119,45 @@ bool digest_open_sha1(digest_t *digest, int maclength) {
|
|||
}
|
||||
|
||||
void digest_close(digest_t *digest) {
|
||||
if(digest->hmac)
|
||||
gcry_md_close(digest->hmac);
|
||||
digest->hmac = NULL;
|
||||
}
|
||||
|
||||
bool digest_set_key(digest_t *digest, const void *key, size_t len) {
|
||||
if(!digest->hmac)
|
||||
gcry_md_open(&digest->hmac, digest->algo, GCRY_MD_FLAG_HMAC);
|
||||
if(!digest->hmac)
|
||||
return false;
|
||||
|
||||
return !gcry_md_setkey(digest->hmac, key, len);
|
||||
}
|
||||
|
||||
bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
|
||||
unsigned int len = gcry_md_get_algo_dlen(digest->algo);
|
||||
char tmpdata[len];
|
||||
|
||||
gcry_md_hash_buffer(digest->algo, tmpdata, indata, inlen);
|
||||
memcpy(outdata, tmpdata, digest->maclength);
|
||||
if(digest->hmac) {
|
||||
char *tmpdata;
|
||||
gcry_md_reset(digest->hmac);
|
||||
gcry_md_write(digest->hmac, indata, inlen);
|
||||
tmpdata = gcry_md_read(digest->hmac, digest->algo);
|
||||
if(!tmpdata)
|
||||
return false;
|
||||
memcpy(outdata, tmpdata, digest->maclength);
|
||||
} else {
|
||||
char tmpdata[len];
|
||||
gcry_md_hash_buffer(digest->algo, tmpdata, indata, inlen);
|
||||
memcpy(outdata, tmpdata, digest->maclength);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
|
||||
unsigned int len = gcry_md_get_algo_dlen(digest->algo);
|
||||
unsigned int len = digest->maclength;
|
||||
char outdata[len];
|
||||
|
||||
gcry_md_hash_buffer(digest->algo, outdata, indata, inlen);
|
||||
return !memcmp(cmpdata, outdata, digest->maclength);
|
||||
return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, len);
|
||||
}
|
||||
|
||||
int digest_get_nid(const digest_t *digest) {
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ typedef struct digest {
|
|||
int algo;
|
||||
int nid;
|
||||
int maclength;
|
||||
gcry_md_hd_t hmac;
|
||||
} digest_t;
|
||||
|
||||
extern bool digest_open_by_name(struct digest *, const char *name, int maclength);
|
||||
|
|
@ -36,6 +37,7 @@ extern bool digest_open_sha1(struct digest *, int maclength);
|
|||
extern void digest_close(struct digest *);
|
||||
extern bool digest_create(struct digest *, const void *indata, size_t inlen, void *outdata);
|
||||
extern bool digest_verify(struct digest *, const void *indata, size_t inlen, const void *digestdata);
|
||||
extern bool digest_set_key(struct digest *, const void *key, size_t len);
|
||||
extern int digest_get_nid(const struct digest *);
|
||||
extern size_t digest_length(const struct digest *);
|
||||
extern bool digest_active(const struct digest *);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue