├── .gitignore ├── LICENSE ├── README.md └── eac.py /.gitignore: -------------------------------------------------------------------------------- 1 | .venv 2 | logs/ 3 | __pycache__/ 4 | .DS_Store 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 puddly 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EAC Log Signer 2 | 3 | This is a transparent implementation of the Exact Audio Copy log checksum algorithm in Python 3.7+. Includes an option to fix those pesky edited logs. 4 | 5 | # Installation 6 | 7 | Only depends on `pprp` (for an implementation of Rijndael-256 with variable block sizes): 8 | 9 | $ pip install pprp==0.2.6 10 | $ curl https://raw.githubusercontent.com/puddly/eac_logsigner/master/eac.py > eac_logsigner 11 | $ chmod +x eac_logsigner 12 | 13 | # Usage 14 | 15 | usage: eac.py [-h] {verify,sign} ... 16 | 17 | Verifies and resigns EAC logs 18 | 19 | positional arguments: 20 | {verify,sign} 21 | verify verify a log 22 | sign sign or fix an existing log 23 | 24 | optional arguments: 25 | -h, --help show this help message and exit 26 | 27 | # Example 28 | 29 | $ python3 eac.py sign bad.log good.log 30 | $ python3 eac.py verify *.log 31 | log1.log: OK 32 | log2.log: OK 33 | log3.log: Malformed 34 | 35 | 36 | # Algorithm 37 | 38 | 1. Strip the log file of newlines and BOMs. 39 | 2. Cut off the existing signature block and (re-)encode the log text back into little-endian UTF-16 40 | 3. Encrypt the log file with Rijndael-256: 41 | - in CBC mode 42 | - with a 256-bit block size (most AES implementations hard-code a 128-bit block size) 43 | - all-zeroes IV 44 | - zero-padding 45 | - the hex key `9378716cf13e4265ae55338e940b376184da389e50647726b35f6f341ee3efd9` 46 | 4. XOR together all of the resulting 256-bit ciphertext blocks. You can do it byte-by-byte, it doesn't matter in the end. 47 | 5. Output the little-endian representation of the above number, in uppercase hex. 48 | -------------------------------------------------------------------------------- /eac.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import re 4 | import sys 5 | import enum 6 | import argparse 7 | import contextlib 8 | 9 | import pprp 10 | 11 | CHECKSUM_MIN_VERSION = (1, 0, 1) # V1.0 beta 1 12 | 13 | 14 | def eac_checksum(text): 15 | # Ignore newlines 16 | text = text.replace('\r', '').replace('\n', '') 17 | 18 | # Fuzzing reveals BOMs are also ignored 19 | text = text.replace('\ufeff', '').replace('\ufffe', '') 20 | 21 | # Setup Rijndael-256 with a 256-bit blocksize 22 | cipher = pprp.crypto_3.rijndael( 23 | # Probably SHA256('super secret password') but it doesn't actually matter 24 | key=bytes.fromhex('9378716cf13e4265ae55338e940b376184da389e50647726b35f6f341ee3efd9'), 25 | block_size=256 // 8 26 | ) 27 | 28 | # Encode the text as UTF-16-LE 29 | plaintext = text.encode('utf-16-le') 30 | 31 | # The IV is all zeroes so we don't have to handle it 32 | signature = b'\x00' * 32 33 | 34 | # Process it block-by-block 35 | for i in range(0, len(plaintext), 32): 36 | # Zero-pad the last block, if necessary 37 | plaintext_block = plaintext[i:i + 32].ljust(32, b'\x00') 38 | 39 | # CBC mode (XOR the previous ciphertext block into the plaintext) 40 | cbc_plaintext = bytes(a ^ b for a, b in zip(signature, plaintext_block)) 41 | 42 | # New signature is the ciphertext. 43 | signature = cipher.encrypt(cbc_plaintext) 44 | 45 | # Textual signature is just the hex representation 46 | return signature.hex().upper() 47 | 48 | 49 | def extract_info(text): 50 | version = None 51 | 52 | # Find the line with the version number, breaking at the first non-empty line 53 | for line in text.splitlines(): 54 | if version is None and line.startswith('Exact Audio Copy'): 55 | version_text = line.replace('Exact Audio Copy ', '').split(' from', 1)[0] 56 | major_minor, *beta = version_text[1:].split(' beta ', 1) 57 | 58 | major, minor = map(int, major_minor.split('.')) 59 | 60 | if beta: 61 | beta = int(beta[0]) 62 | else: 63 | beta = float('+inf') # so V1.0 > v1.0 beta 1 64 | 65 | version = (major, minor, beta) 66 | elif re.match(r'[a-zA-Z]', line): 67 | break 68 | 69 | if '\r\n\r\n==== Log checksum' not in text: 70 | signature = None 71 | else: 72 | text, signature_parts = text.split('\r\n\r\n==== Log checksum', 1) 73 | signature = signature_parts.split()[0].strip() 74 | 75 | return text, version, signature 76 | 77 | 78 | def eac_verify(data): 79 | # Log is encoded as Little Endian UTF-16 80 | text = data.decode('utf-16-le') 81 | 82 | # Strip off the BOM 83 | if text.startswith('\ufeff'): 84 | text = text[1:] 85 | 86 | # Null bytes screw it up 87 | if '\x00' in text: 88 | text = text[:text.index('\x00')] 89 | 90 | # EAC crashes if there are more than 2^14 bytes in a line 91 | if any(len(l) + 1 > 2**13 for l in text.split('\n')): 92 | raise RuntimeError('EAC cannot handle lines longer than 2^13 chars') 93 | 94 | unsigned_text, version, old_signature = extract_info(text) 95 | 96 | return unsigned_text, version, old_signature, eac_checksum(unsigned_text) 97 | 98 | 99 | class FixedFileType(argparse.FileType): 100 | def __call__(self, string): 101 | file = super().__call__(string) 102 | 103 | # Properly handle stdin/stdout with 'b' mode 104 | if 'b' in self._mode and file in (sys.stdin, sys.stdout): 105 | return file.buffer 106 | 107 | return file 108 | 109 | 110 | if __name__ == '__main__': 111 | parser = argparse.ArgumentParser(description='Verifies and resigns EAC logs') 112 | 113 | subparsers = parser.add_subparsers(dest='command', required=True) 114 | 115 | verify_parser = subparsers.add_parser('verify', help='verify a log') 116 | verify_parser.add_argument('files', type=FixedFileType(mode='rb'), nargs='+', help='input log file(s)') 117 | 118 | sign_parser = subparsers.add_parser('sign', help='sign or fix an existing log') 119 | sign_parser.add_argument('--force', action='store_true', help='forces signing even if EAC version is too old') 120 | sign_parser.add_argument('input_file', type=FixedFileType(mode='rb'), help='input log file') 121 | sign_parser.add_argument('output_file', type=FixedFileType(mode='wb'), help='output log file') 122 | 123 | args = parser.parse_args() 124 | 125 | if args.command == 'sign': 126 | with contextlib.closing(args.input_file) as handle: 127 | try: 128 | data, version, old_signature, actual_signature = eac_verify(handle.read()) 129 | except ValueError as e: 130 | print(args.input_file, ': ', e, sep='') 131 | sys.exit(1) 132 | 133 | if not args.force and (version is None or version < CHECKSUM_MIN_VERSION): 134 | raise ValueError('EAC version is too old to be signed') 135 | 136 | data += f'\r\n\r\n==== Log checksum {actual_signature} ====\r\n' 137 | 138 | with contextlib.closing(args.output_file or args.input_file) as handle: 139 | handle.write(b'\xff\xfe' + data.encode('utf-16le')) 140 | elif args.command == 'verify': 141 | max_length = max(len(f.name) for f in args.files) 142 | 143 | for file in args.files: 144 | prefix = (file.name + ':').ljust(max_length + 2) 145 | 146 | with contextlib.closing(file) as handle: 147 | try: 148 | data, version, old_signature, actual_signature = eac_verify(handle.read()) 149 | except RuntimeError as e: 150 | print(prefix, e) 151 | continue 152 | except ValueError as e: 153 | print(prefix, 'Not a log file') 154 | continue 155 | 156 | if version is None: 157 | print(prefix, 'Not a log file') 158 | elif old_signature is None: 159 | print(prefix, 'Log file without a signature') 160 | elif old_signature != actual_signature: 161 | print(prefix, 'Malformed') 162 | elif version < CHECKSUM_MIN_VERSION: 163 | print(prefix, 'Forged') 164 | else: 165 | print(prefix, 'OK') 166 | --------------------------------------------------------------------------------