@@ -20,6 +20,7 @@ module MessageBody {
2020
2121 import opened Wrappers
2222 import opened UInt = StandardLibrary. UInt
23+ import opened StandardLibrary. MemoryMath
2324 import Types = AwsCryptographyEncryptionSdkTypes
2425 import MPL = AwsCryptographyMaterialProvidersTypes
2526 import Primitives = AtomicPrimitives
@@ -49,7 +50,7 @@ module MessageBody {
4950 const ENDFRAME_SEQUENCE_NUMBER: uint32 := Frames. ENDFRAME_SEQUENCE_NUMBER
5051 const NONFRAMED_SEQUENCE_NUMBER: uint32 := Frames. NONFRAMED_SEQUENCE_NUMBER
5152
52- function method IVSeq (suite: MPL .AlgorithmSuiteInfo, sequenceNumber: uint32 )
53+ function method {:opaque} IVSeq (suite: MPL .AlgorithmSuiteInfo, sequenceNumber: uint32 )
5354 :(ret: seq < uint8> )
5455 requires 4 <= SerializableTypes. GetIvLength (suite)
5556 // = compliance/data-format/message-body.txt#2.5.2.1.2
@@ -64,7 +65,13 @@ module MessageBody {
6465 // # (../framework/algorithm-suites.md) that generated the message.
6566 ensures |ret| == SerializableTypes. GetIvLength (suite) as nat
6667 {
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
6875 }
6976
7077 // = compliance/data-format/message-body.txt#2.5.2.1.2
@@ -81,6 +88,7 @@ module MessageBody {
8188 ensures IVSeq (suite, m) != IVSeq (suite, n)
8289 {
8390 var paddingLength := SerializableTypes. GetIvLength (suite) as nat - 4;
91+ reveal IVSeq;
8492 assert IVSeq (suite, m)[paddingLength.. ] == UInt32ToSeq (m);
8593 assert IVSeq (suite, n)[paddingLength.. ] == UInt32ToSeq (n);
8694 UInt32SeqSerializeDeserialize (m);
@@ -248,7 +256,7 @@ module MessageBody {
248256 && frame. authTag == callEvent. output. value. authTag
249257 )
250258 {
251- var n : int , sequenceNumber := 0, START_SEQUENCE_NUMBER;
259+ var n : uint64 , sequenceNumber := 0, START_SEQUENCE_NUMBER;
252260 var regularFrames: MessageRegularFrames := [];
253261
254262 // = compliance/client-apis/encrypt.txt#2.7
@@ -265,8 +273,9 @@ module MessageBody {
265273 // adding another frame puts us at < |plaintext|. This means we will never
266274 // consume the entire plaintext in this while loop, and will always construct
267275 // 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|
270279 invariant |plaintext| == 0 ==> 0 == n
271280 invariant START_SEQUENCE_NUMBER <= sequenceNumber <= ENDFRAME_SEQUENCE_NUMBER
272281 invariant |regularFrames| == (sequenceNumber - START_SEQUENCE_NUMBER) as nat
@@ -294,7 +303,7 @@ module MessageBody {
294303 {
295304 :- Need (sequenceNumber < ENDFRAME_SEQUENCE_NUMBER, Types.AwsEncryptionSdkException(
296305 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) ];
298307
299308 // = compliance/client-apis/encrypt.txt#2.7
300309 // # * If there are enough input plaintext bytes consumable to create a
@@ -314,7 +323,7 @@ module MessageBody {
314323 LemmaAddingNextRegularFrame (regularFrames, regularFrame);
315324 regularFrames := regularFrames + [regularFrame];
316325
317- n := n + header. body. frameLength as nat ;
326+ n := Add (n, header.body.frameLength as uint64) ;
318327
319328 // = compliance/client-apis/encrypt.txt#2.7.1
320329 // # Otherwise, this value MUST be 1 greater than
@@ -710,7 +719,7 @@ module MessageBody {
710719 assert |AESDecryptHistory| == 0;
711720 assert SumDecryptCalls (AESDecryptHistory) == plaintext;
712721
713- for i := 0 to |body. regularFrames|
722+ for i : uint64 : = 0 to |body. regularFrames| as uint64
714723 // // The goal is to assert FramesEncryptPlaintext.
715724 // // But this requires the final frame e.g. a FramedMessage.
716725 // // So I decompose this into parts
@@ -734,7 +743,7 @@ module MessageBody {
734743
735744 AESDecryptHistory := AESDecryptHistory + [Seq. Last (crypto.History.AESDecrypt)];
736745 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;
738747 }
739748
740749 var finalPlaintextSegment :- DecryptFrame (body.finalFrame, key, crypto);
@@ -977,7 +986,7 @@ module MessageBody {
977986 }
978987 by method { // because Seq.DropLast makes a full copy
979988 var result : seq < uint8> := [];
980- for i := 0 to |frames|
989+ for i : uint64 : = 0 to |frames| as uint64
981990 invariant IsMessageRegularFrames (frames)
982991 invariant IsMessageRegularFrames (frames[..i])
983992 invariant result == WriteMessageRegularFrames (frames[..i])
@@ -1008,7 +1017,7 @@ module MessageBody {
10081017 requires forall frame: Frames. Frame | frame in regularFrames :: frame. header == header
10091018 requires buffer. bytes == continuation. bytes
10101019 requires buffer. start <= continuation. start
1011- requires 0 <= continuation. start <= |buffer. bytes|
1020+ requires 0 <= continuation. start as nat <= |buffer. bytes|
10121021 requires CorrectlyReadRange (buffer, continuation, buffer.bytes[buffer.start..continuation.start])
10131022 requires CorrectlyRead (buffer, Success(SuccessfulRead(regularFrames, continuation)), WriteMessageRegularFrames)
10141023 decreases ENDFRAME_SEQUENCE_NUMBER as nat - |regularFrames|
@@ -1024,7 +1033,7 @@ module MessageBody {
10241033 // # data), this operation MUST use the first 4 bytes of a frame to
10251034 // # determine if the frame MUST be deserialized as a final frame
10261035 // # (../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).
10281037 if (sequenceNumber. data != ENDFRAME_SEQUENCE_NUMBER) then
10291038
10301039 // = compliance/client-apis/decrypt.txt#2.7.4
@@ -1043,13 +1052,15 @@ module MessageBody {
10431052 // # Otherwise, this
10441053 // # value MUST be 1 greater than the value of the sequence number
10451054 // # 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;
10481058 LemmaAddingNextRegularFrame (regularFrames, regularFrame.data);
10491059
10501060 var nextRegularFrames: MessageRegularFrames := regularFrames + [regularFrame. data];
10511061
10521062 CorrectlyReadByteRange (buffer, continuation, WriteMessageRegularFrames(regularFrames));
1063+ assert CorrectlyReadRange (continuation, regularFrame.tail, Frames.WriteRegularFrame(regularFrame.data));
10531064 AppendToCorrectlyReadByteRange (buffer, continuation, regularFrame.tail, Frames.WriteRegularFrame(regularFrame.data));
10541065 assert buffer. bytes == continuation. bytes == regularFrame. tail. bytes by {
10551066 reveal CorrectlyReadRange ();
@@ -1097,10 +1108,11 @@ module MessageBody {
10971108
10981109 var finalFrame :- Frames. ReadFinalFrame (continuation, header);
10991110 :- Need (
1100- finalFrame.data.seqNum as nat == |regularFrames| + 1,
1111+ finalFrame.data.seqNum as uint64 == |regularFrames| as uint64 + 1,
11011112 Error("Sequence number out of order.")
11021113 );
11031114
1115+ assert finalFrame. data. seqNum as nat == |regularFrames| + 1;
11041116 assert MessageFramesAreMonotonic (regularFrames + [finalFrame.data]);
11051117 assert MessageFramesAreForTheSameMessage (regularFrames + [finalFrame.data]);
11061118
0 commit comments