34
34
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35
35
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
36
*/
37
-
37
+
38
38
#include "includes.h"
39
39
40
40
#include <sys/types.h>
43
43
#include <stdarg.h>
44
44
#include <stdio.h>
45
45
46
+
46
47
#include "cipher.h"
47
48
#include "misc.h"
48
49
#include "sshbuf.h"
51
52
52
53
#include "openbsd-compat/openssl-compat.h"
53
54
55
+
56
+
57
+ #ifdef USE_MSCNG
58
+ #undef WITH_OPENSSL
59
+ #endif
60
+
54
61
#ifdef WITH_SSH1
55
62
extern const EVP_CIPHER * evp_ssh1_bf (void );
56
63
extern const EVP_CIPHER * evp_ssh1_3des (void );
@@ -108,9 +115,19 @@ static const struct sshcipher ciphers[] = {
108
115
SSH_CIPHER_SSH2 , 16 , 32 , 12 , 16 , 0 , 0 , EVP_aes_256_gcm },
109
116
# endif /* OPENSSL_HAVE_EVPGCM */
110
117
#else /* WITH_OPENSSL */
118
+
119
+ #ifdef USE_MSCNG
120
+ { "aes128-ctr" , SSH_CIPHER_SSH2 , 16 , 16 , 0 , 0 , 0 , _CNG_CIPHER_AES | _CNG_MODE_CTR , NULL },
121
+ { "aes192-ctr" , SSH_CIPHER_SSH2 , 16 , 24 , 0 , 0 , 0 , _CNG_CIPHER_AES | _CNG_MODE_CTR , NULL },
122
+ { "aes256-ctr" , SSH_CIPHER_SSH2 , 16 , 32 , 0 , 0 , 0 , _CNG_CIPHER_AES | _CNG_MODE_CTR , NULL },
123
+ { "aes128-cbc" , SSH_CIPHER_SSH2 , 16 , 16 , 0 , 0 , 0 , _CNG_CIPHER_AES | _CNG_MODE_CBC , NULL },
124
+ { "aes192-cbc" , SSH_CIPHER_SSH2 , 16 , 24 , 0 , 0 , 0 , _CNG_CIPHER_AES | _CNG_MODE_CBC , NULL },
125
+ { "aes256-cbc" , SSH_CIPHER_SSH2 , 16 , 32 , 0 , 0 , 0 , _CNG_CIPHER_AES | _CNG_MODE_CBC , NULL },
126
+ #else
111
127
{ "aes128-ctr" , SSH_CIPHER_SSH2 , 16 , 16 , 0 , 0 , 0 , CFLAG_AESCTR , NULL },
112
128
{ "aes192-ctr" , SSH_CIPHER_SSH2 , 16 , 24 , 0 , 0 , 0 , CFLAG_AESCTR , NULL },
113
129
{ "aes256-ctr" , SSH_CIPHER_SSH2 , 16 , 32 , 0 , 0 , 0 , CFLAG_AESCTR , NULL },
130
+ #endif
114
131
{ "none" , SSH_CIPHER_NONE , 8 , 0 , 0 , 0 , 0 , CFLAG_NONE , NULL },
115
132
#endif /* WITH_OPENSSL */
116
133
@@ -293,6 +310,8 @@ cipher_init(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
293
310
const u_char * key , u_int keylen , const u_char * iv , u_int ivlen ,
294
311
int do_encrypt )
295
312
{
313
+
314
+
296
315
#ifdef WITH_OPENSSL
297
316
int ret = SSH_ERR_INTERNAL_ERROR ;
298
317
const EVP_CIPHER * type ;
@@ -316,11 +335,25 @@ cipher_init(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
316
335
return chachapoly_init (& cc -> cp_ctx , key , keylen );
317
336
}
318
337
#ifndef WITH_OPENSSL
338
+
339
+ #ifdef USE_MSCNG
340
+
341
+ /* cng shares cipher flag with NONE. Make sure the NONE cipher isn't requested */
342
+ if ((cc -> cipher -> flags & CFLAG_NONE ) == 0 )
343
+ {
344
+
345
+ if (cng_cipher_init (& cc -> cng_ctx ,key ,keylen ,iv , ivlen ,cc -> cipher -> flags ))
346
+ return SSH_ERR_LIBCRYPTO_ERROR ;
347
+
348
+ return 0 ;
349
+ }
350
+ #else
319
351
if ((cc -> cipher -> flags & CFLAG_AESCTR ) != 0 ) {
320
352
aesctr_keysetup (& cc -> ac_ctx , key , 8 * keylen , 8 * ivlen );
321
353
aesctr_ivsetup (& cc -> ac_ctx , iv );
322
354
return 0 ;
323
355
}
356
+ #endif
324
357
if ((cc -> cipher -> flags & CFLAG_NONE ) != 0 )
325
358
return 0 ;
326
359
return SSH_ERR_INVALID_ARGUMENT ;
@@ -373,6 +406,7 @@ cipher_init(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
373
406
return 0 ;
374
407
}
375
408
409
+
376
410
/*
377
411
* cipher_crypt() operates as following:
378
412
* Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
@@ -387,18 +421,44 @@ int
387
421
cipher_crypt (struct sshcipher_ctx * cc , u_int seqnr , u_char * dest ,
388
422
const u_char * src , u_int len , u_int aadlen , u_int authlen )
389
423
{
424
+ #ifdef USE_MSCNG
425
+ int ret = 0 ;
426
+ #endif
427
+
390
428
if ((cc -> cipher -> flags & CFLAG_CHACHAPOLY ) != 0 ) {
391
429
return chachapoly_crypt (& cc -> cp_ctx , seqnr , dest , src ,
392
430
len , aadlen , authlen , cc -> encrypt );
393
431
}
394
432
#ifndef WITH_OPENSSL
395
- if ((cc -> cipher -> flags & CFLAG_AESCTR ) != 0 ) {
433
+
434
+ #ifdef USE_MSCNG
435
+
436
+ /* cng shares cipher flag with NONE. Make sure the NONE cipher isn't requested */
437
+ if ((cc -> cipher -> flags & CFLAG_NONE ) == 0 )
438
+ {
439
+ if (aadlen )
440
+ memcpy (dest , src , aadlen );
441
+ if (cc -> encrypt )
442
+ ret = cng_cipher_encrypt (& cc -> cng_ctx ,dest + aadlen , len , src + aadlen ,len );
443
+ else
444
+ ret = cng_cipher_decrypt (& cc -> cng_ctx ,dest + aadlen , len , src + aadlen , len );
445
+
446
+ if (ret != len ){
447
+ return SSH_ERR_LIBCRYPTO_ERROR ;
448
+ }
449
+ return 0 ;
450
+ }
451
+ #else
452
+ if ((cc -> cipher -> flags & CFLAG_AESCTR ) != 0 ) {
396
453
if (aadlen )
397
454
memcpy (dest , src , aadlen );
398
455
aesctr_encrypt_bytes (& cc -> ac_ctx , src + aadlen ,
399
456
dest + aadlen , len );
400
457
return 0 ;
401
458
}
459
+ #endif
460
+
461
+
402
462
if ((cc -> cipher -> flags & CFLAG_NONE ) != 0 ) {
403
463
memcpy (dest , src , aadlen + len );
404
464
return 0 ;
@@ -472,6 +532,10 @@ cipher_cleanup(struct sshcipher_ctx *cc)
472
532
else if (EVP_CIPHER_CTX_cleanup (& cc -> evp ) == 0 )
473
533
return SSH_ERR_LIBCRYPTO_ERROR ;
474
534
#endif
535
+ #ifdef USE_MSCNG
536
+ else
537
+ cng_cipher_cleanup (& cc -> cng_ctx );
538
+ #endif
475
539
return 0 ;
476
540
}
477
541
0 commit comments