Mercurial > dropbear
view libtomcrypt/src/misc/hkdf/hkdf.c @ 1499:2d450c1056e3
options: Complete the transition to numeric toggles (`#if')
For the sake of review, this commit alters only the code; the affiliated
comments within the source files also need to be updated, but doing so
now would obscure the operational changes that have been made here.
* All on/off options have been switched to the numeric `#if' variant;
that is the only way to make this `default_options.h.in' thing work
in a reasonable manner.
* There is now some very minor compile-time checking of the user's
choice of options.
* NO_FAST_EXPTMOD doesn't seem to be used, so it has been removed.
* ENABLE_USER_ALGO_LIST was supposed to be renamed DROPBEAR_USER_ALGO_LIST,
and this commit completes that work.
* DROPBEAR_FUZZ seems to be a relatively new, as-yet undocumented option,
which was added by the following commit:
commit 6e0b539e9ca0b5628c6c5a3d118ad6a2e79e8039
Author: Matt Johnston <[email protected]>
Date: Tue May 23 22:29:21 2017 +0800
split out checkpubkey_line() separately
It has now been added to `sysoptions.h' and defined as `0' by default.
* The configuration option `DROPBEAR_PASSWORD_ENV' is no longer listed in
`default_options.h.in'; it is no longer meant to be set by the user, and
is instead left to be defined in `sysoptions.h' (where it was already being
defined) as merely the name of the environment variable in question:
DROPBEAR_PASSWORD
To enable or disable use of that environment variable, the user must now
toggle `DROPBEAR_USE_DROPBEAR_PASSWORD'.
* The sFTP support is now toggled by setting `DROPBEAR_SFTPSERVER', and the
path of the sFTP server program is set independently through the usual
SFTPSERVER_PATH.
author | Michael Witten <mfwitten@gmail.com> |
---|---|
date | Thu, 20 Jul 2017 19:38:26 +0000 |
parents | 6dba84798cd5 |
children |
line wrap: on
line source
/* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. */ #include <assert.h> #include <stdio.h> #include <stdlib.h> #include "tomcrypt.h" #ifdef LTC_HKDF /* This is mostly just a wrapper around hmac_memory */ int hkdf_extract(int hash_idx, const unsigned char *salt, unsigned long saltlen, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) { /* libtomcrypt chokes on a zero length HMAC key, so we need to check for that. HMAC specifies that keys shorter than the hash's blocksize are 0 padded to the block size. HKDF specifies that a NULL salt is to be substituted with a salt comprised of hashLen 0 bytes. HMAC's padding means that in either case the HMAC is actually using a blocksize long zero filled key. Unless blocksize < hashLen (which wouldn't make any sense), we can use a single 0 byte as the HMAC key and still generate valid results for HKDF. */ if (salt == NULL || saltlen == 0) { return hmac_memory(hash_idx, (const unsigned char *)"", 1, in, inlen, out, outlen); } else { return hmac_memory(hash_idx, salt, saltlen, in, inlen, out, outlen); } } int hkdf_expand(int hash_idx, const unsigned char *info, unsigned long infolen, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long outlen) { unsigned long hashsize; int err; unsigned char N; unsigned long Noutlen, outoff; unsigned char *T, *dat; unsigned long Tlen, datlen; /* make sure hash descriptor is valid */ if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { return err; } hashsize = hash_descriptor[hash_idx].hashsize; /* RFC5869 parameter restrictions */ if (inlen < hashsize || outlen > hashsize * 255) return CRYPT_INVALID_ARG; if (info == NULL && infolen != 0) return CRYPT_INVALID_ARG; LTC_ARGCHK(out != NULL); Tlen = hashsize + infolen + 1; T = XMALLOC(Tlen); /* Replace with static buffer? */ if (T == NULL) { return CRYPT_MEM; } if (info != NULL) { XMEMCPY(T + hashsize, info, infolen); } /* HMAC data T(1) doesn't include a previous hash value */ dat = T + hashsize; datlen = Tlen - hashsize; N = 0; outoff = 0; /* offset in out to write to */ while (1) { /* an exit condition breaks mid-loop */ Noutlen = MIN(hashsize, outlen - outoff); T[Tlen - 1] = ++N; if ((err = hmac_memory(hash_idx, in, inlen, dat, datlen, out + outoff, &Noutlen)) != CRYPT_OK) { zeromem(T, Tlen); XFREE(T); return err; } outoff += Noutlen; if (outoff >= outlen) /* loop exit condition */ break; /* All subsequent HMAC data T(N) DOES include the previous hash value */ XMEMCPY(T, out + hashsize * (N-1), hashsize); if (N == 1) { dat = T; datlen = Tlen; } } zeromem(T, Tlen); XFREE(T); return CRYPT_OK; } /* all in one step */ int hkdf(int hash_idx, const unsigned char *salt, unsigned long saltlen, const unsigned char *info, unsigned long infolen, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long outlen) { unsigned long hashsize; int err; unsigned char *extracted; /* make sure hash descriptor is valid */ if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { return err; } hashsize = hash_descriptor[hash_idx].hashsize; extracted = XMALLOC(hashsize); /* replace with static buffer? */ if (extracted == NULL) { return CRYPT_MEM; } if ((err = hkdf_extract(hash_idx, salt, saltlen, in, inlen, extracted, &hashsize)) != 0) { zeromem(extracted, hashsize); XFREE(extracted); return err; } err = hkdf_expand(hash_idx, info, infolen, extracted, hashsize, out, outlen); zeromem(extracted, hashsize); XFREE(extracted); return err; } #endif /* LTC_HKDF */ /* vim: set ts=2 sw=2 et ai si: */ /* ref: $Format:%D$ */ /* git commit: $Format:%H$ */ /* commit time: $Format:%ai$ */