.MUTILATE LDA CHK3 ; Set A to the third checksum byte at CHK3 for this ; commander file ; We now use this value to change the four random number ; seeds in RAND to RAND+3 into four different values ; that we can use to encrypt the file EOR RAND ; EOR it into the first random number seed in RAND STA RAND STA comfil2 ; Store the seed value in comfil2, so it gets saved as ; part of the commander file EOR #$A5 ; EOR and OR the result into the second random number ORA #17 ; seed in RAND+1 EOR RAND+1 STA RAND+1 STA comfil2+1 ; Store the seed value in comfil2+1, so it gets saved as ; part of the commander file EOR RAND+2 ; EOR the result into the third random number seed in EOR #$F8 ; RAND+2 STA RAND+2 STA comfil2+2 ; Store the seed value in comfil2+2, so it gets saved as ; part of the commander file EOR RAND+3 ; EOR the result into the fourth random number seed in EOR #$12 ; RAND+3 STA RAND+3 STA comfil2+3 ; Store the seed value in comfil2+3, so it gets saved as ; part of the commander file ; We now have four random seeds that are partially based ; on the third checksum and partially based on the ; previous value of the four random seeds, so we now use ; these seeds to encrypt the file ; ; The encryption process uses a simple EOR with the next ; random number from the repeatable sequence produced by ; the four seeds we stored at comfil2, so repeating the ; encryption process with the same four seeds will ; decrypt the file .MUTIL3 LDY #comsiz-5 ; Set Y to a byte counter so we can work our way ; backwards through the whole commander file structure, ; omitting the four bytes at comfil2 at the end .MUTIL1 JSR DORND2 ; Set A and X to random numbers, making sure the C flag ; doesn't affect the outcome EOR comfil,Y ; EOR the Y-th byte in the commander file with the next STA comfil,Y ; random number in the sequence generated by the four ; seeds stored at comfil2 DEY ; Decrement the byte counter BPL MUTIL1 ; Loop back until we have processed the whole commander ; file structure RTS ; Return from the subroutineName: MUTILATE [Show more] Type: Subroutine Category: Save and load Summary: Encrypt the commander file in the buffer at comfilContext: See this subroutine in context in the source code References: This subroutine is called as follows: * wfile calls MUTILATE * UNMUTILATE calls via MUTIL3
At this point, the commander file is set up in memory like this (as defined in the configuration variables comsiz, comfil and comfil2): .comfil 20 bytes .TAP% 77 bytes containing the full commander file from byte #0 to byte #76 9 bytes .comfil2 4 bytes The entire structure above contains comsiz (110) bytes. This routine encrypts the commander file by first calculating a set of four random number seeds, using a set of bitwise operations that start with the third checksum byte at CHK3. These seed values get stored in the four bytes at comfil and are saved with the file, so they can be used by the UNMUTILATE routine to reverse the encryption. The encryption process simply takes the repeatable sequence of random numbers that are generated from these four seeds, and EOR's the bytes in the commander file with the numbers in the sequence. Because this is a simple EOR with a number sequence that can be reproduced from the four seeds, the decryption process just repeats the encryption process, generating the same sequence of random numbers from the seeds in the commander file, and EOR'ing them with the bytes in the encrypted file to produce the decrypted bytes (which works because a EOR b EOR b = a).
Other entry points: MUTIL3 Decrypt the commander file
[X]
Variable CHK3 (category: Save and load)
Third checksum byte for the saved commander data file
[X]
Label MUTIL1 is local to this routine
[X]
Configuration variable comfil = TAP%-20
The address of the commander file structure that is encrypted by MUTLIATE and decrypted by UNMUTILATE
[X]
Configuration variable comfil2 = comfil + comsiz - 4
The address of the four seeds that are used to encrypt and decrypt the commander file, which are at the end of the commander file structure that is saved to disk
[X]
Configuration variable comsiz = 110
The size of the commander file structure that is saved to disk (must be no more than 252 bytes so the file fits into a 256-byte sector, along with the four seeds used to encrypt and decrypt the file)