libsec: add rfc5869 hmac-based key derivation function hkdf_x()

This commit is contained in:
cinap_lenrek 2015-09-02 11:30:17 +02:00
parent b749f36baa
commit 7ed22aaeec
3 changed files with 46 additions and 1 deletions

View file

@ -464,3 +464,8 @@ void curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]);
/* password-based key derivation function 2 (rfc2898) */ /* password-based key derivation function 2 (rfc2898) */
void pbkdf2_x(uchar *p, ulong plen, uchar *s, ulong slen, ulong rounds, uchar *d, ulong dlen, void pbkdf2_x(uchar *p, ulong plen, uchar *s, ulong slen, ulong rounds, uchar *d, ulong dlen,
DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen); DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);
/* hmac-based key derivation function (rfc5869) */
void hkdf_x(uchar *salt, ulong nsalt, uchar *info, ulong ninfo, uchar *key, ulong nkey, uchar *d, ulong dlen,
DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);

View file

@ -0,0 +1,39 @@
#include "os.h"
#include <mp.h>
#include <libsec.h>
/* rfc5869 */
void
hkdf_x(salt, nsalt, info, ninfo, key, nkey, d, dlen, x, xlen)
uchar *salt, *info, *key, *d;
ulong nsalt, ninfo, nkey, dlen;
DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
int xlen;
{
uchar prk[256], tmp[256], cnt;
DigestState *ds;
assert(xlen <= sizeof(tmp));
memset(tmp, 0, xlen);
if(nsalt == 0){
salt = tmp;
nsalt = xlen;
}
/* note that salt and key are swapped in this case */
(*x)(key, nkey, salt, nsalt, prk, nil);
ds = nil;
for(cnt=1;; cnt++) {
if(ninfo > 0)
ds = (*x)(info, ninfo, prk, xlen, nil, ds);
(*x)(&cnt, 1, prk, xlen, tmp, ds);
if(dlen <= xlen){
memmove(d, tmp, dlen);
break;
}
memmove(d, tmp, xlen);
dlen -= xlen;
d += xlen;
ds = (*x)(tmp, xlen, prk, xlen, nil, nil);
}
}

View file

@ -21,9 +21,10 @@ CFILES = des.c desmodes.c desECB.c desCBC.c des3ECB.c des3CBC.c\
ecc.c\ ecc.c\
ripemd.c\ ripemd.c\
dh.c\ dh.c\
pbkdf2.c\
curve25519.c\ curve25519.c\
curve25519_dh.c\ curve25519_dh.c\
pbkdf2.c\
hkdf.c\
ALLOFILES=${CFILES:%.c=%.$O} ALLOFILES=${CFILES:%.c=%.$O}