Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added gcm #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyaes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@

VERSION = [1, 3, 0]

from .aes import AES, AESModeOfOperationCTR, AESModeOfOperationCBC, AESModeOfOperationCFB, AESModeOfOperationECB, AESModeOfOperationOFB, AESModesOfOperation, Counter
from .aes import AES, AESModeOfOperationCTR, AESModeOfOperationCBC, AESModeOfOperationCFB, AESModeOfOperationECB, AESModeOfOperationOFB, AESModeOfOperationGCM, AESModesOfOperation, Counter
from .blockfeeder import decrypt_stream, Decrypter, encrypt_stream, Encrypter
from .blockfeeder import PADDING_NONE, PADDING_DEFAULT
12 changes: 11 additions & 1 deletion pyaes/aes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import struct

__all__ = ["AES", "AESModeOfOperationCTR", "AESModeOfOperationCBC", "AESModeOfOperationCFB",
"AESModeOfOperationECB", "AESModeOfOperationOFB", "AESModesOfOperation", "Counter"]
"AESModeOfOperationECB", "AESModeOfOperationOFB", "AESModeOfOperationGCM", "AESModesOfOperation", "Counter"]


def _compact_word(word):
Expand Down Expand Up @@ -578,6 +578,15 @@ def decrypt(self, crypttext):
# AES-CTR is symetric
return self.encrypt(crypttext)

class AESModeOfOperationGCM(AESModeOfOperationCTR):
name = "GCM"

def __init__(self, key, iv):
iv = iv + b"\x00\x00\x00\x02"
iv_int = 0
for i in xrange(0, len(iv), 4):
iv_int = (iv_int << 32) + struct.unpack('>I', iv[i:i+4])[0]
AESModeOfOperationCTR.__init__(self, key, counter=Counter(iv_int))

# Simple lookup table for each mode
AESModesOfOperation = dict(
Expand All @@ -586,4 +595,5 @@ def decrypt(self, crypttext):
cfb = AESModeOfOperationCFB,
ecb = AESModeOfOperationECB,
ofb = AESModeOfOperationOFB,
gcm = AESModeOfOperationGCM,
)
8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/env python

from distutils.core import setup
try:
from setuptools import setup
except ImportError:
from distutils.core import setup

LONG_DESCRIPTION = '''A pure-Python implementation of the AES (FIPS-197)
block-cipher algorithm and common modes of operation (CBC, CFB, CTR, ECB,
OFB) with no dependencies beyond standard Python libraries. See README.md
for API reference and details.'''

setup(name = 'pyaes',
version = '1.6.1',
version = '1.6.2',
description = 'Pure-Python Implementation of the AES block-cipher and common modes of operation',
long_description = LONG_DESCRIPTION,
author = 'Richard Moore',
Expand Down
19 changes: 18 additions & 1 deletion tests/test-aes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# compare against a known working implementation
from Crypto.Cipher import AES as KAES
from Crypto.Util import Counter as KCounter
for mode in [ 'CBC', 'CTR', 'CFB', 'ECB', 'OFB' ]:
for mode in [ 'CBC', 'CTR', 'GCM', 'CFB', 'ECB', 'OFB' ]:

(tt_ksetup, tt_kencrypt, tt_kdecrypt) = (0.0, 0.0, 0.0)
(tt_setup, tt_encrypt, tt_decrypt) = (0.0, 0.0, 0.0)
Expand Down Expand Up @@ -122,6 +122,23 @@
aes2 = AESModeOfOperationCTR(key, counter = Counter(initial_value = 0))
tt_setup += time.time() - t0

elif mode == 'GCM':
text_length = [None, 3, 16, 127, 128, 129, 1500, 10000, 100000, 10001, 10002, 10003, 10004, 10005, 10006, 10007, 10008][test]
if test < 6:
plaintext = [ os.urandom(text_length) ]
else:
plaintext = [ os.urandom(text_length) for x in xrange(0, test) ]
iv = os.urandom(12)

t0 = time.time()
kaes = KAES.new(key, KAES.MODE_GCM, iv)
kaes2 = KAES.new(key, KAES.MODE_GCM, iv)
tt_ksetup += time.time() - t0

t0 = time.time()
aes = AESModeOfOperationGCM(key, iv)
aes2 = AESModeOfOperationGCM(key, iv)
tt_setup += time.time() - t0
count += 1

t0 = time.time()
Expand Down