Path: blob/main/crypto/libecc/scripts/expand_libecc.py
34869 views
#/*1# * Copyright (C) 2017 - This file is part of libecc project2# *3# * Authors:4# * Ryad BENADJILA <[email protected]>5# * Arnaud EBALARD <[email protected]>6# * Jean-Pierre FLORI <[email protected]>7# *8# * Contributors:9# * Nicolas VIVET <[email protected]>10# * Karim KHALFALLAH <[email protected]>11# *12# * This software is licensed under a dual BSD and GPL v2 license.13# * See LICENSE file at the root folder of the project.14# */15#! /usr/bin/env python1617import random, sys, re, math, os, getopt, glob, copy, hashlib, binascii, string, signal, base641819# External dependecy for SHA-320# It is an independent module, since hashlib has no support21# for SHA-3 functions for now22import sha32324# Handle Python 2/3 issues25def is_python_2():26if sys.version_info[0] < 3:27return True28else:29return False3031### Ctrl-C handler32def handler(signal, frame):33print("\nSIGINT caught: exiting ...")34exit(0)3536# Helper to ask the user for something37def get_user_input(prompt):38# Handle the Python 2/3 issue39if is_python_2() == False:40return input(prompt)41else:42return raw_input(prompt)4344##########################################################45#### Math helpers46def egcd(b, n):47x0, x1, y0, y1 = 1, 0, 0, 148while n != 0:49q, b, n = b // n, n, b % n50x0, x1 = x1, x0 - q * x151y0, y1 = y1, y0 - q * y152return b, x0, y05354def modinv(a, m):55g, x, y = egcd(a, m)56if g != 1:57raise Exception("Error: modular inverse does not exist")58else:59return x % m6061def compute_monty_coef(prime, pbitlen, wlen):62"""63Compute montgomery coeff r, r^2 and mpinv. pbitlen is the size64of p in bits. It is expected to be a multiple of word65bit size.66"""67r = (1 << int(pbitlen)) % prime68r_square = (1 << (2 * int(pbitlen))) % prime69mpinv = 2**wlen - (modinv(prime, 2**wlen))70return r, r_square, mpinv7172def compute_div_coef(prime, pbitlen, wlen):73"""74Compute division coeffs p_normalized, p_shift and p_reciprocal.75"""76tmp = prime77cnt = 078while tmp != 0:79tmp = tmp >> 180cnt += 181pshift = int(pbitlen - cnt)82primenorm = prime << pshift83B = 2**wlen84prec = B**3 // ((primenorm >> int(pbitlen - 2*wlen)) + 1) - B85return pshift, primenorm, prec8687def is_probprime(n):88# ensure n is odd89if n % 2 == 0:90return False91# write n-1 as 2**s * d92# repeatedly try to divide n-1 by 293s = 094d = n-195while True:96quotient, remainder = divmod(d, 2)97if remainder == 1:98break99s += 1100d = quotient101assert(2**s * d == n-1)102# test the base a to see whether it is a witness for the compositeness of n103def try_composite(a):104if pow(a, d, n) == 1:105return False106for i in range(s):107if pow(a, 2**i * d, n) == n-1:108return False109return True # n is definitely composite110for i in range(5):111a = random.randrange(2, n)112if try_composite(a):113return False114return True # no base tested showed n as composite115116def legendre_symbol(a, p):117ls = pow(a, (p - 1) // 2, p)118return -1 if ls == p - 1 else ls119120# Tonelli-Shanks algorithm to find square roots121# over prime fields122def mod_sqrt(a, p):123# Square root of 0 is 0124if a == 0:125return 0126# Simple cases127if legendre_symbol(a, p) != 1:128# No square residue129return None130elif p == 2:131return a132elif p % 4 == 3:133return pow(a, (p + 1) // 4, p)134s = p - 1135e = 0136while s % 2 == 0:137s = s // 2138e += 1139n = 2140while legendre_symbol(n, p) != -1:141n += 1142x = pow(a, (s + 1) // 2, p)143b = pow(a, s, p)144g = pow(n, s, p)145r = e146while True:147t = b148m = 0149if is_python_2():150for m in xrange(r):151if t == 1:152break153t = pow(t, 2, p)154else:155for m in range(r):156if t == 1:157break158t = pow(t, 2, p)159if m == 0:160return x161gs = pow(g, 2 ** (r - m - 1), p)162g = (gs * gs) % p163x = (x * gs) % p164b = (b * g) % p165r = m166167##########################################################168### Math elliptic curves basic blocks169170# WARNING: these blocks are only here for testing purpose and171# are not intended to be used in a security oriented library!172# This explains the usage of naive affine coordinates fomulas173class Curve(object):174def __init__(self, a, b, prime, order, cofactor, gx, gy, npoints, name, oid):175self.a = a176self.b = b177self.p = prime178self.q = order179self.c = cofactor180self.gx = gx181self.gy = gy182self.n = npoints183self.name = name184self.oid = oid185# Equality testing186def __eq__(self, other):187return self.__dict__ == other.__dict__188# Deep copy is implemented using the ~X operator189def __invert__(self):190return copy.deepcopy(self)191192193class Point(object):194# Affine coordinates (x, y), infinity point is (None, None)195def __init__(self, curve, x, y):196self.curve = curve197if x != None:198self.x = (x % curve.p)199else:200self.x = None201if y != None:202self.y = (y % curve.p)203else:204self.y = None205# Check that the point is indeed on the curve206if (x != None):207if (pow(y, 2, curve.p) != ((pow(x, 3, curve.p) + (curve.a * x) + curve.b ) % curve.p)):208raise Exception("Error: point is not on curve!")209# Addition210def __add__(self, Q):211x1 = self.x212y1 = self.y213x2 = Q.x214y2 = Q.y215curve = self.curve216# Check that we are on the same curve217if Q.curve != curve:218raise Exception("Point add error: two point don't have the same curve")219# If Q is infinity point, return ourself220if Q.x == None:221return Point(self.curve, self.x, self.y)222# If we are the infinity point return Q223if self.x == None:224return Q225# Infinity point or Doubling226if (x1 == x2):227if (((y1 + y2) % curve.p) == 0):228# Return infinity point229return Point(self.curve, None, None)230else:231# Doubling232L = ((3*pow(x1, 2, curve.p) + curve.a) * modinv(2*y1, curve.p)) % curve.p233# Addition234else:235L = ((y2 - y1) * modinv((x2 - x1) % curve.p, curve.p)) % curve.p236resx = (pow(L, 2, curve.p) - x1 - x2) % curve.p237resy = ((L * (x1 - resx)) - y1) % curve.p238# Return the point239return Point(self.curve, resx, resy)240# Negation241def __neg__(self):242if (self.x == None):243return Point(self.curve, None, None)244else:245return Point(self.curve, self.x, -self.y)246# Subtraction247def __sub__(self, other):248return self + (-other)249# Scalar mul250def __rmul__(self, scalar):251# Implement simple double and add algorithm252P = self253Q = Point(P.curve, None, None)254for i in range(getbitlen(scalar), 0, -1):255Q = Q + Q256if (scalar >> (i-1)) & 0x1 == 0x1:257Q = Q + P258return Q259# Equality testing260def __eq__(self, other):261return self.__dict__ == other.__dict__262# Deep copy is implemented using the ~X operator263def __invert__(self):264return copy.deepcopy(self)265def __str__(self):266if self.x == None:267return "Inf"268else:269return ("(x = %s, y = %s)" % (hex(self.x), hex(self.y)))270271##########################################################272### Private and public keys structures273class PrivKey(object):274def __init__(self, curve, x):275self.curve = curve276self.x = x277278class PubKey(object):279def __init__(self, curve, Y):280# Sanity check281if Y.curve != curve:282raise Exception("Error: curve and point curve differ in public key!")283self.curve = curve284self.Y = Y285286class KeyPair(object):287def __init__(self, pubkey, privkey):288self.pubkey = pubkey289self.privkey = privkey290291292def fromprivkey(privkey, is_eckcdsa=False):293curve = privkey.curve294q = curve.q295gx = curve.gx296gy = curve.gy297G = Point(curve, gx, gy)298if is_eckcdsa == False:299return PubKey(curve, privkey.x * G)300else:301return PubKey(curve, modinv(privkey.x, q) * G)302303def genKeyPair(curve, is_eckcdsa=False):304p = curve.p305q = curve.q306gx = curve.gx307gy = curve.gy308G = Point(curve, gx, gy)309OK = False310while OK == False:311x = getrandomint(q)312if x == 0:313continue314OK = True315privkey = PrivKey(curve, x)316pubkey = fromprivkey(privkey, is_eckcdsa)317return KeyPair(pubkey, privkey)318319##########################################################320### Signature algorithms helpers321def getrandomint(modulo):322return random.randrange(0, modulo+1)323324def getbitlen(bint):325"""326Returns the number of bits encoding an integer327"""328if bint == None:329return 0330if bint == 0:331# Zero is encoded on one bit332return 1333else:334return int(bint).bit_length()335336def getbytelen(bint):337"""338Returns the number of bytes encoding an integer339"""340bitsize = getbitlen(bint)341bytesize = int(bitsize // 8)342if bitsize % 8 != 0:343bytesize += 1344return bytesize345346def stringtoint(bitstring):347acc = 0348size = len(bitstring)349for i in range(0, size):350acc = acc + (ord(bitstring[i]) * (2**(8*(size - 1 - i))))351return acc352353def inttostring(a):354size = int(getbytelen(a))355outstr = ""356for i in range(0, size):357outstr = outstr + chr((a >> (8*(size - 1 - i))) & 0xFF)358return outstr359360def expand(bitstring, bitlen, direction):361bytelen = int(math.ceil(bitlen / 8.))362if len(bitstring) >= bytelen:363return bitstring364else:365if direction == "LEFT":366return ((bytelen-len(bitstring))*"\x00") + bitstring367elif direction == "RIGHT":368return bitstring + ((bytelen-len(bitstring))*"\x00")369else:370raise Exception("Error: unknown direction "+direction+" in expand")371372def truncate(bitstring, bitlen, keep):373"""374Takes a bit string and truncates it to keep the left375most or the right most bits376"""377strbitlen = 8*len(bitstring)378# Check if truncation is needed379if strbitlen > bitlen:380if keep == "LEFT":381return expand(inttostring(stringtoint(bitstring) >> int(strbitlen - bitlen)), bitlen, "LEFT")382elif keep == "RIGHT":383mask = (2**bitlen)-1384return expand(inttostring(stringtoint(bitstring) & mask), bitlen, "LEFT")385else:386raise Exception("Error: unknown direction "+keep+" in truncate")387else:388# No need to truncate!389return bitstring390391##########################################################392### Hash algorithms393def sha224(message):394ctx = hashlib.sha224()395if(is_python_2() == True):396ctx.update(message)397digest = ctx.digest()398else:399ctx.update(message.encode('latin-1'))400digest = ctx.digest().decode('latin-1')401return (digest, ctx.digest_size, ctx.block_size)402403def sha256(message):404ctx = hashlib.sha256()405if(is_python_2() == True):406ctx.update(message)407digest = ctx.digest()408else:409ctx.update(message.encode('latin-1'))410digest = ctx.digest().decode('latin-1')411return (digest, ctx.digest_size, ctx.block_size)412413def sha384(message):414ctx = hashlib.sha384()415if(is_python_2() == True):416ctx.update(message)417digest = ctx.digest()418else:419ctx.update(message.encode('latin-1'))420digest = ctx.digest().decode('latin-1')421return (digest, ctx.digest_size, ctx.block_size)422423def sha512(message):424ctx = hashlib.sha512()425if(is_python_2() == True):426ctx.update(message)427digest = ctx.digest()428else:429ctx.update(message.encode('latin-1'))430digest = ctx.digest().decode('latin-1')431return (digest, ctx.digest_size, ctx.block_size)432433def sha3_224(message):434ctx = sha3.Sha3_ctx(224)435if(is_python_2() == True):436ctx.update(message)437digest = ctx.digest()438else:439ctx.update(message.encode('latin-1'))440digest = ctx.digest().decode('latin-1')441return (digest, ctx.digest_size, ctx.block_size)442443def sha3_256(message):444ctx = sha3.Sha3_ctx(256)445if(is_python_2() == True):446ctx.update(message)447digest = ctx.digest()448else:449ctx.update(message.encode('latin-1'))450digest = ctx.digest().decode('latin-1')451return (digest, ctx.digest_size, ctx.block_size)452453def sha3_384(message):454ctx = sha3.Sha3_ctx(384)455if(is_python_2() == True):456ctx.update(message)457digest = ctx.digest()458else:459ctx.update(message.encode('latin-1'))460digest = ctx.digest().decode('latin-1')461return (digest, ctx.digest_size, ctx.block_size)462463def sha3_512(message):464ctx = sha3.Sha3_ctx(512)465if(is_python_2() == True):466ctx.update(message)467digest = ctx.digest()468else:469ctx.update(message.encode('latin-1'))470digest = ctx.digest().decode('latin-1')471return (digest, ctx.digest_size, ctx.block_size)472473##########################################################474### Signature algorithms475476# *| IUF - ECDSA signature477# *|478# *| UF 1. Compute h = H(m)479# *| F 2. If |h| > bitlen(q), set h to bitlen(q)480# *| leftmost (most significant) bits of h481# *| F 3. e = OS2I(h) mod q482# *| F 4. Get a random value k in ]0,q[483# *| F 5. Compute W = (W_x,W_y) = kG484# *| F 6. Compute r = W_x mod q485# *| F 7. If r is 0, restart the process at step 4.486# *| F 8. If e == rx, restart the process at step 4.487# *| F 9. Compute s = k^-1 * (xr + e) mod q488# *| F 10. If s is 0, restart the process at step 4.489# *| F 11. Return (r,s)490def ecdsa_sign(hashfunc, keypair, message, k=None):491privkey = keypair.privkey492# Get important parameters from the curve493p = privkey.curve.p494q = privkey.curve.q495gx = privkey.curve.gx496gy = privkey.curve.gy497G = Point(privkey.curve, gx, gy)498q_limit_len = getbitlen(q)499# Compute the hash500(h, _, _) = hashfunc(message)501# Truncate hash value502h = truncate(h, q_limit_len, "LEFT")503# Convert the hash value to an int504e = stringtoint(h) % q505OK = False506while OK == False:507if k == None:508k = getrandomint(q)509if k == 0:510continue511W = k * G512r = W.x % q513if r == 0:514continue515if e == r * privkey.x:516continue517s = (modinv(k, q) * ((privkey.x * r) + e)) % q518if s == 0:519continue520OK = True521return ((expand(inttostring(r), 8*getbytelen(q), "LEFT") + expand(inttostring(s), 8*getbytelen(q), "LEFT")), k)522523# *| IUF - ECDSA verification524# *|525# *| I 1. Reject the signature if r or s is 0.526# *| UF 2. Compute h = H(m)527# *| F 3. If |h| > bitlen(q), set h to bitlen(q)528# *| leftmost (most significant) bits of h529# *| F 4. Compute e = OS2I(h) mod q530# *| F 5. Compute u = (s^-1)e mod q531# *| F 6. Compute v = (s^-1)r mod q532# *| F 7. Compute W' = uG + vY533# *| F 8. If W' is the point at infinity, reject the signature.534# *| F 9. Compute r' = W'_x mod q535# *| F 10. Accept the signature if and only if r equals r'536def ecdsa_verify(hashfunc, keypair, message, sig):537pubkey = keypair.pubkey538# Get important parameters from the curve539p = pubkey.curve.p540q = pubkey.curve.q541gx = pubkey.curve.gx542gy = pubkey.curve.gy543q_limit_len = getbitlen(q)544G = Point(pubkey.curve, gx, gy)545# Extract r and s546if len(sig) != 2*getbytelen(q):547raise Exception("ECDSA verify: bad signature length!")548r = stringtoint(sig[0:int(len(sig)/2)])549s = stringtoint(sig[int(len(sig)/2):])550if r == 0 or s == 0:551return False552# Compute the hash553(h, _, _) = hashfunc(message)554# Truncate hash value555h = truncate(h, q_limit_len, "LEFT")556# Convert the hash value to an int557e = stringtoint(h) % q558u = (modinv(s, q) * e) % q559v = (modinv(s, q) * r) % q560W_ = (u * G) + (v * pubkey.Y)561if W_.x == None:562return False563r_ = W_.x % q564if r == r_:565return True566else:567return False568569def eckcdsa_genKeyPair(curve):570return genKeyPair(curve, True)571572# *| IUF - ECKCDSA signature573# *|574# *| IUF 1. Compute h = H(z||m)575# *| F 2. If hsize > bitlen(q), set h to bitlen(q)576# *| rightmost (less significant) bits of h.577# *| F 3. Get a random value k in ]0,q[578# *| F 4. Compute W = (W_x,W_y) = kG579# *| F 5. Compute r = h(FE2OS(W_x)).580# *| F 6. If hsize > bitlen(q), set r to bitlen(q)581# *| rightmost (less significant) bits of r.582# *| F 7. Compute e = OS2I(r XOR h) mod q583# *| F 8. Compute s = x(k - e) mod q584# *| F 9. if s == 0, restart at step 3.585# *| F 10. return (r,s)586def eckcdsa_sign(hashfunc, keypair, message, k=None):587privkey = keypair.privkey588# Get important parameters from the curve589p = privkey.curve.p590q = privkey.curve.q591gx = privkey.curve.gx592gy = privkey.curve.gy593G = Point(privkey.curve, gx, gy)594q_limit_len = getbitlen(q)595# Compute the certificate data596(_, _, hblocksize) = hashfunc("")597z = expand(inttostring(keypair.pubkey.Y.x), 8*getbytelen(p), "LEFT")598z = z + expand(inttostring(keypair.pubkey.Y.y), 8*getbytelen(p), "LEFT")599if len(z) > hblocksize:600# Truncate601z = truncate(z, 8*hblocksize, "LEFT")602else:603# Expand604z = expand(z, 8*hblocksize, "RIGHT")605# Compute the hash606(h, _, _) = hashfunc(z + message)607# Truncate hash value608h = truncate(h, 8 * int(math.ceil(q_limit_len / 8)), "RIGHT")609OK = False610while OK == False:611if k == None:612k = getrandomint(q)613if k == 0:614continue615W = k * G616(r, _, _) = hashfunc(expand(inttostring(W.x), 8*getbytelen(p), "LEFT"))617r = truncate(r, 8 * int(math.ceil(q_limit_len / 8)), "RIGHT")618e = (stringtoint(r) ^ stringtoint(h)) % q619s = (privkey.x * (k - e)) % q620if s == 0:621continue622OK = True623return (r + expand(inttostring(s), 8*getbytelen(q), "LEFT"), k)624625# *| IUF - ECKCDSA verification626# *|627# *| I 1. Check the length of r:628# *| - if hsize > bitlen(q), r must be of629# *| length bitlen(q)630# *| - if hsize <= bitlen(q), r must be of631# *| length hsize632# *| I 2. Check that s is in ]0,q[633# *| IUF 3. Compute h = H(z||m)634# *| F 4. If hsize > bitlen(q), set h to bitlen(q)635# *| rightmost (less significant) bits of h.636# *| F 5. Compute e = OS2I(r XOR h) mod q637# *| F 6. Compute W' = sY + eG, where Y is the public key638# *| F 7. Compute r' = h(FE2OS(W'x))639# *| F 8. If hsize > bitlen(q), set r' to bitlen(q)640# *| rightmost (less significant) bits of r'.641# *| F 9. Check if r == r'642def eckcdsa_verify(hashfunc, keypair, message, sig):643pubkey = keypair.pubkey644# Get important parameters from the curve645p = pubkey.curve.p646q = pubkey.curve.q647gx = pubkey.curve.gx648gy = pubkey.curve.gy649G = Point(pubkey.curve, gx, gy)650q_limit_len = getbitlen(q)651(_, hsize, hblocksize) = hashfunc("")652# Extract r and s653if (8*hsize) > q_limit_len:654r_len = int(math.ceil(q_limit_len / 8.))655else:656r_len = hsize657r = stringtoint(sig[0:int(r_len)])658s = stringtoint(sig[int(r_len):])659if (s >= q) or (s < 0):660return False661# Compute the certificate data662z = expand(inttostring(keypair.pubkey.Y.x), 8*getbytelen(p), "LEFT")663z = z + expand(inttostring(keypair.pubkey.Y.y), 8*getbytelen(p), "LEFT")664if len(z) > hblocksize:665# Truncate666z = truncate(z, 8*hblocksize, "LEFT")667else:668# Expand669z = expand(z, 8*hblocksize, "RIGHT")670# Compute the hash671(h, _, _) = hashfunc(z + message)672# Truncate hash value673h = truncate(h, 8 * int(math.ceil(q_limit_len / 8)), "RIGHT")674e = (r ^ stringtoint(h)) % q675W_ = (s * pubkey.Y) + (e * G)676(h, _, _) = hashfunc(expand(inttostring(W_.x), 8*getbytelen(p), "LEFT"))677r_ = truncate(h, 8 * int(math.ceil(q_limit_len / 8)), "RIGHT")678if stringtoint(r_) == r:679return True680else:681return False682683# *| IUF - ECFSDSA signature684# *|685# *| I 1. Get a random value k in ]0,q[686# *| I 2. Compute W = (W_x,W_y) = kG687# *| I 3. Compute r = FE2OS(W_x)||FE2OS(W_y)688# *| I 4. If r is an all zero string, restart the process at step 1.689# *| IUF 5. Compute h = H(r||m)690# *| F 6. Compute e = OS2I(h) mod q691# *| F 7. Compute s = (k + ex) mod q692# *| F 8. If s is 0, restart the process at step 1 (see c. below)693# *| F 9. Return (r,s)694def ecfsdsa_sign(hashfunc, keypair, message, k=None):695privkey = keypair.privkey696# Get important parameters from the curve697p = privkey.curve.p698q = privkey.curve.q699gx = privkey.curve.gx700gy = privkey.curve.gy701G = Point(privkey.curve, gx, gy)702OK = False703while OK == False:704if k == None:705k = getrandomint(q)706if k == 0:707continue708W = k * G709r = expand(inttostring(W.x), 8*getbytelen(p), "LEFT") + expand(inttostring(W.y), 8*getbytelen(p), "LEFT")710if stringtoint(r) == 0:711continue712(h, _, _) = hashfunc(r + message)713e = stringtoint(h) % q714s = (k + e * privkey.x) % q715if s == 0:716continue717OK = True718return (r + expand(inttostring(s), 8*getbytelen(q), "LEFT"), k)719720721# *| IUF - ECFSDSA verification722# *|723# *| I 1. Reject the signature if r is not a valid point on the curve.724# *| I 2. Reject the signature if s is not in ]0,q[725# *| IUF 3. Compute h = H(r||m)726# *| F 4. Convert h to an integer and then compute e = -h mod q727# *| F 5. compute W' = sG + eY, where Y is the public key728# *| F 6. Compute r' = FE2OS(W'_x)||FE2OS(W'_y)729# *| F 7. Accept the signature if and only if r equals r'730def ecfsdsa_verify(hashfunc, keypair, message, sig):731pubkey = keypair.pubkey732# Get important parameters from the curve733p = pubkey.curve.p734q = pubkey.curve.q735gx = pubkey.curve.gx736gy = pubkey.curve.gy737G = Point(pubkey.curve, gx, gy)738# Extract coordinates from r and s from signature739if len(sig) != (2*getbytelen(p)) + getbytelen(q):740raise Exception("ECFSDSA verify: bad signature length!")741wx = sig[:int(getbytelen(p))]742wy = sig[int(getbytelen(p)):int(2*getbytelen(p))]743r = wx + wy744s = stringtoint(sig[int(2*getbytelen(p)):int((2*getbytelen(p))+getbytelen(q))])745# Check r is on the curve746W = Point(pubkey.curve, stringtoint(wx), stringtoint(wy))747# Check s is in ]0,q[748if s == 0 or s > q:749raise Exception("ECFSDSA verify: s not in ]0,q[")750(h, _, _) = hashfunc(r + message)751e = (-stringtoint(h)) % q752W_ = s * G + e * pubkey.Y753r_ = expand(inttostring(W_.x), 8*getbytelen(p), "LEFT") + expand(inttostring(W_.y), 8*getbytelen(p), "LEFT")754if r == r_:755return True756else:757return False758759760# NOTE: ISO/IEC 14888-3 standard seems to diverge from the existing implementations761# of ECRDSA when treating the message hash, and from the examples of certificates provided762# in RFC 7091 and draft-deremin-rfc4491-bis. While in ISO/IEC 14888-3 it is explicitely asked763# to proceed with the hash of the message as big endian, the RFCs derived from the Russian764# standard expect the hash value to be treated as little endian when importing it as an integer765# (this discrepancy is exhibited and confirmed by test vectors present in ISO/IEC 14888-3, and766# by X.509 certificates present in the RFCs). This seems (to be confirmed) to be a discrepancy of767# ISO/IEC 14888-3 algorithm description that must be fixed there.768#769# In order to be conservative, libecc uses the Russian standard behavior as expected to be in line with770# other implemetations, but keeps the ISO/IEC 14888-3 behavior if forced/asked by the user using771# the USE_ISO14888_3_ECRDSA toggle. This allows to keep backward compatibility with previous versions of the772# library if needed.773774# *| IUF - ECRDSA signature775# *|776# *| UF 1. Compute h = H(m)777# *| F 2. Get a random value k in ]0,q[778# *| F 3. Compute W = (W_x,W_y) = kG779# *| F 4. Compute r = W_x mod q780# *| F 5. If r is 0, restart the process at step 2.781# *| F 6. Compute e = OS2I(h) mod q. If e is 0, set e to 1.782# *| NOTE: here, ISO/IEC 14888-3 and RFCs differ in the way e treated.783# *| e = OS2I(h) for ISO/IEC 14888-3, or e = OS2I(reversed(h)) when endianness of h784# *| is reversed for RFCs.785# *| F 7. Compute s = (rx + ke) mod q786# *| F 8. If s is 0, restart the process at step 2.787# *| F 11. Return (r,s)788def ecrdsa_sign(hashfunc, keypair, message, k=None, use_iso14888_divergence=False):789privkey = keypair.privkey790# Get important parameters from the curve791p = privkey.curve.p792q = privkey.curve.q793gx = privkey.curve.gx794gy = privkey.curve.gy795G = Point(privkey.curve, gx, gy)796(h, _, _) = hashfunc(message)797if use_iso14888_divergence == False:798# Reverse the endianness for Russian standard RFC ECRDSA (contrary to ISO/IEC 14888-3 case)799h = h[::-1]800OK = False801while OK == False:802if k == None:803k = getrandomint(q)804if k == 0:805continue806W = k * G807r = W.x % q808if r == 0:809continue810e = stringtoint(h) % q811if e == 0:812e = 1813s = ((r * privkey.x) + (k * e)) % q814if s == 0:815continue816OK = True817return (expand(inttostring(r), 8*getbytelen(q), "LEFT") + expand(inttostring(s), 8*getbytelen(q), "LEFT"), k)818819# *| IUF - ECRDSA verification820# *|821# *| UF 1. Check that r and s are both in ]0,q[822# *| F 2. Compute h = H(m)823# *| F 3. Compute e = OS2I(h)^-1 mod q824# *| NOTE: here, ISO/IEC 14888-3 and RFCs differ in the way e treated.825# *| e = OS2I(h) for ISO/IEC 14888-3, or e = OS2I(reversed(h)) when endianness of h826# *| is reversed for RFCs.827# *| F 4. Compute u = es mod q828# *| F 4. Compute v = -er mod q829# *| F 5. Compute W' = uG + vY = (W'_x, W'_y)830# *| F 6. Let's now compute r' = W'_x mod q831# *| F 7. Check r and r' are the same832def ecrdsa_verify(hashfunc, keypair, message, sig, use_iso14888_divergence=False):833pubkey = keypair.pubkey834# Get important parameters from the curve835p = pubkey.curve.p836q = pubkey.curve.q837gx = pubkey.curve.gx838gy = pubkey.curve.gy839G = Point(pubkey.curve, gx, gy)840# Extract coordinates from r and s from signature841if len(sig) != 2*getbytelen(q):842raise Exception("ECRDSA verify: bad signature length!")843r = stringtoint(sig[:int(getbytelen(q))])844s = stringtoint(sig[int(getbytelen(q)):int(2*getbytelen(q))])845if r == 0 or r > q:846raise Exception("ECRDSA verify: r not in ]0,q[")847if s == 0 or s > q:848raise Exception("ECRDSA verify: s not in ]0,q[")849(h, _, _) = hashfunc(message)850if use_iso14888_divergence == False:851# Reverse the endianness for Russian standard RFC ECRDSA (contrary to ISO/IEC 14888-3 case)852h = h[::-1]853e = modinv(stringtoint(h) % q, q)854u = (e * s) % q855v = (-e * r) % q856W_ = u * G + v * pubkey.Y857r_ = W_.x % q858if r == r_:859return True860else:861return False862863864# *| IUF - ECGDSA signature865# *|866# *| UF 1. Compute h = H(m). If |h| > bitlen(q), set h to bitlen(q)867# *| leftmost (most significant) bits of h868# *| F 2. Convert e = - OS2I(h) mod q869# *| F 3. Get a random value k in ]0,q[870# *| F 4. Compute W = (W_x,W_y) = kG871# *| F 5. Compute r = W_x mod q872# *| F 6. If r is 0, restart the process at step 4.873# *| F 7. Compute s = x(kr + e) mod q874# *| F 8. If s is 0, restart the process at step 4.875# *| F 9. Return (r,s)876def ecgdsa_sign(hashfunc, keypair, message, k=None):877privkey = keypair.privkey878# Get important parameters from the curve879p = privkey.curve.p880q = privkey.curve.q881gx = privkey.curve.gx882gy = privkey.curve.gy883G = Point(privkey.curve, gx, gy)884(h, _, _) = hashfunc(message)885q_limit_len = getbitlen(q)886# Truncate hash value887h = truncate(h, q_limit_len, "LEFT")888e = (-stringtoint(h)) % q889OK = False890while OK == False:891if k == None:892k = getrandomint(q)893if k == 0:894continue895W = k * G896r = W.x % q897if r == 0:898continue899s = (privkey.x * ((k * r) + e)) % q900if s == 0:901continue902OK = True903return (expand(inttostring(r), 8*getbytelen(q), "LEFT") + expand(inttostring(s), 8*getbytelen(q), "LEFT"), k)904905# *| IUF - ECGDSA verification906# *|907# *| I 1. Reject the signature if r or s is 0.908# *| UF 2. Compute h = H(m). If |h| > bitlen(q), set h to bitlen(q)909# *| leftmost (most significant) bits of h910# *| F 3. Compute e = OS2I(h) mod q911# *| F 4. Compute u = ((r^-1)e mod q)912# *| F 5. Compute v = ((r^-1)s mod q)913# *| F 6. Compute W' = uG + vY914# *| F 7. Compute r' = W'_x mod q915# *| F 8. Accept the signature if and only if r equals r'916def ecgdsa_verify(hashfunc, keypair, message, sig):917pubkey = keypair.pubkey918# Get important parameters from the curve919p = pubkey.curve.p920q = pubkey.curve.q921gx = pubkey.curve.gx922gy = pubkey.curve.gy923G = Point(pubkey.curve, gx, gy)924# Extract coordinates from r and s from signature925if len(sig) != 2*getbytelen(q):926raise Exception("ECGDSA verify: bad signature length!")927r = stringtoint(sig[:int(getbytelen(q))])928s = stringtoint(sig[int(getbytelen(q)):int(2*getbytelen(q))])929if r == 0 or r > q:930raise Exception("ECGDSA verify: r not in ]0,q[")931if s == 0 or s > q:932raise Exception("ECGDSA verify: s not in ]0,q[")933(h, _, _) = hashfunc(message)934q_limit_len = getbitlen(q)935# Truncate hash value936h = truncate(h, q_limit_len, "LEFT")937e = stringtoint(h) % q938r_inv = modinv(r, q)939u = (r_inv * e) % q940v = (r_inv * s) % q941W_ = u * G + v * pubkey.Y942r_ = W_.x % q943if r == r_:944return True945else:946return False947948# *| IUF - ECSDSA/ECOSDSA signature949# *|950# *| I 1. Get a random value k in ]0, q[951# *| I 2. Compute W = kG = (Wx, Wy)952# *| IUF 3. Compute r = H(Wx [|| Wy] || m)953# *| - In the normal version (ECSDSA), r = h(Wx || Wy || m).954# *| - In the optimized version (ECOSDSA), r = h(Wx || m).955# *| F 4. Compute e = OS2I(r) mod q956# *| F 5. if e == 0, restart at step 1.957# *| F 6. Compute s = (k + ex) mod q.958# *| F 7. if s == 0, restart at step 1.959# *| F 8. Return (r, s)960def ecsdsa_common_sign(hashfunc, keypair, message, optimized, k=None):961privkey = keypair.privkey962# Get important parameters from the curve963p = privkey.curve.p964q = privkey.curve.q965gx = privkey.curve.gx966gy = privkey.curve.gy967G = Point(privkey.curve, gx, gy)968OK = False969while OK == False:970if k == None:971k = getrandomint(q)972if k == 0:973continue974W = k * G975if optimized == False:976(r, _, _) = hashfunc(expand(inttostring(W.x), 8*getbytelen(p), "LEFT") + expand(inttostring(W.y), 8*getbytelen(p), "LEFT") + message)977else:978(r, _, _) = hashfunc(expand(inttostring(W.x), 8*getbytelen(p), "LEFT") + message)979e = stringtoint(r) % q980if e == 0:981continue982s = (k + (e * privkey.x)) % q983if s == 0:984continue985OK = True986return (r + expand(inttostring(s), 8*getbytelen(q), "LEFT"), k)987988def ecsdsa_sign(hashfunc, keypair, message, k=None):989return ecsdsa_common_sign(hashfunc, keypair, message, False, k)990991def ecosdsa_sign(hashfunc, keypair, message, k=None):992return ecsdsa_common_sign(hashfunc, keypair, message, True, k)993994# *| IUF - ECSDSA/ECOSDSA verification995# *|996# *| I 1. if s is not in ]0,q[, reject the signature.x997# *| I 2. Compute e = -r mod q998# *| I 3. If e == 0, reject the signature.999# *| I 4. Compute W' = sG + eY1000# *| IUF 5. Compute r' = H(W'x [|| W'y] || m)1001# *| - In the normal version (ECSDSA), r = h(W'x || W'y || m).1002# *| - In the optimized version (ECOSDSA), r = h(W'x || m).1003# *| F 6. Accept the signature if and only if r and r' are the same1004def ecsdsa_common_verify(hashfunc, keypair, message, sig, optimized):1005pubkey = keypair.pubkey1006# Get important parameters from the curve1007p = pubkey.curve.p1008q = pubkey.curve.q1009gx = pubkey.curve.gx1010gy = pubkey.curve.gy1011G = Point(pubkey.curve, gx, gy)1012(_, hlen, _) = hashfunc("")1013# Extract coordinates from r and s from signature1014if len(sig) != hlen + getbytelen(q):1015raise Exception("EC[O]SDSA verify: bad signature length!")1016r = stringtoint(sig[:int(hlen)])1017s = stringtoint(sig[int(hlen):int(hlen+getbytelen(q))])1018if s == 0 or s > q:1019raise Exception("EC[O]DSA verify: s not in ]0,q[")1020e = (-r) % q1021if e == 0:1022raise Exception("EC[O]DSA verify: e is null")1023W_ = s * G + e * pubkey.Y1024if optimized == False:1025(r_, _, _) = hashfunc(expand(inttostring(W_.x), 8*getbytelen(p), "LEFT") + expand(inttostring(W_.y), 8*getbytelen(p), "LEFT") + message)1026else:1027(r_, _, _) = hashfunc(expand(inttostring(W_.x), 8*getbytelen(p), "LEFT") + message)1028if sig[:int(hlen)] == r_:1029return True1030else:1031return False10321033def ecsdsa_verify(hashfunc, keypair, message, sig):1034return ecsdsa_common_verify(hashfunc, keypair, message, sig, False)10351036def ecosdsa_verify(hashfunc, keypair, message, sig):1037return ecsdsa_common_verify(hashfunc, keypair, message, sig, True)103810391040##########################################################1041### Generate self-tests for all the algorithms10421043all_hash_funcs = [ (sha224, "SHA224"), (sha256, "SHA256"), (sha384, "SHA384"), (sha512, "SHA512"), (sha3_224, "SHA3_224"), (sha3_256, "SHA3_256"), (sha3_384, "SHA3_384"), (sha3_512, "SHA3_512") ]10441045all_sig_algs = [ (ecdsa_sign, ecdsa_verify, genKeyPair, "ECDSA"),1046(eckcdsa_sign, eckcdsa_verify, eckcdsa_genKeyPair, "ECKCDSA"),1047(ecfsdsa_sign, ecfsdsa_verify, genKeyPair, "ECFSDSA"),1048(ecrdsa_sign, ecrdsa_verify, genKeyPair, "ECRDSA"),1049(ecgdsa_sign, ecgdsa_verify, eckcdsa_genKeyPair, "ECGDSA"),1050(ecsdsa_sign, ecsdsa_verify, genKeyPair, "ECSDSA"),1051(ecosdsa_sign, ecosdsa_verify, genKeyPair, "ECOSDSA"), ]105210531054curr_test = 01055def pretty_print_curr_test(num_test, total_gen_tests):1056num_decimal = int(math.log10(total_gen_tests))+11057format_buf = "%0"+str(num_decimal)+"d/%0"+str(num_decimal)+"d"1058sys.stdout.write('\b'*((2*num_decimal)+1))1059sys.stdout.flush()1060sys.stdout.write(format_buf % (num_test, total_gen_tests))1061if num_test == total_gen_tests:1062print("")1063return10641065def gen_self_test(curve, hashfunc, sig_alg_sign, sig_alg_verify, sig_alg_genkeypair, num, hashfunc_name, sig_alg_name, total_gen_tests):1066global curr_test1067curr_test = curr_test + 11068if num != 0:1069pretty_print_curr_test(curr_test, total_gen_tests)1070output_list = []1071for test_num in range(0, num):1072out_vectors = ""1073# Generate a random key pair1074keypair = sig_alg_genkeypair(curve)1075# Generate a random message with a random size1076size = getrandomint(256)1077if is_python_2():1078message = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(size)])1079else:1080message = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(size)])1081test_name = sig_alg_name + "_" + hashfunc_name + "_" + curve.name.upper() + "_" + str(test_num)1082# Sign the message1083(sig, k) = sig_alg_sign(hashfunc, keypair, message)1084# Check that everything is OK with a verify1085if sig_alg_verify(hashfunc, keypair, message, sig) != True:1086raise Exception("Error during self test generation: sig verify failed! "+test_name+ " / msg="+message+" / sig="+binascii.hexlify(sig)+" / k="+hex(k)+" / privkey.x="+hex(keypair.privkey.x))1087if sig_alg_name == "ECRDSA":1088out_vectors += "#ifndef USE_ISO14888_3_ECRDSA\n"1089# Now generate the test vector1090out_vectors += "#ifdef WITH_HASH_"+hashfunc_name.upper()+"\n"1091out_vectors += "#ifdef WITH_CURVE_"+curve.name.upper()+"\n"1092out_vectors += "#ifdef WITH_SIG_"+sig_alg_name.upper()+"\n"1093out_vectors += "/* "+test_name+" known test vectors */\n"1094out_vectors += "static int "+test_name+"_test_vectors_get_random(nn_t out, nn_src_t q)\n{\n"1095# k_buf MUST be exported padded to the length of q1096out_vectors += "\tconst u8 k_buf[] = "+bigint_to_C_array(k, getbytelen(curve.q))1097out_vectors += "\tint ret, cmp;\n\tret = nn_init_from_buf(out, k_buf, sizeof(k_buf)); EG(ret, err);\n\tret = nn_cmp(out, q, &cmp); EG(ret, err);\n\tret = (cmp >= 0) ? -1 : 0;\nerr:\n\treturn ret;\n}\n"1098out_vectors += "static const u8 "+test_name+"_test_vectors_priv_key[] = \n"+bigint_to_C_array(keypair.privkey.x, getbytelen(keypair.privkey.x))1099out_vectors += "static const u8 "+test_name+"_test_vectors_expected_sig[] = \n"+bigint_to_C_array(stringtoint(sig), len(sig))1100out_vectors += "static const ec_test_case "+test_name+"_test_case = {\n"1101out_vectors += "\t.name = \""+test_name+"\",\n"1102out_vectors += "\t.ec_str_p = &"+curve.name+"_str_params,\n"1103out_vectors += "\t.priv_key = "+test_name+"_test_vectors_priv_key,\n"1104out_vectors += "\t.priv_key_len = sizeof("+test_name+"_test_vectors_priv_key),\n"1105out_vectors += "\t.nn_random = "+test_name+"_test_vectors_get_random,\n"1106out_vectors += "\t.hash_type = "+hashfunc_name+",\n"1107out_vectors += "\t.msg = \""+message+"\",\n"1108out_vectors += "\t.msglen = "+str(len(message))+",\n"1109out_vectors += "\t.sig_type = "+sig_alg_name+",\n"1110out_vectors += "\t.exp_sig = "+test_name+"_test_vectors_expected_sig,\n"1111out_vectors += "\t.exp_siglen = sizeof("+test_name+"_test_vectors_expected_sig),\n};\n"1112out_vectors += "#endif /* WITH_HASH_"+hashfunc_name+" */\n"1113out_vectors += "#endif /* WITH_CURVE_"+curve.name+" */\n"1114out_vectors += "#endif /* WITH_SIG_"+sig_alg_name+" */\n"1115if sig_alg_name == "ECRDSA":1116out_vectors += "#endif /* !USE_ISO14888_3_ECRDSA */\n"1117out_name = ""1118if sig_alg_name == "ECRDSA":1119out_name += "#ifndef USE_ISO14888_3_ECRDSA"+"/* For "+test_name+" */\n"1120out_name += "#ifdef WITH_HASH_"+hashfunc_name.upper()+"/* For "+test_name+" */\n"1121out_name += "#ifdef WITH_CURVE_"+curve.name.upper()+"/* For "+test_name+" */\n"1122out_name += "#ifdef WITH_SIG_"+sig_alg_name.upper()+"/* For "+test_name+" */\n"1123out_name += "\t&"+test_name+"_test_case,\n"1124out_name += "#endif /* WITH_HASH_"+hashfunc_name+" for "+test_name+" */\n"1125out_name += "#endif /* WITH_CURVE_"+curve.name+" for "+test_name+" */\n"1126out_name += "#endif /* WITH_SIG_"+sig_alg_name+" for "+test_name+" */"1127if sig_alg_name == "ECRDSA":1128out_name += "\n#endif /* !USE_ISO14888_3_ECRDSA */"+"/* For "+test_name+" */"1129output_list.append((out_name, out_vectors))1130# In the specific case of ECRDSA, we also generate an ISO/IEC compatible test vector1131if sig_alg_name == "ECRDSA":1132out_vectors = ""1133(sig, k) = sig_alg_sign(hashfunc, keypair, message, use_iso14888_divergence=True)1134# Check that everything is OK with a verify1135if sig_alg_verify(hashfunc, keypair, message, sig, use_iso14888_divergence=True) != True:1136raise Exception("Error during self test generation: sig verify failed! "+test_name+ " / msg="+message+" / sig="+binascii.hexlify(sig)+" / k="+hex(k)+" / privkey.x="+hex(keypair.privkey.x))1137out_vectors += "#ifdef USE_ISO14888_3_ECRDSA\n"1138# Now generate the test vector1139out_vectors += "#ifdef WITH_HASH_"+hashfunc_name.upper()+"\n"1140out_vectors += "#ifdef WITH_CURVE_"+curve.name.upper()+"\n"1141out_vectors += "#ifdef WITH_SIG_"+sig_alg_name.upper()+"\n"1142out_vectors += "/* "+test_name+" known test vectors */\n"1143out_vectors += "static int "+test_name+"_test_vectors_get_random(nn_t out, nn_src_t q)\n{\n"1144# k_buf MUST be exported padded to the length of q1145out_vectors += "\tconst u8 k_buf[] = "+bigint_to_C_array(k, getbytelen(curve.q))1146out_vectors += "\tint ret, cmp;\n\tret = nn_init_from_buf(out, k_buf, sizeof(k_buf)); EG(ret, err);\n\tret = nn_cmp(out, q, &cmp); EG(ret, err);\n\tret = (cmp >= 0) ? -1 : 0;\nerr:\n\treturn ret;\n}\n"1147out_vectors += "static const u8 "+test_name+"_test_vectors_priv_key[] = \n"+bigint_to_C_array(keypair.privkey.x, getbytelen(keypair.privkey.x))1148out_vectors += "static const u8 "+test_name+"_test_vectors_expected_sig[] = \n"+bigint_to_C_array(stringtoint(sig), len(sig))1149out_vectors += "static const ec_test_case "+test_name+"_test_case = {\n"1150out_vectors += "\t.name = \""+test_name+"\",\n"1151out_vectors += "\t.ec_str_p = &"+curve.name+"_str_params,\n"1152out_vectors += "\t.priv_key = "+test_name+"_test_vectors_priv_key,\n"1153out_vectors += "\t.priv_key_len = sizeof("+test_name+"_test_vectors_priv_key),\n"1154out_vectors += "\t.nn_random = "+test_name+"_test_vectors_get_random,\n"1155out_vectors += "\t.hash_type = "+hashfunc_name+",\n"1156out_vectors += "\t.msg = \""+message+"\",\n"1157out_vectors += "\t.msglen = "+str(len(message))+",\n"1158out_vectors += "\t.sig_type = "+sig_alg_name+",\n"1159out_vectors += "\t.exp_sig = "+test_name+"_test_vectors_expected_sig,\n"1160out_vectors += "\t.exp_siglen = sizeof("+test_name+"_test_vectors_expected_sig),\n};\n"1161out_vectors += "#endif /* WITH_HASH_"+hashfunc_name+" */\n"1162out_vectors += "#endif /* WITH_CURVE_"+curve.name+" */\n"1163out_vectors += "#endif /* WITH_SIG_"+sig_alg_name+" */\n"1164out_vectors += "#endif /* USE_ISO14888_3_ECRDSA */\n"1165out_name = ""1166out_name += "#ifdef USE_ISO14888_3_ECRDSA"+"/* For "+test_name+" */\n"1167out_name += "#ifdef WITH_HASH_"+hashfunc_name.upper()+"/* For "+test_name+" */\n"1168out_name += "#ifdef WITH_CURVE_"+curve.name.upper()+"/* For "+test_name+" */\n"1169out_name += "#ifdef WITH_SIG_"+sig_alg_name.upper()+"/* For "+test_name+" */\n"1170out_name += "\t&"+test_name+"_test_case,\n"1171out_name += "#endif /* WITH_HASH_"+hashfunc_name+" for "+test_name+" */\n"1172out_name += "#endif /* WITH_CURVE_"+curve.name+" for "+test_name+" */\n"1173out_name += "#endif /* WITH_SIG_"+sig_alg_name+" for "+test_name+" */\n"1174out_name += "#endif /* USE_ISO14888_3_ECRDSA */"+"/* For "+test_name+" */"1175output_list.append((out_name, out_vectors))11761177return output_list11781179def gen_self_tests(curve, num):1180global curr_test1181curr_test = 01182total_gen_tests = len(all_hash_funcs) * len(all_sig_algs)1183vectors = [[ gen_self_test(curve, hashf, sign, verify, genkp, num, hash_name, sig_alg_name, total_gen_tests)1184for (hashf, hash_name) in all_hash_funcs ] for (sign, verify, genkp, sig_alg_name) in all_sig_algs ]1185return vectors11861187##########################################################1188### ASN.1 stuff1189def parse_DER_extract_size(derbuf):1190# Extract the size1191if ord(derbuf[0]) & 0x80 != 0:1192encoding_len_bytes = ord(derbuf[0]) & ~0x801193# Skip1194base = 11195else:1196encoding_len_bytes = 11197base = 01198if len(derbuf) < encoding_len_bytes+1:1199return (False, 0, 0)1200else:1201length = stringtoint(derbuf[base:base+encoding_len_bytes])1202if len(derbuf) < length+encoding_len_bytes:1203return (False, 0, 0)1204else:1205return (True, encoding_len_bytes+base, length)12061207def extract_DER_object(derbuf, object_tag):1208# Check type1209if ord(derbuf[0]) != object_tag:1210# Not the type we expect ...1211return (False, 0, "")1212else:1213derbuf = derbuf[1:]1214# Extract the size1215(check, encoding_len, size) = parse_DER_extract_size(derbuf)1216if check == False:1217return (False, 0, "")1218else:1219if len(derbuf) < encoding_len + size:1220return (False, 0, "")1221else:1222return (True, size+encoding_len+1, derbuf[encoding_len:encoding_len+size])12231224def extract_DER_sequence(derbuf):1225return extract_DER_object(derbuf, 0x30)12261227def extract_DER_integer(derbuf):1228return extract_DER_object(derbuf, 0x02)12291230def extract_DER_octetstring(derbuf):1231return extract_DER_object(derbuf, 0x04)12321233def extract_DER_bitstring(derbuf):1234return extract_DER_object(derbuf, 0x03)12351236def extract_DER_oid(derbuf):1237return extract_DER_object(derbuf, 0x06)12381239# See ECParameters sequence in RFC 32791240def parse_DER_ECParameters(derbuf):1241# XXX: this is a very ugly way of extracting the information1242# regarding an EC curve, but since the ASN.1 structure is quite1243# "static", this might be sufficient without embedding a full1244# ASN.1 parser ...1245# Default return (a, b, prime, order, cofactor, gx, gy)1246default_ret = (0, 0, 0, 0, 0, 0, 0)1247# Get ECParameters wrapping sequence1248(check, size_ECParameters, ECParameters) = extract_DER_sequence(derbuf)1249if check == False:1250return (False, default_ret)1251# Get integer1252(check, size_ECPVer, ECPVer) = extract_DER_integer(ECParameters)1253if check == False:1254return (False, default_ret)1255# Get sequence1256(check, size_FieldID, FieldID) = extract_DER_sequence(ECParameters[size_ECPVer:])1257if check == False:1258return (False, default_ret)1259# Get OID1260(check, size_Oid, Oid) = extract_DER_oid(FieldID)1261if check == False:1262return (False, default_ret)1263# Does the OID correspond to a prime field?1264if(Oid != "\x2A\x86\x48\xCE\x3D\x01\x01"):1265print("DER parse error: only prime fields are supported ...")1266return (False, default_ret)1267# Get prime p of prime field1268(check, size_P, P) = extract_DER_integer(FieldID[size_Oid:])1269if check == False:1270return (False, default_ret)1271# Get curve (sequence)1272(check, size_Curve, Curve) = extract_DER_sequence(ECParameters[size_ECPVer+size_FieldID:])1273if check == False:1274return (False, default_ret)1275# Get A in curve1276(check, size_A, A) = extract_DER_octetstring(Curve)1277if check == False:1278return (False, default_ret)1279# Get B in curve1280(check, size_B, B) = extract_DER_octetstring(Curve[size_A:])1281if check == False:1282return (False, default_ret)1283# Get ECPoint1284(check, size_ECPoint, ECPoint) = extract_DER_octetstring(ECParameters[size_ECPVer+size_FieldID+size_Curve:])1285if check == False:1286return (False, default_ret)1287# Get Order1288(check, size_Order, Order) = extract_DER_integer(ECParameters[size_ECPVer+size_FieldID+size_Curve+size_ECPoint:])1289if check == False:1290return (False, default_ret)1291# Get Cofactor1292(check, size_Cofactor, Cofactor) = extract_DER_integer(ECParameters[size_ECPVer+size_FieldID+size_Curve+size_ECPoint+size_Order:])1293if check == False:1294return (False, default_ret)1295# If we end up here, everything is OK, we can extract all our elements1296prime = stringtoint(P)1297a = stringtoint(A)1298b = stringtoint(B)1299order = stringtoint(Order)1300cofactor = stringtoint(Cofactor)1301# Extract Gx and Gy, see X9.62-19981302if len(ECPoint) < 1:1303return (False, default_ret)1304ECPoint_type = ord(ECPoint[0])1305if (ECPoint_type == 0x04) or (ECPoint_type == 0x06) or (ECPoint_type == 0x07):1306# Uncompressed and hybrid points1307if len(ECPoint[1:]) % 2 != 0:1308return (False, default_ret)1309ECPoint = ECPoint[1:]1310gx = stringtoint(ECPoint[:int(len(ECPoint)/2)])1311gy = stringtoint(ECPoint[int(len(ECPoint)/2):])1312elif (ECPoint_type == 0x02) or (ECPoint_type == 0x03):1313# Compressed point: uncompress it, see X9.62-1998 section 4.2.11314ECPoint = ECPoint[1:]1315gx = stringtoint(ECPoint)1316alpha = (pow(gx, 3, prime) + (a * gx) + b) % prime1317beta = mod_sqrt(alpha, prime)1318if (beta == None) or ((beta == 0) and (alpha != 0)):1319return (False, 0)1320if (beta & 0x1) == (ECPoint_type & 0x1):1321gy = beta1322else:1323gy = prime - beta1324else:1325print("DER parse error: hybrid points are unsupported!")1326return (False, default_ret)1327return (True, (a, b, prime, order, cofactor, gx, gy))13281329##########################################################1330### Text and format helpers1331def bigint_to_C_array(bint, size):1332"""1333Format a python big int to a C hex array1334"""1335hexstr = format(int(bint), 'x')1336# Left pad to the size!1337hexstr = ("0"*int((2*size)-len(hexstr)))+hexstr1338hexstr = ("0"*(len(hexstr) % 2))+hexstr1339out_str = "{\n"1340for i in range(0, len(hexstr) - 1, 2):1341if (i%16 == 0):1342if(i!=0):1343out_str += "\n"1344out_str += "\t"1345out_str += "0x"+hexstr[i:i+2]+", "1346out_str += "\n};\n"1347return out_str13481349def check_in_file(fname, pat):1350# See if the pattern is in the file.1351with open(fname) as f:1352if not any(re.search(pat, line) for line in f):1353return False # pattern does not occur in file so we are done.1354else:1355return True13561357def num_patterns_in_file(fname, pat):1358num_pat = 01359with open(fname) as f:1360for line in f:1361if re.search(pat, line):1362num_pat = num_pat+11363return num_pat13641365def file_replace_pattern(fname, pat, s_after):1366# first, see if the pattern is even in the file.1367with open(fname) as f:1368if not any(re.search(pat, line) for line in f):1369return # pattern does not occur in file so we are done.13701371# pattern is in the file, so perform replace operation.1372with open(fname) as f:1373out_fname = fname + ".tmp"1374out = open(out_fname, "w")1375for line in f:1376out.write(re.sub(pat, s_after, line))1377out.close()1378os.rename(out_fname, fname)13791380def file_remove_pattern(fname, pat):1381# first, see if the pattern is even in the file.1382with open(fname) as f:1383if not any(re.search(pat, line) for line in f):1384return # pattern does not occur in file so we are done.13851386# pattern is in the file, so perform remove operation.1387with open(fname) as f:1388out_fname = fname + ".tmp"1389out = open(out_fname, "w")1390for line in f:1391if not re.search(pat, line):1392out.write(line)1393out.close()13941395if os.path.exists(fname):1396remove_file(fname)1397os.rename(out_fname, fname)13981399def remove_file(fname):1400# Remove file1401os.remove(fname)14021403def remove_files_pattern(fpattern):1404[remove_file(x) for x in glob.glob(fpattern)]14051406def buffer_remove_pattern(buff, pat):1407if is_python_2() == False:1408buff = buff.decode('latin-1')1409if re.search(pat, buff) == None:1410return (False, buff) # pattern does not occur in file so we are done.1411# Remove the pattern1412buff = re.sub(pat, "", buff)1413return (True, buff)14141415def is_base64(s):1416s = ''.join([s.strip() for s in s.split("\n")])1417try:1418enc = base64.b64encode(base64.b64decode(s)).strip()1419if type(enc) is bytes:1420return enc == s.encode('latin-1')1421else:1422return enc == s1423except TypeError:1424return False14251426### Curve helpers1427def export_curve_int(curvename, intname, bigint, size):1428if bigint == None:1429out = "static const u8 "+curvename+"_"+intname+"[] = {\n\t0x00,\n};\n"1430out += "TO_EC_STR_PARAM_FIXED_SIZE("+curvename+"_"+intname+", 0);\n\n"1431else:1432out = "static const u8 "+curvename+"_"+intname+"[] = "+bigint_to_C_array(bigint, size)+"\n"1433out += "TO_EC_STR_PARAM("+curvename+"_"+intname+");\n\n"1434return out14351436def export_curve_string(curvename, stringname, stringvalue):1437out = "static const u8 "+curvename+"_"+stringname+"[] = \""+stringvalue+"\";\n"1438out += "TO_EC_STR_PARAM("+curvename+"_"+stringname+");\n\n"1439return out14401441def export_curve_struct(curvename, paramname, paramnamestr):1442return "\t."+paramname+" = &"+curvename+"_"+paramnamestr+"_str_param, \n"14431444def curve_params(name, prime, pbitlen, a, b, gx, gy, order, cofactor, oid, alpha_montgomery, gamma_montgomery, alpha_edwards):1445"""1446Take as input some elliptic curve parameters and generate the1447C parameters in a string1448"""1449bytesize = int(pbitlen / 8)1450if pbitlen % 8 != 0:1451bytesize += 11452# Compute the rounded word size for each word size1453if bytesize % 8 != 0:1454wordsbitsize64 = 8*((int(bytesize/8)+1)*8)1455else:1456wordsbitsize64 = 8*bytesize1457if bytesize % 4 != 0:1458wordsbitsize32 = 8*((int(bytesize/4)+1)*4)1459else:1460wordsbitsize32 = 8*bytesize1461if bytesize % 2 != 0:1462wordsbitsize16 = 8*((int(bytesize/2)+1)*2)1463else:1464wordsbitsize16 = 8*bytesize1465# Compute some parameters1466(r64, r_square64, mpinv64) = compute_monty_coef(prime, wordsbitsize64, 64)1467(r32, r_square32, mpinv32) = compute_monty_coef(prime, wordsbitsize32, 32)1468(r16, r_square16, mpinv16) = compute_monty_coef(prime, wordsbitsize16, 16)1469# Compute p_reciprocal for each word size1470(pshift64, primenorm64, p_reciprocal64) = compute_div_coef(prime, wordsbitsize64, 64)1471(pshift32, primenorm32, p_reciprocal32) = compute_div_coef(prime, wordsbitsize32, 32)1472(pshift16, primenorm16, p_reciprocal16) = compute_div_coef(prime, wordsbitsize16, 16)1473# Compute the number of points on the curve1474npoints = order * cofactor14751476# Now output the parameters1477ec_params_string = "#include <libecc/lib_ecc_config.h>\n"1478ec_params_string += "#ifdef WITH_CURVE_"+name.upper()+"\n\n"1479ec_params_string += "#ifndef __EC_PARAMS_"+name.upper()+"_H__\n"1480ec_params_string += "#define __EC_PARAMS_"+name.upper()+"_H__\n"1481ec_params_string += "#include <libecc/curves/known/ec_params_external.h>\n"1482ec_params_string += export_curve_int(name, "p", prime, bytesize)14831484ec_params_string += "#define CURVE_"+name.upper()+"_P_BITLEN "+str(pbitlen)+"\n"1485ec_params_string += export_curve_int(name, "p_bitlen", pbitlen, getbytelen(pbitlen))14861487ec_params_string += "#if (WORD_BYTES == 8) /* 64-bit words */\n"1488ec_params_string += export_curve_int(name, "r", r64, getbytelen(r64))1489ec_params_string += export_curve_int(name, "r_square", r_square64, getbytelen(r_square64))1490ec_params_string += export_curve_int(name, "mpinv", mpinv64, getbytelen(mpinv64))1491ec_params_string += export_curve_int(name, "p_shift", pshift64, getbytelen(pshift64))1492ec_params_string += export_curve_int(name, "p_normalized", primenorm64, getbytelen(primenorm64))1493ec_params_string += export_curve_int(name, "p_reciprocal", p_reciprocal64, getbytelen(p_reciprocal64))1494ec_params_string += "#elif (WORD_BYTES == 4) /* 32-bit words */\n"1495ec_params_string += export_curve_int(name, "r", r32, getbytelen(r32))1496ec_params_string += export_curve_int(name, "r_square", r_square32, getbytelen(r_square32))1497ec_params_string += export_curve_int(name, "mpinv", mpinv32, getbytelen(mpinv32))1498ec_params_string += export_curve_int(name, "p_shift", pshift32, getbytelen(pshift32))1499ec_params_string += export_curve_int(name, "p_normalized", primenorm32, getbytelen(primenorm32))1500ec_params_string += export_curve_int(name, "p_reciprocal", p_reciprocal32, getbytelen(p_reciprocal32))1501ec_params_string += "#elif (WORD_BYTES == 2) /* 16-bit words */\n"1502ec_params_string += export_curve_int(name, "r", r16, getbytelen(r16))1503ec_params_string += export_curve_int(name, "r_square", r_square16, getbytelen(r_square16))1504ec_params_string += export_curve_int(name, "mpinv", mpinv16, getbytelen(mpinv16))1505ec_params_string += export_curve_int(name, "p_shift", pshift16, getbytelen(pshift16))1506ec_params_string += export_curve_int(name, "p_normalized", primenorm16, getbytelen(primenorm16))1507ec_params_string += export_curve_int(name, "p_reciprocal", p_reciprocal16, getbytelen(p_reciprocal16))1508ec_params_string += "#else /* unknown word size */\n"1509ec_params_string += "#error \"Unsupported word size\"\n"1510ec_params_string += "#endif\n\n"15111512ec_params_string += export_curve_int(name, "a", a, bytesize)1513ec_params_string += export_curve_int(name, "b", b, bytesize)15141515curve_order_bitlen = getbitlen(npoints)1516ec_params_string += "#define CURVE_"+name.upper()+"_CURVE_ORDER_BITLEN "+str(curve_order_bitlen)+"\n"1517ec_params_string += export_curve_int(name, "curve_order", npoints, getbytelen(npoints))15181519ec_params_string += export_curve_int(name, "gx", gx, bytesize)1520ec_params_string += export_curve_int(name, "gy", gy, bytesize)1521ec_params_string += export_curve_int(name, "gz", 0x01, bytesize)15221523qbitlen = getbitlen(order)15241525ec_params_string += export_curve_int(name, "gen_order", order, getbytelen(order))1526ec_params_string += "#define CURVE_"+name.upper()+"_Q_BITLEN "+str(qbitlen)+"\n"1527ec_params_string += export_curve_int(name, "gen_order_bitlen", qbitlen, getbytelen(qbitlen))15281529ec_params_string += export_curve_int(name, "cofactor", cofactor, getbytelen(cofactor))15301531ec_params_string += export_curve_int(name, "alpha_montgomery", alpha_montgomery, getbytelen(alpha_montgomery))1532ec_params_string += export_curve_int(name, "gamma_montgomery", gamma_montgomery, getbytelen(gamma_montgomery))1533ec_params_string += export_curve_int(name, "alpha_edwards", alpha_edwards, getbytelen(alpha_edwards))15341535ec_params_string += export_curve_string(name, "name", name.upper());15361537if oid == None:1538oid = ""1539ec_params_string += export_curve_string(name, "oid", oid);15401541ec_params_string += "static const ec_str_params "+name+"_str_params = {\n"+\1542export_curve_struct(name, "p", "p") +\1543export_curve_struct(name, "p_bitlen", "p_bitlen") +\1544export_curve_struct(name, "r", "r") +\1545export_curve_struct(name, "r_square", "r_square") +\1546export_curve_struct(name, "mpinv", "mpinv") +\1547export_curve_struct(name, "p_shift", "p_shift") +\1548export_curve_struct(name, "p_normalized", "p_normalized") +\1549export_curve_struct(name, "p_reciprocal", "p_reciprocal") +\1550export_curve_struct(name, "a", "a") +\1551export_curve_struct(name, "b", "b") +\1552export_curve_struct(name, "curve_order", "curve_order") +\1553export_curve_struct(name, "gx", "gx") +\1554export_curve_struct(name, "gy", "gy") +\1555export_curve_struct(name, "gz", "gz") +\1556export_curve_struct(name, "gen_order", "gen_order") +\1557export_curve_struct(name, "gen_order_bitlen", "gen_order_bitlen") +\1558export_curve_struct(name, "cofactor", "cofactor") +\1559export_curve_struct(name, "alpha_montgomery", "alpha_montgomery") +\1560export_curve_struct(name, "gamma_montgomery", "gamma_montgomery") +\1561export_curve_struct(name, "alpha_edwards", "alpha_edwards") +\1562export_curve_struct(name, "oid", "oid") +\1563export_curve_struct(name, "name", "name")1564ec_params_string += "};\n\n"15651566ec_params_string += "/*\n"+\1567" * Compute max bit length of all curves for p and q\n"+\1568" */\n"+\1569"#ifndef CURVES_MAX_P_BIT_LEN\n"+\1570"#define CURVES_MAX_P_BIT_LEN 0\n"+\1571"#endif\n"+\1572"#if (CURVES_MAX_P_BIT_LEN < CURVE_"+name.upper()+"_P_BITLEN)\n"+\1573"#undef CURVES_MAX_P_BIT_LEN\n"+\1574"#define CURVES_MAX_P_BIT_LEN CURVE_"+name.upper()+"_P_BITLEN\n"+\1575"#endif\n"+\1576"#ifndef CURVES_MAX_Q_BIT_LEN\n"+\1577"#define CURVES_MAX_Q_BIT_LEN 0\n"+\1578"#endif\n"+\1579"#if (CURVES_MAX_Q_BIT_LEN < CURVE_"+name.upper()+"_Q_BITLEN)\n"+\1580"#undef CURVES_MAX_Q_BIT_LEN\n"+\1581"#define CURVES_MAX_Q_BIT_LEN CURVE_"+name.upper()+"_Q_BITLEN\n"+\1582"#endif\n"+\1583"#ifndef CURVES_MAX_CURVE_ORDER_BIT_LEN\n"+\1584"#define CURVES_MAX_CURVE_ORDER_BIT_LEN 0\n"+\1585"#endif\n"+\1586"#if (CURVES_MAX_CURVE_ORDER_BIT_LEN < CURVE_"+name.upper()+"_CURVE_ORDER_BITLEN)\n"+\1587"#undef CURVES_MAX_CURVE_ORDER_BIT_LEN\n"+\1588"#define CURVES_MAX_CURVE_ORDER_BIT_LEN CURVE_"+name.upper()+"_CURVE_ORDER_BITLEN\n"+\1589"#endif\n\n"15901591ec_params_string += "/*\n"+\1592" * Compute and adapt max name and oid length\n"+\1593" */\n"+\1594"#ifndef MAX_CURVE_OID_LEN\n"+\1595"#define MAX_CURVE_OID_LEN 0\n"+\1596"#endif\n"+\1597"#ifndef MAX_CURVE_NAME_LEN\n"+\1598"#define MAX_CURVE_NAME_LEN 0\n"+\1599"#endif\n"+\1600"#if (MAX_CURVE_OID_LEN < "+str(len(oid)+1)+")\n"+\1601"#undef MAX_CURVE_OID_LEN\n"+\1602"#define MAX_CURVE_OID_LEN "+str(len(oid)+1)+"\n"+\1603"#endif\n"+\1604"#if (MAX_CURVE_NAME_LEN < "+str(len(name.upper())+1)+")\n"+\1605"#undef MAX_CURVE_NAME_LEN\n"+\1606"#define MAX_CURVE_NAME_LEN "+str(len(name.upper())+1)+"\n"+\1607"#endif\n\n"16081609ec_params_string += "#endif /* __EC_PARAMS_"+name.upper()+"_H__ */\n\n"+"#endif /* WITH_CURVE_"+name.upper()+" */\n"16101611return ec_params_string16121613def usage():1614print("This script is intented to *statically* expand the ECC library with user defined curves.")1615print("By statically we mean that the source code of libecc is expanded with new curves parameters through")1616print("automatic code generation filling place holders in the existing code base of the library. Though the")1617print("choice of static code generation versus dynamic curves import (such as what OpenSSL does) might be")1618print("argued, this choice has been driven by simplicity and security design decisions: we want libecc to have")1619print("all its parameters (such as memory consumption) set at compile time and statically adapted to the curves.")1620print("Since libecc only supports curves over prime fields, the script can only add this kind of curves.")1621print("This script implements elliptic curves and ISO signature algorithms from scratch over Python's multi-precision")1622print("big numbers library. Addition and doubling over curves use naive formulas. Please DO NOT use the functions of this")1623print("script for production code: they are not securely implemented and are very inefficient. Their only purpose is to expand")1624print("libecc and produce test vectors.")1625print("")1626print("In order to add a curve, there are two ways:")1627print("Adding a user defined curve with explicit parameters:")1628print("-----------------------------------------------------")1629print(sys.argv[0]+" --name=\"YOURCURVENAME\" --prime=... --order=... --a=... --b=... --gx=... --gy=... --cofactor=... --oid=THEOID")1630print("\t> name: name of the curve in the form of a string")1631print("\t> prime: prime number representing the curve prime field")1632print("\t> order: prime number representing the generator order")1633print("\t> cofactor: cofactor of the curve")1634print("\t> a: 'a' coefficient of the short Weierstrass equation of the curve")1635print("\t> b: 'b' coefficient of the short Weierstrass equation of the curve")1636print("\t> gx: x coordinate of the generator G")1637print("\t> gy: y coordinate of the generator G")1638print("\t> oid: optional OID of the curve")1639print(" Notes:")1640print(" ******")1641print("\t1) These elements are verified to indeed satisfy the curve equation.")1642print("\t2) All the numbers can be given either in decimal or hexadecimal format with a prepending '0x'.")1643print("\t3) The script automatically generates all the necessary files for the curve to be included in the library." )1644print("\tYou will find the new curve definition in the usual 'lib_ecc_config.h' file (one can activate it or not at compile time).")1645print("")1646print("Adding a user defined curve through RFC3279 ASN.1 parameters:")1647print("-------------------------------------------------------------")1648print(sys.argv[0]+" --name=\"YOURCURVENAME\" --ECfile=... --oid=THEOID")1649print("\t> ECfile: the DER or PEM encoded file containing the curve parameters (see RFC3279)")1650print(" Notes:")1651print("\tCurve parameters encoded in DER or PEM format can be generated with tools like OpenSSL (among others). As an illustrative example,")1652print("\tone can list all the supported curves under OpenSSL with:")1653print("\t $ openssl ecparam -list_curves")1654print("\tOnly the listed so called \"prime\" curves are supported. Then, one can extract an explicit curve representation in ASN.1")1655print("\tas defined in RFC3279, for example for BRAINPOOLP320R1:")1656print("\t $ openssl ecparam -param_enc explicit -outform DER -name brainpoolP320r1 -out brainpoolP320r1.der")1657print("")1658print("Removing user defined curves:")1659print("-----------------------------")1660print("\t*All the user defined curves can be removed with the --remove-all toggle.")1661print("\t*A specific named user define curve can be removed with the --remove toggle: in this case the --name option is used to ")1662print("\tlocate which named curve must be deleted.")1663print("")1664print("Test vectors:")1665print("-------------")1666print("\tTest vectors can be automatically generated and added to the library self tests when providing the --add-test-vectors=X toggle.")1667print("\tIn this case, X test vectors will be generated for *each* (curve, sign algorithm, hash algorithm) 3-uplet (beware of combinatorial")1668print("\tissues when X is big!). These tests are transparently added and compiled with the self tests.")1669return16701671def get_int(instring):1672if len(instring) == 0:1673return 01674if len(instring) >= 2:1675if instring[:2] == "0x":1676return int(instring, 16)1677return int(instring)16781679def parse_cmd_line(args):1680"""1681Get elliptic curve parameters from command line1682"""1683name = oid = prime = a = b = gx = gy = g = order = cofactor = ECfile = remove = remove_all = add_test_vectors = None1684alpha_montgomery = gamma_montgomery = alpha_edwards = None1685try:1686opts, args = getopt.getopt(sys.argv[1:], ":h", ["help", "remove", "remove-all", "name=", "prime=", "a=", "b=", "generator=", "gx=", "gy=", "order=", "cofactor=", "alpha_montgomery=","gamma_montgomery=", "alpha_edwards=", "ECfile=", "oid=", "add-test-vectors="])1687except getopt.GetoptError as err:1688# print help information and exit:1689print(err) # will print something like "option -a not recognized"1690usage()1691return False1692for o, arg in opts:1693if o in ("-h", "--help"):1694usage()1695return True1696elif o in ("--name"):1697name = arg1698# Prepend the custom string before name to avoid any collision1699name = "user_defined_"+name1700# Replace any unwanted name char1701name = re.sub("\-", "_", name)1702elif o in ("--oid="):1703oid = arg1704elif o in ("--prime"):1705prime = get_int(arg.replace(' ', ''))1706elif o in ("--a"):1707a = get_int(arg.replace(' ', ''))1708elif o in ("--b"):1709b = get_int(arg.replace(' ', ''))1710elif o in ("--gx"):1711gx = get_int(arg.replace(' ', ''))1712elif o in ("--gy"):1713gy = get_int(arg.replace(' ', ''))1714elif o in ("--generator"):1715g = arg.replace(' ', '')1716elif o in ("--order"):1717order = get_int(arg.replace(' ', ''))1718elif o in ("--cofactor"):1719cofactor = get_int(arg.replace(' ', ''))1720elif o in ("--alpha_montgomery"):1721alpha_montgomery = get_int(arg.replace(' ', ''))1722elif o in ("--gamma_montgomery"):1723gamma_montgomery = get_int(arg.replace(' ', ''))1724elif o in ("--alpha_edwards"):1725alpha_edwards = get_int(arg.replace(' ', ''))1726elif o in ("--remove"):1727remove = True1728elif o in ("--remove-all"):1729remove_all = True1730elif o in ("--add-test-vectors"):1731add_test_vectors = get_int(arg.replace(' ', ''))1732elif o in ("--ECfile"):1733ECfile = arg1734else:1735print("unhandled option")1736usage()1737return False17381739# File paths1740script_path = os.path.abspath(os.path.dirname(sys.argv[0])) + "/"1741ec_params_path = script_path + "../include/libecc/curves/user_defined/"1742curves_list_path = script_path + "../include/libecc/curves/"1743lib_ecc_types_path = script_path + "../include/libecc/"1744lib_ecc_config_path = script_path + "../include/libecc/"1745ec_self_tests_path = script_path + "../src/tests/"1746meson_options_path = script_path + "../"17471748# If remove is True, we have been asked to remove already existing user defined curves1749if remove == True:1750if name == None:1751print("--remove option expects a curve name provided with --name")1752return False1753asked = ""1754while asked != "y" and asked != "n":1755asked = get_user_input("You asked to remove everything related to user defined "+name.replace("user_defined_", "")+" curve. Enter y to confirm, n to cancel [y/n]. ")1756if asked == "n":1757print("NOT removing curve "+name.replace("user_defined_", "")+" (cancelled).")1758return True1759# Remove any user defined stuff with given name1760print("Removing user defined curve "+name.replace("user_defined_", "")+" ...")1761if name == None:1762print("Error: you must provide a curve name with --remove")1763return False1764file_remove_pattern(curves_list_path + "curves_list.h", ".*"+name+".*")1765file_remove_pattern(curves_list_path + "curves_list.h", ".*"+name.upper()+".*")1766file_remove_pattern(lib_ecc_types_path + "lib_ecc_types.h", ".*"+name.upper()+".*")1767file_remove_pattern(lib_ecc_config_path + "lib_ecc_config.h", ".*"+name.upper()+".*")1768file_remove_pattern(ec_self_tests_path + "ec_self_tests_core.h", ".*"+name+".*")1769file_remove_pattern(ec_self_tests_path + "ec_self_tests_core.h", ".*"+name.upper()+".*")1770file_remove_pattern(meson_options_path + "meson.options", ".*"+name.lower()+".*")1771try:1772remove_file(ec_params_path + "ec_params_"+name+".h")1773except:1774print("Error: curve name "+name+" does not seem to be present in the sources!")1775return False1776try:1777remove_file(ec_self_tests_path + "ec_self_tests_core_"+name+".h")1778except:1779print("Warning: curve name "+name+" self tests do not seem to be present ...")1780return True1781return True1782if remove_all == True:1783asked = ""1784while asked != "y" and asked != "n":1785asked = get_user_input("You asked to remove everything related to ALL user defined curves. Enter y to confirm, n to cancel [y/n]. ")1786if asked == "n":1787print("NOT removing user defined curves (cancelled).")1788return True1789# Remove any user defined stuff with given name1790print("Removing ALL user defined curves ...")1791# Remove any user defined stuff (whatever name)1792file_remove_pattern(curves_list_path + "curves_list.h", ".*user_defined.*")1793file_remove_pattern(curves_list_path + "curves_list.h", ".*USER_DEFINED.*")1794file_remove_pattern(lib_ecc_types_path + "lib_ecc_types.h", ".*USER_DEFINED.*")1795file_remove_pattern(lib_ecc_config_path + "lib_ecc_config.h", ".*USER_DEFINED.*")1796file_remove_pattern(ec_self_tests_path + "ec_self_tests_core.h", ".*USER_DEFINED.*")1797file_remove_pattern(ec_self_tests_path + "ec_self_tests_core.h", ".*user_defined.*")1798file_remove_pattern(meson_options_path + "meson.options", ".*user_defined.*")1799remove_files_pattern(ec_params_path + "ec_params_user_defined_*.h")1800remove_files_pattern(ec_self_tests_path + "ec_self_tests_core_user_defined_*.h")1801return True18021803# If a g is provided, split it in two gx and gy1804if g != None:1805if (len(g)/2)%2 == 0:1806gx = get_int(g[:len(g)/2])1807gy = get_int(g[len(g)/2:])1808else:1809# This is probably a generator encapsulated in a bit string1810if g[0:2] != "04":1811print("Error: provided generator g is not conforming!")1812return False1813else:1814g = g[2:]1815gx = get_int(g[:len(g)/2])1816gy = get_int(g[len(g)/2:])1817if ECfile != None:1818# ASN.1 DER input incompatible with other options1819if (prime != None) or (a != None) or (b != None) or (gx != None) or (gy != None) or (order != None) or (cofactor != None):1820print("Error: option ECfile incompatible with explicit (prime, a, b, gx, gy, order, cofactor) options!")1821return False1822# We need at least a name1823if (name == None):1824print("Error: option ECfile needs a curve name!")1825return False1826# Open the file1827try:1828buf = open(ECfile, 'rb').read()1829except:1830print("Error: cannot open ECfile file "+ECfile)1831return False1832# Check if we have a PEM or a DER file1833(check, derbuf) = buffer_remove_pattern(buf, "-----.*-----")1834if (check == True):1835# This a PEM file, proceed with base64 decoding1836if(is_base64(derbuf) == False):1837print("Error: error when decoding ECfile file "+ECfile+" (seems to be PEM, but failed to decode)")1838return False1839derbuf = base64.b64decode(derbuf)1840(check, (a, b, prime, order, cofactor, gx, gy)) = parse_DER_ECParameters(derbuf)1841if (check == False):1842print("Error: error when parsing ECfile file "+ECfile+" (malformed or unsupported ASN.1)")1843return False18441845else:1846if (prime == None) or (a == None) or (b == None) or (gx == None) or (gy == None) or (order == None) or (cofactor == None) or (name == None):1847err_string = (prime == None)*"prime "+(a == None)*"a "+(b == None)*"b "+(gx == None)*"gx "+(gy == None)*"gy "+(order == None)*"order "+(cofactor == None)*"cofactor "+(name == None)*"name "1848print("Error: missing "+err_string+" in explicit curve definition (name, prime, a, b, gx, gy, order, cofactor)!")1849print("See the help with -h or --help")1850return False18511852# Some sanity checks here1853# Check that prime is indeed a prime1854if is_probprime(prime) == False:1855print("Error: given prime is *NOT* prime!")1856return False1857if is_probprime(order) == False:1858print("Error: given order is *NOT* prime!")1859return False1860if (a > prime) or (b > prime) or (gx > prime) or (gy > prime):1861err_string = (a > prime)*"a "+(b > prime)*"b "+(gx > prime)*"gx "+(gy > prime)*"gy "1862print("Error: "+err_string+"is > prime")1863return False1864# Check that the provided generator is on the curve1865if pow(gy, 2, prime) != ((pow(gx, 3, prime) + (a*gx) + b) % prime):1866print("Error: the given parameters (prime, a, b, gx, gy) do not verify the elliptic curve equation!")1867return False18681869# Check Montgomery and Edwards transfer coefficients1870if ((alpha_montgomery != None) and (gamma_montgomery == None)) or ((alpha_montgomery == None) and (gamma_montgomery != None)):1871print("Error: alpha_montgomery and gamma_montgomery must be both defined if used!")1872return False1873if (alpha_edwards != None):1874if (alpha_montgomery == None) or (gamma_montgomery == None):1875print("Error: alpha_edwards needs alpha_montgomery and gamma_montgomery to be both defined if used!")1876return False18771878# Now that we have our parameters, call the function to get bitlen1879pbitlen = getbitlen(prime)1880ec_params = curve_params(name, prime, pbitlen, a, b, gx, gy, order, cofactor, oid, alpha_montgomery, gamma_montgomery, alpha_edwards)1881# Check if there is a name collision somewhere1882if os.path.exists(ec_params_path + "ec_params_"+name+".h") == True :1883print("Error: file %s already exists!" % (ec_params_path + "ec_params_"+name+".h"))1884return False1885if (check_in_file(curves_list_path + "curves_list.h", "ec_params_"+name+"_str_params") == True) or (check_in_file(curves_list_path + "curves_list.h", "WITH_CURVE_"+name.upper()+"\n") == True) or (check_in_file(lib_ecc_types_path + "lib_ecc_types.h", "WITH_CURVE_"+name.upper()+"\n") == True):1886print("Error: name %s already exists in files" % ("ec_params_"+name))1887return False1888# Create a new file with the parameters1889if not os.path.exists(ec_params_path):1890# Create the "user_defined" folder if it does not exist1891os.mkdir(ec_params_path)1892f = open(ec_params_path + "ec_params_"+name+".h", 'w')1893f.write(ec_params)1894f.close()1895# Include the file in curves_list.h1896magic = "ADD curves header here"1897magic_re = "\/\* "+magic+" \*\/"1898magic_back = "/* "+magic+" */"1899file_replace_pattern(curves_list_path + "curves_list.h", magic_re, "#include <libecc/curves/user_defined/ec_params_"+name+".h>\n"+magic_back)1900# Add the curve mapping1901magic = "ADD curves mapping here"1902magic_re = "\/\* "+magic+" \*\/"1903magic_back = "/* "+magic+" */"1904file_replace_pattern(curves_list_path + "curves_list.h", magic_re, "#ifdef WITH_CURVE_"+name.upper()+"\n\t{ .type = "+name.upper()+", .params = &"+name+"_str_params },\n#endif /* WITH_CURVE_"+name.upper()+" */\n"+magic_back)1905# Add the new curve type in the enum1906# First we get the number of already defined curves so that we increment the enum counter1907num_with_curve = num_patterns_in_file(lib_ecc_types_path + "lib_ecc_types.h", "#ifdef WITH_CURVE_")1908magic = "ADD curves type here"1909magic_re = "\/\* "+magic+" \*\/"1910magic_back = "/* "+magic+" */"1911file_replace_pattern(lib_ecc_types_path + "lib_ecc_types.h", magic_re, "#ifdef WITH_CURVE_"+name.upper()+"\n\t"+name.upper()+" = "+str(num_with_curve+1)+",\n#endif /* WITH_CURVE_"+name.upper()+" */\n"+magic_back)1912# Add the new curve define in the config1913magic = "ADD curves define here"1914magic_re = "\/\* "+magic+" \*\/"1915magic_back = "/* "+magic+" */"1916file_replace_pattern(lib_ecc_config_path + "lib_ecc_config.h", magic_re, "#define WITH_CURVE_"+name.upper()+"\n"+magic_back)1917# Add the new curve meson option in the meson.options file1918magic = "ADD curves meson option here"1919magic_re = "# " + magic1920magic_back = "# " + magic1921file_replace_pattern(meson_options_path + "meson.options", magic_re, "\t'"+name.lower()+"',\n"+magic_back)19221923# Do we need to add some test vectors?1924if add_test_vectors != None:1925print("Test vectors generation asked: this can take some time! Please wait ...")1926# Create curve1927c = Curve(a, b, prime, order, cofactor, gx, gy, cofactor * order, name, oid)1928# Generate key pair for the algorithm1929vectors = gen_self_tests(c, add_test_vectors)1930# Iterate through all the tests1931f = open(ec_self_tests_path + "ec_self_tests_core_"+name+".h", 'w')1932for l in vectors:1933for v in l:1934for case in v:1935(case_name, case_vector) = case1936# Add the new test case1937magic = "ADD curve test case here"1938magic_re = "\/\* "+magic+" \*\/"1939magic_back = "/* "+magic+" */"1940file_replace_pattern(ec_self_tests_path + "ec_self_tests_core.h", magic_re, case_name+"\n"+magic_back)1941# Create/Increment the header file1942f.write(case_vector)1943f.close()1944# Add the new test cases header1945magic = "ADD curve test vectors header here"1946magic_re = "\/\* "+magic+" \*\/"1947magic_back = "/* "+magic+" */"1948file_replace_pattern(ec_self_tests_path + "ec_self_tests_core.h", magic_re, "#include \"ec_self_tests_core_"+name+".h\"\n"+magic_back)1949return True195019511952#### Main1953if __name__ == "__main__":1954signal.signal(signal.SIGINT, handler)1955parse_cmd_line(sys.argv[1:])195619571958