Skip to content

Commit

Permalink
Create python-app.yml (#15)
Browse files Browse the repository at this point in the history
* Create python-app.yml
* Fix lint warning
  • Loading branch information
gammazero authored Oct 15, 2024
1 parent 9c02ae1 commit 7929c74
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
42 changes: 21 additions & 21 deletions pymakeself/aes/aesctr.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def _compact_word(word):


try:
xrange
range
except Exception: # Python 3
xrange = range
range = range

def _bytes_to_string(binary):
return bytes(binary)
Expand All @@ -48,7 +48,7 @@ def __init__(self, key):

# Convert the initial value into an array of bytes long
iv = 1
self._counter = [((iv >> i) % 256) for i in xrange(128 - 8, -1, -8)]
self._counter = [((iv >> i) % 256) for i in range(128 - 8, -1, -8)]

self._remaining_counter = []

Expand All @@ -68,7 +68,7 @@ def decrypt(self, crypttext):

def _inc_counter(self):
"""Increment the counter (overflow rolls back to 0)."""
for i in xrange(len(self._counter) - 1, -1, -1):
for i in range(len(self._counter) - 1, -1, -1):
self._counter[i] += 1
if self._counter[i] < 256:
break
Expand Down Expand Up @@ -115,20 +115,20 @@ def __init__(self, key):
rounds = self.number_of_rounds[len(key)]

# Encryption round keys
self._Ke = [[0] * 4 for i in xrange(rounds + 1)]
self._Ke = [[0] * 4 for i in range(rounds + 1)]

# Decryption round keys
self._Kd = [[0] * 4 for i in xrange(rounds + 1)]
self._Kd = [[0] * 4 for i in range(rounds + 1)]

round_key_count = (rounds + 1) * 4
KC = len(key) // 4

# Convert the key into ints
tk = [struct.unpack('>i', key[i:i + 4])[0]
for i in xrange(0, len(key), 4)]
for i in range(0, len(key), 4)]

# Copy values into round key arrays
for i in xrange(0, KC):
for i in range(0, KC):
self._Ke[i // 4][i % 4] = tk[i]
self._Kd[rounds - (i // 4)][i % 4] = tk[i]

Expand All @@ -145,12 +145,12 @@ def __init__(self, key):
rconpointer += 1

if KC != 8:
for i in xrange(1, KC):
for i in range(1, KC):
tk[i] ^= tk[i - 1]

# Key expansion for 256-bit keys is "slightly different" (fips-197)
else:
for i in xrange(1, KC // 2):
for i in range(1, KC // 2):
tk[i] ^= tk[i - 1]
tt = tk[KC // 2 - 1]

Expand All @@ -159,7 +159,7 @@ def __init__(self, key):
(self.S[(tt >> 16) & 0xFF] << 16) ^
(self.S[(tt >> 24) & 0xFF] << 24))

for i in xrange(KC // 2 + 1, KC):
for i in range(KC // 2 + 1, KC):
tk[i] ^= tk[i - 1]

# Copy values into round key arrays
Expand All @@ -171,8 +171,8 @@ def __init__(self, key):
t += 1

# Inverse-Cipher-ify the decryption round key (fips-197 section 5.3)
for r in xrange(1, rounds):
for j in xrange(0, 4):
for r in range(1, rounds):
for j in range(0, 4):
tt = self._Kd[r][j]
self._Kd[r][j] = (self.U1[(tt >> 24) & 0xFF] ^
self.U2[(tt >> 16) & 0xFF] ^
Expand All @@ -190,11 +190,11 @@ def encrypt(self, plaintext):
a = [0, 0, 0, 0]

# Convert plaintext to (ints ^ key)
t = [(_compact_word(plaintext[4 * i:4 * i + 4]) ^ self._Ke[0][i]) for i in xrange(0, 4)]
t = [(_compact_word(plaintext[4 * i:4 * i + 4]) ^ self._Ke[0][i]) for i in range(0, 4)]

# Apply round transforms
for r in xrange(1, rounds):
for i in xrange(0, 4):
for r in range(1, rounds):
for i in range(0, 4):
a[i] = (self.T1[(t[ i ] >> 24) & 0xFF] ^
self.T2[(t[(i + s1) % 4] >> 16) & 0xFF] ^
self.T3[(t[(i + s2) % 4] >> 8) & 0xFF] ^
Expand All @@ -204,7 +204,7 @@ def encrypt(self, plaintext):

# The last round is special
result = []
for i in xrange(0, 4):
for i in range(0, 4):
tt = self._Ke[rounds][i]
result.append((self.S[(t[ i ] >> 24) & 0xFF] ^ (tt >> 24)) & 0xFF)
result.append((self.S[(t[(i + s1) % 4] >> 16) & 0xFF] ^ (tt >> 16)) & 0xFF)
Expand All @@ -224,11 +224,11 @@ def decrypt(self, ciphertext):
a = [0, 0, 0, 0]

# Convert ciphertext to (ints ^ key)
t = [(_compact_word(ciphertext[4 * i:4 * i + 4]) ^ self._Kd[0][i]) for i in xrange(0, 4)]
t = [(_compact_word(ciphertext[4 * i:4 * i + 4]) ^ self._Kd[0][i]) for i in range(0, 4)]

# Apply round transforms
for r in xrange(1, rounds):
for i in xrange(0, 4):
for r in range(1, rounds):
for i in range(0, 4):
a[i] = (self.T5[(t[ i ] >> 24) & 0xFF] ^
self.T6[(t[(i + s1) % 4] >> 16) & 0xFF] ^
self.T7[(t[(i + s2) % 4] >> 8) & 0xFF] ^
Expand All @@ -238,7 +238,7 @@ def decrypt(self, ciphertext):

# The last round is special
result = []
for i in xrange(0, 4):
for i in range(0, 4):
tt = self._Kd[rounds][i]
result.append((self.Si[(t[ i ] >> 24) & 0xFF] ^ (tt >> 24)) & 0xFF)
result.append((self.Si[(t[(i + s1) % 4] >> 16) & 0xFF] ^ (tt >> 16)) & 0xFF)
Expand Down

0 comments on commit 7929c74

Please sign in to comment.