otp - implementation of TOTP using C and Gcrypt -


i'm trying implement simple totp client using c , libgcrypt.
i've read both rfc 6238 , rfc 4226 still have problems result.

#include <stdio.h> #include <time.h> #include <string.h> #include <gcrypt.h>  int digits_power[] = {1,10,100,1000,10000,100000,1000000,10000000,100000000};  int truncate (unsigned char *hmac, int n) {     // uint64_t o = least_four_significant_bits_hmac;     int o = (hmac[19] & 0x0f);     int bin_code = ((hmac[o] & 0x7f) << 24) | ((hmac[o+1] & 0xff) << 16) | ((hmac[o+2] & 0xff) << 8) | ((hmac[o+3] & 0xff));     printf("%d\n", bin_code);     int token = bin_code % digits_power[n];     return token; }  unsigned char *hmac (char *k, long c) {     // k key, c message     unsigned char *buffer = (unsigned char *)&c;     gcry_md_hd_t hd;     gcry_md_open (&hd, gcry_md_sha1, gcry_md_flag_hmac);     gcry_md_setkey (hd, k, strlen(k));     gcry_md_write (hd, buffer, 4); // 4 because c (in hex) 0273ef07     gcry_md_final (hd);     unsigned char *hmac =  gcry_md_read (hd, gcry_md_sha1);     return hmac; }   int hotp (char *k, long c, int n) {     unsigned char *hmac = hmac(k, c);     int token = truncate (hmac, n);     return token; }   int totp (char *k, int n) {     long tc = 1234567890/30;// ((uint64_t) time (null))/30;     return hotp(k, tc, n); }  int main (void) {     printf ("%d\n", totp("12345678901234567890", 6));     return 0; } 

according test vectors written in appendix-b, if use tc=1234567890/30 , secret_key="12345678901234567890" should receive 89005924 totp value but, instead, receive 37734835.
what's wrong code? :-/
thanks


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -