http://git-wip-us.apache.org/repos/asf/incubator-milagro-crypto/blob/c25f9e5c/version3/c/x509.c
----------------------------------------------------------------------
diff --git a/version3/c/x509.c b/version3/c/x509.c
new file mode 100644
index 0000000..84f6c53
--- /dev/null
+++ b/version3/c/x509.c
@@ -0,0 +1,1097 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+/* AMCL X.509 Functions */
+
+// To run test program, define HAS_MAIN
+// gcc -std=c99 x509.c  amcl.a -o x509.exe
+
+//#define HAS_MAIN
+
+#include <stdio.h>
+#include "amcl.h"  // for octet support only
+#include "x509.h"
+
+// ASN.1 tags
+
+#define ANY 0x00
+#define SEQ 0x30
+#define OID 0x06
+#define INT 0x02
+#define NUL 0x05
+#define ZER 0x00
+#define UTF 0x0C
+#define UTC 0x17
+#define LOG 0x01
+#define BIT 0x03
+#define OCT 0x04
+#define STR 0x13
+#define SET 0x31
+#define IA5 0x16
+
+// Supported Encryption Methods
+
+#define ECC 1
+#define RSA 2
+
+// Supported Hash functions
+
+#define H256 2
+#define H384 3
+#define H512 4
+
+// Supported Curves
+
+#define NIST256 0    /**< For the NIST 256-bit standard curve - WEIERSTRASS 
only */
+#define C25519 1     /**< Bernstein's Modulus 2^255-19 - EDWARDS or MONTGOMERY 
only */
+#define BRAINPOOL 2  /**< For Brainpool 256-bit curve - WEIERSTRASS only */
+#define ANSSI 3      /**< For French 256-bit standard curve - WEIERSTRASS only 
*/
+#define NIST384 10   /**< For the NIST 384-bit standard curve - WEIERSTRASS 
only */
+#define NIST521 12   /**< For the NIST 521-bit standard curve - WEIERSTRASS 
only */
+
+
+// Define some OIDs
+
+// Elliptic Curve with SHA256
+static unsigned char eccsha256[8]= {0x2a,0x86,0x48,0xce,0x3d,0x04,0x03,0x02};
+static octet ECCSHA256= {8,sizeof(eccsha256),(char *)eccsha256};
+
+// Elliptic Curve with SHA384
+static unsigned char eccsha384[8]= {0x2a,0x86,0x48,0xce,0x3d,0x04,0x03,0x03};
+static octet ECCSHA384= {8,sizeof(eccsha384),(char *)eccsha384};
+
+// Elliptic Curve with SHA512
+static unsigned char eccsha512[8]= {0x2a,0x86,0x48,0xce,0x3d,0x04,0x03,0x04};
+static octet ECCSHA512= {8,sizeof(eccsha512),(char *)eccsha512};
+
+// EC Public Key
+static unsigned char ecpk[7]= {0x2a,0x86,0x48,0xce,0x3d,0x02,0x01};
+static octet ECPK= {7,sizeof(ecpk),(char *)ecpk};
+
+// C25519 curve
+static unsigned char prime25519[9]= 
{0x2B,0x06,0x01,0x04,0x01,0xDA,0x47,0x0F,0x01}; /*****/
+static octet PRIME25519= {9,sizeof(prime25519),(char *)prime25519};
+
+// NIST256 curve
+static unsigned char prime256v1[8]= {0x2a,0x86,0x48,0xce,0x3d,0x03,0x01,0x07};
+static octet PRIME256V1= {8,sizeof(prime256v1),(char *)prime256v1};
+
+// NIST384 curve
+static unsigned char secp384r1[5]= {0x2B,0x81,0x04,0x00,0x22};
+static octet SECP384R1= {5,sizeof(secp384r1),(char *)secp384r1};
+
+// NIST521 curve
+static unsigned char secp521r1[5]= {0x2B,0x81,0x04,0x00,0x23};
+static octet SECP521R1= {5,sizeof(secp521r1),(char *)secp521r1};
+
+// RSA Public Key
+static unsigned char rsapk[9]= {0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01};
+static octet RSAPK= {9,sizeof(rsapk),(char *)rsapk};
+
+// RSA with SHA256
+static unsigned char rsasha256[9]= 
{0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x0b};
+static octet RSASHA256= {9,sizeof(rsasha256),(char *)rsasha256};
+
+// RSA with SHA384
+static unsigned char rsasha384[9]= 
{0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x0c};
+static octet RSASHA384= {9,sizeof(rsasha384),(char *)rsasha384};
+
+// RSA with SHA512
+static unsigned char rsasha512[9]= 
{0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x0d};
+static octet RSASHA512= {9,sizeof(rsasha512),(char *)rsasha512};
+
+#ifdef HAS_MAIN
+// countryName
+static unsigned char cn[3]= {0x55,0x04,0x06};
+static octet CN= {3,sizeof(cn),(char *)cn};
+
+// stateName
+// static char sn[3]= {0x55,0x04,0x08};
+// static octet SN= {3,sizeof(sn),sn};
+
+// localName
+// static char ln[3]= {0x55,0x04,0x07};
+// static octet LN= {3,sizeof(ln),ln};
+
+// orgName
+static unsigned char on[3]= {0x55,0x04,0x0A};
+static octet ON= {3,sizeof(on),(char *)on};
+
+// unitName
+// static char un[3]= {0x55,0x04,0x0B};
+// static octet UN= {3,sizeof(un),un};
+
+// myName
+// static char mn[3]= {0x55,0x04,0x03};
+// static octet MN= {3,sizeof(mn),mn};
+
+// emailName
+static unsigned char en[9]= {0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x09,0x01};
+static octet EN= {9,sizeof(en),(char *)en};
+#endif // HAS_MAIN
+
+/* Check expected TAG and return ASN.1 field length. If tag=0 skip check. */
+static int getalen(int tag,char *b,int j)
+{
+    int len;
+
+    if (tag!=0 && (unsigned char)b[j]!=tag) return -1; // not a valid tag
+    j++;
+
+    if ((unsigned char)b[j]==0x81)
+    {
+        j++;
+        len=(unsigned char)b[j];
+    }
+    else if ((unsigned char)b[j]==0x82)
+    {
+        j++;
+        len=256*b[j++];
+        len+=(unsigned char)b[j];
+    }
+    else
+    {
+        len=(unsigned char)b[j];
+        if (len>127) return -1;
+    }
+    return len;
+}
+
+/* jump over length field */
+static int skip(int len)
+{
+    if (len<128) return 2;
+    if (len>=128 && len<256) return 3;
+    return 4;
+}
+
+/* round length up to nearest 8-byte length */
+static int bround(int len)
+{
+    if (len%8==0) return len;
+    return len+(8-len%8);
+
+}
+
+//     Input signed cert as octet, and extract signature
+//     Return 0 for failure, ECC for Elliptic Curve signature, RSA for RSA 
signature
+//  Note that signature type is not provided here - its the type of the public 
key that
+//  is used to verify it that matters, and which determines for example the 
curve to be used!
+
+pktype X509_extract_cert_sig(octet *sc,octet *sig)
+{
+    int i,j,k,fin,len,rlen,sj,ex;
+    char soid[9];
+    octet SOID= {0,sizeof(soid),soid};
+    pktype ret;
+
+    ret.type=0;
+    ret.hash=0;
+
+    j=0;
+
+    len=getalen(SEQ,sc->val,j);                // Check for expected SEQ 
clause, and get length
+    if (len<0) return ret;                     // if not a SEQ clause, there 
is a problem, exit
+    j+=skip(len);                                      // skip over length to 
clause contents. Add len to skip clause
+
+    if (len+j!=sc->len) return ret;
+
+    len=getalen(SEQ,sc->val,j);
+    if (len<0) return ret;
+    j+=skip(len)+len; // jump over cert to signature OID
+
+    len=getalen(SEQ,sc->val,j);
+    if (len<0) return ret;
+    j+=skip(len);
+
+    sj=j+len; // Needed to jump over signature OID
+
+// dive in to extract OID
+    len=getalen(OID,sc->val,j);
+    if (len<0) return ret;
+    j+=skip(len);
+
+    fin=j+len;
+    SOID.len=len;
+    for (i=0; j<fin; j++)
+        SOID.val[i++]= sc->val[j];
+
+    // check OID here..
+
+    if (OCT_comp(&ECCSHA256,&SOID))
+    {
+        ret.type=ECC;
+        ret.hash=H256;
+    }
+    if (OCT_comp(&ECCSHA384,&SOID))
+    {
+        ret.type=ECC;
+        ret.hash=H384;
+    }
+    if (OCT_comp(&ECCSHA512,&SOID))
+    {
+        ret.type=ECC;
+        ret.hash=H512;
+    }
+    if (OCT_comp(&RSASHA256,&SOID))
+    {
+        ret.type=RSA;
+        ret.hash=H256;
+    }
+    if (OCT_comp(&RSASHA384,&SOID))
+    {
+        ret.type=RSA;
+        ret.hash=H384;
+    }
+    if (OCT_comp(&RSASHA512,&SOID))
+    {
+        ret.type=RSA;
+        ret.hash=H512;
+    }
+
+    if (ret.type==0) return ret; // unsupported type
+
+    j=sj;  // jump out to signature
+
+    len=getalen(BIT,sc->val,j);
+    if (len<0)
+    {
+        ret.type=0;
+        return ret;
+    }
+    j+=skip(len);
+    j++;
+    len--; // skip bit shift (hopefully 0!)
+
+    if (ret.type==ECC)
+    {
+        // signature in the form (r,s)
+        len=getalen(SEQ,sc->val,j);
+        if (len<0)
+        {
+            ret.type=0;
+            return ret;
+        }
+        j+=skip(len);
+
+        // pick up r part of signature
+        len=getalen(INT,sc->val,j);
+        if (len<0)
+        {
+            ret.type=0;
+            return ret;
+        }
+        j+=skip(len);
+
+        if (sc->val[j]==0)
+        {
+            // skip leading zero
+            j++;
+            len--;
+        }
+        rlen=bround(len);
+
+        ex=rlen-len;
+        sig->len=2*rlen;
+
+        i=0;
+        for (k=0; k<ex; k++)
+            sig->val[i++]=0;
+
+        fin=j+len;
+        for (; j<fin; j++)
+            sig->val[i++]= sc->val[j];
+
+        // pick up s part of signature
+        len=getalen(INT,sc->val,j);
+        if (len<0)
+        {
+            ret.type=0;
+            return ret;
+        }
+        j+=skip(len);
+
+        if (sc->val[j]==0)
+        {
+            // skip leading zeros
+            j++;
+            len--;
+        }
+        rlen=bround(len);
+        ex=rlen-len;
+        for (k=0; k<ex; k++)
+            sig->val[i++]=0;
+
+        fin=j+len;
+        for (; j<fin; j++)
+            sig->val[i++]= sc->val[j];
+
+    }
+    if (ret.type==RSA)
+    {
+        rlen=bround(len);
+        ex=rlen-len;
+
+        sig->len=rlen;
+        i=0;
+        for (k=0; k<ex; k++)
+            sig->val[i++]=0;
+
+        fin=j+len;
+        for (; j<fin; j++)
+            sig->val[i++]= sc->val[j];
+
+    }
+    if (ret.hash==H256) ret.curve=NIST256;
+    if (ret.hash==H384) ret.curve=NIST384;
+    if (ret.hash==H512) ret.curve=NIST521;
+
+    return ret;
+}
+
+// Extract certificate from signed cert
+int X509_extract_cert(octet *sc,octet *cert)
+{
+    int i,j,fin,len,k;
+
+    j=0;
+    len=getalen(SEQ,sc->val,j);
+
+    if (len<0) return 0;
+    j+=skip(len);
+
+    k=j;
+
+    len=getalen(SEQ,sc->val,j);
+    if (len<0) return 0;
+    j+=skip(len);
+
+    fin=j+len;
+    cert->len=fin-k;
+    for (i=k; i<fin; i++) cert->val[i-k]=sc->val[i];
+
+    return 1;
+}
+
+// Extract Public Key from inside Certificate
+pktype X509_extract_public_key(octet *c,octet *key)
+{
+    int i,j,fin,len,sj;
+    char koid[12];     /*****/
+    octet KOID= {0,sizeof(koid),koid};
+    pktype ret;
+
+    ret.type=ret.hash=0;
+    ret.curve=-1;
+
+    j=0;
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return ret;
+    j+=skip(len);
+
+    if (len+j!=c->len) return ret;
+
+    len=getalen(0,c->val,j);
+    if (len<0) return ret;
+    j+=skip(len)+len; //jump over version clause
+
+    len=getalen(INT,c->val,j);
+
+    if (len>0) j+=skip(len)+len; // jump over serial number clause (if there 
is one)
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return ret;
+    j+=skip(len)+len;  // jump over signature algorithm
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return ret;
+    j+=skip(len)+len; // skip issuer
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return ret;
+    j+=skip(len)+len; // skip validity
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return ret;
+    j+=skip(len)+len; // skip subject
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return ret;
+    j+=skip(len); //
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return ret;
+    j+=skip(len);
+
+// ** Maybe dive in and check Public Key OIDs here?
+// ecpublicKey & prime256v1, secp384r1 or secp521r1 for ECC
+// rsapublicKey for RSA
+
+    sj=j+len;
+
+    len=getalen(OID,c->val,j);
+    if (len<0) return ret;
+    j+=skip(len);
+
+    fin=j+len;
+    KOID.len=len;
+    for (i=0; j<fin; j++)
+        KOID.val[i++]= c->val[j];
+
+    ret.type=0;
+    if (OCT_comp(&ECPK,&KOID)) ret.type=ECC;
+    if (OCT_comp(&RSAPK,&KOID)) ret.type=RSA;
+
+    if (ret.type==0) return ret;
+
+    if (ret.type==ECC)
+    {
+        // which elliptic curve?
+        len=getalen(OID,c->val,j);
+        if (len<0)
+        {
+            ret.type=0;
+            return ret;
+        }
+        j+=skip(len);
+
+        fin=j+len;
+        KOID.len=len;
+        for (i=0; j<fin; j++)
+            KOID.val[i++]= c->val[j];
+
+        if (OCT_comp(&PRIME25519,&KOID)) ret.curve=C25519;   /*****/
+        if (OCT_comp(&PRIME256V1,&KOID)) ret.curve=NIST256;
+        if (OCT_comp(&SECP384R1,&KOID)) ret.curve=NIST384;
+        if (OCT_comp(&SECP521R1,&KOID)) ret.curve=NIST521;
+    }
+
+    j=sj; // skip to actual Public Key
+
+    len=getalen(BIT,c->val,j);
+    if (len<0)
+    {
+        ret.type=0;
+        return ret;
+    }
+    j+=skip(len); //
+    j++;
+    len--; // skip bit shift (hopefully 0!)
+
+// extract key
+    if (ret.type==ECC)
+    {
+        key->len=len;
+        fin=j+len;
+        for (i=0; j<fin; j++)
+            key->val[i++]= c->val[j];
+
+    }
+    if (ret.type==RSA)
+    {
+        // Key is (modulus,exponent) - assume exponent is 65537
+        len=getalen(SEQ,c->val,j);
+        if (len<0)
+        {
+            ret.type=0;
+            return ret;
+        }
+        j+=skip(len); //
+
+        len=getalen(INT,c->val,j); // get modulus
+        if (len<0)
+        {
+            ret.type=0;
+            return ret;
+        }
+        j+=skip(len); //
+        if (c->val[j]==0)
+        {
+            j++;
+            len--; // remove leading zero
+        }
+
+        key->len=len;
+        fin=j+len;
+        for (i=0; j<fin; j++)
+            key->val[i++]= c->val[j];
+
+        ret.curve=8*len;
+    }
+    return ret;
+}
+
+// Find pointer to main sections of cert, before extracting individual field
+// Find index to issuer in cert
+int X509_find_issuer(octet *c)
+{
+    int j,len;
+    j=0;
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return 0;
+    j+=skip(len);
+
+    if (len+j!=c->len) return 0;
+
+    len=getalen(0,c->val,j);
+    if (len<0) return 0;
+    j+=skip(len)+len; //jump over version clause
+
+    len=getalen(INT,c->val,j);
+
+    if (len>0) j+=skip(len)+len; // jump over serial number clause (if there 
is one)
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return 0;
+    j+=skip(len)+len;  // jump over signature algorithm
+
+    return j;
+}
+
+// Find index to validity period
+int X509_find_validity(octet *c)
+{
+    int j,len;
+    j=X509_find_issuer(c);
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return 0;
+    j+=skip(len)+len; // skip issuer
+
+    return j;
+}
+
+// Find index to subject in cert
+int X509_find_subject(octet *c)
+{
+    int j,len;
+    j=X509_find_validity(c);
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return 0;
+    j+=skip(len)+len; // skip validity
+
+    return j;
+}
+
+// NOTE: When extracting cert information, we actually return just an index to 
the data inside the cert, and maybe its length
+// So no memory is assigned to store cert info. It is the callers 
responsibility to allocate such memory if required, and copy
+// cert information into it.
+
+// Find entity property indicated by SOID, given start of issuer or subject 
field. Return index in cert, flen=length of field
+
+int X509_find_entity_property(octet *c,octet *SOID,int start,int *flen)
+{
+    int i,j,k,fin,len,tlen;
+    char foid[50];  /*****/
+    octet FOID= {0,sizeof(foid),foid};
+
+    j=start;
+
+    tlen=getalen(SEQ,c->val,j);
+    if (tlen<0) return 0;
+    j+=skip(tlen);
+
+    for (k=j; j<k+tlen;)
+    {
+        // search for Owner OID
+        len=getalen(SET,c->val,j);
+        if (len<0) return 0;
+        j+=skip(len);
+        len=getalen(SEQ,c->val,j);
+        if (len<0) return 0;
+        j+=skip(len);
+        len=getalen(OID,c->val,j);
+        if (len<0) return 0;
+        j+=skip(len);
+        fin=j+len;  // extract OID
+        FOID.len=len;
+        for (i=0; j<fin; j++)
+            FOID.val[i++]= c->val[j];
+        len=getalen(ANY,c->val,j);  // get text, could be any type
+        if (len<0) return 0;
+
+        j+=skip(len);
+        if (OCT_comp(&FOID,SOID))
+        {
+            // if its the right one return
+            *flen=len;
+            return j;
+        }
+        j+=len;  // skip over it
+    }
+    *flen=0; /*****/
+    return 0;
+}
+
+// Find start date of certificate validity period
+int X509_find_start_date(octet *c,int start)
+{
+    int j,len;
+    j=start;
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return 0;
+    j+=skip(len);
+
+    len=getalen(UTC,c->val,j);
+    if (len<0) return 0;
+    j+=skip(len);
+    return j;
+}
+
+// Find expiry date of certificate validity period
+int X509_find_expiry_date(octet *c,int start)
+{
+    int j,len;
+    j=start;
+
+    len=getalen(SEQ,c->val,j);
+    if (len<0) return 0;
+    j+=skip(len);
+
+    len=getalen(UTC,c->val,j);
+    if (len<0) return 0;
+    j+=skip(len)+len;
+
+    len=getalen(UTC,c->val,j);
+    if (len<0) return 0;
+    j+=skip(len);
+
+    return j;
+}
+
+void print_out(char *des,octet *c,int index,int len)
+{
+    int i;
+    printf("%s [",des);
+    for (i=0; i<len; i++)
+        printf("%c",c->val[index+i]);
+    printf("]\n");
+}
+
+void print_date(char *des,octet *c,int index)
+{
+    int i=index;
+    printf("%s [",des);
+    if (i==0) printf("]\n");
+    else printf("20%c%c-%c%c-%c%c 
%c%c:%c%c:%c%c]\n",c->val[i],c->val[i+1],c->val[i+2],c->val[i+3],c->val[i+4],c->val[i+5],c->val[i+6],c->val[i+7],c->val[i+8],c->val[i+9],c->val[i+10],c->val[i+11]);
+}
+
+
+#ifdef HAS_MAIN
+
+/* This simple driver program is hard-wired to support just one elliptic curve 
and one
+   RSA bit length. To change replace the text string NIST256 in the main 
program to
+   another curve and 2048 to some other value, where the curve and the RSA bit 
length
+   are suppported by the library. Of course a more elaborate program could 
support
+   muliple curves simultaneously */
+
+#define CHOICE NIST256
+
+
+
+/* test driver program */
+// Sample Certs. Uncomment one CA cert and one example cert. Note that AMCL 
library must be built to support given curve.
+// Sample Certs all created using OpenSSL - see 
http://blog.didierstevens.com/2008/12/30/howto-make-your-own-cert-with-openssl/
+// Note - SSL currently only supports NIST curves. Howevever version 1.1.0 of 
OpenSSL now supports C25519
+
+#if CHOICE==NIST256
+
+#include "ecdh_NIST256.h"
+#include "rsa_2048.h"
+
+// ** CA is RSA 2048-bit based - for use with NIST256 build of library - 
assumes use of SHA256 in Certs
+// RSA 2048 Self-Signed CA cert
+char 
ca_b64[]="MIIDuzCCAqOgAwIBAgIJAP44jcM1MOROMA0GCSqGSIb3DQEBCwUAMHQxCzAJBgNVBAYTAklFMRAwDgYDVQQIDAdJcmVsYW5kMQ8wDQYDVQQHDAZEdWJsaW4xITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEfMB0GCSqGSIb3DQEJARYQbXNjb3R0QGluZGlnby5pZTAeFw0xNTExMjYwOTUwMzlaFw0yMDExMjUwOTUwMzlaMHQxCzAJBgNVBAYTAklFMRAwDgYDVQQIDAdJcmVsYW5kMQ8wDQYDVQQHDAZEdWJsaW4xITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEfMB0GCSqGSIb3DQEJARYQbXNjb3R0QGluZGlnby5pZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANUs7/nri9J8zw8rW8JVszXP0ZqeLoQJaq2X28ebm8x5VT3okr9rnBjFjpx0YKQCAFQf8iSOOYuNpDvtZ/YpsjPbk2rg5sLY9G0eUMqrTuZ7moPSxnrXS5evizjD9Z9HqaqeNEYD3sPouPg+lhU1oAUQjUTJVFhEr1x0EnSEYbbrWtY9ZDSuZv+d4NIeqqPOYFd1yZc+LYZyQbAAQqwRLNPZH/rnIykLa6I7w7mGT7H6SBz2O09BtgpTHhalL40ecXa4ZOEze0xwzlc+mEFIrnmdadg3vQrJt42RVbo3LN6RfDIqUZOMOtQW/53pUR1lIpCwVWJTiOpmSEIEqhhjFq0CAwEAAaNQME4wHQYDVR0OBBYEFJrz6LHeT6FcjR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l2b3gxDTALBgNVBAsMBExhYnMxDTALBgNVBAMMBE1pa2UxHzAdBgkqhkiG9w0BCQEWEG1zY290dEBpbmRpZ28uaWUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCjPBVwmPg8Gwx0+8xekmomptA0BDwS7NUfBetqDqNMNyji0bSe8LAfpciU7NW/HWfUE1lndCqSDDwnMJmwC5e3GAl/Bus+a+z8ruEhWGbn95xrHXFkOawbRlXuS7UcEQCvPr8KQHhNsg4cyV7Hn527CPUl27n+WN8/pANo01cTN/dQaK87naU0Mid09vktlMKSN0zyJOnc5CsaTLs+vCRKJ9sUL3d4IQIA2y7gvrTe+iY/QI26nqhGpNWYyFkAdy9PdHUEnDI6JsfF7jFh37yG7XEgDDA3asp/oi1T1+ZoASj2boL++opdqCzDndeWwzDWAWuvJ9wULd80ti6x737ZAgMBAAEwCgYIKoZIzj0EAwIDSAAwRQIgCDwgl98+9moBo+etaLt8MvB/z5Ti6i9neRTZkvoFl7YCIQDq//M3OB757fepErRzIQo3aFAFYjOooi6WdSqP3XqGIg==";
+
+#endif
+
+#if CHOICE==NIST384
+
+#include "ecdh_NIST384.h"
+#include "rsa_3072.h"
+
+// ** CA is RSA 3072-bit based  - for use with NIST384 build of library - 
assumes use of SHA384 in Certs
+// RSA 3072 Self-Signed CA cert
+char 
ca_b64[]="MIIElzCCAv+gAwIBAgIJAJA+8OyEeK4FMA0GCSqGSIb3DQEBDAUAMGIxCzAJBgNVBAYTAklFMRAwDgYDVQQIDAdJcmVsYW5kMQ8wDQYDVQQHDAZEdWJsaW4xITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDENMAsGA1UEAwwETWlrZTAeFw0xNTExMjYxNDQ0MDBaFw0yMDExMjUxNDQ0MDBaMGIxCzAJBgNVBAYTAklFMRAwDgYDVQQIDAdJcmVsYW5kMQ8wDQYDVQQHDAZEdWJsaW4xITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5I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Hmh8JFXzoBmzibabrvyTz79+1QOrR+6MEEsKtmJIYPJi+GsQ0PmjF2HmJncM1zeQh7DQYJf2Xc8p5Vjd8//6YREBVfN3UIyrl87MSucy+mjANBgkqhkiG9w0BAQwFAAOCAYEAmuwa64+K1qlCELpcnCyWwMhSb+Zsw0Hh6q/BfxalZhsX1UFEwE9nHoVJcokaEEYF4u4AYXU5rdysRHxYBfgMbohguTT7sJwCfve2JqpqvhQOkGDG1DB4Ho4y7NPPYB2+UMd7JMD0TOcHXdgQ8FtAE0ClD8VkW0gAC0lCrbQbynfLoUjIWqg3w2g79hvdZPgRt208nFiHuezynOaEFePoXl8CxHInsxAnMaJn2fEs5/QH67pwD65mPdNFsvlr0zdzYcceqEmEHpRAXFOQAJtffGjWAGGX/CsghLuqlpdCiTGA1B53XoXKJvArr/kHpTNMsU1NnkQIHZ5n4USCo4QgL6n9nwem7U2mYBYjmxPi5Y3JJnTZz4zUnv0bD0vSwoivnFZox9H6qTAkeIX1ojJ2ujxWHNOMvOFb6nU2gqNZj2vYcO38OIrK9gwM9lm4FF20YBuf
 h+WOzQthrHJv0YuQt3NuDQEMkvz+23YvzZlr+e2XqDlMhyR01Kk0MXeLGGcv";
+
+// ** CA is ECC 384 based - - for use with NIST384 build of library - assumes 
use of SHA384 in Certs
+// ECC 384 Self-Signed CA Cert
+//char 
ca_b64[]="MIICSTCCAc6gAwIBAgIJAIwHpOFSZLXnMAoGCCqGSM49BAMDMGIxCzAJBgNVBAYTAklFMRAwDgYDVQQIDAdJcmVsYW5kMQ8wDQYDVQQHDAZEdWJsaW4xITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDENMAsGA1UEAwwEbWlrZTAeFw0xNTExMjYxNTQ0NTlaFw0yMDExMjUxNTQ0NTlaMGIxCzAJBgNVBAYTAklFMRAwDgYDVQQIDAdJcmVsYW5kMQ8wDQYDVQQHDAZEdWJsaW4xITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx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A2YAMGMCLxHSQAYP2EsuIpR4TzDDSIlsw4BBsD7W0ZfH91v9J0j5UWQJD/
 
yNjMtyA2Qlkq/0AjB+SJQbLgycNJH5SnR/X5wx26/62ln9s0swUtlCYVtNzyEQ3YRHSZbmTbh16RUT7Ak=";
+
+#endif
+
+#if CHOICE==NIST521
+
+#include "ecdh_NIST521.h"
+#include "rsa_4096.h"
+
+// ** CA is ECC 521 based - - for use with NIST521 build of library - assumes 
use of SHA512 in Certs
+// ECC 521 Self-Signed CA Cert
+char 
ca_b64[]="MIIC+TCCAlqgAwIBAgIJAKlppiHsRpY8MAoGCCqGSM49BAMEMIGUMQswCQYDVQQGEwJJRTEQMA4GA1UECAwHSXJlbGFuZDEPMA0GA1UEBwwGRHVibGluMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxDTALBgNVBAsMBExhYnMxDzANBgNVBAMMBm1zY290dDEfMB0GCSqGSIb3DQEJARYQbXNjb3R0QGluZGlnby5pZTAeFw0xNTEyMDExMzE5MjZaFw0yMDExMzAxMzE5MjZaMIGUMQswCQYDVQQGEwJJRTEQMA4GA1UECAwHSXJlbGFuZDEPMA0GA1UEBwwGRHVibGluMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxDTALBgNVBAsMBExhYnMxDzANBgNVBAMMBm1zY290dDEfMB0GCSqGSIb3DQEJARYQbXNjb3R0QGluZGlnby5pZTCBmzAQBgcqhkjOPQIBBgUrgQQAIwOBhgAEAKUj6Qa4Vr1vyango8XHlLIIEzY9IVppdpGUrMlNfo0Spu+AXGhnluwJTZXOYLi8jSIPEAL7vuwS5H6uPPIz1QWXALRETVYAQfK0pIfPHq+edTHVTXMcAUpdNla2d4LwYO7HpkSQFHd7aaDN3yVhSL2J0LBLgy0wGkEHuyK1O2r0xNu6o1AwTjAdBgNVHQ4EFgQU966PLshKffU/NRCivMmNq8RiRkAwHwYDVR0jBBgwFoAU966PLshKffU/NRCivMmNq8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gd4Nbs6kIe1GyYbrcyC4wT";
+
+#endif
+
+char io[5000];
+octet IO= {0,sizeof(io),io};
+
+#define MAXMODBYTES 72
+#define MAXFFLEN 16
+
+char sig[MAXMODBYTES*MAXFFLEN];
+octet SIG= {0,sizeof(sig),sig};
+
+char r[MAXMODBYTES];
+octet R= {0,sizeof(r),r};
+
+char s[MAXMODBYTES];
+octet S= {0,sizeof(s),s};
+
+char cakey[MAXMODBYTES*MAXFFLEN];
+octet CAKEY= {0,sizeof(cakey),cakey};
+
+char certkey[MAXMODBYTES*MAXFFLEN];
+octet CERTKEY= {0,sizeof(certkey),certkey};
+
+char h[5000];
+octet H= {0,sizeof(h),h};
+
+char hh[5000];
+octet HH= {0,sizeof(hh),hh};
+
+char hp[RFS_2048];
+octet HP= {0,sizeof(hp),hp};
+
+
+int main()
+{
+    int res,len,sha;
+    int c,ic;
+    rsa_public_key_2048 PK;
+    pktype st,ca,pt;
+
+    printf("First check signature on self-signed cert and extract CA public 
key\n");
+    OCT_frombase64(&IO,ca_b64);
+    printf("CA Self-Signed Cert= \n");
+    OCT_output(&IO);
+    printf("\n");
+
+    st=X509_extract_cert_sig(&IO,&SIG); // returns signature type
+
+    if (st.type==0)
+    {
+        printf("Unable to extract cert signature\n");
+        return 0;
+    }
+
+    if (st.type==ECC)
+    {
+        OCT_chop(&SIG,&S,SIG.len/2);
+        OCT_copy(&R,&SIG);
+        printf("ECC SIG= \n");
+        OCT_output(&R);
+        OCT_output(&S);
+        printf("\n");
+    }
+
+    if (st.type==RSA)
+    {
+        printf("RSA SIG= \n");
+        OCT_output(&SIG);
+        printf("\n");
+    }
+
+    if (st.hash==H256) printf("Hashed with SHA256\n");
+    if (st.hash==H384) printf("Hashed with SHA384\n");
+    if (st.hash==H512) printf("Hashed with SHA512\n");
+
+// Extract Cert from signed Cert
+
+    c=X509_extract_cert(&IO,&H);
+
+    printf("\nCert= \n");
+    OCT_output(&H);
+    printf("\n");
+
+// show some details
+    printf("Issuer Details\n");
+    ic=X509_find_issuer(&H);
+    c=X509_find_entity_property(&H,&ON,ic,&len);
+    print_out("owner=",&H,c,len);
+    c=X509_find_entity_property(&H,&CN,ic,&len);
+    print_out("country=",&H,c,len);
+    c=X509_find_entity_property(&H,&EN,ic,&len);
+    print_out("email=",&H,c,len);
+    printf("\n");
+
+    ca=X509_extract_public_key(&H,&CAKEY);
+
+    if (ca.type==0)
+    {
+        printf("Not supported by library\n");
+        return 0;
+    }
+    if (ca.type!=st.type)
+    {
+        printf("Not self-signed\n");
+    }
+
+    if (ca.type==ECC)
+    {
+        printf("EXTRACTED ECC PUBLIC KEY= \n");
+        OCT_output(&CAKEY);
+    }
+    if (ca.type==RSA)
+    {
+        printf("EXTRACTED RSA PUBLIC KEY= \n");
+        OCT_output(&CAKEY);
+    }
+    printf("\n");
+
+// Cert is self-signed - so check signature
+
+    printf("Checking Self-Signed Signature\n");
+    if (ca.type==ECC)
+    {
+        if (ca.curve!=CHOICE)
+        {
+            printf("Curve is not supported\n");
+            return 0;
+        }
+        res=ECP_NIST256_PUBLIC_KEY_VALIDATE(1,&CAKEY);
+        if (res!=0)
+        {
+            printf("ECP Public Key is invalid!\n");
+            return 0;
+        }
+        else printf("ECP Public Key is Valid\n");
+
+        sha=0;
+
+        if (st.hash==H256) sha=SHA256;
+        if (st.hash==H384) sha=SHA384;
+        if (st.hash==H512) sha=SHA512;
+        if (st.hash==0)
+        {
+            printf("Hash Function not supported\n");
+            return 0;
+        }
+
+        if (ECP_NIST256_VP_DSA(sha,&CAKEY,&H,&R,&S)!=0)
+        {
+            printf("***ECDSA Verification Failed\n");
+            return 0;
+        }
+        else
+            printf("ECDSA Signature/Verification succeeded \n");
+    }
+
+    if (ca.type==RSA)
+    {
+        if (ca.curve!=2048)
+        {
+            printf("RSA bit size is not supported\n");
+            return 0;
+        }
+        PK.e=65537; // assuming this!
+        RSA_2048_fromOctet(PK.n,&CAKEY);
+
+        sha=0;
+
+        if (st.hash==H256) sha=SHA256;
+        if (st.hash==H384) sha=SHA384;
+        if (st.hash==H512) sha=SHA512;
+        if (st.hash==0)
+        {
+            printf("Hash Function not supported\n");
+            return 0;
+        }
+        PKCS15(sha,&H,&HP);
+
+        RSA_2048_ENCRYPT(&PK,&SIG,&HH);
+
+        if (OCT_comp(&HP,&HH))
+            printf("RSA Signature/Verification succeeded \n");
+        else
+        {
+            printf("***RSA Verification Failed\n");
+            return 0;
+        }
+    }
+
+    printf("\nNext check CA signature on cert, and extract public key\n");
+
+    OCT_frombase64(&IO,cert_b64);
+    printf("Example Cert= \n");
+    OCT_output(&IO);
+    printf("\n");
+
+    st=X509_extract_cert_sig(&IO,&SIG);
+
+    if (st.type==0)
+    {
+        printf("Unable to check cert signature\n");
+        return 0;
+    }
+
+    if (st.type==ECC)
+    {
+        OCT_chop(&SIG,&S,SIG.len/2);
+        OCT_copy(&R,&SIG);
+        printf("SIG= \n");
+        OCT_output(&R);
+
+        OCT_output(&S);
+
+        printf("\n");
+    }
+
+    if (st.type==RSA)
+    {
+        printf("SIG= \n");
+        OCT_output(&SIG);
+        printf("\n");
+    }
+
+    c=X509_extract_cert(&IO,&H);
+
+    printf("Cert= \n");
+    OCT_output(&H);
+    printf("\n");
+
+    printf("Subject Details\n");
+    ic=X509_find_subject(&H);
+    c=X509_find_entity_property(&H,&ON,ic,&len);
+    print_out("owner=",&H,c,len);
+    c=X509_find_entity_property(&H,&CN,ic,&len);
+    print_out("country=",&H,c,len);
+    c=X509_find_entity_property(&H,&EN,ic,&len);
+    print_out("email=",&H,c,len);
+    printf("\n");
+
+    ic=X509_find_validity(&H);
+    c=X509_find_start_date(&H,ic);
+    print_date("start date= ",&H,c);
+    c=X509_find_expiry_date(&H,ic);
+    print_date("expiry date=",&H,c);
+    printf("\n");
+
+    pt=X509_extract_public_key(&H,&CERTKEY);
+
+    if (pt.type==0)
+    {
+        printf("Not supported by library\n");
+        return 0;
+    }
+
+    if (pt.type==ECC)
+    {
+        printf("EXTRACTED ECC PUBLIC KEY= \n");
+        OCT_output(&CERTKEY);
+    }
+    if (pt.type==RSA)
+    {
+        printf("EXTRACTED RSA PUBLIC KEY= \n");
+        OCT_output(&CERTKEY);
+    }
+
+    printf("\n");
+
+    /* Check CA signature */
+
+    if (ca.type==ECC)
+    {
+        printf("Checking CA's ECC Signature on Cert\n");
+        res=ECP_NIST256_PUBLIC_KEY_VALIDATE(1,&CAKEY);
+        if (res!=0)
+            printf("ECP Public Key is invalid!\n");
+        else printf("ECP Public Key is Valid\n");
+
+        sha=0;
+
+        if (st.hash==H256) sha=SHA256;
+        if (st.hash==H384) sha=SHA384;
+        if (st.hash==H512) sha=SHA512;
+        if (st.hash==0)
+        {
+            printf("Hash Function not supported\n");
+            return 0;
+        }
+
+        if (ECP_NIST256_VP_DSA(sha,&CAKEY,&H,&R,&S)!=0)
+            printf("***ECDSA Verification Failed\n");
+        else
+            printf("ECDSA Signature/Verification succeeded \n");
+    }
+
+    if (ca.type==RSA)
+    {
+        printf("Checking CA's RSA Signature on Cert\n");
+        PK.e=65537; // assuming this!
+        RSA_2048_fromOctet(PK.n,&CAKEY);
+
+        sha=0;
+
+        if (st.hash==H256) sha=SHA256;
+        if (st.hash==H384) sha=SHA384;
+        if (st.hash==H512) sha=SHA512;
+        if (st.hash==0)
+        {
+            printf("Hash Function not supported\n");
+            return 0;
+        }
+        PKCS15(sha,&H,&HP);
+
+        RSA_2048_ENCRYPT(&PK,&SIG,&HH);
+
+        if (OCT_comp(&HP,&HH))
+            printf("RSA Signature/Verification succeeded \n");
+        else
+            printf("***RSA Verification Failed\n");
+
+    }
+
+    return 0;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-milagro-crypto/blob/c25f9e5c/version3/c/x509.h
----------------------------------------------------------------------
diff --git a/version3/c/x509.h b/version3/c/x509.h
new file mode 100644
index 0000000..2c4a7b9
--- /dev/null
+++ b/version3/c/x509.h
@@ -0,0 +1,109 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+/* AMCL x509 header file */
+
+/**
+ * @file x509.h
+ * @author Mike Scott
+ * @brief X509 function Header File
+ *
+ */
+
+#ifndef X509_H
+#define X509_H
+
+/**
+ * @brief Public key type
+ */
+typedef struct
+{
+    int type;  /**< signature type (ECC or RSA) */
+    int hash;  /**< hash type */
+    int curve; /**< elliptic curve used or RSA key length in bits  */
+} pktype;
+
+
+/* X.509 functions */
+/** @brief Extract certificate signature
+ *
+       @param c an X.509 certificate
+       @param s the extracted signature
+       @return 0 on failure, or indicator of signature type (ECC or RSA)
+
+*/
+extern pktype X509_extract_cert_sig(octet *c,octet *s);
+/** @brief
+ *
+       @param sc a signed certificate
+       @param c the extracted certificate
+       @return 0 on failure
+*/
+extern int X509_extract_cert(octet *sc,octet *c);
+/** @brief
+ *
+       @param c an X.509 certificate
+       @param k the extracted key
+       @return 0 on failure, or indicator of public key type (ECC or RSA)
+*/
+extern pktype X509_extract_public_key(octet *c,octet *k);
+/** @brief
+ *
+       @param c an X.509 certificate
+       @return 0 on failure, or pointer to issuer field in cert
+*/
+extern int X509_find_issuer(octet *c);
+/** @brief
+ *
+       @param c an X.509 certificate
+       @return 0 on failure, or pointer to validity field in cert
+*/
+extern int X509_find_validity(octet *c);
+/** @brief
+ *
+       @param c an X.509 certificate
+       @return 0 on failure, or pointer to subject field in cert
+*/
+extern int X509_find_subject(octet *c);
+/** @brief
+ *
+       @param c an X.509 certificate
+       @param S is OID of property we are looking for
+       @param s is a pointer to the section of interest in the cert
+       @param f is pointer to the length of the property
+       @return 0 on failure, or pointer to the property
+*/
+extern int X509_find_entity_property(octet *c,octet *S,int s,int *f);
+/** @brief
+ *
+       @param c an X.509 certificate
+       @param s is a pointer to the start of the validity field
+       @return 0 on failure, or pointer to the start date
+*/
+extern int X509_find_start_date(octet *c,int s);
+/** @brief
+ *
+       @param c an X.509 certificate
+       @param s is a pointer to the start of the validity field
+       @return 0 on failure, or pointer to the expiry date
+*/
+extern int X509_find_expiry_date(octet *c,int s);
+
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-milagro-crypto/blob/c25f9e5c/version3/check.cpp
----------------------------------------------------------------------
diff --git a/version3/check.cpp b/version3/check.cpp
new file mode 100644
index 0000000..5835644
--- /dev/null
+++ b/version3/check.cpp
@@ -0,0 +1,108 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+/* Utility to recommend best choice for BASEBITS 
+
+(MINGW build)
+
+g++ -O2 check.cpp big.cpp miracl.a -o check.exe */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "big.h"
+
+using namespace std;
+
+Miracl precision=20;
+
+// calculate maximum excess given base, length in words, length in bits
+int maxxes(int b,int n,int m)
+{
+       int mx=n*b-m;
+       int tp=m%b;
+       if (mx >= tp)
+               mx=tp-1;
+       return mx;
+}
+
+int main(int argc, char *argv[])
+{
+       int p,w,b,n,s,t,e,ex,mx;
+       Big lhs,rhs;
+
+       argc--; argv++;
+
+    if (argc!=2)
+    {
+       printf("Bad arguments\n");
+       printf("check wordlength modulus-length\n");
+          printf("Wordlength can be 16, 32 or 64 (or 26 for Javascript)\n");
+       printf("Example:\n");
+       printf("check 32 256\n");
+          printf("Outputs choices for BASEBITS, number of words per Big, and 
number of spare bits\n");
+          printf("Normally choose for minimum words per Big, and maximum 
excess bits\n");
+          printf("Ideally >=5 excess bits for Edwards curves\n");
+          printf("Ideally >=10 excess bits for Weierstrass curves\n");
+          printf("Ideally >=23 excess bits for pairings\n");
+       exit(0);
+    }
+
+    n=atoi(argv[0]);
+       p=atoi(argv[1]); // Number of bits in prime modulus
+
+       if (n!=16 && n!=26 && n!=32 && n!=64)
+       {
+               printf("wordlength must be 16, 32 or 64 (or 26 for 
Javascript)\n");
+               return 0;
+       }
+
+       rhs=pow((Big)2,2*n-1);
+       e=3;            // need at least 3 bits to allow adds and subtracts 
without normalisation
+       if (n==26)
+       {
+               rhs*=2; // no sign bit to worry about in Javascript
+               e=1;    // not an issue for Javascript
+       }
+
+       for (b=n-e;b>=n-8;b--)
+       {
+               if (n==64 && b%2!=0) continue; // insist on even values for 
64-bit builds 
+               w=p/b; if (p%b!=0) w++;
+               s=w*b-p;
+
+               lhs=(w+2)*pow((Big)2,2*b);  // sum of products plus carry plus 
one for redc
+
+               if (lhs>=rhs)    {printf("Stability violation for BASEBITS= 
%d\n",b); continue;}
+
+               mx=s;
+               if (mx>(n-4)/2)
+                       mx=(n-4)/2;
+
+// At least 4 bits spare needed for field excess.  
+               if (mx<4) {printf("Not enough Fp excess for BASEBITS= %d\n",b); 
continue;}
+// At least 2 spare bits needed for FF excess 
+               t=b*(1+(p-1)/b) - 8*(1+(p-1)/8);
+               if (t<2 || t>=b) {printf("Not enough FF excess for BASEBITS= 
%d\n",b);}
+
+               printf("Solution for BASEBITS= %d, Words Per Big=%d, Max 
excess= %d\n",b,w,mx);
+       }
+       
+       return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-milagro-crypto/blob/c25f9e5c/version3/cpp/aes.cpp
----------------------------------------------------------------------
diff --git a/version3/cpp/aes.cpp b/version3/cpp/aes.cpp
new file mode 100644
index 0000000..4d4c325
--- /dev/null
+++ b/version3/cpp/aes.cpp
@@ -0,0 +1,706 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+
+/*
+ * Implementation of the NIST Advanced Ecryption Standard
+ *
+ * SU=m, SU is Stack Usage
+ */
+
+#include <stdlib.h>
+
+
+#include "arch.h"
+#include "amcl.h"
+
+/* this is fixed */
+#define NB 4
+
+/* Rotates 32-bit word left by 1, 2 or 3 byte  */
+
+#define ROTL8(x) (((x)<<8)|((x)>>24))
+#define ROTL16(x) (((x)<<16)|((x)>>16))
+#define ROTL24(x) (((x)<<24)|((x)>>8))
+
+static const uchar InCo[4]= {0xB,0xD,0x9,0xE}; /* Inverse Coefficients */
+
+static const uchar ptab[]=
+{
+    1,3,5,15,17,51,85,255,26,46,114,150,161,248,19,53,
+    95,225,56,72,216,115,149,164,247,2,6,10,30,34,102,170,
+    229,52,92,228,55,89,235,38,106,190,217,112,144,171,230,49,
+    83,245,4,12,20,60,68,204,79,209,104,184,211,110,178,205,
+    76,212,103,169,224,59,77,215,98,166,241,8,24,40,120,136,
+    131,158,185,208,107,189,220,127,129,152,179,206,73,219,118,154,
+    181,196,87,249,16,48,80,240,11,29,39,105,187,214,97,163,
+    254,25,43,125,135,146,173,236,47,113,147,174,233,32,96,160,
+    251,22,58,78,210,109,183,194,93,231,50,86,250,21,63,65,
+    195,94,226,61,71,201,64,192,91,237,44,116,156,191,218,117,
+    159,186,213,100,172,239,42,126,130,157,188,223,122,142,137,128,
+    155,182,193,88,232,35,101,175,234,37,111,177,200,67,197,84,
+    252,31,33,99,165,244,7,9,27,45,119,153,176,203,70,202,
+    69,207,74,222,121,139,134,145,168,227,62,66,198,81,243,14,
+    18,54,90,238,41,123,141,140,143,138,133,148,167,242,13,23,
+    57,75,221,124,132,151,162,253,28,36,108,180,199,82,246,1
+};
+
+static const uchar ltab[]=
+{
+    0,255,25,1,50,2,26,198,75,199,27,104,51,238,223,3,
+    100,4,224,14,52,141,129,239,76,113,8,200,248,105,28,193,
+    125,194,29,181,249,185,39,106,77,228,166,114,154,201,9,120,
+    101,47,138,5,33,15,225,36,18,240,130,69,53,147,218,142,
+    150,143,219,189,54,208,206,148,19,92,210,241,64,70,131,56,
+    102,221,253,48,191,6,139,98,179,37,226,152,34,136,145,16,
+    126,110,72,195,163,182,30,66,58,107,40,84,250,133,61,186,
+    43,121,10,21,155,159,94,202,78,212,172,229,243,115,167,87,
+    175,88,168,80,244,234,214,116,79,174,233,213,231,230,173,232,
+    44,215,117,122,235,22,11,245,89,203,95,176,156,169,81,160,
+    127,12,246,111,23,196,73,236,216,67,31,45,164,118,123,183,
+    204,187,62,90,251,96,177,134,59,82,161,108,170,85,41,157,
+    151,178,135,144,97,190,220,252,188,149,207,205,55,63,91,209,
+    83,57,132,60,65,162,109,71,20,42,158,93,86,242,211,171,
+    68,17,146,217,35,32,46,137,180,124,184,38,119,153,227,165,
+    103,74,237,222,197,49,254,24,13,99,140,128,192,247,112,7
+};
+
+static const uchar fbsub[]=
+{
+    99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,118,
+    202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192,
+    183,253,147,38,54,63,247,204,52,165,229,241,113,216,49,21,
+    4,199,35,195,24,150,5,154,7,18,128,226,235,39,178,117,
+    9,131,44,26,27,110,90,160,82,59,214,179,41,227,47,132,
+    83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207,
+    208,239,170,251,67,77,51,133,69,249,2,127,80,60,159,168,
+    81,163,64,143,146,157,56,245,188,182,218,33,16,255,243,210,
+    205,12,19,236,95,151,68,23,196,167,126,61,100,93,25,115,
+    96,129,79,220,34,42,144,136,70,238,184,20,222,94,11,219,
+    224,50,58,10,73,6,36,92,194,211,172,98,145,149,228,121,
+    231,200,55,109,141,213,78,169,108,86,244,234,101,122,174,8,
+    186,120,37,46,28,166,180,198,232,221,116,31,75,189,139,138,
+    112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,158,
+    225,248,152,17,105,217,142,148,155,30,135,233,206,85,40,223,
+    140,161,137,13,191,230,66,104,65,153,45,15,176,84,187,22
+};
+
+static const uchar rbsub[]=
+{
+    82,9,106,213,48,54,165,56,191,64,163,158,129,243,215,251,
+    124,227,57,130,155,47,255,135,52,142,67,68,196,222,233,203,
+    84,123,148,50,166,194,35,61,238,76,149,11,66,250,195,78,
+    8,46,161,102,40,217,36,178,118,91,162,73,109,139,209,37,
+    114,248,246,100,134,104,152,22,212,164,92,204,93,101,182,146,
+    108,112,72,80,253,237,185,218,94,21,70,87,167,141,157,132,
+    144,216,171,0,140,188,211,10,247,228,88,5,184,179,69,6,
+    208,44,30,143,202,63,15,2,193,175,189,3,1,19,138,107,
+    58,145,17,65,79,103,220,234,151,242,207,206,240,180,230,115,
+    150,172,116,34,231,173,53,133,226,249,55,232,28,117,223,110,
+    71,241,26,113,29,41,197,137,111,183,98,14,170,24,190,27,
+    252,86,62,75,198,210,121,32,154,219,192,254,120,205,90,244,
+    31,221,168,51,136,7,199,49,177,18,16,89,39,128,236,95,
+    96,81,127,169,25,181,74,13,45,229,122,159,147,201,156,239,
+    160,224,59,77,174,42,245,176,200,235,187,60,131,83,153,97,
+    23,43,4,126,186,119,214,38,225,105,20,99,85,33,12,125
+};
+
+static const unsign32 rco[]=
+{1,2,4,8,16,32,64,128,27,54,108,216,171,77,154,47};
+
+static const unsign32 ftable[]=
+{
+    0xa56363c6,0x847c7cf8,0x997777ee,0x8d7b7bf6,0xdf2f2ff,0xbd6b6bd6,
+    0xb16f6fde,0x54c5c591,0x50303060,0x3010102,0xa96767ce,0x7d2b2b56,
+    0x19fefee7,0x62d7d7b5,0xe6abab4d,0x9a7676ec,0x45caca8f,0x9d82821f,
+    0x40c9c989,0x877d7dfa,0x15fafaef,0xeb5959b2,0xc947478e,0xbf0f0fb,
+    0xecadad41,0x67d4d4b3,0xfda2a25f,0xeaafaf45,0xbf9c9c23,0xf7a4a453,
+    0x967272e4,0x5bc0c09b,0xc2b7b775,0x1cfdfde1,0xae93933d,0x6a26264c,
+    0x5a36366c,0x413f3f7e,0x2f7f7f5,0x4fcccc83,0x5c343468,0xf4a5a551,
+    0x34e5e5d1,0x8f1f1f9,0x937171e2,0x73d8d8ab,0x53313162,0x3f15152a,
+    0xc040408,0x52c7c795,0x65232346,0x5ec3c39d,0x28181830,0xa1969637,
+    0xf05050a,0xb59a9a2f,0x907070e,0x36121224,0x9b80801b,0x3de2e2df,
+    0x26ebebcd,0x6927274e,0xcdb2b27f,0x9f7575ea,0x1b090912,0x9e83831d,
+    0x742c2c58,0x2e1a1a34,0x2d1b1b36,0xb26e6edc,0xee5a5ab4,0xfba0a05b,
+    0xf65252a4,0x4d3b3b76,0x61d6d6b7,0xceb3b37d,0x7b292952,0x3ee3e3dd,
+    0x712f2f5e,0x97848413,0xf55353a6,0x68d1d1b9,0x0,0x2cededc1,
+    0x60202040,0x1ffcfce3,0xc8b1b179,0xed5b5bb6,0xbe6a6ad4,0x46cbcb8d,
+    0xd9bebe67,0x4b393972,0xde4a4a94,0xd44c4c98,0xe85858b0,0x4acfcf85,
+    0x6bd0d0bb,0x2aefefc5,0xe5aaaa4f,0x16fbfbed,0xc5434386,0xd74d4d9a,
+    0x55333366,0x94858511,0xcf45458a,0x10f9f9e9,0x6020204,0x817f7ffe,
+    0xf05050a0,0x443c3c78,0xba9f9f25,0xe3a8a84b,0xf35151a2,0xfea3a35d,
+    0xc0404080,0x8a8f8f05,0xad92923f,0xbc9d9d21,0x48383870,0x4f5f5f1,
+    0xdfbcbc63,0xc1b6b677,0x75dadaaf,0x63212142,0x30101020,0x1affffe5,
+    0xef3f3fd,0x6dd2d2bf,0x4ccdcd81,0x140c0c18,0x35131326,0x2fececc3,
+    0xe15f5fbe,0xa2979735,0xcc444488,0x3917172e,0x57c4c493,0xf2a7a755,
+    0x827e7efc,0x473d3d7a,0xac6464c8,0xe75d5dba,0x2b191932,0x957373e6,
+    0xa06060c0,0x98818119,0xd14f4f9e,0x7fdcdca3,0x66222244,0x7e2a2a54,
+    0xab90903b,0x8388880b,0xca46468c,0x29eeeec7,0xd3b8b86b,0x3c141428,
+    0x79dedea7,0xe25e5ebc,0x1d0b0b16,0x76dbdbad,0x3be0e0db,0x56323264,
+    0x4e3a3a74,0x1e0a0a14,0xdb494992,0xa06060c,0x6c242448,0xe45c5cb8,
+    0x5dc2c29f,0x6ed3d3bd,0xefacac43,0xa66262c4,0xa8919139,0xa4959531,
+    0x37e4e4d3,0x8b7979f2,0x32e7e7d5,0x43c8c88b,0x5937376e,0xb76d6dda,
+    0x8c8d8d01,0x64d5d5b1,0xd24e4e9c,0xe0a9a949,0xb46c6cd8,0xfa5656ac,
+    0x7f4f4f3,0x25eaeacf,0xaf6565ca,0x8e7a7af4,0xe9aeae47,0x18080810,
+    0xd5baba6f,0x887878f0,0x6f25254a,0x722e2e5c,0x241c1c38,0xf1a6a657,
+    0xc7b4b473,0x51c6c697,0x23e8e8cb,0x7cdddda1,0x9c7474e8,0x211f1f3e,
+    0xdd4b4b96,0xdcbdbd61,0x868b8b0d,0x858a8a0f,0x907070e0,0x423e3e7c,
+    0xc4b5b571,0xaa6666cc,0xd8484890,0x5030306,0x1f6f6f7,0x120e0e1c,
+    0xa36161c2,0x5f35356a,0xf95757ae,0xd0b9b969,0x91868617,0x58c1c199,
+    0x271d1d3a,0xb99e9e27,0x38e1e1d9,0x13f8f8eb,0xb398982b,0x33111122,
+    0xbb6969d2,0x70d9d9a9,0x898e8e07,0xa7949433,0xb69b9b2d,0x221e1e3c,
+    0x92878715,0x20e9e9c9,0x49cece87,0xff5555aa,0x78282850,0x7adfdfa5,
+    0x8f8c8c03,0xf8a1a159,0x80898909,0x170d0d1a,0xdabfbf65,0x31e6e6d7,
+    0xc6424284,0xb86868d0,0xc3414182,0xb0999929,0x772d2d5a,0x110f0f1e,
+    0xcbb0b07b,0xfc5454a8,0xd6bbbb6d,0x3a16162c
+};
+
+static const unsign32 rtable[]=
+{
+    0x50a7f451,0x5365417e,0xc3a4171a,0x965e273a,0xcb6bab3b,0xf1459d1f,
+    0xab58faac,0x9303e34b,0x55fa3020,0xf66d76ad,0x9176cc88,0x254c02f5,
+    0xfcd7e54f,0xd7cb2ac5,0x80443526,0x8fa362b5,0x495ab1de,0x671bba25,
+    0x980eea45,0xe1c0fe5d,0x2752fc3,0x12f04c81,0xa397468d,0xc6f9d36b,
+    0xe75f8f03,0x959c9215,0xeb7a6dbf,0xda595295,0x2d83bed4,0xd3217458,
+    0x2969e049,0x44c8c98e,0x6a89c275,0x78798ef4,0x6b3e5899,0xdd71b927,
+    0xb64fe1be,0x17ad88f0,0x66ac20c9,0xb43ace7d,0x184adf63,0x82311ae5,
+    0x60335197,0x457f5362,0xe07764b1,0x84ae6bbb,0x1ca081fe,0x942b08f9,
+    0x58684870,0x19fd458f,0x876cde94,0xb7f87b52,0x23d373ab,0xe2024b72,
+    0x578f1fe3,0x2aab5566,0x728ebb2,0x3c2b52f,0x9a7bc586,0xa50837d3,
+    0xf2872830,0xb2a5bf23,0xba6a0302,0x5c8216ed,0x2b1ccf8a,0x92b479a7,
+    0xf0f207f3,0xa1e2694e,0xcdf4da65,0xd5be0506,0x1f6234d1,0x8afea6c4,
+    0x9d532e34,0xa055f3a2,0x32e18a05,0x75ebf6a4,0x39ec830b,0xaaef6040,
+    0x69f715e,0x51106ebd,0xf98a213e,0x3d06dd96,0xae053edd,0x46bde64d,
+    0xb58d5491,0x55dc471,0x6fd40604,0xff155060,0x24fb9819,0x97e9bdd6,
+    0xcc434089,0x779ed967,0xbd42e8b0,0x888b8907,0x385b19e7,0xdbeec879,
+    0x470a7ca1,0xe90f427c,0xc91e84f8,0x0,0x83868009,0x48ed2b32,
+    0xac70111e,0x4e725a6c,0xfbff0efd,0x5638850f,0x1ed5ae3d,0x27392d36,
+    0x64d90f0a,0x21a65c68,0xd1545b9b,0x3a2e3624,0xb1670a0c,0xfe75793,
+    0xd296eeb4,0x9e919b1b,0x4fc5c080,0xa220dc61,0x694b775a,0x161a121c,
+    0xaba93e2,0xe52aa0c0,0x43e0223c,0x1d171b12,0xb0d090e,0xadc78bf2,
+    0xb9a8b62d,0xc8a91e14,0x8519f157,0x4c0775af,0xbbdd99ee,0xfd607fa3,
+    0x9f2601f7,0xbcf5725c,0xc53b6644,0x347efb5b,0x7629438b,0xdcc623cb,
+    0x68fcedb6,0x63f1e4b8,0xcadc31d7,0x10856342,0x40229713,0x2011c684,
+    0x7d244a85,0xf83dbbd2,0x1132f9ae,0x6da129c7,0x4b2f9e1d,0xf330b2dc,
+    0xec52860d,0xd0e3c177,0x6c16b32b,0x99b970a9,0xfa489411,0x2264e947,
+    0xc48cfca8,0x1a3ff0a0,0xd82c7d56,0xef903322,0xc74e4987,0xc1d138d9,
+    0xfea2ca8c,0x360bd498,0xcf81f5a6,0x28de7aa5,0x268eb7da,0xa4bfad3f,
+    0xe49d3a2c,0xd927850,0x9bcc5f6a,0x62467e54,0xc2138df6,0xe8b8d890,
+    0x5ef7392e,0xf5afc382,0xbe805d9f,0x7c93d069,0xa92dd56f,0xb31225cf,
+    0x3b99acc8,0xa77d1810,0x6e639ce8,0x7bbb3bdb,0x97826cd,0xf418596e,
+    0x1b79aec,0xa89a4f83,0x656e95e6,0x7ee6ffaa,0x8cfbc21,0xe6e815ef,
+    0xd99be7ba,0xce366f4a,0xd4099fea,0xd67cb029,0xafb2a431,0x31233f2a,
+    0x3094a5c6,0xc066a235,0x37bc4e74,0xa6ca82fc,0xb0d090e0,0x15d8a733,
+    0x4a9804f1,0xf7daec41,0xe50cd7f,0x2ff69117,0x8dd64d76,0x4db0ef43,
+    0x544daacc,0xdf0496e4,0xe3b5d19e,0x1b886a4c,0xb81f2cc1,0x7f516546,
+    0x4ea5e9d,0x5d358c01,0x737487fa,0x2e410bfb,0x5a1d67b3,0x52d2db92,
+    0x335610e9,0x1347d66d,0x8c61d79a,0x7a0ca137,0x8e14f859,0x893c13eb,
+    0xee27a9ce,0x35c961b7,0xede51ce1,0x3cb1477a,0x59dfd29c,0x3f73f255,
+    0x79ce1418,0xbf37c773,0xeacdf753,0x5baafd5f,0x146f3ddf,0x86db4478,
+    0x81f3afca,0x3ec468b9,0x2c342438,0x5f40a3c2,0x72c31d16,0xc25e2bc,
+    0x8b493c28,0x41950dff,0x7101a839,0xdeb30c08,0x9ce4b4d8,0x90c15664,
+    0x6184cb7b,0x70b632d5,0x745c6c48,0x4257b8d0
+};
+
+#define MR_TOBYTE(x) ((uchar)((x)))
+
+static unsign32 pack(const uchar *b)
+{
+    /* pack bytes into a 32-bit Word */
+    return 
((unsign32)b[3]<<24)|((unsign32)b[2]<<16)|((unsign32)b[1]<<8)|(unsign32)b[0];
+}
+
+static void unpack(unsign32 a,uchar *b)
+{
+    /* unpack bytes from a word */
+    b[0]=MR_TOBYTE(a);
+    b[1]=MR_TOBYTE(a>>8);
+    b[2]=MR_TOBYTE(a>>16);
+    b[3]=MR_TOBYTE(a>>24);
+}
+
+/* SU= 8 */
+static uchar bmul(uchar x,uchar y)
+{
+    /* x.y= AntiLog(Log(x) + Log(y)) */
+    if (x && y) return ptab[(ltab[x]+ltab[y])%255];
+    else return 0;
+}
+
+static unsign32 SubByte(unsign32 a)
+{
+    uchar b[4];
+    unpack(a,b);
+    b[0]=fbsub[b[0]];
+    b[1]=fbsub[b[1]];
+    b[2]=fbsub[b[2]];
+    b[3]=fbsub[b[3]];
+    return pack(b);
+}
+
+/* SU= 16 */
+static uchar product(unsign32 x,unsign32 y)
+{
+    /* dot product of two 4-byte arrays */
+    uchar xb[4],yb[4];
+    unpack(x,xb);
+    unpack(y,yb);
+    return 
bmul(xb[0],yb[0])^bmul(xb[1],yb[1])^bmul(xb[2],yb[2])^bmul(xb[3],yb[3]);
+}
+
+static unsign32 InvMixCol(unsign32 x)
+{
+    /* matrix Multiplication */
+    unsign32 y,m;
+    uchar b[4];
+
+    m=pack(InCo);
+    b[3]=product(m,x);
+    m=ROTL24(m);
+    b[2]=product(m,x);
+    m=ROTL24(m);
+    b[1]=product(m,x);
+    m=ROTL24(m);
+    b[0]=product(m,x);
+    y=pack(b);
+    return y;
+}
+
+/* SU= 8 */
+/* reset cipher */
+void amcl::AES_reset(amcl::aes *a,int mode,char *iv)
+{
+    /* reset mode, or reset iv */
+    int i;
+    a->mode=mode;
+    for (i=0; i<4*NB; i++)
+        a->f[i]=0;
+    if (mode!=ECB && iv!=NULL)
+    {
+        for (i=0; i<4*NB; i++)
+            a->f[i]=iv[i];
+    }
+}
+
+void amcl::AES_getreg(amcl::aes *a,char *ir)
+{
+    int i;
+    for (i=0; i<4*NB; i++) ir[i]=a->f[i];
+}
+
+/* SU= 72 */
+/* Initialise cipher */
+int amcl::AES_init(amcl::aes* a,int mode,int nk,char *key,char *iv)
+{
+    /* Key length Nk=16, 24 or 32 bytes */
+    /* Key Scheduler. Create expanded encryption key */
+    int i,j,k,N,nr;
+    unsign32 CipherKey[8];
+
+    nk/=4;
+
+    if (nk!=4 && nk!=6 && nk!=8) return 0;
+
+    nr=6+nk;
+
+    a->Nk=nk;
+    a->Nr=nr;
+
+    AES_reset(a,mode,iv);
+
+    N=NB*(nr+1);
+
+    for (i=j=0; i<nk; i++,j+=4)
+    {
+        CipherKey[i]=pack((uchar *)&key[j]);
+    }
+    for (i=0; i<nk; i++) a->fkey[i]=CipherKey[i];
+    for (j=nk,k=0; j<N; j+=nk,k++)
+    {
+        a->fkey[j]=a->fkey[j-nk]^SubByte(ROTL24(a->fkey[j-1]))^rco[k];
+        if (nk<=6)
+        {
+            for (i=1; i<nk && (i+j)<N; i++)
+                a->fkey[i+j]=a->fkey[i+j-nk]^a->fkey[i+j-1];
+        }
+        else
+        {
+            for (i=1; i<4 && (i+j)<N; i++)
+                a->fkey[i+j]=a->fkey[i+j-nk]^a->fkey[i+j-1];
+            if ((j+4)<N) a->fkey[j+4]=a->fkey[j+4-nk]^SubByte(a->fkey[j+3]);
+            for (i=5; i<nk && (i+j)<N; i++)
+                a->fkey[i+j]=a->fkey[i+j-nk]^a->fkey[i+j-1];
+        }
+
+    }
+    /* now for the expanded decrypt key in reverse order */
+
+    for (j=0; j<NB; j++) a->rkey[j+N-NB]=a->fkey[j];
+    for (i=NB; i<N-NB; i+=NB)
+    {
+        k=N-NB-i;
+        for (j=0; j<NB; j++) a->rkey[k+j]=InvMixCol(a->fkey[i+j]);
+    }
+    for (j=N-NB; j<N; j++) a->rkey[j-N+NB]=a->fkey[j];
+    return 1;
+}
+
+/* SU= 80 */
+/* Encrypt a single block */
+void amcl::AES_ecb_encrypt(amcl::aes *a,uchar *buff)
+{
+    int i,j,k;
+    unsign32 p[4],q[4],*x,*y,*t;
+
+    for (i=j=0; i<NB; i++,j+=4)
+    {
+        p[i]=pack((uchar *)&buff[j]);
+        p[i]^=a->fkey[i];
+    }
+
+    k=NB;
+    x=p;
+    y=q;
+
+    /* State alternates between x and y */
+    for (i=1; i<a->Nr; i++)
+    {
+
+        y[0]=a->fkey[k]^ftable[MR_TOBYTE(x[0])]^
+             ROTL8(ftable[MR_TOBYTE(x[1]>>8)])^
+             ROTL16(ftable[MR_TOBYTE(x[2]>>16)])^
+             ROTL24(ftable[x[3]>>24]);
+        y[1]=a->fkey[k+1]^ftable[MR_TOBYTE(x[1])]^
+             ROTL8(ftable[MR_TOBYTE(x[2]>>8)])^
+             ROTL16(ftable[MR_TOBYTE(x[3]>>16)])^
+             ROTL24(ftable[x[0]>>24]);
+        y[2]=a->fkey[k+2]^ftable[MR_TOBYTE(x[2])]^
+             ROTL8(ftable[MR_TOBYTE(x[3]>>8)])^
+             ROTL16(ftable[MR_TOBYTE(x[0]>>16)])^
+             ROTL24(ftable[x[1]>>24]);
+        y[3]=a->fkey[k+3]^ftable[MR_TOBYTE(x[3])]^
+             ROTL8(ftable[MR_TOBYTE(x[0]>>8)])^
+             ROTL16(ftable[MR_TOBYTE(x[1]>>16)])^
+             ROTL24(ftable[x[2]>>24]);
+
+        k+=4;
+        t=x;
+        x=y;
+        y=t;      /* swap pointers */
+    }
+
+    /* Last Round */
+
+    y[0]=a->fkey[k]^(unsign32)fbsub[MR_TOBYTE(x[0])]^
+         ROTL8((unsign32)fbsub[MR_TOBYTE(x[1]>>8)])^
+         ROTL16((unsign32)fbsub[MR_TOBYTE(x[2]>>16)])^
+         ROTL24((unsign32)fbsub[x[3]>>24]);
+    y[1]=a->fkey[k+1]^(unsign32)fbsub[MR_TOBYTE(x[1])]^
+         ROTL8((unsign32)fbsub[MR_TOBYTE(x[2]>>8)])^
+         ROTL16((unsign32)fbsub[MR_TOBYTE(x[3]>>16)])^
+         ROTL24((unsign32)fbsub[x[0]>>24]);
+    y[2]=a->fkey[k+2]^(unsign32)fbsub[MR_TOBYTE(x[2])]^
+         ROTL8((unsign32)fbsub[MR_TOBYTE(x[3]>>8)])^
+         ROTL16((unsign32)fbsub[MR_TOBYTE(x[0]>>16)])^
+         ROTL24((unsign32)fbsub[x[1]>>24]);
+    y[3]=a->fkey[k+3]^(unsign32)fbsub[MR_TOBYTE(x[3])]^
+         ROTL8((unsign32)fbsub[MR_TOBYTE(x[0]>>8)])^
+         ROTL16((unsign32)fbsub[MR_TOBYTE(x[1]>>16)])^
+         ROTL24((unsign32)fbsub[x[2]>>24]);
+
+    for (i=j=0; i<NB; i++,j+=4)
+    {
+        unpack(y[i],(uchar *)&buff[j]);
+        x[i]=y[i]=0;   /* clean up stack */
+    }
+}
+
+/* SU= 80 */
+/* Decrypt a single block */
+void amcl::AES_ecb_decrypt(amcl::aes *a,uchar *buff)
+{
+    int i,j,k;
+    unsign32 p[4],q[4],*x,*y,*t;
+
+    for (i=j=0; i<NB; i++,j+=4)
+    {
+        p[i]=pack((uchar *)&buff[j]);
+        p[i]^=a->rkey[i];
+    }
+
+    k=NB;
+    x=p;
+    y=q;
+
+    /* State alternates between x and y */
+    for (i=1; i<a->Nr; i++)
+    {
+        /* Nr is number of rounds. May be odd. */
+
+        y[0]=a->rkey[k]^rtable[MR_TOBYTE(x[0])]^
+             ROTL8(rtable[MR_TOBYTE(x[3]>>8)])^
+             ROTL16(rtable[MR_TOBYTE(x[2]>>16)])^
+             ROTL24(rtable[x[1]>>24]);
+        y[1]=a->rkey[k+1]^rtable[MR_TOBYTE(x[1])]^
+             ROTL8(rtable[MR_TOBYTE(x[0]>>8)])^
+             ROTL16(rtable[MR_TOBYTE(x[3]>>16)])^
+             ROTL24(rtable[x[2]>>24]);
+        y[2]=a->rkey[k+2]^rtable[MR_TOBYTE(x[2])]^
+             ROTL8(rtable[MR_TOBYTE(x[1]>>8)])^
+             ROTL16(rtable[MR_TOBYTE(x[0]>>16)])^
+             ROTL24(rtable[x[3]>>24]);
+        y[3]=a->rkey[k+3]^rtable[MR_TOBYTE(x[3])]^
+             ROTL8(rtable[MR_TOBYTE(x[2]>>8)])^
+             ROTL16(rtable[MR_TOBYTE(x[1]>>16)])^
+             ROTL24(rtable[x[0]>>24]);
+
+        k+=4;
+        t=x;
+        x=y;
+        y=t;      /* swap pointers */
+    }
+
+
+    /* Last Round */
+    y[0]=a->rkey[k]^(unsign32)rbsub[MR_TOBYTE(x[0])]^
+         ROTL8((unsign32)rbsub[MR_TOBYTE(x[3]>>8)])^
+         ROTL16((unsign32)rbsub[MR_TOBYTE(x[2]>>16)])^
+         ROTL24((unsign32)rbsub[x[1]>>24]);
+    y[1]=a->rkey[k+1]^(unsign32)rbsub[MR_TOBYTE(x[1])]^
+         ROTL8((unsign32)rbsub[MR_TOBYTE(x[0]>>8)])^
+         ROTL16((unsign32)rbsub[MR_TOBYTE(x[3]>>16)])^
+         ROTL24((unsign32)rbsub[x[2]>>24]);
+    y[2]=a->rkey[k+2]^(unsign32)rbsub[MR_TOBYTE(x[2])]^
+         ROTL8((unsign32)rbsub[MR_TOBYTE(x[1]>>8)])^
+         ROTL16((unsign32)rbsub[MR_TOBYTE(x[0]>>16)])^
+         ROTL24((unsign32)rbsub[x[3]>>24]);
+    y[3]=a->rkey[k+3]^(unsign32)rbsub[MR_TOBYTE(x[3])]^
+         ROTL8((unsign32)rbsub[MR_TOBYTE(x[2]>>8)])^
+         ROTL16((unsign32)rbsub[MR_TOBYTE(x[1]>>16)])^
+         ROTL24((unsign32)rbsub[x[0]>>24]);
+
+    for (i=j=0; i<NB; i++,j+=4)
+    {
+        unpack(y[i],(uchar *)&buff[j]);
+        x[i]=y[i]=0;   /* clean up stack */
+    }
+
+}
+
+/* simple default increment function */
+static void increment(char *f)
+{
+    int i;
+    for (i=0; i<16; i++)
+    {
+        f[i]++;
+        if (f[i]!=0) break;
+    }
+}
+
+/* SU= 40 */
+/* Encrypt using selected mode of operation */
+unsign32 amcl::AES_encrypt(amcl::aes* a,char *buff)
+{
+    int j,bytes;
+    char st[16];
+    unsign32 fell_off;
+
+    /* Supported Modes of Operation */
+
+    fell_off=0;
+    switch (a->mode)
+    {
+    case ECB:
+        AES_ecb_encrypt(a,(uchar *)buff);
+        return 0;
+    case CBC:
+        for (j=0; j<4*NB; j++) buff[j]^=a->f[j];
+        AES_ecb_encrypt(a,(uchar *)buff);
+        for (j=0; j<4*NB; j++) a->f[j]=buff[j];
+        return 0;
+
+    case CFB1:
+    case CFB2:
+    case CFB4:
+        bytes=a->mode-CFB1+1;
+        for (j=0; j<bytes; j++) fell_off=(fell_off<<8)|a->f[j];
+        for (j=0; j<4*NB; j++) st[j]=a->f[j];
+        for (j=bytes; j<4*NB; j++) a->f[j-bytes]=a->f[j];
+        AES_ecb_encrypt(a,(uchar *)st);
+        for (j=0; j<bytes; j++)
+        {
+            buff[j]^=st[j];
+            a->f[16-bytes+j]=buff[j];
+        }
+        return fell_off;
+
+    case OFB1:
+    case OFB2:
+    case OFB4:
+    case OFB8:
+    case OFB16:
+
+        bytes=a->mode-OFB1+1;
+        AES_ecb_encrypt(a,(uchar *)(a->f));
+        for (j=0; j<bytes; j++) buff[j]^=a->f[j];
+        return 0;
+
+    case CTR1:
+    case CTR2:
+    case CTR4:
+    case CTR8:
+    case CTR16:
+
+        bytes=a->mode-CTR1+1;
+        for (j=0; j<4*NB; j++) st[j]=a->f[j];
+        AES_ecb_encrypt(a,(uchar *)st);
+        for (j=0; j<bytes; j++) buff[j]^=st[j];
+        increment(a->f);
+               return 0;
+
+    default:
+        return 0;
+    }
+}
+
+/* SU= 40 */
+/* Decrypt using selected mode of operation */
+unsign32 amcl::AES_decrypt(amcl::aes *a,char *buff)
+{
+    int j,bytes;
+    char st[16];
+    unsign32 fell_off;
+
+    /* Supported modes of operation */
+    fell_off=0;
+    switch (a->mode)
+    {
+    case ECB:
+        AES_ecb_decrypt(a,(uchar *)buff);
+        return 0;
+    case CBC:
+        for (j=0; j<4*NB; j++)
+        {
+            st[j]=a->f[j];
+            a->f[j]=buff[j];
+        }
+        AES_ecb_decrypt(a,(uchar *)buff);
+        for (j=0; j<4*NB; j++)
+        {
+            buff[j]^=st[j];
+            st[j]=0;
+        }
+        return 0;
+    case CFB1:
+    case CFB2:
+    case CFB4:
+        bytes=a->mode-CFB1+1;
+        for (j=0; j<bytes; j++) fell_off=(fell_off<<8)|a->f[j];
+        for (j=0; j<4*NB; j++) st[j]=a->f[j];
+        for (j=bytes; j<4*NB; j++) a->f[j-bytes]=a->f[j];
+        AES_ecb_encrypt(a,(uchar *)st);
+        for (j=0; j<bytes; j++)
+        {
+            a->f[16-bytes+j]=buff[j];
+            buff[j]^=st[j];
+        }
+        return fell_off;
+    case OFB1:
+    case OFB2:
+    case OFB4:
+    case OFB8:
+    case OFB16:
+        bytes=a->mode-OFB1+1;
+        AES_ecb_encrypt(a,(uchar *)(a->f));
+        for (j=0; j<bytes; j++) buff[j]^=a->f[j];
+        return 0;
+
+    case CTR1:
+    case CTR2:
+    case CTR4:
+    case CTR8:
+    case CTR16:
+
+        bytes=a->mode-CTR1+1;
+        for (j=0; j<4*NB; j++) st[j]=a->f[j];
+        AES_ecb_encrypt(a,(uchar *)st);
+        for (j=0; j<bytes; j++) buff[j]^=st[j];
+        increment(a->f);
+               return 0;
+
+    default:
+        return 0;
+    }
+}
+
+/* Clean up and delete left-overs */
+void amcl::AES_end(amcl::aes *a)
+{
+    /* clean up */
+    int i;
+    for (i=0; i<NB*(a->Nr+1); i++)
+        a->fkey[i]=a->rkey[i]=0;
+    for (i=0; i<4*NB; i++)
+        a->f[i]=0;
+}
+
+
+/*
+#include <stdio.h>
+
+#define KK 32
+
+int main()
+{
+    int i;
+    aes a;
+       unsign32 t;
+       uchar x,y;
+
+    char key[KK];
+    char block[16];
+    char iv[16];
+    for (i=0;i<KK;i++) key[i]=5;
+    key[0]=1;
+    for (i=0;i<16;i++) iv[i]=i;
+    for (i=0;i<16;i++) block[i]=i;
+
+    AES_init(&a,CTR16,KK,key,iv);
+
+    printf("Plain=   ");
+    for (i=0;i<16;i++) printf("%02x",block[i]);
+    printf("\n");
+    AES_encrypt(&a,block);
+    printf("Encrypt= ");
+    for (i=0;i<16;i++) printf("%02x",(uchar)block[i]);
+    printf("\n");
+    AES_reset(&a,CTR16,iv);
+    AES_decrypt(&a,block);
+    printf("Decrypt= ");
+    for (i=0;i<16;i++) printf("%02x",(uchar)block[i]);
+    printf("\n");
+
+    AES_end(&a);
+
+    return 0;
+}
+
+*/
+

Reply via email to