1*ba1276acSMatthew Dillon /* $OpenBSD: sshbuf.h,v 1.28 2022/12/02 04:40:27 djm Exp $ */ 236e94dc5SPeter Avalos /* 336e94dc5SPeter Avalos * Copyright (c) 2011 Damien Miller 436e94dc5SPeter Avalos * 536e94dc5SPeter Avalos * Permission to use, copy, modify, and distribute this software for any 636e94dc5SPeter Avalos * purpose with or without fee is hereby granted, provided that the above 736e94dc5SPeter Avalos * copyright notice and this permission notice appear in all copies. 836e94dc5SPeter Avalos * 936e94dc5SPeter Avalos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1036e94dc5SPeter Avalos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1136e94dc5SPeter Avalos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1236e94dc5SPeter Avalos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1336e94dc5SPeter Avalos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1436e94dc5SPeter Avalos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1536e94dc5SPeter Avalos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1636e94dc5SPeter Avalos */ 1736e94dc5SPeter Avalos 1836e94dc5SPeter Avalos #ifndef _SSHBUF_H 1936e94dc5SPeter Avalos #define _SSHBUF_H 2036e94dc5SPeter Avalos 2136e94dc5SPeter Avalos #include <sys/types.h> 2236e94dc5SPeter Avalos #include <stdarg.h> 2336e94dc5SPeter Avalos #include <stdio.h> 2436e94dc5SPeter Avalos #ifdef WITH_OPENSSL 2536e94dc5SPeter Avalos # include <openssl/bn.h> 2636e94dc5SPeter Avalos # ifdef OPENSSL_HAS_ECC 2736e94dc5SPeter Avalos # include <openssl/ec.h> 2836e94dc5SPeter Avalos # endif /* OPENSSL_HAS_ECC */ 2936e94dc5SPeter Avalos #endif /* WITH_OPENSSL */ 3036e94dc5SPeter Avalos 3136e94dc5SPeter Avalos #define SSHBUF_SIZE_MAX 0x8000000 /* Hard maximum size */ 3236e94dc5SPeter Avalos #define SSHBUF_REFS_MAX 0x100000 /* Max child buffers */ 3336e94dc5SPeter Avalos #define SSHBUF_MAX_BIGNUM (16384 / 8) /* Max bignum *bytes* */ 3436e94dc5SPeter Avalos #define SSHBUF_MAX_ECPOINT ((528 * 2 / 8) + 1) /* Max EC point *bytes* */ 3536e94dc5SPeter Avalos 36*ba1276acSMatthew Dillon struct sshbuf; 3736e94dc5SPeter Avalos 3836e94dc5SPeter Avalos /* 3936e94dc5SPeter Avalos * Create a new sshbuf buffer. 4036e94dc5SPeter Avalos * Returns pointer to buffer on success, or NULL on allocation failure. 4136e94dc5SPeter Avalos */ 4236e94dc5SPeter Avalos struct sshbuf *sshbuf_new(void); 4336e94dc5SPeter Avalos 4436e94dc5SPeter Avalos /* 4536e94dc5SPeter Avalos * Create a new, read-only sshbuf buffer from existing data. 4636e94dc5SPeter Avalos * Returns pointer to buffer on success, or NULL on allocation failure. 4736e94dc5SPeter Avalos */ 4836e94dc5SPeter Avalos struct sshbuf *sshbuf_from(const void *blob, size_t len); 4936e94dc5SPeter Avalos 5036e94dc5SPeter Avalos /* 5136e94dc5SPeter Avalos * Create a new, read-only sshbuf buffer from the contents of an existing 5236e94dc5SPeter Avalos * buffer. The contents of "buf" must not change in the lifetime of the 5336e94dc5SPeter Avalos * resultant buffer. 5436e94dc5SPeter Avalos * Returns pointer to buffer on success, or NULL on allocation failure. 5536e94dc5SPeter Avalos */ 5636e94dc5SPeter Avalos struct sshbuf *sshbuf_fromb(struct sshbuf *buf); 5736e94dc5SPeter Avalos 5836e94dc5SPeter Avalos /* 5936e94dc5SPeter Avalos * Create a new, read-only sshbuf buffer from the contents of a string in 6036e94dc5SPeter Avalos * an existing buffer (the string is consumed in the process). 6136e94dc5SPeter Avalos * The contents of "buf" must not change in the lifetime of the resultant 6236e94dc5SPeter Avalos * buffer. 6336e94dc5SPeter Avalos * Returns pointer to buffer on success, or NULL on allocation failure. 6436e94dc5SPeter Avalos */ 6536e94dc5SPeter Avalos int sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp); 6636e94dc5SPeter Avalos 6736e94dc5SPeter Avalos /* 6836e94dc5SPeter Avalos * Clear and free buf 6936e94dc5SPeter Avalos */ 7036e94dc5SPeter Avalos void sshbuf_free(struct sshbuf *buf); 7136e94dc5SPeter Avalos 7236e94dc5SPeter Avalos /* 7336e94dc5SPeter Avalos * Reset buf, clearing its contents. NB. max_size is preserved. 7436e94dc5SPeter Avalos */ 7536e94dc5SPeter Avalos void sshbuf_reset(struct sshbuf *buf); 7636e94dc5SPeter Avalos 7736e94dc5SPeter Avalos /* 7836e94dc5SPeter Avalos * Return the maximum size of buf 7936e94dc5SPeter Avalos */ 8036e94dc5SPeter Avalos size_t sshbuf_max_size(const struct sshbuf *buf); 8136e94dc5SPeter Avalos 8236e94dc5SPeter Avalos /* 8336e94dc5SPeter Avalos * Set the maximum size of buf 8436e94dc5SPeter Avalos * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 8536e94dc5SPeter Avalos */ 8636e94dc5SPeter Avalos int sshbuf_set_max_size(struct sshbuf *buf, size_t max_size); 8736e94dc5SPeter Avalos 8836e94dc5SPeter Avalos /* 8936e94dc5SPeter Avalos * Returns the length of data in buf 9036e94dc5SPeter Avalos */ 9136e94dc5SPeter Avalos size_t sshbuf_len(const struct sshbuf *buf); 9236e94dc5SPeter Avalos 9336e94dc5SPeter Avalos /* 9436e94dc5SPeter Avalos * Returns number of bytes left in buffer before hitting max_size. 9536e94dc5SPeter Avalos */ 9636e94dc5SPeter Avalos size_t sshbuf_avail(const struct sshbuf *buf); 9736e94dc5SPeter Avalos 9836e94dc5SPeter Avalos /* 99e9778795SPeter Avalos * Returns a read-only pointer to the start of the data in buf 10036e94dc5SPeter Avalos */ 10136e94dc5SPeter Avalos const u_char *sshbuf_ptr(const struct sshbuf *buf); 10236e94dc5SPeter Avalos 10336e94dc5SPeter Avalos /* 104e9778795SPeter Avalos * Returns a mutable pointer to the start of the data in buf, or 10536e94dc5SPeter Avalos * NULL if the buffer is read-only. 10636e94dc5SPeter Avalos */ 10736e94dc5SPeter Avalos u_char *sshbuf_mutable_ptr(const struct sshbuf *buf); 10836e94dc5SPeter Avalos 10936e94dc5SPeter Avalos /* 11036e94dc5SPeter Avalos * Check whether a reservation of size len will succeed in buf 11136e94dc5SPeter Avalos * Safer to use than direct comparisons again sshbuf_avail as it copes 11236e94dc5SPeter Avalos * with unsigned overflows correctly. 11336e94dc5SPeter Avalos * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 11436e94dc5SPeter Avalos */ 11536e94dc5SPeter Avalos int sshbuf_check_reserve(const struct sshbuf *buf, size_t len); 11636e94dc5SPeter Avalos 11736e94dc5SPeter Avalos /* 118ce74bacaSMatthew Dillon * Preallocates len additional bytes in buf. 119ce74bacaSMatthew Dillon * Useful for cases where the caller knows how many bytes will ultimately be 120ce74bacaSMatthew Dillon * required to avoid realloc in the buffer code. 121ce74bacaSMatthew Dillon * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 122ce74bacaSMatthew Dillon */ 123ce74bacaSMatthew Dillon int sshbuf_allocate(struct sshbuf *buf, size_t len); 124ce74bacaSMatthew Dillon 125ce74bacaSMatthew Dillon /* 12636e94dc5SPeter Avalos * Reserve len bytes in buf. 12736e94dc5SPeter Avalos * Returns 0 on success and a pointer to the first reserved byte via the 1280cbfa66cSDaniel Fojt * optional dpp parameter or a negative SSH_ERR_* error code on failure. 12936e94dc5SPeter Avalos */ 13036e94dc5SPeter Avalos int sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp); 13136e94dc5SPeter Avalos 13236e94dc5SPeter Avalos /* 13336e94dc5SPeter Avalos * Consume len bytes from the start of buf 13436e94dc5SPeter Avalos * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 13536e94dc5SPeter Avalos */ 13636e94dc5SPeter Avalos int sshbuf_consume(struct sshbuf *buf, size_t len); 13736e94dc5SPeter Avalos 13836e94dc5SPeter Avalos /* 13936e94dc5SPeter Avalos * Consume len bytes from the end of buf 14036e94dc5SPeter Avalos * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 14136e94dc5SPeter Avalos */ 14236e94dc5SPeter Avalos int sshbuf_consume_end(struct sshbuf *buf, size_t len); 14336e94dc5SPeter Avalos 14436e94dc5SPeter Avalos /* Extract or deposit some bytes */ 14536e94dc5SPeter Avalos int sshbuf_get(struct sshbuf *buf, void *v, size_t len); 14636e94dc5SPeter Avalos int sshbuf_put(struct sshbuf *buf, const void *v, size_t len); 14736e94dc5SPeter Avalos int sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v); 14836e94dc5SPeter Avalos 14936e94dc5SPeter Avalos /* Append using a printf(3) format */ 15036e94dc5SPeter Avalos int sshbuf_putf(struct sshbuf *buf, const char *fmt, ...) 15136e94dc5SPeter Avalos __attribute__((format(printf, 2, 3))); 15236e94dc5SPeter Avalos int sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap); 15336e94dc5SPeter Avalos 15436e94dc5SPeter Avalos /* Functions to extract or store big-endian words of various sizes */ 15536e94dc5SPeter Avalos int sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp); 15636e94dc5SPeter Avalos int sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp); 15736e94dc5SPeter Avalos int sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp); 15836e94dc5SPeter Avalos int sshbuf_get_u8(struct sshbuf *buf, u_char *valp); 15936e94dc5SPeter Avalos int sshbuf_put_u64(struct sshbuf *buf, u_int64_t val); 16036e94dc5SPeter Avalos int sshbuf_put_u32(struct sshbuf *buf, u_int32_t val); 16136e94dc5SPeter Avalos int sshbuf_put_u16(struct sshbuf *buf, u_int16_t val); 16236e94dc5SPeter Avalos int sshbuf_put_u8(struct sshbuf *buf, u_char val); 16336e94dc5SPeter Avalos 1640cbfa66cSDaniel Fojt /* Functions to peek at the contents of a buffer without modifying it. */ 1650cbfa66cSDaniel Fojt int sshbuf_peek_u64(const struct sshbuf *buf, size_t offset, 1660cbfa66cSDaniel Fojt u_int64_t *valp); 1670cbfa66cSDaniel Fojt int sshbuf_peek_u32(const struct sshbuf *buf, size_t offset, 1680cbfa66cSDaniel Fojt u_int32_t *valp); 1690cbfa66cSDaniel Fojt int sshbuf_peek_u16(const struct sshbuf *buf, size_t offset, 1700cbfa66cSDaniel Fojt u_int16_t *valp); 1710cbfa66cSDaniel Fojt int sshbuf_peek_u8(const struct sshbuf *buf, size_t offset, 1720cbfa66cSDaniel Fojt u_char *valp); 1730cbfa66cSDaniel Fojt 1740cbfa66cSDaniel Fojt /* 1750cbfa66cSDaniel Fojt * Functions to poke values into an existing buffer (e.g. a length header 1760cbfa66cSDaniel Fojt * to a packet). The destination bytes must already exist in the buffer. 1770cbfa66cSDaniel Fojt */ 1780cbfa66cSDaniel Fojt int sshbuf_poke_u64(struct sshbuf *buf, size_t offset, u_int64_t val); 1790cbfa66cSDaniel Fojt int sshbuf_poke_u32(struct sshbuf *buf, size_t offset, u_int32_t val); 1800cbfa66cSDaniel Fojt int sshbuf_poke_u16(struct sshbuf *buf, size_t offset, u_int16_t val); 1810cbfa66cSDaniel Fojt int sshbuf_poke_u8(struct sshbuf *buf, size_t offset, u_char val); 1820cbfa66cSDaniel Fojt int sshbuf_poke(struct sshbuf *buf, size_t offset, void *v, size_t len); 1830cbfa66cSDaniel Fojt 18436e94dc5SPeter Avalos /* 18536e94dc5SPeter Avalos * Functions to extract or store SSH wire encoded strings (u32 len || data) 18636e94dc5SPeter Avalos * The "cstring" variants admit no \0 characters in the string contents. 18736e94dc5SPeter Avalos * Caller must free *valp. 18836e94dc5SPeter Avalos */ 18936e94dc5SPeter Avalos int sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp); 19036e94dc5SPeter Avalos int sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp); 19136e94dc5SPeter Avalos int sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v); 19236e94dc5SPeter Avalos int sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len); 19336e94dc5SPeter Avalos int sshbuf_put_cstring(struct sshbuf *buf, const char *v); 19436e94dc5SPeter Avalos int sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v); 19536e94dc5SPeter Avalos 19636e94dc5SPeter Avalos /* 19736e94dc5SPeter Avalos * "Direct" variant of sshbuf_get_string, returns pointer into the sshbuf to 19836e94dc5SPeter Avalos * avoid an malloc+memcpy. The pointer is guaranteed to be valid until the 19936e94dc5SPeter Avalos * next sshbuf-modifying function call. Caller does not free. 20036e94dc5SPeter Avalos */ 20136e94dc5SPeter Avalos int sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp, 20236e94dc5SPeter Avalos size_t *lenp); 20336e94dc5SPeter Avalos 20436e94dc5SPeter Avalos /* Skip past a string */ 20536e94dc5SPeter Avalos #define sshbuf_skip_string(buf) sshbuf_get_string_direct(buf, NULL, NULL) 20636e94dc5SPeter Avalos 20736e94dc5SPeter Avalos /* Another variant: "peeks" into the buffer without modifying it */ 20836e94dc5SPeter Avalos int sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp, 20936e94dc5SPeter Avalos size_t *lenp); 21036e94dc5SPeter Avalos 21136e94dc5SPeter Avalos /* 21236e94dc5SPeter Avalos * Functions to extract or store SSH wire encoded bignums and elliptic 21336e94dc5SPeter Avalos * curve points. 21436e94dc5SPeter Avalos */ 21536e94dc5SPeter Avalos int sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len); 216e9778795SPeter Avalos int sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf, 217e9778795SPeter Avalos const u_char **valp, size_t *lenp); 21836e94dc5SPeter Avalos #ifdef WITH_OPENSSL 219664f4763Szrj int sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM **valp); 22036e94dc5SPeter Avalos int sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v); 22136e94dc5SPeter Avalos # ifdef OPENSSL_HAS_ECC 22236e94dc5SPeter Avalos int sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g); 22336e94dc5SPeter Avalos int sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v); 22436e94dc5SPeter Avalos int sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g); 22536e94dc5SPeter Avalos int sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v); 22636e94dc5SPeter Avalos # endif /* OPENSSL_HAS_ECC */ 22736e94dc5SPeter Avalos #endif /* WITH_OPENSSL */ 22836e94dc5SPeter Avalos 22936e94dc5SPeter Avalos /* Dump the contents of the buffer in a human-readable format */ 23050a69bb5SSascha Wildner void sshbuf_dump(const struct sshbuf *buf, FILE *f); 23136e94dc5SPeter Avalos 23236e94dc5SPeter Avalos /* Dump specified memory in a human-readable format */ 23336e94dc5SPeter Avalos void sshbuf_dump_data(const void *s, size_t len, FILE *f); 23436e94dc5SPeter Avalos 23536e94dc5SPeter Avalos /* Return the hexadecimal representation of the contents of the buffer */ 23636e94dc5SPeter Avalos char *sshbuf_dtob16(struct sshbuf *buf); 23736e94dc5SPeter Avalos 23836e94dc5SPeter Avalos /* Encode the contents of the buffer as base64 */ 2390cbfa66cSDaniel Fojt char *sshbuf_dtob64_string(const struct sshbuf *buf, int wrap); 2400cbfa66cSDaniel Fojt int sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap); 24150a69bb5SSascha Wildner /* RFC4648 "base64url" encoding variant */ 24250a69bb5SSascha Wildner int sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap); 24336e94dc5SPeter Avalos 24436e94dc5SPeter Avalos /* Decode base64 data and append it to the buffer */ 24536e94dc5SPeter Avalos int sshbuf_b64tod(struct sshbuf *buf, const char *b64); 24636e94dc5SPeter Avalos 247e9778795SPeter Avalos /* 2480cbfa66cSDaniel Fojt * Tests whether the buffer contains the specified byte sequence at the 2490cbfa66cSDaniel Fojt * specified offset. Returns 0 on successful match, or a ssherr.h code 2500cbfa66cSDaniel Fojt * otherwise. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were 2510cbfa66cSDaniel Fojt * present but the buffer contents did not match those supplied. Zero- 2520cbfa66cSDaniel Fojt * length comparisons are not allowed. 2530cbfa66cSDaniel Fojt * 2540cbfa66cSDaniel Fojt * If sufficient data is present to make a comparison, then it is 2550cbfa66cSDaniel Fojt * performed with timing independent of the value of the data. If 2560cbfa66cSDaniel Fojt * insufficient data is present then the comparison is not attempted at 2570cbfa66cSDaniel Fojt * all. 2580cbfa66cSDaniel Fojt */ 2590cbfa66cSDaniel Fojt int sshbuf_cmp(const struct sshbuf *b, size_t offset, 2600cbfa66cSDaniel Fojt const void *s, size_t len); 2610cbfa66cSDaniel Fojt 2620cbfa66cSDaniel Fojt /* 2630cbfa66cSDaniel Fojt * Searches the buffer for the specified string. Returns 0 on success 2640cbfa66cSDaniel Fojt * and updates *offsetp with the offset of the first match, relative to 2650cbfa66cSDaniel Fojt * the start of the buffer. Otherwise sshbuf_find will return a ssherr.h 2660cbfa66cSDaniel Fojt * error code. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were 2670cbfa66cSDaniel Fojt * present in the buffer for a match to be possible but none was found. 2680cbfa66cSDaniel Fojt * Searches for zero-length data are not allowed. 2690cbfa66cSDaniel Fojt */ 2700cbfa66cSDaniel Fojt int 2710cbfa66cSDaniel Fojt sshbuf_find(const struct sshbuf *b, size_t start_offset, 2720cbfa66cSDaniel Fojt const void *s, size_t len, size_t *offsetp); 2730cbfa66cSDaniel Fojt 2740cbfa66cSDaniel Fojt /* 275e9778795SPeter Avalos * Duplicate the contents of a buffer to a string (caller to free). 276e9778795SPeter Avalos * Returns NULL on buffer error, or if the buffer contains a premature 277e9778795SPeter Avalos * nul character. 278e9778795SPeter Avalos */ 279e9778795SPeter Avalos char *sshbuf_dup_string(struct sshbuf *buf); 280e9778795SPeter Avalos 2810cbfa66cSDaniel Fojt /* 2820cbfa66cSDaniel Fojt * Fill a buffer from a file descriptor or filename. Both allocate the 2830cbfa66cSDaniel Fojt * buffer for the caller. 2840cbfa66cSDaniel Fojt */ 2850cbfa66cSDaniel Fojt int sshbuf_load_fd(int, struct sshbuf **) 2860cbfa66cSDaniel Fojt __attribute__((__nonnull__ (2))); 2870cbfa66cSDaniel Fojt int sshbuf_load_file(const char *, struct sshbuf **) 2880cbfa66cSDaniel Fojt __attribute__((__nonnull__ (2))); 2890cbfa66cSDaniel Fojt 2900cbfa66cSDaniel Fojt /* 2910cbfa66cSDaniel Fojt * Write a buffer to a path, creating/truncating as needed (mode 0644, 2920cbfa66cSDaniel Fojt * subject to umask). The buffer contents are not modified. 2930cbfa66cSDaniel Fojt */ 2940cbfa66cSDaniel Fojt int sshbuf_write_file(const char *path, struct sshbuf *buf) 2950cbfa66cSDaniel Fojt __attribute__((__nonnull__ (2))); 2960cbfa66cSDaniel Fojt 297ee116499SAntonio Huete Jimenez /* Read up to maxlen bytes from a fd directly to a buffer */ 298ee116499SAntonio Huete Jimenez int sshbuf_read(int, struct sshbuf *, size_t, size_t *) 299ee116499SAntonio Huete Jimenez __attribute__((__nonnull__ (2))); 300ee116499SAntonio Huete Jimenez 30136e94dc5SPeter Avalos /* Macros for decoding/encoding integers */ 30236e94dc5SPeter Avalos #define PEEK_U64(p) \ 303e9778795SPeter Avalos (((u_int64_t)(((const u_char *)(p))[0]) << 56) | \ 304e9778795SPeter Avalos ((u_int64_t)(((const u_char *)(p))[1]) << 48) | \ 305e9778795SPeter Avalos ((u_int64_t)(((const u_char *)(p))[2]) << 40) | \ 306e9778795SPeter Avalos ((u_int64_t)(((const u_char *)(p))[3]) << 32) | \ 307e9778795SPeter Avalos ((u_int64_t)(((const u_char *)(p))[4]) << 24) | \ 308e9778795SPeter Avalos ((u_int64_t)(((const u_char *)(p))[5]) << 16) | \ 309e9778795SPeter Avalos ((u_int64_t)(((const u_char *)(p))[6]) << 8) | \ 310e9778795SPeter Avalos (u_int64_t)(((const u_char *)(p))[7])) 31136e94dc5SPeter Avalos #define PEEK_U32(p) \ 312e9778795SPeter Avalos (((u_int32_t)(((const u_char *)(p))[0]) << 24) | \ 313e9778795SPeter Avalos ((u_int32_t)(((const u_char *)(p))[1]) << 16) | \ 314e9778795SPeter Avalos ((u_int32_t)(((const u_char *)(p))[2]) << 8) | \ 315e9778795SPeter Avalos (u_int32_t)(((const u_char *)(p))[3])) 31636e94dc5SPeter Avalos #define PEEK_U16(p) \ 317e9778795SPeter Avalos (((u_int16_t)(((const u_char *)(p))[0]) << 8) | \ 318e9778795SPeter Avalos (u_int16_t)(((const u_char *)(p))[1])) 31936e94dc5SPeter Avalos 32036e94dc5SPeter Avalos #define POKE_U64(p, v) \ 32136e94dc5SPeter Avalos do { \ 322e9778795SPeter Avalos const u_int64_t __v = (v); \ 323e9778795SPeter Avalos ((u_char *)(p))[0] = (__v >> 56) & 0xff; \ 324e9778795SPeter Avalos ((u_char *)(p))[1] = (__v >> 48) & 0xff; \ 325e9778795SPeter Avalos ((u_char *)(p))[2] = (__v >> 40) & 0xff; \ 326e9778795SPeter Avalos ((u_char *)(p))[3] = (__v >> 32) & 0xff; \ 327e9778795SPeter Avalos ((u_char *)(p))[4] = (__v >> 24) & 0xff; \ 328e9778795SPeter Avalos ((u_char *)(p))[5] = (__v >> 16) & 0xff; \ 329e9778795SPeter Avalos ((u_char *)(p))[6] = (__v >> 8) & 0xff; \ 330e9778795SPeter Avalos ((u_char *)(p))[7] = __v & 0xff; \ 33136e94dc5SPeter Avalos } while (0) 33236e94dc5SPeter Avalos #define POKE_U32(p, v) \ 33336e94dc5SPeter Avalos do { \ 334e9778795SPeter Avalos const u_int32_t __v = (v); \ 335e9778795SPeter Avalos ((u_char *)(p))[0] = (__v >> 24) & 0xff; \ 336e9778795SPeter Avalos ((u_char *)(p))[1] = (__v >> 16) & 0xff; \ 337e9778795SPeter Avalos ((u_char *)(p))[2] = (__v >> 8) & 0xff; \ 338e9778795SPeter Avalos ((u_char *)(p))[3] = __v & 0xff; \ 33936e94dc5SPeter Avalos } while (0) 34036e94dc5SPeter Avalos #define POKE_U16(p, v) \ 34136e94dc5SPeter Avalos do { \ 342e9778795SPeter Avalos const u_int16_t __v = (v); \ 343e9778795SPeter Avalos ((u_char *)(p))[0] = (__v >> 8) & 0xff; \ 344e9778795SPeter Avalos ((u_char *)(p))[1] = __v & 0xff; \ 34536e94dc5SPeter Avalos } while (0) 34636e94dc5SPeter Avalos 34736e94dc5SPeter Avalos /* Internal definitions follow. Exposed for regress tests */ 34836e94dc5SPeter Avalos #ifdef SSHBUF_INTERNAL 34936e94dc5SPeter Avalos 35036e94dc5SPeter Avalos /* 35136e94dc5SPeter Avalos * Return the allocation size of buf 35236e94dc5SPeter Avalos */ 35336e94dc5SPeter Avalos size_t sshbuf_alloc(const struct sshbuf *buf); 35436e94dc5SPeter Avalos 35536e94dc5SPeter Avalos /* 35636e94dc5SPeter Avalos * Increment the reference count of buf. 35736e94dc5SPeter Avalos */ 35836e94dc5SPeter Avalos int sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent); 35936e94dc5SPeter Avalos 36036e94dc5SPeter Avalos /* 36136e94dc5SPeter Avalos * Return the parent buffer of buf, or NULL if it has no parent. 36236e94dc5SPeter Avalos */ 36336e94dc5SPeter Avalos const struct sshbuf *sshbuf_parent(const struct sshbuf *buf); 36436e94dc5SPeter Avalos 36536e94dc5SPeter Avalos /* 36636e94dc5SPeter Avalos * Return the reference count of buf 36736e94dc5SPeter Avalos */ 36836e94dc5SPeter Avalos u_int sshbuf_refcount(const struct sshbuf *buf); 36936e94dc5SPeter Avalos 37036e94dc5SPeter Avalos # define SSHBUF_SIZE_INIT 256 /* Initial allocation */ 37136e94dc5SPeter Avalos # define SSHBUF_SIZE_INC 256 /* Preferred increment length */ 372ee116499SAntonio Huete Jimenez # define SSHBUF_PACK_MIN 8192 /* Minimum packable offset */ 37336e94dc5SPeter Avalos 37436e94dc5SPeter Avalos /* # define SSHBUF_ABORT abort */ 37536e94dc5SPeter Avalos /* # define SSHBUF_DEBUG */ 37636e94dc5SPeter Avalos 37736e94dc5SPeter Avalos # ifndef SSHBUF_ABORT 37836e94dc5SPeter Avalos # define SSHBUF_ABORT() 37936e94dc5SPeter Avalos # endif 38036e94dc5SPeter Avalos 38136e94dc5SPeter Avalos # ifdef SSHBUF_DEBUG 38236e94dc5SPeter Avalos # define SSHBUF_DBG(x) do { \ 38336e94dc5SPeter Avalos printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \ 38436e94dc5SPeter Avalos printf x; \ 38536e94dc5SPeter Avalos printf("\n"); \ 38636e94dc5SPeter Avalos fflush(stdout); \ 38736e94dc5SPeter Avalos } while (0) 38836e94dc5SPeter Avalos # else 38936e94dc5SPeter Avalos # define SSHBUF_DBG(x) 39036e94dc5SPeter Avalos # endif 39136e94dc5SPeter Avalos #endif /* SSHBUF_INTERNAL */ 39236e94dc5SPeter Avalos 39336e94dc5SPeter Avalos #endif /* _SSHBUF_H */ 394