diff --git a/cipher/blake2.c b/cipher/blake2.c index 642c5bfd..dc70eb48 100644 --- a/cipher/blake2.c +++ b/cipher/blake2.c @@ -1,986 +1,1037 @@ /* blake2.c - BLAKE2b and BLAKE2s hash functions (RFC 7693) * Copyright (C) 2017 Jussi Kivilinna * * This file is part of Libgcrypt. * * Libgcrypt 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. * * Libgcrypt 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 . */ /* The code is based on public-domain/CC0 BLAKE2 reference implementation * by Samual Neves, at https://github.com/BLAKE2/BLAKE2/tree/master/ref * Copyright 2012, Samuel Neves */ #include #include #include "g10lib.h" #include "bithelp.h" #include "bufhelp.h" #include "cipher.h" #include "hash-common.h" /* USE_AVX indicates whether to compile with Intel AVX code. */ #undef USE_AVX #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX) && \ (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \ defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) # define USE_AVX 1 #endif /* USE_AVX2 indicates whether to compile with Intel AVX2 code. */ #undef USE_AVX2 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX2) && \ (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \ defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) # define USE_AVX2 1 #endif /* AMD64 assembly implementations use SystemV ABI, ABI conversion and additional * stack to store XMM6-XMM15 needed on Win64. */ #undef ASM_FUNC_ABI #undef ASM_EXTRA_STACK #if defined(USE_AVX2) && defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS) # define ASM_FUNC_ABI __attribute__((sysv_abi)) # define ASM_EXTRA_STACK (10 * 16) #else # define ASM_FUNC_ABI # define ASM_EXTRA_STACK 0 #endif #define BLAKE2B_BLOCKBYTES 128 #define BLAKE2B_OUTBYTES 64 #define BLAKE2B_KEYBYTES 64 #define BLAKE2S_BLOCKBYTES 64 #define BLAKE2S_OUTBYTES 32 #define BLAKE2S_KEYBYTES 32 typedef struct { u64 h[8]; u64 t[2]; u64 f[2]; } BLAKE2B_STATE; struct blake2b_param_s { byte digest_length; byte key_length; byte fanout; byte depth; byte leaf_length[4]; byte node_offset[4]; byte xof_length[4]; byte node_depth; byte inner_length; byte reserved[14]; byte salt[16]; byte personal[16]; }; typedef struct BLAKE2B_CONTEXT_S { BLAKE2B_STATE state; byte buf[BLAKE2B_BLOCKBYTES]; size_t buflen; size_t outlen; #ifdef USE_AVX2 unsigned int use_avx2:1; #endif } BLAKE2B_CONTEXT; typedef struct { u32 h[8]; u32 t[2]; u32 f[2]; } BLAKE2S_STATE; struct blake2s_param_s { byte digest_length; byte key_length; byte fanout; byte depth; byte leaf_length[4]; byte node_offset[4]; byte xof_length[2]; byte node_depth; byte inner_length; /* byte reserved[0]; */ byte salt[8]; byte personal[8]; }; typedef struct BLAKE2S_CONTEXT_S { BLAKE2S_STATE state; byte buf[BLAKE2S_BLOCKBYTES]; size_t buflen; size_t outlen; #ifdef USE_AVX unsigned int use_avx:1; #endif } BLAKE2S_CONTEXT; typedef unsigned int (*blake2_transform_t)(void *S, const void *inblk, size_t nblks); static const u64 blake2b_IV[8] = { U64_C(0x6a09e667f3bcc908), U64_C(0xbb67ae8584caa73b), U64_C(0x3c6ef372fe94f82b), U64_C(0xa54ff53a5f1d36f1), U64_C(0x510e527fade682d1), U64_C(0x9b05688c2b3e6c1f), U64_C(0x1f83d9abfb41bd6b), U64_C(0x5be0cd19137e2179) }; static const u32 blake2s_IV[8] = { 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL }; static byte zero_block[BLAKE2B_BLOCKBYTES] = { 0, }; static void blake2_write(void *S, const void *inbuf, size_t inlen, byte *tmpbuf, size_t *tmpbuflen, size_t blkbytes, blake2_transform_t transform_fn) { const byte* in = inbuf; unsigned int burn = 0; if (inlen > 0) { size_t left = *tmpbuflen; size_t fill = blkbytes - left; size_t nblks; if (inlen > fill) { if (fill > 0) buf_cpy (tmpbuf + left, in, fill); /* Fill buffer */ left = 0; burn = transform_fn (S, tmpbuf, 1); /* Increment counter + Compress */ in += fill; inlen -= fill; nblks = inlen / blkbytes - !(inlen % blkbytes); if (nblks) { burn = transform_fn(S, in, nblks); in += blkbytes * nblks; inlen -= blkbytes * nblks; } } gcry_assert (inlen > 0); buf_cpy (tmpbuf + left, in, inlen); *tmpbuflen = left + inlen; } if (burn) _gcry_burn_stack (burn); return; } static inline void blake2b_set_lastblock(BLAKE2B_STATE *S) { S->f[0] = U64_C(0xffffffffffffffff); } static inline int blake2b_is_lastblock(const BLAKE2B_STATE *S) { return S->f[0] != 0; } static inline void blake2b_increment_counter(BLAKE2B_STATE *S, const int inc) { S->t[0] += (u64)inc; S->t[1] += (S->t[0] < (u64)inc) - (inc < 0); } static inline u64 rotr64(u64 x, u64 n) { return ((x >> (n & 63)) | (x << ((64 - n) & 63))); } static unsigned int blake2b_transform_generic(BLAKE2B_STATE *S, const void *inblks, size_t nblks) { static const byte blake2b_sigma[12][16] = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }, { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } }; const byte* in = inblks; u64 m[16]; u64 v[16]; while (nblks--) { /* Increment counter */ blake2b_increment_counter (S, BLAKE2B_BLOCKBYTES); /* Compress */ m[0] = buf_get_le64 (in + 0 * sizeof(m[0])); m[1] = buf_get_le64 (in + 1 * sizeof(m[0])); m[2] = buf_get_le64 (in + 2 * sizeof(m[0])); m[3] = buf_get_le64 (in + 3 * sizeof(m[0])); m[4] = buf_get_le64 (in + 4 * sizeof(m[0])); m[5] = buf_get_le64 (in + 5 * sizeof(m[0])); m[6] = buf_get_le64 (in + 6 * sizeof(m[0])); m[7] = buf_get_le64 (in + 7 * sizeof(m[0])); m[8] = buf_get_le64 (in + 8 * sizeof(m[0])); m[9] = buf_get_le64 (in + 9 * sizeof(m[0])); m[10] = buf_get_le64 (in + 10 * sizeof(m[0])); m[11] = buf_get_le64 (in + 11 * sizeof(m[0])); m[12] = buf_get_le64 (in + 12 * sizeof(m[0])); m[13] = buf_get_le64 (in + 13 * sizeof(m[0])); m[14] = buf_get_le64 (in + 14 * sizeof(m[0])); m[15] = buf_get_le64 (in + 15 * sizeof(m[0])); v[ 0] = S->h[0]; v[ 1] = S->h[1]; v[ 2] = S->h[2]; v[ 3] = S->h[3]; v[ 4] = S->h[4]; v[ 5] = S->h[5]; v[ 6] = S->h[6]; v[ 7] = S->h[7]; v[ 8] = blake2b_IV[0]; v[ 9] = blake2b_IV[1]; v[10] = blake2b_IV[2]; v[11] = blake2b_IV[3]; v[12] = blake2b_IV[4] ^ S->t[0]; v[13] = blake2b_IV[5] ^ S->t[1]; v[14] = blake2b_IV[6] ^ S->f[0]; v[15] = blake2b_IV[7] ^ S->f[1]; #define G(r,i,a,b,c,d) \ do { \ a = a + b + m[blake2b_sigma[r][2*i+0]]; \ d = rotr64(d ^ a, 32); \ c = c + d; \ b = rotr64(b ^ c, 24); \ a = a + b + m[blake2b_sigma[r][2*i+1]]; \ d = rotr64(d ^ a, 16); \ c = c + d; \ b = rotr64(b ^ c, 63); \ } while(0) #define ROUND(r) \ do { \ G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \ G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \ G(r,2,v[ 2],v[ 6],v[10],v[14]); \ G(r,3,v[ 3],v[ 7],v[11],v[15]); \ G(r,4,v[ 0],v[ 5],v[10],v[15]); \ G(r,5,v[ 1],v[ 6],v[11],v[12]); \ G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \ G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \ } while(0) ROUND(0); ROUND(1); ROUND(2); ROUND(3); ROUND(4); ROUND(5); ROUND(6); ROUND(7); ROUND(8); ROUND(9); ROUND(10); ROUND(11); #undef G #undef ROUND S->h[0] = S->h[0] ^ v[0] ^ v[0 + 8]; S->h[1] = S->h[1] ^ v[1] ^ v[1 + 8]; S->h[2] = S->h[2] ^ v[2] ^ v[2 + 8]; S->h[3] = S->h[3] ^ v[3] ^ v[3 + 8]; S->h[4] = S->h[4] ^ v[4] ^ v[4 + 8]; S->h[5] = S->h[5] ^ v[5] ^ v[5 + 8]; S->h[6] = S->h[6] ^ v[6] ^ v[6 + 8]; S->h[7] = S->h[7] ^ v[7] ^ v[7 + 8]; in += BLAKE2B_BLOCKBYTES; } return sizeof(void *) * 4 + sizeof(u64) * 16 * 2; } #ifdef USE_AVX2 unsigned int _gcry_blake2b_transform_amd64_avx2(BLAKE2B_STATE *S, const void *inblks, size_t nblks) ASM_FUNC_ABI; #endif static unsigned int blake2b_transform(void *ctx, const void *inblks, size_t nblks) { BLAKE2B_CONTEXT *c = ctx; unsigned int nburn; if (0) {} #ifdef USE_AVX2 if (c->use_avx2) nburn = _gcry_blake2b_transform_amd64_avx2(&c->state, inblks, nblks); #endif else nburn = blake2b_transform_generic(&c->state, inblks, nblks); if (nburn) nburn += ASM_EXTRA_STACK; return nburn; } static void blake2b_final(void *ctx) { BLAKE2B_CONTEXT *c = ctx; BLAKE2B_STATE *S = &c->state; unsigned int burn; size_t i; gcry_assert (sizeof(c->buf) >= c->outlen); if (blake2b_is_lastblock(S)) return; if (c->buflen < BLAKE2B_BLOCKBYTES) memset (c->buf + c->buflen, 0, BLAKE2B_BLOCKBYTES - c->buflen); /* Padding */ blake2b_set_lastblock (S); blake2b_increment_counter (S, (int)c->buflen - BLAKE2B_BLOCKBYTES); burn = blake2b_transform (ctx, c->buf, 1); /* Output full hash to buffer */ for (i = 0; i < 8; ++i) buf_put_le64 (c->buf + sizeof(S->h[i]) * i, S->h[i]); /* Zero out extra buffer bytes. */ if (c->outlen < sizeof(c->buf)) memset (c->buf + c->outlen, 0, sizeof(c->buf) - c->outlen); if (burn) _gcry_burn_stack (burn); } static byte *blake2b_read(void *ctx) { BLAKE2B_CONTEXT *c = ctx; return c->buf; } static void blake2b_write(void *ctx, const void *inbuf, size_t inlen) { BLAKE2B_CONTEXT *c = ctx; BLAKE2B_STATE *S = &c->state; blake2_write(S, inbuf, inlen, c->buf, &c->buflen, BLAKE2B_BLOCKBYTES, blake2b_transform); } static inline void blake2b_init_param(BLAKE2B_STATE *S, const struct blake2b_param_s *P) { const byte *p = (const byte *)P; size_t i; /* init xors IV with input parameter block */ /* IV XOR ParamBlock */ for (i = 0; i < 8; ++i) S->h[i] = blake2b_IV[i] ^ buf_get_le64(p + sizeof(S->h[i]) * i); } static inline gcry_err_code_t blake2b_init(BLAKE2B_CONTEXT *ctx, const byte *key, size_t keylen) { struct blake2b_param_s P[1] = { { 0, } }; BLAKE2B_STATE *S = &ctx->state; if (!ctx->outlen || ctx->outlen > BLAKE2B_OUTBYTES) return GPG_ERR_INV_ARG; if (sizeof(P[0]) != sizeof(u64) * 8) return GPG_ERR_INTERNAL; if (keylen && (!key || keylen > BLAKE2B_KEYBYTES)) return GPG_ERR_INV_KEYLEN; P->digest_length = ctx->outlen; P->key_length = keylen; P->fanout = 1; P->depth = 1; blake2b_init_param (S, P); wipememory (P, sizeof(P)); if (key) { blake2b_write (ctx, key, keylen); blake2b_write (ctx, zero_block, BLAKE2B_BLOCKBYTES - keylen); } return 0; } static gcry_err_code_t blake2b_init_ctx(void *ctx, unsigned int flags, const byte *key, size_t keylen, unsigned int dbits) { BLAKE2B_CONTEXT *c = ctx; unsigned int features = _gcry_get_hw_features (); (void)features; (void)flags; memset (c, 0, sizeof (*c)); #ifdef USE_AVX2 c->use_avx2 = !!(features & HWF_INTEL_AVX2); #endif c->outlen = dbits / 8; c->buflen = 0; return blake2b_init(c, key, keylen); } +/* Variable-length Hash Function H'. */ +gcry_err_code_t +blake2b_vl_hash (const void *in, size_t inlen, size_t outputlen, void *output) +{ + gcry_err_code_t ec; + BLAKE2B_CONTEXT ctx; + unsigned char buf[4]; + + ec = blake2b_init_ctx (&ctx, 0, NULL, 0, + (outputlen < 64 ? outputlen: 64)*8); + if (ec) + return ec; + + buf_put_le32 (buf, outputlen); + blake2b_write (&ctx, buf, 4); + blake2b_write (&ctx, in, inlen); + blake2b_final (&ctx); + + if (outputlen <= 64) + memcpy (output, ctx.buf, outputlen); + else + { + int r = (outputlen-1)/32; + unsigned int remained = outputlen - 32*r; + int i; + unsigned char d[64]; + + i = 0; + while (1) + { + memcpy (d, ctx.buf, 64); + memcpy ((unsigned char *)output+i*32, d, 32); + + if (++i >= r) + break; + + ec = blake2b_init_ctx (&ctx, 0, NULL, 0, 64*8); + if (ec) + return ec; + + blake2b_write (&ctx, d, 64); + blake2b_final (&ctx); + } + + if (remained) + memcpy ((unsigned char *)output+r*32, d+32, remained); + } + + return 0; +} + static inline void blake2s_set_lastblock(BLAKE2S_STATE *S) { S->f[0] = 0xFFFFFFFFUL; } static inline int blake2s_is_lastblock(BLAKE2S_STATE *S) { return S->f[0] != 0; } static inline void blake2s_increment_counter(BLAKE2S_STATE *S, const int inc) { S->t[0] += (u32)inc; S->t[1] += (S->t[0] < (u32)inc) - (inc < 0); } static unsigned int blake2s_transform_generic(BLAKE2S_STATE *S, const void *inblks, size_t nblks) { static const byte blake2s_sigma[10][16] = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }, { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 }, }; unsigned int burn = 0; const byte* in = inblks; u32 m[16]; u32 v[16]; while (nblks--) { /* Increment counter */ blake2s_increment_counter (S, BLAKE2S_BLOCKBYTES); /* Compress */ m[0] = buf_get_le32 (in + 0 * sizeof(m[0])); m[1] = buf_get_le32 (in + 1 * sizeof(m[0])); m[2] = buf_get_le32 (in + 2 * sizeof(m[0])); m[3] = buf_get_le32 (in + 3 * sizeof(m[0])); m[4] = buf_get_le32 (in + 4 * sizeof(m[0])); m[5] = buf_get_le32 (in + 5 * sizeof(m[0])); m[6] = buf_get_le32 (in + 6 * sizeof(m[0])); m[7] = buf_get_le32 (in + 7 * sizeof(m[0])); m[8] = buf_get_le32 (in + 8 * sizeof(m[0])); m[9] = buf_get_le32 (in + 9 * sizeof(m[0])); m[10] = buf_get_le32 (in + 10 * sizeof(m[0])); m[11] = buf_get_le32 (in + 11 * sizeof(m[0])); m[12] = buf_get_le32 (in + 12 * sizeof(m[0])); m[13] = buf_get_le32 (in + 13 * sizeof(m[0])); m[14] = buf_get_le32 (in + 14 * sizeof(m[0])); m[15] = buf_get_le32 (in + 15 * sizeof(m[0])); v[ 0] = S->h[0]; v[ 1] = S->h[1]; v[ 2] = S->h[2]; v[ 3] = S->h[3]; v[ 4] = S->h[4]; v[ 5] = S->h[5]; v[ 6] = S->h[6]; v[ 7] = S->h[7]; v[ 8] = blake2s_IV[0]; v[ 9] = blake2s_IV[1]; v[10] = blake2s_IV[2]; v[11] = blake2s_IV[3]; v[12] = S->t[0] ^ blake2s_IV[4]; v[13] = S->t[1] ^ blake2s_IV[5]; v[14] = S->f[0] ^ blake2s_IV[6]; v[15] = S->f[1] ^ blake2s_IV[7]; #define G(r,i,a,b,c,d) \ do { \ a = a + b + m[blake2s_sigma[r][2*i+0]]; \ d = ror(d ^ a, 16); \ c = c + d; \ b = ror(b ^ c, 12); \ a = a + b + m[blake2s_sigma[r][2*i+1]]; \ d = ror(d ^ a, 8); \ c = c + d; \ b = ror(b ^ c, 7); \ } while(0) #define ROUND(r) \ do { \ G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \ G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \ G(r,2,v[ 2],v[ 6],v[10],v[14]); \ G(r,3,v[ 3],v[ 7],v[11],v[15]); \ G(r,4,v[ 0],v[ 5],v[10],v[15]); \ G(r,5,v[ 1],v[ 6],v[11],v[12]); \ G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \ G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \ } while(0) ROUND(0); ROUND(1); ROUND(2); ROUND(3); ROUND(4); ROUND(5); ROUND(6); ROUND(7); ROUND(8); ROUND(9); #undef G #undef ROUND S->h[0] = S->h[0] ^ v[0] ^ v[0 + 8]; S->h[1] = S->h[1] ^ v[1] ^ v[1 + 8]; S->h[2] = S->h[2] ^ v[2] ^ v[2 + 8]; S->h[3] = S->h[3] ^ v[3] ^ v[3 + 8]; S->h[4] = S->h[4] ^ v[4] ^ v[4 + 8]; S->h[5] = S->h[5] ^ v[5] ^ v[5 + 8]; S->h[6] = S->h[6] ^ v[6] ^ v[6 + 8]; S->h[7] = S->h[7] ^ v[7] ^ v[7 + 8]; in += BLAKE2S_BLOCKBYTES; } return burn; } #ifdef USE_AVX unsigned int _gcry_blake2s_transform_amd64_avx(BLAKE2S_STATE *S, const void *inblks, size_t nblks) ASM_FUNC_ABI; #endif static unsigned int blake2s_transform(void *ctx, const void *inblks, size_t nblks) { BLAKE2S_CONTEXT *c = ctx; unsigned int nburn; if (0) {} #ifdef USE_AVX if (c->use_avx) nburn = _gcry_blake2s_transform_amd64_avx(&c->state, inblks, nblks); #endif else nburn = blake2s_transform_generic(&c->state, inblks, nblks); if (nburn) nburn += ASM_EXTRA_STACK; return nburn; } static void blake2s_final(void *ctx) { BLAKE2S_CONTEXT *c = ctx; BLAKE2S_STATE *S = &c->state; unsigned int burn; size_t i; gcry_assert (sizeof(c->buf) >= c->outlen); if (blake2s_is_lastblock(S)) return; if (c->buflen < BLAKE2S_BLOCKBYTES) memset (c->buf + c->buflen, 0, BLAKE2S_BLOCKBYTES - c->buflen); /* Padding */ blake2s_set_lastblock (S); blake2s_increment_counter (S, (int)c->buflen - BLAKE2S_BLOCKBYTES); burn = blake2s_transform (ctx, c->buf, 1); /* Output full hash to buffer */ for (i = 0; i < 8; ++i) buf_put_le32 (c->buf + sizeof(S->h[i]) * i, S->h[i]); /* Zero out extra buffer bytes. */ if (c->outlen < sizeof(c->buf)) memset (c->buf + c->outlen, 0, sizeof(c->buf) - c->outlen); if (burn) _gcry_burn_stack (burn); } static byte *blake2s_read(void *ctx) { BLAKE2S_CONTEXT *c = ctx; return c->buf; } static void blake2s_write(void *ctx, const void *inbuf, size_t inlen) { BLAKE2S_CONTEXT *c = ctx; BLAKE2S_STATE *S = &c->state; blake2_write(S, inbuf, inlen, c->buf, &c->buflen, BLAKE2S_BLOCKBYTES, blake2s_transform); } static inline void blake2s_init_param(BLAKE2S_STATE *S, const struct blake2s_param_s *P) { const byte *p = (const byte *)P; size_t i; /* init2 xors IV with input parameter block */ /* IV XOR ParamBlock */ for (i = 0; i < 8; ++i) S->h[i] ^= blake2s_IV[i] ^ buf_get_le32(&p[i * 4]); } static inline gcry_err_code_t blake2s_init(BLAKE2S_CONTEXT *ctx, const byte *key, size_t keylen) { struct blake2s_param_s P[1] = { { 0, } }; BLAKE2S_STATE *S = &ctx->state; if (!ctx->outlen || ctx->outlen > BLAKE2S_OUTBYTES) return GPG_ERR_INV_ARG; if (sizeof(P[0]) != sizeof(u32) * 8) return GPG_ERR_INTERNAL; if (keylen && (!key || keylen > BLAKE2S_KEYBYTES)) return GPG_ERR_INV_KEYLEN; P->digest_length = ctx->outlen; P->key_length = keylen; P->fanout = 1; P->depth = 1; blake2s_init_param (S, P); wipememory (P, sizeof(P)); if (key) { blake2s_write (ctx, key, keylen); blake2s_write (ctx, zero_block, BLAKE2S_BLOCKBYTES - keylen); } return 0; } static gcry_err_code_t blake2s_init_ctx(void *ctx, unsigned int flags, const byte *key, size_t keylen, unsigned int dbits) { BLAKE2S_CONTEXT *c = ctx; unsigned int features = _gcry_get_hw_features (); (void)features; (void)flags; memset (c, 0, sizeof (*c)); #ifdef USE_AVX c->use_avx = !!(features & HWF_INTEL_AVX); #endif c->outlen = dbits / 8; c->buflen = 0; return blake2s_init(c, key, keylen); } /* Selftests from "RFC 7693, Appendix E. BLAKE2b and BLAKE2s Self-Test * Module C Source". */ static void selftest_seq(byte *out, size_t len, u32 seed) { size_t i; u32 t, a, b; a = 0xDEAD4BAD * seed; b = 1; for (i = 0; i < len; i++) { t = a + b; a = b; b = t; out[i] = (t >> 24) & 0xFF; } } static gpg_err_code_t selftests_blake2b (int algo, int extended, selftest_report_func_t report) { static const byte blake2b_res[32] = { 0xC2, 0x3A, 0x78, 0x00, 0xD9, 0x81, 0x23, 0xBD, 0x10, 0xF5, 0x06, 0xC6, 0x1E, 0x29, 0xDA, 0x56, 0x03, 0xD7, 0x63, 0xB8, 0xBB, 0xAD, 0x2E, 0x73, 0x7F, 0x5E, 0x76, 0x5A, 0x7B, 0xCC, 0xD4, 0x75 }; static const size_t b2b_md_len[4] = { 20, 32, 48, 64 }; static const size_t b2b_in_len[6] = { 0, 3, 128, 129, 255, 1024 }; size_t i, j, outlen, inlen; byte in[1024], key[64]; BLAKE2B_CONTEXT ctx; BLAKE2B_CONTEXT ctx2; const char *what; const char *errtxt; (void)extended; what = "rfc7693 BLAKE2b selftest"; /* 256-bit hash for testing */ if (blake2b_init_ctx(&ctx, 0, NULL, 0, 32 * 8)) { errtxt = "init failed"; goto failed; } for (i = 0; i < 4; i++) { outlen = b2b_md_len[i]; for (j = 0; j < 6; j++) { inlen = b2b_in_len[j]; selftest_seq(in, inlen, inlen); /* unkeyed hash */ blake2b_init_ctx(&ctx2, 0, NULL, 0, outlen * 8); blake2b_write(&ctx2, in, inlen); blake2b_final(&ctx2); blake2b_write(&ctx, ctx2.buf, outlen); /* hash the hash */ selftest_seq(key, outlen, outlen); /* keyed hash */ blake2b_init_ctx(&ctx2, 0, key, outlen, outlen * 8); blake2b_write(&ctx2, in, inlen); blake2b_final(&ctx2); blake2b_write(&ctx, ctx2.buf, outlen); /* hash the hash */ } } /* compute and compare the hash of hashes */ blake2b_final(&ctx); for (i = 0; i < 32; i++) { if (ctx.buf[i] != blake2b_res[i]) { errtxt = "digest mismatch"; goto failed; } } return 0; failed: if (report) report ("digest", algo, what, errtxt); return GPG_ERR_SELFTEST_FAILED; } static gpg_err_code_t selftests_blake2s (int algo, int extended, selftest_report_func_t report) { static const byte blake2s_res[32] = { 0x6A, 0x41, 0x1F, 0x08, 0xCE, 0x25, 0xAD, 0xCD, 0xFB, 0x02, 0xAB, 0xA6, 0x41, 0x45, 0x1C, 0xEC, 0x53, 0xC5, 0x98, 0xB2, 0x4F, 0x4F, 0xC7, 0x87, 0xFB, 0xDC, 0x88, 0x79, 0x7F, 0x4C, 0x1D, 0xFE }; static const size_t b2s_md_len[4] = { 16, 20, 28, 32 }; static const size_t b2s_in_len[6] = { 0, 3, 64, 65, 255, 1024 }; size_t i, j, outlen, inlen; byte in[1024], key[32]; BLAKE2S_CONTEXT ctx; BLAKE2S_CONTEXT ctx2; const char *what; const char *errtxt; (void)extended; what = "rfc7693 BLAKE2s selftest"; /* 256-bit hash for testing */ if (blake2s_init_ctx(&ctx, 0, NULL, 0, 32 * 8)) { errtxt = "init failed"; goto failed; } for (i = 0; i < 4; i++) { outlen = b2s_md_len[i]; for (j = 0; j < 6; j++) { inlen = b2s_in_len[j]; selftest_seq(in, inlen, inlen); /* unkeyed hash */ blake2s_init_ctx(&ctx2, 0, NULL, 0, outlen * 8); blake2s_write(&ctx2, in, inlen); blake2s_final(&ctx2); blake2s_write(&ctx, ctx2.buf, outlen); /* hash the hash */ selftest_seq(key, outlen, outlen); /* keyed hash */ blake2s_init_ctx(&ctx2, 0, key, outlen, outlen * 8); blake2s_write(&ctx2, in, inlen); blake2s_final(&ctx2); blake2s_write(&ctx, ctx2.buf, outlen); /* hash the hash */ } } /* compute and compare the hash of hashes */ blake2s_final(&ctx); for (i = 0; i < 32; i++) { if (ctx.buf[i] != blake2s_res[i]) { errtxt = "digest mismatch"; goto failed; } } return 0; failed: if (report) report ("digest", algo, what, errtxt); return GPG_ERR_SELFTEST_FAILED; } gcry_err_code_t _gcry_blake2_init_with_key(void *ctx, unsigned int flags, const unsigned char *key, size_t keylen, int algo) { gcry_err_code_t rc; switch (algo) { case GCRY_MD_BLAKE2B_512: rc = blake2b_init_ctx (ctx, flags, key, keylen, 512); break; case GCRY_MD_BLAKE2B_384: rc = blake2b_init_ctx (ctx, flags, key, keylen, 384); break; case GCRY_MD_BLAKE2B_256: rc = blake2b_init_ctx (ctx, flags, key, keylen, 256); break; case GCRY_MD_BLAKE2B_160: rc = blake2b_init_ctx (ctx, flags, key, keylen, 160); break; case GCRY_MD_BLAKE2S_256: rc = blake2s_init_ctx (ctx, flags, key, keylen, 256); break; case GCRY_MD_BLAKE2S_224: rc = blake2s_init_ctx (ctx, flags, key, keylen, 224); break; case GCRY_MD_BLAKE2S_160: rc = blake2s_init_ctx (ctx, flags, key, keylen, 160); break; case GCRY_MD_BLAKE2S_128: rc = blake2s_init_ctx (ctx, flags, key, keylen, 128); break; default: rc = GPG_ERR_DIGEST_ALGO; break; } return rc; } #define DEFINE_BLAKE2_VARIANT(bs, BS, dbits, oid_branch) \ static void blake2##bs##_##dbits##_init(void *ctx, unsigned int flags) \ { \ int err = blake2##bs##_init_ctx (ctx, flags, NULL, 0, dbits); \ gcry_assert (err == 0); \ } \ static void \ _gcry_blake2##bs##_##dbits##_hash_buffers(void *outbuf, size_t nbytes, \ const gcry_buffer_t *iov, int iovcnt) \ { \ BLAKE2##BS##_CONTEXT hd; \ (void)nbytes; \ blake2##bs##_##dbits##_init (&hd, 0); \ for (;iovcnt > 0; iov++, iovcnt--) \ blake2##bs##_write (&hd, (const char*)iov[0].data + iov[0].off, \ iov[0].len); \ blake2##bs##_final (&hd); \ memcpy (outbuf, blake2##bs##_read (&hd), dbits / 8); \ } \ static const byte blake2##bs##_##dbits##_asn[] = { 0x30 }; \ static const gcry_md_oid_spec_t oid_spec_blake2##bs##_##dbits[] = \ { \ { " 1.3.6.1.4.1.1722.12.2." oid_branch }, \ { NULL } \ }; \ const gcry_md_spec_t _gcry_digest_spec_blake2##bs##_##dbits = \ { \ GCRY_MD_BLAKE2##BS##_##dbits, {0, 0}, \ "BLAKE2" #BS "_" #dbits, blake2##bs##_##dbits##_asn, \ DIM (blake2##bs##_##dbits##_asn), oid_spec_blake2##bs##_##dbits, \ dbits / 8, blake2##bs##_##dbits##_init, blake2##bs##_write, \ blake2##bs##_final, blake2##bs##_read, NULL, \ _gcry_blake2##bs##_##dbits##_hash_buffers, \ sizeof (BLAKE2##BS##_CONTEXT), selftests_blake2##bs \ }; DEFINE_BLAKE2_VARIANT(b, B, 512, "1.16") DEFINE_BLAKE2_VARIANT(b, B, 384, "1.12") DEFINE_BLAKE2_VARIANT(b, B, 256, "1.8") DEFINE_BLAKE2_VARIANT(b, B, 160, "1.5") DEFINE_BLAKE2_VARIANT(s, S, 256, "2.8") DEFINE_BLAKE2_VARIANT(s, S, 224, "2.7") DEFINE_BLAKE2_VARIANT(s, S, 160, "2.5") DEFINE_BLAKE2_VARIANT(s, S, 128, "2.4") diff --git a/cipher/kdf-internal.h b/cipher/kdf-internal.h index 7079860e..9e9a432e 100644 --- a/cipher/kdf-internal.h +++ b/cipher/kdf-internal.h @@ -1,40 +1,43 @@ /* kdf-internal.h - Internal defs for kdf.c * Copyright (C) 2013 g10 Code GmbH * * This file is part of Libgcrypt. * * Libgcrypt 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. * * Libgcrypt 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 . */ #ifndef GCRY_KDF_INTERNAL_H #define GCRY_KDF_INTERNAL_H /*-- kdf.c --*/ gpg_err_code_t _gcry_kdf_pkdf2 (const void *passphrase, size_t passphraselen, int hashalgo, const void *salt, size_t saltlen, unsigned long iterations, size_t keysize, void *keybuffer); /*-- scrypt.c --*/ gcry_err_code_t _gcry_kdf_scrypt (const unsigned char *passwd, size_t passwdlen, int algo, int subalgo, const unsigned char *salt, size_t saltlen, unsigned long iterations, size_t dklen, unsigned char *dk); +/*-- blake2.c --*/ +gcry_err_code_t +blake2b_vl_hash (const void *in, size_t inlen, size_t outputlen, void *output); #endif /*GCRY_KDF_INTERNAL_H*/ diff --git a/cipher/kdf.c b/cipher/kdf.c index bdc7a2a0..94cd064f 100644 --- a/cipher/kdf.c +++ b/cipher/kdf.c @@ -1,1229 +1,1172 @@ /* kdf.c - Key Derivation Functions * Copyright (C) 1998, 2008, 2011 Free Software Foundation, Inc. * Copyright (C) 2013 g10 Code GmbH * * This file is part of Libgcrypt. * * Libgcrypt 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. * * Libgcrypt 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 . */ #include #include #include #include #include #include "g10lib.h" #include "cipher.h" #include "kdf-internal.h" /* Transform a passphrase into a suitable key of length KEYSIZE and store this key in the caller provided buffer KEYBUFFER. The caller must provide an HASHALGO, a valid ALGO and depending on that algo a SALT of 8 bytes and the number of ITERATIONS. Code taken from gnupg/agent/protect.c:hash_passphrase. */ static gpg_err_code_t openpgp_s2k (const void *passphrase, size_t passphraselen, int algo, int hashalgo, const void *salt, size_t saltlen, unsigned long iterations, size_t keysize, void *keybuffer) { gpg_err_code_t ec; gcry_md_hd_t md; char *key = keybuffer; int pass, i; int used = 0; int secmode; if ((algo == GCRY_KDF_SALTED_S2K || algo == GCRY_KDF_ITERSALTED_S2K) && (!salt || saltlen != 8)) return GPG_ERR_INV_VALUE; secmode = _gcry_is_secure (passphrase) || _gcry_is_secure (keybuffer); ec = _gcry_md_open (&md, hashalgo, secmode? GCRY_MD_FLAG_SECURE : 0); if (ec) return ec; for (pass=0; used < keysize; pass++) { if (pass) { _gcry_md_reset (md); for (i=0; i < pass; i++) /* Preset the hash context. */ _gcry_md_putc (md, 0); } if (algo == GCRY_KDF_SALTED_S2K || algo == GCRY_KDF_ITERSALTED_S2K) { int len2 = passphraselen + 8; unsigned long count = len2; if (algo == GCRY_KDF_ITERSALTED_S2K) { count = iterations; if (count < len2) count = len2; } while (count > len2) { _gcry_md_write (md, salt, saltlen); _gcry_md_write (md, passphrase, passphraselen); count -= len2; } if (count < saltlen) _gcry_md_write (md, salt, count); else { _gcry_md_write (md, salt, saltlen); count -= saltlen; _gcry_md_write (md, passphrase, count); } } else _gcry_md_write (md, passphrase, passphraselen); _gcry_md_final (md); i = _gcry_md_get_algo_dlen (hashalgo); if (i > keysize - used) i = keysize - used; memcpy (key+used, _gcry_md_read (md, hashalgo), i); used += i; } _gcry_md_close (md); return 0; } /* Transform a passphrase into a suitable key of length KEYSIZE and store this key in the caller provided buffer KEYBUFFER. The caller must provide PRFALGO which indicates the pseudorandom function to use: This shall be the algorithms id of a hash algorithm; it is used in HMAC mode. SALT is a salt of length SALTLEN and ITERATIONS gives the number of iterations. */ gpg_err_code_t _gcry_kdf_pkdf2 (const void *passphrase, size_t passphraselen, int hashalgo, const void *salt, size_t saltlen, unsigned long iterations, size_t keysize, void *keybuffer) { gpg_err_code_t ec; gcry_md_hd_t md; int secmode; unsigned long dklen = keysize; char *dk = keybuffer; unsigned int hlen; /* Output length of the digest function. */ unsigned int l; /* Rounded up number of blocks. */ unsigned int r; /* Number of octets in the last block. */ char *sbuf; /* Malloced buffer to concatenate salt and iter as well as space to hold TBUF and UBUF. */ char *tbuf; /* Buffer for T; ptr into SBUF, size is HLEN. */ char *ubuf; /* Buffer for U; ptr into SBUF, size is HLEN. */ unsigned int lidx; /* Current block number. */ unsigned long iter; /* Current iteration number. */ unsigned int i; /* We allow for a saltlen of 0 here to support scrypt. It is not clear whether rfc2898 allows for this this, thus we do a test on saltlen > 0 only in gcry_kdf_derive. */ if (!salt || !iterations || !dklen) return GPG_ERR_INV_VALUE; hlen = _gcry_md_get_algo_dlen (hashalgo); if (!hlen) return GPG_ERR_DIGEST_ALGO; secmode = _gcry_is_secure (passphrase) || _gcry_is_secure (keybuffer); /* Step 1 */ /* If dkLen > (2^32 - 1) * hLen, output "derived key too long" and * stop. We use a stronger inequality but only if our type can hold * a larger value. */ #if SIZEOF_UNSIGNED_LONG > 4 if (dklen > 0xffffffffU) return GPG_ERR_INV_VALUE; #endif /* Step 2 */ l = ((dklen - 1)/ hlen) + 1; r = dklen - (l - 1) * hlen; /* Setup buffers and prepare a hash context. */ sbuf = (secmode ? xtrymalloc_secure (saltlen + 4 + hlen + hlen) : xtrymalloc (saltlen + 4 + hlen + hlen)); if (!sbuf) return gpg_err_code_from_syserror (); tbuf = sbuf + saltlen + 4; ubuf = tbuf + hlen; ec = _gcry_md_open (&md, hashalgo, (GCRY_MD_FLAG_HMAC | (secmode?GCRY_MD_FLAG_SECURE:0))); if (ec) { xfree (sbuf); return ec; } ec = _gcry_md_setkey (md, passphrase, passphraselen); if (ec) { _gcry_md_close (md); xfree (sbuf); return ec; } /* Step 3 and 4. */ memcpy (sbuf, salt, saltlen); for (lidx = 1; lidx <= l; lidx++) { for (iter = 0; iter < iterations; iter++) { _gcry_md_reset (md); if (!iter) /* Compute U_1: */ { sbuf[saltlen] = (lidx >> 24); sbuf[saltlen + 1] = (lidx >> 16); sbuf[saltlen + 2] = (lidx >> 8); sbuf[saltlen + 3] = lidx; _gcry_md_write (md, sbuf, saltlen + 4); memcpy (ubuf, _gcry_md_read (md, 0), hlen); memcpy (tbuf, ubuf, hlen); } else /* Compute U_(2..c): */ { _gcry_md_write (md, ubuf, hlen); memcpy (ubuf, _gcry_md_read (md, 0), hlen); for (i=0; i < hlen; i++) tbuf[i] ^= ubuf[i]; } } if (lidx == l) /* Last block. */ memcpy (dk, tbuf, r); else { memcpy (dk, tbuf, hlen); dk += hlen; } } _gcry_md_close (md); xfree (sbuf); return 0; } /* Derive a key from a passphrase. KEYSIZE gives the requested size of the keys in octets. KEYBUFFER is a caller provided buffer filled on success with the derived key. The input passphrase is taken from (PASSPHRASE,PASSPHRASELEN) which is an arbitrary memory buffer. ALGO specifies the KDF algorithm to use; these are the constants GCRY_KDF_*. SUBALGO specifies an algorithm used internally by the KDF algorithms; this is usually a hash algorithm but certain KDF algorithm may use it differently. {SALT,SALTLEN} is a salt as needed by most KDF algorithms. ITERATIONS is a positive integer parameter to most KDFs. 0 is returned on success, or an error code on failure. */ gpg_err_code_t _gcry_kdf_derive (const void *passphrase, size_t passphraselen, int algo, int subalgo, const void *salt, size_t saltlen, unsigned long iterations, size_t keysize, void *keybuffer) { gpg_err_code_t ec; if (!passphrase) { ec = GPG_ERR_INV_DATA; goto leave; } if (!keybuffer || !keysize) { ec = GPG_ERR_INV_VALUE; goto leave; } switch (algo) { case GCRY_KDF_SIMPLE_S2K: case GCRY_KDF_SALTED_S2K: case GCRY_KDF_ITERSALTED_S2K: if (!passphraselen) ec = GPG_ERR_INV_DATA; else ec = openpgp_s2k (passphrase, passphraselen, algo, subalgo, salt, saltlen, iterations, keysize, keybuffer); break; case GCRY_KDF_PBKDF1: ec = GPG_ERR_UNSUPPORTED_ALGORITHM; break; case GCRY_KDF_PBKDF2: if (!saltlen) ec = GPG_ERR_INV_VALUE; else ec = _gcry_kdf_pkdf2 (passphrase, passphraselen, subalgo, salt, saltlen, iterations, keysize, keybuffer); break; case 41: case GCRY_KDF_SCRYPT: #if USE_SCRYPT ec = _gcry_kdf_scrypt (passphrase, passphraselen, algo, subalgo, salt, saltlen, iterations, keysize, keybuffer); #else ec = GPG_ERR_UNSUPPORTED_ALGORITHM; #endif /*USE_SCRYPT*/ break; default: ec = GPG_ERR_UNKNOWN_ALGORITHM; break; } leave: return ec; } #include "bufhelp.h" typedef struct argon2_context *argon2_ctx_t; /* Per thread data for Argon2. */ struct argon2_thread_data { argon2_ctx_t a; - union { - void *user_data; - gpg_err_code_t ec; - } u; - unsigned int pass; unsigned int slice; unsigned int lane; }; /* Argon2 context */ struct argon2_context { int algo; int hash_type; unsigned int outlen; - unsigned int n_threads; const unsigned char *password; size_t passwordlen; const unsigned char *salt; size_t saltlen; const unsigned char *key; size_t keylen; const unsigned char *ad; size_t adlen; unsigned int m_cost; unsigned int passes; unsigned int memory_blocks; unsigned int segment_length; unsigned int lane_length; unsigned int lanes; - unsigned int step; - - unsigned int r; - unsigned int s; - unsigned int l; - unsigned int t; - - gcry_md_hd_t hd; - unsigned char *block; + u64 *block; struct argon2_thread_data *thread_data; unsigned char out[1]; /* In future, we may use flexible array member. */ }; -enum argon2_iterator_step { - ARGON2_ITERATOR_STEP0, - ARGON2_ITERATOR_STEP1, - ARGON2_ITERATOR_STEP2, - ARGON2_ITERATOR_STEP3, - ARGON2_ITERATOR_STEP4 -}; - #define ARGON2_VERSION 0x13 -static gpg_err_code_t -hash (gcry_md_hd_t hd, const unsigned char *input, unsigned int inputlen, - unsigned char *output, unsigned int outputlen) -{ - gpg_err_code_t ec = 0; - unsigned char buf[4]; - const unsigned char *digest; - gcry_md_hd_t hd1; - int algo; - - _gcry_md_reset (hd); - - if (outputlen < 64) - { - if (outputlen == 48) - algo = GCRY_MD_BLAKE2B_384; - else if (outputlen == 32) - algo = GCRY_MD_BLAKE2B_256; - else if (outputlen == 20) - algo = GCRY_MD_BLAKE2B_160; - else - return GPG_ERR_NOT_IMPLEMENTED; - - ec = _gcry_md_open (&hd1, algo, 0); - if (ec) - return ec; - - buf_put_le32 (buf, outputlen); - _gcry_md_write (hd1, buf, 4); - _gcry_md_write (hd1, input, inputlen); - digest = _gcry_md_read (hd1, algo); - memcpy (output, digest, outputlen); - _gcry_md_close (hd1); - } - else if (outputlen == 64) - { - buf_put_le32 (buf, outputlen); - _gcry_md_write (hd, buf, 4); - _gcry_md_write (hd, input, inputlen); - digest = _gcry_md_read (hd, GCRY_MD_BLAKE2B_512); - memcpy (output, digest, 64); - } - else - { - int i, r; - unsigned int remained; - unsigned char d[64]; - - i = 0; - r = outputlen/32; - - buf_put_le32 (buf, outputlen); - _gcry_md_write (hd, buf, 4); - _gcry_md_write (hd, input, inputlen); - - do - { - digest = _gcry_md_read (hd, GCRY_MD_BLAKE2B_512); - memcpy (d, digest, 64); - memcpy (output+i*32, digest, 32); - i++; - - _gcry_md_reset (hd); - _gcry_md_write (hd, d, 64); - } - while (i < r); - - remained = outputlen - 32*r; - if (remained) - { - if (remained == 20) - algo = GCRY_MD_BLAKE2B_160; - else - return GPG_ERR_NOT_IMPLEMENTED; - - ec = _gcry_md_open (&hd1, algo, 0); - if (ec) - return ec; +#define ARGON2_WORDS_IN_BLOCK (1024/8) - _gcry_md_write (hd1, d, 64); - digest = _gcry_md_read (hd1, algo); - memcpy (output+r*32, digest, remained); - _gcry_md_close (hd1); - } - } +static void +xor_block (u64 *dst, const u64 *src) +{ + int i; - return 0; + for (i = 0; i < ARGON2_WORDS_IN_BLOCK; i++) + dst[i] ^= src[i]; } static gpg_err_code_t -argon2_genh0_first_blocks (argon2_ctx_t a) +argon2_fill_first_blocks (argon2_ctx_t a) { - gpg_err_code_t ec = 0; + gpg_err_code_t ec; unsigned char h0_01_i[72]; const unsigned char *digest; unsigned char buf[4]; int i; + gcry_md_hd_t hd; + ec = _gcry_md_open (&hd, GCRY_MD_BLAKE2B_512, 0); + if (ec) + return ec; + + /* Generate H0. */ buf_put_le32 (buf, a->lanes); - _gcry_md_write (a->hd, buf, 4); + _gcry_md_write (hd, buf, 4); buf_put_le32 (buf, a->outlen); - _gcry_md_write (a->hd, buf, 4); + _gcry_md_write (hd, buf, 4); buf_put_le32 (buf, a->m_cost); - _gcry_md_write (a->hd, buf, 4); + _gcry_md_write (hd, buf, 4); buf_put_le32 (buf, a->passes); - _gcry_md_write (a->hd, buf, 4); + _gcry_md_write (hd, buf, 4); buf_put_le32 (buf, ARGON2_VERSION); - _gcry_md_write (a->hd, buf, 4); + _gcry_md_write (hd, buf, 4); buf_put_le32 (buf, a->hash_type); - _gcry_md_write (a->hd, buf, 4); + _gcry_md_write (hd, buf, 4); buf_put_le32 (buf, a->passwordlen); - _gcry_md_write (a->hd, buf, 4); - _gcry_md_write (a->hd, a->password, a->passwordlen); + _gcry_md_write (hd, buf, 4); + _gcry_md_write (hd, a->password, a->passwordlen); buf_put_le32 (buf, a->saltlen); - _gcry_md_write (a->hd, buf, 4); - _gcry_md_write (a->hd, a->salt, a->saltlen); + _gcry_md_write (hd, buf, 4); + _gcry_md_write (hd, a->salt, a->saltlen); buf_put_le32 (buf, a->keylen); - _gcry_md_write (a->hd, buf, 4); + _gcry_md_write (hd, buf, 4); if (a->key) - _gcry_md_write (a->hd, a->key, a->keylen); + _gcry_md_write (hd, a->key, a->keylen); buf_put_le32 (buf, a->adlen); - _gcry_md_write (a->hd, buf, 4); + _gcry_md_write (hd, buf, 4); if (a->ad) - _gcry_md_write (a->hd, a->ad, a->adlen); + _gcry_md_write (hd, a->ad, a->adlen); - digest = _gcry_md_read (a->hd, GCRY_MD_BLAKE2B_512); + digest = _gcry_md_read (hd, GCRY_MD_BLAKE2B_512); memcpy (h0_01_i, digest, 64); + _gcry_md_close (hd); + for (i = 0; i < a->lanes; i++) { - /*FIXME*/ memset (h0_01_i+64, 0, 4); buf_put_le32 (h0_01_i+64+4, i); - ec = hash (a->hd, h0_01_i, 72, a->block+1024*i, 1024); - if (ec) - break; + blake2b_vl_hash (h0_01_i, 72, 1024, + &a->block[i*a->lane_length*ARGON2_WORDS_IN_BLOCK]); buf_put_le32 (h0_01_i+64, 1); - ec = hash (a->hd, h0_01_i, 72, a->block+1024*(i+a->lanes), 1024); - if (ec) - break; + blake2b_vl_hash (h0_01_i, 72, 1024, + &a->block[(i*a->lane_length+1)*ARGON2_WORDS_IN_BLOCK]); } - - return ec; + return 0; } static gpg_err_code_t argon2_init (argon2_ctx_t a, unsigned int parallelism, unsigned int m_cost, unsigned int t_cost) { gpg_err_code_t ec = 0; unsigned int memory_blocks; unsigned int segment_length; - gcry_md_hd_t hd; void *block; struct argon2_thread_data *thread_data; memory_blocks = m_cost; if (memory_blocks < 8 * parallelism) memory_blocks = 8 * parallelism; segment_length = memory_blocks / (parallelism * 4); memory_blocks = segment_length * parallelism * 4; a->passes = t_cost; a->memory_blocks = memory_blocks; a->segment_length = segment_length; a->lane_length = segment_length * 4; a->lanes = parallelism; - a->r = a->s = a->l = a->t = 0; - a->step = ARGON2_ITERATOR_STEP0; - - a->hd = NULL; a->block = NULL; a->thread_data = NULL; - ec = _gcry_md_open (&hd, GCRY_MD_BLAKE2B_512, 0); - if (ec) - return ec; - block = xtrymalloc (1024 * memory_blocks); if (!block) { ec = gpg_err_code_from_errno (errno); - _gcry_md_close (hd); return ec; } memset (block, 0, 1024 * memory_blocks); - thread_data = xtrymalloc (a->n_threads * sizeof (struct argon2_thread_data)); + thread_data = xtrymalloc (a->lanes * sizeof (struct argon2_thread_data)); if (!thread_data) { ec = gpg_err_code_from_errno (errno); xfree (block); - _gcry_md_close (hd); return ec; } - memset (thread_data, 0, a->n_threads * sizeof (struct argon2_thread_data)); + memset (thread_data, 0, a->lanes * sizeof (struct argon2_thread_data)); - a->hd = hd; a->block = block; a->thread_data = thread_data; return 0; } -static gpg_err_code_t -argon2_ctl (argon2_ctx_t a, int cmd, void *buffer, size_t buflen) -{ - gpg_err_code_t ec = GPG_ERR_NOT_IMPLEMENTED; - (void)a; - (void)cmd; - (void)buffer; - (void)buflen; - return ec; +static u64 fBlaMka (u64 x, u64 y) +{ + const u64 m = U64_C(0xFFFFFFFF); + return x + y + 2 * (x & m) * (y & m); } -static gpg_err_code_t -argon2_iterator (argon2_ctx_t a, int *action_p, - struct gcry_kdf_pt_head **t_p) +static u64 rotr64 (uint64_t w, unsigned int c) { - switch (a->step) - { - case ARGON2_ITERATOR_STEP0: - argon2_genh0_first_blocks (a); - /* continue */ - *action_p = 3; - *t_p = NULL; - a->step = ARGON2_ITERATOR_STEP1; - return 0; - - case ARGON2_ITERATOR_STEP1: - for (a->r = 0; a->r < a->passes; a->r++) - for (a->s = 0; a->s < 4; a->s++) - { - struct argon2_thread_data *thread_data; + return (w >> c) | (w << (64 - c)); +} - for (a->l = 0; a->l < a->lanes; a->l++) - { - if (a->l >= a->n_threads) - { - /* Join a thread. */ - thread_data = &a->thread_data[a->t]; - *action_p = 2; - *t_p = (struct gcry_kdf_pt_head *)thread_data; - a->step = ARGON2_ITERATOR_STEP2; - return 0; - - case ARGON2_ITERATOR_STEP2: - thread_data = &a->thread_data[a->t]; - if (thread_data->u.ec) - return thread_data->u.ec; - } - - /* Create a thread. */ - thread_data = &a->thread_data[a->t]; - thread_data->a = a; - thread_data->u.user_data = NULL; - thread_data->pass = a->r; - thread_data->slice = a->s; - thread_data->lane = a->l; - *action_p = 1; - *t_p = (struct gcry_kdf_pt_head *)thread_data; - a->step = ARGON2_ITERATOR_STEP3; - return 0; - - case ARGON2_ITERATOR_STEP3: - a->t = (a->t + 1) % a->n_threads; - } - - for (a->l = a->lanes - a->n_threads; a->l < a->lanes; a->l++) - { - thread_data = &a->thread_data[a->t]; - - /* Join a thread. */ - *action_p = 2; - *t_p = (struct gcry_kdf_pt_head *)thread_data; - a->step = ARGON2_ITERATOR_STEP4; - return 0; - - case ARGON2_ITERATOR_STEP4: - thread_data = &a->thread_data[a->t]; - if (thread_data->u.ec) - return thread_data->u.ec; - a->t = (a->t + 1) % a->n_threads; - } - } - } +#define G(a, b, c, d) \ + do { \ + a = fBlaMka(a, b); \ + d = rotr64(d ^ a, 32); \ + c = fBlaMka(c, d); \ + b = rotr64(b ^ c, 24); \ + a = fBlaMka(a, b); \ + d = rotr64(d ^ a, 16); \ + c = fBlaMka(c, d); \ + b = rotr64(b ^ c, 63); \ + } while ((void)0, 0) + +#define BLAKE2_ROUND_NOMSG(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, \ + v12, v13, v14, v15) \ + do { \ + G(v0, v4, v8, v12); \ + G(v1, v5, v9, v13); \ + G(v2, v6, v10, v14); \ + G(v3, v7, v11, v15); \ + G(v0, v5, v10, v15); \ + G(v1, v6, v11, v12); \ + G(v2, v7, v8, v13); \ + G(v3, v4, v9, v14); \ + } while ((void)0, 0) - *action_p = 0; - *t_p = NULL; - a->step = ARGON2_ITERATOR_STEP0; - return 0; +static void +fill_block (const u64 *prev_block, const u64 *ref_block, u64 *curr_block, + int with_xor) +{ + u64 block_r[ARGON2_WORDS_IN_BLOCK]; + u64 block_tmp[ARGON2_WORDS_IN_BLOCK]; + int i; + + memcpy (block_r, ref_block, 1024); + if (prev_block) + xor_block (block_r, prev_block); + memcpy (block_tmp, block_r, 1024); + + if (with_xor) + xor_block (block_tmp, curr_block); + + for (i = 0; i < 8; ++i) + BLAKE2_ROUND_NOMSG + (block_r[16 * i], block_r[16 * i + 1], block_r[16 * i + 2], + block_r[16 * i + 3], block_r[16 * i + 4], block_r[16 * i + 5], + block_r[16 * i + 6], block_r[16 * i + 7], block_r[16 * i + 8], + block_r[16 * i + 9], block_r[16 * i + 10], block_r[16 * i + 11], + block_r[16 * i + 12], block_r[16 * i + 13], block_r[16 * i + 14], + block_r[16 * i + 15]); + + for (i = 0; i < 8; i++) + BLAKE2_ROUND_NOMSG + (block_r[2 * i], block_r[2 * i + 1], block_r[2 * i + 16], + block_r[2 * i + 17], block_r[2 * i + 32], block_r[2 * i + 33], + block_r[2 * i + 48], block_r[2 * i + 49], block_r[2 * i + 64], + block_r[2 * i + 65], block_r[2 * i + 80], block_r[2 * i + 81], + block_r[2 * i + 96], block_r[2 * i + 97], block_r[2 * i + 112], + block_r[2 * i + 113]); + + memcpy (curr_block, block_tmp, 1024); + xor_block (curr_block, block_r); } static void -argon2_pseudo_rand_gen (argon2_ctx_t a, const struct argon2_thread_data *t, - u32 *random_index) +pseudo_random_generate (u64 *random_block, u64 *input_block) { - (void)a; - (void)t; - (void)random_index; + u64 v; + + v = buf_get_le64 (&input_block[6]); + buf_put_le64 (&input_block[6], ++v); + + fill_block (NULL, input_block, random_block, 0); + fill_block (NULL, random_block, random_block, 0); } -static gpg_err_code_t -argon2_compute_segment (argon2_ctx_t a, const struct argon2_thread_data *t) +static u32 +index_alpha (argon2_ctx_t a, const struct argon2_thread_data *t, + int segment_index, u32 random, int same_lane) { - gpg_err_code_t ec = 0; - u32 *random_index = NULL; + u32 reference_area_size; + u64 relative_position; + u32 start_position; + + if (t->pass == 0) + { + if (t->slice == 0) + reference_area_size = segment_index - 1; + else + { + if (same_lane) + reference_area_size = t->slice * a->segment_length + + segment_index - 1; + else + reference_area_size = t->slice * a->segment_length + + ((segment_index == 0) ? -1 : 0); + } + } + else + { + if (same_lane) + reference_area_size = a->lane_length + - a->segment_length + segment_index - 1; + else + reference_area_size = a->lane_length + - a->segment_length + ((segment_index == 0) ? -1 : 0); + } + + relative_position = (random * (u64)random) >> 32; + relative_position = reference_area_size - 1 - + ((reference_area_size * relative_position) >> 32); + + if (t->pass == 0) + start_position = 0; + else + start_position = (t->slice == 4 - 1) + ? 0 + : (t->slice + 1) * a->segment_length; + + return (start_position + relative_position) % a->lane_length; +} + +static void +argon2_compute_segment (void *priv) +{ + const struct argon2_thread_data *t = (const struct argon2_thread_data *)priv; + argon2_ctx_t a = t->a; int i; int prev_offset, curr_offset; + u32 ref_index, ref_lane; + u64 input_block[1024/sizeof (u64)]; + u64 address_block[1024/sizeof (u64)]; + u64 *random_block = NULL; if (a->hash_type == GCRY_KDF_ARGON2I || (a->hash_type == GCRY_KDF_ARGON2ID && t->pass == 0 && t->slice < 2)) { - random_index = xtrymalloc (sizeof (u32)*a->segment_length); - if (!random_index) - return gpg_err_code_from_errno (errno); - argon2_pseudo_rand_gen (a, t, random_index); + memset (input_block, 0, 1024); + buf_put_le64 ((unsigned char *)input_block+0*8, t->pass); + buf_put_le64 ((unsigned char *)input_block+1*8, t->lane); + buf_put_le64 ((unsigned char *)input_block+2*8, t->slice); + buf_put_le64 ((unsigned char *)input_block+3*8, a->memory_blocks); + buf_put_le64 ((unsigned char *)input_block+4*8, a->passes); + buf_put_le64 ((unsigned char *)input_block+5*8, a->hash_type); + random_block = address_block; } if (t->pass == 0 && t->slice == 0) - i = 2; + { + if (random_block) + pseudo_random_generate (random_block, input_block); + i = 2; + } else i = 0; curr_offset = t->lane * a->lane_length + t->slice * a->segment_length + i; if ((curr_offset % a->lane_length)) prev_offset = curr_offset - 1; else prev_offset = curr_offset + a->lane_length - 1; for (; i < a->segment_length; i++, curr_offset++, prev_offset++) { - /* Not yet implemented. */; + void *rand64_p; + u64 *ref_block, *curr_block; + + if ((curr_offset % a->lane_length) == 1) + prev_offset = curr_offset - 1; + + if (random_block) + { + if ((i % (1024/sizeof (u64))) == 0) + pseudo_random_generate (random_block, input_block); + + rand64_p = &random_block[(i% (1024/sizeof (u64)))]; + } + else + rand64_p = &a->block[prev_offset*ARGON2_WORDS_IN_BLOCK]; + + if (t->pass == 0 && t->slice == 0) + ref_lane = t->lane; + else + ref_lane = buf_get_le32 ((unsigned char *)rand64_p+4) % a->lanes; + + ref_index = index_alpha (a, t, i, buf_get_le32 (rand64_p), + ref_lane == t->lane); + ref_block = + &a->block[(a->lane_length * ref_lane + ref_index)* ARGON2_WORDS_IN_BLOCK]; + + curr_block = &a->block[curr_offset * ARGON2_WORDS_IN_BLOCK]; + fill_block (&a->block[prev_offset * ARGON2_WORDS_IN_BLOCK], ref_block, + curr_block, t->pass != 0); } +} - xfree (random_index); - return ec; + +static gpg_err_code_t +argon2_compute (argon2_ctx_t a, const struct gcry_kdf_thread_ops *ops) +{ + gpg_err_code_t ec; + unsigned int r; + unsigned int s; + unsigned int l; + + ec = argon2_fill_first_blocks (a); + if (ec) + return ec; + + for (r = 0; r < a->passes; r++) + for (s = 0; s < 4; s++) + { + for (l = 0; l < a->lanes; l++) + { + struct argon2_thread_data *thread_data; + + /* launch a thread. */ + thread_data = &a->thread_data[l]; + thread_data->a = a; + thread_data->pass = r; + thread_data->slice = s; + thread_data->lane = l; + + if (ops) + ops->launch_job (ops->jobs_context, + argon2_compute_segment, thread_data); + else + argon2_compute_segment (thread_data); + } + + if (ops) + ops->wait_all_jobs_completion (ops->jobs_context); + } + + return 0; } static gpg_err_code_t argon2_final (argon2_ctx_t a, size_t resultlen, void *result) { - gpg_err_code_t ec; - int i, j; + int i; if (resultlen != a->outlen) return GPG_ERR_INV_VALUE; memset (a->block, 0, 1024); for (i = 0; i < a->lanes; i++) { - unsigned char *p0; - unsigned char *p1; /*FIXME*/ - - p0 = a->block; - p1 = p0 + a->lane_length * i + (a->segment_length - 1)*1024; + u64 *last_block; - for (j = 0; j < 1024; j++) - p0[j] ^= p1[j]; + last_block = &a->block[(a->lane_length * i + (a->lane_length - 1)) + * ARGON2_WORDS_IN_BLOCK]; + xor_block (a->block, last_block); } - ec = hash (a->hd, a->block, 1024, result, a->outlen); - return ec; + blake2b_vl_hash (a->block, 1024, a->outlen, result); + return 0; } static void argon2_close (argon2_ctx_t a) { size_t n; n = offsetof (struct argon2_context, out) + a->outlen; - if (a->hd) - _gcry_md_close (a->hd); - if (a->block) { wipememory (a->block, 1024 * a->memory_blocks); xfree (a->block); } if (a->thread_data) xfree (a->thread_data); wipememory (a, n); xfree (a); } static gpg_err_code_t argon2_open (gcry_kdf_hd_t *hd, int subalgo, const unsigned long *param, unsigned int paramlen, const void *password, size_t passwordlen, const void *salt, size_t saltlen, const void *key, size_t keylen, const void *ad, size_t adlen) { int hash_type; unsigned int taglen; unsigned int t_cost; unsigned int m_cost; unsigned int parallelism = 1; - unsigned int n_threads = 1; argon2_ctx_t a; gpg_err_code_t ec; size_t n; if (subalgo != GCRY_KDF_ARGON2D && subalgo != GCRY_KDF_ARGON2I && subalgo != GCRY_KDF_ARGON2ID) return GPG_ERR_INV_VALUE; else hash_type = subalgo; - /* param : [ tag_length, t_cost, m_cost, parallelism, n_threads ] */ - if (paramlen < 3 || paramlen > 5) + /* param : [ tag_length, t_cost, m_cost, parallelism ] */ + if (paramlen < 3 || paramlen > 4) return GPG_ERR_INV_VALUE; else { taglen = (unsigned int)param[0]; t_cost = (unsigned int)param[1]; m_cost = (unsigned int)param[2]; - if (paramlen == 4) + if (paramlen >= 4) parallelism = (unsigned int)param[3]; - if (paramlen == 5) - { - n_threads = (unsigned int)param[4]; - if (n_threads > parallelism) - n_threads = parallelism; - } - - if (!(taglen == 64 || taglen == 48 - || taglen % 32 == 0 || taglen % 32 == 20)) - /* - * FIXME: To support arbitrary taglen, we need to expose - * internal API of Blake2b. - */ - return GPG_ERR_NOT_IMPLEMENTED; } n = offsetof (struct argon2_context, out) + taglen; a = xtrymalloc (n); if (!a) return gpg_err_code_from_errno (errno); a->algo = GCRY_KDF_ARGON2; a->hash_type = hash_type; a->outlen = taglen; - a->n_threads = n_threads; a->password = password; a->passwordlen = passwordlen; a->salt = salt; a->saltlen = saltlen; a->key = key; a->keylen = keylen; a->ad = ad; a->adlen = adlen; + a->m_cost = m_cost; + a->block = NULL; a->thread_data = NULL; ec = argon2_init (a, parallelism, m_cost, t_cost); if (ec) { xfree (a); return ec; } *hd = (void *)a; return 0; } static gpg_err_code_t balloon_open (gcry_kdf_hd_t *hd, int subalgo, const unsigned long *param, unsigned int paramlen, const void *passphrase, size_t passphraselen, const void *salt, size_t saltlen) { /* * It should have space_cost and time_cost. * Optionally, for parallelised version, it has parallelism. */ if (paramlen != 2 && paramlen != 3) return GPG_ERR_INV_VALUE; (void)param; (void)subalgo; (void)passphrase; (void)passphraselen; (void)salt; (void)saltlen; *hd = NULL; return GPG_ERR_NOT_IMPLEMENTED; } struct gcry_kdf_handle { int algo; /* And algo specific parts come. */ }; gpg_err_code_t _gcry_kdf_open (gcry_kdf_hd_t *hd, int algo, int subalgo, const unsigned long *param, unsigned int paramlen, const void *passphrase, size_t passphraselen, const void *salt, size_t saltlen, const void *key, size_t keylen, const void *ad, size_t adlen) { gpg_err_code_t ec; switch (algo) { case GCRY_KDF_ARGON2: if (!passphraselen || !saltlen) ec = GPG_ERR_INV_VALUE; else ec = argon2_open (hd, subalgo, param, paramlen, passphrase, passphraselen, salt, saltlen, key, keylen, ad, adlen); break; case GCRY_KDF_BALLOON: if (!passphraselen || !saltlen) ec = GPG_ERR_INV_VALUE; else { (void)key; (void)keylen; (void)ad; (void)adlen; ec = balloon_open (hd, subalgo, param, paramlen, passphrase, passphraselen, salt, saltlen); } break; default: ec = GPG_ERR_UNKNOWN_ALGORITHM; break; } return ec; } gpg_err_code_t -_gcry_kdf_ctl (gcry_kdf_hd_t h, int cmd, void *buffer, size_t buflen) -{ - gpg_err_code_t ec; - - switch (h->algo) - { - case GCRY_KDF_ARGON2: - ec = argon2_ctl ((argon2_ctx_t)h, cmd, buffer, buflen); - break; - - default: - ec = GPG_ERR_UNKNOWN_ALGORITHM; - break; - } - - return ec; -} - -gpg_err_code_t -_gcry_kdf_iterator (gcry_kdf_hd_t h, int *action_p, - struct gcry_kdf_pt_head **t_p) -{ - gpg_err_code_t ec; - - switch (h->algo) - { - case GCRY_KDF_ARGON2: - ec = argon2_iterator ((argon2_ctx_t)h, action_p, t_p); - break; - - default: - ec = GPG_ERR_UNKNOWN_ALGORITHM; - break; - } - - return ec; -} - -gpg_err_code_t -_gcry_kdf_compute_segment (gcry_kdf_hd_t h, const struct gcry_kdf_pt_head *t) +_gcry_kdf_compute (gcry_kdf_hd_t h, const struct gcry_kdf_thread_ops *ops) { gpg_err_code_t ec; switch (h->algo) { case GCRY_KDF_ARGON2: - ec = argon2_compute_segment ((argon2_ctx_t)h, - (const struct argon2_thread_data *)t); + ec = argon2_compute ((argon2_ctx_t)h, ops); break; default: ec = GPG_ERR_UNKNOWN_ALGORITHM; break; } return ec; } gpg_err_code_t _gcry_kdf_final (gcry_kdf_hd_t h, size_t resultlen, void *result) { gpg_err_code_t ec; switch (h->algo) { case GCRY_KDF_ARGON2: ec = argon2_final ((argon2_ctx_t)h, resultlen, result); break; default: ec = GPG_ERR_UNKNOWN_ALGORITHM; break; } return ec; } void _gcry_kdf_close (gcry_kdf_hd_t h) { switch (h->algo) { case GCRY_KDF_ARGON2: argon2_close ((argon2_ctx_t)h); break; default: break; } } /* Check one KDF call with ALGO and HASH_ALGO using the regular KDF * API. (passphrase,passphraselen) is the password to be derived, * (salt,saltlen) the salt for the key derivation, * iterations is the number of the kdf iterations, * and (expect,expectlen) the expected result. Returns NULL on * success or a string describing the failure. */ static const char * check_one (int algo, int hash_algo, const void *passphrase, size_t passphraselen, const void *salt, size_t saltlen, unsigned long iterations, const void *expect, size_t expectlen) { unsigned char key[512]; /* hardcoded to avoid allocation */ size_t keysize = expectlen; /* Skip test with shoter passphrase in FIPS mode. */ if (fips_mode () && passphraselen < 14) return NULL; if (keysize > sizeof(key)) return "invalid tests data"; if (_gcry_kdf_derive (passphrase, passphraselen, algo, hash_algo, salt, saltlen, iterations, keysize, key)) return "gcry_kdf_derive failed"; if (memcmp (key, expect, expectlen)) return "does not match"; return NULL; } static gpg_err_code_t selftest_pbkdf2 (int extended, selftest_report_func_t report) { static const struct { const char *desc; const char *p; /* Passphrase. */ size_t plen; /* Length of P. */ const char *salt; size_t saltlen; int hashalgo; unsigned long c; /* Iterations. */ int dklen; /* Requested key length. */ const char *dk; /* Derived key. */ int disabled; } tv[] = { #if USE_SHA1 #define NUM_TEST_VECTORS 9 /* SHA1 test vectors are from RFC-6070. */ { "Basic PBKDF2 SHA1 #1", "password", 8, "salt", 4, GCRY_MD_SHA1, 1, 20, "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9" "\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6" }, { "Basic PBKDF2 SHA1 #2", "password", 8, "salt", 4, GCRY_MD_SHA1, 2, 20, "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e" "\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57" }, { "Basic PBKDF2 SHA1 #3", "password", 8, "salt", 4, GCRY_MD_SHA1, 4096, 20, "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad" "\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1" }, { "Basic PBKDF2 SHA1 #4", "password", 8, "salt", 4, GCRY_MD_SHA1, 16777216, 20, "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94" "\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84", 1 /* This test takes too long. */ }, { "Basic PBKDF2 SHA1 #5", "passwordPASSWORDpassword", 24, "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, GCRY_MD_SHA1, 4096, 25, "\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8" "\xd8\x36\x62\xc0\xe4\x4a\x8b\x29\x1a\x96" "\x4c\xf2\xf0\x70\x38" }, { "Basic PBKDF2 SHA1 #6", "pass\0word", 9, "sa\0lt", 5, GCRY_MD_SHA1, 4096, 16, "\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37" "\xd7\xf0\x34\x25\xe0\xc3" }, { /* empty password test, not in RFC-6070 */ "Basic PBKDF2 SHA1 #7", "", 0, "salt", 4, GCRY_MD_SHA1, 2, 20, "\x13\x3a\x4c\xe8\x37\xb4\xd2\x52\x1e\xe2" "\xbf\x03\xe1\x1c\x71\xca\x79\x4e\x07\x97" }, #else #define NUM_TEST_VECTORS 2 #endif { "Basic PBKDF2 SHA256", "password", 8, "salt", 4, GCRY_MD_SHA256, 2, 32, "\xae\x4d\x0c\x95\xaf\x6b\x46\xd3\x2d\x0a\xdf\xf9\x28\xf0\x6d\xd0" "\x2a\x30\x3f\x8e\xf3\xc2\x51\xdf\xd6\xe2\xd8\x5a\x95\x47\x4c\x43" }, { "Extended PBKDF2 SHA256", "passwordPASSWORDpassword", 24, "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, GCRY_MD_SHA256, 4096, 40, "\x34\x8c\x89\xdb\xcb\xd3\x2b\x2f\x32\xd8\x14\xb8\x11\x6e\x84\xcf" "\x2b\x17\x34\x7e\xbc\x18\x00\x18\x1c\x4e\x2a\x1f\xb8\xdd\x53\xe1" "\xc6\x35\x51\x8c\x7d\xac\x47\xe9" }, { NULL } }; const char *what; const char *errtxt; int tvidx; for (tvidx=0; tv[tvidx].desc; tvidx++) { what = tv[tvidx].desc; if (tv[tvidx].disabled) continue; errtxt = check_one (GCRY_KDF_PBKDF2, tv[tvidx].hashalgo, tv[tvidx].p, tv[tvidx].plen, tv[tvidx].salt, tv[tvidx].saltlen, tv[tvidx].c, tv[tvidx].dk, tv[tvidx].dklen); if (errtxt) goto failed; if (tvidx >= NUM_TEST_VECTORS - 1 && !extended) break; } return 0; /* Succeeded. */ failed: if (report) report ("kdf", GCRY_KDF_PBKDF2, what, errtxt); return GPG_ERR_SELFTEST_FAILED; } /* Run the selftests for KDF with KDF algorithm ALGO with optional reporting function REPORT. */ gpg_error_t _gcry_kdf_selftest (int algo, int extended, selftest_report_func_t report) { gcry_err_code_t ec = 0; if (algo == GCRY_KDF_PBKDF2) ec = selftest_pbkdf2 (extended, report); else { ec = GPG_ERR_UNSUPPORTED_ALGORITHM; if (report) report ("kdf", algo, "module", "algorithm not available"); } return gpg_error (ec); } diff --git a/src/gcrypt-int.h b/src/gcrypt-int.h index 11e55703..08977d32 100644 --- a/src/gcrypt-int.h +++ b/src/gcrypt-int.h @@ -1,567 +1,563 @@ /* gcrypt-int.h - Internal version of gcrypt.h * Copyright (C) 2013 g10 Code GmbH * * This file is part of Libgcrypt. * * Libgcrypt 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. * * Libgcrypt 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 . */ #ifndef GCRY_GCRYPT_INT_H #define GCRY_GCRYPT_INT_H #ifdef _GCRYPT_H #error gcrypt.h already included #endif #include "gcrypt.h" #include "types.h" /* These error codes are used but not defined in the required * libgpg-error N.MM. Define them here. [None right now.] */ /* Context used with elliptic curve functions. */ struct mpi_ec_ctx_s; typedef struct mpi_ec_ctx_s *mpi_ec_t; /* Underscore prefixed internal versions of the public functions. They return gpg_err_code_t and not gpg_error_t. Some macros also need an underscore prefixed internal version. Note that the memory allocation functions and macros (xmalloc etc.) are not defined here but in g10lib.h because this file here is included by some test programs which define theie own xmalloc macros. */ gpg_err_code_t _gcry_cipher_open (gcry_cipher_hd_t *handle, int algo, int mode, unsigned int flags); void _gcry_cipher_close (gcry_cipher_hd_t h); gpg_err_code_t _gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer, size_t buflen); gpg_err_code_t _gcry_cipher_info (gcry_cipher_hd_t h, int what, void *buffer, size_t *nbytes); gpg_err_code_t _gcry_cipher_algo_info (int algo, int what, void *buffer, size_t *nbytes); const char *_gcry_cipher_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE; int _gcry_cipher_map_name (const char *name) _GCRY_GCC_ATTR_PURE; int _gcry_cipher_mode_from_oid (const char *string) _GCRY_GCC_ATTR_PURE; gpg_err_code_t _gcry_cipher_encrypt (gcry_cipher_hd_t h, void *out, size_t outsize, const void *in, size_t inlen); gpg_err_code_t _gcry_cipher_decrypt (gcry_cipher_hd_t h, void *out, size_t outsize, const void *in, size_t inlen); gcry_err_code_t _gcry_cipher_setkey (gcry_cipher_hd_t hd, const void *key, size_t keylen); gcry_err_code_t _gcry_cipher_setiv (gcry_cipher_hd_t hd, const void *iv, size_t ivlen); gpg_err_code_t _gcry_cipher_authenticate (gcry_cipher_hd_t hd, const void *abuf, size_t abuflen); gpg_err_code_t _gcry_cipher_gettag (gcry_cipher_hd_t hd, void *outtag, size_t taglen); gpg_err_code_t _gcry_cipher_checktag (gcry_cipher_hd_t hd, const void *intag, size_t taglen); gpg_err_code_t _gcry_cipher_setctr (gcry_cipher_hd_t hd, const void *ctr, size_t ctrlen); gpg_err_code_t _gcry_cipher_getctr (gcry_cipher_hd_t hd, void *ctr, size_t ctrlen); size_t _gcry_cipher_get_algo_keylen (int algo); size_t _gcry_cipher_get_algo_blklen (int algo); #define _gcry_cipher_reset(h) _gcry_cipher_ctl ((h), GCRYCTL_RESET, NULL, 0) gpg_err_code_t _gcry_pk_encrypt (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t pkey); gpg_err_code_t _gcry_pk_decrypt (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t skey); gpg_err_code_t _gcry_pk_sign (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t skey); gpg_err_code_t _gcry_pk_verify (gcry_sexp_t sigval, gcry_sexp_t data, gcry_sexp_t pkey); gpg_err_code_t _gcry_pk_testkey (gcry_sexp_t key); gpg_err_code_t _gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms); gpg_err_code_t _gcry_pk_ctl (int cmd, void *buffer, size_t buflen); gpg_err_code_t _gcry_pk_algo_info (int algo, int what, void *buffer, size_t *nbytes); const char *_gcry_pk_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE; int _gcry_pk_map_name (const char* name) _GCRY_GCC_ATTR_PURE; unsigned int _gcry_pk_get_nbits (gcry_sexp_t key) _GCRY_GCC_ATTR_PURE; unsigned char *_gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array); const char *_gcry_pk_get_curve (gcry_sexp_t key, int iterator, unsigned int *r_nbits); gcry_sexp_t _gcry_pk_get_param (int algo, const char *name); gpg_err_code_t _gcry_pubkey_get_sexp (gcry_sexp_t *r_sexp, int mode, gcry_ctx_t ctx); unsigned int _gcry_ecc_get_algo_keylen (int algo); gpg_error_t _gcry_ecc_mul_point (int algo, unsigned char *result, const unsigned char *scalar, const unsigned char *point); gcry_err_code_t _gcry_pk_sign_md (gcry_sexp_t *r_sig, const char *tmpl, gcry_md_hd_t hd, gcry_sexp_t s_skey, gcry_ctx_t ctx); gcry_err_code_t _gcry_pk_verify_md (gcry_sexp_t s_sig, const char *tmpl, gcry_md_hd_t hd, gcry_sexp_t s_pkey, gcry_ctx_t ctx); gpg_err_code_t _gcry_pk_random_override_new (gcry_ctx_t *r_ctx, const unsigned char *p, size_t len); gpg_err_code_t _gcry_pk_get_random_override (gcry_ctx_t ctx, const unsigned char **r_p, size_t *r_len); gpg_err_code_t _gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags); void _gcry_md_close (gcry_md_hd_t hd); gpg_err_code_t _gcry_md_enable (gcry_md_hd_t hd, int algo); gpg_err_code_t _gcry_md_copy (gcry_md_hd_t *bhd, gcry_md_hd_t ahd); void _gcry_md_reset (gcry_md_hd_t hd); gpg_err_code_t _gcry_md_ctl (gcry_md_hd_t hd, int cmd, void *buffer, size_t buflen); void _gcry_md_write (gcry_md_hd_t hd, const void *buffer, size_t length); unsigned char *_gcry_md_read (gcry_md_hd_t hd, int algo); gpg_err_code_t _gcry_md_extract (gcry_md_hd_t hd, int algo, void *buffer, size_t length); void _gcry_md_hash_buffer (int algo, void *digest, const void *buffer, size_t length); gpg_err_code_t _gcry_md_hash_buffers_extract (int algo, unsigned int flags, void *digest, int digestlen, const gcry_buffer_t *iov, int iovcnt); gpg_err_code_t _gcry_md_hash_buffers (int algo, unsigned int flags, void *digest, const gcry_buffer_t *iov, int iovcnt); int _gcry_md_get_algo (gcry_md_hd_t hd); unsigned int _gcry_md_get_algo_dlen (int algo); int _gcry_md_is_enabled (gcry_md_hd_t a, int algo); int _gcry_md_is_secure (gcry_md_hd_t a); gpg_err_code_t _gcry_md_info (gcry_md_hd_t h, int what, void *buffer, size_t *nbytes); gpg_err_code_t _gcry_md_algo_info (int algo, int what, void *buffer, size_t *nbytes); const char *_gcry_md_algo_name (int algo) _GCRY_GCC_ATTR_PURE; int _gcry_md_map_name (const char* name) _GCRY_GCC_ATTR_PURE; gpg_err_code_t _gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen); void _gcry_md_debug (gcry_md_hd_t hd, const char *suffix); #define _gcry_md_test_algo(a) \ _gcry_md_algo_info ((a), GCRYCTL_TEST_ALGO, NULL, NULL) #define _gcry_md_final(a) \ _gcry_md_ctl ((a), GCRYCTL_FINALIZE, NULL, 0) #define _gcry_md_putc(h,c) \ do { \ gcry_md_hd_t h__ = (h); \ if( (h__)->bufpos == (h__)->bufsize ) \ _gcry_md_write( (h__), NULL, 0 ); \ (h__)->buf[(h__)->bufpos++] = (c) & 0xff; \ } while(0) gpg_err_code_t _gcry_mac_open (gcry_mac_hd_t *handle, int algo, unsigned int flags, gcry_ctx_t ctx); void _gcry_mac_close (gcry_mac_hd_t h); gpg_err_code_t _gcry_mac_ctl (gcry_mac_hd_t h, int cmd, void *buffer, size_t buflen); gpg_err_code_t _gcry_mac_algo_info (int algo, int what, void *buffer, size_t *nbytes); gpg_err_code_t _gcry_mac_setkey (gcry_mac_hd_t hd, const void *key, size_t keylen); gpg_err_code_t _gcry_mac_setiv (gcry_mac_hd_t hd, const void *iv, size_t ivlen); gpg_err_code_t _gcry_mac_write (gcry_mac_hd_t hd, const void *buffer, size_t length); gpg_err_code_t _gcry_mac_read (gcry_mac_hd_t hd, void *buffer, size_t *buflen); gpg_err_code_t _gcry_mac_verify (gcry_mac_hd_t hd, const void *buffer, size_t buflen); int _gcry_mac_get_algo (gcry_mac_hd_t hd); unsigned int _gcry_mac_get_algo_maclen (int algo); unsigned int _gcry_mac_get_algo_keylen (int algo); const char *_gcry_mac_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE; int _gcry_mac_map_name (const char *name) _GCRY_GCC_ATTR_PURE; #define _gcry_mac_reset(h) _gcry_mac_ctl ((h), GCRYCTL_RESET, NULL, 0) gpg_err_code_t _gcry_kdf_derive (const void *passphrase, size_t passphraselen, int algo, int subalgo, const void *salt, size_t saltlen, unsigned long iterations, size_t keysize, void *keybuffer); gpg_err_code_t _gcry_kdf_open (gcry_kdf_hd_t *hd, int algo, int subalgo, const unsigned long *param, unsigned int paramlen, const void *passphrase, size_t passphraselen, const void *salt, size_t saltlen, const void *key, size_t keylen, const void *ad, size_t adlen); -gpg_err_code_t _gcry_kdf_ctl (gcry_kdf_hd_t h, int cmd, void *buffer, - size_t buflen); -gpg_err_code_t _gcry_kdf_iterator (gcry_kdf_hd_t h, int *action, - struct gcry_kdf_pt_head **t_p); -gpg_err_code_t _gcry_kdf_compute_segment (gcry_kdf_hd_t h, - const struct gcry_kdf_pt_head *t); +gcry_error_t _gcry_kdf_compute (gcry_kdf_hd_t h, + const struct gcry_kdf_thread_ops *ops); gpg_err_code_t _gcry_kdf_final (gcry_kdf_hd_t h, size_t resultlen, void *result); void _gcry_kdf_close (gcry_kdf_hd_t h); gpg_err_code_t _gcry_prime_generate (gcry_mpi_t *prime, unsigned int prime_bits, unsigned int factor_bits, gcry_mpi_t **factors, gcry_prime_check_func_t cb_func, void *cb_arg, gcry_random_level_t random_level, unsigned int flags); gpg_err_code_t _gcry_prime_group_generator (gcry_mpi_t *r_g, gcry_mpi_t prime, gcry_mpi_t *factors, gcry_mpi_t start_g); void _gcry_prime_release_factors (gcry_mpi_t *factors); gpg_err_code_t _gcry_prime_check (gcry_mpi_t x, unsigned int flags); void _gcry_randomize (void *buffer, size_t length, enum gcry_random_level level); gpg_err_code_t _gcry_random_add_bytes (const void *buffer, size_t length, int quality); void *_gcry_random_bytes (size_t nbytes, enum gcry_random_level level) _GCRY_GCC_ATTR_MALLOC; void *_gcry_random_bytes_secure (size_t nbytes, enum gcry_random_level level) _GCRY_GCC_ATTR_MALLOC; void _gcry_mpi_randomize (gcry_mpi_t w, unsigned int nbits, enum gcry_random_level level); void _gcry_create_nonce (void *buffer, size_t length); void _gcry_ctx_release (gcry_ctx_t ctx); const char *_gcry_check_version (const char *req_version); void _gcry_set_allocation_handler (gcry_handler_alloc_t func_alloc, gcry_handler_alloc_t func_alloc_secure, gcry_handler_secure_check_t func_secure_check, gcry_handler_realloc_t func_realloc, gcry_handler_free_t func_free); void _gcry_set_outofcore_handler (gcry_handler_no_mem_t h, void *opaque); void _gcry_set_fatalerror_handler (gcry_handler_error_t fnc, void *opaque); void _gcry_set_log_handler (gcry_handler_log_t f, void *opaque); void _gcry_set_gettext_handler (const char *(*f)(const char*)); void _gcry_set_progress_handler (gcry_handler_progress_t cb, void *cb_data); /* Return a pointer to a string containing a description of the error code in the error value ERR. */ static inline const char * _gcry_strerror (gcry_error_t err) { return gpg_strerror (err); } /* Return a pointer to a string containing a description of the error source in the error value ERR. */ static inline const char * _gcry_strsource (gcry_error_t err) { return gpg_strsource (err); } /* Retrieve the error code for the system error ERR. This returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report this). */ static inline gcry_err_code_t _gcry_err_code_from_errno (int err) { return gpg_err_code_from_errno (err); } /* Retrieve the system error for the error code CODE. This returns 0 if CODE is not a system error code. */ static inline int _gcry_err_code_to_errno (gcry_err_code_t code) { return gpg_err_code_from_errno (code); } /* Return an error value with the error source SOURCE and the system error ERR. */ static inline gcry_error_t _gcry_err_make_from_errno (gpg_err_source_t source, int err) { return gpg_err_make_from_errno (source, err); } /* Return an error value with the system error ERR. */ static inline gcry_error_t _gcry_error_from_errno (int err) { return gpg_error (gpg_err_code_from_errno (err)); } gpg_err_code_t _gcry_sexp_new (gcry_sexp_t *retsexp, const void *buffer, size_t length, int autodetect); gpg_err_code_t _gcry_sexp_create (gcry_sexp_t *retsexp, void *buffer, size_t length, int autodetect, void (*freefnc) (void *)); gpg_err_code_t _gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff, const char *buffer, size_t length); gpg_err_code_t _gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff, const char *format, ...); gpg_err_code_t _gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff, const char *format, void **arg_list); void _gcry_sexp_release (gcry_sexp_t sexp); size_t _gcry_sexp_canon_len (const unsigned char *buffer, size_t length, size_t *erroff, gcry_err_code_t *errcode); size_t _gcry_sexp_sprint (gcry_sexp_t sexp, int mode, void *buffer, size_t maxlength); void _gcry_sexp_dump (const gcry_sexp_t a); gcry_sexp_t _gcry_sexp_cons (const gcry_sexp_t a, const gcry_sexp_t b); gcry_sexp_t _gcry_sexp_alist (const gcry_sexp_t *array); gcry_sexp_t _gcry_sexp_vlist (const gcry_sexp_t a, ...); gcry_sexp_t _gcry_sexp_append (const gcry_sexp_t a, const gcry_sexp_t n); gcry_sexp_t _gcry_sexp_prepend (const gcry_sexp_t a, const gcry_sexp_t n); gcry_sexp_t _gcry_sexp_find_token (gcry_sexp_t list, const char *tok, size_t toklen); int _gcry_sexp_length (const gcry_sexp_t list); gcry_sexp_t _gcry_sexp_nth (const gcry_sexp_t list, int number); gcry_sexp_t _gcry_sexp_car (const gcry_sexp_t list); gcry_sexp_t _gcry_sexp_cdr (const gcry_sexp_t list); gcry_sexp_t _gcry_sexp_cadr (const gcry_sexp_t list); const char *_gcry_sexp_nth_data (const gcry_sexp_t list, int number, size_t *datalen); void *_gcry_sexp_nth_buffer (const gcry_sexp_t list, int number, size_t *rlength); char *_gcry_sexp_nth_string (gcry_sexp_t list, int number); gcry_mpi_t _gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt); gpg_err_code_t _gcry_sexp_extract_param (gcry_sexp_t sexp, const char *path, const char *list, ...) _GCRY_GCC_ATTR_SENTINEL(0); #define sexp_new(a, b, c, d) _gcry_sexp_new ((a), (b), (c), (d)) #define sexp_create(a, b, c, d, e) _gcry_sexp_create ((a), (b), (c), (d), (e)) #define sexp_sscan(a, b, c, d) _gcry_sexp_sscan ((a), (b), (c), (d)) #define sexp_build _gcry_sexp_build #define sexp_build_array(a, b, c, d) _gcry_sexp_build_array ((a), (b), (c), (d)) #define sexp_release(a) _gcry_sexp_release ((a)) #define sexp_canon_len(a, b, c, d) _gcry_sexp_canon_len ((a), (b), (c), (d)) #define sexp_sprint(a, b, c, d) _gcry_sexp_sprint ((a), (b), (c), (d)) #define sexp_dump(a) _gcry_sexp_dump ((a)) #define sexp_cons(a, b) _gcry_sexp_cons ((a), (b)) #define sexp_alist(a) _gcry_sexp_alist ((a)) #define sexp_vlist _gcry_sexp_vlist #define sexp_append(a, b) _gcry_sexp_append ((a), (b)) #define sexp_prepend(a, b) _gcry_sexp_prepend ((a), (b)) #define sexp_find_token(a, b, c) _gcry_sexp_find_token ((a), (b), (c)) #define sexp_length(a) _gcry_sexp_length ((a)) #define sexp_nth(a, b) _gcry_sexp_nth ((a), (b)) #define sexp_car(a) _gcry_sexp_car ((a)) #define sexp_cdr(a) _gcry_sexp_cdr ((a)) #define sexp_cadr(a) _gcry_sexp_cadr ((a)) #define sexp_nth_data(a, b, c) _gcry_sexp_nth_data ((a), (b), (c)) #define sexp_nth_buffer(a, b, c) _gcry_sexp_nth_buffer ((a), (b), (c)) #define sexp_nth_string(a, b) _gcry_sexp_nth_string ((a), (b)) #define sexp_nth_mpi(a, b, c) _gcry_sexp_nth_mpi ((a), (b), (c)) #define sexp_extract_param _gcry_sexp_extract_param gcry_mpi_t _gcry_mpi_new (unsigned int nbits); gcry_mpi_t _gcry_mpi_snew (unsigned int nbits); void _gcry_mpi_release (gcry_mpi_t a); gcry_mpi_t _gcry_mpi_copy (const gcry_mpi_t a); void _gcry_mpi_snatch (gcry_mpi_t w, gcry_mpi_t u); gcry_mpi_t _gcry_mpi_set (gcry_mpi_t w, const gcry_mpi_t u); gcry_mpi_t _gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u); gcry_err_code_t _gcry_mpi_get_ui (unsigned int *w, gcry_mpi_t u); void _gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b); int _gcry_mpi_is_neg (gcry_mpi_t a); void _gcry_mpi_neg (gcry_mpi_t w, gcry_mpi_t u); void _gcry_mpi_abs (gcry_mpi_t w); int _gcry_mpi_cmp (const gcry_mpi_t u, const gcry_mpi_t v); int _gcry_mpi_cmpabs (const gcry_mpi_t u, const gcry_mpi_t v); int _gcry_mpi_cmp_ui (const gcry_mpi_t u, unsigned long v); gpg_err_code_t _gcry_mpi_scan (gcry_mpi_t *ret_mpi, enum gcry_mpi_format format, const void *buffer, size_t buflen, size_t *nscanned); gpg_err_code_t _gcry_mpi_print (enum gcry_mpi_format format, unsigned char *buffer, size_t buflen, size_t *nwritten, const gcry_mpi_t a); gpg_err_code_t _gcry_mpi_aprint (enum gcry_mpi_format format, unsigned char **buffer, size_t *nwritten, const gcry_mpi_t a); void _gcry_mpi_dump (const gcry_mpi_t a); void _gcry_mpi_add (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); void _gcry_mpi_add_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v); void _gcry_mpi_addm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); void _gcry_mpi_sub (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); void _gcry_mpi_sub_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ); void _gcry_mpi_subm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); void _gcry_mpi_mul (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); void _gcry_mpi_mul_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ); void _gcry_mpi_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); void _gcry_mpi_mul_2exp (gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt); void _gcry_mpi_div (gcry_mpi_t q, gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor, int round); void _gcry_mpi_mod (gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor); void _gcry_mpi_powm (gcry_mpi_t w, const gcry_mpi_t b, const gcry_mpi_t e, const gcry_mpi_t m); int _gcry_mpi_gcd (gcry_mpi_t g, gcry_mpi_t a, gcry_mpi_t b); int _gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t m); gcry_mpi_point_t _gcry_mpi_point_new (unsigned int nbits); void _gcry_mpi_point_release (gcry_mpi_point_t point); gcry_mpi_point_t _gcry_mpi_point_copy (gcry_mpi_point_t point); void _gcry_mpi_point_get (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z, gcry_mpi_point_t point); void _gcry_mpi_point_snatch_get (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z, gcry_mpi_point_t point); gcry_mpi_point_t _gcry_mpi_point_set (gcry_mpi_point_t point, gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z); gcry_mpi_point_t _gcry_mpi_point_snatch_set (gcry_mpi_point_t point, gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z); gcry_mpi_t _gcry_mpi_ec_get_mpi (const char *name, gcry_ctx_t ctx, int copy); gcry_mpi_point_t _gcry_mpi_ec_get_point (const char *name, gcry_ctx_t ctx, int copy); int _gcry_mpi_ec_get_affine (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_point_t point, mpi_ec_t ctx); void _gcry_mpi_ec_point_resize (gcry_mpi_point_t p, mpi_ec_t ctx); void _gcry_mpi_ec_dup (gcry_mpi_point_t w, gcry_mpi_point_t u, gcry_ctx_t ctx); void _gcry_mpi_ec_add (gcry_mpi_point_t w, gcry_mpi_point_t u, gcry_mpi_point_t v, mpi_ec_t ctx); void _gcry_mpi_ec_sub (gcry_mpi_point_t w, gcry_mpi_point_t u, gcry_mpi_point_t v, mpi_ec_t ctx); void _gcry_mpi_ec_mul (gcry_mpi_point_t w, gcry_mpi_t n, gcry_mpi_point_t u, mpi_ec_t ctx); int _gcry_mpi_ec_curve_point (gcry_mpi_point_t w, mpi_ec_t ctx); unsigned int _gcry_mpi_get_nbits (gcry_mpi_t a); int _gcry_mpi_test_bit (gcry_mpi_t a, unsigned int n); void _gcry_mpi_set_bit (gcry_mpi_t a, unsigned int n); void _gcry_mpi_clear_bit (gcry_mpi_t a, unsigned int n); void _gcry_mpi_set_highbit (gcry_mpi_t a, unsigned int n); void _gcry_mpi_clear_highbit (gcry_mpi_t a, unsigned int n); void _gcry_mpi_rshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n); void _gcry_mpi_lshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n); gcry_mpi_t _gcry_mpi_set_opaque (gcry_mpi_t a, void *p, unsigned int nbits); gcry_mpi_t _gcry_mpi_set_opaque_copy (gcry_mpi_t a, const void *p, unsigned int nbits); void *_gcry_mpi_get_opaque (gcry_mpi_t a, unsigned int *nbits); void _gcry_mpi_set_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); void _gcry_mpi_clear_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); int _gcry_mpi_get_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); /* Private function - do not use. */ /* gcry_mpi_t _gcry_mpi_get_const (int no); */ /* We need our internal versions of the macros. */ #ifndef GCRYPT_NO_MPI_MACROS # error GCRYPT_NO_MPI_MACROS is not defined #endif #define mpi_new(n) _gcry_mpi_new ((n)) #define mpi_secure_new( n ) _gcry_mpi_snew ((n)) #define mpi_snew(n) _gcry_mpi_snew ((n)) #define mpi_release(a) \ do \ { \ _gcry_mpi_release ((a));\ (a) = NULL; \ } \ while (0) #define mpi_snatch( w, u) _gcry_mpi_snatch( (w), (u) ) #define mpi_set( w, u) _gcry_mpi_set( (w), (u) ) #define mpi_set_ui( w, u) _gcry_mpi_set_ui( (w), (u) ) #define mpi_get_ui(w,u) _gcry_mpi_get_ui( (w), (u) ) #define mpi_swap(a,b) _gcry_mpi_swap ((a),(b)) #define mpi_abs( w ) _gcry_mpi_abs( (w) ) #define mpi_neg( w, u) _gcry_mpi_neg( (w), (u) ) #define mpi_cmp( u, v ) _gcry_mpi_cmp( (u), (v) ) #define mpi_cmpabs( u, v ) _gcry_mpi_cmpabs( (u), (v) ) #define mpi_cmp_ui( u, v ) _gcry_mpi_cmp_ui( (u), (v) ) #define mpi_is_neg( a ) _gcry_mpi_is_neg ((a)) #define mpi_add_ui(w,u,v) _gcry_mpi_add_ui((w),(u),(v)) #define mpi_add(w,u,v) _gcry_mpi_add ((w),(u),(v)) #define mpi_addm(w,u,v,m) _gcry_mpi_addm ((w),(u),(v),(m)) #define mpi_sub_ui(w,u,v) _gcry_mpi_sub_ui ((w),(u),(v)) #define mpi_sub(w,u,v) _gcry_mpi_sub ((w),(u),(v)) #define mpi_subm(w,u,v,m) _gcry_mpi_subm ((w),(u),(v),(m)) #define mpi_mul_ui(w,u,v) _gcry_mpi_mul_ui ((w),(u),(v)) #define mpi_mul_2exp(w,u,v) _gcry_mpi_mul_2exp ((w),(u),(v)) #define mpi_mul(w,u,v) _gcry_mpi_mul ((w),(u),(v)) #define mpi_mulm(w,u,v,m) _gcry_mpi_mulm ((w),(u),(v),(m)) #define mpi_powm(w,b,e,m) _gcry_mpi_powm ( (w), (b), (e), (m) ) #define mpi_tdiv(q,r,a,m) _gcry_mpi_div ( (q), (r), (a), (m), 0) #define mpi_fdiv(q,r,a,m) _gcry_mpi_div ( (q), (r), (a), (m), -1) #define mpi_mod(r,a,m) _gcry_mpi_mod ((r), (a), (m)) #define mpi_gcd(g,a,b) _gcry_mpi_gcd ( (g), (a), (b) ) #define mpi_invm(g,a,b) _gcry_mpi_invm ( (g), (a), (b) ) #define mpi_point_new(n) _gcry_mpi_point_new((n)) #define mpi_point_release(p) \ do \ { \ _gcry_mpi_point_release ((p)); \ (p) = NULL; \ } \ while (0) #define mpi_point_copy(p) _gcry_mpi_point_copy((p)) #define mpi_point_get(x,y,z,p) _gcry_mpi_point_get((x),(y),(z),(p)) #define mpi_point_snatch_get(x,y,z,p) _gcry_mpi_point_snatch_get((x),(y), \ (z),(p)) #define mpi_point_set(p,x,y,z) _gcry_mpi_point_set((p),(x),(y),(z)) #define mpi_point_snatch_set(p,x,y,z) _gcry_mpi_point_snatch_set((p),(x), \ (y),(z)) #define mpi_point_resize(p,ctx) _gcry_mpi_ec_point_resize (p, ctx) #define mpi_get_nbits(a) _gcry_mpi_get_nbits ((a)) #define mpi_test_bit(a,b) _gcry_mpi_test_bit ((a),(b)) #define mpi_set_bit(a,b) _gcry_mpi_set_bit ((a),(b)) #define mpi_set_highbit(a,b) _gcry_mpi_set_highbit ((a),(b)) #define mpi_clear_bit(a,b) _gcry_mpi_clear_bit ((a),(b)) #define mpi_clear_highbit(a,b) _gcry_mpi_clear_highbit ((a),(b)) #define mpi_rshift(a,b,c) _gcry_mpi_rshift ((a),(b),(c)) #define mpi_lshift(a,b,c) _gcry_mpi_lshift ((a),(b),(c)) #define mpi_set_opaque(a,b,c) _gcry_mpi_set_opaque ((a), (b), (c)) #define mpi_get_opaque(a,b) _gcry_mpi_get_opaque ((a), (b)) #define mpi_set_flag(a,f) _gcry_mpi_set_flag ((a), (f)) #define mpi_set_flag(a,f) _gcry_mpi_set_flag ((a), (f)) #define mpi_clear_flag(a,f) _gcry_mpi_clear_flag ((a), (f)) #define mpi_get_flag(a,f) _gcry_mpi_get_flag ((a), (f)) #endif /*GCRY_GCRYPT_INT_H*/ diff --git a/src/gcrypt.h.in b/src/gcrypt.h.in index 0df53342..3af36f40 100644 --- a/src/gcrypt.h.in +++ b/src/gcrypt.h.in @@ -1,1911 +1,1911 @@ /* gcrypt.h - GNU Cryptographic Library Interface -*- c -*- * Copyright (C) 2012-2022 g10 Code GmbH * Copyright (C) 2013-2022 Jussi Kivilinna * Copyright (C) 1998-2018 Free Software Foundation, Inc. * * This file is part of Libgcrypt. * * Libgcrypt 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. * * Libgcrypt 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 . * * File: @configure_input@ */ #ifndef _GCRYPT_H #define _GCRYPT_H #include #include #include #include #include #if defined _WIN32 || defined __WIN32__ # ifndef __GNUC__ typedef long ssize_t; typedef int pid_t; # endif /*!__GNUC__*/ #endif /*_WIN32*/ /* This is required for error code compatibility. */ #define _GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_GCRYPT #ifdef __cplusplus extern "C" { #if 0 /* (Keep Emacsens' auto-indent happy.) */ } #endif #endif /* The version of this header should match the one of the library. It should not be used by a program because gcry_check_version() should return the same version. The purpose of this macro is to let autoconf (using the AM_PATH_GCRYPT macro) check that this header matches the installed library. */ #define GCRYPT_VERSION "@VERSION@" /* The version number of this header. It may be used to handle minor API incompatibilities. */ #define GCRYPT_VERSION_NUMBER @VERSION_NUMBER@ /* Internal: We can't use the convenience macros for the multi precision integer functions when building this library. */ #ifdef _GCRYPT_IN_LIBGCRYPT #ifndef GCRYPT_NO_MPI_MACROS #define GCRYPT_NO_MPI_MACROS 1 #endif #endif /* We want to use gcc attributes when possible. Warning: Don't use these macros in your programs: As indicated by the leading underscore they are subject to change without notice. */ #ifdef __GNUC__ #define _GCRY_GCC_VERSION (__GNUC__ * 10000 \ + __GNUC_MINOR__ * 100 \ + __GNUC_PATCHLEVEL__) #if _GCRY_GCC_VERSION >= 30100 #define _GCRY_GCC_ATTR_DEPRECATED __attribute__ ((__deprecated__)) #endif #if _GCRY_GCC_VERSION >= 29600 #define _GCRY_GCC_ATTR_PURE __attribute__ ((__pure__)) #endif #if _GCRY_GCC_VERSION >= 30200 #define _GCRY_GCC_ATTR_MALLOC __attribute__ ((__malloc__)) #endif #define _GCRY_GCC_ATTR_PRINTF(f,a) __attribute__ ((format (printf,f,a))) #if _GCRY_GCC_VERSION >= 40000 #define _GCRY_GCC_ATTR_SENTINEL(a) __attribute__ ((sentinel(a))) #endif #endif /*__GNUC__*/ #ifndef _GCRY_GCC_ATTR_DEPRECATED #define _GCRY_GCC_ATTR_DEPRECATED #endif #ifndef _GCRY_GCC_ATTR_PURE #define _GCRY_GCC_ATTR_PURE #endif #ifndef _GCRY_GCC_ATTR_MALLOC #define _GCRY_GCC_ATTR_MALLOC #endif #ifndef _GCRY_GCC_ATTR_PRINTF #define _GCRY_GCC_ATTR_PRINTF(f,a) #endif #ifndef _GCRY_GCC_ATTR_SENTINEL #define _GCRY_GCC_ATTR_SENTINEL(a) #endif /* Make up an attribute to mark functions and types as deprecated but allow internal use by Libgcrypt. */ #ifdef _GCRYPT_IN_LIBGCRYPT #define _GCRY_ATTR_INTERNAL #else #define _GCRY_ATTR_INTERNAL _GCRY_GCC_ATTR_DEPRECATED #endif /* Wrappers for the libgpg-error library. */ typedef gpg_error_t gcry_error_t; typedef gpg_err_code_t gcry_err_code_t; typedef gpg_err_source_t gcry_err_source_t; static GPG_ERR_INLINE gcry_error_t gcry_err_make (gcry_err_source_t source, gcry_err_code_t code) { return gpg_err_make (source, code); } /* The user can define GPG_ERR_SOURCE_DEFAULT before including this file to specify a default source for gpg_error. */ #ifndef GCRY_ERR_SOURCE_DEFAULT #define GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_1 #endif static GPG_ERR_INLINE gcry_error_t gcry_error (gcry_err_code_t code) { return gcry_err_make (GCRY_ERR_SOURCE_DEFAULT, code); } static GPG_ERR_INLINE gcry_err_code_t gcry_err_code (gcry_error_t err) { return gpg_err_code (err); } static GPG_ERR_INLINE gcry_err_source_t gcry_err_source (gcry_error_t err) { return gpg_err_source (err); } /* Return a pointer to a string containing a description of the error code in the error value ERR. */ const char *gcry_strerror (gcry_error_t err); /* Return a pointer to a string containing a description of the error source in the error value ERR. */ const char *gcry_strsource (gcry_error_t err); /* Retrieve the error code for the system error ERR. This returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report this). */ gcry_err_code_t gcry_err_code_from_errno (int err); /* Retrieve the system error for the error code CODE. This returns 0 if CODE is not a system error code. */ int gcry_err_code_to_errno (gcry_err_code_t code); /* Return an error value with the error source SOURCE and the system error ERR. */ gcry_error_t gcry_err_make_from_errno (gcry_err_source_t source, int err); /* Return an error value with the system error ERR. */ gcry_error_t gcry_error_from_errno (int err); /* NOTE: Since Libgcrypt 1.6 the thread callbacks are not anymore used. However we keep it to allow for some source code compatibility if used in the standard way. */ /* Constants defining the thread model to use. Used with the OPTION field of the struct gcry_thread_cbs. */ #define GCRY_THREAD_OPTION_DEFAULT 0 #define GCRY_THREAD_OPTION_USER 1 #define GCRY_THREAD_OPTION_PTH 2 #define GCRY_THREAD_OPTION_PTHREAD 3 /* The version number encoded in the OPTION field of the struct gcry_thread_cbs. */ #define GCRY_THREAD_OPTION_VERSION 1 /* Wrapper for struct ath_ops. */ struct gcry_thread_cbs { /* The OPTION field encodes the thread model and the version number of this structure. Bits 7 - 0 are used for the thread model Bits 15 - 8 are used for the version number. */ unsigned int option; } _GCRY_ATTR_INTERNAL; #define GCRY_THREAD_OPTION_PTH_IMPL \ static struct gcry_thread_cbs gcry_threads_pth = { \ (GCRY_THREAD_OPTION_PTH | (GCRY_THREAD_OPTION_VERSION << 8))} #define GCRY_THREAD_OPTION_PTHREAD_IMPL \ static struct gcry_thread_cbs gcry_threads_pthread = { \ (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8))} /* A generic context object as used by some functions. */ struct gcry_context; typedef struct gcry_context *gcry_ctx_t; /* The data objects used to hold multi precision integers. */ struct gcry_mpi; typedef struct gcry_mpi *gcry_mpi_t; struct gcry_mpi_point; typedef struct gcry_mpi_point *gcry_mpi_point_t; #ifndef GCRYPT_NO_DEPRECATED typedef struct gcry_mpi *GCRY_MPI _GCRY_GCC_ATTR_DEPRECATED; typedef struct gcry_mpi *GcryMPI _GCRY_GCC_ATTR_DEPRECATED; #endif /* A structure used for scatter gather hashing. */ typedef struct { size_t size; /* The allocated size of the buffer or 0. */ size_t off; /* Offset into the buffer. */ size_t len; /* The used length of the buffer. */ void *data; /* The buffer. */ } gcry_buffer_t; /* Check that the library fulfills the version requirement. */ const char *gcry_check_version (const char *req_version); /* Codes for function dispatchers. */ /* Codes used with the gcry_control function. */ enum gcry_ctl_cmds { /* Note: 1 .. 2 are not anymore used. */ GCRYCTL_CFB_SYNC = 3, GCRYCTL_RESET = 4, /* e.g. for MDs */ GCRYCTL_FINALIZE = 5, GCRYCTL_GET_KEYLEN = 6, GCRYCTL_GET_BLKLEN = 7, GCRYCTL_TEST_ALGO = 8, GCRYCTL_IS_SECURE = 9, GCRYCTL_GET_ASNOID = 10, GCRYCTL_ENABLE_ALGO = 11, GCRYCTL_DISABLE_ALGO = 12, GCRYCTL_DUMP_RANDOM_STATS = 13, GCRYCTL_DUMP_SECMEM_STATS = 14, GCRYCTL_GET_ALGO_NPKEY = 15, GCRYCTL_GET_ALGO_NSKEY = 16, GCRYCTL_GET_ALGO_NSIGN = 17, GCRYCTL_GET_ALGO_NENCR = 18, GCRYCTL_SET_VERBOSITY = 19, GCRYCTL_SET_DEBUG_FLAGS = 20, GCRYCTL_CLEAR_DEBUG_FLAGS = 21, GCRYCTL_USE_SECURE_RNDPOOL= 22, GCRYCTL_DUMP_MEMORY_STATS = 23, GCRYCTL_INIT_SECMEM = 24, GCRYCTL_TERM_SECMEM = 25, GCRYCTL_DISABLE_SECMEM_WARN = 27, GCRYCTL_SUSPEND_SECMEM_WARN = 28, GCRYCTL_RESUME_SECMEM_WARN = 29, GCRYCTL_DROP_PRIVS = 30, GCRYCTL_ENABLE_M_GUARD = 31, GCRYCTL_START_DUMP = 32, GCRYCTL_STOP_DUMP = 33, GCRYCTL_GET_ALGO_USAGE = 34, GCRYCTL_IS_ALGO_ENABLED = 35, GCRYCTL_DISABLE_INTERNAL_LOCKING = 36, GCRYCTL_DISABLE_SECMEM = 37, GCRYCTL_INITIALIZATION_FINISHED = 38, GCRYCTL_INITIALIZATION_FINISHED_P = 39, GCRYCTL_ANY_INITIALIZATION_P = 40, GCRYCTL_SET_CBC_CTS = 41, GCRYCTL_SET_CBC_MAC = 42, /* Note: 43 is not anymore used. */ GCRYCTL_ENABLE_QUICK_RANDOM = 44, GCRYCTL_SET_RANDOM_SEED_FILE = 45, GCRYCTL_UPDATE_RANDOM_SEED_FILE = 46, GCRYCTL_SET_THREAD_CBS = 47, GCRYCTL_FAST_POLL = 48, GCRYCTL_SET_RANDOM_DAEMON_SOCKET = 49, GCRYCTL_USE_RANDOM_DAEMON = 50, GCRYCTL_FAKED_RANDOM_P = 51, GCRYCTL_SET_RNDEGD_SOCKET = 52, GCRYCTL_PRINT_CONFIG = 53, GCRYCTL_OPERATIONAL_P = 54, GCRYCTL_FIPS_MODE_P = 55, GCRYCTL_FORCE_FIPS_MODE = 56, GCRYCTL_SELFTEST = 57, /* Note: 58 .. 62 are used internally. */ GCRYCTL_DISABLE_HWF = 63, GCRYCTL_SET_ENFORCED_FIPS_FLAG = 64, GCRYCTL_SET_PREFERRED_RNG_TYPE = 65, GCRYCTL_GET_CURRENT_RNG_TYPE = 66, GCRYCTL_DISABLE_LOCKED_SECMEM = 67, GCRYCTL_DISABLE_PRIV_DROP = 68, GCRYCTL_SET_CCM_LENGTHS = 69, GCRYCTL_CLOSE_RANDOM_DEVICE = 70, GCRYCTL_INACTIVATE_FIPS_FLAG = 71, GCRYCTL_REACTIVATE_FIPS_FLAG = 72, GCRYCTL_SET_SBOX = 73, GCRYCTL_DRBG_REINIT = 74, GCRYCTL_SET_TAGLEN = 75, GCRYCTL_GET_TAGLEN = 76, GCRYCTL_REINIT_SYSCALL_CLAMP = 77, GCRYCTL_AUTO_EXPAND_SECMEM = 78, GCRYCTL_SET_ALLOW_WEAK_KEY = 79, GCRYCTL_SET_DECRYPTION_TAG = 80, GCRYCTL_FIPS_SERVICE_INDICATOR_CIPHER = 81, GCRYCTL_FIPS_SERVICE_INDICATOR_KDF = 82, GCRYCTL_NO_FIPS_MODE = 83 }; /* Perform various operations defined by CMD. */ gcry_error_t gcry_control (enum gcry_ctl_cmds CMD, ...); /* S-expression management. */ /* The object to represent an S-expression as used with the public key functions. */ struct gcry_sexp; typedef struct gcry_sexp *gcry_sexp_t; #ifndef GCRYPT_NO_DEPRECATED typedef struct gcry_sexp *GCRY_SEXP _GCRY_GCC_ATTR_DEPRECATED; typedef struct gcry_sexp *GcrySexp _GCRY_GCC_ATTR_DEPRECATED; #endif /* The possible values for the S-expression format. */ enum gcry_sexp_format { GCRYSEXP_FMT_DEFAULT = 0, GCRYSEXP_FMT_CANON = 1, GCRYSEXP_FMT_BASE64 = 2, GCRYSEXP_FMT_ADVANCED = 3 }; /* Create an new S-expression object from BUFFER of size LENGTH and return it in RETSEXP. With AUTODETECT set to 0 the data in BUFFER is expected to be in canonized format. */ gcry_error_t gcry_sexp_new (gcry_sexp_t *retsexp, const void *buffer, size_t length, int autodetect); /* Same as gcry_sexp_new but allows to pass a FREEFNC which has the effect to transfer ownership of BUFFER to the created object. */ gcry_error_t gcry_sexp_create (gcry_sexp_t *retsexp, void *buffer, size_t length, int autodetect, void (*freefnc) (void *)); /* Scan BUFFER and return a new S-expression object in RETSEXP. This function expects a printf like string in BUFFER. */ gcry_error_t gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff, const char *buffer, size_t length); /* Same as gcry_sexp_sscan but expects a string in FORMAT and can thus only be used for certain encodings. */ gcry_error_t gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff, const char *format, ...); /* Like gcry_sexp_build, but uses an array instead of variable function arguments. */ gcry_error_t gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff, const char *format, void **arg_list); /* Release the S-expression object SEXP */ void gcry_sexp_release (gcry_sexp_t sexp); /* Calculate the length of an canonized S-expression in BUFFER and check for a valid encoding. */ size_t gcry_sexp_canon_len (const unsigned char *buffer, size_t length, size_t *erroff, gcry_error_t *errcode); /* Copies the S-expression object SEXP into BUFFER using the format specified in MODE. */ size_t gcry_sexp_sprint (gcry_sexp_t sexp, int mode, void *buffer, size_t maxlength); /* Dumps the S-expression object A in a format suitable for debugging to Libgcrypt's logging stream. */ void gcry_sexp_dump (const gcry_sexp_t a); gcry_sexp_t gcry_sexp_cons (const gcry_sexp_t a, const gcry_sexp_t b); gcry_sexp_t gcry_sexp_alist (const gcry_sexp_t *array); gcry_sexp_t gcry_sexp_vlist (const gcry_sexp_t a, ...); gcry_sexp_t gcry_sexp_append (const gcry_sexp_t a, const gcry_sexp_t n); gcry_sexp_t gcry_sexp_prepend (const gcry_sexp_t a, const gcry_sexp_t n); /* Scan the S-expression for a sublist with a type (the car of the list) matching the string TOKEN. If TOKLEN is not 0, the token is assumed to be raw memory of this length. The function returns a newly allocated S-expression consisting of the found sublist or `NULL' when not found. */ gcry_sexp_t gcry_sexp_find_token (gcry_sexp_t list, const char *tok, size_t toklen); /* Return the length of the LIST. For a valid S-expression this should be at least 1. */ int gcry_sexp_length (const gcry_sexp_t list); /* Create and return a new S-expression from the element with index NUMBER in LIST. Note that the first element has the index 0. If there is no such element, `NULL' is returned. */ gcry_sexp_t gcry_sexp_nth (const gcry_sexp_t list, int number); /* Create and return a new S-expression from the first element in LIST; this called the "type" and should always exist and be a string. `NULL' is returned in case of a problem. */ gcry_sexp_t gcry_sexp_car (const gcry_sexp_t list); /* Create and return a new list form all elements except for the first one. Note, that this function may return an invalid S-expression because it is not guaranteed, that the type exists and is a string. However, for parsing a complex S-expression it might be useful for intermediate lists. Returns `NULL' on error. */ gcry_sexp_t gcry_sexp_cdr (const gcry_sexp_t list); gcry_sexp_t gcry_sexp_cadr (const gcry_sexp_t list); /* This function is used to get data from a LIST. A pointer to the actual data with index NUMBER is returned and the length of this data will be stored to DATALEN. If there is no data at the given index or the index represents another list, `NULL' is returned. *Note:* The returned pointer is valid as long as LIST is not modified or released. */ const char *gcry_sexp_nth_data (const gcry_sexp_t list, int number, size_t *datalen); /* This function is used to get data from a LIST. A malloced buffer to the data with index NUMBER is returned and the length of this data will be stored to RLENGTH. If there is no data at the given index or the index represents another list, `NULL' is returned. */ void *gcry_sexp_nth_buffer (const gcry_sexp_t list, int number, size_t *rlength); /* This function is used to get and convert data from a LIST. The data is assumed to be a Nul terminated string. The caller must release the returned value using `gcry_free'. If there is no data at the given index, the index represents a list or the value can't be converted to a string, `NULL' is returned. */ char *gcry_sexp_nth_string (gcry_sexp_t list, int number); /* This function is used to get and convert data from a LIST. This data is assumed to be an MPI stored in the format described by MPIFMT and returned as a standard Libgcrypt MPI. The caller must release this returned value using `gcry_mpi_release'. If there is no data at the given index, the index represents a list or the value can't be converted to an MPI, `NULL' is returned. */ gcry_mpi_t gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt); /* Extract MPIs from an s-expression using a list of parameters. The * names of these parameters are given by the string LIST. Some * special characters may be given to control the conversion: * * + :: Switch to unsigned integer format (default). * - :: Switch to standard signed format. * / :: Switch to opaque format. * & :: Switch to buffer descriptor mode - see below. * ? :: The previous parameter is optional. * * In general parameter names are single letters. To use a string for * a parameter name, enclose the name in single quotes. * * Unless in gcry_buffer_t mode for each parameter name a pointer to * an MPI variable is expected that must be set to NULL prior to * invoking this function, and finally a NULL is expected. Example: * * _gcry_sexp_extract_param (key, NULL, "n/x+ed", * &mpi_n, &mpi_x, &mpi_e, NULL) * * This stores the parameter "N" from KEY as an unsigned MPI into * MPI_N, the parameter "X" as an opaque MPI into MPI_X, and the * parameter "E" again as an unsigned MPI into MPI_E. * * If in buffer descriptor mode a pointer to gcry_buffer_t descriptor * is expected instead of a pointer to an MPI. The caller may use two * different operation modes: If the DATA field of the provided buffer * descriptor is NULL, the function allocates a new buffer and stores * it at DATA; the other fields are set accordingly with OFF being 0. * If DATA is not NULL, the function assumes that DATA, SIZE, and OFF * describe a buffer where to but the data; on return the LEN field * receives the number of bytes copied to that buffer; if the buffer * is too small, the function immediately returns with an error code * (and LEN set to 0). * * PATH is an optional string used to locate a token. The exclamation * mark separated tokens are used to via gcry_sexp_find_token to find * a start point inside SEXP. * * The function returns 0 on success. On error an error code is * returned, all passed MPIs that might have been allocated up to this * point are deallocated and set to NULL, and all passed buffers are * either truncated if the caller supplied the buffer, or deallocated * if the function allocated the buffer. */ gpg_error_t gcry_sexp_extract_param (gcry_sexp_t sexp, const char *path, const char *list, ...) _GCRY_GCC_ATTR_SENTINEL(0); /******************************************* * * * Multi Precision Integer Functions * * * *******************************************/ /* Different formats of external big integer representation. */ enum gcry_mpi_format { GCRYMPI_FMT_NONE= 0, GCRYMPI_FMT_STD = 1, /* Twos complement stored without length. */ GCRYMPI_FMT_PGP = 2, /* As used by OpenPGP (unsigned only). */ GCRYMPI_FMT_SSH = 3, /* As used by SSH (like STD but with length). */ GCRYMPI_FMT_HEX = 4, /* Hex format. */ GCRYMPI_FMT_USG = 5, /* Like STD but unsigned. */ GCRYMPI_FMT_OPAQUE = 8 /* Opaque format (some functions only). */ }; /* Flags used for creating big integers. */ enum gcry_mpi_flag { GCRYMPI_FLAG_SECURE = 1, /* Allocate the number in "secure" memory. */ GCRYMPI_FLAG_OPAQUE = 2, /* The number is not a real one but just a way to store some bytes. This is useful for encrypted big integers. */ GCRYMPI_FLAG_IMMUTABLE = 4, /* Mark the MPI as immutable. */ GCRYMPI_FLAG_CONST = 8, /* Mark the MPI as a constant. */ GCRYMPI_FLAG_USER1 = 0x0100,/* User flag 1. */ GCRYMPI_FLAG_USER2 = 0x0200,/* User flag 2. */ GCRYMPI_FLAG_USER3 = 0x0400,/* User flag 3. */ GCRYMPI_FLAG_USER4 = 0x0800 /* User flag 4. */ }; /* Macros to return pre-defined MPI constants. */ #define GCRYMPI_CONST_ONE (_gcry_mpi_get_const (1)) #define GCRYMPI_CONST_TWO (_gcry_mpi_get_const (2)) #define GCRYMPI_CONST_THREE (_gcry_mpi_get_const (3)) #define GCRYMPI_CONST_FOUR (_gcry_mpi_get_const (4)) #define GCRYMPI_CONST_EIGHT (_gcry_mpi_get_const (8)) /* Allocate a new big integer object, initialize it with 0 and initially allocate memory for a number of at least NBITS. */ gcry_mpi_t gcry_mpi_new (unsigned int nbits); /* Same as gcry_mpi_new() but allocate in "secure" memory. */ gcry_mpi_t gcry_mpi_snew (unsigned int nbits); /* Release the number A and free all associated resources. */ void gcry_mpi_release (gcry_mpi_t a); /* Create a new number with the same value as A. */ gcry_mpi_t gcry_mpi_copy (const gcry_mpi_t a); /* Store the big integer value U in W and release U. */ void gcry_mpi_snatch (gcry_mpi_t w, gcry_mpi_t u); /* Store the big integer value U in W. */ gcry_mpi_t gcry_mpi_set (gcry_mpi_t w, const gcry_mpi_t u); /* Store the unsigned integer value U in W. */ gcry_mpi_t gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u); /* Store U as an unsigned int at W or return GPG_ERR_ERANGE. */ gpg_error_t gcry_mpi_get_ui (unsigned int *w, gcry_mpi_t u); /* Swap the values of A and B. */ void gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b); /* Return 1 if A is negative; 0 if zero or positive. */ int gcry_mpi_is_neg (gcry_mpi_t a); /* W = - U */ void gcry_mpi_neg (gcry_mpi_t w, gcry_mpi_t u); /* W = [W] */ void gcry_mpi_abs (gcry_mpi_t w); /* Compare the big integer number U and V returning 0 for equality, a positive value for U > V and a negative for U < V. */ int gcry_mpi_cmp (const gcry_mpi_t u, const gcry_mpi_t v); /* Compare the big integer number U with the unsigned integer V returning 0 for equality, a positive value for U > V and a negative for U < V. */ int gcry_mpi_cmp_ui (const gcry_mpi_t u, unsigned long v); /* Convert the external representation of an integer stored in BUFFER with a length of BUFLEN into a newly create MPI returned in RET_MPI. If NSCANNED is not NULL, it will receive the number of bytes actually scanned after a successful operation. */ gcry_error_t gcry_mpi_scan (gcry_mpi_t *ret_mpi, enum gcry_mpi_format format, const void *buffer, size_t buflen, size_t *nscanned); /* Convert the big integer A into the external representation described by FORMAT and store it in the provided BUFFER which has been allocated by the user with a size of BUFLEN bytes. NWRITTEN receives the actual length of the external representation unless it has been passed as NULL. */ gcry_error_t gcry_mpi_print (enum gcry_mpi_format format, unsigned char *buffer, size_t buflen, size_t *nwritten, const gcry_mpi_t a); /* Convert the big integer A into the external representation described by FORMAT and store it in a newly allocated buffer which address will be put into BUFFER. NWRITTEN receives the actual lengths of the external representation. */ gcry_error_t gcry_mpi_aprint (enum gcry_mpi_format format, unsigned char **buffer, size_t *nwritten, const gcry_mpi_t a); /* Dump the value of A in a format suitable for debugging to Libgcrypt's logging stream. Note that one leading space but no trailing space or linefeed will be printed. It is okay to pass NULL for A. */ void gcry_mpi_dump (const gcry_mpi_t a); /* W = U + V. */ void gcry_mpi_add (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); /* W = U + V. V is an unsigned integer. */ void gcry_mpi_add_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v); /* W = U + V mod M. */ void gcry_mpi_addm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); /* W = U - V. */ void gcry_mpi_sub (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); /* W = U - V. V is an unsigned integer. */ void gcry_mpi_sub_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ); /* W = U - V mod M */ void gcry_mpi_subm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); /* W = U * V. */ void gcry_mpi_mul (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); /* W = U * V. V is an unsigned integer. */ void gcry_mpi_mul_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ); /* W = U * V mod M. */ void gcry_mpi_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); /* W = U * (2 ^ CNT). */ void gcry_mpi_mul_2exp (gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt); /* Q = DIVIDEND / DIVISOR, R = DIVIDEND % DIVISOR, Q or R may be passed as NULL. ROUND should be negative or 0. */ void gcry_mpi_div (gcry_mpi_t q, gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor, int round); /* R = DIVIDEND % DIVISOR */ void gcry_mpi_mod (gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor); /* W = B ^ E mod M. */ void gcry_mpi_powm (gcry_mpi_t w, const gcry_mpi_t b, const gcry_mpi_t e, const gcry_mpi_t m); /* Set G to the greatest common divisor of A and B. Return true if the G is 1. */ int gcry_mpi_gcd (gcry_mpi_t g, gcry_mpi_t a, gcry_mpi_t b); /* Set X to the multiplicative inverse of A mod M. Return true if the value exists. */ int gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t m); /* Create a new point object. NBITS is usually 0. */ gcry_mpi_point_t gcry_mpi_point_new (unsigned int nbits); /* Release the object POINT. POINT may be NULL. */ void gcry_mpi_point_release (gcry_mpi_point_t point); /* Return a copy of POINT. */ gcry_mpi_point_t gcry_mpi_point_copy (gcry_mpi_point_t point); /* Store the projective coordinates from POINT into X, Y, and Z. */ void gcry_mpi_point_get (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z, gcry_mpi_point_t point); /* Store the projective coordinates from POINT into X, Y, and Z and release POINT. */ void gcry_mpi_point_snatch_get (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z, gcry_mpi_point_t point); /* Store the projective coordinates X, Y, and Z into POINT. */ gcry_mpi_point_t gcry_mpi_point_set (gcry_mpi_point_t point, gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z); /* Store the projective coordinates X, Y, and Z into POINT and release X, Y, and Z. */ gcry_mpi_point_t gcry_mpi_point_snatch_set (gcry_mpi_point_t point, gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z); /* Allocate a new context for elliptic curve operations based on the parameters given by KEYPARAM or using CURVENAME. */ gpg_error_t gcry_mpi_ec_new (gcry_ctx_t *r_ctx, gcry_sexp_t keyparam, const char *curvename); /* Get a named MPI from an elliptic curve context. */ gcry_mpi_t gcry_mpi_ec_get_mpi (const char *name, gcry_ctx_t ctx, int copy); /* Get a named point from an elliptic curve context. */ gcry_mpi_point_t gcry_mpi_ec_get_point (const char *name, gcry_ctx_t ctx, int copy); /* Store a named MPI into an elliptic curve context. */ gpg_error_t gcry_mpi_ec_set_mpi (const char *name, gcry_mpi_t newvalue, gcry_ctx_t ctx); /* Store a named point into an elliptic curve context. */ gpg_error_t gcry_mpi_ec_set_point (const char *name, gcry_mpi_point_t newvalue, gcry_ctx_t ctx); /* Decode and store VALUE into RESULT. */ gpg_error_t gcry_mpi_ec_decode_point (gcry_mpi_point_t result, gcry_mpi_t value, gcry_ctx_t ctx); /* Store the affine coordinates of POINT into X and Y. */ int gcry_mpi_ec_get_affine (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_point_t point, gcry_ctx_t ctx); /* W = 2 * U. */ void gcry_mpi_ec_dup (gcry_mpi_point_t w, gcry_mpi_point_t u, gcry_ctx_t ctx); /* W = U + V. */ void gcry_mpi_ec_add (gcry_mpi_point_t w, gcry_mpi_point_t u, gcry_mpi_point_t v, gcry_ctx_t ctx); /* W = U - V. */ void gcry_mpi_ec_sub (gcry_mpi_point_t w, gcry_mpi_point_t u, gcry_mpi_point_t v, gcry_ctx_t ctx); /* W = N * U. */ void gcry_mpi_ec_mul (gcry_mpi_point_t w, gcry_mpi_t n, gcry_mpi_point_t u, gcry_ctx_t ctx); /* Return true if POINT is on the curve described by CTX. */ int gcry_mpi_ec_curve_point (gcry_mpi_point_t w, gcry_ctx_t ctx); /* Return the number of bits required to represent A. */ unsigned int gcry_mpi_get_nbits (gcry_mpi_t a); /* Return true when bit number N (counting from 0) is set in A. */ int gcry_mpi_test_bit (gcry_mpi_t a, unsigned int n); /* Set bit number N in A. */ void gcry_mpi_set_bit (gcry_mpi_t a, unsigned int n); /* Clear bit number N in A. */ void gcry_mpi_clear_bit (gcry_mpi_t a, unsigned int n); /* Set bit number N in A and clear all bits greater than N. */ void gcry_mpi_set_highbit (gcry_mpi_t a, unsigned int n); /* Clear bit number N in A and all bits greater than N. */ void gcry_mpi_clear_highbit (gcry_mpi_t a, unsigned int n); /* Shift the value of A by N bits to the right and store the result in X. */ void gcry_mpi_rshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n); /* Shift the value of A by N bits to the left and store the result in X. */ void gcry_mpi_lshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n); /* Store NBITS of the value P points to in A and mark A as an opaque value. On success A received the the ownership of the value P. WARNING: Never use an opaque MPI for anything thing else than gcry_mpi_release, gcry_mpi_get_opaque. */ gcry_mpi_t gcry_mpi_set_opaque (gcry_mpi_t a, void *p, unsigned int nbits); /* Store NBITS of the value P points to in A and mark A as an opaque value. The function takes a copy of the provided value P. WARNING: Never use an opaque MPI for anything thing else than gcry_mpi_release, gcry_mpi_get_opaque. */ gcry_mpi_t gcry_mpi_set_opaque_copy (gcry_mpi_t a, const void *p, unsigned int nbits); /* Return a pointer to an opaque value stored in A and return its size in NBITS. Note that the returned pointer is still owned by A and that the function should never be used for an non-opaque MPI. */ void *gcry_mpi_get_opaque (gcry_mpi_t a, unsigned int *nbits); /* Set the FLAG for the big integer A. Currently only the flag GCRYMPI_FLAG_SECURE is allowed to convert A into an big intger stored in "secure" memory. */ void gcry_mpi_set_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); /* Clear FLAG for the big integer A. Note that this function is currently useless as no flags are allowed. */ void gcry_mpi_clear_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); /* Return true if the FLAG is set for A. */ int gcry_mpi_get_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); /* Private function - do not use. */ gcry_mpi_t _gcry_mpi_get_const (int no); /* Unless the GCRYPT_NO_MPI_MACROS is used, provide a couple of convenience macros for the big integer functions. */ #ifndef GCRYPT_NO_MPI_MACROS #define mpi_new(n) gcry_mpi_new( (n) ) #define mpi_secure_new( n ) gcry_mpi_snew( (n) ) #define mpi_release(a) \ do \ { \ gcry_mpi_release ((a)); \ (a) = NULL; \ } \ while (0) #define mpi_copy( a ) gcry_mpi_copy( (a) ) #define mpi_snatch( w, u) gcry_mpi_snatch( (w), (u) ) #define mpi_set( w, u) gcry_mpi_set( (w), (u) ) #define mpi_set_ui( w, u) gcry_mpi_set_ui( (w), (u) ) #define mpi_get_ui( w, u) gcry_mpi_get_ui( (w), (u) ) #define mpi_abs( w ) gcry_mpi_abs( (w) ) #define mpi_neg( w, u) gcry_mpi_neg( (w), (u) ) #define mpi_cmp( u, v ) gcry_mpi_cmp( (u), (v) ) #define mpi_cmp_ui( u, v ) gcry_mpi_cmp_ui( (u), (v) ) #define mpi_is_neg( a ) gcry_mpi_is_neg ((a)) #define mpi_add_ui(w,u,v) gcry_mpi_add_ui((w),(u),(v)) #define mpi_add(w,u,v) gcry_mpi_add ((w),(u),(v)) #define mpi_addm(w,u,v,m) gcry_mpi_addm ((w),(u),(v),(m)) #define mpi_sub_ui(w,u,v) gcry_mpi_sub_ui ((w),(u),(v)) #define mpi_sub(w,u,v) gcry_mpi_sub ((w),(u),(v)) #define mpi_subm(w,u,v,m) gcry_mpi_subm ((w),(u),(v),(m)) #define mpi_mul_ui(w,u,v) gcry_mpi_mul_ui ((w),(u),(v)) #define mpi_mul_2exp(w,u,v) gcry_mpi_mul_2exp ((w),(u),(v)) #define mpi_mul(w,u,v) gcry_mpi_mul ((w),(u),(v)) #define mpi_mulm(w,u,v,m) gcry_mpi_mulm ((w),(u),(v),(m)) #define mpi_powm(w,b,e,m) gcry_mpi_powm ( (w), (b), (e), (m) ) #define mpi_tdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), 0) #define mpi_fdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), -1) #define mpi_mod(r,a,m) gcry_mpi_mod ((r), (a), (m)) #define mpi_gcd(g,a,b) gcry_mpi_gcd ( (g), (a), (b) ) #define mpi_invm(g,a,b) gcry_mpi_invm ( (g), (a), (b) ) #define mpi_point_new(n) gcry_mpi_point_new((n)) #define mpi_point_release(p) \ do \ { \ gcry_mpi_point_release ((p)); \ (p) = NULL; \ } \ while (0) #define mpi_point_copy(p) gcry_mpi_point_copy((p)) #define mpi_point_get(x,y,z,p) gcry_mpi_point_get((x),(y),(z),(p)) #define mpi_point_snatch_get(x,y,z,p) gcry_mpi_point_snatch_get((x),(y),(z),(p)) #define mpi_point_set(p,x,y,z) gcry_mpi_point_set((p),(x),(y),(z)) #define mpi_point_snatch_set(p,x,y,z) gcry_mpi_point_snatch_set((p),(x),(y),(z)) #define mpi_get_nbits(a) gcry_mpi_get_nbits ((a)) #define mpi_test_bit(a,b) gcry_mpi_test_bit ((a),(b)) #define mpi_set_bit(a,b) gcry_mpi_set_bit ((a),(b)) #define mpi_set_highbit(a,b) gcry_mpi_set_highbit ((a),(b)) #define mpi_clear_bit(a,b) gcry_mpi_clear_bit ((a),(b)) #define mpi_clear_highbit(a,b) gcry_mpi_clear_highbit ((a),(b)) #define mpi_rshift(a,b,c) gcry_mpi_rshift ((a),(b),(c)) #define mpi_lshift(a,b,c) gcry_mpi_lshift ((a),(b),(c)) #define mpi_set_opaque(a,b,c) gcry_mpi_set_opaque( (a), (b), (c) ) #define mpi_get_opaque(a,b) gcry_mpi_get_opaque( (a), (b) ) #endif /* GCRYPT_NO_MPI_MACROS */ /************************************ * * * Symmetric Cipher Functions * * * ************************************/ /* The data object used to hold a handle to an encryption object. */ struct gcry_cipher_handle; typedef struct gcry_cipher_handle *gcry_cipher_hd_t; #ifndef GCRYPT_NO_DEPRECATED typedef struct gcry_cipher_handle *GCRY_CIPHER_HD _GCRY_GCC_ATTR_DEPRECATED; typedef struct gcry_cipher_handle *GcryCipherHd _GCRY_GCC_ATTR_DEPRECATED; #endif /* All symmetric encryption algorithms are identified by their IDs. More IDs may be registered at runtime. */ enum gcry_cipher_algos { GCRY_CIPHER_NONE = 0, GCRY_CIPHER_IDEA = 1, GCRY_CIPHER_3DES = 2, GCRY_CIPHER_CAST5 = 3, GCRY_CIPHER_BLOWFISH = 4, GCRY_CIPHER_SAFER_SK128 = 5, GCRY_CIPHER_DES_SK = 6, GCRY_CIPHER_AES = 7, GCRY_CIPHER_AES192 = 8, GCRY_CIPHER_AES256 = 9, GCRY_CIPHER_TWOFISH = 10, /* Other cipher numbers are above 300 for OpenPGP reasons. */ GCRY_CIPHER_ARCFOUR = 301, /* Fully compatible with RSA's RC4 (tm). */ GCRY_CIPHER_DES = 302, /* Yes, this is single key 56 bit DES. */ GCRY_CIPHER_TWOFISH128 = 303, GCRY_CIPHER_SERPENT128 = 304, GCRY_CIPHER_SERPENT192 = 305, GCRY_CIPHER_SERPENT256 = 306, GCRY_CIPHER_RFC2268_40 = 307, /* Ron's Cipher 2 (40 bit). */ GCRY_CIPHER_RFC2268_128 = 308, /* Ron's Cipher 2 (128 bit). */ GCRY_CIPHER_SEED = 309, /* 128 bit cipher described in RFC4269. */ GCRY_CIPHER_CAMELLIA128 = 310, GCRY_CIPHER_CAMELLIA192 = 311, GCRY_CIPHER_CAMELLIA256 = 312, GCRY_CIPHER_SALSA20 = 313, GCRY_CIPHER_SALSA20R12 = 314, GCRY_CIPHER_GOST28147 = 315, GCRY_CIPHER_CHACHA20 = 316, GCRY_CIPHER_GOST28147_MESH = 317, /* With CryptoPro key meshing. */ GCRY_CIPHER_SM4 = 318 }; /* The Rijndael algorithm is basically AES, so provide some macros. */ #define GCRY_CIPHER_AES128 GCRY_CIPHER_AES #define GCRY_CIPHER_RIJNDAEL GCRY_CIPHER_AES #define GCRY_CIPHER_RIJNDAEL128 GCRY_CIPHER_AES128 #define GCRY_CIPHER_RIJNDAEL192 GCRY_CIPHER_AES192 #define GCRY_CIPHER_RIJNDAEL256 GCRY_CIPHER_AES256 /* The supported encryption modes. Note that not all of them are supported for each algorithm. */ enum gcry_cipher_modes { GCRY_CIPHER_MODE_NONE = 0, /* Not yet specified. */ GCRY_CIPHER_MODE_ECB = 1, /* Electronic codebook. */ GCRY_CIPHER_MODE_CFB = 2, /* Cipher feedback. */ GCRY_CIPHER_MODE_CBC = 3, /* Cipher block chaining. */ GCRY_CIPHER_MODE_STREAM = 4, /* Used with stream ciphers. */ GCRY_CIPHER_MODE_OFB = 5, /* Outer feedback. */ GCRY_CIPHER_MODE_CTR = 6, /* Counter. */ GCRY_CIPHER_MODE_AESWRAP = 7, /* AES-WRAP algorithm. */ GCRY_CIPHER_MODE_CCM = 8, /* Counter with CBC-MAC. */ GCRY_CIPHER_MODE_GCM = 9, /* Galois Counter Mode. */ GCRY_CIPHER_MODE_POLY1305 = 10, /* Poly1305 based AEAD mode. */ GCRY_CIPHER_MODE_OCB = 11, /* OCB3 mode. */ GCRY_CIPHER_MODE_CFB8 = 12, /* Cipher feedback (8 bit mode). */ GCRY_CIPHER_MODE_XTS = 13, /* XTS mode. */ GCRY_CIPHER_MODE_EAX = 14, /* EAX mode. */ GCRY_CIPHER_MODE_SIV = 15, /* SIV mode. */ GCRY_CIPHER_MODE_GCM_SIV = 16 /* GCM-SIV mode. */ }; /* Flags used with the open function. */ enum gcry_cipher_flags { GCRY_CIPHER_SECURE = 1, /* Allocate in secure memory. */ GCRY_CIPHER_ENABLE_SYNC = 2, /* Enable CFB sync mode. */ GCRY_CIPHER_CBC_CTS = 4, /* Enable CBC cipher text stealing (CTS). */ GCRY_CIPHER_CBC_MAC = 8, /* Enable CBC message auth. code (MAC). */ GCRY_CIPHER_EXTENDED = 16 /* Enable extended AES-WRAP. */ }; /* GCM works only with blocks of 128 bits */ #define GCRY_GCM_BLOCK_LEN (128 / 8) /* CCM works only with blocks of 128 bits. */ #define GCRY_CCM_BLOCK_LEN (128 / 8) /* OCB works only with blocks of 128 bits. */ #define GCRY_OCB_BLOCK_LEN (128 / 8) /* XTS works only with blocks of 128 bits. */ #define GCRY_XTS_BLOCK_LEN (128 / 8) /* SIV and GCM-SIV works only with blocks of 128 bits */ #define GCRY_SIV_BLOCK_LEN (128 / 8) /* Create a handle for algorithm ALGO to be used in MODE. FLAGS may be given as an bitwise OR of the gcry_cipher_flags values. */ gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *handle, int algo, int mode, unsigned int flags); /* Close the cipher handle H and release all resource. */ void gcry_cipher_close (gcry_cipher_hd_t h); /* Perform various operations on the cipher object H. */ gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer, size_t buflen); /* Retrieve various information about the cipher object H. */ gcry_error_t gcry_cipher_info (gcry_cipher_hd_t h, int what, void *buffer, size_t *nbytes); /* Retrieve various information about the cipher algorithm ALGO. */ gcry_error_t gcry_cipher_algo_info (int algo, int what, void *buffer, size_t *nbytes); /* Map the cipher algorithm whose ID is contained in ALGORITHM to a string representation of the algorithm name. For unknown algorithm IDs this function returns "?". */ const char *gcry_cipher_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE; /* Map the algorithm name NAME to an cipher algorithm ID. Return 0 if the algorithm name is not known. */ int gcry_cipher_map_name (const char *name) _GCRY_GCC_ATTR_PURE; /* Given an ASN.1 object identifier in standard IETF dotted decimal format in STRING, return the encryption mode associated with that OID or 0 if not known or applicable. */ int gcry_cipher_mode_from_oid (const char *string) _GCRY_GCC_ATTR_PURE; /* Encrypt the plaintext of size INLEN in IN using the cipher handle H into the buffer OUT which has an allocated length of OUTSIZE. For most algorithms it is possible to pass NULL for in and 0 for INLEN and do a in-place decryption of the data provided in OUT. */ gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h, void *out, size_t outsize, const void *in, size_t inlen); /* The counterpart to gcry_cipher_encrypt. */ gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t h, void *out, size_t outsize, const void *in, size_t inlen); /* Set KEY of length KEYLEN bytes for the cipher handle HD. */ gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t hd, const void *key, size_t keylen); /* Set initialization vector IV of length IVLEN for the cipher handle HD. */ gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t hd, const void *iv, size_t ivlen); /* Provide additional authentication data for AEAD modes/ciphers. */ gcry_error_t gcry_cipher_authenticate (gcry_cipher_hd_t hd, const void *abuf, size_t abuflen); /* Get authentication tag for AEAD modes/ciphers. */ gcry_error_t gcry_cipher_gettag (gcry_cipher_hd_t hd, void *outtag, size_t taglen); /* Check authentication tag for AEAD modes/ciphers. */ gcry_error_t gcry_cipher_checktag (gcry_cipher_hd_t hd, const void *intag, size_t taglen); /* Reset the handle to the state after open. */ #define gcry_cipher_reset(h) gcry_cipher_ctl ((h), GCRYCTL_RESET, NULL, 0) /* Perform the OpenPGP sync operation if this is enabled for the cipher handle H. */ #define gcry_cipher_sync(h) gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, NULL, 0) /* Enable or disable CTS in future calls to gcry_encrypt(). CBC mode only. */ #define gcry_cipher_cts(h,on) gcry_cipher_ctl( (h), GCRYCTL_SET_CBC_CTS, \ NULL, on ) #define gcry_cipher_set_sbox(h,oid) gcry_cipher_ctl( (h), GCRYCTL_SET_SBOX, \ (void *) oid, 0); /* Indicate to the encrypt and decrypt functions that the next call provides the final data. Only used with some modes. */ #define gcry_cipher_final(a) \ gcry_cipher_ctl ((a), GCRYCTL_FINALIZE, NULL, 0) /* Set counter for CTR mode. (CTR,CTRLEN) must denote a buffer of block size length, or (NULL,0) to set the CTR to the all-zero block. */ gpg_error_t gcry_cipher_setctr (gcry_cipher_hd_t hd, const void *ctr, size_t ctrlen); /* Retrieve the key length in bytes used with algorithm A. */ size_t gcry_cipher_get_algo_keylen (int algo); /* Retrieve the block length in bytes used with algorithm A. */ size_t gcry_cipher_get_algo_blklen (int algo); /* Return 0 if the algorithm A is available for use. */ #define gcry_cipher_test_algo(a) \ gcry_cipher_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) /* Setup tag for decryption (for SIV and GCM-SIV mode). */ #define gcry_cipher_set_decryption_tag(a, tag, taglen) \ gcry_cipher_ctl ((a), GCRYCTL_SET_DECRYPTION_TAG, \ (void *)(tag), (taglen)) /************************************ * * * Asymmetric Cipher Functions * * * ************************************/ /* The algorithms and their IDs we support. */ enum gcry_pk_algos { GCRY_PK_RSA = 1, /* RSA */ GCRY_PK_RSA_E = 2, /* (deprecated: use 1). */ GCRY_PK_RSA_S = 3, /* (deprecated: use 1). */ GCRY_PK_ELG_E = 16, /* (deprecated: use 20). */ GCRY_PK_DSA = 17, /* Digital Signature Algorithm. */ GCRY_PK_ECC = 18, /* Generic ECC. */ GCRY_PK_ELG = 20, /* Elgamal */ GCRY_PK_ECDSA = 301, /* (only for external use). */ GCRY_PK_ECDH = 302, /* (only for external use). */ GCRY_PK_EDDSA = 303 /* (only for external use). */ }; /* Flags describing usage capabilities of a PK algorithm. */ #define GCRY_PK_USAGE_SIGN 1 /* Good for signatures. */ #define GCRY_PK_USAGE_ENCR 2 /* Good for encryption. */ #define GCRY_PK_USAGE_CERT 4 /* Good to certify other keys. */ #define GCRY_PK_USAGE_AUTH 8 /* Good for authentication. */ #define GCRY_PK_USAGE_UNKN 128 /* Unknown usage flag. */ /* Modes used with gcry_pubkey_get_sexp. */ #define GCRY_PK_GET_PUBKEY 1 #define GCRY_PK_GET_SECKEY 2 /* Encrypt the DATA using the public key PKEY and store the result as a newly created S-expression at RESULT. */ gcry_error_t gcry_pk_encrypt (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t pkey); /* Decrypt the DATA using the private key SKEY and store the result as a newly created S-expression at RESULT. */ gcry_error_t gcry_pk_decrypt (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t skey); /* Sign the DATA using the private key SKEY and store the result as a newly created S-expression at RESULT. */ gcry_error_t gcry_pk_sign (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t skey); /* Check the signature SIGVAL on DATA using the public key PKEY. */ gcry_error_t gcry_pk_verify (gcry_sexp_t sigval, gcry_sexp_t data, gcry_sexp_t pkey); /* Check that private KEY is sane. */ gcry_error_t gcry_pk_testkey (gcry_sexp_t key); /* Generate a new key pair according to the parameters given in S_PARMS. The new key pair is returned in as an S-expression in R_KEY. */ gcry_error_t gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms); /* Catch all function for miscellaneous operations. */ gcry_error_t gcry_pk_ctl (int cmd, void *buffer, size_t buflen); /* Retrieve information about the public key algorithm ALGO. */ gcry_error_t gcry_pk_algo_info (int algo, int what, void *buffer, size_t *nbytes); /* Map the public key algorithm whose ID is contained in ALGORITHM to a string representation of the algorithm name. For unknown algorithm IDs this functions returns "?". */ const char *gcry_pk_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE; /* Map the algorithm NAME to a public key algorithm Id. Return 0 if the algorithm name is not known. */ int gcry_pk_map_name (const char* name) _GCRY_GCC_ATTR_PURE; /* Return what is commonly referred as the key length for the given public or private KEY. */ unsigned int gcry_pk_get_nbits (gcry_sexp_t key) _GCRY_GCC_ATTR_PURE; /* Return the so called KEYGRIP which is the SHA-1 hash of the public key parameters expressed in a way depending on the algorithm. */ unsigned char *gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array); /* Return the name of the curve matching KEY. */ const char *gcry_pk_get_curve (gcry_sexp_t key, int iterator, unsigned int *r_nbits); /* Return an S-expression with the parameters of the named ECC curve NAME. ALGO must be set to an ECC algorithm. */ gcry_sexp_t gcry_pk_get_param (int algo, const char *name); /* Return 0 if the public key algorithm A is available for use. */ #define gcry_pk_test_algo(a) \ gcry_pk_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) /* Return an S-expression representing the context CTX. */ gcry_error_t gcry_pubkey_get_sexp (gcry_sexp_t *r_sexp, int mode, gcry_ctx_t ctx); /************************************ * * * Modern ECC Functions * * * ************************************/ /* The curves we support. */ enum gcry_ecc_curves { GCRY_ECC_CURVE25519 = 1, GCRY_ECC_CURVE448 = 2 }; /* Get the length of point to prepare buffer for the result. */ unsigned int gcry_ecc_get_algo_keylen (int curveid); /* Convenience function to compute scalar multiplication of the * Montgomery form of curve. */ gpg_error_t gcry_ecc_mul_point (int curveid, unsigned char *result, const unsigned char *scalar, const unsigned char *point); /************************************ * * * Cryptograhic Hash Functions * * * ************************************/ /* Algorithm IDs for the hash functions we know about. Not all of them are implemented. */ enum gcry_md_algos { GCRY_MD_NONE = 0, GCRY_MD_MD5 = 1, GCRY_MD_SHA1 = 2, GCRY_MD_RMD160 = 3, GCRY_MD_MD2 = 5, GCRY_MD_TIGER = 6, /* TIGER/192 as used by gpg <= 1.3.2. */ GCRY_MD_HAVAL = 7, /* HAVAL, 5 pass, 160 bit. */ GCRY_MD_SHA256 = 8, GCRY_MD_SHA384 = 9, GCRY_MD_SHA512 = 10, GCRY_MD_SHA224 = 11, GCRY_MD_MD4 = 301, GCRY_MD_CRC32 = 302, GCRY_MD_CRC32_RFC1510 = 303, GCRY_MD_CRC24_RFC2440 = 304, GCRY_MD_WHIRLPOOL = 305, GCRY_MD_TIGER1 = 306, /* TIGER fixed. */ GCRY_MD_TIGER2 = 307, /* TIGER2 variant. */ GCRY_MD_GOSTR3411_94 = 308, /* GOST R 34.11-94. */ GCRY_MD_STRIBOG256 = 309, /* GOST R 34.11-2012, 256 bit. */ GCRY_MD_STRIBOG512 = 310, /* GOST R 34.11-2012, 512 bit. */ GCRY_MD_GOSTR3411_CP = 311, /* GOST R 34.11-94 with CryptoPro-A S-Box. */ GCRY_MD_SHA3_224 = 312, GCRY_MD_SHA3_256 = 313, GCRY_MD_SHA3_384 = 314, GCRY_MD_SHA3_512 = 315, GCRY_MD_SHAKE128 = 316, GCRY_MD_SHAKE256 = 317, GCRY_MD_BLAKE2B_512 = 318, GCRY_MD_BLAKE2B_384 = 319, GCRY_MD_BLAKE2B_256 = 320, GCRY_MD_BLAKE2B_160 = 321, GCRY_MD_BLAKE2S_256 = 322, GCRY_MD_BLAKE2S_224 = 323, GCRY_MD_BLAKE2S_160 = 324, GCRY_MD_BLAKE2S_128 = 325, GCRY_MD_SM3 = 326, GCRY_MD_SHA512_256 = 327, GCRY_MD_SHA512_224 = 328 }; /* Flags used with the open function. */ enum gcry_md_flags { GCRY_MD_FLAG_SECURE = 1, /* Allocate all buffers in "secure" memory. */ GCRY_MD_FLAG_HMAC = 2, /* Make an HMAC out of this algorithm. */ GCRY_MD_FLAG_BUGEMU1 = 0x0100 }; /* (Forward declaration.) */ struct gcry_md_context; /* This object is used to hold a handle to a message digest object. This structure is private - only to be used by the public gcry_md_* macros. */ typedef struct gcry_md_handle { /* Actual context. */ struct gcry_md_context *ctx; /* Buffer management. */ int bufpos; int bufsize; unsigned char buf[1]; } *gcry_md_hd_t; /* Compatibility types, do not use them. */ #ifndef GCRYPT_NO_DEPRECATED typedef struct gcry_md_handle *GCRY_MD_HD _GCRY_GCC_ATTR_DEPRECATED; typedef struct gcry_md_handle *GcryMDHd _GCRY_GCC_ATTR_DEPRECATED; #endif /* Create a message digest object for algorithm ALGO. FLAGS may be given as an bitwise OR of the gcry_md_flags values. ALGO may be given as 0 if the algorithms to be used are later set using gcry_md_enable. */ gcry_error_t gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags); /* Release the message digest object HD. */ void gcry_md_close (gcry_md_hd_t hd); /* Add the message digest algorithm ALGO to the digest object HD. */ gcry_error_t gcry_md_enable (gcry_md_hd_t hd, int algo); /* Create a new digest object as an exact copy of the object HD. */ gcry_error_t gcry_md_copy (gcry_md_hd_t *bhd, gcry_md_hd_t ahd); /* Reset the digest object HD to its initial state. */ void gcry_md_reset (gcry_md_hd_t hd); /* Perform various operations on the digest object HD. */ gcry_error_t gcry_md_ctl (gcry_md_hd_t hd, int cmd, void *buffer, size_t buflen); /* Pass LENGTH bytes of data in BUFFER to the digest object HD so that it can update the digest values. This is the actual hash function. */ void gcry_md_write (gcry_md_hd_t hd, const void *buffer, size_t length); /* Read out the final digest from HD return the digest value for algorithm ALGO. */ unsigned char *gcry_md_read (gcry_md_hd_t hd, int algo); /* Read more output from algorithm ALGO to BUFFER of size LENGTH from * digest object HD. Algorithm needs to be 'expendable-output function'. */ gpg_error_t gcry_md_extract (gcry_md_hd_t hd, int algo, void *buffer, size_t length); /* Convenience function to calculate the hash from the data in BUFFER of size LENGTH using the algorithm ALGO avoiding the creation of a hash object. The hash is returned in the caller provided buffer DIGEST which must be large enough to hold the digest of the given algorithm. */ void gcry_md_hash_buffer (int algo, void *digest, const void *buffer, size_t length); /* Convenience function to hash multiple buffers. */ gpg_error_t gcry_md_hash_buffers (int algo, unsigned int flags, void *digest, const gcry_buffer_t *iov, int iovcnt); /* Retrieve the algorithm used with HD. This does not work reliable if more than one algorithm is enabled in HD. */ int gcry_md_get_algo (gcry_md_hd_t hd); /* Retrieve the length in bytes of the digest yielded by algorithm ALGO. */ unsigned int gcry_md_get_algo_dlen (int algo); /* Return true if the the algorithm ALGO is enabled in the digest object A. */ int gcry_md_is_enabled (gcry_md_hd_t a, int algo); /* Return true if the digest object A is allocated in "secure" memory. */ int gcry_md_is_secure (gcry_md_hd_t a); /* Deprecated: Use gcry_md_is_enabled or gcry_md_is_secure. */ gcry_error_t gcry_md_info (gcry_md_hd_t h, int what, void *buffer, size_t *nbytes) _GCRY_ATTR_INTERNAL; /* Retrieve various information about the algorithm ALGO. */ gcry_error_t gcry_md_algo_info (int algo, int what, void *buffer, size_t *nbytes); /* Map the digest algorithm id ALGO to a string representation of the algorithm name. For unknown algorithms this function returns "?". */ const char *gcry_md_algo_name (int algo) _GCRY_GCC_ATTR_PURE; /* Map the algorithm NAME to a digest algorithm Id. Return 0 if the algorithm name is not known. */ int gcry_md_map_name (const char* name) _GCRY_GCC_ATTR_PURE; /* For use with the HMAC feature, the set MAC key to the KEY of KEYLEN bytes. */ gcry_error_t gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen); /* Start or stop debugging for digest handle HD; i.e. create a file named dbgmd-. while hashing. If SUFFIX is NULL, debugging stops and the file will be closed. */ void gcry_md_debug (gcry_md_hd_t hd, const char *suffix); /* Update the hash(s) of H with the character C. This is a buffered version of the gcry_md_write function. */ #define gcry_md_putc(h,c) \ do { \ gcry_md_hd_t h__ = (h); \ if( (h__)->bufpos == (h__)->bufsize ) \ gcry_md_write( (h__), NULL, 0 ); \ (h__)->buf[(h__)->bufpos++] = (c) & 0xff; \ } while(0) /* Finalize the digest calculation. This is not really needed because gcry_md_read() does this implicitly. */ #define gcry_md_final(a) \ gcry_md_ctl ((a), GCRYCTL_FINALIZE, NULL, 0) /* Return 0 if the algorithm A is available for use. */ #define gcry_md_test_algo(a) \ gcry_md_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) /* Return an DER encoded ASN.1 OID for the algorithm A in buffer B. N must point to size_t variable with the available size of buffer B. After return it will receive the actual size of the returned OID. */ #define gcry_md_get_asnoid(a,b,n) \ gcry_md_algo_info((a), GCRYCTL_GET_ASNOID, (b), (n)) /********************************************** * * * Message Authentication Code Functions * * * **********************************************/ /* The data object used to hold a handle to an encryption object. */ struct gcry_mac_handle; typedef struct gcry_mac_handle *gcry_mac_hd_t; /* Algorithm IDs for the hash functions we know about. Not all of them are implemented. */ enum gcry_mac_algos { GCRY_MAC_NONE = 0, GCRY_MAC_GOST28147_IMIT = 1, GCRY_MAC_HMAC_SHA256 = 101, GCRY_MAC_HMAC_SHA224 = 102, GCRY_MAC_HMAC_SHA512 = 103, GCRY_MAC_HMAC_SHA384 = 104, GCRY_MAC_HMAC_SHA1 = 105, GCRY_MAC_HMAC_MD5 = 106, GCRY_MAC_HMAC_MD4 = 107, GCRY_MAC_HMAC_RMD160 = 108, GCRY_MAC_HMAC_TIGER1 = 109, /* The fixed TIGER variant */ GCRY_MAC_HMAC_WHIRLPOOL = 110, GCRY_MAC_HMAC_GOSTR3411_94 = 111, GCRY_MAC_HMAC_STRIBOG256 = 112, GCRY_MAC_HMAC_STRIBOG512 = 113, GCRY_MAC_HMAC_MD2 = 114, GCRY_MAC_HMAC_SHA3_224 = 115, GCRY_MAC_HMAC_SHA3_256 = 116, GCRY_MAC_HMAC_SHA3_384 = 117, GCRY_MAC_HMAC_SHA3_512 = 118, GCRY_MAC_HMAC_GOSTR3411_CP = 119, GCRY_MAC_HMAC_BLAKE2B_512 = 120, GCRY_MAC_HMAC_BLAKE2B_384 = 121, GCRY_MAC_HMAC_BLAKE2B_256 = 122, GCRY_MAC_HMAC_BLAKE2B_160 = 123, GCRY_MAC_HMAC_BLAKE2S_256 = 124, GCRY_MAC_HMAC_BLAKE2S_224 = 125, GCRY_MAC_HMAC_BLAKE2S_160 = 126, GCRY_MAC_HMAC_BLAKE2S_128 = 127, GCRY_MAC_HMAC_SM3 = 128, GCRY_MAC_HMAC_SHA512_256 = 129, GCRY_MAC_HMAC_SHA512_224 = 130, GCRY_MAC_CMAC_AES = 201, GCRY_MAC_CMAC_3DES = 202, GCRY_MAC_CMAC_CAMELLIA = 203, GCRY_MAC_CMAC_CAST5 = 204, GCRY_MAC_CMAC_BLOWFISH = 205, GCRY_MAC_CMAC_TWOFISH = 206, GCRY_MAC_CMAC_SERPENT = 207, GCRY_MAC_CMAC_SEED = 208, GCRY_MAC_CMAC_RFC2268 = 209, GCRY_MAC_CMAC_IDEA = 210, GCRY_MAC_CMAC_GOST28147 = 211, GCRY_MAC_CMAC_SM4 = 212, GCRY_MAC_GMAC_AES = 401, GCRY_MAC_GMAC_CAMELLIA = 402, GCRY_MAC_GMAC_TWOFISH = 403, GCRY_MAC_GMAC_SERPENT = 404, GCRY_MAC_GMAC_SEED = 405, GCRY_MAC_POLY1305 = 501, GCRY_MAC_POLY1305_AES = 502, GCRY_MAC_POLY1305_CAMELLIA = 503, GCRY_MAC_POLY1305_TWOFISH = 504, GCRY_MAC_POLY1305_SERPENT = 505, GCRY_MAC_POLY1305_SEED = 506 }; /* Flags used with the open function. */ enum gcry_mac_flags { GCRY_MAC_FLAG_SECURE = 1 /* Allocate all buffers in "secure" memory. */ }; /* Create a MAC handle for algorithm ALGO. FLAGS may be given as an bitwise OR of the gcry_mac_flags values. CTX maybe NULL or gcry_ctx_t object to be associated with HANDLE. */ gcry_error_t gcry_mac_open (gcry_mac_hd_t *handle, int algo, unsigned int flags, gcry_ctx_t ctx); /* Close the MAC handle H and release all resource. */ void gcry_mac_close (gcry_mac_hd_t h); /* Perform various operations on the MAC object H. */ gcry_error_t gcry_mac_ctl (gcry_mac_hd_t h, int cmd, void *buffer, size_t buflen); /* Retrieve various information about the MAC algorithm ALGO. */ gcry_error_t gcry_mac_algo_info (int algo, int what, void *buffer, size_t *nbytes); /* Set KEY of length KEYLEN bytes for the MAC handle HD. */ gcry_error_t gcry_mac_setkey (gcry_mac_hd_t hd, const void *key, size_t keylen); /* Set initialization vector IV of length IVLEN for the MAC handle HD. */ gcry_error_t gcry_mac_setiv (gcry_mac_hd_t hd, const void *iv, size_t ivlen); /* Pass LENGTH bytes of data in BUFFER to the MAC object HD so that it can update the MAC values. */ gcry_error_t gcry_mac_write (gcry_mac_hd_t hd, const void *buffer, size_t length); /* Read out the final authentication code from the MAC object HD to BUFFER. */ gcry_error_t gcry_mac_read (gcry_mac_hd_t hd, void *buffer, size_t *buflen); /* Verify the final authentication code from the MAC object HD with BUFFER. */ gcry_error_t gcry_mac_verify (gcry_mac_hd_t hd, const void *buffer, size_t buflen); /* Retrieve the algorithm used with MAC. */ int gcry_mac_get_algo (gcry_mac_hd_t hd); /* Retrieve the length in bytes of the MAC yielded by algorithm ALGO. */ unsigned int gcry_mac_get_algo_maclen (int algo); /* Retrieve the default key length in bytes used with algorithm A. */ unsigned int gcry_mac_get_algo_keylen (int algo); /* Map the MAC algorithm whose ID is contained in ALGORITHM to a string representation of the algorithm name. For unknown algorithm IDs this function returns "?". */ const char *gcry_mac_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE; /* Map the algorithm name NAME to an MAC algorithm ID. Return 0 if the algorithm name is not known. */ int gcry_mac_map_name (const char *name) _GCRY_GCC_ATTR_PURE; /* Reset the handle to the state after open/setkey. */ #define gcry_mac_reset(h) gcry_mac_ctl ((h), GCRYCTL_RESET, NULL, 0) /* Return 0 if the algorithm A is available for use. */ #define gcry_mac_test_algo(a) \ gcry_mac_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) /****************************** * * * Key Derivation Functions * * * ******************************/ /* Algorithm IDs for the KDFs. */ enum gcry_kdf_algos { GCRY_KDF_NONE = 0, GCRY_KDF_SIMPLE_S2K = 16, GCRY_KDF_SALTED_S2K = 17, GCRY_KDF_ITERSALTED_S2K = 19, GCRY_KDF_PBKDF1 = 33, GCRY_KDF_PBKDF2 = 34, GCRY_KDF_SCRYPT = 48, GCRY_KDF_ARGON2 = 64, GCRY_KDF_BALLOON = 65 }; enum gcry_kdf_subalgo_argon2 { GCRY_KDF_ARGON2D = 0, GCRY_KDF_ARGON2I = 1, GCRY_KDF_ARGON2ID = 2 }; /* Derive a key from a passphrase. */ gpg_error_t gcry_kdf_derive (const void *passphrase, size_t passphraselen, int algo, int subalgo, const void *salt, size_t saltlen, unsigned long iterations, size_t keysize, void *keybuffer); /* Another API to derive a key from a passphrase. */ typedef struct gcry_kdf_handle *gcry_kdf_hd_t; -/* Head of per thread data. */ -struct gcry_kdf_pt_head { - gcry_kdf_hd_t h; - union { - void *user_data; - gpg_err_code_t ec; - } u; + +typedef int (*gcry_kdf_lauch_job_t) (void *jobs_context, + void (*job) (void *work_priv), + void *work_priv); +typedef int (*gcry_kdf_wait_all_jobs_completion_t) (void *jobs_context); + +/* Exposed structure for KDF computation to decouple thread functionality. */ +struct gcry_kdf_thread_ops { + void *jobs_context; + gcry_kdf_lauch_job_t launch_job; + gcry_kdf_wait_all_jobs_completion_t wait_all_jobs_completion; }; gcry_error_t gcry_kdf_open (gcry_kdf_hd_t *hd, int algo, int subalgo, const unsigned long *param, unsigned int paramlen, const void *passphrase, size_t passphraselen, const void *salt, size_t saltlen, const void *key, size_t keylen, const void *ad, size_t adlen); -gcry_error_t gcry_kdf_ctl (gcry_kdf_hd_t h, int cmd, void *buffer, - size_t buflen); -gcry_error_t gcry_kdf_iterator (gcry_kdf_hd_t h, int *action_p, - struct gcry_kdf_pt_head **t_p); -gcry_error_t gcry_kdf_compute_segment (gcry_kdf_hd_t h, - const struct gcry_kdf_pt_head *t); +gcry_error_t gcry_kdf_compute (gcry_kdf_hd_t h, + const struct gcry_kdf_thread_ops *ops); gcry_error_t gcry_kdf_final (gcry_kdf_hd_t h, size_t resultlen, void *result); void gcry_kdf_close (gcry_kdf_hd_t h); /************************************ * * * Random Generating Functions * * * ************************************/ /* The type of the random number generator. */ enum gcry_rng_types { GCRY_RNG_TYPE_STANDARD = 1, /* The default CSPRNG generator. */ GCRY_RNG_TYPE_FIPS = 2, /* The FIPS X9.31 AES generator. */ GCRY_RNG_TYPE_SYSTEM = 3 /* The system's native generator. */ }; /* The possible values for the random quality. The rule of thumb is to use STRONG for session keys and VERY_STRONG for key material. WEAK is usually an alias for STRONG and should not be used anymore (except with gcry_mpi_randomize); use gcry_create_nonce instead. */ typedef enum gcry_random_level { GCRY_WEAK_RANDOM = 0, GCRY_STRONG_RANDOM = 1, GCRY_VERY_STRONG_RANDOM = 2 } gcry_random_level_t; /* Fill BUFFER with LENGTH bytes of random, using random numbers of quality LEVEL. */ void gcry_randomize (void *buffer, size_t length, enum gcry_random_level level); /* Add the external random from BUFFER with LENGTH bytes into the pool. QUALITY should either be -1 for unknown or in the range of 0 to 100 */ gcry_error_t gcry_random_add_bytes (const void *buffer, size_t length, int quality); /* If random numbers are used in an application, this macro should be called from time to time so that new stuff gets added to the internal pool of the RNG. */ #define gcry_fast_random_poll() gcry_control (GCRYCTL_FAST_POLL, NULL) /* Return NBYTES of allocated random using a random numbers of quality LEVEL. */ void *gcry_random_bytes (size_t nbytes, enum gcry_random_level level) _GCRY_GCC_ATTR_MALLOC; /* Return NBYTES of allocated random using a random numbers of quality LEVEL. The random is returned in "secure" memory. */ void *gcry_random_bytes_secure (size_t nbytes, enum gcry_random_level level) _GCRY_GCC_ATTR_MALLOC; /* Set the big integer W to a random value of NBITS using a random generator with quality LEVEL. Note that by using a level of GCRY_WEAK_RANDOM gcry_create_nonce is used internally. */ void gcry_mpi_randomize (gcry_mpi_t w, unsigned int nbits, enum gcry_random_level level); /* Create an unpredicable nonce of LENGTH bytes in BUFFER. */ void gcry_create_nonce (void *buffer, size_t length); /*******************************/ /* */ /* Prime Number Functions */ /* */ /*******************************/ /* Mode values passed to a gcry_prime_check_func_t. */ #define GCRY_PRIME_CHECK_AT_FINISH 0 #define GCRY_PRIME_CHECK_AT_GOT_PRIME 1 #define GCRY_PRIME_CHECK_AT_MAYBE_PRIME 2 /* The function should return 1 if the operation shall continue, 0 to reject the prime candidate. */ typedef int (*gcry_prime_check_func_t) (void *arg, int mode, gcry_mpi_t candidate); /* Flags for gcry_prime_generate(): */ /* Allocate prime numbers and factors in secure memory. */ #define GCRY_PRIME_FLAG_SECRET (1 << 0) /* Make sure that at least one prime factor is of size `FACTOR_BITS'. */ #define GCRY_PRIME_FLAG_SPECIAL_FACTOR (1 << 1) /* Generate a new prime number of PRIME_BITS bits and store it in PRIME. If FACTOR_BITS is non-zero, one of the prime factors of (prime - 1) / 2 must be FACTOR_BITS bits long. If FACTORS is non-zero, allocate a new, NULL-terminated array holding the prime factors and store it in FACTORS. FLAGS might be used to influence the prime number generation process. */ gcry_error_t gcry_prime_generate (gcry_mpi_t *prime, unsigned int prime_bits, unsigned int factor_bits, gcry_mpi_t **factors, gcry_prime_check_func_t cb_func, void *cb_arg, gcry_random_level_t random_level, unsigned int flags); /* Find a generator for PRIME where the factorization of (prime-1) is in the NULL terminated array FACTORS. Return the generator as a newly allocated MPI in R_G. If START_G is not NULL, use this as the start for the search. */ gcry_error_t gcry_prime_group_generator (gcry_mpi_t *r_g, gcry_mpi_t prime, gcry_mpi_t *factors, gcry_mpi_t start_g); /* Convenience function to release the FACTORS array. */ void gcry_prime_release_factors (gcry_mpi_t *factors); /* Check whether the number X is prime. */ gcry_error_t gcry_prime_check (gcry_mpi_t x, unsigned int flags); /************************************ * * * Miscellaneous Stuff * * * ************************************/ /* Release the context object CTX. */ void gcry_ctx_release (gcry_ctx_t ctx); /* Log data using Libgcrypt's own log interface. */ void gcry_log_debug (const char *fmt, ...) _GCRY_GCC_ATTR_PRINTF(1,2); void gcry_log_debughex (const char *text, const void *buffer, size_t length); void gcry_log_debugmpi (const char *text, gcry_mpi_t mpi); void gcry_log_debugpnt (const char *text, gcry_mpi_point_t point, gcry_ctx_t ctx); void gcry_log_debugsxp (const char *text, gcry_sexp_t sexp); char *gcry_get_config (int mode, const char *what); /* Log levels used by the internal logging facility. */ enum gcry_log_levels { GCRY_LOG_CONT = 0, /* (Continue the last log line.) */ GCRY_LOG_INFO = 10, GCRY_LOG_WARN = 20, GCRY_LOG_ERROR = 30, GCRY_LOG_FATAL = 40, GCRY_LOG_BUG = 50, GCRY_LOG_DEBUG = 100 }; /* Type for progress handlers. */ typedef void (*gcry_handler_progress_t) (void *, const char *, int, int, int); /* Type for memory allocation handlers. */ typedef void *(*gcry_handler_alloc_t) (size_t n); /* Type for secure memory check handlers. */ typedef int (*gcry_handler_secure_check_t) (const void *); /* Type for memory reallocation handlers. */ typedef void *(*gcry_handler_realloc_t) (void *p, size_t n); /* Type for memory free handlers. */ typedef void (*gcry_handler_free_t) (void *); /* Type for out-of-memory handlers. */ typedef int (*gcry_handler_no_mem_t) (void *, size_t, unsigned int); /* Type for fatal error handlers. */ typedef void (*gcry_handler_error_t) (void *, int, const char *); /* Type for logging handlers. */ typedef void (*gcry_handler_log_t) (void *, int, const char *, va_list); /* Certain operations can provide progress information. This function is used to register a handler for retrieving these information. */ void gcry_set_progress_handler (gcry_handler_progress_t cb, void *cb_data); /* Register a custom memory allocation functions. */ void gcry_set_allocation_handler ( gcry_handler_alloc_t func_alloc, gcry_handler_alloc_t func_alloc_secure, gcry_handler_secure_check_t func_secure_check, gcry_handler_realloc_t func_realloc, gcry_handler_free_t func_free); /* Register a function used instead of the internal out of memory handler. */ void gcry_set_outofcore_handler (gcry_handler_no_mem_t h, void *opaque); /* Register a function used instead of the internal fatal error handler. */ void gcry_set_fatalerror_handler (gcry_handler_error_t fnc, void *opaque); /* Register a function used instead of the internal logging facility. */ void gcry_set_log_handler (gcry_handler_log_t f, void *opaque); /* Reserved for future use. */ void gcry_set_gettext_handler (const char *(*f)(const char*)); /* Libgcrypt uses its own memory allocation. It is important to use gcry_free () to release memory allocated by libgcrypt. */ void *gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC; void *gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; void *gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *gcry_realloc (void *a, size_t n); char *gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC; void *gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC; void *gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; void *gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *gcry_xrealloc (void *a, size_t n); char *gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC; void gcry_free (void *a); /* Return true if A is allocated in "secure" memory. */ int gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE; /* Return true if Libgcrypt is in FIPS mode. */ #define gcry_fips_mode_active() !!gcry_control (GCRYCTL_FIPS_MODE_P, 0) /* Variant of gcry_pk_sign which takes as additional parameter a HD * handle for hash and an optional context. The hash algorithm used by the * handle needs to be enabled and input needs to be supplied beforehand. * DATA-TMPL specifies a template to compose an S-expression to be signed. * A template should include '(hash %s %b)' or '(hash ALGONAME %b)'. * For the former case, '%s' is substituted by the string of algorithm * of gcry_md_get_algo (HD) and when gcry_md_read is called, ALGO=0 is * used internally. For the latter case, hash algorithm by ALGONAME * is used when gcry_md_read is called internally. * The hash handle must not yet been finalized; the function * takes a copy of the state and does a finalize on the copy. This * function shall be used if a policy requires that hashing and signing * is done by the same function. CTX is currently not used and should * be passed as NULL. */ gcry_error_t gcry_pk_hash_sign (gcry_sexp_t *result, const char *data_tmpl, gcry_sexp_t skey, gcry_md_hd_t hd, gcry_ctx_t ctx); /* Variant of gcry_pk_verify which takes as additional parameter a HD * handle for hash and an optional context. Similar to gcry_pk_hash_sign. */ gcry_error_t gcry_pk_hash_verify (gcry_sexp_t sigval, const char *data_tmpl, gcry_sexp_t pkey, gcry_md_hd_t hd, gcry_ctx_t ctx); gcry_error_t gcry_pk_random_override_new (gcry_ctx_t *r_ctx, const unsigned char *p, size_t len); #if 0 /* (Keep Emacsens' auto-indent happy.) */ { #endif #ifdef __cplusplus } #endif #endif /* _GCRYPT_H */ /* @emacs_local_vars_begin@ @emacs_local_vars_read_only@ @emacs_local_vars_end@ */ diff --git a/src/libgcrypt.def b/src/libgcrypt.def index 177ef897..d6de731f 100644 --- a/src/libgcrypt.def +++ b/src/libgcrypt.def @@ -1,303 +1,301 @@ ;; libgcrypt.defs - Exported symbols for W32 ;; Copyright (C) 2003, 2007 Free Software Foundation, Inc. ;; ;; This file is part of Libgcrypt. ;; ;; Libgcrypt 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. ;; ;; Libgcrypt 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, write to the Free Software ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA ;; ;; Note: This file should be updated manually and the ordinals shall ;; never be changed. Also check libgcrypt.vers and visibility.h. EXPORTS gcry_check_version @1 gcry_control @2 gcry_malloc @3 gcry_calloc @4 gcry_malloc_secure @5 gcry_calloc_secure @6 gcry_realloc @7 gcry_strdup @8 gcry_xmalloc @9 gcry_xcalloc @10 gcry_xmalloc_secure @11 gcry_xcalloc_secure @12 gcry_xrealloc @13 gcry_xstrdup @14 gcry_is_secure @15 gcry_free @16 gcry_set_progress_handler @17 gcry_set_allocation_handler @18 gcry_set_outofcore_handler @19 gcry_set_fatalerror_handler @20 gcry_set_log_handler @21 gcry_set_gettext_handler @22 gcry_strerror @23 gcry_strsource @24 gcry_err_code_from_errno @25 gcry_err_code_to_errno @26 gcry_err_make_from_errno @27 gcry_error_from_errno @28 gcry_sexp_new @29 gcry_sexp_create @30 gcry_sexp_sscan @31 gcry_sexp_build @32 gcry_sexp_build_array @33 gcry_sexp_release @34 gcry_sexp_canon_len @35 gcry_sexp_sprint @36 gcry_sexp_dump @37 gcry_sexp_cons @38 gcry_sexp_alist @39 gcry_sexp_vlist @40 gcry_sexp_append @41 gcry_sexp_prepend @42 gcry_sexp_find_token @43 gcry_sexp_length @44 gcry_sexp_nth @45 gcry_sexp_car @46 gcry_sexp_cdr @47 gcry_sexp_cadr @48 gcry_sexp_nth_data @49 gcry_sexp_nth_mpi @50 gcry_mpi_new @51 gcry_mpi_snew @52 gcry_mpi_release @53 gcry_mpi_copy @54 gcry_mpi_set @55 gcry_mpi_set_ui @56 gcry_mpi_swap @57 gcry_mpi_cmp @58 gcry_mpi_cmp_ui @59 gcry_mpi_scan @60 gcry_mpi_print @61 gcry_mpi_aprint @62 gcry_mpi_dump @63 gcry_mpi_add @64 gcry_mpi_add_ui @65 gcry_mpi_addm @66 gcry_mpi_sub @67 gcry_mpi_sub_ui @68 gcry_mpi_subm @69 gcry_mpi_mul @70 gcry_mpi_mul_ui @71 gcry_mpi_mulm @72 gcry_mpi_mul_2exp @73 gcry_mpi_div @74 gcry_mpi_mod @75 gcry_mpi_powm @76 gcry_mpi_gcd @77 gcry_mpi_invm @78 gcry_mpi_get_nbits @79 gcry_mpi_test_bit @80 gcry_mpi_set_bit @81 gcry_mpi_clear_bit @82 gcry_mpi_set_highbit @83 gcry_mpi_clear_highbit @84 gcry_mpi_rshift @85 gcry_mpi_set_opaque @86 gcry_mpi_get_opaque @87 gcry_mpi_set_flag @88 gcry_mpi_clear_flag @89 gcry_mpi_get_flag @90 gcry_mpi_get_ui @91 gcry_cipher_open @92 gcry_cipher_close @93 gcry_cipher_ctl @94 gcry_cipher_info @95 gcry_cipher_algo_info @96 gcry_cipher_algo_name @97 gcry_cipher_map_name @98 gcry_cipher_mode_from_oid @99 gcry_cipher_encrypt @100 gcry_cipher_decrypt @101 gcry_cipher_get_algo_keylen @102 gcry_cipher_get_algo_blklen @103 ;; @104 used to be part of the module register interface gcry_pk_encrypt @105 gcry_pk_decrypt @106 gcry_pk_sign @107 gcry_pk_verify @108 gcry_pk_testkey @109 gcry_pk_genkey @110 gcry_pk_ctl @111 gcry_pk_algo_info @112 gcry_pk_algo_name @113 gcry_pk_map_name @114 gcry_pk_get_nbits @115 gcry_pk_get_keygrip @116 ;; @117 used to be part of the module register interface ;; ;; 118 to 142 were used in previous Libgcrypt versions for the gcry_ac ;; interface ;; gcry_md_open @143 gcry_md_close @144 gcry_md_enable @145 gcry_md_copy @146 gcry_md_reset @147 gcry_md_ctl @148 gcry_md_write @149 gcry_md_read @150 gcry_md_hash_buffer @151 gcry_md_get_algo @152 gcry_md_get_algo_dlen @153 gcry_md_is_enabled @154 gcry_md_is_secure @155 gcry_md_info @156 gcry_md_algo_info @157 gcry_md_algo_name @158 gcry_md_map_name @159 gcry_md_setkey @160 ;; @161 used to be part of the module register interface gcry_randomize @162 gcry_random_add_bytes @163 gcry_random_bytes @164 gcry_random_bytes_secure @165 gcry_mpi_randomize @166 gcry_prime_generate @167 gcry_prime_group_generator @168 gcry_prime_release_factors @169 gcry_prime_check @170 gcry_create_nonce @171 gcry_md_debug @172 ;; @173 used to be part of the module register interface ;; @174 used to be part of the module register interface ;; @175 used to be part of the module register interface ;; @176 used to be part of the module register interface ;; @177 used to be part of the module register interface ;; @178 used to be part of the module register interface ;; ;; @179 to @186 used to be part of the removed gcry_ac interface ;; gcry_sexp_nth_string @187 gcry_cipher_setkey @188 gcry_cipher_setiv @189 gcry_cipher_setctr @190 gcry_mpi_lshift @191 gcry_pk_get_curve @192 gcry_pk_get_param @193 gcry_kdf_derive @194 gcry_mpi_snatch @195 gcry_mpi_point_new @196 gcry_mpi_point_release @197 gcry_mpi_point_get @198 gcry_mpi_point_snatch_get @199 gcry_mpi_point_set @200 gcry_mpi_point_snatch_set @201 gcry_ctx_release @202 gcry_mpi_ec_new @203 gcry_mpi_ec_get_mpi @204 gcry_mpi_ec_get_point @205 gcry_mpi_ec_set_mpi @206 gcry_mpi_ec_set_point @207 gcry_mpi_ec_get_affine @208 gcry_mpi_ec_dup @209 gcry_mpi_ec_add @210 gcry_mpi_ec_mul @211 gcry_pubkey_get_sexp @212 _gcry_mpi_get_const @213 gcry_sexp_nth_buffer @214 gcry_mpi_is_neg @215 gcry_mpi_neg @216 gcry_mpi_abs @217 gcry_mpi_ec_curve_point @218 gcry_md_hash_buffers @219 gcry_log_debug @220 gcry_log_debughex @221 gcry_log_debugmpi @222 gcry_log_debugpnt @223 gcry_log_debugsxp @224 gcry_sexp_extract_param @225 gcry_cipher_authenticate @226 gcry_cipher_gettag @227 gcry_cipher_checktag @228 gcry_mpi_set_opaque_copy @229 gcry_mac_algo_info @230 gcry_mac_algo_name @231 gcry_mac_map_name @232 gcry_mac_get_algo_maclen @233 gcry_mac_get_algo_keylen @234 gcry_mac_open @235 gcry_mac_close @236 gcry_mac_setkey @237 gcry_mac_setiv @238 gcry_mac_write @239 gcry_mac_read @240 gcry_mac_verify @241 gcry_mac_ctl @242 gcry_mac_get_algo @243 gcry_mpi_ec_sub @244 gcry_md_extract @245 gcry_mpi_ec_decode_point @246 gcry_get_config @247 gcry_mpi_point_copy @248 gcry_ecc_get_algo_keylen @249 gcry_ecc_mul_point @250 gcry_pk_hash_sign @255 gcry_pk_hash_verify @256 gcry_pk_random_override_new @257 gcry_kdf_open @258 - gcry_kdf_ctl @259 - gcry_kdf_iterator @260 - gcry_kdf_compute_segment @261 - gcry_kdf_final @262 - gcry_kdf_close @263 + gcry_kdf_compute @259 + gcry_kdf_final @260 + gcry_kdf_close @261 ;; end of file with public symbols for Windows. diff --git a/src/libgcrypt.vers b/src/libgcrypt.vers index 9bf4148e..2e274f60 100644 --- a/src/libgcrypt.vers +++ b/src/libgcrypt.vers @@ -1,132 +1,131 @@ # libgcrypt.vers - What symbols to export -*- std -*- # Copyright (C) 2002, 2004, 2008, 2011 Free Software Foundation, Inc. # # This file is part of Libgcrypt. # # Libgcrypt 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. # # Libgcrypt 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, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA # NOTE: When adding new functions, please make sure to add them to # visibility.h and libgcrypt.def as well. GCRYPT_1.6 { global: gcry_check_version; gcry_control; gcry_set_allocation_handler; gcry_set_fatalerror_handler; gcry_set_gettext_handler; gcry_set_log_handler; gcry_set_outofcore_handler; gcry_set_progress_handler; gcry_err_code_from_errno; gcry_err_code_to_errno; gcry_err_make_from_errno; gcry_error_from_errno; gcry_strerror; gcry_strsource; gcry_free; gcry_malloc; gcry_malloc_secure; gcry_calloc; gcry_calloc_secure; gcry_realloc; gcry_strdup; gcry_is_secure; gcry_xcalloc; gcry_xcalloc_secure; gcry_xmalloc; gcry_xmalloc_secure; gcry_xrealloc; gcry_xstrdup; gcry_md_algo_info; gcry_md_algo_name; gcry_md_close; gcry_md_copy; gcry_md_ctl; gcry_md_enable; gcry_md_get; gcry_md_get_algo; gcry_md_get_algo_dlen; gcry_md_hash_buffer; gcry_md_hash_buffers; gcry_md_info; gcry_md_is_enabled; gcry_md_is_secure; gcry_md_map_name; gcry_md_open; gcry_md_read; gcry_md_extract; gcry_md_reset; gcry_md_setkey; gcry_md_write; gcry_md_debug; gcry_cipher_algo_info; gcry_cipher_algo_name; gcry_cipher_close; gcry_cipher_ctl; gcry_cipher_decrypt; gcry_cipher_encrypt; gcry_cipher_get_algo_blklen; gcry_cipher_get_algo_keylen; gcry_cipher_info; gcry_cipher_map_name; gcry_cipher_mode_from_oid; gcry_cipher_open; gcry_cipher_setkey; gcry_cipher_setiv; gcry_cipher_setctr; gcry_cipher_authenticate; gcry_cipher_gettag; gcry_cipher_checktag; gcry_mac_algo_info; gcry_mac_algo_name; gcry_mac_map_name; gcry_mac_get_algo_maclen; gcry_mac_get_algo_keylen; gcry_mac_get_algo; gcry_mac_open; gcry_mac_close; gcry_mac_setkey; gcry_mac_setiv; gcry_mac_write; gcry_mac_read; gcry_mac_verify; gcry_mac_ctl; gcry_pk_algo_info; gcry_pk_algo_name; gcry_pk_ctl; gcry_pk_decrypt; gcry_pk_encrypt; gcry_pk_genkey; gcry_pk_get_keygrip; gcry_pk_get_nbits; gcry_pk_map_name; gcry_pk_register; gcry_pk_sign; gcry_pk_testkey; gcry_pk_verify; gcry_pk_get_curve; gcry_pk_get_param; gcry_pubkey_get_sexp; gcry_ecc_get_algo_keylen; gcry_ecc_mul_point; gcry_kdf_derive; gcry_prime_check; gcry_prime_generate; gcry_prime_group_generator; gcry_prime_release_factors; gcry_random_add_bytes; gcry_random_bytes; gcry_random_bytes_secure; gcry_randomize; gcry_create_nonce; gcry_sexp_alist; gcry_sexp_append; gcry_sexp_build; gcry_sexp_build_array; gcry_sexp_cadr; gcry_sexp_canon_len; gcry_sexp_car; gcry_sexp_cdr; gcry_sexp_cons; gcry_sexp_create; gcry_sexp_dump; gcry_sexp_find_token; gcry_sexp_length; gcry_sexp_new; gcry_sexp_nth; gcry_sexp_nth_buffer; gcry_sexp_nth_data; gcry_sexp_nth_mpi; gcry_sexp_prepend; gcry_sexp_release; gcry_sexp_sprint; gcry_sexp_sscan; gcry_sexp_vlist; gcry_sexp_nth_string; gcry_sexp_extract_param; gcry_mpi_is_neg; gcry_mpi_neg; gcry_mpi_abs; gcry_mpi_add; gcry_mpi_add_ui; gcry_mpi_addm; gcry_mpi_aprint; gcry_mpi_clear_bit; gcry_mpi_clear_flag; gcry_mpi_clear_highbit; gcry_mpi_cmp; gcry_mpi_cmp_ui; gcry_mpi_copy; gcry_mpi_div; gcry_mpi_dump; gcry_mpi_gcd; gcry_mpi_get_flag; gcry_mpi_get_nbits; gcry_mpi_get_opaque; gcry_mpi_invm; gcry_mpi_mod; gcry_mpi_mul; gcry_mpi_mul_2exp; gcry_mpi_mul_ui; gcry_mpi_mulm; gcry_mpi_new; gcry_mpi_powm; gcry_mpi_print; gcry_mpi_randomize; gcry_mpi_release; gcry_mpi_rshift; gcry_mpi_scan; gcry_mpi_set; gcry_mpi_set_bit; gcry_mpi_set_flag; gcry_mpi_set_highbit; gcry_mpi_set_opaque; gcry_mpi_set_opaque_copy; gcry_mpi_set_ui; gcry_mpi_snew; gcry_mpi_sub; gcry_mpi_sub_ui; gcry_mpi_subm; gcry_mpi_swap; gcry_mpi_test_bit; gcry_mpi_lshift; gcry_mpi_snatch; gcry_mpi_point_new; gcry_mpi_point_release; gcry_mpi_point_get; gcry_mpi_point_snatch_get; gcry_mpi_point_set; gcry_mpi_point_snatch_set; gcry_mpi_ec_new; gcry_mpi_ec_get_mpi; gcry_mpi_ec_get_point; gcry_mpi_ec_set_mpi; gcry_mpi_ec_set_point; gcry_mpi_ec_get_affine; gcry_mpi_ec_dup; gcry_mpi_ec_add; gcry_mpi_ec_sub; gcry_mpi_ec_mul; gcry_mpi_ec_curve_point; gcry_mpi_ec_decode_point; gcry_mpi_point_copy; gcry_mpi_get_ui; gcry_log_debug; gcry_log_debughex; gcry_log_debugmpi; gcry_log_debugpnt; gcry_log_debugsxp; gcry_get_config; _gcry_mpi_get_const; gcry_ctx_release; gcry_pk_hash_sign; gcry_pk_hash_verify; gcry_pk_random_override_new; - gcry_kdf_open; gcry_kdf_ctl; gcry_kdf_iterator; - gcry_kdf_compute_segment; gcry_kdf_final; gcry_kdf_close; + gcry_kdf_open; gcry_kdf_compute; gcry_kdf_final; gcry_kdf_close; local: *; }; diff --git a/src/visibility.c b/src/visibility.c index bcae630c..c98247d8 100644 --- a/src/visibility.c +++ b/src/visibility.c @@ -1,1699 +1,1681 @@ /* visibility.c - Wrapper for all public functions. * Copyright (C) 2007, 2008, 2011 Free Software Foundation, Inc. * Copyright (C) 2013 g10 Code GmbH * * This file is part of Libgcrypt. * * Libgcrypt 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. * * Libgcrypt 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 . */ #include #include #define _GCRY_INCLUDED_BY_VISIBILITY_C #include "g10lib.h" #include "cipher-proto.h" #include "context.h" #include "mpi.h" #include "ec-context.h" const char * gcry_strerror (gcry_error_t err) { return _gcry_strerror (err); } const char * gcry_strsource (gcry_error_t err) { return _gcry_strsource (err); } gcry_err_code_t gcry_err_code_from_errno (int err) { return _gcry_err_code_from_errno (err); } int gcry_err_code_to_errno (gcry_err_code_t code) { return _gcry_err_code_to_errno (code); } gcry_error_t gcry_err_make_from_errno (gcry_err_source_t source, int err) { return _gcry_err_make_from_errno (source, err); } gcry_error_t gcry_error_from_errno (int err) { return _gcry_error_from_errno (err); } const char * gcry_check_version (const char *req_version) { return _gcry_check_version (req_version); } gcry_error_t gcry_control (enum gcry_ctl_cmds cmd, ...) { gcry_error_t err; va_list arg_ptr; va_start (arg_ptr, cmd); err = gpg_error (_gcry_vcontrol (cmd, arg_ptr)); va_end(arg_ptr); return err; } gcry_error_t gcry_sexp_new (gcry_sexp_t *retsexp, const void *buffer, size_t length, int autodetect) { return gpg_error (_gcry_sexp_new (retsexp, buffer, length, autodetect)); } gcry_error_t gcry_sexp_create (gcry_sexp_t *retsexp, void *buffer, size_t length, int autodetect, void (*freefnc) (void *)) { return gpg_error (_gcry_sexp_create (retsexp, buffer, length, autodetect, freefnc)); } gcry_error_t gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff, const char *buffer, size_t length) { return gpg_error (_gcry_sexp_sscan (retsexp, erroff, buffer, length)); } gcry_error_t gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff, const char *format, ...) { gcry_err_code_t rc; va_list arg_ptr; va_start (arg_ptr, format); rc = _gcry_sexp_vbuild (retsexp, erroff, format, arg_ptr); va_end (arg_ptr); return gpg_error (rc); } gcry_error_t gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff, const char *format, void **arg_list) { return gpg_error (_gcry_sexp_build_array (retsexp, erroff, format, arg_list)); } void gcry_sexp_release (gcry_sexp_t sexp) { _gcry_sexp_release (sexp); } size_t gcry_sexp_canon_len (const unsigned char *buffer, size_t length, size_t *erroff, gcry_error_t *errcode) { size_t n; gpg_err_code_t rc; n = _gcry_sexp_canon_len (buffer, length, erroff, &rc); if (errcode) *errcode = gpg_error (rc); return n; } size_t gcry_sexp_sprint (gcry_sexp_t sexp, int mode, void *buffer, size_t maxlength) { return _gcry_sexp_sprint (sexp, mode, buffer, maxlength); } void gcry_sexp_dump (const gcry_sexp_t a) { _gcry_sexp_dump (a); } gcry_sexp_t gcry_sexp_cons (const gcry_sexp_t a, const gcry_sexp_t b) { return _gcry_sexp_cons (a, b); } gcry_sexp_t gcry_sexp_alist (const gcry_sexp_t *array) { return _gcry_sexp_alist (array); } gcry_sexp_t gcry_sexp_vlist (const gcry_sexp_t a, ...) { /* This is not yet implemented in sexp.c. */ (void)a; BUG (); return NULL; } gcry_sexp_t gcry_sexp_append (const gcry_sexp_t a, const gcry_sexp_t n) { return _gcry_sexp_append (a, n); } gcry_sexp_t gcry_sexp_prepend (const gcry_sexp_t a, const gcry_sexp_t n) { return _gcry_sexp_prepend (a, n); } gcry_sexp_t gcry_sexp_find_token (gcry_sexp_t list, const char *tok, size_t toklen) { return _gcry_sexp_find_token (list, tok, toklen); } int gcry_sexp_length (const gcry_sexp_t list) { return _gcry_sexp_length (list); } gcry_sexp_t gcry_sexp_nth (const gcry_sexp_t list, int number) { return _gcry_sexp_nth (list, number); } gcry_sexp_t gcry_sexp_car (const gcry_sexp_t list) { return _gcry_sexp_car (list); } gcry_sexp_t gcry_sexp_cdr (const gcry_sexp_t list) { return _gcry_sexp_cdr (list); } gcry_sexp_t gcry_sexp_cadr (const gcry_sexp_t list) { return _gcry_sexp_cadr (list); } const char * gcry_sexp_nth_data (const gcry_sexp_t list, int number, size_t *datalen) { return _gcry_sexp_nth_data (list, number, datalen); } void * gcry_sexp_nth_buffer (const gcry_sexp_t list, int number, size_t *rlength) { return _gcry_sexp_nth_buffer (list, number, rlength); } char * gcry_sexp_nth_string (gcry_sexp_t list, int number) { return _gcry_sexp_nth_string (list, number); } gcry_mpi_t gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt) { return _gcry_sexp_nth_mpi (list, number, mpifmt); } gpg_error_t gcry_sexp_extract_param (gcry_sexp_t sexp, const char *path, const char *list, ...) { gcry_err_code_t rc; va_list arg_ptr; va_start (arg_ptr, list); rc = _gcry_sexp_vextract_param (sexp, path, list, arg_ptr); va_end (arg_ptr); return gpg_error (rc); } gcry_mpi_t gcry_mpi_new (unsigned int nbits) { return _gcry_mpi_new (nbits); } gcry_mpi_t gcry_mpi_snew (unsigned int nbits) { return _gcry_mpi_snew (nbits); } void gcry_mpi_release (gcry_mpi_t a) { _gcry_mpi_release (a); } gcry_mpi_t gcry_mpi_copy (const gcry_mpi_t a) { return _gcry_mpi_copy (a); } void gcry_mpi_snatch (gcry_mpi_t w, const gcry_mpi_t u) { _gcry_mpi_snatch (w, u); } gcry_mpi_t gcry_mpi_set (gcry_mpi_t w, const gcry_mpi_t u) { return _gcry_mpi_set (w, u); } gcry_mpi_t gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u) { return _gcry_mpi_set_ui (w, u); } gcry_error_t gcry_mpi_get_ui (unsigned int *w, gcry_mpi_t u) { return gpg_error (_gcry_mpi_get_ui (w, u)); } void gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b) { _gcry_mpi_swap (a, b); } int gcry_mpi_is_neg (gcry_mpi_t a) { return _gcry_mpi_is_neg (a); } void gcry_mpi_neg (gcry_mpi_t w, gcry_mpi_t u) { _gcry_mpi_neg (w, u); } void gcry_mpi_abs (gcry_mpi_t w) { _gcry_mpi_abs (w); } int gcry_mpi_cmp (const gcry_mpi_t u, const gcry_mpi_t v) { return _gcry_mpi_cmp (u, v); } int gcry_mpi_cmp_ui (const gcry_mpi_t u, unsigned long v) { return _gcry_mpi_cmp_ui (u, v); } gcry_error_t gcry_mpi_scan (gcry_mpi_t *ret_mpi, enum gcry_mpi_format format, const void *buffer, size_t buflen, size_t *nscanned) { return gpg_error (_gcry_mpi_scan (ret_mpi, format, buffer, buflen, nscanned)); } gcry_error_t gcry_mpi_print (enum gcry_mpi_format format, unsigned char *buffer, size_t buflen, size_t *nwritten, const gcry_mpi_t a) { return gpg_error (_gcry_mpi_print (format, buffer, buflen, nwritten, a)); } gcry_error_t gcry_mpi_aprint (enum gcry_mpi_format format, unsigned char **buffer, size_t *nwritten, const gcry_mpi_t a) { return gpg_error (_gcry_mpi_aprint (format, buffer, nwritten, a)); } void gcry_mpi_dump (const gcry_mpi_t a) { _gcry_log_printmpi (NULL, a); } void gcry_mpi_add (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v) { _gcry_mpi_add (w, u, v); } void gcry_mpi_add_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v) { _gcry_mpi_add_ui (w, u, v); } void gcry_mpi_addm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m) { _gcry_mpi_addm (w, u, v, m); } void gcry_mpi_sub (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v) { _gcry_mpi_sub (w, u, v); } void gcry_mpi_sub_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ) { _gcry_mpi_sub_ui (w, u, v); } void gcry_mpi_subm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m) { _gcry_mpi_subm (w, u, v, m); } void gcry_mpi_mul (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v) { _gcry_mpi_mul (w, u, v); } void gcry_mpi_mul_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ) { _gcry_mpi_mul_ui (w, u, v); } void gcry_mpi_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m) { _gcry_mpi_mulm (w, u, v, m); } void gcry_mpi_mul_2exp (gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt) { _gcry_mpi_mul_2exp (w, u, cnt); } void gcry_mpi_div (gcry_mpi_t q, gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor, int round) { _gcry_mpi_div (q, r, dividend, divisor, round); } void gcry_mpi_mod (gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor) { _gcry_mpi_mod (r, dividend, divisor); } void gcry_mpi_powm (gcry_mpi_t w, const gcry_mpi_t b, const gcry_mpi_t e, const gcry_mpi_t m) { _gcry_mpi_powm (w, b, e, m); } int gcry_mpi_gcd (gcry_mpi_t g, gcry_mpi_t a, gcry_mpi_t b) { return _gcry_mpi_gcd (g, a, b); } int gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t m) { return _gcry_mpi_invm (x, a, m); } gcry_mpi_point_t gcry_mpi_point_new (unsigned int nbits) { return _gcry_mpi_point_new (nbits); } void gcry_mpi_point_release (gcry_mpi_point_t point) { _gcry_mpi_point_release (point); } gcry_mpi_point_t gcry_mpi_point_copy (gcry_mpi_point_t point) { return _gcry_mpi_point_copy (point); } void gcry_mpi_point_get (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z, gcry_mpi_point_t point) { _gcry_mpi_point_get (x, y, z, point); } void gcry_mpi_point_snatch_get (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z, gcry_mpi_point_t point) { _gcry_mpi_point_snatch_get (x, y, z, point); } gcry_mpi_point_t gcry_mpi_point_set (gcry_mpi_point_t point, gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z) { return _gcry_mpi_point_set (point, x, y, z); } gcry_mpi_point_t gcry_mpi_point_snatch_set (gcry_mpi_point_t point, gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z) { return _gcry_mpi_point_snatch_set (point, x, y, z); } gpg_error_t gcry_mpi_ec_new (gcry_ctx_t *r_ctx, gcry_sexp_t keyparam, const char *curvename) { return gpg_error (_gcry_mpi_ec_new (r_ctx, keyparam, curvename)); } gcry_mpi_t gcry_mpi_ec_get_mpi (const char *name, gcry_ctx_t ctx, int copy) { return _gcry_mpi_ec_get_mpi (name, ctx, copy); } gcry_mpi_point_t gcry_mpi_ec_get_point (const char *name, gcry_ctx_t ctx, int copy) { return _gcry_mpi_ec_get_point (name, ctx, copy); } gpg_error_t gcry_mpi_ec_set_mpi (const char *name, gcry_mpi_t newvalue, gcry_ctx_t ctx) { return gpg_error (_gcry_mpi_ec_set_mpi (name, newvalue, ctx)); } gpg_error_t gcry_mpi_ec_set_point (const char *name, gcry_mpi_point_t newvalue, gcry_ctx_t ctx) { return gpg_error (_gcry_mpi_ec_set_point (name, newvalue, ctx)); } gpg_error_t gcry_mpi_ec_decode_point (gcry_mpi_point_t result, gcry_mpi_t value, gcry_ctx_t ctx) { return gpg_error (_gcry_mpi_ec_decode_point (result, value, ctx? _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC) : NULL)); } int gcry_mpi_ec_get_affine (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_point_t point, gcry_ctx_t ctx) { return _gcry_mpi_ec_get_affine (x, y, point, _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC)); } void gcry_mpi_ec_dup (gcry_mpi_point_t w, gcry_mpi_point_t u, gcry_ctx_t ctx) { mpi_ec_t ec = _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC); if (ec->model == MPI_EC_EDWARDS || ec->model == MPI_EC_MONTGOMERY) { mpi_point_resize (w, ec); mpi_point_resize (u, ec); } _gcry_mpi_ec_dup_point (w, u, ec); } void gcry_mpi_ec_add (gcry_mpi_point_t w, gcry_mpi_point_t u, gcry_mpi_point_t v, gcry_ctx_t ctx) { mpi_ec_t ec = _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC); if (ec->model == MPI_EC_EDWARDS || ec->model == MPI_EC_MONTGOMERY) { mpi_point_resize (w, ec); mpi_point_resize (u, ec); mpi_point_resize (v, ec); } _gcry_mpi_ec_add_points (w, u, v, ec); } void gcry_mpi_ec_sub (gcry_mpi_point_t w, gcry_mpi_point_t u, gcry_mpi_point_t v, gcry_ctx_t ctx) { mpi_ec_t ec = _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC); if (ec->model == MPI_EC_EDWARDS || ec->model == MPI_EC_MONTGOMERY) { mpi_point_resize (w, ec); mpi_point_resize (u, ec); mpi_point_resize (v, ec); } _gcry_mpi_ec_sub_points (w, u, v, ec); } void gcry_mpi_ec_mul (gcry_mpi_point_t w, gcry_mpi_t n, gcry_mpi_point_t u, gcry_ctx_t ctx) { _gcry_mpi_ec_mul_point (w, n, u, _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC)); } int gcry_mpi_ec_curve_point (gcry_mpi_point_t point, gcry_ctx_t ctx) { return _gcry_mpi_ec_curve_point (point, _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC)); } unsigned int gcry_mpi_get_nbits (gcry_mpi_t a) { return _gcry_mpi_get_nbits (a); } int gcry_mpi_test_bit (gcry_mpi_t a, unsigned int n) { return _gcry_mpi_test_bit (a, n); } void gcry_mpi_set_bit (gcry_mpi_t a, unsigned int n) { _gcry_mpi_set_bit (a, n); } void gcry_mpi_clear_bit (gcry_mpi_t a, unsigned int n) { _gcry_mpi_clear_bit (a, n); } void gcry_mpi_set_highbit (gcry_mpi_t a, unsigned int n) { _gcry_mpi_set_highbit (a, n); } void gcry_mpi_clear_highbit (gcry_mpi_t a, unsigned int n) { _gcry_mpi_clear_highbit (a, n); } void gcry_mpi_rshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n) { _gcry_mpi_rshift (x, a, n); } void gcry_mpi_lshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n) { _gcry_mpi_lshift (x, a, n); } gcry_mpi_t gcry_mpi_set_opaque (gcry_mpi_t a, void *p, unsigned int nbits) { return _gcry_mpi_set_opaque (a, p, nbits); } gcry_mpi_t gcry_mpi_set_opaque_copy (gcry_mpi_t a, const void *p, unsigned int nbits) { return _gcry_mpi_set_opaque_copy (a, p, nbits); } void * gcry_mpi_get_opaque (gcry_mpi_t a, unsigned int *nbits) { return _gcry_mpi_get_opaque (a, nbits); } void gcry_mpi_set_flag (gcry_mpi_t a, enum gcry_mpi_flag flag) { _gcry_mpi_set_flag (a, flag); } void gcry_mpi_clear_flag (gcry_mpi_t a, enum gcry_mpi_flag flag) { _gcry_mpi_clear_flag (a, flag); } int gcry_mpi_get_flag (gcry_mpi_t a, enum gcry_mpi_flag flag) { return _gcry_mpi_get_flag (a, flag); } gcry_mpi_t _gcry_mpi_get_const (int no) { switch (no) { case 1: return _gcry_mpi_const (MPI_C_ONE); case 2: return _gcry_mpi_const (MPI_C_TWO); case 3: return _gcry_mpi_const (MPI_C_THREE); case 4: return _gcry_mpi_const (MPI_C_FOUR); case 8: return _gcry_mpi_const (MPI_C_EIGHT); default: log_bug("unsupported GCRYMPI_CONST_ macro used\n"); } } gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *handle, int algo, int mode, unsigned int flags) { if (!fips_is_operational ()) { *handle = NULL; return gpg_error (fips_not_operational ()); } return gpg_error (_gcry_cipher_open (handle, algo, mode, flags)); } void gcry_cipher_close (gcry_cipher_hd_t h) { _gcry_cipher_close (h); } gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t hd, const void *key, size_t keylen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gcry_error (_gcry_cipher_setkey (hd, key, keylen)); } gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t hd, const void *iv, size_t ivlen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gcry_error (_gcry_cipher_setiv (hd, iv, ivlen)); } gpg_error_t gcry_cipher_setctr (gcry_cipher_hd_t hd, const void *ctr, size_t ctrlen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gcry_error (_gcry_cipher_setctr (hd, ctr, ctrlen)); } gcry_error_t gcry_cipher_authenticate (gcry_cipher_hd_t hd, const void *abuf, size_t abuflen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_cipher_authenticate (hd, abuf, abuflen)); } gcry_error_t gcry_cipher_gettag (gcry_cipher_hd_t hd, void *outtag, size_t taglen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_cipher_gettag (hd, outtag, taglen)); } gcry_error_t gcry_cipher_checktag (gcry_cipher_hd_t hd, const void *intag, size_t taglen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_cipher_checktag (hd, intag, taglen)); } gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer, size_t buflen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_cipher_ctl (h, cmd, buffer, buflen)); } gcry_error_t gcry_cipher_info (gcry_cipher_hd_t h, int what, void *buffer, size_t *nbytes) { return gpg_error (_gcry_cipher_info (h, what, buffer, nbytes)); } gcry_error_t gcry_cipher_algo_info (int algo, int what, void *buffer, size_t *nbytes) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_cipher_algo_info (algo, what, buffer, nbytes)); } const char * gcry_cipher_algo_name (int algorithm) { return _gcry_cipher_algo_name (algorithm); } int gcry_cipher_map_name (const char *name) { return _gcry_cipher_map_name (name); } int gcry_cipher_mode_from_oid (const char *string) { return _gcry_cipher_mode_from_oid (string); } gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h, void *out, size_t outsize, const void *in, size_t inlen) { if (!fips_is_operational ()) { /* Make sure that the plaintext will never make it to OUT. */ if (out) memset (out, 0x42, outsize); return gpg_error (fips_not_operational ()); } return gpg_error (_gcry_cipher_encrypt (h, out, outsize, in, inlen)); } gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t h, void *out, size_t outsize, const void *in, size_t inlen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_cipher_decrypt (h, out, outsize, in, inlen)); } size_t gcry_cipher_get_algo_keylen (int algo) { return _gcry_cipher_get_algo_keylen (algo); } size_t gcry_cipher_get_algo_blklen (int algo) { return _gcry_cipher_get_algo_blklen (algo); } gcry_error_t gcry_mac_algo_info (int algo, int what, void *buffer, size_t *nbytes) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_mac_algo_info (algo, what, buffer, nbytes)); } const char * gcry_mac_algo_name (int algorithm) { return _gcry_mac_algo_name (algorithm); } int gcry_mac_map_name (const char *string) { return _gcry_mac_map_name (string); } int gcry_mac_get_algo (gcry_mac_hd_t hd) { return _gcry_mac_get_algo (hd); } unsigned int gcry_mac_get_algo_maclen (int algo) { return _gcry_mac_get_algo_maclen (algo); } unsigned int gcry_mac_get_algo_keylen (int algo) { return _gcry_mac_get_algo_keylen (algo); } gcry_error_t gcry_mac_open (gcry_mac_hd_t *handle, int algo, unsigned int flags, gcry_ctx_t ctx) { if (!fips_is_operational ()) { *handle = NULL; return gpg_error (fips_not_operational ()); } return gpg_error (_gcry_mac_open (handle, algo, flags, ctx)); } void gcry_mac_close (gcry_mac_hd_t hd) { _gcry_mac_close (hd); } gcry_error_t gcry_mac_setkey (gcry_mac_hd_t hd, const void *key, size_t keylen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_mac_setkey (hd, key, keylen)); } gcry_error_t gcry_mac_setiv (gcry_mac_hd_t hd, const void *iv, size_t ivlen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_mac_setiv (hd, iv, ivlen)); } gcry_error_t gcry_mac_write (gcry_mac_hd_t hd, const void *buf, size_t buflen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_mac_write (hd, buf, buflen)); } gcry_error_t gcry_mac_read (gcry_mac_hd_t hd, void *outbuf, size_t *outlen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_mac_read (hd, outbuf, outlen)); } gcry_error_t gcry_mac_verify (gcry_mac_hd_t hd, const void *buf, size_t buflen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_mac_verify (hd, buf, buflen)); } gcry_error_t gcry_mac_ctl (gcry_mac_hd_t h, int cmd, void *buffer, size_t buflen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_mac_ctl (h, cmd, buffer, buflen)); } gcry_error_t gcry_pk_encrypt (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t pkey) { if (!fips_is_operational ()) { *result = NULL; return gpg_error (fips_not_operational ()); } return gpg_error (_gcry_pk_encrypt (result, data, pkey)); } gcry_error_t gcry_pk_decrypt (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t skey) { if (!fips_is_operational ()) { *result = NULL; return gpg_error (fips_not_operational ()); } return gpg_error (_gcry_pk_decrypt (result, data, skey)); } gcry_error_t gcry_pk_sign (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t skey) { if (!fips_is_operational ()) { *result = NULL; return gpg_error (fips_not_operational ()); } return gpg_error (_gcry_pk_sign (result, data, skey)); } gcry_error_t gcry_pk_hash_sign (gcry_sexp_t *result, const char *data_tmpl, gcry_sexp_t skey, gcry_md_hd_t hd, gcry_ctx_t ctx) { return gpg_error (_gcry_pk_sign_md (result, data_tmpl, hd, skey, ctx)); } gcry_error_t gcry_pk_verify (gcry_sexp_t sigval, gcry_sexp_t data, gcry_sexp_t pkey) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_pk_verify (sigval, data, pkey)); } gcry_error_t gcry_pk_hash_verify (gcry_sexp_t sigval, const char *data_tmpl, gcry_sexp_t pkey, gcry_md_hd_t hd, gcry_ctx_t ctx) { return gpg_error (_gcry_pk_verify_md (sigval, data_tmpl, hd, pkey, ctx)); } gcry_error_t gcry_pk_random_override_new (gcry_ctx_t *r_ctx, const unsigned char *p, size_t len) { return gpg_error (_gcry_pk_random_override_new (r_ctx, p, len)); } gcry_error_t gcry_pk_testkey (gcry_sexp_t key) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_pk_testkey (key)); } gcry_error_t gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms) { if (!fips_is_operational ()) { *r_key = NULL; return gpg_error (fips_not_operational ()); } return gpg_error (_gcry_pk_genkey (r_key, s_parms)); } gcry_error_t gcry_pk_ctl (int cmd, void *buffer, size_t buflen) { return gpg_error (_gcry_pk_ctl (cmd, buffer, buflen)); } gcry_error_t gcry_pk_algo_info (int algo, int what, void *buffer, size_t *nbytes) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_pk_algo_info (algo, what, buffer, nbytes)); } const char * gcry_pk_algo_name (int algorithm) { return _gcry_pk_algo_name (algorithm); } int gcry_pk_map_name (const char *name) { return _gcry_pk_map_name (name); } unsigned int gcry_pk_get_nbits (gcry_sexp_t key) { if (!fips_is_operational ()) { (void)fips_not_operational (); return 0; } return _gcry_pk_get_nbits (key); } unsigned char * gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array) { if (!fips_is_operational ()) { (void)fips_not_operational (); return NULL; } return _gcry_pk_get_keygrip (key, array); } const char * gcry_pk_get_curve (gcry_sexp_t key, int iterator, unsigned int *r_nbits) { if (!fips_is_operational ()) { (void)fips_not_operational (); return NULL; } return _gcry_pk_get_curve (key, iterator, r_nbits); } gcry_sexp_t gcry_pk_get_param (int algo, const char *name) { if (!fips_is_operational ()) { (void)fips_not_operational (); return NULL; } return _gcry_pk_get_param (algo, name); } gcry_error_t gcry_pubkey_get_sexp (gcry_sexp_t *r_sexp, int mode, gcry_ctx_t ctx) { if (!fips_is_operational ()) { *r_sexp = NULL; return gpg_error (fips_not_operational ()); } return gpg_error (_gcry_pubkey_get_sexp (r_sexp, mode, ctx)); } unsigned int gcry_ecc_get_algo_keylen (int curveid) { return _gcry_ecc_get_algo_keylen (curveid); } gpg_error_t gcry_ecc_mul_point (int curveid, unsigned char *result, const unsigned char *scalar, const unsigned char *point) { return _gcry_ecc_mul_point (curveid, result, scalar, point); } gcry_error_t gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags) { if (!fips_is_operational ()) { *h = NULL; return gpg_error (fips_not_operational ()); } return gpg_error (_gcry_md_open (h, algo, flags)); } void gcry_md_close (gcry_md_hd_t hd) { _gcry_md_close (hd); } gcry_error_t gcry_md_enable (gcry_md_hd_t hd, int algo) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_md_enable (hd, algo)); } gcry_error_t gcry_md_copy (gcry_md_hd_t *bhd, gcry_md_hd_t ahd) { if (!fips_is_operational ()) { *bhd = NULL; return gpg_error (fips_not_operational ()); } return gpg_error (_gcry_md_copy (bhd, ahd)); } void gcry_md_reset (gcry_md_hd_t hd) { _gcry_md_reset (hd); } gcry_error_t gcry_md_ctl (gcry_md_hd_t hd, int cmd, void *buffer, size_t buflen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_md_ctl (hd, cmd, buffer, buflen)); } void gcry_md_write (gcry_md_hd_t hd, const void *buffer, size_t length) { if (!fips_is_operational ()) { (void)fips_not_operational (); return; } _gcry_md_write (hd, buffer, length); } unsigned char * gcry_md_read (gcry_md_hd_t hd, int algo) { return _gcry_md_read (hd, algo); } gcry_error_t gcry_md_extract (gcry_md_hd_t hd, int algo, void *buffer, size_t length) { return gpg_error (_gcry_md_extract(hd, algo, buffer, length)); } void gcry_md_hash_buffer (int algo, void *digest, const void *buffer, size_t length) { if (!fips_is_operational ()) { (void)fips_not_operational (); fips_signal_error ("called in non-operational state"); } _gcry_md_hash_buffer (algo, digest, buffer, length); } gpg_error_t gcry_md_hash_buffers (int algo, unsigned int flags, void *digest, const gcry_buffer_t *iov, int iovcnt) { if (!fips_is_operational ()) { (void)fips_not_operational (); fips_signal_error ("called in non-operational state"); } return gpg_error (_gcry_md_hash_buffers (algo, flags, digest, iov, iovcnt)); } int gcry_md_get_algo (gcry_md_hd_t hd) { if (!fips_is_operational ()) { (void)fips_not_operational (); fips_signal_error ("used in non-operational state"); return 0; } return _gcry_md_get_algo (hd); } unsigned int gcry_md_get_algo_dlen (int algo) { return _gcry_md_get_algo_dlen (algo); } int gcry_md_is_enabled (gcry_md_hd_t a, int algo) { if (!fips_is_operational ()) { (void)fips_not_operational (); return 0; } return _gcry_md_is_enabled (a, algo); } int gcry_md_is_secure (gcry_md_hd_t a) { return _gcry_md_is_secure (a); } gcry_error_t gcry_md_info (gcry_md_hd_t h, int what, void *buffer, size_t *nbytes) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_md_info (h, what, buffer, nbytes)); } gcry_error_t gcry_md_algo_info (int algo, int what, void *buffer, size_t *nbytes) { return gpg_error (_gcry_md_algo_info (algo, what, buffer, nbytes)); } const char * gcry_md_algo_name (int algo) { return _gcry_md_algo_name (algo); } int gcry_md_map_name (const char* name) { return _gcry_md_map_name (name); } gcry_error_t gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_md_setkey (hd, key, keylen)); } void gcry_md_debug (gcry_md_hd_t hd, const char *suffix) { _gcry_md_debug (hd, suffix); } gpg_error_t gcry_kdf_derive (const void *passphrase, size_t passphraselen, int algo, int hashalgo, const void *salt, size_t saltlen, unsigned long iterations, size_t keysize, void *keybuffer) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_kdf_derive (passphrase, passphraselen, algo, hashalgo, salt, saltlen, iterations, keysize, keybuffer)); } gpg_error_t gcry_kdf_open (gcry_kdf_hd_t *hd, int algo, int subalgo, const unsigned long *param, unsigned int paramlen, const void *passphrase, size_t passphraselen, const void *salt, size_t saltlen, const void *key, size_t keylen, const void *ad, size_t adlen) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_kdf_open (hd, algo, subalgo, param, paramlen, passphrase, passphraselen, salt, saltlen, key, keylen, ad, adlen)); } gcry_error_t -gcry_kdf_ctl (gcry_kdf_hd_t h, int cmd, void *buffer, size_t buflen) +gcry_kdf_compute (gcry_kdf_hd_t h, const struct gcry_kdf_thread_ops *ops) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); - return gpg_error (_gcry_kdf_ctl (h, cmd, buffer, buflen)); -} - -gcry_error_t -gcry_kdf_iterator (gcry_kdf_hd_t h, int *action_p, - struct gcry_kdf_pt_head **t_p) -{ - if (!fips_is_operational ()) - return gpg_error (fips_not_operational ()); - return gpg_error (_gcry_kdf_iterator (h, action_p, t_p)); -} - -gcry_error_t -gcry_kdf_compute_segment (gcry_kdf_hd_t h, - const struct gcry_kdf_pt_head *t) -{ - if (!fips_is_operational ()) - return gpg_error (fips_not_operational ()); - return gpg_error (_gcry_kdf_compute_segment (h, t)); + return gpg_error (_gcry_kdf_compute (h, ops)); } gcry_error_t gcry_kdf_final (gcry_kdf_hd_t h, size_t resultlen, void *result) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_kdf_final (h, resultlen, result)); } void gcry_kdf_close (gcry_kdf_hd_t h) { _gcry_kdf_close (h); } void gcry_randomize (void *buffer, size_t length, enum gcry_random_level level) { if (!fips_is_operational ()) { (void)fips_not_operational (); fips_signal_fatal_error ("called in non-operational state"); fips_noreturn (); } _gcry_randomize (buffer, length, level); } gcry_error_t gcry_random_add_bytes (const void *buffer, size_t length, int quality) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_random_add_bytes (buffer, length, quality)); } void * gcry_random_bytes (size_t nbytes, enum gcry_random_level level) { if (!fips_is_operational ()) { (void)fips_not_operational (); fips_signal_fatal_error ("called in non-operational state"); fips_noreturn (); } return _gcry_random_bytes (nbytes,level); } void * gcry_random_bytes_secure (size_t nbytes, enum gcry_random_level level) { if (!fips_is_operational ()) { (void)fips_not_operational (); fips_signal_fatal_error ("called in non-operational state"); fips_noreturn (); } return _gcry_random_bytes_secure (nbytes, level); } void gcry_mpi_randomize (gcry_mpi_t w, unsigned int nbits, enum gcry_random_level level) { if (!fips_is_operational ()) { (void)fips_not_operational (); fips_signal_fatal_error ("called in non-operational state"); fips_noreturn (); } _gcry_mpi_randomize (w, nbits, level); } void gcry_create_nonce (void *buffer, size_t length) { if (!fips_is_operational ()) { (void)fips_not_operational (); fips_signal_fatal_error ("called in non-operational state"); fips_noreturn (); } _gcry_create_nonce (buffer, length); } gcry_error_t gcry_prime_generate (gcry_mpi_t *prime, unsigned int prime_bits, unsigned int factor_bits, gcry_mpi_t **factors, gcry_prime_check_func_t cb_func, void *cb_arg, gcry_random_level_t random_level, unsigned int flags) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_prime_generate (prime, prime_bits, factor_bits, factors, cb_func, cb_arg, random_level, flags)); } gcry_error_t gcry_prime_group_generator (gcry_mpi_t *r_g, gcry_mpi_t prime, gcry_mpi_t *factors, gcry_mpi_t start_g) { if (!fips_is_operational ()) return gpg_error (fips_not_operational ()); return gpg_error (_gcry_prime_group_generator (r_g, prime, factors, start_g)); } void gcry_prime_release_factors (gcry_mpi_t *factors) { _gcry_prime_release_factors (factors); } gcry_error_t gcry_prime_check (gcry_mpi_t x, unsigned int flags) { return gpg_error (_gcry_prime_check (x, flags)); } void gcry_ctx_release (gcry_ctx_t ctx) { _gcry_ctx_release (ctx); } void gcry_log_debug (const char *fmt, ...) { va_list arg_ptr ; va_start( arg_ptr, fmt ) ; _gcry_logv (GCRY_LOG_DEBUG, fmt, arg_ptr); va_end (arg_ptr); } void gcry_log_debughex (const char *text, const void *buffer, size_t length) { _gcry_log_printhex (text, buffer, length); } void gcry_log_debugmpi (const char *text, gcry_mpi_t mpi) { _gcry_log_printmpi (text, mpi); } void gcry_log_debugpnt (const char *text, mpi_point_t point, gcry_ctx_t ctx) { mpi_ec_t ec = ctx? _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC) : NULL; _gcry_mpi_point_log (text, point, ec); } void gcry_log_debugsxp (const char *text, gcry_sexp_t sexp) { _gcry_log_printsxp (text, sexp); } char * gcry_get_config (int mode, const char *what) { return _gcry_get_config (mode, what); } void gcry_set_progress_handler (gcry_handler_progress_t cb, void *cb_data) { _gcry_set_progress_handler (cb, cb_data); } void gcry_set_allocation_handler (gcry_handler_alloc_t func_alloc, gcry_handler_alloc_t func_alloc_secure, gcry_handler_secure_check_t func_secure_check, gcry_handler_realloc_t func_realloc, gcry_handler_free_t func_free) { _gcry_set_allocation_handler (func_alloc, func_alloc_secure, func_secure_check, func_realloc, func_free); } void gcry_set_outofcore_handler (gcry_handler_no_mem_t h, void *opaque) { _gcry_set_outofcore_handler (h, opaque); } void gcry_set_fatalerror_handler (gcry_handler_error_t fnc, void *opaque) { _gcry_set_fatalerror_handler (fnc, opaque); } void gcry_set_log_handler (gcry_handler_log_t f, void *opaque) { _gcry_set_log_handler (f, opaque); } void gcry_set_gettext_handler (const char *(*f)(const char*)) { _gcry_set_gettext_handler (f); } void * gcry_malloc (size_t n) { return _gcry_malloc (n); } void * gcry_calloc (size_t n, size_t m) { return _gcry_calloc (n, m); } void * gcry_malloc_secure (size_t n) { return _gcry_malloc_secure (n); } void * gcry_calloc_secure (size_t n, size_t m) { return _gcry_calloc_secure (n,m); } void * gcry_realloc (void *a, size_t n) { return _gcry_realloc (a, n); } char * gcry_strdup (const char *string) { return _gcry_strdup (string); } void * gcry_xmalloc (size_t n) { return _gcry_xmalloc (n); } void * gcry_xcalloc (size_t n, size_t m) { return _gcry_xcalloc (n, m); } void * gcry_xmalloc_secure (size_t n) { return _gcry_xmalloc_secure (n); } void * gcry_xcalloc_secure (size_t n, size_t m) { return _gcry_xcalloc_secure (n, m); } void * gcry_xrealloc (void *a, size_t n) { return _gcry_xrealloc (a, n); } char * gcry_xstrdup (const char *a) { return _gcry_xstrdup (a); } void gcry_free (void *a) { _gcry_free (a); } int gcry_is_secure (const void *a) { return _gcry_is_secure (a); } diff --git a/src/visibility.h b/src/visibility.h index a4ffadf0..14bf6248 100644 --- a/src/visibility.h +++ b/src/visibility.h @@ -1,539 +1,535 @@ /* visibility.h - Set visibility attribute * Copyright (C) 2007 Free Software Foundation, Inc. * * This file is part of Libgcrypt. * * Libgcrypt 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. * * Libgcrypt 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 . */ #ifndef GCRY_VISIBILITY_H #define GCRY_VISIBILITY_H /* Redefine all public symbols with an underscore unless we already use the underscore prefixed version internally. */ /* Include the main header here so that public symbols are mapped to the internal underscored ones. */ #ifdef _GCRY_INCLUDED_BY_VISIBILITY_C /* We need to redeclare the deprecated functions without the deprecated attribute. */ # define GCRYPT_NO_DEPRECATED # include "gcrypt-int.h" /* None in this version. */ #else # include "gcrypt-int.h" #endif /* Prototypes of functions exported but not ready for use. */ gcry_err_code_t gcry_md_get (gcry_md_hd_t hd, int algo, unsigned char *buffer, int buflen); /* Our use of the ELF visibility feature works by passing -fvisibiliy=hidden on the command line and by explicitly marking all exported functions as visible. NOTE: When adding new functions, please make sure to add them to libgcrypt.vers and libgcrypt.def as well. */ #ifdef _GCRY_INCLUDED_BY_VISIBILITY_C /* A macro to flag a function as visible. */ #ifdef GCRY_USE_VISIBILITY # define MARK_VISIBLEX(name) \ extern __typeof__ (name) name __attribute__ ((visibility("default"))); #else # define MARK_VISIBLEX(name) /* */ #endif /* Now mark all symbols. */ MARK_VISIBLEX (gcry_check_version) MARK_VISIBLEX (gcry_control) MARK_VISIBLEX (gcry_set_allocation_handler) MARK_VISIBLEX (gcry_set_fatalerror_handler) MARK_VISIBLEX (gcry_set_gettext_handler) MARK_VISIBLEX (gcry_set_log_handler) MARK_VISIBLEX (gcry_set_outofcore_handler) MARK_VISIBLEX (gcry_set_progress_handler) MARK_VISIBLEX (gcry_err_code_from_errno) MARK_VISIBLEX (gcry_err_code_to_errno) MARK_VISIBLEX (gcry_err_make_from_errno) MARK_VISIBLEX (gcry_error_from_errno) MARK_VISIBLEX (gcry_strerror) MARK_VISIBLEX (gcry_strsource) MARK_VISIBLEX (gcry_malloc) MARK_VISIBLEX (gcry_malloc_secure) MARK_VISIBLEX (gcry_calloc) MARK_VISIBLEX (gcry_calloc_secure) MARK_VISIBLEX (gcry_realloc) MARK_VISIBLEX (gcry_strdup) MARK_VISIBLEX (gcry_is_secure) MARK_VISIBLEX (gcry_xcalloc) MARK_VISIBLEX (gcry_xcalloc_secure) MARK_VISIBLEX (gcry_xmalloc) MARK_VISIBLEX (gcry_xmalloc_secure) MARK_VISIBLEX (gcry_xrealloc) MARK_VISIBLEX (gcry_xstrdup) MARK_VISIBLEX (gcry_free) MARK_VISIBLEX (gcry_md_algo_info) MARK_VISIBLEX (gcry_md_algo_name) MARK_VISIBLEX (gcry_md_close) MARK_VISIBLEX (gcry_md_copy) MARK_VISIBLEX (gcry_md_ctl) MARK_VISIBLEX (gcry_md_enable) MARK_VISIBLEX (gcry_md_get) MARK_VISIBLEX (gcry_md_get_algo) MARK_VISIBLEX (gcry_md_get_algo_dlen) MARK_VISIBLEX (gcry_md_hash_buffer) MARK_VISIBLEX (gcry_md_hash_buffers) MARK_VISIBLEX (gcry_md_info) MARK_VISIBLEX (gcry_md_is_enabled) MARK_VISIBLEX (gcry_md_is_secure) MARK_VISIBLEX (gcry_md_map_name) MARK_VISIBLEX (gcry_md_open) MARK_VISIBLEX (gcry_md_read) MARK_VISIBLEX (gcry_md_extract) MARK_VISIBLEX (gcry_md_reset) MARK_VISIBLEX (gcry_md_setkey) MARK_VISIBLEX (gcry_md_write) MARK_VISIBLEX (gcry_md_debug) MARK_VISIBLEX (gcry_cipher_algo_info) MARK_VISIBLEX (gcry_cipher_algo_name) MARK_VISIBLEX (gcry_cipher_close) MARK_VISIBLEX (gcry_cipher_setkey) MARK_VISIBLEX (gcry_cipher_setiv) MARK_VISIBLEX (gcry_cipher_setctr) MARK_VISIBLEX (gcry_cipher_authenticate) MARK_VISIBLEX (gcry_cipher_checktag) MARK_VISIBLEX (gcry_cipher_gettag) MARK_VISIBLEX (gcry_cipher_ctl) MARK_VISIBLEX (gcry_cipher_decrypt) MARK_VISIBLEX (gcry_cipher_encrypt) MARK_VISIBLEX (gcry_cipher_get_algo_blklen) MARK_VISIBLEX (gcry_cipher_get_algo_keylen) MARK_VISIBLEX (gcry_cipher_info) MARK_VISIBLEX (gcry_cipher_map_name) MARK_VISIBLEX (gcry_cipher_mode_from_oid) MARK_VISIBLEX (gcry_cipher_open) MARK_VISIBLEX (gcry_mac_algo_info) MARK_VISIBLEX (gcry_mac_algo_name) MARK_VISIBLEX (gcry_mac_map_name) MARK_VISIBLEX (gcry_mac_get_algo) MARK_VISIBLEX (gcry_mac_get_algo_maclen) MARK_VISIBLEX (gcry_mac_get_algo_keylen) MARK_VISIBLEX (gcry_mac_open) MARK_VISIBLEX (gcry_mac_close) MARK_VISIBLEX (gcry_mac_setkey) MARK_VISIBLEX (gcry_mac_setiv) MARK_VISIBLEX (gcry_mac_write) MARK_VISIBLEX (gcry_mac_read) MARK_VISIBLEX (gcry_mac_verify) MARK_VISIBLEX (gcry_mac_ctl) MARK_VISIBLEX (gcry_pk_algo_info) MARK_VISIBLEX (gcry_pk_algo_name) MARK_VISIBLEX (gcry_pk_ctl) MARK_VISIBLEX (gcry_pk_decrypt) MARK_VISIBLEX (gcry_pk_encrypt) MARK_VISIBLEX (gcry_pk_genkey) MARK_VISIBLEX (gcry_pk_get_keygrip) MARK_VISIBLEX (gcry_pk_get_curve) MARK_VISIBLEX (gcry_pk_get_param) MARK_VISIBLEX (gcry_pk_get_nbits) MARK_VISIBLEX (gcry_pk_map_name) MARK_VISIBLEX (gcry_pk_sign) MARK_VISIBLEX (gcry_pk_testkey) MARK_VISIBLEX (gcry_pk_verify) MARK_VISIBLEX (gcry_pubkey_get_sexp) MARK_VISIBLEX (gcry_ecc_get_algo_keylen) MARK_VISIBLEX (gcry_ecc_mul_point) MARK_VISIBLEX (gcry_pk_hash_sign) MARK_VISIBLEX (gcry_pk_hash_verify) MARK_VISIBLEX (gcry_pk_random_override_new) MARK_VISIBLEX (gcry_kdf_derive) MARK_VISIBLEX (gcry_kdf_open) -MARK_VISIBLEX (gcry_kdf_ctl) -MARK_VISIBLEX (gcry_kdf_iterator) -MARK_VISIBLEX (gcry_kdf_compute_segment) +MARK_VISIBLEX (gcry_kdf_compute) MARK_VISIBLEX (gcry_kdf_final) MARK_VISIBLEX (gcry_kdf_close) MARK_VISIBLEX (gcry_prime_check) MARK_VISIBLEX (gcry_prime_generate) MARK_VISIBLEX (gcry_prime_group_generator) MARK_VISIBLEX (gcry_prime_release_factors) MARK_VISIBLEX (gcry_random_add_bytes) MARK_VISIBLEX (gcry_random_bytes) MARK_VISIBLEX (gcry_random_bytes_secure) MARK_VISIBLEX (gcry_randomize) MARK_VISIBLEX (gcry_create_nonce) MARK_VISIBLEX (gcry_sexp_alist) MARK_VISIBLEX (gcry_sexp_append) MARK_VISIBLEX (gcry_sexp_build) MARK_VISIBLEX (gcry_sexp_build_array) MARK_VISIBLEX (gcry_sexp_cadr) MARK_VISIBLEX (gcry_sexp_canon_len) MARK_VISIBLEX (gcry_sexp_car) MARK_VISIBLEX (gcry_sexp_cdr) MARK_VISIBLEX (gcry_sexp_cons) MARK_VISIBLEX (gcry_sexp_create) MARK_VISIBLEX (gcry_sexp_dump) MARK_VISIBLEX (gcry_sexp_find_token) MARK_VISIBLEX (gcry_sexp_length) MARK_VISIBLEX (gcry_sexp_new) MARK_VISIBLEX (gcry_sexp_nth) MARK_VISIBLEX (gcry_sexp_nth_buffer) MARK_VISIBLEX (gcry_sexp_nth_data) MARK_VISIBLEX (gcry_sexp_nth_mpi) MARK_VISIBLEX (gcry_sexp_nth_string) MARK_VISIBLEX (gcry_sexp_prepend) MARK_VISIBLEX (gcry_sexp_release) MARK_VISIBLEX (gcry_sexp_sprint) MARK_VISIBLEX (gcry_sexp_sscan) MARK_VISIBLEX (gcry_sexp_vlist) MARK_VISIBLEX (gcry_sexp_extract_param) MARK_VISIBLEX (gcry_mpi_abs) MARK_VISIBLEX (gcry_mpi_add) MARK_VISIBLEX (gcry_mpi_add_ui) MARK_VISIBLEX (gcry_mpi_addm) MARK_VISIBLEX (gcry_mpi_aprint) MARK_VISIBLEX (gcry_mpi_clear_bit) MARK_VISIBLEX (gcry_mpi_clear_flag) MARK_VISIBLEX (gcry_mpi_clear_highbit) MARK_VISIBLEX (gcry_mpi_cmp) MARK_VISIBLEX (gcry_mpi_cmp_ui) MARK_VISIBLEX (gcry_mpi_copy) MARK_VISIBLEX (gcry_mpi_div) MARK_VISIBLEX (gcry_mpi_dump) MARK_VISIBLEX (gcry_mpi_ec_add) MARK_VISIBLEX (gcry_mpi_ec_sub) MARK_VISIBLEX (gcry_mpi_ec_curve_point) MARK_VISIBLEX (gcry_mpi_ec_dup) MARK_VISIBLEX (gcry_mpi_ec_decode_point) MARK_VISIBLEX (gcry_mpi_ec_get_affine) MARK_VISIBLEX (gcry_mpi_ec_mul) MARK_VISIBLEX (gcry_mpi_ec_new) MARK_VISIBLEX (gcry_mpi_ec_get_mpi) MARK_VISIBLEX (gcry_mpi_ec_get_point) MARK_VISIBLEX (gcry_mpi_ec_set_mpi) MARK_VISIBLEX (gcry_mpi_ec_set_point) MARK_VISIBLEX (gcry_mpi_gcd) MARK_VISIBLEX (gcry_mpi_get_flag) MARK_VISIBLEX (gcry_mpi_get_nbits) MARK_VISIBLEX (gcry_mpi_get_opaque) MARK_VISIBLEX (gcry_mpi_is_neg) MARK_VISIBLEX (gcry_mpi_invm) MARK_VISIBLEX (gcry_mpi_mod) MARK_VISIBLEX (gcry_mpi_mul) MARK_VISIBLEX (gcry_mpi_mul_2exp) MARK_VISIBLEX (gcry_mpi_mul_ui) MARK_VISIBLEX (gcry_mpi_mulm) MARK_VISIBLEX (gcry_mpi_neg) MARK_VISIBLEX (gcry_mpi_new) MARK_VISIBLEX (gcry_mpi_point_get) MARK_VISIBLEX (gcry_mpi_point_new) MARK_VISIBLEX (gcry_mpi_point_release) MARK_VISIBLEX (gcry_mpi_point_copy) MARK_VISIBLEX (gcry_mpi_point_set) MARK_VISIBLEX (gcry_mpi_point_snatch_get) MARK_VISIBLEX (gcry_mpi_point_snatch_set) MARK_VISIBLEX (gcry_mpi_powm) MARK_VISIBLEX (gcry_mpi_print) MARK_VISIBLEX (gcry_mpi_randomize) MARK_VISIBLEX (gcry_mpi_release) MARK_VISIBLEX (gcry_mpi_rshift) MARK_VISIBLEX (gcry_mpi_lshift) MARK_VISIBLEX (gcry_mpi_scan) MARK_VISIBLEX (gcry_mpi_snatch) MARK_VISIBLEX (gcry_mpi_set) MARK_VISIBLEX (gcry_mpi_set_bit) MARK_VISIBLEX (gcry_mpi_set_flag) MARK_VISIBLEX (gcry_mpi_set_highbit) MARK_VISIBLEX (gcry_mpi_set_opaque) MARK_VISIBLEX (gcry_mpi_set_opaque_copy) MARK_VISIBLEX (gcry_mpi_set_ui) MARK_VISIBLEX (gcry_mpi_get_ui) MARK_VISIBLEX (gcry_mpi_snew) MARK_VISIBLEX (gcry_mpi_sub) MARK_VISIBLEX (gcry_mpi_sub_ui) MARK_VISIBLEX (gcry_mpi_subm) MARK_VISIBLEX (gcry_mpi_swap) MARK_VISIBLEX (gcry_mpi_test_bit) MARK_VISIBLEX (gcry_ctx_release) MARK_VISIBLEX (gcry_log_debug) MARK_VISIBLEX (gcry_log_debughex) MARK_VISIBLEX (gcry_log_debugmpi) MARK_VISIBLEX (gcry_log_debugpnt) MARK_VISIBLEX (gcry_log_debugsxp) MARK_VISIBLEX (gcry_get_config) /* Functions used to implement macros. */ MARK_VISIBLEX (_gcry_mpi_get_const) #undef MARK_VISIBLEX #else /*!_GCRY_INCLUDED_BY_VISIBILITY_C*/ /* To avoid accidental use of the public functions inside Libgcrypt, we redefine them to catch such errors. The usual difference between a public and an internal version is that the internal version use gpg_err_code_t and the public version gpg_error_t. */ #define gcry_check_version _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_control _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_set_allocation_handler _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_set_fatalerror_handler _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_set_gettext_handler _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_set_log_handler _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_set_outofcore_handler _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_set_progress_handler _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_err_code_from_errno _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_err_code_to_errno _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_err_make_from_errno _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_error_from_errno _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_strerror _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_strsource _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_malloc _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_malloc_secure _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_calloc _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_calloc_secure _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_realloc _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_strdup _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_xcalloc _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_xcalloc_secure _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_xmalloc _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_xmalloc_secure _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_xrealloc _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_xstrdup _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_free _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_is_secure _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_open _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_close _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_setkey _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_setiv _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_setctr _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_algo_info _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_algo_name _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_authenticate _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_checktag _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_gettag _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_ctl _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_decrypt _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_encrypt _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_get_algo_blklen _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_get_algo_keylen _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_info _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_map_name _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_cipher_mode_from_oid _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_algo_info _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_algo_name _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_ctl _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_decrypt _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_encrypt _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_genkey _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_get_keygrip _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_get_curve _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_get_param _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_get_nbits _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_map_name _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_sign _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_testkey _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_verify _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pubkey_get_sexp _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_ecc_get_algo_keylen _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_ecc_mul_point _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_hash_sign _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_hash_verify _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_pk_random_override_new _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_algo_info _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_algo_name _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_close _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_copy _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_ctl _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_enable _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_get _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_get_algo _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_get_algo_dlen _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_hash_buffer _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_hash_buffers _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_info _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_is_enabled _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_is_secure _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_map_name _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_open _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_read _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_extract _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_reset _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_setkey _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_write _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_md_debug _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_algo_info _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_algo_name _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_map_name _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_get_algo _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_get_algo_maclen _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_get_algo_keylen _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_open _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_close _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_setkey _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_setiv _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_write _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_read _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_verify _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mac_ctl _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_kdf_derive _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_kdf_open _gcry_USE_THE_UNDERSCORED_FUNCTION -#define gcry_kdf_ctl _gcry_USE_THE_UNDERSCORED_FUNCTION -#define gcry_kdf_iterator _gcry_USE_THE_UNDERSCORED_FUNCTION -#define gcry_kdf_compute_row _gcry_USE_THE_UNDERSCORED_FUNCTION +#define gcry_kdf_compute _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_kdf_final _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_kdf_close _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_prime_check _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_prime_generate _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_prime_group_generator _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_prime_release_factors _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_random_add_bytes _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_random_bytes _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_random_bytes_secure _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_randomize _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_create_nonce _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_ctx_release _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_alist _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_append _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_build _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_build_array _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_cadr _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_canon_len _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_car _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_cdr _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_cons _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_create _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_dump _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_find_token _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_length _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_new _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_nth _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_nth_buffer _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_nth_data _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_nth_mpi _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_nth_string _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_prepend _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_release _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_sprint _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_sscan _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_vlist _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_sexp_extract_param _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_add _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_add_ui _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_addm _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_aprint _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_clear_bit _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_clear_flag _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_clear_highbit _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_cmp _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_cmp_ui _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_copy _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_div _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_dump _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_gcd _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_get_flag _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_get_nbits _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_get_opaque _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_invm _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_mod _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_mul _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_mul_2exp _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_mul_ui _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_mulm _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_new _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_point_get _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_point_new _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_point_release _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_point_copy _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_point_set _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_point_snatch_get _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_point_snatch_set _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_powm _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_print _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_randomize _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_release _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_rshift _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_lshift _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_scan _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_set _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_set_bit _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_set_flag _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_set_highbit _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_set_opaque _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_set_ui _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_get_ui _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_snatch _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_snew _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_sub _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_sub_ui _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_subm _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_swap _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_test_bit _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_abs _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_add _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_sub _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_curve_point _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_dup _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_decode_point _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_get_affine _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_get_mpi _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_get_point _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_mul _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_new _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_set_mpi _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_set_point _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_is_neg _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_neg _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_set_opaque_copy _gcry_USE_THE_UNDERSCORED_FUNCTION #endif /*!_GCRY_INCLUDED_BY_VISIBILITY_C*/ #endif /*GCRY_VISIBILITY_H*/ diff --git a/tests/t-kdf.c b/tests/t-kdf.c index 6828aaf4..59559a4c 100644 --- a/tests/t-kdf.c +++ b/tests/t-kdf.c @@ -1,1502 +1,1557 @@ /* t-kdf.c - KDF regression tests * Copyright (C) 2011 Free Software Foundation, Inc. * * This file is part of Libgcrypt. * * Libgcrypt 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. * * Libgcrypt 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "stopwatch.h" #define PGM "t-kdf" #include "t-common.h" static int in_fips_mode; static void dummy_consumer (volatile char *buffer, size_t buflen) { (void)buffer; (void)buflen; } static void bench_s2k (unsigned long s2kcount) { gpg_error_t err; const char passphrase[] = "123456789abcdef0"; char keybuf[128/8]; unsigned int repetitions = 10; unsigned int count; const char *elapsed; int pass = 0; again: start_timer (); for (count = 0; count < repetitions; count++) { err = gcry_kdf_derive (passphrase, strlen (passphrase), GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "saltsalt", 8, s2kcount, sizeof keybuf, keybuf); if (err) die ("gcry_kdf_derive failed: %s\n", gpg_strerror (err)); dummy_consumer (keybuf, sizeof keybuf); } stop_timer (); elapsed = elapsed_time (repetitions); if (!pass++) { if (!atoi (elapsed)) { repetitions = 10000; goto again; } else if (atoi (elapsed) < 10) { repetitions = 100; goto again; } } printf ("%s\n", elapsed); } static void check_openpgp (void) { /* Test vectors manually created with gpg 1.4 derived code: In passphrase.c:hash_passpharse, add this code to the end of the function: ===8<=== printf ("{\n" " \""); for (i=0; i < pwlen; i++) { if (i && !(i%16)) printf ("\"\n \""); printf ("\\x%02x", ((const unsigned char *)pw)[i]); } printf ("\", %d,\n", pwlen); printf (" %s, %s,\n", s2k->mode == 0? "GCRY_KDF_SIMPLE_S2K": s2k->mode == 1? "GCRY_KDF_SALTED_S2K": s2k->mode == 3? "GCRY_KDF_ITERSALTED_S2K":"?", s2k->hash_algo == DIGEST_ALGO_MD5 ? "GCRY_MD_MD5" : s2k->hash_algo == DIGEST_ALGO_SHA1 ? "GCRY_MD_SHA1" : s2k->hash_algo == DIGEST_ALGO_RMD160? "GCRY_MD_RMD160" : s2k->hash_algo == DIGEST_ALGO_SHA256? "GCRY_MD_SHA256" : s2k->hash_algo == DIGEST_ALGO_SHA384? "GCRY_MD_SHA384" : s2k->hash_algo == DIGEST_ALGO_SHA512? "GCRY_MD_SHA512" : s2k->hash_algo == DIGEST_ALGO_SHA224? "GCRY_MD_SHA224" : "?"); if (s2k->mode == 0) printf (" NULL, 0,\n"); else { printf (" \""); for (i=0; i < 8; i++) printf ("\\x%02x", (unsigned int)s2k->salt[i]); printf ("\", %d,\n", 8); } if (s2k->mode == 3) printf (" %lu,\n", (unsigned long)S2K_DECODE_COUNT(s2k->count)); else printf (" 0,\n"); printf (" %d,\n", (int)dek->keylen); printf (" \""); for (i=0; i < dek->keylen; i++) { if (i && !(i%16)) printf ("\"\n \""); printf ("\\x%02x", ((unsigned char *)dek->key)[i]); } printf ("\"\n},\n"); ===>8=== Then prepare a file x.inp with utf8 encoding: ===8<=== 0 aes md5 1024 a 0 aes md5 1024 ab 0 aes md5 1024 abc 0 aes md5 1024 abcd 0 aes md5 1024 abcde 0 aes md5 1024 abcdef 0 aes md5 1024 abcdefg 0 aes md5 1024 abcdefgh 0 aes md5 1024 abcdefghi 0 aes md5 1024 abcdefghijklmno 0 aes md5 1024 abcdefghijklmnop 0 aes md5 1024 abcdefghijklmnopq 0 aes md5 1024 Long_sentence_used_as_passphrase 0 aes md5 1024 With_utf8_umlauts:äüÖß 0 aes sha1 1024 a 0 aes sha1 1024 ab 0 aes sha1 1024 abc 0 aes sha1 1024 abcd 0 aes sha1 1024 abcde 0 aes sha1 1024 abcdef 0 aes sha1 1024 abcdefg 0 aes sha1 1024 abcdefgh 0 aes sha1 1024 abcdefghi 0 aes sha1 1024 abcdefghijklmno 0 aes sha1 1024 abcdefghijklmnop 0 aes sha1 1024 abcdefghijklmnopq 0 aes sha1 1024 abcdefghijklmnopqr 0 aes sha1 1024 abcdefghijklmnopqrs 0 aes sha1 1024 abcdefghijklmnopqrst 0 aes sha1 1024 abcdefghijklmnopqrstu 0 aes sha1 1024 Long_sentence_used_as_passphrase 0 aes256 sha1 1024 Long_sentence_used_as_passphrase 0 aes sha1 1024 With_utf8_umlauts:äüÖß 3 aes sha1 1024 a 3 aes sha1 1024 ab 3 aes sha1 1024 abc 3 aes sha1 1024 abcd 3 aes sha1 1024 abcde 3 aes sha1 1024 abcdef 3 aes sha1 1024 abcdefg 3 aes sha1 1024 abcdefgh 3 aes sha1 1024 abcdefghi 3 aes sha1 1024 abcdefghijklmno 3 aes sha1 1024 abcdefghijklmnop 3 aes sha1 1024 abcdefghijklmnopq 3 aes sha1 1024 abcdefghijklmnopqr 3 aes sha1 1024 abcdefghijklmnopqrs 3 aes sha1 1024 abcdefghijklmnopqrst 3 aes sha1 1024 abcdefghijklmnopqrstu 3 aes sha1 1024 With_utf8_umlauts:äüÖß 3 aes sha1 1024 Long_sentence_used_as_passphrase 3 aes sha1 10240 Long_sentence_used_as_passphrase 3 aes sha1 102400 Long_sentence_used_as_passphrase 3 aes192 sha1 1024 a 3 aes192 sha1 1024 abcdefg 3 aes192 sha1 1024 abcdefghi 3 aes192 sha1 1024 abcdefghi 3 aes192 sha1 1024 Long_sentence_used_as_passphrase 3 aes256 sha1 1024 a 3 aes256 sha1 1024 abcdefg 3 aes256 sha1 1024 abcdefghi 3 aes256 sha1 1024 abcdefghi 3 aes256 sha1 1024 Long_sentence_used_as_passphrase 0 aes sha256 1024 Long_sentence_used_as_passphrase 1 aes sha256 1024 Long_sentence_used_as_passphrase 3 aes sha256 1024 Long_sentence_used_as_passphrase 3 aes sha256 10240 Long_sentence_used_as_passphrase 3 aes sha384 1024 Long_sentence_used_as_passphrase 3 aes sha512 1024 Long_sentence_used_as_passphrase 3 aes256 sha512 1024 Long_sentence_used_as_passphrase 3 3des sha512 1024 Long_sentence_used_as_passphrase ===>8=== and finally using a proper utf-8 enabled shell, run: cat x.inp | while read mode cipher digest count pass dummy; do \ ./gpg x.out */ static struct { const char *p; /* Passphrase. */ size_t plen; /* Length of P. */ int algo; int hashalgo; const char *salt; size_t saltlen; unsigned long c; /* Iterations. */ int dklen; /* Requested key length. */ const char *dk; /* Derived key. */ int disabled; } tv[] = { { "\x61", 1, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\x0c\xc1\x75\xb9\xc0\xf1\xb6\xa8\x31\xc3\x99\xe2\x69\x77\x26\x61" }, { "\x61\x62", 2, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\x18\x7e\xf4\x43\x61\x22\xd1\xcc\x2f\x40\xdc\x2b\x92\xf0\xeb\xa0" }, { "\x61\x62\x63", 3, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72" }, { "\x61\x62\x63\x64", 4, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\xe2\xfc\x71\x4c\x47\x27\xee\x93\x95\xf3\x24\xcd\x2e\x7f\x33\x1f" }, { "\x61\x62\x63\x64\x65", 5, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\xab\x56\xb4\xd9\x2b\x40\x71\x3a\xcc\x5a\xf8\x99\x85\xd4\xb7\x86" }, { "\x61\x62\x63\x64\x65\x66", 6, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\xe8\x0b\x50\x17\x09\x89\x50\xfc\x58\xaa\xd8\x3c\x8c\x14\x97\x8e" }, { "\x61\x62\x63\x64\x65\x66\x67", 7, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\x7a\xc6\x6c\x0f\x14\x8d\xe9\x51\x9b\x8b\xd2\x64\x31\x2c\x4d\x64" }, { "\x61\x62\x63\x64\x65\x66\x67\x68", 8, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\xe8\xdc\x40\x81\xb1\x34\x34\xb4\x51\x89\xa7\x20\xb7\x7b\x68\x18" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69", 9, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\x8a\xa9\x9b\x1f\x43\x9f\xf7\x12\x93\xe9\x53\x57\xba\xc6\xfd\x94" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f", 15, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\x8a\x73\x19\xdb\xf6\x54\x4a\x74\x22\xc9\xe2\x54\x52\x58\x0e\xa5" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70", 16, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\x1d\x64\xdc\xe2\x39\xc4\x43\x7b\x77\x36\x04\x1d\xb0\x89\xe1\xb9" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70" "\x71", 17, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\x9a\x8d\x98\x45\xa6\xb4\xd8\x2d\xfc\xb2\xc2\xe3\x51\x62\xc8\x30" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\x35\x2a\xf0\xfc\xdf\xe9\xbb\x62\x16\xfc\x99\x9d\x8d\x58\x05\xcb" }, { "\x57\x69\x74\x68\x5f\x75\x74\x66\x38\x5f\x75\x6d\x6c\x61\x75\x74" "\x73\x3a\xc3\xa4\xc3\xbc\xc3\x96\xc3\x9f", 26, GCRY_KDF_SIMPLE_S2K, GCRY_MD_MD5, NULL, 0, 0, 16, "\x21\xa4\xeb\xd8\xfd\xf0\x59\x25\xd1\x32\x31\xdb\xe7\xf2\x13\x5d" }, { "\x61", 1, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\x86\xf7\xe4\x37\xfa\xa5\xa7\xfc\xe1\x5d\x1d\xdc\xb9\xea\xea\xea" }, { "\x61\x62", 2, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\xda\x23\x61\x4e\x02\x46\x9a\x0d\x7c\x7b\xd1\xbd\xab\x5c\x9c\x47" }, { "\x61\x62\x63", 3, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\xa9\x99\x3e\x36\x47\x06\x81\x6a\xba\x3e\x25\x71\x78\x50\xc2\x6c" }, { "\x61\x62\x63\x64", 4, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\x81\xfe\x8b\xfe\x87\x57\x6c\x3e\xcb\x22\x42\x6f\x8e\x57\x84\x73" }, { "\x61\x62\x63\x64\x65", 5, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\x03\xde\x6c\x57\x0b\xfe\x24\xbf\xc3\x28\xcc\xd7\xca\x46\xb7\x6e" }, { "\x61\x62\x63\x64\x65\x66", 6, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\x1f\x8a\xc1\x0f\x23\xc5\xb5\xbc\x11\x67\xbd\xa8\x4b\x83\x3e\x5c" }, { "\x61\x62\x63\x64\x65\x66\x67", 7, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\x2f\xb5\xe1\x34\x19\xfc\x89\x24\x68\x65\xe7\xa3\x24\xf4\x76\xec" }, { "\x61\x62\x63\x64\x65\x66\x67\x68", 8, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\x42\x5a\xf1\x2a\x07\x43\x50\x2b\x32\x2e\x93\xa0\x15\xbc\xf8\x68" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69", 9, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\xc6\x3b\x19\xf1\xe4\xc8\xb5\xf7\x6b\x25\xc4\x9b\x8b\x87\xf5\x7d" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f", 15, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\x29\x38\xdc\xc2\xe3\xaa\x77\x98\x7c\x7e\x5d\x4a\x0f\x26\x96\x67" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70", 16, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\x14\xf3\x99\x52\x88\xac\xd1\x89\xe6\xe5\x0a\x7a\xf4\x7e\xe7\x09" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70" "\x71", 17, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\xd8\x3d\x62\x1f\xcd\x2d\x4d\x29\x85\x54\x70\x43\xa7\xa5\xfd\x4d" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70" "\x71\x72", 18, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\xe3\x81\xfe\x42\xc5\x7e\x48\xa0\x82\x17\x86\x41\xef\xfd\x1c\xb9" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70" "\x71\x72\x73", 19, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\x89\x3e\x69\xff\x01\x09\xf3\x45\x9c\x42\x43\x01\x3b\x3d\xe8\xb1" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70" "\x71\x72\x73\x74", 20, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\x14\xa2\x3a\xd7\x0f\x2a\x5d\xd7\x25\x57\x5d\xe6\xc4\x3e\x1c\xdd" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70" "\x71\x72\x73\x74\x75", 21, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\xec\xa9\x86\xb9\x5d\x58\x7f\x34\xd7\x1c\xa7\x75\x2a\x4e\x00\x10" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\x3e\x1b\x9a\x50\x7d\x6e\x9a\xd8\x93\x64\x96\x7a\x3f\xcb\x27\x3f" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 32, "\x3e\x1b\x9a\x50\x7d\x6e\x9a\xd8\x93\x64\x96\x7a\x3f\xcb\x27\x3f" "\xc3\x7b\x3a\xb2\xef\x4d\x68\xaa\x9c\xd7\xe4\x88\xee\xd1\x5e\x70" }, { "\x57\x69\x74\x68\x5f\x75\x74\x66\x38\x5f\x75\x6d\x6c\x61\x75\x74" "\x73\x3a\xc3\xa4\xc3\xbc\xc3\x96\xc3\x9f", 26, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA1, NULL, 0, 0, 16, "\xe0\x4e\x1e\xe3\xad\x0b\x49\x7c\x7a\x5f\x37\x3b\x4d\x90\x3c\x2e" }, { "\x61", 1, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x6d\x47\xe3\x68\x5d\x2c\x36\x16", 8, 1024, 16, "\x41\x9f\x48\x6e\xbf\xe6\xdd\x05\x9a\x72\x23\x17\x44\xd8\xd3\xf3" }, { "\x61\x62", 2, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x7c\x34\x78\xfb\x28\x2d\x25\xc7", 8, 1024, 16, "\x0a\x9d\x09\x06\x43\x3d\x4f\xf9\x87\xd6\xf7\x48\x90\xde\xd1\x1c" }, { "\x61\x62\x63", 3, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xc3\x16\x37\x2e\x27\xf6\x9f\x6f", 8, 1024, 16, "\xf8\x27\xa0\x07\xc6\xcb\xdd\xf1\xfe\x5c\x88\x3a\xfc\xcd\x84\x4d" }, { "\x61\x62\x63\x64", 4, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xf0\x0c\x73\x38\xb7\xc3\xd5\x14", 8, 1024, 16, "\x9b\x5f\x26\xba\x52\x3b\xcd\xd9\xa5\x2a\xef\x3c\x03\x4d\xd1\x52" }, { "\x61\x62\x63\x64\x65", 5, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xe1\x7d\xa2\x36\x09\x59\xee\xc5", 8, 1024, 16, "\x94\x9d\x5b\x1a\x5a\x66\x8c\xfa\x8f\x6f\x22\xaf\x8b\x60\x9f\xaf" }, { "\x61\x62\x63\x64\x65\x66", 6, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xaf\xa7\x0c\x68\xdf\x7e\xaa\x27", 8, 1024, 16, "\xe5\x38\xf4\x39\x62\x27\xcd\xcc\x91\x37\x7f\x1b\xdc\x58\x64\x27" }, { "\x61\x62\x63\x64\x65\x66\x67", 7, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x40\x57\xb2\x9d\x5f\xbb\x11\x4f", 8, 1024, 16, "\xad\xa2\x33\xd9\xdd\xe0\xfb\x94\x8e\xcc\xec\xcc\xb3\xa8\x3a\x9e" }, { "\x61\x62\x63\x64\x65\x66\x67\x68", 8, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x38\xf5\x65\xc5\x0f\x8c\x19\x61", 8, 1024, 16, "\xa0\xb0\x3e\x29\x76\xe6\x8f\xa0\xd8\x34\x8f\xa4\x2d\xfd\x65\xee" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69", 9, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xc3\xb7\x99\xcc\xda\x2d\x05\x7b", 8, 1024, 16, "\x27\x21\xc8\x99\x5f\xcf\x20\xeb\xf2\xd9\xff\x6a\x69\xff\xad\xe8" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f", 15, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x7d\xd8\x68\x8a\x1c\xc5\x47\x22", 8, 1024, 16, "\x0f\x96\x7a\x12\x23\x54\xf6\x92\x61\x67\x07\xb4\x68\x17\xb8\xaa" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70", 16, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x8a\x95\xd4\x88\x0b\xb8\xe9\x9d", 8, 1024, 16, "\xcc\xe4\xc8\x82\x53\x32\xf1\x93\x5a\x00\xd4\x7f\xd4\x46\xfa\x07" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70" "\x71", 17, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xb5\x22\x48\xa6\xc4\xad\x74\x67", 8, 1024, 16, "\x0c\xe3\xe0\xee\x3d\x8f\x35\xd2\x35\x14\x14\x29\x0c\xf1\xe3\x34" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70" "\x71\x72", 18, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xac\x9f\x04\x63\x83\x0e\x3c\x95", 8, 1024, 16, "\x49\x0a\x04\x68\xa8\x2a\x43\x6f\xb9\x73\x94\xb4\x85\x9a\xaa\x0e" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70" "\x71\x72\x73", 19, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x03\x6f\x60\x30\x3a\x19\x61\x0d", 8, 1024, 16, "\x15\xe5\x9b\xbf\x1c\xf0\xbe\x74\x95\x1a\xb2\xc4\xda\x09\xcd\x99" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70" "\x71\x72\x73\x74", 20, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x51\x40\xa5\x57\xf5\x28\xfd\x03", 8, 1024, 16, "\xa6\xf2\x7e\x6b\x30\x4d\x8d\x67\xd4\xa2\x7f\xa2\x57\x27\xab\x96" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70" "\x71\x72\x73\x74\x75", 21, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x4c\xf1\x10\x11\x04\x70\xd3\x6e", 8, 1024, 16, "\x2c\x50\x79\x8d\x83\x23\xac\xd6\x22\x29\x37\xaf\x15\x0d\xdd\x8f" }, { "\x57\x69\x74\x68\x5f\x75\x74\x66\x38\x5f\x75\x6d\x6c\x61\x75\x74" "\x73\x3a\xc3\xa4\xc3\xbc\xc3\x96\xc3\x9f", 26, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xfe\x3a\x25\xcb\x78\xef\xe1\x21", 8, 1024, 16, "\x2a\xb0\x53\x08\xf3\x2f\xd4\x6e\xeb\x01\x49\x5d\x87\xf6\x27\xf6" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x04\x97\xd0\x02\x6a\x44\x2d\xde", 8, 1024, 16, "\x57\xf5\x70\x41\xa0\x9b\x8c\x09\xca\x74\xa9\x22\xa5\x82\x2d\x17" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xdd\xf3\x31\x7c\xce\xf4\x81\x26", 8, 10240, 16, "\xc3\xdd\x01\x6d\xaf\xf6\x58\xc8\xd7\x79\xb4\x40\x00\xb5\xe8\x0b" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x95\xd6\x72\x4e\xfb\xe1\xc3\x1a", 8, 102400, 16, "\xf2\x3f\x36\x7f\xb4\x6a\xd0\x3a\x31\x9e\x65\x11\x8e\x2b\x99\x9b" }, { "\x61", 1, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x6d\x69\x15\x18\xe4\x13\x42\x82", 8, 1024, 24, "\x28\x0c\x7e\xf2\x31\xf6\x1c\x6b\x5c\xef\x6a\xd5\x22\x64\x97\x91" "\xe3\x5e\x37\xfd\x50\xe2\xfc\x6c" }, { "\x61\x62\x63\x64\x65\x66\x67", 7, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x9b\x76\x5e\x81\xde\x13\xdf\x15", 8, 1024, 24, "\x91\x1b\xa1\xc1\x7b\x4f\xc3\xb1\x80\x61\x26\x08\xbe\x53\xe6\x50" "\x40\x6f\x28\xed\xc6\xe6\x67\x55" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69", 9, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x7a\xac\xcc\x6e\x15\x56\xbd\xa1", 8, 1024, 24, "\xfa\x7e\x20\x07\xb6\x47\xb0\x09\x46\xb8\x38\xfb\xa1\xaf\xf7\x75" "\x2a\xfa\x77\x14\x06\x54\xcb\x34" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69", 9, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x1c\x68\xf8\xfb\x98\xf7\x8c\x39", 8, 1024, 24, "\xcb\x1e\x86\xf5\xe0\xe4\xfb\xbf\x71\x34\x99\x24\xf4\x39\x8c\xc2" "\x8e\x25\x1c\x4c\x96\x47\x22\xe8" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x10\xa9\x4e\xc1\xa5\xec\x17\x52", 8, 1024, 24, "\x0f\x83\xa2\x77\x92\xbb\xe4\x58\x68\xc5\xf2\x14\x6e\x6e\x2e\x6b" "\x98\x17\x70\x92\x07\x44\xe0\x51" }, { "\x61", 1, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xef\x8f\x37\x61\x8f\xab\xae\x4f", 8, 1024, 32, "\x6d\x65\xae\x86\x23\x91\x39\x98\xec\x1c\x23\x44\xb6\x0d\xad\x32" "\x54\x46\xc7\x23\x26\xbb\xdf\x4b\x54\x6e\xd4\xc2\xfa\xc6\x17\x17" }, { "\x61\x62\x63\x64\x65\x66\x67", 7, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xaa\xfb\xd9\x06\x7d\x7c\x40\xaf", 8, 1024, 32, "\x7d\x10\x54\x13\x3c\x43\x7a\xb3\x54\x1f\x38\xd4\x8f\x70\x0a\x09" "\xe2\xfa\xab\x97\x9a\x70\x16\xef\x66\x68\xca\x34\x2e\xce\xfa\x1f" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69", 9, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x58\x03\x4f\x56\x8b\x97\xd4\x98", 8, 1024, 32, "\xf7\x40\xb1\x25\x86\x0d\x35\x8f\x9f\x91\x2d\xce\x04\xee\x5a\x04" "\x9d\xbd\x44\x23\x4c\xa6\xbb\xab\xb0\xd0\x56\x82\xa9\xda\x47\x16" }, { "\x61\x62\x63\x64\x65\x66\x67\x68\x69", 9, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\x5d\x41\x3d\xa3\xa7\xfc\x5d\x0c", 8, 1024, 32, "\x4c\x7a\x86\xed\x81\x8a\x94\x99\x7d\x4a\xc4\xf7\x1c\xf8\x08\xdb" "\x09\x35\xd9\xa3\x2d\x22\xde\x32\x2d\x74\x38\xe5\xc8\xf2\x50\x6e" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA1, "\xca\xa7\xdc\x59\xce\x31\xe7\x49", 8, 1024, 32, "\x67\xe9\xd6\x29\x49\x1c\xb6\xa0\x85\xe8\xf9\x8b\x85\x47\x3a\x7e" "\xa7\xee\x89\x52\x6f\x19\x00\x53\x93\x07\x0a\x8b\xb9\xa8\x86\x94" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_SIMPLE_S2K, GCRY_MD_SHA256, NULL, 0, 0, 16, "\x88\x36\x78\x6b\xd9\x5a\x62\xff\x47\xd3\xfb\x79\xc9\x08\x70\x56" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_SALTED_S2K, GCRY_MD_SHA256, "\x05\x8b\xfe\x31\xaa\xf3\x29\x11", 8, 0, 16, "\xb2\x42\xfe\x5e\x09\x02\xd9\x62\xb9\x35\xf3\xa8\x43\x80\x9f\xb1" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA256, "\xd3\x4a\xea\xc9\x97\x1b\xcc\x83", 8, 1024, 16, "\x35\x37\x99\x62\x07\x26\x68\x23\x05\x47\xb2\xa0\x0b\x2b\x2b\x8d" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA256, "\x5e\x71\xbd\x00\x5f\x96\xc4\x23", 8, 10240, 16, "\xa1\x6a\xee\xba\xde\x73\x25\x25\xd1\xab\xa0\xc5\x7e\xc6\x39\xa7" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA384, "\xc3\x08\xeb\x17\x62\x08\x89\xef", 8, 1024, 16, "\x9b\x7f\x0c\x81\x6f\x71\x59\x9b\xd5\xf6\xbf\x3a\x86\x20\x16\x33" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA512, "\xe6\x7d\x13\x6b\x39\xe3\x44\x05", 8, 1024, 16, "\xc8\xcd\x4b\xa4\xf3\xf1\xd5\xb0\x59\x06\xf0\xbb\x89\x34\x6a\xad" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA512, "\xed\x7d\x30\x47\xe4\xc3\xf8\xb6", 8, 1024, 32, "\x89\x7a\xef\x70\x97\xe7\x10\xdb\x75\xcc\x20\x22\xab\x7b\xf3\x05" "\x4b\xb6\x2e\x17\x11\x9f\xd6\xeb\xbf\xdf\x4d\x70\x59\xf0\xf9\xe5" }, { "\x4c\x6f\x6e\x67\x5f\x73\x65\x6e\x74\x65\x6e\x63\x65\x5f\x75\x73" "\x65\x64\x5f\x61\x73\x5f\x70\x61\x73\x73\x70\x68\x72\x61\x73\x65", 32, GCRY_KDF_ITERSALTED_S2K, GCRY_MD_SHA512, "\xbb\x1a\x45\x30\x68\x62\x6d\x63", 8, 1024, 24, "\xde\x5c\xb8\xd5\x75\xf6\xad\x69\x5b\xc9\xf6\x2f\xba\xeb\xfb\x36" "\x34\xf2\xb8\xee\x3b\x37\x21\xb7" } }; int tvidx; gpg_error_t err; unsigned char outbuf[32]; int i; for (tvidx=0; tvidx < DIM(tv); tvidx++) { if (tv[tvidx].disabled) continue; /* MD5 isn't supported in fips mode */ if (in_fips_mode && tv[tvidx].hashalgo == GCRY_MD_MD5) continue; if (verbose) fprintf (stderr, "checking S2K test vector %d\n", tvidx); assert (tv[tvidx].dklen <= sizeof outbuf); err = gcry_kdf_derive (tv[tvidx].p, tv[tvidx].plen, tv[tvidx].algo, tv[tvidx].hashalgo, tv[tvidx].salt, tv[tvidx].saltlen, tv[tvidx].c, tv[tvidx].dklen, outbuf); if (err) fail ("s2k test %d failed: %s\n", tvidx, gpg_strerror (err)); else if (memcmp (outbuf, tv[tvidx].dk, tv[tvidx].dklen)) { fail ("s2k test %d failed: mismatch\n", tvidx); fputs ("got:", stderr); for (i=0; i < tv[tvidx].dklen; i++) fprintf (stderr, " %02x", outbuf[i]); putc ('\n', stderr); } } } static void check_pbkdf2 (void) { /* Test vectors are from RFC-6070. */ static struct { const char *p; /* Passphrase. */ size_t plen; /* Length of P. */ const char *salt; size_t saltlen; int hashalgo; unsigned long c; /* Iterations. */ int dklen; /* Requested key length. */ const char *dk; /* Derived key. */ int disabled; } tv[] = { { "password", 8, "salt", 4, GCRY_MD_SHA1, 1, 20, "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9" "\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6" }, { "password", 8, "salt", 4, GCRY_MD_SHA1, 2, 20, "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e" "\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57" }, { "password", 8, "salt", 4, GCRY_MD_SHA1, 4096, 20, "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad" "\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1" }, { "password", 8, "salt", 4, GCRY_MD_SHA1, 16777216, 20, "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94" "\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84", 1 /* This test takes too long. */ }, { "passwordPASSWORDpassword", 24, "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, GCRY_MD_SHA1, 4096, 25, "\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8" "\xd8\x36\x62\xc0\xe4\x4a\x8b\x29\x1a\x96" "\x4c\xf2\xf0\x70\x38" }, { "pass\0word", 9, "sa\0lt", 5, GCRY_MD_SHA1, 4096, 16, "\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37" "\xd7\xf0\x34\x25\xe0\xc3" }, { /* empty password test, not in RFC-6070 */ "", 0, "salt", 4, GCRY_MD_SHA1, 2, 20, "\x13\x3a\x4c\xe8\x37\xb4\xd2\x52\x1e\xe2" "\xbf\x03\xe1\x1c\x71\xca\x79\x4e\x07\x97" }, { "password", 8, "salt", 4, GCRY_MD_GOSTR3411_CP, 1, 32, "\x73\x14\xe7\xc0\x4f\xb2\xe6\x62\xc5\x43\x67\x42\x53\xf6\x8b\xd0" "\xb7\x34\x45\xd0\x7f\x24\x1b\xed\x87\x28\x82\xda\x21\x66\x2d\x58" }, { "password", 8, "salt", 4, GCRY_MD_GOSTR3411_CP, 2, 32, "\x99\x0d\xfa\x2b\xd9\x65\x63\x9b\xa4\x8b\x07\xb7\x92\x77\x5d\xf7" "\x9f\x2d\xb3\x4f\xef\x25\xf2\x74\x37\x88\x72\xfe\xd7\xed\x1b\xb3" }, { "password", 8, "salt", 4, GCRY_MD_GOSTR3411_CP, 4096, 32, "\x1f\x18\x29\xa9\x4b\xdf\xf5\xbe\x10\xd0\xae\xb3\x6a\xf4\x98\xe7" "\xa9\x74\x67\xf3\xb3\x11\x16\xa5\xa7\xc1\xaf\xff\x9d\xea\xda\xfe" }, /* { -- takes too long (4-5 min) to calculate "password", 8, "salt", 4, GCRY_MD_GOSTR3411_CP, 16777216, 32, "\xa5\x7a\xe5\xa6\x08\x83\x96\xd1\x20\x85\x0c\x5c\x09\xde\x0a\x52" "\x51\x00\x93\x8a\x59\xb1\xb5\xc3\xf7\x81\x09\x10\xd0\x5f\xcd\x97" }, */ { "passwordPASSWORDpassword", 24, "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, GCRY_MD_GOSTR3411_CP, 4096, 40, "\x78\x83\x58\xc6\x9c\xb2\xdb\xe2\x51\xa7\xbb\x17\xd5\xf4\x24\x1f" "\x26\x5a\x79\x2a\x35\xbe\xcd\xe8\xd5\x6f\x32\x6b\x49\xc8\x50\x47" "\xb7\x63\x8a\xcb\x47\x64\xb1\xfd" }, { "pass\0word", 9, "sa\0lt", 5, GCRY_MD_GOSTR3411_CP, 4096, 20, "\x43\xe0\x6c\x55\x90\xb0\x8c\x02\x25\x24" "\x23\x73\x12\x7e\xdf\x9c\x8e\x9c\x32\x91" }, { "password", 8, "salt", 4, GCRY_MD_STRIBOG512, 1, 64, "\x64\x77\x0a\xf7\xf7\x48\xc3\xb1\xc9\xac\x83\x1d\xbc\xfd\x85\xc2" "\x61\x11\xb3\x0a\x8a\x65\x7d\xdc\x30\x56\xb8\x0c\xa7\x3e\x04\x0d" "\x28\x54\xfd\x36\x81\x1f\x6d\x82\x5c\xc4\xab\x66\xec\x0a\x68\xa4" "\x90\xa9\xe5\xcf\x51\x56\xb3\xa2\xb7\xee\xcd\xdb\xf9\xa1\x6b\x47" }, { "password", 8, "salt", 4, GCRY_MD_STRIBOG512, 2, 64, "\x5a\x58\x5b\xaf\xdf\xbb\x6e\x88\x30\xd6\xd6\x8a\xa3\xb4\x3a\xc0" "\x0d\x2e\x4a\xeb\xce\x01\xc9\xb3\x1c\x2c\xae\xd5\x6f\x02\x36\xd4" "\xd3\x4b\x2b\x8f\xbd\x2c\x4e\x89\xd5\x4d\x46\xf5\x0e\x47\xd4\x5b" "\xba\xc3\x01\x57\x17\x43\x11\x9e\x8d\x3c\x42\xba\x66\xd3\x48\xde" }, { "password", 8, "salt", 4, GCRY_MD_STRIBOG512, 4096, 64, "\xe5\x2d\xeb\x9a\x2d\x2a\xaf\xf4\xe2\xac\x9d\x47\xa4\x1f\x34\xc2" "\x03\x76\x59\x1c\x67\x80\x7f\x04\x77\xe3\x25\x49\xdc\x34\x1b\xc7" "\x86\x7c\x09\x84\x1b\x6d\x58\xe2\x9d\x03\x47\xc9\x96\x30\x1d\x55" "\xdf\x0d\x34\xe4\x7c\xf6\x8f\x4e\x3c\x2c\xda\xf1\xd9\xab\x86\xc3" }, /* { -- takes toooo long "password", 8, "salt", 4, GCRY_MD_STRIBOG512, 16777216, 64, "\x49\xe4\x84\x3b\xba\x76\xe3\x00\xaf\xe2\x4c\x4d\x23\xdc\x73\x92" "\xde\xf1\x2f\x2c\x0e\x24\x41\x72\x36\x7c\xd7\x0a\x89\x82\xac\x36" "\x1a\xdb\x60\x1c\x7e\x2a\x31\x4e\x8c\xb7\xb1\xe9\xdf\x84\x0e\x36" "\xab\x56\x15\xbe\x5d\x74\x2b\x6c\xf2\x03\xfb\x55\xfd\xc4\x80\x71" }, */ { "passwordPASSWORDpassword", 24, "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, GCRY_MD_STRIBOG512, 4096, 100, "\xb2\xd8\xf1\x24\x5f\xc4\xd2\x92\x74\x80\x20\x57\xe4\xb5\x4e\x0a" "\x07\x53\xaa\x22\xfc\x53\x76\x0b\x30\x1c\xf0\x08\x67\x9e\x58\xfe" "\x4b\xee\x9a\xdd\xca\xe9\x9b\xa2\xb0\xb2\x0f\x43\x1a\x9c\x5e\x50" "\xf3\x95\xc8\x93\x87\xd0\x94\x5a\xed\xec\xa6\xeb\x40\x15\xdf\xc2" "\xbd\x24\x21\xee\x9b\xb7\x11\x83\xba\x88\x2c\xee\xbf\xef\x25\x9f" "\x33\xf9\xe2\x7d\xc6\x17\x8c\xb8\x9d\xc3\x74\x28\xcf\x9c\xc5\x2a" "\x2b\xaa\x2d\x3a" }, { "pass\0word", 9, "sa\0lt", 5, GCRY_MD_STRIBOG512, 4096, 64, "\x50\xdf\x06\x28\x85\xb6\x98\x01\xa3\xc1\x02\x48\xeb\x0a\x27\xab" "\x6e\x52\x2f\xfe\xb2\x0c\x99\x1c\x66\x0f\x00\x14\x75\xd7\x3a\x4e" "\x16\x7f\x78\x2c\x18\xe9\x7e\x92\x97\x6d\x9c\x1d\x97\x08\x31\xea" "\x78\xcc\xb8\x79\xf6\x70\x68\xcd\xac\x19\x10\x74\x08\x44\xe8\x30" } }; int tvidx; gpg_error_t err; unsigned char outbuf[100]; int i; for (tvidx=0; tvidx < DIM(tv); tvidx++) { if (tv[tvidx].disabled) continue; if (verbose) fprintf (stderr, "checking PBKDF2 test vector %d algo %d\n", tvidx, tv[tvidx].hashalgo); assert (tv[tvidx].dklen <= sizeof outbuf); err = gcry_kdf_derive (tv[tvidx].p, tv[tvidx].plen, GCRY_KDF_PBKDF2, tv[tvidx].hashalgo, tv[tvidx].salt, tv[tvidx].saltlen, tv[tvidx].c, tv[tvidx].dklen, outbuf); if (in_fips_mode && tvidx > 6) { if (!err) fail ("pbkdf2 test %d unexpectedly passed in FIPS mode: %s\n", tvidx, gpg_strerror (err)); continue; } if (err) { if (in_fips_mode && tv[tvidx].plen < 14) { if (verbose) fprintf (stderr, " shorter key (%u) rejected correctly in fips mode\n", (unsigned int)tv[tvidx].plen); } else fail ("pbkdf2 test %d failed: %s\n", tvidx, gpg_strerror (err)); } else if (memcmp (outbuf, tv[tvidx].dk, tv[tvidx].dklen)) { fail ("pbkdf2 test %d failed: mismatch\n", tvidx); fputs ("got:", stderr); for (i=0; i < tv[tvidx].dklen; i++) fprintf (stderr, " %02x", outbuf[i]); putc ('\n', stderr); } } } static void check_scrypt (void) { /* Test vectors are from draft-josefsson-scrypt-kdf-01. */ static struct { const char *p; /* Passphrase. */ size_t plen; /* Length of P. */ const char *salt; size_t saltlen; int parm_n; /* CPU/memory cost. */ int parm_r; /* blocksize */ unsigned long parm_p; /* parallelization. */ int dklen; /* Requested key length. */ const char *dk; /* Derived key. */ int disabled; } tv[] = { { "", 0, "", 0, 16, 1, 1, 64, "\x77\xd6\x57\x62\x38\x65\x7b\x20\x3b\x19\xca\x42\xc1\x8a\x04\x97" "\xf1\x6b\x48\x44\xe3\x07\x4a\xe8\xdf\xdf\xfa\x3f\xed\xe2\x14\x42" "\xfc\xd0\x06\x9d\xed\x09\x48\xf8\x32\x6a\x75\x3a\x0f\xc8\x1f\x17" "\xe8\xd3\xe0\xfb\x2e\x0d\x36\x28\xcf\x35\xe2\x0c\x38\xd1\x89\x06" }, { "password", 8, "NaCl", 4, 1024, 8, 16, 64, "\xfd\xba\xbe\x1c\x9d\x34\x72\x00\x78\x56\xe7\x19\x0d\x01\xe9\xfe" "\x7c\x6a\xd7\xcb\xc8\x23\x78\x30\xe7\x73\x76\x63\x4b\x37\x31\x62" "\x2e\xaf\x30\xd9\x2e\x22\xa3\x88\x6f\xf1\x09\x27\x9d\x98\x30\xda" "\xc7\x27\xaf\xb9\x4a\x83\xee\x6d\x83\x60\xcb\xdf\xa2\xcc\x06\x40" }, { "pleaseletmein", 13, "SodiumChloride", 14, 16384, 8, 1, 64, "\x70\x23\xbd\xcb\x3a\xfd\x73\x48\x46\x1c\x06\xcd\x81\xfd\x38\xeb" "\xfd\xa8\xfb\xba\x90\x4f\x8e\x3e\xa9\xb5\x43\xf6\x54\x5d\xa1\xf2" "\xd5\x43\x29\x55\x61\x3f\x0f\xcf\x62\xd4\x97\x05\x24\x2a\x9a\xf9" "\xe6\x1e\x85\xdc\x0d\x65\x1e\x40\xdf\xcf\x01\x7b\x45\x57\x58\x87" }, { "pleaseletmein", 13, "SodiumChloride", 14, 1048576, 8, 1, 64, "\x21\x01\xcb\x9b\x6a\x51\x1a\xae\xad\xdb\xbe\x09\xcf\x70\xf8\x81" "\xec\x56\x8d\x57\x4a\x2f\xfd\x4d\xab\xe5\xee\x98\x20\xad\xaa\x47" "\x8e\x56\xfd\x8f\x4b\xa5\xd0\x9f\xfa\x1c\x6d\x92\x7c\x40\xf4\xc3" "\x37\x30\x40\x49\xe8\xa9\x52\xfb\xcb\xf4\x5c\x6f\xa7\x7a\x41\xa4", 2 /* Only in debug mode. */ } }; int tvidx; gpg_error_t err; unsigned char outbuf[64]; int i; for (tvidx=0; tvidx < DIM(tv); tvidx++) { if (tv[tvidx].disabled && !(tv[tvidx].disabled == 2 && debug)) continue; if (verbose) fprintf (stderr, "checking SCRYPT test vector %d\n", tvidx); assert (tv[tvidx].dklen <= sizeof outbuf); err = gcry_kdf_derive (tv[tvidx].p, tv[tvidx].plen, tv[tvidx].parm_r == 1 ? 41 : GCRY_KDF_SCRYPT, tv[tvidx].parm_n, tv[tvidx].salt, tv[tvidx].saltlen, tv[tvidx].parm_p, tv[tvidx].dklen, outbuf); if (err) { if (in_fips_mode && tv[tvidx].plen < 14) { if (verbose) fprintf (stderr, " shorter key (%u) rejected correctly in fips mode\n", (unsigned int)tv[tvidx].plen); } else fail ("scrypt test %d failed: %s\n", tvidx, gpg_strerror (err)); } else if (memcmp (outbuf, tv[tvidx].dk, tv[tvidx].dklen)) { fail ("scrypt test %d failed: mismatch\n", tvidx); fputs ("got:", stderr); for (i=0; i < tv[tvidx].dklen; i++) fprintf (stderr, " %02x", outbuf[i]); putc ('\n', stderr); } } } #ifdef HAVE_PTHREAD #include +#define MAX_THREADS 8 + +struct user_defined_threads_ctx +{ + int oldest_thread_idx; + int next_thread_idx; + int num_threads_running; + pthread_attr_t attr; + pthread_t thread[MAX_THREADS]; + struct job_thread_param { + void (*job) (void *work_priv); + void *priv; + } work[MAX_THREADS]; +}; + static void * -start_thread (void *arg) +job_thread (void *p) { - struct gcry_kdf_pt_head *k = arg; - gpg_err_code_t ec; + struct job_thread_param *param = p; + param->job (param->priv); + pthread_exit (NULL); +} + +static int +pthread_jobs_launch_job (void *jobs_context, + void (*job) (void *work_priv), void *work_priv) +{ + struct user_defined_threads_ctx *ctx = jobs_context; + + if (ctx->num_threads_running + && ctx->next_thread_idx == ctx->oldest_thread_idx) + { + assert (ctx->num_threads_running == MAX_THREADS); + /* thread limit reached, join a thread */ + pthread_join (ctx->thread[ctx->oldest_thread_idx], NULL); + ctx->oldest_thread_idx = (ctx->oldest_thread_idx + 1) % MAX_THREADS; + ctx->num_threads_running--; + } - ec = gcry_kdf_compute_segment (k->h, k); - pthread_exit ((void *)ec); - return NULL; + ctx->work[ctx->next_thread_idx].job = job; + ctx->work[ctx->next_thread_idx].priv = work_priv; + pthread_create (&ctx->thread[ctx->next_thread_idx], &ctx->attr, + job_thread, &ctx->work[ctx->next_thread_idx]); + ctx->next_thread_idx = (ctx->next_thread_idx + 1) % MAX_THREADS; + ctx->num_threads_running++; + return 0; } -#define MAX_THREAD 8 +static int +wait_all_jobs_completion (void *jobs_context) +{ + struct user_defined_threads_ctx *ctx = jobs_context; + int i, idx; + + for (i = 0; i < ctx->num_threads_running; i++) + { + idx = (ctx->oldest_thread_idx + i) % MAX_THREADS; + pthread_join (ctx->thread[idx], NULL); + } + + /* reset context for next round of parallel work */ + ctx->num_threads_running = 0; + ctx->oldest_thread_idx = 0; + ctx->next_thread_idx = 0; + + return 0; +} #endif static gcry_error_t my_kdf_derive (int parallel, int algo, int subalgo, const unsigned long *params, unsigned int paramslen, const unsigned char *pass, size_t passlen, const unsigned char *salt, size_t saltlen, const unsigned char *key, size_t keylen, const unsigned char *ad, size_t adlen, size_t outlen, unsigned char *out) { gcry_error_t err; gcry_kdf_hd_t hd; #ifdef HAVE_PTHREAD - pthread_attr_t attr; - pthread_t thr[MAX_THREAD]; - int i; + struct user_defined_threads_ctx jobs_context; #endif err = gcry_kdf_open (&hd, algo, subalgo, params, paramslen, pass, passlen, salt, saltlen, key, keylen, ad, adlen); if (err) return err; #ifdef HAVE_PTHREAD if (parallel) { - memset (thr, 0, sizeof (thr)); + memset (&jobs_context, 0, sizeof (struct user_defined_threads_ctx)); - if (pthread_attr_init (&attr)) + if (pthread_attr_init (&jobs_context.attr)) { + err = gpg_error_from_syserror (); gcry_kdf_close (hd); - return gpg_error_from_syserror (); + return err; } - if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE)) + if (pthread_attr_setdetachstate (&jobs_context.attr, + PTHREAD_CREATE_JOINABLE)) { - pthread_attr_destroy (&attr); + err = gpg_error_from_syserror (); + pthread_attr_destroy (&jobs_context.attr); gcry_kdf_close (hd); - return gpg_error_from_syserror (); + return err; } } #endif - i = 0; - while (1) + if (!parallel) + err = gcry_kdf_compute (hd, NULL); + else { - int action; - struct gcry_kdf_pt_head *t; - - err = gcry_kdf_iterator (hd, &action, &t); - if (err) - break; + struct gcry_kdf_thread_ops ops = { + &jobs_context, + pthread_jobs_launch_job, + wait_all_jobs_completion + }; - if (action == 0) - /* DONE */ - break; - else if (action == 1) - { /* request to ask creating a thread */ -#ifdef HAVE_PTHREAD - if (parallel) - { - pthread_create (&thr[i], &attr, start_thread, (void *)t); - t->u.user_data = &thr[i]; - i++; - } - else -#endif - t->u.ec = gcry_kdf_compute_segment (t->h, t); - } - else if (action == 2) - { /* request to ask joining a thread */ -#ifdef HAVE_PTHREAD - if (parallel) - { - pthread_t *user_data = t->u.user_data; - void *retval; - - pthread_join (*user_data, &retval); - t->u.ec = (gpg_err_code_t)retval; - memset (user_data, 0, sizeof (pthread_t)); - --i; - } -#endif - } + err = gcry_kdf_compute (hd, &ops); } #ifdef HAVE_PTHREAD if (parallel) - pthread_attr_destroy (&attr); + pthread_attr_destroy (&jobs_context. attr); #endif if (!err) err = gcry_kdf_final (hd, outlen, out); gcry_kdf_close (hd); return err; } static void check_argon2 (void) { gcry_error_t err; - const unsigned long param[5] = { 32, 3, 16, 4, 4 }; + const unsigned long param[4] = { 32, 3, 32, 4 }; const unsigned char pass[32] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; const unsigned char salt[16] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, }; const unsigned char key[8] = { 3, 3, 3, 3, 3, 3, 3, 3 }; const unsigned char ad[12] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; unsigned char out[32]; - unsigned char expected[32] = { - 0xf8, 0x7c, 0x95, 0x96, 0xbd, 0xbf, 0x75, 0x0b, - 0xfb, 0x35, 0x3a, 0x89, 0x70, 0xe5, 0x44, 0x1a, - 0x70, 0x24, 0x3e, 0xb4, 0x90, 0x30, 0xdf, 0xe2, - 0x74, 0xd9, 0xad, 0x4e, 0x37, 0x0e, 0x38, 0x9b + unsigned char expected[3][32] = { + { /* GCRY_KDF_ARGON2D */ + 0x51, 0x2b, 0x39, 0x1b, 0x6f, 0x11, 0x62, 0x97, + 0x53, 0x71, 0xd3, 0x09, 0x19, 0x73, 0x42, 0x94, + 0xf8, 0x68, 0xe3, 0xbe, 0x39, 0x84, 0xf3, 0xc1, + 0xa1, 0x3a, 0x4d, 0xb9, 0xfa, 0xbe, 0x4a, 0xcb + }, + { /* GCRY_KDF_ARGON2I */ + 0xc8, 0x14, 0xd9, 0xd1, 0xdc, 0x7f, 0x37, 0xaa, + 0x13, 0xf0, 0xd7, 0x7f, 0x24, 0x94, 0xbd, 0xa1, + 0xc8, 0xde, 0x6b, 0x01, 0x6d, 0xd3, 0x88, 0xd2, + 0x99, 0x52, 0xa4, 0xc4, 0x67, 0x2b, 0x6c, 0xe8 + }, + { /* GCRY_KDF_ARGON2ID */ + 0x0d, 0x64, 0x0d, 0xf5, 0x8d, 0x78, 0x76, 0x6c, + 0x08, 0xc0, 0x37, 0xa3, 0x4a, 0x8b, 0x53, 0xc9, + 0xd0, 0x1e, 0xf0, 0x45, 0x2d, 0x75, 0xb6, 0x5e, + 0xb5, 0x25, 0x20, 0xe9, 0x6b, 0x01, 0xe6, 0x59 + } }; int i; + int subalgo = GCRY_KDF_ARGON2D; + int count = 0; + + again: + + if (verbose) + fprintf (stderr, "checking ARGON2 test vector %d\n", count); err = my_kdf_derive (0, - GCRY_KDF_ARGON2, GCRY_KDF_ARGON2ID, param, 4, + GCRY_KDF_ARGON2, subalgo, param, 4, pass, 32, salt, 16, key, 8, ad, 12, 32, out); if (err) fail ("argon2 test %d failed: %s\n", 0, gpg_strerror (err)); - else if (memcmp (out, expected, 32)) + else if (memcmp (out, expected[count], 32)) { fail ("argon2 test %d failed: mismatch\n", 0); fputs ("got:", stderr); for (i=0; i < 32; i++) fprintf (stderr, " %02x", out[i]); putc ('\n', stderr); } #ifdef HAVE_PTHREAD err = my_kdf_derive (1, - GCRY_KDF_ARGON2, GCRY_KDF_ARGON2ID, param, 5, + GCRY_KDF_ARGON2, subalgo, param, 4, pass, 32, salt, 16, key, 8, ad, 12, 32, out); if (err) fail ("argon2 test %d failed: %s\n", 1, gpg_strerror (err)); - else if (memcmp (out, expected, 32)) + else if (memcmp (out, expected[count], 32)) { fail ("argon2 test %d failed: mismatch\n", 1); fputs ("got:", stderr); for (i=0; i < 32; i++) fprintf (stderr, " %02x", out[i]); putc ('\n', stderr); } #endif + + /* Next algo */ + if (subalgo == GCRY_KDF_ARGON2D) + subalgo = GCRY_KDF_ARGON2I; + else if (subalgo == GCRY_KDF_ARGON2I) + subalgo = GCRY_KDF_ARGON2ID; + + count++; + if (count < 3) + goto again; } int main (int argc, char **argv) { int last_argc = -1; unsigned long s2kcount = 0; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) { fputs ("usage: t-kdf [options]" "Options:\n" " --verbose print timinigs etc.\n" " --debug flyswatter\n" " --s2k print the time needed for S2K\n", stdout); exit (0); } else if (!strcmp (*argv, "--verbose")) { verbose++; argc--; argv++; } else if (!strcmp (*argv, "--debug")) { verbose += 2; debug++; argc--; argv++; } else if (!strcmp (*argv, "--s2k")) { s2kcount = 1; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) die ("unknown option '%s'\n", *argv); } if (s2kcount) { if (argc != 1) die ("usage: t-kdf --s2k S2KCOUNT\n"); s2kcount = strtoul (*argv, NULL, 10); if (!s2kcount) die ("t-kdf: S2KCOUNT must be positive\n"); } if (!gcry_check_version (GCRYPT_VERSION)) die ("version mismatch\n"); if (gcry_fips_mode_active ()) in_fips_mode = 1; if (!in_fips_mode) xgcry_control ((GCRYCTL_DISABLE_SECMEM, 0)); xgcry_control ((GCRYCTL_INITIALIZATION_FINISHED, 0)); if (debug) xgcry_control ((GCRYCTL_SET_DEBUG_FLAGS, 1u, 0)); if (s2kcount) bench_s2k (s2kcount); else { check_openpgp (); check_pbkdf2 (); check_scrypt (); -#if 0 check_argon2 (); -#endif } return error_count ? 1 : 0; }