Esempio n. 1
0
std::string count_it(std::string a, std::string b, count_fn f)
{
    std::string res;// res.reserve(1024);
    if (f!=mul)
        prepend_zeros(a,b);
    DLOGF("ENTER: a=%s b=%s\n",a.c_str(),b.c_str());

    res = f(a,b);

    remove_prep_zeros(res);

    return res;
}
Esempio n. 2
0
std::string sub(std::string a,std::string b)
{
    std::string res;// res.reserve(1024);
    prepend_zeros(a,b);
    DLOGF("ENTER: a=%s b=%s\n",a.c_str(),b.c_str());

    int borrowed=0;
    for (int i=a.size()-1;i>=0;--i) {
        int c=a[i]&0xf;
        int d=b[i]&0xf;
        int e=c-d-borrowed;
        if (e<0) {
            borrowed=1;
            e+=10;
        } else 
            borrowed=0;
        res+=e+0x30;
    }
    reverse(res);
    return res;
}
Esempio n. 3
0
std::string add(std::string a,std::string b)
{
    std::string res;// res.reserve(1024);
    prepend_zeros(a,b);
    DLOGF("ENTER: a=%s b=%s\n",a.c_str(),b.c_str());

    int rest=0;
    int i=0;
    DLOG("Counting sum \n");
    for (i=a.size()-1;i>=0;--i) {
        int c=a[i]&0xf;
        int d=b[i]&0xf;
        res+=(((c+d+rest)%10) + 0x30);
        DLOGF("i=%d c=%d d=%d rest=%d sum=%d res=%s\n",i,c,d,rest,c+d+rest,res.c_str());
        if (c+d+rest>9) rest=1;else rest=0;
    }
    if (rest)
        res+='1';

    reverse(res);
    return res;
}
Esempio n. 4
0
int 
validateOTP(char * secret_hex, uint8_t * data, char * HOTP_string)
{
	int i, j;
    uint8_t ipad[65]; /* inner padding - key XORd with ipad */
    uint8_t opad[65]; /* outer padding - key XORd with opad */
    int s_len = strlen(secret_hex);
    int k_len = s_len/2;
    uint8_t key[k_len];
    SHA1_INFO ctx;


    // Convert string of 20 hex characters to an array of 
    // bytes (two hex chars correspond to 1 uint8_t value)
    for (i = 0, j = 0; i < s_len; i+=2, j++) 
        key[j] = hexstr_to_hex(secret_hex[i]) * 16 + hexstr_to_hex(secret_hex[i + 1]);
    

    /* The HMAC_SHA1 transform looks like: */
    /* SHA1(K XOR opad, SHA1(K XOR ipad, text)) */
    /* where K is an n byte key */
    /* ipad is the byte 0x36 repeated 64 times */
    /* opad is the byte 0x5c repeated 64 times */
    /* and text is the data being protected */ 

    memset(ipad, 0, sizeof(ipad));
    memset(opad, 0, sizeof(opad));
    memcpy(ipad, key, k_len);
    memcpy(opad, key, k_len);

    /* XOR key with ipad and opad values */
    for (i = 0; i < 64; i++) {
        ipad[i] ^= 0x36;
        opad[i] ^= 0x5c;
    }

	
    // Compute inner hash
    uint8_t ihmac[SHA1_DIGEST_LENGTH];
    sha1_init(&ctx);
    sha1_update(&ctx, ipad, 64);
    sha1_update(&ctx, data, sizeof(data));
    sha1_final(&ctx, ihmac);

    // Compute inner hash
    uint8_t hmac[SHA1_DIGEST_LENGTH];
    sha1_init(&ctx);
    sha1_update(&ctx, opad, 64);
    sha1_update(&ctx, ihmac, SHA1_DIGEST_LENGTH);
    sha1_final(&ctx, hmac);

	// Extract 4B dynamic binary code from HMAC
	int offset = hmac[SHA1_DIGEST_LENGTH - 1] & 0x0f;
	long binary = ((hmac[offset] & 0x7f) << 24)
		| ((hmac[offset + 1] & 0xff) << 16)
		| ((hmac[offset + 2] & 0xff) << 8)
		| ( hmac[offset + 3] & 0xff);

	long otp = binary % 1000000;
	char otp_str[7];
	sprintf(otp_str, "%ld", otp);
	while(strlen(otp_str) < 6)		
		prepend_zeros(otp_str);

	if(strcmp(HOTP_string, otp_str)==0)
		return 1;
	else return 0;

}