libssh  0.9.6
The SSH library
libcrypto.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 LIBCRYPTO_H_
22 #define LIBCRYPTO_H_
23 
24 #include "config.h"
25 
26 #ifdef HAVE_LIBCRYPTO
27 
28 #include <openssl/dsa.h>
29 #include <openssl/rsa.h>
30 #include <openssl/sha.h>
31 #include <openssl/md5.h>
32 #include <openssl/hmac.h>
33 #include <openssl/evp.h>
34 #include <openssl/crypto.h>
35 
36 typedef EVP_MD_CTX* SHACTX;
37 typedef EVP_MD_CTX* SHA256CTX;
38 typedef EVP_MD_CTX* SHA384CTX;
39 typedef EVP_MD_CTX* SHA512CTX;
40 typedef EVP_MD_CTX* MD5CTX;
41 typedef HMAC_CTX* HMACCTX;
42 #ifdef HAVE_ECC
43 typedef EVP_MD_CTX *EVPCTX;
44 #else
45 typedef void *EVPCTX;
46 #endif
47 
48 #define SHA_DIGEST_LEN SHA_DIGEST_LENGTH
49 #define SHA256_DIGEST_LEN SHA256_DIGEST_LENGTH
50 #define SHA384_DIGEST_LEN SHA384_DIGEST_LENGTH
51 #define SHA512_DIGEST_LEN SHA512_DIGEST_LENGTH
52 #ifdef MD5_DIGEST_LEN
53  #undef MD5_DIGEST_LEN
54 #endif
55 #define MD5_DIGEST_LEN MD5_DIGEST_LENGTH
56 
57 #ifdef HAVE_OPENSSL_ECC
58 #define EVP_DIGEST_LEN EVP_MAX_MD_SIZE
59 #endif
60 
61 #include <openssl/bn.h>
62 #include <openssl/opensslv.h>
63 #define OPENSSL_0_9_7b 0x0090702fL
64 #if (OPENSSL_VERSION_NUMBER <= OPENSSL_0_9_7b)
65 #define BROKEN_AES_CTR
66 #endif
67 typedef BIGNUM* bignum;
68 typedef const BIGNUM* const_bignum;
69 typedef BN_CTX* bignum_CTX;
70 
71 #define bignum_new() BN_new()
72 #define bignum_safe_free(num) do { \
73  if ((num) != NULL) { \
74  BN_clear_free((num)); \
75  (num)=NULL; \
76  } \
77  } while(0)
78 #define bignum_set_word(bn,n) BN_set_word(bn,n)
79 #define bignum_bin2bn(data, datalen, dest) \
80  do { \
81  (*dest) = BN_new(); \
82  if ((*dest) != NULL) { \
83  BN_bin2bn(data,datalen,(*dest)); \
84  } \
85  } while(0)
86 #define bignum_bn2dec(num) BN_bn2dec(num)
87 #define bignum_dec2bn(data, bn) BN_dec2bn(bn, data)
88 #define bignum_hex2bn(data, bn) BN_hex2bn(bn, data)
89 #define bignum_bn2hex(num, dest) (*dest)=(unsigned char *)BN_bn2hex(num)
90 #define bignum_rand(rnd, bits) BN_rand(rnd, bits, 0, 1)
91 #define bignum_rand_range(rnd, max) BN_rand_range(rnd, max)
92 #define bignum_ctx_new() BN_CTX_new()
93 #define bignum_ctx_free(num) BN_CTX_free(num)
94 #define bignum_ctx_invalid(ctx) ((ctx) == NULL)
95 #define bignum_mod_exp(dest,generator,exp,modulo,ctx) BN_mod_exp(dest,generator,exp,modulo,ctx)
96 #define bignum_add(dest, a, b) BN_add(dest, a, b)
97 #define bignum_sub(dest, a, b) BN_sub(dest, a, b)
98 #define bignum_mod(dest, a, b, ctx) BN_mod(dest, a, b, ctx)
99 #define bignum_num_bytes(num) (size_t)BN_num_bytes(num)
100 #define bignum_num_bits(num) (size_t)BN_num_bits(num)
101 #define bignum_is_bit_set(num,bit) BN_is_bit_set(num, (int)bit)
102 #define bignum_bn2bin(num,len, ptr) BN_bn2bin(num, ptr)
103 #define bignum_cmp(num1,num2) BN_cmp(num1,num2)
104 #define bignum_rshift1(dest, src) BN_rshift1(dest, src)
105 #define bignum_dup(orig, dest) do { \
106  if (*(dest) == NULL) { \
107  *(dest) = BN_dup(orig); \
108  } else { \
109  BN_copy(*(dest), orig); \
110  } \
111  } while(0)
112 
113 
114 /* Returns true if the OpenSSL is operating in FIPS mode */
115 #ifdef HAVE_OPENSSL_FIPS_MODE
116 #define ssh_fips_mode() (FIPS_mode() != 0)
117 #else
118 #define ssh_fips_mode() false
119 #endif
120 
121 #endif /* HAVE_LIBCRYPTO */
122 
123 #endif /* LIBCRYPTO_H_ */