1 #ifndef __LIBSSH2_LIBGCRYPT_H 2 #define __LIBSSH2_LIBGCRYPT_H 3 /* 4 * Copyright (C) 2008, 2009, 2010 Simon Josefsson 5 * Copyright (C) 2006, 2007, The Written Word, Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, 9 * with or without modification, are permitted provided 10 * that the following conditions are met: 11 * 12 * Redistributions of source code must retain the above 13 * copyright notice, this list of conditions and the 14 * following disclaimer. 15 * 16 * Redistributions in binary form must reproduce the above 17 * copyright notice, this list of conditions and the following 18 * disclaimer in the documentation and/or other materials 19 * provided with the distribution. 20 * 21 * Neither the name of the copyright holder nor the names 22 * of any other contributors may be used to endorse or 23 * promote products derived from this software without 24 * specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 27 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 28 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 36 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 38 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 39 * OF SUCH DAMAGE. 40 */ 41 42 #include <gcrypt.h> 43 44 #define LIBSSH2_MD5 1 45 46 #define LIBSSH2_HMAC_RIPEMD 1 47 #define LIBSSH2_HMAC_SHA256 1 48 #define LIBSSH2_HMAC_SHA512 1 49 50 #define LIBSSH2_AES 1 51 #define LIBSSH2_AES_CTR 1 52 #define LIBSSH2_BLOWFISH 1 53 #define LIBSSH2_RC4 1 54 #define LIBSSH2_CAST 1 55 #define LIBSSH2_3DES 1 56 57 #define LIBSSH2_RSA 1 58 #define LIBSSH2_DSA 1 59 #define LIBSSH2_ECDSA 0 60 #define LIBSSH2_ED25519 0 61 62 #define MD5_DIGEST_LENGTH 16 63 #define SHA_DIGEST_LENGTH 20 64 #define SHA256_DIGEST_LENGTH 32 65 #define SHA384_DIGEST_LENGTH 48 66 #define SHA512_DIGEST_LENGTH 64 67 68 #define EC_MAX_POINT_LEN ((528 * 2 / 8) + 1) 69 70 #define _libssh2_random(buf, len) \ 71 (gcry_randomize ((buf), (len), GCRY_STRONG_RANDOM), 1) 72 73 #define libssh2_prepare_iovec(vec, len) /* Empty. */ 74 75 #define libssh2_sha1_ctx gcry_md_hd_t 76 77 /* returns 0 in case of failure */ 78 #define libssh2_sha1_init(ctx) \ 79 (GPG_ERR_NO_ERROR == gcry_md_open(ctx, GCRY_MD_SHA1, 0)) 80 #define libssh2_sha1_update(ctx, data, len) \ 81 gcry_md_write(ctx, (unsigned char *) data, len) 82 #define libssh2_sha1_final(ctx, out) \ 83 memcpy(out, gcry_md_read(ctx, 0), SHA_DIGEST_LENGTH), gcry_md_close(ctx) 84 #define libssh2_sha1(message, len, out) \ 85 gcry_md_hash_buffer(GCRY_MD_SHA1, out, message, len) 86 87 #define libssh2_sha256_ctx gcry_md_hd_t 88 89 #define libssh2_sha256_init(ctx) \ 90 (GPG_ERR_NO_ERROR == gcry_md_open(ctx, GCRY_MD_SHA256, 0)) 91 #define libssh2_sha256_update(ctx, data, len) \ 92 gcry_md_write(ctx, (unsigned char *) data, len) 93 #define libssh2_sha256_final(ctx, out) \ 94 memcpy(out, gcry_md_read(ctx, 0), SHA256_DIGEST_LENGTH), gcry_md_close(ctx) 95 #define libssh2_sha256(message, len, out) \ 96 gcry_md_hash_buffer(GCRY_MD_SHA256, out, message, len) 97 98 #define libssh2_sha384_ctx gcry_md_hd_t 99 100 #define libssh2_sha384_init(ctx) \ 101 (GPG_ERR_NO_ERROR == gcry_md_open(ctx, GCRY_MD_SHA384, 0)) 102 #define libssh2_sha384_update(ctx, data, len) \ 103 gcry_md_write(ctx, (unsigned char *) data, len) 104 #define libssh2_sha384_final(ctx, out) \ 105 memcpy(out, gcry_md_read(ctx, 0), SHA384_DIGEST_LENGTH), gcry_md_close(ctx) 106 #define libssh2_sha384(message, len, out) \ 107 gcry_md_hash_buffer(GCRY_MD_SHA384, out, message, len) 108 109 #define libssh2_sha512_ctx gcry_md_hd_t 110 111 #define libssh2_sha512_init(ctx) \ 112 (GPG_ERR_NO_ERROR == gcry_md_open(ctx, GCRY_MD_SHA512, 0)) 113 #define libssh2_sha512_update(ctx, data, len) \ 114 gcry_md_write(ctx, (unsigned char *) data, len) 115 #define libssh2_sha512_final(ctx, out) \ 116 memcpy(out, gcry_md_read(ctx, 0), SHA512_DIGEST_LENGTH), gcry_md_close(ctx) 117 #define libssh2_sha512(message, len, out) \ 118 gcry_md_hash_buffer(GCRY_MD_SHA512, out, message, len) 119 120 #define libssh2_md5_ctx gcry_md_hd_t 121 122 /* returns 0 in case of failure */ 123 #define libssh2_md5_init(ctx) \ 124 (GPG_ERR_NO_ERROR == gcry_md_open(ctx, GCRY_MD_MD5, 0)) 125 126 #define libssh2_md5_update(ctx, data, len) \ 127 gcry_md_write(ctx, (unsigned char *) data, len) 128 #define libssh2_md5_final(ctx, out) \ 129 memcpy(out, gcry_md_read(ctx, 0), MD5_DIGEST_LENGTH), gcry_md_close(ctx) 130 #define libssh2_md5(message, len, out) \ 131 gcry_md_hash_buffer(GCRY_MD_MD5, out, message, len) 132 133 #define libssh2_hmac_ctx gcry_md_hd_t 134 #define libssh2_hmac_ctx_init(ctx) 135 #define libssh2_hmac_sha1_init(ctx, key, keylen) \ 136 gcry_md_open(ctx, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC), \ 137 gcry_md_setkey(*ctx, key, keylen) 138 #define libssh2_hmac_md5_init(ctx, key, keylen) \ 139 gcry_md_open(ctx, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC), \ 140 gcry_md_setkey(*ctx, key, keylen) 141 #define libssh2_hmac_ripemd160_init(ctx, key, keylen) \ 142 gcry_md_open(ctx, GCRY_MD_RMD160, GCRY_MD_FLAG_HMAC), \ 143 gcry_md_setkey(*ctx, key, keylen) 144 #define libssh2_hmac_sha256_init(ctx, key, keylen) \ 145 gcry_md_open(ctx, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC), \ 146 gcry_md_setkey(*ctx, key, keylen) 147 #define libssh2_hmac_sha512_init(ctx, key, keylen) \ 148 gcry_md_open(ctx, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC), \ 149 gcry_md_setkey(*ctx, key, keylen) 150 #define libssh2_hmac_update(ctx, data, datalen) \ 151 gcry_md_write(ctx, (unsigned char *) data, datalen) 152 #define libssh2_hmac_final(ctx, data) \ 153 memcpy(data, gcry_md_read(ctx, 0), \ 154 gcry_md_get_algo_dlen(gcry_md_get_algo(ctx))) 155 #define libssh2_hmac_cleanup(ctx) gcry_md_close (*ctx); 156 157 #define libssh2_crypto_init() gcry_control (GCRYCTL_DISABLE_SECMEM) 158 #define libssh2_crypto_exit() 159 160 #define libssh2_rsa_ctx struct gcry_sexp 161 162 #define _libssh2_rsa_free(rsactx) gcry_sexp_release (rsactx) 163 164 #define libssh2_dsa_ctx struct gcry_sexp 165 166 #define _libssh2_dsa_free(dsactx) gcry_sexp_release (dsactx) 167 168 #if LIBSSH2_ECDSA 169 #else 170 #define _libssh2_ec_key void 171 #endif 172 173 #define _libssh2_cipher_type(name) int name 174 #define _libssh2_cipher_ctx gcry_cipher_hd_t 175 176 #define _libssh2_gcry_ciphermode(c,m) ((c << 8) | m) 177 #define _libssh2_gcry_cipher(c) (c >> 8) 178 #define _libssh2_gcry_mode(m) (m & 0xFF) 179 180 #define _libssh2_cipher_aes256ctr \ 181 _libssh2_gcry_ciphermode(GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CTR) 182 #define _libssh2_cipher_aes192ctr \ 183 _libssh2_gcry_ciphermode(GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CTR) 184 #define _libssh2_cipher_aes128ctr \ 185 _libssh2_gcry_ciphermode(GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CTR) 186 #define _libssh2_cipher_aes256 \ 187 _libssh2_gcry_ciphermode(GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC) 188 #define _libssh2_cipher_aes192 \ 189 _libssh2_gcry_ciphermode(GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CBC) 190 #define _libssh2_cipher_aes128 \ 191 _libssh2_gcry_ciphermode(GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC) 192 #define _libssh2_cipher_blowfish \ 193 _libssh2_gcry_ciphermode(GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CBC) 194 #define _libssh2_cipher_arcfour \ 195 _libssh2_gcry_ciphermode(GCRY_CIPHER_ARCFOUR, GCRY_CIPHER_MODE_STREAM) 196 #define _libssh2_cipher_cast5 \ 197 _libssh2_gcry_ciphermode(GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC) 198 #define _libssh2_cipher_3des \ 199 _libssh2_gcry_ciphermode(GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_CBC) 200 201 202 #define _libssh2_cipher_dtor(ctx) gcry_cipher_close(*(ctx)) 203 204 #define _libssh2_bn struct gcry_mpi 205 #define _libssh2_bn_ctx int 206 #define _libssh2_bn_ctx_new() 0 207 #define _libssh2_bn_ctx_free(bnctx) ((void)0) 208 #define _libssh2_bn_init() gcry_mpi_new(0) 209 #define _libssh2_bn_init_from_bin() NULL /* because gcry_mpi_scan() creates a 210 new bignum */ 211 #define _libssh2_bn_set_word(bn, val) gcry_mpi_set_ui(bn, val) 212 #define _libssh2_bn_from_bin(bn, len, val) \ 213 gcry_mpi_scan(&((bn)), GCRYMPI_FMT_USG, val, len, NULL) 214 #define _libssh2_bn_to_bin(bn, val) \ 215 gcry_mpi_print(GCRYMPI_FMT_USG, val, _libssh2_bn_bytes(bn), NULL, bn) 216 #define _libssh2_bn_bytes(bn) \ 217 (gcry_mpi_get_nbits (bn) / 8 + \ 218 ((gcry_mpi_get_nbits (bn) % 8 == 0) ? 0 : 1)) 219 #define _libssh2_bn_bits(bn) gcry_mpi_get_nbits (bn) 220 #define _libssh2_bn_free(bn) gcry_mpi_release(bn) 221 222 #define _libssh2_dh_ctx struct gcry_mpi * 223 #define libssh2_dh_init(dhctx) _libssh2_dh_init(dhctx) 224 #define libssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ 225 _libssh2_dh_key_pair(dhctx, public, g, p, group_order) 226 #define libssh2_dh_secret(dhctx, secret, f, p, bnctx) \ 227 _libssh2_dh_secret(dhctx, secret, f, p) 228 #define libssh2_dh_dtor(dhctx) _libssh2_dh_dtor(dhctx) 229 extern void _libssh2_dh_init(_libssh2_dh_ctx *dhctx); 230 extern int _libssh2_dh_key_pair(_libssh2_dh_ctx *dhctx, _libssh2_bn *public, 231 _libssh2_bn *g, _libssh2_bn *p, 232 int group_order); 233 extern int _libssh2_dh_secret(_libssh2_dh_ctx *dhctx, _libssh2_bn *secret, 234 _libssh2_bn *f, _libssh2_bn *p); 235 extern void _libssh2_dh_dtor(_libssh2_dh_ctx *dhctx); 236 237 #endif /* __LIBSSH2_LIBGCRYPT_H */ 238