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