@@ -20,6 +20,7 @@ module MessageBody {
20
20
21
21
import opened Wrappers
22
22
import opened UInt = StandardLibrary. UInt
23
+ import opened StandardLibrary. MemoryMath
23
24
import Types = AwsCryptographyEncryptionSdkTypes
24
25
import MPL = AwsCryptographyMaterialProvidersTypes
25
26
import Primitives = AtomicPrimitives
@@ -49,7 +50,7 @@ module MessageBody {
49
50
const ENDFRAME_SEQUENCE_NUMBER: uint32 := Frames. ENDFRAME_SEQUENCE_NUMBER
50
51
const NONFRAMED_SEQUENCE_NUMBER: uint32 := Frames. NONFRAMED_SEQUENCE_NUMBER
51
52
52
- function method IVSeq (suite: MPL .AlgorithmSuiteInfo, sequenceNumber: uint32 )
53
+ function method {:opaque} IVSeq (suite: MPL .AlgorithmSuiteInfo, sequenceNumber: uint32 )
53
54
:(ret: seq < uint8> )
54
55
requires 4 <= SerializableTypes. GetIvLength (suite)
55
56
// = compliance/data-format/message-body.txt#2.5.2.1.2
@@ -64,7 +65,13 @@ module MessageBody {
64
65
// # (../framework/algorithm-suites.md) that generated the message.
65
66
ensures |ret| == SerializableTypes. GetIvLength (suite) as nat
66
67
{
67
- seq (SerializableTypes.GetIvLength(suite) as nat - 4, _ => 0) + UInt32ToSeq (sequenceNumber)
68
+ var len : uint8 := SerializableTypes. GetIvLength (suite);
69
+ var num := UInt32ToSeq (sequenceNumber);
70
+ if len == 12 then
71
+ [0,0,0,0,0,0,0,0] + num
72
+ else
73
+ // We never actually get here, but maybe one day
74
+ seq (len as nat - 4, _ => 0) + num
68
75
}
69
76
70
77
// = compliance/data-format/message-body.txt#2.5.2.1.2
@@ -81,6 +88,7 @@ module MessageBody {
81
88
ensures IVSeq (suite, m) != IVSeq (suite, n)
82
89
{
83
90
var paddingLength := SerializableTypes. GetIvLength (suite) as nat - 4;
91
+ reveal IVSeq;
84
92
assert IVSeq (suite, m)[paddingLength.. ] == UInt32ToSeq (m);
85
93
assert IVSeq (suite, n)[paddingLength.. ] == UInt32ToSeq (n);
86
94
UInt32SeqSerializeDeserialize (m);
@@ -248,7 +256,7 @@ module MessageBody {
248
256
&& frame. authTag == callEvent. output. value. authTag
249
257
)
250
258
{
251
- var n : int , sequenceNumber := 0, START_SEQUENCE_NUMBER;
259
+ var n : uint64 , sequenceNumber := 0, START_SEQUENCE_NUMBER;
252
260
var regularFrames: MessageRegularFrames := [];
253
261
254
262
// = compliance/client-apis/encrypt.txt#2.7
@@ -265,8 +273,9 @@ module MessageBody {
265
273
// adding another frame puts us at < |plaintext|. This means we will never
266
274
// consume the entire plaintext in this while loop, and will always construct
267
275
// a final frame after exiting it.
268
- while n + header. body. frameLength as nat < |plaintext|
269
- invariant |plaintext| != 0 ==> 0 <= n < |plaintext|
276
+ SequenceIsSafeBecauseItIsInMemory (plaintext);
277
+ while Add (n, header.body.frameLength as uint64) < |plaintext| as uint64
278
+ invariant |plaintext| != 0 ==> 0 <= n as nat < |plaintext|
270
279
invariant |plaintext| == 0 ==> 0 == n
271
280
invariant START_SEQUENCE_NUMBER <= sequenceNumber <= ENDFRAME_SEQUENCE_NUMBER
272
281
invariant |regularFrames| == (sequenceNumber - START_SEQUENCE_NUMBER) as nat
@@ -294,7 +303,7 @@ module MessageBody {
294
303
{
295
304
:- Need (sequenceNumber < ENDFRAME_SEQUENCE_NUMBER, Types.AwsEncryptionSdkException(
296
305
message := "too many frames"));
297
- var plaintextFrame := plaintext[n.. n + header. body. frameLength as nat ];
306
+ var plaintextFrame := plaintext[n.. Add (n, header.body.frameLength as uint64) ];
298
307
299
308
// = compliance/client-apis/encrypt.txt#2.7
300
309
// # * If there are enough input plaintext bytes consumable to create a
@@ -314,7 +323,7 @@ module MessageBody {
314
323
LemmaAddingNextRegularFrame (regularFrames, regularFrame);
315
324
regularFrames := regularFrames + [regularFrame];
316
325
317
- n := n + header. body. frameLength as nat ;
326
+ n := Add (n, header.body.frameLength as uint64) ;
318
327
319
328
// = compliance/client-apis/encrypt.txt#2.7.1
320
329
// # Otherwise, this value MUST be 1 greater than
@@ -710,7 +719,7 @@ module MessageBody {
710
719
assert |AESDecryptHistory| == 0;
711
720
assert SumDecryptCalls (AESDecryptHistory) == plaintext;
712
721
713
- for i := 0 to |body. regularFrames|
722
+ for i : uint64 : = 0 to |body. regularFrames| as uint64
714
723
// // The goal is to assert FramesEncryptPlaintext.
715
724
// // But this requires the final frame e.g. a FramedMessage.
716
725
// // So I decompose this into parts
@@ -734,7 +743,7 @@ module MessageBody {
734
743
735
744
AESDecryptHistory := AESDecryptHistory + [Seq. Last (crypto.History.AESDecrypt)];
736
745
assert Seq. Last (AESDecryptHistory) == Seq. Last (crypto.History.AESDecrypt);
737
- assert crypto. History. AESDecrypt[i + |old (crypto. History. AESDecrypt)|]. input. iv == body. regularFrames[i]. iv;
746
+ assert crypto. History. AESDecrypt[i as nat + |old (crypto. History. AESDecrypt)|]. input. iv == body. regularFrames[i]. iv;
738
747
}
739
748
740
749
var finalPlaintextSegment :- DecryptFrame (body.finalFrame, key, crypto);
@@ -977,7 +986,7 @@ module MessageBody {
977
986
}
978
987
by method { // because Seq.DropLast makes a full copy
979
988
var result : seq < uint8> := [];
980
- for i := 0 to |frames|
989
+ for i : uint64 : = 0 to |frames| as uint64
981
990
invariant IsMessageRegularFrames (frames)
982
991
invariant IsMessageRegularFrames (frames[..i])
983
992
invariant result == WriteMessageRegularFrames (frames[..i])
@@ -1008,7 +1017,7 @@ module MessageBody {
1008
1017
requires forall frame: Frames. Frame | frame in regularFrames :: frame. header == header
1009
1018
requires buffer. bytes == continuation. bytes
1010
1019
requires buffer. start <= continuation. start
1011
- requires 0 <= continuation. start <= |buffer. bytes|
1020
+ requires 0 <= continuation. start as nat <= |buffer. bytes|
1012
1021
requires CorrectlyReadRange (buffer, continuation, buffer.bytes[buffer.start..continuation.start])
1013
1022
requires CorrectlyRead (buffer, Success(SuccessfulRead(regularFrames, continuation)), WriteMessageRegularFrames)
1014
1023
decreases ENDFRAME_SEQUENCE_NUMBER as nat - |regularFrames|
@@ -1024,7 +1033,7 @@ module MessageBody {
1024
1033
// # data), this operation MUST use the first 4 bytes of a frame to
1025
1034
// # determine if the frame MUST be deserialized as a final frame
1026
1035
// # (../data-format/message-body.md#final-frame) or regular frame
1027
- // # (../fata -format/message-body/md#regular-frame).
1036
+ // # (../data -format/message-body/md#regular-frame).
1028
1037
if (sequenceNumber. data != ENDFRAME_SEQUENCE_NUMBER) then
1029
1038
1030
1039
// = compliance/client-apis/decrypt.txt#2.7.4
@@ -1043,13 +1052,15 @@ module MessageBody {
1043
1052
// # Otherwise, this
1044
1053
// # value MUST be 1 greater than the value of the sequence number
1045
1054
// # of the previous frame.
1046
- :- Need (regularFrame.data.seqNum as nat == |regularFrames| + 1, Error("Sequence number out of order."));
1047
-
1055
+ SequenceIsSafeBecauseItIsInMemory (regularFrames);
1056
+ :- Need (regularFrame.data.seqNum as uint64 == |regularFrames| as uint64 + 1, Error("Sequence number out of order."));
1057
+ assert regularFrame. data. seqNum as nat == |regularFrames| + 1;
1048
1058
LemmaAddingNextRegularFrame (regularFrames, regularFrame.data);
1049
1059
1050
1060
var nextRegularFrames: MessageRegularFrames := regularFrames + [regularFrame. data];
1051
1061
1052
1062
CorrectlyReadByteRange (buffer, continuation, WriteMessageRegularFrames(regularFrames));
1063
+ assert CorrectlyReadRange (continuation, regularFrame.tail, Frames.WriteRegularFrame(regularFrame.data));
1053
1064
AppendToCorrectlyReadByteRange (buffer, continuation, regularFrame.tail, Frames.WriteRegularFrame(regularFrame.data));
1054
1065
assert buffer. bytes == continuation. bytes == regularFrame. tail. bytes by {
1055
1066
reveal CorrectlyReadRange ();
@@ -1097,10 +1108,11 @@ module MessageBody {
1097
1108
1098
1109
var finalFrame :- Frames. ReadFinalFrame (continuation, header);
1099
1110
:- Need (
1100
- finalFrame.data.seqNum as nat == |regularFrames| + 1,
1111
+ finalFrame.data.seqNum as uint64 == |regularFrames| as uint64 + 1,
1101
1112
Error("Sequence number out of order.")
1102
1113
);
1103
1114
1115
+ assert finalFrame. data. seqNum as nat == |regularFrames| + 1;
1104
1116
assert MessageFramesAreMonotonic (regularFrames + [finalFrame.data]);
1105
1117
assert MessageFramesAreForTheSameMessage (regularFrames + [finalFrame.data]);
1106
1118
0 commit comments