std::string SHA512::hexdigest() { context tmp = ctx; uint64_t size = stack.size(); std::string last = stack + "\x80" + std::string((((size & 127) > 111)?239:111) - (size & 127), 0) + unhexlify(makehex((clen+size) << 3, 32)); calc(last, tmp); return makehex(tmp.h0, 16) + makehex(tmp.h1, 16) + makehex(tmp.h2, 16) + makehex(tmp.h3, 16) + makehex(tmp.h4, 16) + makehex(tmp.h5, 16) + makehex(tmp.h6, 16) + makehex(tmp.h7, 16); }
// returns formatted length string // partial takes precedence over octets std::string write_old_length(const uint8_t tag, const std::string & data, const Packet::PartialBodyLength part, uint8_t octets) { std::string::size_type length = data.size(); std::string out(1, 0x80 | (tag << 2)); // old header: 10TT TTLL if (part == Packet::PARTIAL) { // partial out[0] |= 3; } else{ // try to use user requested octet length if (octets == 1) { if (length > 255) { octets = 0; } } else if (octets == 2) { if (length > 65535) { octets = 0; } } else if ((octets == 3) || (octets == 4) || (octets > 5)) { octets = 0; } // 1 octet if ((octets == 1) || // user requested ((octets == 0) && // default (length < 256))) { out[0] |= 0; out += std::string(1, length); } // 2 octest else if ((octets == 2) || // user requested ((octets == 0) && // default (256 <= length) && (length < 65536))) { out[0] |= 1; out += unhexlify(makehex(length, 4)); } // 5 octets else if ((octets == 5) || // use requested ((octets == 0) && // default (65536 <= length))) { out[0] |= 2; out += unhexlify(makehex(length, 8)); } } return out + data; }
TEST(Tag2Sub3, read_write) { const std::string raw = unhexlify(makehex(dt, 8)); OpenPGP::Subpacket::Tag2::Sub3 sub3(raw); TAG2_SUB3_EQ(sub3); EXPECT_EQ(sub3.raw(), raw); }
std::string overkey(const Key::Ptr & key){ std::string key_str = key -> raw(); // remove private data by copying over to Tag 6 Tag6 tag6(key_str); key_str = tag6.raw(); return "\x99" + unhexlify(makehex(key_str.size(), 4)) + key_str; }
std::string certification(uint8_t version, const ID::Ptr & id){ if (version == 3){ return id -> raw(); } else if (version == 4){ std::string data = id -> raw(); if (id -> get_tag() == 13){ // User ID packet return "\xb4" + unhexlify(makehex(data.size(), 8)) + data; } else if (id -> get_tag() == 17){// User Attribute packet return "\xd1" + unhexlify(makehex(data.size(), 8)) + data; } } else{ std::stringstream s; s << static_cast <unsigned int> (version); throw std::runtime_error("Error: Certification for version " + s.str() + " not defined."); } return ""; // should never reach here; mainly just to remove compiler warnings }
TEST(Tag6, read_write) { const std::string raw = std::string(1, version) + unhexlify(makehex(timestamp, 8)) + std::string(1, pka) + OpenPGP::write_MPI(mpi[0]) + OpenPGP::write_MPI(mpi[1]); OpenPGP::Packet::Tag6 tag6(raw); TAG6_EQ(tag6); EXPECT_EQ(tag6.raw(), raw); }
std::string CAST256::run(const std::string & DATA){ if (!keyset){ throw std::runtime_error("Error: Key has not been set."); } if (DATA.size() != 16){ throw std::runtime_error("Error: Data must be 128 bits in length."); } A = toint(DATA.substr(0, 4), 256); B = toint(DATA.substr(4, 4), 256); C = toint(DATA.substr(8, 4), 256); D = toint(DATA.substr(12, 4), 256); for(uint8_t i = 0; i < 6; i++){ Q(i); } for(uint8_t i = 6; i < 12; i++){ QBAR(i); } return unhexlify(makehex(A, 8) + makehex(B, 8) + makehex(C, 8) + makehex(D, 8)); }
TEST(Tag7, read_write) { const std::string raw = std::string(1, version) + unhexlify(makehex(timestamp, 8)) + std::string(1, pka) + OpenPGP::write_MPI(mpi[0]) + OpenPGP::write_MPI(mpi[1]) + std::string(1, s2k_con) + secret; OpenPGP::Packet::Tag7 tag7(raw); TAG7_EQ(tag7); EXPECT_EQ(tag7.raw(), raw); }
std::string addtrailer(const std::string & data, const Tag2::Ptr & sig){ std::string trailer = sig -> get_up_to_hashed(); if (sig -> get_version() == 3){ return data + trailer.substr(1, trailer.size() - 1); // remove version from trailer } else if (sig -> get_version() == 4){ return data + trailer + "\x04\xff" + unhexlify(makehex(trailer.size(), 8)); } else{ std::stringstream s; s << static_cast <unsigned int> (sig -> get_version()); throw std::runtime_error("Error: addtrailer for version " + s.str() + " not defined."); } return ""; // should never reach here; mainly just to remove compiler warnings }
std::string IDEA::run(const std::string & DATA){ if (!keyset){ throw std::runtime_error("Error: Key has not been set"); } if (DATA.size() != 8){ throw std::runtime_error("Error: Data must be 64 bits in length."); } uint16_t x1 = toint(DATA.substr(0, 2), 256); uint16_t x2 = toint(DATA.substr(2, 2), 256); uint16_t x3 = toint(DATA.substr(4, 2), 256); uint16_t x4 = toint(DATA.substr(6, 2), 256); for(uint8_t x = 0; x < 8; x++){ uint16_t t1 = mult(x1, keys[x][0]); uint16_t t2 = static_cast <uint16_t> (x2 + keys[x][1]); uint16_t t3 = static_cast <uint16_t> (x3 + keys[x][2]); uint16_t t4 = mult(x4, keys[x][3]); uint16_t t5 = t1 ^ t3; uint16_t t6 = t2 ^ t4; uint16_t t7 = mult(t5, keys[x][4]); uint16_t t8 = static_cast <uint16_t> (t6 + t7); uint16_t t9 = mult(t8, keys[x][5]); uint16_t t10 = static_cast <uint16_t> (t7 + t9); x1 = t1 ^ t9; x2 = t3 ^ t9; x3 = t2 ^ t10; x4 = t4 ^ t10; } std::swap(x2, x3); x1 = mult(x1, keys[8][0]); x2 = static_cast <uint16_t> (x2 + keys[8][1]); x3 = static_cast <uint16_t> (x3 + keys[8][2]); x4 = mult(x4, keys[8][3]); return unhexlify(makehex(x1, 4) + makehex(x2, 4) + makehex(x3, 4) + makehex(x4, 4)); }
int main(int argc, char *argv[]) { int i, fd; if (argc < 2) { fprintf(stderr,"usage: %s rom-file hex-file\n",argv[0]); exit(2); } if ((fd = open(argv[1], O_RDWR)) < 0) { perror("unable to open file"); return 1; } bzero(rom, ROMSIZE); if (read(fd, rom, ROMSIZE) < 0) { perror("read error"); return 1; } /* store size in ROM header */ rom[2] = ROMSIZE / 512; /* store size in PCI ROM header */ rom[0x18 + 0x10 + 4] = ROMSIZE / 512; rom[5] = 0; for (i=0,sum=0; i<ROMSIZE; i++) sum += rom[i]; rom[5] = -sum; for (i=0,sum=0; i<ROMSIZE; i++) sum += rom[i]; if (sum & 0x00FF) printf("checksum fails.\n"); close(fd); makehex(argv[2]); return 0; }
void IDEA::setkey(const std::string & KEY){ if (keyset){ throw std::runtime_error("Error: Key has already been set."); } if (KEY.size() != 16){ throw std::runtime_error("Error: Key must be 128 bits in length."); } std::string key = hexlify(KEY); std::vector <std::string> temp; for(uint8_t x = 0; x < 7; x++){ for(int y = 0; y < 8; y++){ temp.push_back(key.substr(y << 2, 4)); } for(uint8_t y = 0; y < 16; y++){ key += makebin(toint(key.substr(y << 1 , 2), 16), 8); } key = key.substr(32, 128); key = key.substr(25, 103) + key.substr(0, 25); for(uint8_t y = 0; y < 16; y++){ key += makehex(toint(key.substr(y << 3, 8), 2), 2); } key = key.substr(128, 32); } temp.erase(temp.begin() + 52, temp.end()); for(uint8_t x = 0; x < temp.size(); x++){ k.push_back(toint(temp[x], 16)); } keyset = true; }
std::string Sub3::raw() const { return unhexlify(makehex(dt, 8)); }
std::string DES::run(const std::string & DATA){ if (!keyset){ throw std::runtime_error("Error: Key has not been set."); } if (DATA.size() != 8){ throw std::runtime_error("Error: Data must be 64 bits in length."); } std::string data = "", temp = ""; for(uint8_t x = 0; x < 8; x++){ data += makebin(static_cast <uint8_t> (DATA[x]), 8); } // IP for(uint8_t x = 0; x < 64; x++){ temp += data[DES_IP[x] - 1]; } data = temp; for(uint8_t x = 0; x < 16; x++){ // split left and right and duplicate right std::string left = data.substr(0, 32); std::string right = data.substr(32, 32); std::string old_right = right; // expand right side uint64_t t = 0; temp = ""; for(uint8_t y = 0; y < 48; y++){ temp += right[DES_EX[y] - 1]; } t = toint(temp, 2); // expanded_right xor key right = makebin(t ^ keys[x], 48); // split right into 8 parts std::string RIGHT[8]; for(uint8_t y = 0; y < 8; y++){ RIGHT[y] = right.substr(6 * y, 6); } // use sboxes temp = ""; for(uint8_t y = 0; y < 8; y++){ std::string s = ""; s += RIGHT[y][0]; s += RIGHT[y][5]; temp += makebin(DES_S_BOX[y][toint(s, 2)][toint(RIGHT[y].substr(1, 4), 2)], 4); } // permutate right = ""; for(uint8_t y = 0; y < 32; y++){ right += temp[DES_P[y]-1]; } // right xor left and combine with old right data = old_right + makebin(toint(left, 2) ^ toint(right, 2), 32); } // reverse last switch data = data.substr(32, 32) + data.substr(0, 32); // IP^-1 uint64_t out = 0; for(uint8_t x = 0; x < 64; x++){ out += static_cast <uint64_t> (data[DES_INVIP[x] - 1] == '1') << (63 - x); } return unhexlify(makehex(out, 16)); }
std::string to_sign_50(const Tag2 & sig, const Tag2::Ptr & /*tag2*/){ std::string data = sig.get_without_unhashed(); return "\x88" + unhexlify(makehex(data.size(), 8)) + data; }
} // 1 octet if ((octets == 1) || // user requested ((octets == 0) && // default (length <= 191))) { out += std::string(1, length); } // 2 octets else if ((octets == 2) || // user requested ((octets == 0) && // default ((192 <= length) && (length <= 8383)))) { length -= 0xc0; out += std::string(1, (length >> 8) + 0xc0) + std::string(1, length & 0xff); } // 5 octets else if ((octets == 5) || // user requested ((octets == 0) && // default (length > 8383))) { out += std::string(1, '\xff') + unhexlify(makehex(length, 8)); } out += data; } return out; } }
static void getprimarytoken(void) { static int wildcard = 0; int count = 0, ch; while(isspace(ch=getnext())); tokenindent = current->columnnr; if(tokenindent < tokenoffside) { ungetnext(ch); tokentype = offside; tokenval[count++] = '\0'; return; } if(isdigit(ch)) { tokentype = NUMBER; while(isdigit(ch)) { tokenval[count++] = ch; ch = getnext(); } if(ch == '.') { ch = getnext(); if(isdigit(ch)) tokenval[count++] = '.'; else { ungetnext(ch); ch = '.'; } while(isdigit(ch)) { tokenval[count++] = ch; ch = getnext(); } } tokenval[count++] = '\0'; ungetnext(ch); return; } if(isalpha(ch) || ch == '_') { if('A' <= ch && ch <= 'Z') tokentype = TYPEID; else tokentype = IDENTIFIER; while(isalpha(ch) || isdigit(ch) || ch == '_') { tokenval[count++] = ch; ch = getnext(); } tokenval[count++] = '\0'; ungetnext(ch); if(strcmp(tokenval, "where") == 0) tokentype = WHERE; if(strcmp(tokenval, "otherwise") == 0) tokentype = OTHERWISE; if(strcmp(tokenval, "abstype") == 0) tokentype = ABSTYPE; if(strcmp(tokenval, "with") == 0) tokentype = WITH; if(strcmp(tokenval, "generic") == 0) tokentype = GENERIC; if(strcmp(tokenval, "True") == 0) tokentype = IDENTIFIER; if(strcmp(tokenval, "False") == 0) tokentype = IDENTIFIER; if(strcmp(tokenval, "Nil") == 0) tokentype = IDENTIFIER; if(strcmp(tokenval, "_") == 0) sprintf(tokenval, "_??_%d", wildcard++); return; } if(ch == '$') { ch = getnext(); if(('a' <= ch && ch <= 'z') || ch == '_') { tokentype = OPERATOR; while(isalpha(ch) || isdigit(ch) || ch == '_') { tokenval[count++] = ch; ch = getnext(); } tokenval[count++] = '\0'; ungetnext(ch); } else parseerror(25); return; } if(ch == '\'') { tokentype = CHARACTER; ch = getnext(); if(!isprint(ch)) parseerror(26); if(ch == '\\') { ch = getnext(); switch(ch) { case 'a': ch = '\a'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case 'v': ch = '\v'; break; case 'e': ch = 27; break; case 'x': ch = 16 * makehex(getnext()) + makehex(getnext()); break; } } tokenval[count++] = ch; ch = getnext(); if(ch != '\'') parseerror(27); tokenval[count++] = '\0'; return; } if(ch == '\"') { tokentype = STRING; ch = getnext(); while(isprint(ch) && ch != '\"') { if(ch == '\\') { ch = getnext(); switch(ch) { case 'a': ch = '\a'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case 'v': ch = '\v'; break; case 'e': ch = 27; break; case 'x': ch = 16 * makehex(getnext()) + makehex(getnext()); break; } } tokenval[count++] = ch; ch = getnext(); } if(ch != '\"') parseerror(28); tokenval[count++] = '\0'; return; } if(ch == '(') { tokentype = LPAR; tokenval[count++] = ch; tokenval[count++] = '\0'; return; } if(ch == ')') { tokentype = RPAR; tokenval[count++] = ch; tokenval[count++] = '\0'; return; } if(ch == ';') { tokentype = SEP; tokenval[count++] = ch; tokenval[count++] = '\0'; return; } if(ch == ',') { tokentype = COMMA; tokenval[count++] = ch; tokenval[count++] = '\0'; return; } if(ch == '[') { tokentype = LBRACK; tokenval[count++] = ch; tokenval[count++] = '\0'; return; } if(ch == ']') { tokentype = RBRACK; tokenval[count++] = ch; tokenval[count++] = '\0'; return; } if(ch == '{') { tokentype = LACC; tokenval[count++] = ch; tokenval[count++] = '\0'; return; } if(ch == '}') { tokentype = RACC; tokenval[count++] = ch; tokenval[count++] = '\0'; return; } if(ch == '|') { tokentype = BAR; tokenval[count++] = ch; ch = getnext(); if(ch == '|') { while(ch != '\n') { ch = getnext(); if(ch == EOF) { tokentype = empty; tokenval[count++] = '\0'; return; } } gettoken(); return; } tokenval[count++] = '\0'; ungetnext(ch); return; } if(ch == '.') { tokentype = OPERATOR; tokenval[count++] = ch; ch = getnext(); if(ch == '.') { tokentype = POINTS; tokenval[count++] = ch; ch = getnext(); } tokenval[count++] = '\0'; ungetnext(ch); return; } if(ch == '/') { ch = getnext(); if(ch == '*') { int comments = 1; while(comments > 0) { ch = getnext(); if(ch == EOF) parseerror(29); else if(ch == '/') { ch = getnext(); if(ch == '*') comments++; else ungetnext(ch); } else if(ch == '*') { ch = getnext(); if(ch == '/') comments--; else ungetnext(ch); } } gettoken(); return; } tokentype = OPERATOR; tokenval[count++] = '/'; while(ispunct(ch) && ch!='(' && ch!=')' && ch!='[' && ch!=']' && ch!='{' && ch!='}' && ch!='|' && ch!=';' && ch!=',' && ch!='\'' && ch!='\"' && ch!='.' && ch!='_') { tokenval[count++] = ch; ch = getnext(); } tokenval[count++] = '\0'; ungetnext(ch); return; } if(ispunct(ch)) { tokentype = OPERATOR; while(ispunct(ch) && ch!='(' && ch!=')' && ch!='[' && ch!=']' && ch!='{' && ch!='}' && ch!='|' && ch!=';' && ch!=',' && ch!='\'' && ch!='\"' && ch!='.' && ch!='_') { tokenval[count++] = ch; ch = getnext(); } tokenval[count++] = '\0'; ungetnext(ch); if (strcmp(tokenval, "<-") == 0) tokentype = GENER; else if(strcmp(tokenval, ":=") == 0) tokentype = ASSIGNMENT; else if(strcmp(tokenval, "->") == 0) tokentype = ARROW; else if(strcmp(tokenval, "::") == 0) tokentype = COLONS; else if(strcmp(tokenval, "::=") == 0) tokentype = DEF; else if(strcmp(tokenval, "==") == 0) tokentype = SYN; else if(strcmp(tokenval, "~") == 0) tokentype = IDENTIFIER; else if(strcmp(tokenval, "#") == 0) tokentype = IDENTIFIER; return; } tokentype = empty; tokenval[count++] = '\0'; ungetnext(ch); }
std::string Sub20::raw() const { return flags + unhexlify(makehex(m.size(), 4)) + unhexlify(makehex(n.size(), 4)) + m + n; }