void expand_macros_in_tokenlist(halfword p) { int old_mode; pointer q= get_avail(); pointer r= get_avail(); token_info(q)= right_brace_token+'}'; token_link(q)= r; token_info(r)= end_write_token; begin_token_list(q,inserted); begin_token_list(write_tokens(p),write_text); q= get_avail(); token_info(q)= left_brace_token+'{'; begin_token_list(q,inserted); old_mode= mode; mode= 0; cur_cs= write_loc; q= scan_toks(false,true); get_token(); if(cur_tok!=end_write_token){ const char*hlp[]= { "On this page there's a \\write with fewer real {'s than }'s.", "I can't handle that very well; good luck.",NULL }; tex_error("Unbalanced write command",hlp); do{ get_token(); }while(cur_tok!=end_write_token); } mode= old_mode; end_token_list(); }
void dump_avail(char * (*get_avail(unsigned *))) { unsigned n_avail, i; char *ptr, ** avail = get_avail(&n_avail); int eventcode = PAPI_NULL, retval; PAPI_event_info_t info; for(i=0;i<n_avail;i++){ if(get_avail == get_native_avail_papi_counters || get_avail == get_preset_avail_papi_counters){ PAPI_event_name_to_code(avail[i],&eventcode); PAPI_get_event_info(eventcode, &info); printf("\t%s%*s, %s\n",info.symbol, (int)(40-strlen(info.symbol))," ", info.long_descr); if(get_avail == get_native_avail_papi_counters && PAPI_enum_event(&eventcode, PAPI_NTV_ENUM_UMASKS) == PAPI_OK){ printf("\t\t%s%*s, %s\n","Masks", (int)(20-strlen("Masks"))," ", "Mask description"); do{ retval = PAPI_get_event_info(eventcode, &info); if (retval == PAPI_OK){ if(parse_unit_masks(&info)){ printf("\t\t%s%*s, %s\n",info.symbol, (int)(20-strlen(info.symbol))," ",info.long_descr); } } } while ( PAPI_enum_event( &eventcode, PAPI_NTV_ENUM_UMASKS) == PAPI_OK ); } } else printf("%s\n",avail[i]); free(avail[i]); } free(avail); }
bool rsa_ssh1_decrypt_pkcs1(mp_int *input, RSAKey *key, strbuf *outbuf) { strbuf *data = strbuf_new_nm(); bool success = false; BinarySource src[1]; { mp_int *b = rsa_ssh1_decrypt(input, key); for (size_t i = (mp_get_nbits(key->modulus) + 7) / 8; i-- > 0 ;) { put_byte(data, mp_get_byte(b, i)); } mp_free(b); } BinarySource_BARE_INIT(src, data->u, data->len); /* Check PKCS#1 formatting prefix */ if (get_byte(src) != 0) goto out; if (get_byte(src) != 2) goto out; while (1) { unsigned char byte = get_byte(src); if (get_err(src)) goto out; if (byte == 0) break; } /* Everything else is the payload */ success = true; put_data(outbuf, get_ptr(src), get_avail(src)); out: strbuf_free(data); return success; }
struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin, struct sftp_request *req) { sfree(req); if (pktin->type == SSH_FXP_NAME) { struct fxp_names *ret; unsigned long i; i = get_uint32(pktin); /* * Sanity-check the number of names. Minimum is obviously * zero. Maximum is the remaining space in the packet * divided by the very minimum length of a name, which is * 12 bytes (4 for an empty filename, 4 for an empty * longname, 4 for a set of attribute flags indicating that * no other attributes are supplied). */ if (get_err(pktin) || i > get_avail(pktin) / 12) { fxp_internal_error("malformed FXP_NAME packet"); sftp_pkt_free(pktin); return NULL; } /* * Ensure the implicit multiplication in the snewn() call * doesn't suffer integer overflow and cause us to malloc * too little space. */ if (i > INT_MAX / sizeof(struct fxp_name)) { fxp_internal_error("unreasonably large FXP_NAME packet"); sftp_pkt_free(pktin); return NULL; } ret = snew(struct fxp_names); ret->nnames = i; ret->names = snewn(ret->nnames, struct fxp_name); for (i = 0; i < (unsigned long)ret->nnames; i++) { ret->names[i].filename = mkstr(get_string(pktin)); ret->names[i].longname = mkstr(get_string(pktin)); get_fxp_attrs(pktin, &ret->names[i].attrs); } if (get_err(pktin)) { fxp_internal_error("malformed FXP_NAME packet"); for (i = 0; i < (unsigned long)ret->nnames; i++) { sfree(ret->names[i].filename); sfree(ret->names[i].longname); } sfree(ret->names); sfree(ret); sfree(pktin); return NULL; } sftp_pkt_free(pktin); return ret; } else {
static WeierstrassPoint *ecdsa_decode( ptrlen encoded, const struct ec_curve *curve) { assert(curve->type == EC_WEIERSTRASS); BinarySource src[1]; BinarySource_BARE_INIT_PL(src, encoded); unsigned char format_type = get_byte(src); WeierstrassPoint *P; size_t len = get_avail(src); mp_int *x; mp_int *y; switch (format_type) { case 0: /* The identity. */ P = ecc_weierstrass_point_new_identity(curve->w.wc); break; case 2: case 3: /* A compressed point, in which the x-coordinate is stored in * full, and y is deduced from that and a single bit * indicating its parity (stored in the format type byte). */ x = mp_from_bytes_be(get_data(src, len)); P = ecc_weierstrass_point_new_from_x(curve->w.wc, x, format_type & 1); mp_free(x); if (!P) /* this can fail if the input is invalid */ return NULL; break; case 4: /* An uncompressed point: the x,y coordinates are stored in * full. We expect the rest of the string to have even length, * and be divided half and half between the two values. */ if (len % 2 != 0) return NULL; len /= 2; x = mp_from_bytes_be(get_data(src, len)); y = mp_from_bytes_be(get_data(src, len)); P = ecc_weierstrass_point_new(curve->w.wc, x, y); mp_free(x); mp_free(y); break; default: /* An unrecognised type byte. */ return NULL; } /* Verify the point is on the curve */ if (!ecc_weierstrass_point_valid(P)) { ecc_weierstrass_point_free(P); return NULL; } return P; }
cpus_t *read_affinity(void) { int sz = get_avail() ; cpus_t *r ; r = cpus_create(sz) ; for (int p=0, *q=r->cpu ; p < sz ; p++) { *q++ = p ; } return r ; }
int scan_tex_toks_register(int j,int c,lstring s) { int a; halfword ref= get_avail(); (void)str_scan_toks(c,s); set_token_ref_count(ref,0); set_token_link(ref,token_link(temp_token_head)); if(global_defs_par> 0) a= 4; else a= 0; define(j+toks_base,call_cmd,ref); return 0; }
static bool eddsa_verify(ssh_key *key, ptrlen sig, ptrlen data) { struct eddsa_key *ek = container_of(key, struct eddsa_key, sshk); const struct ecsign_extra *extra = (const struct ecsign_extra *)ek->sshk.vt->extra; BinarySource src[1]; BinarySource_BARE_INIT_PL(src, sig); /* Check the signature starts with the algorithm name */ if (!ptrlen_eq_string(get_string(src), ek->sshk.vt->ssh_id)) return false; /* Now expect a single string which is the concatenation of an * encoded curve point r and an integer s. */ ptrlen sigstr = get_string(src); if (get_err(src)) return false; BinarySource_BARE_INIT_PL(src, sigstr); ptrlen rstr = get_data(src, ek->curve->fieldBytes); ptrlen sstr = get_data(src, ek->curve->fieldBytes); if (get_err(src) || get_avail(src)) return false; EdwardsPoint *r = eddsa_decode(rstr, ek->curve); if (!r) return false; mp_int *s = mp_from_bytes_le(sstr); mp_int *H = eddsa_signing_exponent_from_data(ek, extra, rstr, data); /* Verify that s*G == r + H*publicKey */ EdwardsPoint *lhs = ecc_edwards_multiply(ek->curve->e.G, s); mp_free(s); EdwardsPoint *hpk = ecc_edwards_multiply(ek->publicKey, H); mp_free(H); EdwardsPoint *rhs = ecc_edwards_add(r, hpk); ecc_edwards_point_free(hpk); unsigned valid = ecc_edwards_eq(lhs, rhs); ecc_edwards_point_free(lhs); ecc_edwards_point_free(rhs); ecc_edwards_point_free(r); return valid; }
static ssh_key *eddsa_new_priv_openssh( const ssh_keyalg *alg, BinarySource *src) { const struct ecsign_extra *extra = (const struct ecsign_extra *)alg->extra; struct ec_curve *curve = extra->curve(); assert(curve->type == EC_EDWARDS); ptrlen pubkey_pl = get_string(src); ptrlen privkey_extended_pl = get_string(src); if (get_err(src) || pubkey_pl.len != curve->fieldBytes) return NULL; /* * The OpenSSH format for ed25519 private keys also for some * reason encodes an extra copy of the public key in the second * half of the secret-key string. Check that that's present and * correct as well, otherwise the key we think we've imported * won't behave identically to the way OpenSSH would have treated * it. */ BinarySource subsrc[1]; BinarySource_BARE_INIT_PL(subsrc, privkey_extended_pl); ptrlen privkey_pl = get_data(subsrc, curve->fieldBytes); ptrlen pubkey_copy_pl = get_data(subsrc, curve->fieldBytes); if (get_err(subsrc) || get_avail(subsrc)) return NULL; if (!ptrlen_eq_ptrlen(pubkey_pl, pubkey_copy_pl)) return NULL; struct eddsa_key *ek = snew(struct eddsa_key); ek->sshk.vt = alg; ek->curve = curve; ek->privateKey = NULL; ek->publicKey = eddsa_decode(pubkey_pl, curve); if (!ek->publicKey) { eddsa_freekey(&ek->sshk); return NULL; } ek->privateKey = mp_from_bytes_le(privkey_pl); return &ek->sshk; }
int tokenlist_from_lua(lua_State * L) { const char *s; int tok, t; size_t i, j; halfword p, q, r; r = get_avail(); token_info(r) = 0; token_link(r) = null; p = r; t = lua_type(L, -1); if (t == LUA_TTABLE) { j = lua_rawlen(L, -1); if (j > 0) { for (i = 1; i <= j; i++) { lua_rawgeti(L, -1, (int) i); tok = token_from_lua(L); if (tok >= 0) { store_new_token(tok); } lua_pop(L, 1); }; } return r; } else if (t == LUA_TSTRING) { s = lua_tolstring(L, -1, &j); for (i = 0; i < j; i++) { if (s[i] == 32) { tok = token_val(10, s[i]); } else { int j1 = (int) str2uni((const unsigned char *) (s + i)); i = i + (size_t) (utf8_size(j1) - 1); tok = token_val(12, j1); } store_new_token(tok); } return r; } else { free_avail(r); return null; } }
mp_int *ssh_rsakex_decrypt( RSAKey *rsa, const ssh_hashalg *h, ptrlen ciphertext) { mp_int *b1, *b2; int outlen, i; unsigned char *out; unsigned char labelhash[64]; ssh_hash *hash; BinarySource src[1]; const int HLEN = h->hlen; /* * Decryption side of the RSA key exchange operation. */ /* The length of the encrypted data should be exactly the length * in octets of the RSA modulus.. */ outlen = (7 + mp_get_nbits(rsa->modulus)) / 8; if (ciphertext.len != outlen) return NULL; /* Do the RSA decryption, and extract the result into a byte array. */ b1 = mp_from_bytes_be(ciphertext); b2 = rsa_privkey_op(b1, rsa); out = snewn(outlen, unsigned char); for (i = 0; i < outlen; i++) out[i] = mp_get_byte(b2, outlen-1-i); mp_free(b1); mp_free(b2); /* Do the OAEP masking operations, in the reverse order from encryption */ oaep_mask(h, out+HLEN+1, outlen-HLEN-1, out+1, HLEN); oaep_mask(h, out+1, HLEN, out+HLEN+1, outlen-HLEN-1); /* Check the leading byte is zero. */ if (out[0] != 0) { sfree(out); return NULL; } /* Check the label hash at position 1+HLEN */ assert(HLEN <= lenof(labelhash)); hash = ssh_hash_new(h); ssh_hash_final(hash, labelhash); if (memcmp(out + HLEN + 1, labelhash, HLEN)) { sfree(out); return NULL; } /* Expect zero bytes followed by a 1 byte */ for (i = 1 + 2 * HLEN; i < outlen; i++) { if (out[i] == 1) { i++; /* skip over the 1 byte */ break; } else if (out[i] != 1) { sfree(out); return NULL; } } /* And what's left is the input message data, which should be * encoded as an ordinary SSH-2 mpint. */ BinarySource_BARE_INIT(src, out + i, outlen - i); b1 = get_mp_ssh2(src); sfree(out); if (get_err(src) || get_avail(src) != 0) { mp_free(b1); return NULL; } /* Success! */ return b1; }