Skip to content

Commit

Permalink
Merge pull request #34 from wadobo/cryptodome
Browse files Browse the repository at this point in the history
Cryptodome
  • Loading branch information
danigm authored Oct 2, 2018
2 parents d408328 + 53c2bf1 commit 213dc89
Show file tree
Hide file tree
Showing 21 changed files with 172 additions and 43 deletions.
33 changes: 33 additions & 0 deletions decide/base/migrations/0002_auto_20180921_1056.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 2.0 on 2018-09-21 10:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('base', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='key',
name='g',
field=models.BigIntegerField(),
),
migrations.AlterField(
model_name='key',
name='p',
field=models.BigIntegerField(),
),
migrations.AlterField(
model_name='key',
name='x',
field=models.BigIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='key',
name='y',
field=models.BigIntegerField(),
),
]
34 changes: 34 additions & 0 deletions decide/base/migrations/0003_auto_20180921_1119.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 2.0 on 2018-09-21 11:19

import base.models
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('base', '0002_auto_20180921_1056'),
]

operations = [
migrations.AlterField(
model_name='key',
name='g',
field=base.models.BigBigField(),
),
migrations.AlterField(
model_name='key',
name='p',
field=base.models.BigBigField(),
),
migrations.AlterField(
model_name='key',
name='x',
field=base.models.BigBigField(blank=True, null=True),
),
migrations.AlterField(
model_name='key',
name='y',
field=base.models.BigBigField(),
),
]
27 changes: 23 additions & 4 deletions decide/base/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
from django.db import models


class BigBigField(models.TextField):
def to_python(self, value):
if isinstance(value, str):
return int(value)
if value is None:
return 0
return int(str(value))

def get_prep_value(self, value):
if value is None:
return 0
return str(value)

def from_db_value(self, value, expression, connection):
if value is None:
return 0
return int(value)


class Auth(models.Model):
name = models.CharField(max_length=200)
url = models.URLField()
Expand All @@ -11,10 +30,10 @@ def __str__(self):


class Key(models.Model):
p = models.IntegerField()
g = models.IntegerField()
y = models.IntegerField()
x = models.IntegerField(blank=True, null=True)
p = BigBigField()
g = BigBigField()
y = BigBigField()
x = BigBigField(blank=True, null=True)

def __str__(self):
if self.x:
Expand Down
4 changes: 4 additions & 0 deletions decide/base/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Meta:


class KeySerializer(serializers.HyperlinkedModelSerializer):
p = serializers.IntegerField()
g = serializers.IntegerField()
y = serializers.IntegerField()

class Meta:
model = Key
fields = ('p', 'g', 'y')
2 changes: 1 addition & 1 deletion decide/booth/static/crypto/elgamal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ElGamal = {};
ElGamal.BITS = 8;
ElGamal.BITS = 256;

ElGamal.getRandomInteger = function(max) {
var bit_length = max.bitLength();
Expand Down
1 change: 1 addition & 0 deletions decide/booth/templates/booth/booth.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ <h2>{{ voting.question.desc }}</h2>
<script src="{% static "crypto/elgamal.js" %}"></script>

<script>
ElGamal.BITS = {{ KEYBITS }};
var bigpk = {
p: BigInt.fromJSONObject("{{voting.pub_key.p}}"),
g: BigInt.fromJSONObject("{{voting.pub_key.g}}"),
Expand Down
1 change: 1 addition & 0 deletions decide/booth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ def get_context_data(self, **kwargs):

context['store_url'] = settings.APIS.get('store', settings.BASEURL)
context['auth_url'] = settings.APIS.get('authentication', settings.BASEURL)
context['KEYBITS'] = settings.KEYBITS

return context
3 changes: 3 additions & 0 deletions decide/decide/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@

STATIC_URL = '/static/'

# number of bits for the key, all auths should use the same number of bits
KEYBITS = 256

try:
from local_settings import *
except ImportError:
Expand Down
32 changes: 18 additions & 14 deletions decide/local_settings.example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ALLOWED_HOSTS = ["*"]

# Modules in use, commented modules that you won't use
MODULES = [
'authentication',
Expand All @@ -12,26 +14,28 @@
]

APIS = {
'authentication': 'http://localhost:8000',
'base': 'http://localhost:8000',
'booth': 'http://localhost:8000',
'census': 'http://localhost:8000',
'mixnet': 'http://localhost:8000',
'postproc': 'http://localhost:8000',
'store': 'http://localhost:8000',
'visualizer': 'http://localhost:8000',
'voting': 'http://localhost:8000',
'authentication': 'http://10.5.0.1:8000',
'base': 'http://10.5.0.1:8000',
'booth': 'http://10.5.0.1:8000',
'census': 'http://10.5.0.1:8000',
'mixnet': 'http://10.5.0.1:8000',
'postproc': 'http://10.5.0.1:8000',
'store': 'http://10.5.0.1:8000',
'visualizer': 'http://10.5.0.1:8000',
'voting': 'http://10.5.0.1:8000',
}

BASEURL = 'http://localhost:8000'
BASEURL = 'http://10.5.0.1:8000'

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'decide',
'USER': 'decide',
'PASSWORD': 'decide',
'HOST': 'localhost',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': '5432',
}
}

# number of bits for the key, all auths should use the same number of bits
KEYBITS = 256
23 changes: 12 additions & 11 deletions decide/mixnet/mixcrypt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
>>> B = 64
>>> B = 256
>>> k1 = MixCrypt(bits=B)
>>> k2 = MixCrypt(k=k1.k, bits=B)
>>> k3 = gen_multiple_key(k1, k2)
Expand All @@ -12,7 +12,7 @@
>>> sorted(clears) == sorted(d)
True
>>> B = 8
>>> B = 256
>>> k1 = MixCrypt(bits=B)
>>> k1.setk(167,156,89,130) #doctest: +ELLIPSIS
<Crypto.PublicKey.ElGamal.ElGamalobj object at 0x...>
Expand Down Expand Up @@ -44,8 +44,8 @@

def rand(p):
while True:
k = random.StrongRandom().randint(1, p - 1)
if GCD(k, p - 1) == 1: break
k = random.StrongRandom().randint(1, int(p) - 1)
if GCD(k, int(p) - 1) == 1: break
return k


Expand Down Expand Up @@ -75,7 +75,7 @@ def multiple_decrypt_shuffle(ciphers, *crypts):

def multiple_decrypt_shuffle2(ciphers, *crypts, pubkey=None):
'''
>>> B = 64
>>> B = 256
>>> k1 = MixCrypt(bits=B)
>>> k2 = MixCrypt(k=k1.k, bits=B)
>>> k3 = gen_multiple_key(k1, k2)
Expand Down Expand Up @@ -129,11 +129,11 @@ def encrypt(self, m, k=None):
r = rand(self.k.p)
if not k:
k = self.k
a, b = k.encrypt(m, r)
a, b = k._encrypt(m, r)
return a, b

def decrypt(self, c):
m = self.k.decrypt(c)
m = self.k._decrypt(c)
return m

def multiple_decrypt(self, msgs, last=True):
Expand Down Expand Up @@ -164,7 +164,7 @@ def shuffle_decrypt(self, msgs, last=True):

def reencrypt(self, cipher, pubkey=None):
'''
>>> B = 64
>>> B = 256
>>> k = MixCrypt(bits=B)
>>> clears = [random.StrongRandom().randint(1, B) for i in range(5)]
>>> cipher = [k.encrypt(i) for i in clears]
Expand All @@ -183,10 +183,11 @@ def reencrypt(self, cipher, pubkey=None):
else:
k = self.k

a, b = cipher
a1, b1 = self.encrypt(1, k=k)
a, b = map(int, cipher)
a1, b1 = map(int, self.encrypt(1, k=k))
p = int(k.p)

return ((a * a1) % k.p, (b * b1) % k.p)
return ((a * a1) % p, (b * b1) % p)

def gen_perm(self, l):
x = list(range(l))
Expand Down
8 changes: 4 additions & 4 deletions decide/mixnet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from base import mods
from base.models import Auth, Key
from base.serializers import AuthSerializer
from django.conf import settings


# number of bits for the key, all auths should use the same number of bits
# TODO: move this to the settings
B = 8
B = settings.KEYBITS


class Mixnet(models.Model):
Expand Down Expand Up @@ -45,14 +45,14 @@ def gen_key(self, p=0, g=0):
k = crypt.setk(self.key.p, self.key.g, self.key.y, self.key.x)
elif (not g or not p):
k = crypt.genk()
key = Key(p=k.p, g=k.g, y=k.y, x=k.x)
key = Key(p=int(k.p), g=int(k.g), y=int(k.y), x=int(k.x))
key.save()

self.key = key
self.save()
else:
k = crypt.getk(p, g)
key = Key(p=k.p, g=k.g, y=k.y, x=k.x)
key = Key(p=int(k.p), g=int(k.g), y=int(k.y), x=int(k.x))
key.save()

self.key = key
Expand Down
3 changes: 2 additions & 1 deletion decide/mixnet/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.test import TestCase
from django.conf import settings
from rest_framework.test import APIClient
from rest_framework.test import APITestCase

Expand All @@ -17,7 +18,7 @@ def setUp(self):
def tearDown(self):
self.client = None

def encrypt_msgs(self, msgs, pk, bits=8):
def encrypt_msgs(self, msgs, pk, bits=settings.KEYBITS):
p, g, y = pk
k = MixCrypt(bits=bits)
k.k = ElGamal.construct((p, g, y))
Expand Down
24 changes: 24 additions & 0 deletions decide/store/migrations/0003_auto_20180921_1522.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.0 on 2018-09-21 15:22

import base.models
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('store', '0002_vote_voted'),
]

operations = [
migrations.AlterField(
model_name='vote',
name='a',
field=base.models.BigBigField(),
),
migrations.AlterField(
model_name='vote',
name='b',
field=base.models.BigBigField(),
),
]
5 changes: 3 additions & 2 deletions decide/store/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from django.db import models
from base.models import BigBigField


class Vote(models.Model):
voting_id = models.PositiveIntegerField()
voter_id = models.PositiveIntegerField()

a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
a = BigBigField()
b = BigBigField()

voted = models.DateTimeField(auto_now=True)

Expand Down
3 changes: 3 additions & 0 deletions decide/store/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@


class VoteSerializer(serializers.HyperlinkedModelSerializer):
a = serializers.IntegerField()
b = serializers.IntegerField()

class Meta:
model = Vote
fields = ('voting_id', 'voter_id', 'a', 'b')
2 changes: 1 addition & 1 deletion decide/test-scripts/js/crypto/elgamal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ElGamal = {};
ElGamal.BITS = 8;
ElGamal.BITS = 256;

ElGamal.getRandomInteger = function(max) {
var bit_length = max.bitLength();
Expand Down
2 changes: 1 addition & 1 deletion decide/test-scripts/test-decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
p, g, y, x = map(int, SK.split(','))
a, b = map(int, MSG.split(','))

k = MixCrypt(bits=8)
k = MixCrypt(bits=256)
k.k = ElGamal.construct((p, g, y, x))

print(k.decrypt((a, b)))
2 changes: 1 addition & 1 deletion decide/test-scripts/test-encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
MSG = sys.argv[2]

p, g, y = map(int, PK.split(','))
k = MixCrypt(bits=8)
k = MixCrypt(bits=256)
k.k = ElGamal.construct((p, g, y))

print(','.join(map(str, k.encrypt(int(MSG)))))
Loading

0 comments on commit 213dc89

Please sign in to comment.