libssh  0.11.0
The SSH library
Loading...
Searching...
No Matches
libgcrypt.h
1/*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2009 by Aris Adamantiadis
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef LIBGCRYPT_H_
22#define LIBGCRYPT_H_
23
24#include "config.h"
25
26#ifdef HAVE_LIBGCRYPT
27
28#include <gcrypt.h>
29typedef gcry_md_hd_t SHACTX;
30typedef gcry_md_hd_t SHA256CTX;
31typedef gcry_md_hd_t SHA384CTX;
32typedef gcry_md_hd_t SHA512CTX;
33typedef gcry_md_hd_t MD5CTX;
34typedef gcry_md_hd_t HMACCTX;
35#define SHA_DIGEST_LENGTH 20
36#define SHA_DIGEST_LEN SHA_DIGEST_LENGTH
37#define MD5_DIGEST_LEN 16
38#define SHA256_DIGEST_LENGTH 32
39#define SHA256_DIGEST_LEN SHA256_DIGEST_LENGTH
40#define SHA384_DIGEST_LENGTH 48
41#define SHA384_DIGEST_LEN SHA384_DIGEST_LENGTH
42#define SHA512_DIGEST_LENGTH 64
43#define SHA512_DIGEST_LEN SHA512_DIGEST_LENGTH
44
45#ifndef EVP_MAX_MD_SIZE
46#define EVP_MAX_MD_SIZE 64
47#endif
48
49#define EVP_DIGEST_LEN EVP_MAX_MD_SIZE
50
51#define ssh_crypto_free(x) gcry_free(x)
52
53typedef gcry_mpi_t bignum;
54typedef const struct gcry_mpi *const_bignum;
55typedef void* bignum_CTX;
56
57/* Constants for curves. */
58#define NID_gcrypt_nistp256 0
59#define NID_gcrypt_nistp384 1
60#define NID_gcrypt_nistp521 2
61
62/* missing gcrypt functions */
63int ssh_gcry_dec2bn(bignum *bn, const char *data);
64char *ssh_gcry_bn2dec(bignum bn);
65int ssh_gcry_rand_range(bignum rnd, bignum max);
66
67#define bignum_new() gcry_mpi_new(0)
68#define bignum_safe_free(num) do { \
69 if ((num) != NULL) { \
70 gcry_mpi_release((num)); \
71 (num)=NULL; \
72 } \
73 } while (0)
74#define bignum_free(num) gcry_mpi_release(num)
75#define bignum_ctx_new() NULL
76#define bignum_ctx_free(ctx) do {(ctx) = NULL;} while(0)
77#define bignum_ctx_invalid(ctx) (ctx != NULL)
78#define bignum_set_word(bn,n) (gcry_mpi_set_ui(bn,n)!=NULL ? 1 : 0)
79#define bignum_bin2bn(data,datalen,dest) gcry_mpi_scan(dest,GCRYMPI_FMT_USG,data,datalen,NULL)
80#define bignum_bn2dec(num) ssh_gcry_bn2dec(num)
81#define bignum_dec2bn(num, data) ssh_gcry_dec2bn(data, num)
82
83#define bignum_bn2hex(num, data) \
84 gcry_mpi_aprint(GCRYMPI_FMT_HEX, data, NULL, (const gcry_mpi_t)num)
85
86#define bignum_hex2bn(data, num) (gcry_mpi_scan(num,GCRYMPI_FMT_HEX,data,0,NULL)==0?1:0)
87#define bignum_rand(num,bits) 1,gcry_mpi_randomize(num,bits,GCRY_STRONG_RANDOM),gcry_mpi_set_bit(num,bits-1),gcry_mpi_set_bit(num,0)
88#define bignum_mod_exp(dest,generator,exp,modulo, ctx) 1,gcry_mpi_powm(dest,generator,exp,modulo)
89#define bignum_num_bits(num) gcry_mpi_get_nbits(num)
90#define bignum_num_bytes(num) ((gcry_mpi_get_nbits(num)+7)/8)
91#define bignum_is_bit_set(num,bit) gcry_mpi_test_bit(num,bit)
92#define bignum_bn2bin(num,datalen,data) gcry_mpi_print(GCRYMPI_FMT_USG,data,datalen,NULL,num)
93#define bignum_cmp(num1,num2) gcry_mpi_cmp(num1,num2)
94#define bignum_rshift1(dest, src) gcry_mpi_rshift (dest, src, 1)
95#define bignum_add(dst, a, b) gcry_mpi_add(dst, a, b)
96#define bignum_sub(dst, a, b) gcry_mpi_sub(dst, a, b)
97#define bignum_mod(dst, a, b, ctx) 1,gcry_mpi_mod(dst, a, b)
98#define bignum_rand_range(rnd, max) ssh_gcry_rand_range(rnd, max);
99#define bignum_dup(orig, dest) do { \
100 if (*(dest) == NULL) { \
101 *(dest) = gcry_mpi_copy(orig); \
102 } else { \
103 gcry_mpi_set(*(dest), orig); \
104 } \
105 } while(0)
106/* Helper functions for data conversions. */
107
108#ifdef __cplusplus
109extern "C" {
110#endif
111
112/* Extract an MPI from the given s-expression SEXP named NAME which is
113 encoded using INFORMAT and store it in a newly allocated ssh_string
114 encoded using OUTFORMAT. */
115ssh_string ssh_sexp_extract_mpi(const gcry_sexp_t sexp,
116 const char *name,
117 enum gcry_mpi_format informat,
118 enum gcry_mpi_format outformat);
119
120#define ssh_fips_mode() false
121
122#ifdef __cplusplus
123}
124#endif
125
126#endif /* HAVE_LIBGCRYPT */
127
128#endif /* LIBGCRYPT_H_ */