libssh2

Форк
0
/
libgcrypt.c 
863 строки · 21.6 Кб
1
/* Copyright (C) Simon Josefsson
2
 * Copyright (C) The Written Word, Inc.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms,
6
 * with or without modification, are permitted provided
7
 * that the following conditions are met:
8
 *
9
 *   Redistributions of source code must retain the above
10
 *   copyright notice, this list of conditions and the
11
 *   following disclaimer.
12
 *
13
 *   Redistributions in binary form must reproduce the above
14
 *   copyright notice, this list of conditions and the following
15
 *   disclaimer in the documentation and/or other materials
16
 *   provided with the distribution.
17
 *
18
 *   Neither the name of the copyright holder nor the names
19
 *   of any other contributors may be used to endorse or
20
 *   promote products derived from this software without
21
 *   specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
36
 * OF SUCH DAMAGE.
37
 *
38
 * SPDX-License-Identifier: BSD-3-Clause
39
 */
40

41
#ifdef LIBSSH2_CRYPTO_C /* Compile this via crypto.c */
42

43
int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx)
44
{
45
    *ctx = NULL;
46
    return 1;
47
}
48

49
#if LIBSSH2_MD5
50
int _libssh2_hmac_md5_init(libssh2_hmac_ctx *ctx,
51
                           void *key, size_t keylen)
52
{
53
    gcry_error_t err;
54
    err = gcry_md_open(ctx, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
55
    if(gcry_err_code(err) != GPG_ERR_NO_ERROR)
56
        return 0;
57
    err = gcry_md_setkey(*ctx, key, keylen);
58
    if(gcry_err_code(err) != GPG_ERR_NO_ERROR)
59
        return 0;
60
    return 1;
61
}
62
#endif
63

64
#if LIBSSH2_HMAC_RIPEMD
65
int _libssh2_hmac_ripemd160_init(libssh2_hmac_ctx *ctx,
66
                                 void *key, size_t keylen)
67
{
68
    gcry_error_t err;
69
    err = gcry_md_open(ctx, GCRY_MD_RMD160, GCRY_MD_FLAG_HMAC);
70
    if(gcry_err_code(err) != GPG_ERR_NO_ERROR)
71
        return 0;
72
    err = gcry_md_setkey(*ctx, key, keylen);
73
    if(gcry_err_code(err) != GPG_ERR_NO_ERROR)
74
        return 0;
75
    return 1;
76
}
77
#endif
78

79
int _libssh2_hmac_sha1_init(libssh2_hmac_ctx *ctx,
80
                            void *key, size_t keylen)
81
{
82
    gcry_error_t err;
83
    err = gcry_md_open(ctx, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
84
    if(gcry_err_code(err) != GPG_ERR_NO_ERROR)
85
        return 0;
86
    err = gcry_md_setkey(*ctx, key, keylen);
87
    if(gcry_err_code(err) != GPG_ERR_NO_ERROR)
88
        return 0;
89
    return 1;
90
}
91

92
int _libssh2_hmac_sha256_init(libssh2_hmac_ctx *ctx,
93
                              void *key, size_t keylen)
94
{
95
    gcry_error_t err;
96
    err = gcry_md_open(ctx, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
97
    if(gcry_err_code(err) != GPG_ERR_NO_ERROR)
98
        return 0;
99
    err = gcry_md_setkey(*ctx, key, keylen);
100
    if(gcry_err_code(err) != GPG_ERR_NO_ERROR)
101
        return 0;
102
    return 1;
103
}
104

105
int _libssh2_hmac_sha512_init(libssh2_hmac_ctx *ctx,
106
                              void *key, size_t keylen)
107
{
108
    gcry_error_t err;
109
    err = gcry_md_open(ctx, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC);
110
    if(gcry_err_code(err) != GPG_ERR_NO_ERROR)
111
        return 0;
112
    err = gcry_md_setkey(*ctx, key, keylen);
113
    if(gcry_err_code(err) != GPG_ERR_NO_ERROR)
114
        return 0;
115
    return 1;
116
}
117

118
int _libssh2_hmac_update(libssh2_hmac_ctx *ctx,
119
                         const void *data, size_t datalen)
120
{
121
    gcry_md_write(*ctx, data, datalen);
122
    return 1;
123
}
124

125
int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *data)
126
{
127
    unsigned char *res = gcry_md_read(*ctx, 0);
128

129
    if(!res)
130
        return 0;
131

132
    memcpy(data, res, gcry_md_get_algo_dlen(gcry_md_get_algo(*ctx)));
133

134
    return 1;
135
}
136

137
void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx)
138
{
139
    gcry_md_close(*ctx);
140
}
141

142
#if LIBSSH2_RSA
143
int
144
_libssh2_rsa_new(libssh2_rsa_ctx ** rsa,
145
                 const unsigned char *edata,
146
                 unsigned long elen,
147
                 const unsigned char *ndata,
148
                 unsigned long nlen,
149
                 const unsigned char *ddata,
150
                 unsigned long dlen,
151
                 const unsigned char *pdata,
152
                 unsigned long plen,
153
                 const unsigned char *qdata,
154
                 unsigned long qlen,
155
                 const unsigned char *e1data,
156
                 unsigned long e1len,
157
                 const unsigned char *e2data,
158
                 unsigned long e2len,
159
                 const unsigned char *coeffdata, unsigned long coefflen)
160
{
161
    int rc;
162

163
    (void)e1data;
164
    (void)e1len;
165
    (void)e2data;
166
    (void)e2len;
167

168
    if(ddata) {
169
        rc = gcry_sexp_build(rsa, NULL,
170
                 "(private-key(rsa(n%b)(e%b)(d%b)(q%b)(p%b)(u%b)))",
171
                 nlen, ndata, elen, edata, dlen, ddata, plen, pdata,
172
                 qlen, qdata, coefflen, coeffdata);
173
    }
174
    else {
175
        rc = gcry_sexp_build(rsa, NULL, "(public-key(rsa(n%b)(e%b)))",
176
                             nlen, ndata, elen, edata);
177
    }
178
    if(rc) {
179
        *rsa = NULL;
180
        return -1;
181
    }
182

183
    return 0;
184
}
185

186
#if LIBSSH2_RSA_SHA1
187
int
188
_libssh2_rsa_sha1_verify(libssh2_rsa_ctx * rsa,
189
                         const unsigned char *sig,
190
                         size_t sig_len,
191
                         const unsigned char *m, size_t m_len)
192
{
193
    unsigned char hash[SHA_DIGEST_LENGTH];
194
    gcry_sexp_t s_sig, s_hash;
195
    int rc = -1;
196

197
    if(libssh2_sha1(m, m_len, hash)) {
198
        return -1;
199
    }
200

201
    rc = gcry_sexp_build(&s_hash, NULL,
202
                         "(data (flags pkcs1) (hash sha1 %b))",
203
                         SHA_DIGEST_LENGTH, hash);
204
    if(rc) {
205
        return -1;
206
    }
207

208
    rc = gcry_sexp_build(&s_sig, NULL, "(sig-val(rsa(s %b)))", sig_len, sig);
209
    if(rc) {
210
        gcry_sexp_release(s_hash);
211
        return -1;
212
    }
213

214
    rc = gcry_pk_verify(s_sig, s_hash, rsa);
215
    gcry_sexp_release(s_sig);
216
    gcry_sexp_release(s_hash);
217

218
    return (rc == 0) ? 0 : -1;
219
}
220
#endif
221
#endif
222

223
#if LIBSSH2_DSA
224
int
225
_libssh2_dsa_new(libssh2_dsa_ctx ** dsactx,
226
                 const unsigned char *p,
227
                 unsigned long p_len,
228
                 const unsigned char *q,
229
                 unsigned long q_len,
230
                 const unsigned char *g,
231
                 unsigned long g_len,
232
                 const unsigned char *y,
233
                 unsigned long y_len,
234
                 const unsigned char *x, unsigned long x_len)
235
{
236
    int rc;
237

238
    if(x_len) {
239
        rc = gcry_sexp_build(dsactx, NULL,
240
                 "(private-key(dsa(p%b)(q%b)(g%b)(y%b)(x%b)))",
241
                  p_len, p, q_len, q, g_len, g, y_len, y, x_len, x);
242
    }
243
    else {
244
        rc = gcry_sexp_build(dsactx, NULL,
245
                             "(public-key(dsa(p%b)(q%b)(g%b)(y%b)))",
246
                             p_len, p, q_len, q, g_len, g, y_len, y);
247
    }
248

249
    if(rc) {
250
        *dsactx = NULL;
251
        return -1;
252
    }
253

254
    return 0;
255
}
256
#endif
257

258
#if LIBSSH2_RSA
259
int
260
_libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx ** rsa,
261
                                    LIBSSH2_SESSION * session,
262
                                    const char *filedata, size_t filedata_len,
263
                                    unsigned const char *passphrase)
264
{
265
    (void)rsa;
266
    (void)filedata;
267
    (void)filedata_len;
268
    (void)passphrase;
269

270
    return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
271
                          "Unable to extract private key from memory: "
272
                          "Method unimplemented in libgcrypt backend");
273
}
274

275
int
276
_libssh2_rsa_new_private(libssh2_rsa_ctx ** rsa,
277
                         LIBSSH2_SESSION * session,
278
                         const char *filename, unsigned const char *passphrase)
279
{
280
    FILE *fp;
281
    unsigned char *data, *save_data;
282
    size_t datalen;
283
    int ret;
284
    unsigned char *n, *e, *d, *p, *q, *e1, *e2, *coeff;
285
    unsigned int nlen, elen, dlen, plen, qlen, e1len, e2len, coefflen;
286

287
    fp = fopen(filename, FOPEN_READTEXT);
288
    if(!fp) {
289
        return -1;
290
    }
291

292
    ret = _libssh2_pem_parse(session,
293
                             "-----BEGIN RSA PRIVATE KEY-----",
294
                             "-----END RSA PRIVATE KEY-----",
295
                             passphrase,
296
                             fp, &data, &datalen);
297
    fclose(fp);
298
    if(ret) {
299
        return -1;
300
    }
301

302
    save_data = data;
303

304
    if(_libssh2_pem_decode_sequence(&data, &datalen)) {
305
        ret = -1;
306
        goto fail;
307
    }
308

309
    /* First read Version field (should be 0). */
310
    ret = _libssh2_pem_decode_integer(&data, &datalen, &n, &nlen);
311
    if(ret || (nlen != 1 && *n != '\0')) {
312
        ret = -1;
313
        goto fail;
314
    }
315

316
    ret = _libssh2_pem_decode_integer(&data, &datalen, &n, &nlen);
317
    if(ret) {
318
        ret = -1;
319
        goto fail;
320
    }
321

322
    ret = _libssh2_pem_decode_integer(&data, &datalen, &e, &elen);
323
    if(ret) {
324
        ret = -1;
325
        goto fail;
326
    }
327

328
    ret = _libssh2_pem_decode_integer(&data, &datalen, &d, &dlen);
329
    if(ret) {
330
        ret = -1;
331
        goto fail;
332
    }
333

334
    ret = _libssh2_pem_decode_integer(&data, &datalen, &p, &plen);
335
    if(ret) {
336
        ret = -1;
337
        goto fail;
338
    }
339

340
    ret = _libssh2_pem_decode_integer(&data, &datalen, &q, &qlen);
341
    if(ret) {
342
        ret = -1;
343
        goto fail;
344
    }
345

346
    ret = _libssh2_pem_decode_integer(&data, &datalen, &e1, &e1len);
347
    if(ret) {
348
        ret = -1;
349
        goto fail;
350
    }
351

352
    ret = _libssh2_pem_decode_integer(&data, &datalen, &e2, &e2len);
353
    if(ret) {
354
        ret = -1;
355
        goto fail;
356
    }
357

358
    ret = _libssh2_pem_decode_integer(&data, &datalen, &coeff, &coefflen);
359
    if(ret) {
360
        ret = -1;
361
        goto fail;
362
    }
363

364
    if(_libssh2_rsa_new(rsa, e, elen, n, nlen, d, dlen, p, plen,
365
                        q, qlen, e1, e1len, e2, e2len, coeff, coefflen)) {
366
        ret = -1;
367
        goto fail;
368
    }
369

370
    ret = 0;
371

372
fail:
373
    LIBSSH2_FREE(session, save_data);
374
    return ret;
375
}
376
#endif
377

378
#if LIBSSH2_DSA
379
int
380
_libssh2_dsa_new_private_frommemory(libssh2_dsa_ctx ** dsa,
381
                                    LIBSSH2_SESSION * session,
382
                                    const char *filedata, size_t filedata_len,
383
                                    unsigned const char *passphrase)
384
{
385
    (void)dsa;
386
    (void)filedata;
387
    (void)filedata_len;
388
    (void)passphrase;
389

390
    return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
391
                          "Unable to extract private key from memory: "
392
                          "Method unimplemented in libgcrypt backend");
393
}
394

395
int
396
_libssh2_dsa_new_private(libssh2_dsa_ctx ** dsa,
397
                         LIBSSH2_SESSION * session,
398
                         const char *filename, unsigned const char *passphrase)
399
{
400
    FILE *fp;
401
    unsigned char *data, *save_data;
402
    size_t datalen;
403
    int ret;
404
    unsigned char *p, *q, *g, *y, *x;
405
    unsigned int plen, qlen, glen, ylen, xlen;
406

407
    fp = fopen(filename, FOPEN_READTEXT);
408
    if(!fp) {
409
        return -1;
410
    }
411

412
    ret = _libssh2_pem_parse(session,
413
                             "-----BEGIN DSA PRIVATE KEY-----",
414
                             "-----END DSA PRIVATE KEY-----",
415
                             passphrase,
416
                             fp, &data, &datalen);
417
    fclose(fp);
418
    if(ret) {
419
        return -1;
420
    }
421

422
    save_data = data;
423

424
    if(_libssh2_pem_decode_sequence(&data, &datalen)) {
425
        ret = -1;
426
        goto fail;
427
    }
428

429
    /* First read Version field (should be 0). */
430
    ret = _libssh2_pem_decode_integer(&data, &datalen, &p, &plen);
431
    if(ret || (plen != 1 && *p != '\0')) {
432
        ret = -1;
433
        goto fail;
434
    }
435

436
    ret = _libssh2_pem_decode_integer(&data, &datalen, &p, &plen);
437
    if(ret) {
438
        ret = -1;
439
        goto fail;
440
    }
441

442
    ret = _libssh2_pem_decode_integer(&data, &datalen, &q, &qlen);
443
    if(ret) {
444
        ret = -1;
445
        goto fail;
446
    }
447

448
    ret = _libssh2_pem_decode_integer(&data, &datalen, &g, &glen);
449
    if(ret) {
450
        ret = -1;
451
        goto fail;
452
    }
453

454
    ret = _libssh2_pem_decode_integer(&data, &datalen, &y, &ylen);
455
    if(ret) {
456
        ret = -1;
457
        goto fail;
458
    }
459

460
    ret = _libssh2_pem_decode_integer(&data, &datalen, &x, &xlen);
461
    if(ret) {
462
        ret = -1;
463
        goto fail;
464
    }
465

466
    if(datalen) {
467
        ret = -1;
468
        goto fail;
469
    }
470

471
    if(_libssh2_dsa_new(dsa, p, plen, q, qlen, g, glen, y, ylen, x, xlen)) {
472
        ret = -1;
473
        goto fail;
474
    }
475

476
    ret = 0;
477

478
fail:
479
    LIBSSH2_FREE(session, save_data);
480
    return ret;
481
}
482
#endif
483

484
#if LIBSSH2_RSA
485
#if LIBSSH2_RSA_SHA1
486
int
487
_libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session,
488
                       libssh2_rsa_ctx * rsactx,
489
                       const unsigned char *hash,
490
                       size_t hash_len,
491
                       unsigned char **signature, size_t *signature_len)
492
{
493
    gcry_sexp_t sig_sexp;
494
    gcry_sexp_t data;
495
    int rc;
496
    const char *tmp;
497
    size_t size;
498

499
    if(hash_len != SHA_DIGEST_LENGTH) {
500
        return -1;
501
    }
502

503
    if(gcry_sexp_build(&data, NULL,
504
                       "(data (flags pkcs1) (hash sha1 %b))",
505
                       hash_len, hash)) {
506
        return -1;
507
    }
508

509
    rc = gcry_pk_sign(&sig_sexp, data, rsactx);
510

511
    gcry_sexp_release(data);
512

513
    if(rc) {
514
        return -1;
515
    }
516

517
    data = gcry_sexp_find_token(sig_sexp, "s", 0);
518
    if(!data) {
519
        return -1;
520
    }
521

522
    tmp = gcry_sexp_nth_data(data, 1, &size);
523
    if(!tmp) {
524
        gcry_sexp_release(data);
525
        return -1;
526
    }
527

528
    if(tmp[0] == '\0') {
529
        tmp++;
530
        size--;
531
    }
532

533
    *signature = LIBSSH2_ALLOC(session, size);
534
    if(!*signature) {
535
        gcry_sexp_release(data);
536
        return -1;
537
    }
538
    memcpy(*signature, tmp, size);
539
    *signature_len = size;
540

541
    gcry_sexp_release(data);
542

543
    return rc;
544
}
545
#endif
546
#endif
547

548
#if LIBSSH2_DSA
549
int
550
_libssh2_dsa_sha1_sign(libssh2_dsa_ctx * dsactx,
551
                       const unsigned char *hash,
552
                       size_t hash_len, unsigned char *sig)
553
{
554
    unsigned char zhash[SHA_DIGEST_LENGTH + 1];
555
    gcry_sexp_t sig_sexp;
556
    gcry_sexp_t data;
557
    int ret;
558
    const char *tmp;
559
    size_t size;
560

561
    if(hash_len != SHA_DIGEST_LENGTH) {
562
        return -1;
563
    }
564

565
    memcpy(zhash + 1, hash, hash_len);
566
    zhash[0] = 0;
567

568
    if(gcry_sexp_build(&data, NULL, "(data (value %b))",
569
                       (int)(hash_len + 1), zhash)) {
570
        return -1;
571
    }
572

573
    ret = gcry_pk_sign(&sig_sexp, data, dsactx);
574

575
    gcry_sexp_release(data);
576

577
    if(ret) {
578
        return -1;
579
    }
580

581
    memset(sig, 0, 40);
582

583
    /* Extract R. */
584

585
    data = gcry_sexp_find_token(sig_sexp, "r", 0);
586
    if(!data)
587
        goto err;
588

589
    tmp = gcry_sexp_nth_data(data, 1, &size);
590
    if(!tmp)
591
        goto err;
592

593
    if(tmp[0] == '\0') {
594
        tmp++;
595
        size--;
596
    }
597

598
    if(size < 1 || size > 20)
599
        goto err;
600

601
    memcpy(sig + (20 - size), tmp, size);
602

603
    gcry_sexp_release(data);
604

605
    /* Extract S. */
606

607
    data = gcry_sexp_find_token(sig_sexp, "s", 0);
608
    if(!data)
609
        goto err;
610

611
    tmp = gcry_sexp_nth_data(data, 1, &size);
612
    if(!tmp)
613
        goto err;
614

615
    if(tmp[0] == '\0') {
616
        tmp++;
617
        size--;
618
    }
619

620
    if(size < 1 || size > 20)
621
        goto err;
622

623
    memcpy(sig + 20 + (20 - size), tmp, size);
624
    goto out;
625

626
err:
627
    ret = -1;
628

629
out:
630
    if(sig_sexp) {
631
        gcry_sexp_release(sig_sexp);
632
    }
633
    if(data) {
634
        gcry_sexp_release(data);
635
    }
636
    return ret;
637
}
638

639
int
640
_libssh2_dsa_sha1_verify(libssh2_dsa_ctx * dsactx,
641
                         const unsigned char *sig,
642
                         const unsigned char *m, size_t m_len)
643
{
644
    unsigned char hash[SHA_DIGEST_LENGTH + 1];
645
    gcry_sexp_t s_sig, s_hash;
646
    int rc = -1;
647

648
    if(libssh2_sha1(m, m_len, hash + 1)) {
649
        return -1;
650
    }
651
    hash[0] = 0;
652

653
    if(gcry_sexp_build(&s_hash, NULL, "(data(flags raw)(value %b))",
654
                       SHA_DIGEST_LENGTH + 1, hash)) {
655
        return -1;
656
    }
657

658
    if(gcry_sexp_build(&s_sig, NULL, "(sig-val(dsa(r %b)(s %b)))",
659
                       20, sig, 20, sig + 20)) {
660
        gcry_sexp_release(s_hash);
661
        return -1;
662
    }
663

664
    rc = gcry_pk_verify(s_sig, s_hash, dsactx);
665
    gcry_sexp_release(s_sig);
666
    gcry_sexp_release(s_hash);
667

668
    return (rc == 0) ? 0 : -1;
669
}
670
#endif
671

672
int
673
_libssh2_cipher_init(_libssh2_cipher_ctx * h,
674
                     _libssh2_cipher_type(algo),
675
                     unsigned char *iv, unsigned char *secret, int encrypt)
676
{
677
    int ret;
678
    int cipher = _libssh2_gcry_cipher(algo);
679
    int mode = _libssh2_gcry_mode(algo);
680
    size_t keylen = gcry_cipher_get_algo_keylen(cipher);
681

682
    (void)encrypt;
683

684
    ret = gcry_cipher_open(h, cipher, mode, 0);
685
    if(ret) {
686
        return -1;
687
    }
688

689
    ret = gcry_cipher_setkey(*h, secret, keylen);
690
    if(ret) {
691
        gcry_cipher_close(*h);
692
        return -1;
693
    }
694

695
    if(mode != GCRY_CIPHER_MODE_STREAM) {
696
        size_t blklen = gcry_cipher_get_algo_blklen(cipher);
697
        if(mode == GCRY_CIPHER_MODE_CTR)
698
            ret = gcry_cipher_setctr(*h, iv, blklen);
699
        else
700
            ret = gcry_cipher_setiv(*h, iv, blklen);
701
        if(ret) {
702
            gcry_cipher_close(*h);
703
            return -1;
704
        }
705
    }
706

707
    return 0;
708
}
709

710
int
711
_libssh2_cipher_crypt(_libssh2_cipher_ctx * ctx,
712
                      _libssh2_cipher_type(algo),
713
                      int encrypt, unsigned char *block, size_t blklen,
714
                      int firstlast)
715
{
716
    int ret;
717

718
    (void)algo;
719
    (void)firstlast;
720

721
    if(encrypt) {
722
        ret = gcry_cipher_encrypt(*ctx, block, blklen, block, blklen);
723
    }
724
    else {
725
        ret = gcry_cipher_decrypt(*ctx, block, blklen, block, blklen);
726
    }
727
    return ret;
728
}
729

730
int
731
_libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session,
732
                                unsigned char **method,
733
                                size_t *method_len,
734
                                unsigned char **pubkeydata,
735
                                size_t *pubkeydata_len,
736
                                const char *privatekeydata,
737
                                size_t privatekeydata_len,
738
                                const char *passphrase)
739
{
740
    (void)method;
741
    (void)method_len;
742
    (void)pubkeydata;
743
    (void)pubkeydata_len;
744
    (void)privatekeydata;
745
    (void)privatekeydata_len;
746
    (void)passphrase;
747

748
    return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
749
                          "Unable to extract public key from private "
750
                          "key in memory: "
751
                          "Method unimplemented in libgcrypt backend");
752
}
753

754
int
755
_libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session,
756
                          unsigned char **method,
757
                          size_t *method_len,
758
                          unsigned char **pubkeydata,
759
                          size_t *pubkeydata_len,
760
                          const char *privatekey,
761
                          const char *passphrase)
762
{
763
    (void)method;
764
    (void)method_len;
765
    (void)pubkeydata;
766
    (void)pubkeydata_len;
767
    (void)privatekey;
768
    (void)passphrase;
769

770
    return _libssh2_error(session, LIBSSH2_ERROR_FILE,
771
                    "Unable to extract public key from private key file: "
772
                    "Method unimplemented in libgcrypt backend");
773
}
774

775
int
776
_libssh2_sk_pub_keyfilememory(LIBSSH2_SESSION *session,
777
                              unsigned char **method,
778
                              size_t *method_len,
779
                              unsigned char **pubkeydata,
780
                              size_t *pubkeydata_len,
781
                              int *algorithm,
782
                              unsigned char *flags,
783
                              const char **application,
784
                              const unsigned char **key_handle,
785
                              size_t *handle_len,
786
                              const char *privatekeydata,
787
                              size_t privatekeydata_len,
788
                              const char *passphrase)
789
{
790
    (void)method;
791
    (void)method_len;
792
    (void)pubkeydata;
793
    (void)pubkeydata_len;
794
    (void)algorithm;
795
    (void)flags;
796
    (void)application;
797
    (void)key_handle;
798
    (void)handle_len;
799
    (void)privatekeydata;
800
    (void)privatekeydata_len;
801
    (void)passphrase;
802

803
    return _libssh2_error(session, LIBSSH2_ERROR_FILE,
804
                    "Unable to extract public SK key from private key file: "
805
                    "Method unimplemented in libgcrypt backend");
806
}
807

808
void _libssh2_init_aes_ctr(void)
809
{
810
    /* no implementation */
811
}
812

813
void
814
_libssh2_dh_init(_libssh2_dh_ctx *dhctx)
815
{
816
    *dhctx = gcry_mpi_new(0);                   /* Random from client */
817
}
818

819
int
820
_libssh2_dh_key_pair(_libssh2_dh_ctx *dhctx, _libssh2_bn *public,
821
                     _libssh2_bn *g, _libssh2_bn *p, int group_order)
822
{
823
    /* Generate x and e */
824
    gcry_mpi_randomize(*dhctx, group_order * 8 - 1, GCRY_WEAK_RANDOM);
825
    gcry_mpi_powm(public, g, *dhctx, p);
826
    return 0;
827
}
828

829
int
830
_libssh2_dh_secret(_libssh2_dh_ctx *dhctx, _libssh2_bn *secret,
831
                   _libssh2_bn *f, _libssh2_bn *p)
832
{
833
    /* Compute the shared secret */
834
    gcry_mpi_powm(secret, f, *dhctx, p);
835
    return 0;
836
}
837

838
void
839
_libssh2_dh_dtor(_libssh2_dh_ctx *dhctx)
840
{
841
    gcry_mpi_release(*dhctx);
842
    *dhctx = NULL;
843
}
844

845
/* _libssh2_supported_key_sign_algorithms
846
 *
847
 * Return supported key hash algo upgrades, see crypto.h
848
 *
849
 */
850

851
const char *
852
_libssh2_supported_key_sign_algorithms(LIBSSH2_SESSION *session,
853
                                       unsigned char *key_method,
854
                                       size_t key_method_len)
855
{
856
    (void)session;
857
    (void)key_method;
858
    (void)key_method_len;
859

860
    return NULL;
861
}
862

863
#endif /* LIBSSH2_CRYPTO_C */
864

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.