SLVAL sl_string_url_decode(sl_vm_t* vm, SLVAL self) { sl_string_t* str = sl_get_string(vm, self); size_t out_cap = 32; size_t out_len = 0; uint8_t* out = sl_alloc_buffer(vm->arena, out_cap); size_t str_i; char tmp[3]; for(str_i = 0; str_i < str->buff_len; str_i++) { if(out_len + 8 >= out_cap) { out_cap *= 2; out = sl_realloc(vm->arena, out, out_cap); } if(str->buff[str_i] == '%') { if(str_i + 2 < str->buff_len) { if(is_hex_char(str->buff[str_i + 1]) && is_hex_char(str->buff[str_i + 2])) { tmp[0] = str->buff[str_i + 1]; tmp[1] = str->buff[str_i + 2]; tmp[2] = 0; out[out_len++] = strtol(tmp, NULL, 16); str_i += 2; continue; } } } if(str->buff[str_i] == '+') { out[out_len++] = ' '; continue; } out[out_len++] = str->buff[str_i]; } return sl_make_string(vm, out, out_len); }
int str_to_hex(uint8_t *str, uint8_t *hex, int swap) { int i, count = 0, actual_len; uint8_t val8; while (*str) { if (!is_hex_char(*str)) { //sysprintf("ERROR - not hex!!\n"); return count; } val8 = char_to_hex(*str); str++; if (!is_hex_char(*str)) { //sysprintf("ERROR - not hex!!\n"); return count; } val8 = (val8 << 4) | char_to_hex(*str); str++; hex[count] = val8; //sysprintf("hex = 0x%x\n", val8); count++; } actual_len = count; for ( ; count % 4 ; count++) hex[count] = 0; if (!swap) return actual_len; // SWAP for (i = 0; i < count; i+=4) { val8 = hex[i]; hex[i] = hex[i+3]; hex[i+3] = val8; val8 = hex[i+1]; hex[i+1] = hex[i+2]; hex[i+2] = val8; } return actual_len; }
int get_next_pattern(void) { int line_num = 1; uint8_t *p; _au8ShaData = (uint8_t *)((uint32_t)_au8ShaData_pool | 0x80000000); while (get_line() == 0) { //sysprintf("LINE %d = %s\n", line_num, _pi8LineBuff); line_num++; if (_pi8LineBuff[0] == '#') continue; if (strncmp(_pi8LineBuff ,"Len", 3) == 0) { p = (uint8_t *)&_pi8LineBuff[3]; while ((*p < '0') || (*p > '9')) p++; _i32DataLen = str_to_decimal(p); continue; } if (strncmp(_pi8LineBuff ,"Msg", 3) == 0) { p = (uint8_t *)&_pi8LineBuff[3]; while (!is_hex_char(*p)) p++; str_to_hex(p, &_au8ShaData[0], 0); continue; } if (strncmp(_pi8LineBuff ,"MD", 2) == 0) { p = (uint8_t *)&_pi8LineBuff[2]; while (!is_hex_char(*p)) p++; str_to_hex(p, &_au8ShaDigest[0], 1); return 0; } } return -1; }
int percent_decode(const char *in_buff, int in_len, char* out_buff, int out_len) { int in_pos, out_pos; in_pos = out_pos = 0; while(in_pos < in_len) { if(out_pos >= out_len) return -1; char c = in_buff[in_pos++]; if(c == '%') { if(!(in_pos + 1 < in_len)) return -1; char h_hi = tolower(in_buff[in_pos++]); char h_lo = tolower(in_buff[in_pos++]); if(!is_hex_char(h_hi) || !is_hex_char(h_lo)) return -1; out_buff[out_pos++] = hex_decode(h_hi, h_lo); } else { out_buff[out_pos++] = c; } } return out_pos; }
static jserr_t js_parse_string(jsparser_t *p, size_t t) { size_t start = p->pos, j; js_ensure_buf(p, 2); if (js(p)[0] != '"') return JS_EPARSE; p->pos++; for (; js(p)[0] != '\"'; p->pos++) { js_ensure_buf(p, 6); if (0 <= js(p)[0] && js(p)[0] < 32) return JS_EPARSE; if (js(p)[0] == '\\') { p->pos++; switch(js(p)[0]) { case '\"': case '\\': case '/': case 'b': case 'f': case 'n': case 'r': case 't': continue; case 'u': for (j = 1; j < 5; j++) if (!is_hex_char(js(p)[j])) return JS_EPARSE; p->pos += 4; break; default: return JS_EPARSE; } } } if (js(p)[0] != '\"') return JS_EPARSE; p->pos++; js_tok(p, t)->type = JS_STRING; js_tok(p, t)->start = start + 1; js_tok(p, t)->end = p->pos - 1; return 0; }
// between pointer, check string is number - need to be changed more functions static char is_digit_string(char *f, char *t) { if(f == t) { if(is_digit_char(*f)) return 1; else return 0; } int is_hex = 0; int i = 0; // 0x, 0X while(f != t) { if(i == 1 && *(f-1) == '0' && (*f == 'x' || *f == 'X')) { is_hex = 1; } // none hex else if(!is_hex && !is_digit_char(*f)) { return 0; } // hex else if(is_hex && !is_hex_char(*f)) { return 0; } f++; i++; } // need to be added function ---------------- // 23e // 23e+1 return 1; }
static inline int lengthOfEscapeSequence(const QByteArray &s, int i) { if (s.at(i) != '\\' || i >= s.length() - 1) return 1; const int startPos = i; ++i; char ch = s.at(i); if (ch == 'x') { ++i; while (i < s.length() && is_hex_char(s.at(i))) ++i; } else if (is_octal_char(ch)) { while (i < startPos + 4 && i < s.length() && is_octal_char(s.at(i))) { ++i; } } else { // single character escape sequence i = qMin(i + 1, s.length()); } return i - startPos; }
int hex2bin(void *buf_dst, char *buf_src, int len) { int new_len = 0; uint8_t *output=buf_dst; char *src_end=buf_src+len; char *num_begin=buf_src, *num_end; while (num_begin<src_end) { while (num_begin<src_end && !is_hex_char(*num_begin)) num_begin++; if (num_begin>=src_end) break; num_end=num_begin+2; if (*num_begin=='0' && (*(num_begin+1)=='x' || *(num_begin+1)=='X')) num_end+=2; *num_end = 0; output[new_len++] =(uint8_t) strtoul(num_begin, NULL, 16); num_begin = num_end+1; } return new_len; }
static Symbols tokenize(const QByteArray &input, int lineNum = 1, TokenizeMode mode = TokenizeCpp) { Symbols symbols; const char *begin = input; const char *data = begin; while (*data) { if (mode == TokenizeCpp) { int column = 0; const char *lexem = data; int state = 0; Token token = NOTOKEN; for (;;) { if (static_cast<signed char>(*data) < 0) { ++data; continue; } int nextindex = keywords[state].next; int next = 0; if (*data == keywords[state].defchar) next = keywords[state].defnext; else if (!state || nextindex) next = keyword_trans[nextindex][(int)*data]; if (!next) break; state = next; token = keywords[state].token; ++data; } // suboptimal, is_ident_char should use a table if (keywords[state].ident && is_ident_char(*data)) token = keywords[state].ident; if (token == NOTOKEN) { // an error really ++data; continue; } ++column; if (token > SPECIAL_TREATMENT_MARK) { switch (token) { case QUOTE: data = skipQuote(data); token = STRING_LITERAL; // concatenate multi-line strings for easier // STRING_LITERAAL handling in moc if (!Preprocessor::preprocessOnly && !symbols.isEmpty() && symbols.last().token == STRING_LITERAL) { QByteArray newString = symbols.last().unquotedLexem(); newString += input.mid(lexem - begin + 1, data - lexem - 2); newString.prepend('\"'); newString.append('\"'); symbols.last() = Symbol(symbols.last().lineNum, STRING_LITERAL, newString); continue; } break; case SINGLEQUOTE: while (*data && (*data != '\'' || (*(data-1)=='\\' && *(data-2)!='\\'))) ++data; if (*data) ++data; token = CHARACTER_LITERAL; break; case LANGLE_SCOPE: // split <:: into two tokens, < and :: token = LANGLE; data -= 2; break; case DIGIT: while (is_digit_char(*data)) ++data; if (!*data || *data != '.') { token = INTEGER_LITERAL; if (data - lexem == 1 && (*data == 'x' || *data == 'X') && *lexem == '0') { ++data; while (is_hex_char(*data)) ++data; } break; } token = FLOATING_LITERAL; ++data; // fall through case FLOATING_LITERAL: while (is_digit_char(*data)) ++data; if (*data == '+' || *data == '-') ++data; if (*data == 'e' || *data == 'E') { ++data; while (is_digit_char(*data)) ++data; } if (*data == 'f' || *data == 'F' || *data == 'l' || *data == 'L') ++data; break; case HASH: if (column == 1) { mode = PreparePreprocessorStatement; while (*data && (*data == ' ' || *data == '\t')) ++data; if (is_ident_char(*data)) mode = TokenizePreprocessorStatement; continue; } break; case NEWLINE: ++lineNum; continue; case BACKSLASH: { const char *rewind = data; while (*data && (*data == ' ' || *data == '\t')) ++data; if (*data && *data == '\n') { ++data; continue; } data = rewind; } break; case CHARACTER: while (is_ident_char(*data)) ++data; token = IDENTIFIER; break; case C_COMMENT: if (*data) { if (*data == '\n') ++lineNum; ++data; if (*data) { if (*data == '\n') ++lineNum; ++data; } } while (*data && (*(data-1) != '/' || *(data-2) != '*')) { if (*data == '\n') ++lineNum; ++data; } token = WHITESPACE; // one comment, one whitespace // fall through; case WHITESPACE: if (column == 1) column = 0; while (*data && (*data == ' ' || *data == '\t')) ++data; if (Preprocessor::preprocessOnly) // tokenize whitespace break; continue; case CPP_COMMENT: while (*data && *data != '\n') ++data; continue; // ignore safely, the newline is a separator default: continue; //ignore } } #ifdef USE_LEXEM_STORE if (!Preprocessor::preprocessOnly && token != IDENTIFIER && token != STRING_LITERAL && token != FLOATING_LITERAL && token != INTEGER_LITERAL) symbols += Symbol(lineNum, token); else #endif symbols += Symbol(lineNum, token, input, lexem-begin, data-lexem); } else { // Preprocessor const char *lexem = data; int state = 0; Token token = NOTOKEN; if (mode == TokenizePreprocessorStatement) { state = pp_keyword_trans[0][(int)'#']; mode = TokenizePreprocessor; } for (;;) { if (static_cast<signed char>(*data) < 0) { ++data; continue; } int nextindex = pp_keywords[state].next; int next = 0; if (*data == pp_keywords[state].defchar) next = pp_keywords[state].defnext; else if (!state || nextindex) next = pp_keyword_trans[nextindex][(int)*data]; if (!next) break; state = next; token = pp_keywords[state].token; ++data; } // suboptimal, is_ident_char should use a table if (pp_keywords[state].ident && is_ident_char(*data)) token = pp_keywords[state].ident; switch (token) { case NOTOKEN: ++data; break; case PP_IFDEF: symbols += Symbol(lineNum, PP_IF); symbols += Symbol(lineNum, PP_DEFINED); continue; case PP_IFNDEF: symbols += Symbol(lineNum, PP_IF); symbols += Symbol(lineNum, PP_NOT); symbols += Symbol(lineNum, PP_DEFINED); continue; case PP_INCLUDE: mode = TokenizeInclude; break; case PP_QUOTE: data = skipQuote(data); token = PP_STRING_LITERAL; break; case PP_SINGLEQUOTE: while (*data && (*data != '\'' || (*(data-1)=='\\' && *(data-2)!='\\'))) ++data; if (*data) ++data; token = PP_CHARACTER_LITERAL; break; case PP_DIGIT: while (is_digit_char(*data)) ++data; if (!*data || *data != '.') { token = PP_INTEGER_LITERAL; if (data - lexem == 1 && (*data == 'x' || *data == 'X') && *lexem == '0') { ++data; while (is_hex_char(*data)) ++data; } break; } token = PP_FLOATING_LITERAL; ++data; // fall through case PP_FLOATING_LITERAL: while (is_digit_char(*data)) ++data; if (*data == '+' || *data == '-') ++data; if (*data == 'e' || *data == 'E') { ++data; while (is_digit_char(*data)) ++data; } if (*data == 'f' || *data == 'F' || *data == 'l' || *data == 'L') ++data; break; case PP_CHARACTER: if (mode == PreparePreprocessorStatement) { // rewind entire token to begin data = lexem; mode = TokenizePreprocessorStatement; continue; } while (is_ident_char(*data)) ++data; token = PP_IDENTIFIER; break; case PP_C_COMMENT: if (*data) { if (*data == '\n') ++lineNum; ++data; if (*data) { if (*data == '\n') ++lineNum; ++data; } } while (*data && (*(data-1) != '/' || *(data-2) != '*')) { if (*data == '\n') ++lineNum; ++data; } token = PP_WHITESPACE; // one comment, one whitespace // fall through; case PP_WHITESPACE: while (*data && (*data == ' ' || *data == '\t')) ++data; continue; // the preprocessor needs no whitespace case PP_CPP_COMMENT: while (*data && *data != '\n') ++data; continue; // ignore safely, the newline is a separator case PP_NEWLINE: ++lineNum; mode = TokenizeCpp; break; case PP_BACKSLASH: { const char *rewind = data; while (*data && (*data == ' ' || *data == '\t')) ++data; if (*data && *data == '\n') { ++data; continue; } data = rewind; } break; case PP_LANGLE: if (mode != TokenizeInclude) break; token = PP_STRING_LITERAL; while (*data && *data != '\n' && *(data-1) != '>') ++data; break; default: break; } if (mode == PreparePreprocessorStatement) continue; #ifdef USE_LEXEM_STORE if (token != PP_IDENTIFIER && token != PP_STRING_LITERAL && token != PP_FLOATING_LITERAL && token != PP_INTEGER_LITERAL) symbols += Symbol(lineNum, token); else #endif symbols += Symbol(lineNum, token, input, lexem-begin, data-lexem); } } symbols += Symbol(); // eof symbol return symbols; }
// Decrypts the input using AES-128 driven by tablefile in the ECB mode using key // as the decryption key (16-byte long and in hexstring format) void decrypt(char *key_chars, FILE *table, FILE *input) { unsigned char round_key[44][4] = { { 0 } }; unsigned char* init_key = (unsigned char*)calloc(1,16); unsigned char* cipher_block = (unsigned char*)calloc(1,16); unsigned char* plain_block = (unsigned char*)calloc(1,16); // Verify table if(!tablecheck(table)) { return; } // Check key length if(strlen(key_chars) != 32) { fprintf(stderr, "ERROR: key must consist of 32 characters, all hex values.\n"); return; } // Read raw key hexchars in and convert to bytes else { char temp_val[3]; for(int i=0; i<16; i++) { if(!is_hex_char(key_chars[i*2]) || !is_hex_char(key_chars[i*2+1])) { fprintf(stderr, "ERROR: all values in the polynomial must be hex values.\n"); return; } temp_val[0] = key_chars[i*2]; temp_val[1] = key_chars[i*2+1]; temp_val[2] = ' '; init_key[i] = strtol(temp_val, NULL, 16); } } // Perform key expansion and store result in round_key key_expansion(init_key, round_key, table); /* // Read first 16 bytes from input text if(fread(cipher_block, 1, 16, input) < 16) { fprintf(stderr, "ERROR: input file was less than 16 bytes long.\n"); return; }*/ // Encrypt everything from input in 16-byte block bool first = true; int size; while((size = fread(cipher_block, 1, 16, input))) { if((*cipher_block == '\n') && (size == 1)) continue; // Encrypt a single block using AES-128 decrypt_block(cipher_block, plain_block, round_key, table, first); // Print current cipher block values to output for(int i=0; i<16; i++) { printf("%c", plain_block[i]); } // Reset blocks for next iteration first = false; for(int i=0; i<16; i++) { plain_block[i] = 0x00; cipher_block[i] = 0x00; } } free(init_key); free(plain_block); free(cipher_block); // PSEUDO-CODE // - Same as AES-128 encrypt, except for a few minor changes: // - The S-box needs to be inverted before it can be used for substitution // - You use the inverse polynomial instead of the original for mix_columns // - During shift_rows, you shift each row to the right instead of the left // - The round keys are used in reverse order // - The order of core operations is also slightly different // - Inv Shift Columns // - Substitute Bytes (inverse S-box) // - Add Round Key // - Mix Columns (inverse polynomial) // EDIT: modified to decrypt entire input with AES-128. Prints state outputs to stderr on first block. }
// Perform the key expansion for AES-128 using key as 16-byte string in hexstring format void keyexpand(char* key_chars, FILE* table) { unsigned char* init_key = (unsigned char*)calloc(1,16); unsigned char round_key[Nb*(Nr+1)][4]; // Verify S values are valid if(!check_s(table)) { // S vals are invalid fprintf(stderr, "ERROR: tablecheck failed for S (Substitution) values.\n"); return; } // Check key length else if(strlen(key_chars) != 32) { fprintf(stderr, "ERROR: key must consist of 32 characters, all hex values.\n"); return; } // Read raw hexchars in and convert to bytes else { char temp_val[3]; for(int i=0; i<16; i++) { if(!is_hex_char(key_chars[i*2]) || !is_hex_char(key_chars[i*2+1])) { fprintf(stderr, "ERROR: all values in the polynomial must be hex values.\n"); return; } temp_val[0] = key_chars[i*2]; temp_val[1] = key_chars[i*2+1]; temp_val[2] = ' '; init_key[i] = strtol(temp_val, NULL, 16); } } // Perform the key expansion key_expansion(init_key, round_key, table); // Print round output for(int i=0; i<44; i++) { if(i >= 10) { printf("w[%d]: %02x%02x%02x%02x\n", i, round_key[i][0], round_key[i][1], round_key[i][2], round_key[i][3]); } else { printf("w[ %d]: %02x%02x%02x%02x\n", i, round_key[i][0], round_key[i][1], round_key[i][2], round_key[i][3]); } } free(init_key); // PSEUDO-CODE // - Initialize the S-box and Rcon // - For the S-box, read in the tablefile; incrementally assign the permutations to a 256-byte array // - For Rcon, starting with rcon[0]=0x8d, modularly mult rcon[i-1] by 0x02 until you have 256 vals // - (For AES-128: Nk=4, Nr=10, key is 16 bytes long) // - For the first 4 rounds, the round_key is one quarter of the init_key // - For the remainder of the rounds (4*(Nr+1) total): // - Every 4th round: // - Rotate the round_key circularly to the left and store in temp val // - Put the temp val through the s_box and XOR the MSB with the next val of Rcon // - For every round, XOR the current temp vals with the previous round_key, store as next round_key // - Print the output of each round_key }
token_t * read_token(lexer_state * state) { token_t * t; int type, value_size; char * left = state->ptr; char * right; char c; // Ignore spaces. while ((c = *left) && (c == ' ')) left++; right = left; if (c == ';') { while (c != '\n') c = *(++right); type = T_COMMENT; } else if (c == '\n') { right++; type = T_NEWLINE; } else if (is_name_char(c)) { while (is_name_char(c)) c = *(++right); type = T_NAME; } else if (c == ':') { c = *(++right); while (is_name_char(c)) c = *(++right); type = T_LABEL; } else if (is_digit_char(c)) { if (*(left + 1) == 'x') // look-ahead { while (is_hex_char(c)) c = *(++right); type = T_INT_HEX; } else { while (is_digit_char(c)) c = *(++right); type = T_INT_DEC; } } else if (c == '[' || c == ']') { right++; type = c == '[' ? T_BRACKET_L : T_BRACKET_R; } else if (c == ',') { right++; type = T_COMMA; } else if (c == '+') { right++; type = T_PLUS; } else if (c == 0) { return NULL; } else { printf("c: '%c' (0x%02x)\n", c, (int)c); CRASH("unhandled token"); } t = (token_t *)malloc(sizeof(token_t)); t->type = type; t->size = value_size = right - left; t->value = (char *)malloc(value_size + 1); strlcpy(t->value, left, value_size + 1); state->ptr = right; return t; }
int main(int argc, char *argv[]) { struct aes_wb_s aes; uint8_t i, round; uint8_t t[12][16]; uint8_t m[16]; for(i = 0; i < 32; i += 2) { if(is_hex_char(argv[1][i]) == 0 || is_hex_char(argv[1][i + 1]) == 0) return EXIT_FAILURE; unsigned char str_bytes[3] = { argv[1][i], argv[1][i + 1], 0 }; m[ i / 2] = strtoul((const char*)str_bytes, NULL, 16); } FILE* f; f = fopen("wbt_allenc", "rb"); fread(&aes, 1, sizeof(aes), f); fclose(f); printf("Input: "); for (i = 0; i < 16; i++) printf("%02X ", m[i]); printf("\n"); for (i = 0; i < 16; i++) t[0][(i%4u)*4 + (i/4u)] = aes.initSub.inv_sub[m[i]]; printf("Enc in: "); for (i = 0; i < 16; i++) printf("%02X ", t[0][i]); printf("\n"); for (round = 1; round < 10; round ++) { // DFA FILE *fp; fp = fopen("/dev/urandom", "r"); uint8_t r1, r2; fread(&r1, 1, 1, fp); fread(&r2, 1, 1, fp); fclose(fp); for (i = 0; i < 16; i++) { uint8_t b[4]; uint8_t j; for (j = 0; j < 4; j++) b[j] = aes.roundTables[round-1][i][j][t[round-1][j*4+((i+j)%4u)]]; //DFA on one of the 4 b that will be recombined with the xor tables if ((round==8) && ((r1&0xF)==i)) b[0] ^= (r2 & 0xFF); t[round][i] = aes.xorTables[2][(aes.xorTables[0][(b[0]<<8)|b[1]] << 8) | aes.xorTables[1][(b[2]<<8)|b[3]]]; } } for (i = 0; i < 16; i++) t[10][i/4u + (i%4u)*4] = aes.finalTable[i][t[9][(i&(~3)) +((i+i/4)%4u)]]; printf("Enc out: "); for (i = 0; i < 16; i++) printf("%02X ", t[10][i]); printf("\n"); printf("Output: "); for (i = 0; i < 16; i++) printf("%02X ", aes.finalSub.inv_sub[t[10][i]]); printf("\n"); return 0; }