Exemple #1
0
bool KG_GeneratePvtY(int level, char* keytemplate, char* pvt_text, char* y_text)
{
    char tmp[256] = "";
    BigInt p, p1, pub, pvt, y, temp, temp2;
    int size, keysystem;
    if(level == 29)
    {
        char basepoint_text[10] = "", public_x_text[100] = "", public_y_text[100] = "";
        KG_GenerateEcdsaParameters(keytemplate, pvt_text, basepoint_text, public_x_text, public_y_text);
        sprintf(y_text, "%s,%s,%s", basepoint_text, public_x_text, public_y_text);
        return true;
    }
    else if(level >= 20)
    {
        keysystem = KS_SHORTV3;
        level = level - 20;
        if(level > 8)
            return false;
    }
    else if(level >= 10)
    {
        keysystem = KS_V3;
        level = level - 10;
        if(level > 8)
            return false;
    }
    else
    {
        keysystem = KS_V2;
        if(level<0 or level>3)
            return false;
    }
    size = level + 4;
    p = BigInt_Create();
    p1 = BigInt_Create();
    pub = BigInt_Create();
    pvt = BigInt_Create();
    y = BigInt_Create();
    temp = BigInt_Create();
    temp2 = BigInt_Create();
    BigInt_Set(temp, 1);
    BigInt_Shift(temp, size * 8, p);
    BigInt_Set(temp, primeoffsets[level]);
    BigInt_Add(p, temp, temp2);
    BigInt_Copy(p, temp2);
    BigInt_Subtract(p, BigInt_One(), p1);
    sprintf(tmp, "%u Level Public Key", level);
    GenerateKeyNumberFromString(tmp, p, &pub, keysystem, ((keysystem == KS_V3 || keysystem == KS_SHORTV3) ? level + 1 : 0));
    GenerateKeyNumberFromString(keytemplate, p, &pvt, keysystem, ((keysystem == KS_V3 || keysystem == KS_SHORTV3) ? level + 1 : 0));
    BigInt_ToString(pvt, 16, pvt_text);
    BigInt_PowerModulus(pub, pvt, p, y);
    BigInt_ToString(y, 16, y_text);
    BigInt_Destroy(temp2);
    BigInt_Destroy(temp);
    BigInt_Destroy(y);
    BigInt_Destroy(pvt);
    BigInt_Destroy(pub);
    BigInt_Destroy(p1);
    BigInt_Destroy(p);
    return true;
}
Exemple #2
0
int DecodeShortV3(const char* serial, bool level10, unsigned char* dest, int dest_len)
{
    char keybytes_[512];

    char* kb=keybytes_;
    char* keybytes=keybytes_;
    char* k1;
    char* k2;
    bool keystring=false;
    int keylength;
    const char* udigits="0123456789ABCDEFGHJKMNPQRTUVWXYZ";
    const char* ldigits="0123456789abcdefghjkmnpqrtuvwxyz";
    const char* c=serial;
    const char* p;
    unsigned char value=0;
    unsigned char firstdigit=1;
    BigInt n, n2, n3;
    n=BigInt_Create();
    n2=BigInt_Create();
    n3=BigInt_Create();
    int level=0;
    if(level10)
        level=10;
    if(serial==0 || serial[0]==0)
    {
        return 0;
    }
    while(c[0])
    {
        p=strchr(udigits, c[0]); //first the current serial character in udigits
        if(p)
        {
            value=p-udigits;
        }
        else
        {
            p=strchr(ldigits, c[0]); //first the current character in ldigits
            if(p)
            {
                value=p-ldigits;
            }
            else if(c[0]=='i' || c[0]=='I' || c[0]=='l' || c[0]=='L')
            {
                value=1;
            }
            else if(c[0]=='o' || c[0]=='O')
            {
                value=0;
            }
            else if(c[0]=='s' || c[0]=='S')
            {
                value=5;
            }
            else
            {
                value=32;
            }
        }
        c++;

        if(value<32) //must be base32
        {
            if(firstdigit) //ignore the first key character
            {
                if(level==10)
                {
                    /* All level 10 keys start with the digit 1. It
                    doesn't convey any information other than the fact
                    that they're level 10 keys; discard it. */
                    //KeyString starts with 3
                    if(value==3)
                    {
                        value=0;
                        keystring=true; //TODO: remove?
                    }
                    if(value!=0)
                    {
                        value=0;
                        firstdigit=0;
                    }
                }
                else
                {
                    //KeyString starts with 3
                    if(value==3)
                    {
                        value=0;
                        keystring=true; //TODO: remove?
                    }
                    if(value!=0 && value>=16)
                    {
                        value-=16;
                        firstdigit=0;
                    }
                }
            }
            BigInt_Shift(n, 5, n2);
            BigInt_SetU(n3, value);
            BigInt_Add(n2, n3, n);
        }
    } //end of loop

    //Spit out the bytes, in reverse order.
    BigInt_Set(n3, 0xFF);
    if(level==10)
    {
        while(BigInt_Compare(n, BigInt_One())>0)
        {
            BigInt_And(n, n3, n2);
            kb[0]=(unsigned char)BigInt_GetU(n2);
            kb++;
            BigInt_Shift(n, -8, n2);
            BigInt_Copy(n, n2);
        }
    }
    else
    {
        while(BigInt_Compare(n, BigInt_Zero())!=0)
        {
            BigInt_And(n, n3, n2);
            kb[0]=(unsigned char)BigInt_GetU(n2);
            kb++;
            BigInt_Shift(n, -8, n2);
            BigInt_Copy(n, n2);
        }
    }
    if((kb-keybytes)%2) //if the length / 2 has a remainder
    {
        kb[0]=0; //discard last byte?
        kb++;
    }
    /* Reverse digits in keybytes */
    k1=keybytes;
    k2=kb-1;
    while(k1<k2)
    {
        char t=k1[0];
        k1[0]=k2[0];
        k2[0]=t;
        k2--;
        k1++;
    }
    BigInt_Destroy(n3);
    BigInt_Destroy(n2);
    BigInt_Destroy(n);
    keylength=kb-keybytes;
    memset(dest, 0, dest_len);
    memcpy(dest, keybytes, keylength);
    return keylength;
}
Exemple #3
0
void BigInt_Subtract(BigInt a, BigInt b, BigInt answer)
{
    b->negative=!b->negative;
    BigInt_Add(a, b, answer);
    b->negative=!b->negative;
}
Exemple #4
0
char* EncodeShortV3(unsigned char* keybytes, int keylength, bool level10)
{
    char* cc;
    char* shortv3digits=(char*)"0123456789ABCDEFGHJKMNPQRTUVWXYZ";
    static char retval[512]="";
    int level=0;
    int dcount;
    int nn;
    if(level10)
        level=29;
    strcpy(retval, "");
    BigInt n=BigInt_Create();
    BigInt t1=BigInt_Create();
    BigInt t2=BigInt_Create();
    if(level==29)
        BigInt_Set(n, 1);
    for(int x=0; x<keylength; ++x)
    {
        BigInt_Shift(n, 8, t1);
        BigInt_SetU(t2, keybytes[x]);
        BigInt_Add(t1, t2, n);
    }
    cc=retval;
    dcount=6;
    while(BigInt_Compare(n, BigInt_Zero())!=0)
    {
        BigInt_SetU(t2, 32);
        BigInt_Modulus(n, t2, t1);
        nn=BigInt_Get(t1);
        BigInt_Shift(n, -5, t2);
        BigInt_Copy(n, t2);
        if(level==29)
        {
            *cc++=shortv3digits[nn];
            if(--dcount==0)
            {
                dcount=6;
                *cc++='-';
            }
        }
        else
        {
            if(BigInt_Compare(n, BigInt_Zero())==0)
            {
                if(nn<16)
                {
                    *cc++=shortv3digits[nn+16];
                    --dcount;
                }
                else
                {
                    *cc++=shortv3digits[nn];
                    if(--dcount==0)
                    {
                        dcount=6;
                        *cc++='-';
                    }
                    *cc++=shortv3digits[16];
                    --dcount;
                }
            }
            else
            {
                *cc++=shortv3digits[nn];
                if(--dcount==0)
                {
                    dcount=6;
                    *cc++='-';
                }
            }
        }
    }
    if(level==29)
    {
        *cc++='1';
        --dcount;
    }
    while(dcount-->0)
        *cc++='0';
    *cc=0;
    mystrrev(retval);
    BigInt_Destroy(t2);
    BigInt_Destroy(t1);
    BigInt_Destroy(n);
    return retval;
}