@@ -103,33 +103,33 @@ TLS-context: context [
103
103
hash-method: ; one of: [MD5 SHA1 SHA256 SHA384]
104
104
crypt-method: none
105
105
is-aead?: false ; crypt-method with "Authenticated Encryption with Additional Data" (not yet supported!)
106
- mac-size: ; Size of message authentication code
106
+ IV-size: ; The amount of data needed to be generated for the initialization vector.
107
+ mac-size: ; Size of message authentication code
107
108
crypt-size: ; The number of bytes from the key_block that are used for generating the write keys.
108
- block-size: ; The amount of data a block cipher enciphers in one chunk; a block
109
+ block-size: 0 ; The amount of data a block cipher enciphers in one chunk; a block
109
110
; cipher running in CBC mode can only encrypt an even multiple of
110
111
; its block size.
111
- IV-size: 0 ; The amount of data needed to be generated for the initialization vector.
112
-
113
112
114
- local-iv :
113
+ local-IV :
115
114
local-mac:
116
115
local-key:
117
116
local-random:
118
117
119
- remote-iv :
118
+ remote-IV :
120
119
remote-mac:
121
120
remote-key:
122
121
remote-random:
123
122
124
123
aead: none ; used now for chacha20/poly1305 combo
125
124
125
+ server-session: none
126
126
server-certs: copy []
127
127
server-extensions: copy []
128
128
129
129
seq-read: 0 ; sequence counters
130
130
seq-write: 0
131
131
132
- server-session:
132
+
133
133
pre-master-secret:
134
134
master-secret:
135
135
certificate:
@@ -723,22 +723,19 @@ client-key-exchange: function [
723
723
AT :pos-key (pick [UI8BYTES UI16BYTES] key-data-len-bytes) :key-data
724
724
]
725
725
726
-
727
726
;-- make all secure data
728
- if ctx/version >= *Protocol-version/TLS1.0 [
729
- ; NOTE: key-expansion is used just to generate keys so it does not need to be stored in context!
730
- ctx/master-secret: prf "master secret" (append copy ctx/local-random ctx/remote-random ) pre-master-secret 48
731
- key-expansion: prf "key expansion" (append copy ctx/remote-random ctx/local-random ) master-secret
732
- (mac-size + crypt-size + iv-size) * 2
733
-
734
- pre-master-secret: none ;-- not needed anymore
735
-
736
- ;?? master-secret
737
- ;?? key-expansion
738
- ;?? mac-size
739
- ;?? crypt-size
740
- ;?? iv-size
741
- ]
727
+ ; NOTE: key-expansion is used just to generate keys so it does not need to be stored in context!
728
+ ctx/master-secret: prf "master secret" (append copy ctx/local-random ctx/remote-random ) pre-master-secret 48
729
+ key-expansion: prf "key expansion" (append copy ctx/remote-random ctx/local-random ) master-secret
730
+ (mac-size + crypt-size + iv-size) * 2
731
+
732
+ pre-master-secret: none ;-- not needed anymore
733
+
734
+ ;?? master-secret
735
+ ;?? key-expansion
736
+ ;?? mac-size
737
+ ;?? crypt-size
738
+ ;?? iv-size
742
739
743
740
unless is-aead? [
744
741
local-mac: take/part key-expansion mac-size
@@ -753,25 +750,25 @@ client-key-exchange: function [
753
750
log-more ["Client-key: ^[ [32m" local-key]
754
751
log-more ["Server-key: ^[ [32m" remote-key]
755
752
756
- local-iv : take/part key-expansion iv-size
757
- remote-iv : take/part key-expansion iv-size
753
+ local-IV : take/part key-expansion iv-size
754
+ remote-IV : take/part key-expansion iv-size
758
755
759
- log-more ["Client-IV: ^[ [32m" local-iv ]
760
- log-more ["Server-IV: ^[ [32m" remote-iv ]
756
+ log-more ["Client-IV: ^[ [32m" local-IV ]
757
+ log-more ["Server-IV: ^[ [32m" remote-IV ]
761
758
762
759
key-expansion: none
763
760
764
761
encrypt-port: open [
765
762
scheme: 'crypt
766
763
algorithm: :crypt-method
767
- init-vector: :local-iv
764
+ init-vector: :local-IV
768
765
key: :local-key
769
766
]
770
767
decrypt-port: open [
771
768
scheme: 'crypt
772
769
direction: 'decrypt
773
770
algorithm: :crypt-method
774
- init-vector: :remote-iv
771
+ init-vector: :remote-IV
775
772
key: :remote-key
776
773
]
777
774
@@ -896,15 +893,12 @@ decrypt-msg: function [
896
893
]
897
894
]
898
895
][
899
- if all [
900
- block-size
901
- version > *Protocol-version/TLS1.0
902
- ][
896
+ if block-size [
903
897
;server's initialization vector is new with each message
904
- remote-iv : take/part data block-size
898
+ remote-IV : take/part data block-size
905
899
]
906
900
;?? data
907
- modify decrypt-port 'init-vector remote-iv
901
+ modify decrypt-port 'init-vector remote-IV
908
902
data: read update write decrypt-port :data
909
903
910
904
;change data decrypt-data ctx data
@@ -926,9 +920,7 @@ decrypt-msg: function [
926
920
927
921
if mac <> mac-check [ critical-error: *Alert/Bad_record_MAC ]
928
922
929
- if version > *Protocol-version/TLS1.0 [
930
- unset 'remote-iv ;-- avoid reuse in TLS 1.1 and above
931
- ]
923
+ unset 'remote-IV ;-- avoid reuse in TLS 1.1 and above
932
924
]
933
925
]
934
926
binary/init bin 0 ;clear the temp bin buffer
@@ -969,24 +961,21 @@ encrypt-data: function [
969
961
][
970
962
971
963
;@@ GenericBlockCipher: https://tools.ietf.org/html/rfc5246#section-6.2.3.2
972
- if version > *Protocol-version/TLS1.0 [
973
- ;
974
- ; "The Initialization Vector (IV) SHOULD be chosen at random, and
975
- ; MUST be unpredictable. Note that in versions of TLS prior to 1.1,
976
- ; there was no IV field, and the last ciphertext block of the
977
- ; previous record (the "CBC residue") was used as the IV. This was
978
- ; changed to prevent the attacks described in [CBCATT]. For block
979
- ; ciphers, the IV length is SecurityParameters.record_iv_length,
980
- ; which is equal to the SecurityParameters.block_size."
981
- ;
982
- binary/write clear local-iv [RANDOM-BYTES :block-size ]
983
- modify encrypt-port 'init-vector local-iv
984
- ]
964
+ ; "The Initialization Vector (IV) SHOULD be chosen at random, and
965
+ ; MUST be unpredictable. Note that in versions of TLS prior to 1.1,
966
+ ; there was no IV field, and the last ciphertext block of the
967
+ ; previous record (the "CBC residue") was used as the IV. This was
968
+ ; changed to prevent the attacks described in [CBCATT]. For block
969
+ ; ciphers, the IV length is SecurityParameters.record_iv_length,
970
+ ; which is equal to the SecurityParameters.block_size."
971
+ ;
972
+ binary/write clear local-IV [RANDOM-BYTES :block-size ]
973
+ modify encrypt-port 'init-vector local-IV
985
974
986
975
;?? ctx/seq-write
987
- log-more ["Client-IV: ^[ [32m" local-iv ]
988
- log-more ["Client-mac: ^[ [32m" local-mac]
989
- log-more ["Hash-method: ^[ [32m" hash-method]
976
+ log-more ["Client-IV: ^[ [32m" local-IV ]
977
+ log-more ["Client-mac: ^[ [32m" local-mac]
978
+ log-more ["Hash-method: ^[ [32m" hash-method]
990
979
991
980
; Message Authentication Code
992
981
; https://tools.ietf.org/html/rfc5246#section-6.2.3.1
@@ -1008,11 +997,9 @@ encrypt-data: function [
1008
997
; on next line are 3 ops.. encrypting content, padding and getting the result
1009
998
encrypted: read update write encrypt-port content
1010
999
1011
- ;-- TLS versions 1.1 and above include the local-iv in plaintext.
1012
- if version > *Protocol-version/TLS1.0 [
1013
- insert encrypted local-iv
1014
- ;clear local-iv ;-- avoid accidental reuse
1015
- ]
1000
+ ;-- TLS versions 1.1 and above include the local-IV in plaintext.
1001
+ insert encrypted local-IV
1002
+ ;clear local-IV ;-- avoid accidental reuse
1016
1003
]
1017
1004
binary/init bin 0 ;reset the bin buffer
1018
1005
]
0 commit comments