1
0
mirror of https://https.git.savannah.gnu.org/git/gnulib.git synced 2026-04-28 06:33:36 +00:00
Files
gnulib/lib/gc-pbkdf2.c
Paul Eggert 8765694360 crypto: add ‘restrict’ to .h files
Use ‘restrict’ on pointer args when appropriate.
It suffices to do this in .h files, as .c files inherit it.
For style, prefer qualifiers after types, to be consistent with
putting ‘restrict’ after types.
* lib/af_alg.h (afalg_buffer, afalg_stream):
* lib/arcfour.h (arcfour_stream, arcfour_setkey):
* lib/arctwo.h (arctwo_setkey_ekb, arctwo_encrypt, arctwo_decrypt):
* lib/des.h (gl_des_setkey, gl_des_makekey, gl_des_ecb_crypt)
(gl_3des_set2keys, gl_3des_set3keys, gl_3des_makekey)
(gl_3des_ecb_crypt):
* lib/gc.h (gc_cipher_setkey, gc_cipher_setiv)
(gc_cipher_encrypt_inline, gc_cipher_decrypt_inline, gc_hash_clone)
(gc_hash_hmac_setkey, gc_hash_write, gc_hash_buffer, gc_md2)
(gc_md4, gc_md5, gc_sha1, gc_sha256, gc_sha512, gc_sm3)
(gc_hmac_md5, gc_hmac_sha1, gc_hmac_sha256, gc_hmac_sha512)
(gc_pbkdf2_hmac, gc_pbkdf2_sha1):
* lib/gl_openssl.h (GL_CRYPTO_FN (_process_bytes))
(GL_CRYPTO_FN (_process_block), GL_CRYPTO_FN (_finish_ctx))
(GL_CRYPTO_FN (_buffer), GL_CRYPTO_FN (_read_ctx)):
* lib/hmac.h (hmac_md5, hmac_sha1, hmac_sha256, hmac_sha512):
* lib/md2.h (md2_process_block, md2_process_bytes, md2_finish_ctx)
(md2_read_ctx, md2_buffer, md2_stream):
* lib/md4.h (md4_process_block, md4_process_bytes, md4_finish_ctx)
(md4_read_ctx, md4_buffer, md4_stream):
* lib/md5.h (__md5_process_block, __md5_process_bytes, __md5_finish_ctx)
(__md5_read_ctx, __md5_buffer, __md5_stream):
* lib/rijndael-alg-fst.h (rijndaelKeySetupEnc)
(rijndaelKeySetupDec, rijndaelEncrypt, rijndaelDecrypt):
* lib/rijndael-api-fst.h (rijndaelMakeKey, rijndaelCipherInit)
(rijndaelBlockEncrypt, rijndaelPadEncrypt, rijndaelBlockDecrypt)
(rijndaelPadDecrypt):
* lib/sha1.h (sha1_process_block, sha1_process_bytes)
(sha1_finish_ctx, sha1_read_ctx, sha1_buffer, sha1_stream):
* lib/sha256.h (sha256_process_block, sha256_process_bytes)
(sha256_finish_ctx, sha224_finish_ctx, sha256_read_ctx)
(sha224_read_ctx, sha256_buffer, sha224_buffer, sha256_stream)
(sha224_stream):
* lib/sha3.h (sha3_process_block, sha3_process_bytes)
(sha3_finish_ctx, sha3_read_ctx, sha3_224_buffer, sha3_256_buffer)
(sha3_384_buffer, sha3_512_buffer, sha3_224_stream)
(sha3_256_stream, sha3_384_stream, sha3_512_stream):
* lib/sha512.h (sha512_process_block, sha512_process_bytes)
(sha512_finish_ctx, sha384_finish_ctx, sha512_read_ctx)
(sha384_read_ctx, sha512_buffer, sha384_buffer, sha512_stream)
(sha384_stream):
* lib/sm3.h (sm3_process_block, sm3_process_bytes, sm3_finish_ctx)
(sm3_read_ctx, sm3_buffer, sm3_stream):
Add ‘restrict’ to pointer args.  All implementations changed.
2026-02-22 16:18:31 -08:00

133 lines
3.3 KiB
C

/* gc-pbkdf2.c --- Password-Based Key Derivation Function a'la PKCS#5
Copyright (C) 2002-2006, 2009-2026 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Simon Josefsson. */
#include <config.h>
#include "gc.h"
#include <stdlib.h>
#include <string.h>
typedef Gc_rc (*gc_prf_func) (void const *restrict key, size_t keylen,
void const *restrict in, size_t inlen,
char *restrict resbuf);
static Gc_rc
gc_pbkdf2_prf (gc_prf_func prf, size_t hLen,
char const *restrict P, size_t Plen,
char const *restrict S, size_t Slen,
unsigned int c,
char *restrict DK, size_t dkLen)
{
if (c == 0)
return GC_PKCS5_INVALID_ITERATION_COUNT;
if (dkLen == 0)
return GC_PKCS5_INVALID_DERIVED_KEY_LENGTH;
if (dkLen > 4294967295U)
return GC_PKCS5_DERIVED_KEY_TOO_LONG;
unsigned int l = ((dkLen - 1) / hLen) + 1;
unsigned int r = dkLen - (l - 1) * hLen;
size_t tmplen = Slen + 4;
char *tmp = malloc (tmplen);
if (tmp == NULL)
return GC_MALLOC_ERROR;
memcpy (tmp, S, Slen);
char U[GC_MAX_DIGEST_SIZE];
char T[GC_MAX_DIGEST_SIZE];
int rc;
for (unsigned int i = 1; i <= l; i++)
{
memset (T, 0, hLen);
for (unsigned int u = 1; u <= c; u++)
{
if (u == 1)
{
tmp[Slen + 0] = (i & 0xff000000) >> 24;
tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
tmp[Slen + 3] = (i & 0x000000ff) >> 0;
rc = prf (P, Plen, tmp, tmplen, U);
}
else
rc = prf (P, Plen, U, hLen, U);
if (rc != GC_OK)
{
free (tmp);
return rc;
}
for (unsigned int k = 0; k < hLen; k++)
T[k] ^= U[k];
}
memcpy (DK + (i - 1) * hLen, T, i == l ? r : hLen);
}
free (tmp);
return GC_OK;
}
Gc_rc
gc_pbkdf2_hmac (Gc_hash hash,
char const *restrict P, size_t Plen,
char const *restrict S, size_t Slen,
unsigned int c, char *restrict DK, size_t dkLen)
{
gc_prf_func prf;
size_t hLen;
switch (hash)
{
#if GNULIB_GC_HMAC_SHA1
case GC_SHA1:
prf = gc_hmac_sha1;
hLen = GC_SHA1_DIGEST_SIZE;
break;
#endif
#if GNULIB_GC_HMAC_SHA256
case GC_SHA256:
prf = gc_hmac_sha256;
hLen = GC_SHA256_DIGEST_SIZE;
break;
#endif
#if GNULIB_GC_HMAC_SHA512
case GC_SHA512:
prf = gc_hmac_sha512;
hLen = GC_SHA512_DIGEST_SIZE;
break;
#endif
default:
return GC_INVALID_HASH;
}
return gc_pbkdf2_prf (prf, hLen, P, Plen, S, Slen, c, DK, dkLen);
}