Subversion Repositories Kolibri OS

Rev

Rev 9216 | Rev 9990 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9216 Rev 9987
Line 32... Line 32...
32
 
32
 
33
        k_h_ctx                 dd ?
33
        k_h_ctx                 dd ?
Line 34... Line 34...
34
        temp_ctx                dd ?
34
        temp_ctx                dd ?
35
 
-
 
36
        H                       dd ?    ; exchange hash
-
 
37
 
-
 
38
        rx_iv                   dd ?    ; Rx initialisation vector
-
 
39
        tx_iv                   dd ?    ; Tx initialisation vector
-
 
40
        rx_enc_key              dd ?    ; Rx encryption key
-
 
41
        tx_enc_key              dd ?    ; Tx encryption key
-
 
42
        rx_int_key              dd ?    ; Rx integrity key
-
 
43
        tx_int_key              dd ?    ; Tx integrity key
35
 
Line 44... Line 36...
44
 
36
        H                       dd ?    ; exchange hash
Line 45... Line 37...
45
        K_length                dd ?
37
        K_length                dd ?
Line 52... Line 44...
52
 
44
 
Line 53... Line 45...
53
endl
45
endl
Line 54... Line 46...
54
 
46
 
55
; Allocate memory for temp variables
47
; Allocate memory for temp variables
56
 
48
 
57
        mov     ecx, 7*(MAX_BITS/8+4) + 7*SHA2_256_LEN + 2*LIBCRASH_CTX_LEN
49
        mov     ecx, 7*(MAX_BITS/8+4) + 2*LIBCRASH_CTX_LEN + 1*SHA2_256_LEN
Line 58... Line 50...
58
        mcall   68, 12
50
        mcall   68, 12
Line 81... Line 73...
81
        mov     [temp_ctx], eax
73
        mov     [temp_ctx], eax
82
        add     eax, LIBCRASH_CTX_LEN
74
        add     eax, LIBCRASH_CTX_LEN
Line 83... Line 75...
83
 
75
 
84
        mov     [H], eax
76
        mov     [H], eax
85
        add     eax, SHA2_256_LEN
-
 
86
        mov     [rx_iv], eax
-
 
87
        add     eax, SHA2_256_LEN
-
 
88
        mov     [tx_iv], eax
-
 
89
        add     eax, SHA2_256_LEN
-
 
90
        mov     [rx_enc_key], eax
-
 
91
        add     eax, SHA2_256_LEN
-
 
92
        mov     [tx_enc_key], eax
-
 
93
        add     eax, SHA2_256_LEN
-
 
94
        mov     [rx_int_key], eax
-
 
95
        add     eax, SHA2_256_LEN
-
 
96
        mov     [tx_int_key], eax
-
 
Line 97... Line 77...
97
;        add     eax, SHA2_256_LEN
77
        add     eax, SHA2_256_LEN
Line 98... Line 78...
98
 
78
 
99
; Copy the partial exchange hash to our temporary one
79
; Copy the partial exchange hash to our temporary one
Line 295... Line 275...
295
        lea     esi, [eax + sshlib_connection.session_id]
275
        lea     esi, [eax + sshlib_connection.session_id]
296
        lea     edi, [session_id_x+1]
276
        lea     edi, [session_id_x+1]
297
        mov     ecx, SHA2_256_LEN/4
277
        mov     ecx, SHA2_256_LEN/4
298
        rep movsd
278
        rep movsd
Line -... Line 279...
-
 
279
 
-
 
280
;-------------------------------------
-
 
281
; << Parse Diffie-Hellman New Keys MSG
-
 
282
 
-
 
283
        stdcall sshlib_recv_packet, [con_ptr], 0
-
 
284
        cmp     eax, 0
-
 
285
        jl      .err
-
 
286
 
-
 
287
        mov     ebx, [con_ptr]
-
 
288
        cmp     [ebx + sshlib_connection.rx_buffer.message_code], SSH_MSG_NEWKEYS
-
 
289
        jne     .err_proto
-
 
290
 
-
 
291
        DEBUGF  2, "Received New Keys\n"
-
 
292
 
-
 
293
;----------------------------------------------------
-
 
294
; >> Reply with New Keys message (Using the old keys)
-
 
295
 
-
 
296
        stdcall sshlib_send_packet, [con_ptr], ssh_msg_new_keys, ssh_msg_new_keys.length, 0
-
 
297
        cmp     eax, 0
Line 299... Line 298...
299
 
298
        jl      .err
300
 
299
 
Line 301... Line 300...
301
;---------------
300
;---------------
Line 319... Line 318...
319
        rep movsd
318
        rep movsd
320
        lea     edx, [session_id_x]
319
        lea     edx, [session_id_x]
321
        mov     byte[edx], 'A'
320
        mov     byte[edx], 'A'
322
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
321
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
323
        invoke  sha2_256_finish, [temp_ctx]
322
        invoke  sha2_256_finish, [temp_ctx]
324
        mov     edi, [tx_iv]
323
        mov     ebx, [con_ptr]
-
 
324
        lea     edi, [ebx + sshlib_connection.tx_iv]
325
        mov     esi, [temp_ctx]
325
        mov     esi, [temp_ctx]
326
        mov     ecx, SHA2_256_LEN/4
326
        mov     ecx, SHA2_256_LEN/4
327
        rep movsd
327
        rep movsd
Line 328... Line -...
328
 
-
 
329
        DEBUGF  1, "Remote IV: "
-
 
330
        stdcall dump_hex, [tx_iv], SHA2_256_LEN/4
-
 
331
 
328
 
332
;---------------------------------------------------------------
329
;---------------------------------------------------------------
Line 333... Line 330...
333
; Initial IV server to client: HASH(K || H || "B" || session_id)
330
; Initial IV server to client: HASH(K || H || "B" || session_id)
334
 
331
 
Line 338... Line 335...
338
        rep movsd
335
        rep movsd
339
        lea     edx, [session_id_x]
336
        lea     edx, [session_id_x]
340
        mov     byte[edx], 'B'
337
        mov     byte[edx], 'B'
341
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
338
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
342
        invoke  sha2_256_finish, [temp_ctx]
339
        invoke  sha2_256_finish, [temp_ctx]
343
        mov     edi, [rx_iv]
340
        mov     ebx, [con_ptr]
-
 
341
        lea     edi, [ebx + sshlib_connection.rx_iv]
344
        mov     esi, [temp_ctx]
342
        mov     esi, [temp_ctx]
345
        mov     ecx, SHA2_256_LEN/4
343
        mov     ecx, SHA2_256_LEN/4
346
        rep movsd
344
        rep movsd
Line 347... Line -...
347
 
-
 
348
        DEBUGF  1, "Local IV: "
-
 
349
        stdcall dump_hex, [rx_iv], SHA2_256_LEN/4
-
 
350
 
345
 
351
;-------------------------------------------------------------------
346
;-------------------------------------------------------------------
Line 352... Line 347...
352
; Encryption key client to server: HASH(K || H || "C" || session_id)
347
; Encryption key client to server: HASH(K || H || "C" || session_id)
353
 
348
 
Line 357... Line 352...
357
        rep movsd
352
        rep movsd
358
        lea     edx, [session_id_x]
353
        lea     edx, [session_id_x]
359
        mov     byte[edx], 'C'
354
        mov     byte[edx], 'C'
360
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
355
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
361
        invoke  sha2_256_finish, [temp_ctx]
356
        invoke  sha2_256_finish, [temp_ctx]
-
 
357
 
362
        mov     edi, [tx_enc_key]
358
        mov     ebx, [con_ptr]
-
 
359
        lea     edi, [ebx+sshlib_connection.tx_enc_key]
363
        mov     esi, [temp_ctx]
360
        mov     esi, [temp_ctx]
364
        mov     ecx, SHA2_256_LEN/4
361
        mov     ecx, SHA2_256_LEN/4
365
        rep movsd
362
        rep movsd
Line -... Line 363...
-
 
363
 
366
 
364
        mov     esi, [k_h_ctx]
-
 
365
        mov     edi, [temp_ctx]
-
 
366
        mov     ecx, LIBCRASH_CTX_LEN/4
-
 
367
        rep movsd
-
 
368
        mov     ebx, [con_ptr]
-
 
369
        lea     edi, [ebx+sshlib_connection.tx_enc_key]
-
 
370
        invoke  sha2_256_update, [temp_ctx], edi, SHA2_256_LEN
-
 
371
        invoke  sha2_256_finish, [temp_ctx]
-
 
372
 
-
 
373
        mov     ebx, [con_ptr]
-
 
374
        add     edi, 256/8
367
        DEBUGF  1, "Remote key: "
375
        mov     esi, [temp_ctx]
-
 
376
        mov     ecx, SHA2_256_LEN/4
Line 368... Line 377...
368
        stdcall dump_hex, [tx_enc_key], SHA2_256_LEN/4
377
        rep movsd
369
 
378
 
Line 370... Line 379...
370
;-------------------------------------------------------------------
379
;-------------------------------------------------------------------
Line 376... Line 385...
376
        rep movsd
385
        rep movsd
377
        lea     edx, [session_id_x]
386
        lea     edx, [session_id_x]
378
        mov     byte[edx], 'D'
387
        mov     byte[edx], 'D'
379
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
388
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
380
        invoke  sha2_256_finish, [temp_ctx]
389
        invoke  sha2_256_finish, [temp_ctx]
-
 
390
 
381
        mov     edi, [rx_enc_key]
391
        mov     ebx, [con_ptr]
-
 
392
        lea     edi, [ebx+sshlib_connection.rx_enc_key]
382
        mov     esi, [temp_ctx]
393
        mov     esi, [temp_ctx]
383
        mov     ecx, SHA2_256_LEN/4
394
        mov     ecx, SHA2_256_LEN/4
384
        rep movsd
395
        rep movsd
Line -... Line 396...
-
 
396
 
-
 
397
        mov     esi, [k_h_ctx]
-
 
398
        mov     edi, [temp_ctx]
-
 
399
        mov     ecx, LIBCRASH_CTX_LEN/4
385
 
400
        rep movsd
-
 
401
        mov     ebx, [con_ptr]
-
 
402
        lea     edi, [ebx+sshlib_connection.rx_enc_key]
-
 
403
        invoke  sha2_256_update, [temp_ctx], edi, SHA2_256_LEN
-
 
404
        invoke  sha2_256_finish, [temp_ctx]
-
 
405
 
-
 
406
        mov     ebx, [con_ptr]
-
 
407
        add     edi, 256/8
386
        DEBUGF  1, "Local key: "
408
        mov     esi, [temp_ctx]
-
 
409
        mov     ecx, SHA2_256_LEN/4
Line 387... Line 410...
387
        stdcall dump_hex, [rx_enc_key], SHA2_256_LEN/4
410
        rep movsd
388
 
411
 
Line 389... Line 412...
389
;------------------------------------------------------------------
412
;------------------------------------------------------------------
Line 395... Line 418...
395
        rep movsd
418
        rep movsd
396
        lea     edx, [session_id_x]
419
        lea     edx, [session_id_x]
397
        mov     byte[edx], 'E'
420
        mov     byte[edx], 'E'
398
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
421
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
399
        invoke  sha2_256_finish, [temp_ctx]
422
        invoke  sha2_256_finish, [temp_ctx]
400
        mov     edi, [tx_int_key]
423
        mov     ebx, [con_ptr]
-
 
424
        lea     edi, [ebx + sshlib_connection.tx_int_key]
401
        mov     esi, [temp_ctx]
425
        mov     esi, [temp_ctx]
402
        mov     ecx, SHA2_256_LEN/4
426
        mov     ecx, SHA2_256_LEN/4
403
        rep movsd
427
        rep movsd
Line 404... Line -...
404
 
-
 
405
        DEBUGF  1, "Remote Integrity key: "
-
 
406
        stdcall dump_hex, [tx_int_key], SHA2_256_LEN/4
-
 
407
 
428
 
408
;------------------------------------------------------------------
429
;------------------------------------------------------------------
Line 409... Line 430...
409
; Integrity key server to client: HASH(K || H || "F" || session_id)
430
; Integrity key server to client: HASH(K || H || "F" || session_id)
410
 
431
 
Line 414... Line 435...
414
        rep movsd
435
        rep movsd
415
        lea     edx, [session_id_x]
436
        lea     edx, [session_id_x]
416
        mov     byte[edx], 'F'
437
        mov     byte[edx], 'F'
417
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
438
        invoke  sha2_256_update, [temp_ctx], edx, SHA2_256_LEN+1
418
        invoke  sha2_256_finish, [temp_ctx]
439
        invoke  sha2_256_finish, [temp_ctx]
419
        mov     edi, [rx_int_key]
440
        mov     ebx, [con_ptr]
-
 
441
        lea     edi, [ebx + sshlib_connection.rx_int_key]
420
        mov     esi, [temp_ctx]
442
        mov     esi, [temp_ctx]
421
        mov     ecx, SHA2_256_LEN/4
443
        mov     ecx, SHA2_256_LEN/4
422
        rep movsd
444
        rep movsd
Line 423... Line -...
423
 
-
 
424
        DEBUGF  1, "Local Integrity key: "
-
 
425
        stdcall dump_hex, [rx_int_key] , SHA2_256_LEN/4
-
 
426
 
-
 
427
;-------------------------------------
-
 
428
; << Parse Diffie-Hellman New Keys MSG
-
 
429
 
-
 
430
        stdcall sshlib_recv_packet, [con_ptr], 0
-
 
431
        cmp     eax, 0
-
 
432
        jl      .err
-
 
433
 
445
 
434
        mov     ebx, [con_ptr]
-
 
435
        cmp     [ebx + sshlib_connection.rx_buffer.message_code], SSH_MSG_NEWKEYS
-
 
436
        jne     .err_proto
-
 
437
 
-
 
438
        DEBUGF  2, "Received New Keys\n"
-
 
439
 
-
 
440
;-------------------------------
-
 
441
; >> Reply with New Keys message
-
 
442
 
-
 
443
        stdcall sshlib_send_packet, [con_ptr], ssh_msg_new_keys, ssh_msg_new_keys.length, 0
-
 
444
        cmp     eax, 0
-
 
445
        jl      .err
-
 
446
 
-
 
447
;----------------------------------------------
-
 
448
; Set keys and initialize transport subroutines
-
 
449
 
-
 
450
        DEBUGF  2, "SSH: Setting encryption keys\n"
-
 
451
 
-
 
452
        mov     ebx, [con_ptr]
-
 
453
        stdcall aes256_ctr_init, [rx_iv]
-
 
454
        test    eax, eax
-
 
455
        jz      .err_nomem
-
 
456
        mov     [ebx + sshlib_connection.rx_crypt_ctx_ptr], eax
-
 
457
        stdcall aes256_set_encrypt_key, eax, [rx_enc_key]
-
 
458
        mov     [ebx + sshlib_connection.rx_crypt_proc], aes256_ctr_crypt
-
 
459
        mov     [ebx + sshlib_connection.rx_crypt_blocksize], AES256_BLOCKSIZE
-
 
460
 
-
 
461
        stdcall aes256_ctr_init, [tx_iv]
-
 
462
        test    eax, eax
-
 
463
        jz      .err_nomem
-
 
464
        mov     [ebx + sshlib_connection.tx_crypt_ctx_ptr], eax
-
 
465
        stdcall aes256_set_encrypt_key, eax, [tx_enc_key]
-
 
466
        mov     [ebx + sshlib_connection.tx_crypt_proc], aes256_ctr_crypt
-
 
467
        mov     [ebx + sshlib_connection.tx_crypt_blocksize], AES256_BLOCKSIZE
-
 
468
 
-
 
469
        mov     [ebx + sshlib_connection.tx_pad_size], AES256_BLOCKSIZE
-
 
470
        mov     [ebx + sshlib_connection.tx_pad_proc], MBRandom
-
 
471
 
-
 
472
        lea     ecx, [ebx + sshlib_connection.rx_mac_ctx]
-
 
473
        stdcall hmac_sha256_setkey, ecx, [rx_int_key], SHA2_256_LEN
-
 
474
        mov     [ebx + sshlib_connection.rx_mac_proc], hmac_sha256
-
 
475
        mov     [ebx + sshlib_connection.rx_mac_length], SHA2_256_LEN
-
 
476
 
-
 
477
        lea     ecx, [ebx + sshlib_connection.tx_mac_ctx]
-
 
478
        stdcall hmac_sha256_setkey, ecx, [tx_int_key], SHA2_256_LEN
-
 
479
        mov     [ebx + sshlib_connection.tx_mac_proc], hmac_sha256
-
 
480
        mov     [ebx + sshlib_connection.tx_mac_length], SHA2_256_LEN
-
 
481
 
446
        mov     ebx, [con_ptr]
482
        mov     [ebx + sshlib_connection.status], SSHLIB_CON_STAT_KEX_DONE
447
        mov     [ebx + sshlib_connection.status], SSHLIB_CON_STAT_KEX_DONE
Line 483... Line 448...
483
        xor     eax, eax
448
        xor     eax, eax
484
 
449
 
485
  .err:
450
  .err:
486
        push    eax
451
        push    eax
487
        xor     eax, eax
452
        xor     eax, eax
488
        mov     ecx, (7*(MAX_BITS/8+4) + 7*SHA2_256_LEN + 2*LIBCRASH_CTX_LEN)/4
453
        mov     ecx, (7*(MAX_BITS/8+4) + 2*LIBCRASH_CTX_LEN + 1*SHA2_256_LEN )/4
Line 489... Line 454...
489
        mov     edi, [mpint_tmp]
454
        mov     edi, [mpint_tmp]
490
        rep stosd
455
        rep stosd