/* * Certificate info: * - id: requester identification * - cname: common name * - time: date on which the certificate has been generated * - valid: date up to the certificate is valid * - auth_key: key used in mutual authentication (SMQV) * - token_key: key used to sign access token (MSS) * - signature: signature under issuer's key * */ void generate_certificate(const unsigned char csr[CSR_MAX_SIZE], const char valid[TIME_BUFFER_SIZE], const unsigned char ca_skey[ECDSA_SKEY_SIZE], unsigned char certificate[CERTIFICATE_MAX_SIZE]) { unsigned int id; char cname[CNAME_MAX_SIZE], time[TIME_BUFFER_SIZE]; unsigned char auth_key[SMQV_PKEY_SIZE], token_key[MSS_PKEY_SIZE], cert_signature[ECDSA_SIGNATURE_SIZE], csr_signature[MSS_SIGNATURE_SIZE]; unsigned char buffer[CERTIFICATE_MAX_SIZE]; memset(buffer, 0, CERTIFICATE_MAX_SIZE); now(&time); if(compare_dates(valid, time) == -1 && read_csr(&id, cname, time, auth_key, token_key, csr_signature, csr)) { unsigned int index = 0; index += cert_append_info(buffer, id, cname, time, valid, auth_key, token_key); unsigned char cert_digest[2 * MSS_SEC_LVL]; sponge_hash(buffer, index, cert_digest, 2 * MSS_SEC_LVL); ecdsa_sign(ca_skey, cert_digest, cert_signature); memcpy(buffer + index, cert_signature, ECDSA_SIGNATURE_SIZE); index += ECDSA_SIGNATURE_SIZE; base64encode(buffer, index, certificate, CSR_MAX_SIZE); } else{ certificate[0] = '\0'; if( compare_dates(valid, time) != -1) printf("Authentication ERROR: !(valid > t_now)\n"); else printf("Authentication ERROR: !mss_verify\n"); } }
int main(void) { struct date date1, date2; printf ("Enter first date (mm/dd/yy): "); scanf ("%d / %d / %d", &date1.month, &date1.day, &date1.year); printf ("Enter second date (mm/dd/yy): "); scanf ("%d / %d / %d", &date2.month, &date2.day, &date2.year); if (compare_dates(date1, date2) == -1){ printf ("%.2d/%.2d/%.2d is earlier than %.2d/%.2d/%.2d\n", date1.month, date1.day, date1.year, date2.month, date2.day, date2.year); }else if (compare_dates(date1, date2) == 1){ printf ("%.2d/%.2d/%.2d is later than %.2d/%.2d/%.2d\n", date1.month, date1.day, date1.year, date2.month, date2.day, date2.year); }else{ printf ("Both dates are identical\n"); } return 0; }
struct transaction * mergeSortedArrays(struct transaction *A, int ALen, struct transaction *B, int BLen) { if (A == NULL || ALen <= 0 || B == NULL || BLen <= 0){ //validity return NULL; } int A_ind, B_ind, Res_ind,pos; struct transaction *Res_arr = (struct transaction *)malloc((ALen + BLen)*sizeof(struct transaction)); //allocation of resultant array for (A_ind = B_ind = Res_ind = 0; A_ind < ALen&&B_ind < BLen; Res_ind++){ //moving up till any one transaction reaches to end. pos = compare_dates(A[A_ind].date, B[B_ind].date); //finding which transaction occures first. if (pos == 1){ Res_arr[Res_ind] = A[A_ind]; A_ind++; } else{ Res_arr[Res_ind] = B[B_ind]; B_ind++; } } for (; A_ind < ALen;A_ind++,Res_ind++){ Res_arr[Res_ind] = A[A_ind]; } for (; B_ind < BLen; B_ind++, Res_ind++){ Res_arr[Res_ind] = B[B_ind]; } return Res_arr; }
int main(void) { struct date date1, date2; int result; printf("Enter first date (mm/dd/yy): "); scanf("%d /%d /%d", &date1.month, &date1.day, &date1.year); printf("Enter second date (mm/dd/yy): "); scanf("%d /%d /%d", &date2.month, &date2.day, &date2.year); result = compare_dates(date1, date2); if (result < 0) printf("%.2d/%.2d/%.2d is earlier than %.2d/%.2d/%.2d.\n", date1.month, date1.day, date1.year, date2.month, date2.day, date2.year); else if (result > 0) printf("%.2d/%.2d/%.2d is earlier than %.2d/%.2d/%.2d.\n", date2.month, date2.day, date2.year, date1.month, date1.day, date1.year); else printf("%.2d/%.2d/%.2d is same as %.2d/%.2d/%.2d.\n", date2.month, date2.day, date2.year, date1.month, date1.day, date1.year); return 0; }
int main (void) { struct date x, y; printf("Please enter day and month, for example 12/02 : "); scanf("%d/%d", &x.day, &x.month); printf("Please enter day and month again : "); scanf("%d/%d", &y.day, &y.month); printf("First date value is %d.\n", day_of_year(x)); printf("Second date value is %d.\n", day_of_year(y)); if (compare_dates(x,y) > 0) printf("Second date is earlier.\n"); else if (compare_dates(x,y) == 0) printf("Two dates are same.\n"); else printf("First date is earlier.\n"); return 0; }
int main (void) { struct date d1 = {1972, 3, 16}, d2 = {1972, 3, 16}; printf("d1.day: %d\n", day_of_year(d1)); printf("compare_dates(d1, d2): %d\n", compare_dates(d1, d2)); return 0; }
// return 1 if certificate is valid, 0 otherwise unsigned char read_certificate(unsigned int *id, char *cname, char time[TIME_BUFFER_SIZE], char valid[TIME_BUFFER_SIZE], unsigned char auth_key[SMQV_PKEY_SIZE], unsigned char token_key[MSS_PKEY_SIZE], unsigned char cert_signature[ECDSA_SIGNATURE_SIZE], const unsigned char ca_pkey[ECDSA_PKEY_SIZE], const unsigned char certificate[CERTIFICATE_MAX_SIZE]) { unsigned int index = 0; int certificate_size = CERTIFICATE_MAX_SIZE; unsigned char buffer[CERTIFICATE_MAX_SIZE]; char t_now[TIME_BUFFER_SIZE]; now(&t_now); memset(buffer, 0, CERTIFICATE_MAX_SIZE); base64decode(certificate, strlen(certificate), buffer, &certificate_size); cert_split_info(buffer, id, cname, time, valid, auth_key, token_key, cert_signature); unsigned char cert_digest[2 * MSS_SEC_LVL]; index = cert_append_info(buffer, *id, cname, time, valid, auth_key, token_key); sponge_hash(buffer, index, cert_digest, 2 * MSS_SEC_LVL); // verify [(id || cname || time || valid || auth_key || token_key), csr_signature, token_key] return compare_dates(t_now, time) <= 0 && compare_dates(t_now, valid) >= 0 && ecdsa_verify(ca_pkey, cert_digest, cert_signature); }
int countGreaterNumbers(struct transaction *Arr, int len, char *date) { int currTrans = 0, count = 0,flag=0; for (currTrans = 0; currTrans < len; currTrans++){ int cmpdate = compare_dates(Arr[currTrans].date, date); if (cmpdate==1){ flag = 1; continue; } if (cmpdate==0 && flag == 1) count++; } return count; }
int main(void) { int n; struct date d1, d2; printf("This program compares the dates.\n"); printf("Enter the first date(mm/dd/yyyy): "); scanf("%d/%d/%d", &d1.month, &d1.day, &d1.year); printf("Enter the second date(mm/dd/yyyy): "); scanf("%d/%d/%d", &d2.month, &d2.day, &d2.year); if (compare_dates(d1, d2) == 1){ printf("Date 1 is later than Date 2\n"); } if (compare_dates(d1, d2) == -1){ printf("Date 1 is earlier than Date 2\n"); } if (compare_dates(d1, d2) == 0){ printf("The same\n"); } return 0; }
int child_compare( void *child1_ptr, void *child2_ptr ) { int greater ; if( *(int *)child1_ptr && *(int *)child2_ptr ) greater = (int) compare_dates( people[*(int *)child1_ptr].birth_date, people[*(int *)child2_ptr].birth_date ) ; else if( *(int *)child1_ptr ) greater = -1 ; else if( *(int *)child2_ptr ) greater = 1 ; else greater = 0 ; return greater ; }
int main(void) { struct date test1 = {1, 1, 2000}; struct date test2 = {1, 1, 2010}; struct date *t1 = &test1; struct date *t2 = &test2; printf("test1 is %d \n", day_of_year(t1)); printf("test1 is %d \n", day_of_year(t2)); if (compare_dates(t1, t2) < 0) { printf("t1 is earlier \n"); } else if (compare_dates(t1, t2) == 0) { printf("equal \n"); } else { printf("t2 is earlier \n"); } return 0; }
// read and verify whether the csr's signature is valid. Returns 1 if it is, otherwise 0. unsigned char read_csr(unsigned int *id, char *cname, char time[TIME_BUFFER_SIZE], unsigned char auth_key[SMQV_PKEY_SIZE], unsigned char token_key[MSS_PKEY_SIZE], unsigned char csr_signature[MSS_SIGNATURE_SIZE], char csr[CSR_MAX_SIZE]) { unsigned int index = 0; int csr_size = CSR_MAX_SIZE; unsigned char buffer[CSR_MAX_SIZE]; memset(buffer, 0, CSR_MAX_SIZE); char t_now[TIME_BUFFER_SIZE]; now(&t_now); base64decode(csr, strlen(csr), buffer, &csr_size); csr_split_info(buffer, id, cname, time, auth_key, token_key, csr_signature); unsigned char digest[2 * MSS_SEC_LVL]; index = csr_append_info(buffer, *id, cname, time, auth_key, token_key); sponge_hash(buffer, index, digest, 2 * MSS_SEC_LVL); // verify [(id || cname || time || auth_key || token_key), csr_signature, token_key] return mss_verify(csr_signature, token_key, digest) && compare_dates(time, t_now) != -1; }