Question: Test case generation for Plain text, cipher text for grain 1 2 8 AEAD stream cipher python code is not working....... import numpy as np

Test case generation for Plain text, cipher text for grain128AEAD stream cipher python code is not working.......
import numpy as np
class Grain128AEAD:
def __init__(self, key, iv):
self.lfsr = np.zeros(128, dtype=np.uint8)
self.nfsr = np.zeros(128, dtype=np.uint8)
key_bits = np.array([int(b) for b in format(key,'0128b')], dtype=np.uint8)
iv_bits = np.array([int(b) for b in format(iv,'096b')], dtype=np.uint8)
self.nfsr[:128]= key_bits
self.lfsr[:96]= iv_bits
self.lfsr[96:]=1 # Set remaining bits to 1
self.initialize()
def f(self, s):
return s[0]^ s[7]^ s[38]^ s[70]^ s[81]^ s[96]
def g(self, b):
return (b[0]^ b[26]^ b[56]^ b[91]^ b[96]^(b[3] & b[67])^
(b[11] & b[13])^(b[17] & b[18])^(b[27] & b[59])^
(b[40] & b[48])^(b[61] & b[65])^(b[68] & b[84]))
def h(self, b):
return (b[93]^ b[2]^ b[15]^ b[36]^ b[45]^ b[64]^ b[73]^
(b[89] & b[12])^(b[8] & b[13])^(b[20] & b[95])^
(b[42] & b[102])^(b[60] & b[79])^(b[94] & b[111]))
def initialize(self):
for _ in range(256):
f_out = self.f(self.lfsr)
g_out = self.g(self.nfsr)
h_out = self.h(self.nfsr)
self.lfsr = np.roll(self.lfsr,-1)
self.nfsr = np.roll(self.nfsr,-1)
self.lfsr[-1]= f_out ^ h_out
self.nfsr[-1]= g_out ^ h_out
def get_keystream_byte(self):
keystream =0
for i in range(8):
f_out = self.f(self.lfsr)
g_out = self.g(self.nfsr)
h_out = self.h(self.nfsr)
self.lfsr = np.roll(self.lfsr,-1)
self.nfsr = np.roll(self.nfsr,-1)
self.lfsr[-1]= f_out
self.nfsr[-1]= g_out
keystream |=(h_out << i)
return keystream
def encrypt(self, plaintext):
if isinstance(plaintext, str):
plaintext = plaintext.encode()
ciphertext = bytearray()
for byte in plaintext:
keystream_byte = self.get_keystream_byte()
ciphertext.append(byte ^ keystream_byte)
return bytes(ciphertext)
def decrypt(self, ciphertext):
return self.encrypt(ciphertext)
def generate_test_vectors():
key =0x0123456789ABCDEF0123456789ABCDEF
iv =0x0123456789ABCDEF0123456789
cipher = Grain128AEAD(key, iv)
test_vectors =[]
plaintexts =[
"Hello, World!",
"Cryptography",
"Secret Message",
"Confidential",
"Secure Data",
"Test Vector 6",
"Private Info",
"Protected",
"Classified",
"Important",
"Data Stream",
"Encryption",
"Algorithm",
"Security",
"Privacy",
"Information",
"Sensitive",
"Top Secret",
"Restricted",
"Final Test"
]
for plaintext in plaintexts:
cipher = Grain128AEAD(key, iv)
ciphertext = cipher.encrypt(plaintext)
test_vectors.append({
'plaintext': plaintext,
'ciphertext': ciphertext.hex(),
'length': len(plaintext)
})
return test_vectors
test_vectors = generate_test_vectors()
print("Grain-128AEAD Test Vectors:")
print("-"*80)
print(f"{'Plaintext':<20}|{'Ciphertext':<40}|{'Length':<6}")
print("-"*80)
for vector in test_vectors:
print(f"{vector['plaintext'][:20]:<20}|{vector['ciphertext'][:40]:<40}|{vector['length']:<6}")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!