xref: /freebsd/crypto/openssh/sshbuf.h (revision f374ba41f55c1a127303d92d830dd58eef2f5243)
1*f374ba41SEd Maste /*	$OpenBSD: sshbuf.h,v 1.28 2022/12/02 04:40:27 djm Exp $	*/
2a0ee8cc6SDag-Erling Smørgrav /*
3a0ee8cc6SDag-Erling Smørgrav  * Copyright (c) 2011 Damien Miller
4a0ee8cc6SDag-Erling Smørgrav  *
5a0ee8cc6SDag-Erling Smørgrav  * Permission to use, copy, modify, and distribute this software for any
6a0ee8cc6SDag-Erling Smørgrav  * purpose with or without fee is hereby granted, provided that the above
7a0ee8cc6SDag-Erling Smørgrav  * copyright notice and this permission notice appear in all copies.
8a0ee8cc6SDag-Erling Smørgrav  *
9a0ee8cc6SDag-Erling Smørgrav  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10a0ee8cc6SDag-Erling Smørgrav  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11a0ee8cc6SDag-Erling Smørgrav  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12a0ee8cc6SDag-Erling Smørgrav  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13a0ee8cc6SDag-Erling Smørgrav  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14a0ee8cc6SDag-Erling Smørgrav  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15a0ee8cc6SDag-Erling Smørgrav  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16a0ee8cc6SDag-Erling Smørgrav  */
17a0ee8cc6SDag-Erling Smørgrav 
18a0ee8cc6SDag-Erling Smørgrav #ifndef _SSHBUF_H
19a0ee8cc6SDag-Erling Smørgrav #define _SSHBUF_H
20a0ee8cc6SDag-Erling Smørgrav 
21a0ee8cc6SDag-Erling Smørgrav #include <sys/types.h>
22a0ee8cc6SDag-Erling Smørgrav #include <stdarg.h>
23a0ee8cc6SDag-Erling Smørgrav #include <stdio.h>
24a0ee8cc6SDag-Erling Smørgrav #ifdef WITH_OPENSSL
25a0ee8cc6SDag-Erling Smørgrav # include <openssl/bn.h>
26a0ee8cc6SDag-Erling Smørgrav # ifdef OPENSSL_HAS_ECC
27a0ee8cc6SDag-Erling Smørgrav #  include <openssl/ec.h>
28a0ee8cc6SDag-Erling Smørgrav # endif /* OPENSSL_HAS_ECC */
29a0ee8cc6SDag-Erling Smørgrav #endif /* WITH_OPENSSL */
30a0ee8cc6SDag-Erling Smørgrav 
31a0ee8cc6SDag-Erling Smørgrav #define SSHBUF_SIZE_MAX		0x8000000	/* Hard maximum size */
32a0ee8cc6SDag-Erling Smørgrav #define SSHBUF_REFS_MAX		0x100000	/* Max child buffers */
33a0ee8cc6SDag-Erling Smørgrav #define SSHBUF_MAX_BIGNUM	(16384 / 8)	/* Max bignum *bytes* */
34a0ee8cc6SDag-Erling Smørgrav #define SSHBUF_MAX_ECPOINT	((528 * 2 / 8) + 1) /* Max EC point *bytes* */
35a0ee8cc6SDag-Erling Smørgrav 
36*f374ba41SEd Maste struct sshbuf;
37a0ee8cc6SDag-Erling Smørgrav 
38a0ee8cc6SDag-Erling Smørgrav /*
39a0ee8cc6SDag-Erling Smørgrav  * Create a new sshbuf buffer.
40a0ee8cc6SDag-Erling Smørgrav  * Returns pointer to buffer on success, or NULL on allocation failure.
41a0ee8cc6SDag-Erling Smørgrav  */
42a0ee8cc6SDag-Erling Smørgrav struct sshbuf *sshbuf_new(void);
43a0ee8cc6SDag-Erling Smørgrav 
44a0ee8cc6SDag-Erling Smørgrav /*
45a0ee8cc6SDag-Erling Smørgrav  * Create a new, read-only sshbuf buffer from existing data.
46a0ee8cc6SDag-Erling Smørgrav  * Returns pointer to buffer on success, or NULL on allocation failure.
47a0ee8cc6SDag-Erling Smørgrav  */
48a0ee8cc6SDag-Erling Smørgrav struct sshbuf *sshbuf_from(const void *blob, size_t len);
49a0ee8cc6SDag-Erling Smørgrav 
50a0ee8cc6SDag-Erling Smørgrav /*
51a0ee8cc6SDag-Erling Smørgrav  * Create a new, read-only sshbuf buffer from the contents of an existing
52a0ee8cc6SDag-Erling Smørgrav  * buffer. The contents of "buf" must not change in the lifetime of the
53a0ee8cc6SDag-Erling Smørgrav  * resultant buffer.
54a0ee8cc6SDag-Erling Smørgrav  * Returns pointer to buffer on success, or NULL on allocation failure.
55a0ee8cc6SDag-Erling Smørgrav  */
56a0ee8cc6SDag-Erling Smørgrav struct sshbuf *sshbuf_fromb(struct sshbuf *buf);
57a0ee8cc6SDag-Erling Smørgrav 
58a0ee8cc6SDag-Erling Smørgrav /*
59a0ee8cc6SDag-Erling Smørgrav  * Create a new, read-only sshbuf buffer from the contents of a string in
60a0ee8cc6SDag-Erling Smørgrav  * an existing buffer (the string is consumed in the process).
61a0ee8cc6SDag-Erling Smørgrav  * The contents of "buf" must not change in the lifetime of the resultant
62a0ee8cc6SDag-Erling Smørgrav  * buffer.
63a0ee8cc6SDag-Erling Smørgrav  * Returns pointer to buffer on success, or NULL on allocation failure.
64a0ee8cc6SDag-Erling Smørgrav  */
65a0ee8cc6SDag-Erling Smørgrav int	sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp);
66a0ee8cc6SDag-Erling Smørgrav 
67a0ee8cc6SDag-Erling Smørgrav /*
68a0ee8cc6SDag-Erling Smørgrav  * Clear and free buf
69a0ee8cc6SDag-Erling Smørgrav  */
70a0ee8cc6SDag-Erling Smørgrav void	sshbuf_free(struct sshbuf *buf);
71a0ee8cc6SDag-Erling Smørgrav 
72a0ee8cc6SDag-Erling Smørgrav /*
73a0ee8cc6SDag-Erling Smørgrav  * Reset buf, clearing its contents. NB. max_size is preserved.
74a0ee8cc6SDag-Erling Smørgrav  */
75a0ee8cc6SDag-Erling Smørgrav void	sshbuf_reset(struct sshbuf *buf);
76a0ee8cc6SDag-Erling Smørgrav 
77a0ee8cc6SDag-Erling Smørgrav /*
78a0ee8cc6SDag-Erling Smørgrav  * Return the maximum size of buf
79a0ee8cc6SDag-Erling Smørgrav  */
80a0ee8cc6SDag-Erling Smørgrav size_t	sshbuf_max_size(const struct sshbuf *buf);
81a0ee8cc6SDag-Erling Smørgrav 
82a0ee8cc6SDag-Erling Smørgrav /*
83a0ee8cc6SDag-Erling Smørgrav  * Set the maximum size of buf
84a0ee8cc6SDag-Erling Smørgrav  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
85a0ee8cc6SDag-Erling Smørgrav  */
86a0ee8cc6SDag-Erling Smørgrav int	sshbuf_set_max_size(struct sshbuf *buf, size_t max_size);
87a0ee8cc6SDag-Erling Smørgrav 
88a0ee8cc6SDag-Erling Smørgrav /*
89a0ee8cc6SDag-Erling Smørgrav  * Returns the length of data in buf
90a0ee8cc6SDag-Erling Smørgrav  */
91a0ee8cc6SDag-Erling Smørgrav size_t	sshbuf_len(const struct sshbuf *buf);
92a0ee8cc6SDag-Erling Smørgrav 
93a0ee8cc6SDag-Erling Smørgrav /*
94a0ee8cc6SDag-Erling Smørgrav  * Returns number of bytes left in buffer before hitting max_size.
95a0ee8cc6SDag-Erling Smørgrav  */
96a0ee8cc6SDag-Erling Smørgrav size_t	sshbuf_avail(const struct sshbuf *buf);
97a0ee8cc6SDag-Erling Smørgrav 
98a0ee8cc6SDag-Erling Smørgrav /*
99acc1a9efSDag-Erling Smørgrav  * Returns a read-only pointer to the start of the data in buf
100a0ee8cc6SDag-Erling Smørgrav  */
101a0ee8cc6SDag-Erling Smørgrav const u_char *sshbuf_ptr(const struct sshbuf *buf);
102a0ee8cc6SDag-Erling Smørgrav 
103a0ee8cc6SDag-Erling Smørgrav /*
104acc1a9efSDag-Erling Smørgrav  * Returns a mutable pointer to the start of the data in buf, or
105a0ee8cc6SDag-Erling Smørgrav  * NULL if the buffer is read-only.
106a0ee8cc6SDag-Erling Smørgrav  */
107a0ee8cc6SDag-Erling Smørgrav u_char *sshbuf_mutable_ptr(const struct sshbuf *buf);
108a0ee8cc6SDag-Erling Smørgrav 
109a0ee8cc6SDag-Erling Smørgrav /*
110a0ee8cc6SDag-Erling Smørgrav  * Check whether a reservation of size len will succeed in buf
111a0ee8cc6SDag-Erling Smørgrav  * Safer to use than direct comparisons again sshbuf_avail as it copes
112a0ee8cc6SDag-Erling Smørgrav  * with unsigned overflows correctly.
113a0ee8cc6SDag-Erling Smørgrav  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
114a0ee8cc6SDag-Erling Smørgrav  */
115a0ee8cc6SDag-Erling Smørgrav int	sshbuf_check_reserve(const struct sshbuf *buf, size_t len);
116a0ee8cc6SDag-Erling Smørgrav 
117a0ee8cc6SDag-Erling Smørgrav /*
118ca86bcf2SDag-Erling Smørgrav  * Preallocates len additional bytes in buf.
119ca86bcf2SDag-Erling Smørgrav  * Useful for cases where the caller knows how many bytes will ultimately be
120ca86bcf2SDag-Erling Smørgrav  * required to avoid realloc in the buffer code.
121ca86bcf2SDag-Erling Smørgrav  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
122ca86bcf2SDag-Erling Smørgrav  */
123ca86bcf2SDag-Erling Smørgrav int	sshbuf_allocate(struct sshbuf *buf, size_t len);
124ca86bcf2SDag-Erling Smørgrav 
125ca86bcf2SDag-Erling Smørgrav /*
126a0ee8cc6SDag-Erling Smørgrav  * Reserve len bytes in buf.
127a0ee8cc6SDag-Erling Smørgrav  * Returns 0 on success and a pointer to the first reserved byte via the
12819261079SEd Maste  * optional dpp parameter or a negative SSH_ERR_* error code on failure.
129a0ee8cc6SDag-Erling Smørgrav  */
130a0ee8cc6SDag-Erling Smørgrav int	sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp);
131a0ee8cc6SDag-Erling Smørgrav 
132a0ee8cc6SDag-Erling Smørgrav /*
133a0ee8cc6SDag-Erling Smørgrav  * Consume len bytes from the start of buf
134a0ee8cc6SDag-Erling Smørgrav  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
135a0ee8cc6SDag-Erling Smørgrav  */
136a0ee8cc6SDag-Erling Smørgrav int	sshbuf_consume(struct sshbuf *buf, size_t len);
137a0ee8cc6SDag-Erling Smørgrav 
138a0ee8cc6SDag-Erling Smørgrav /*
139a0ee8cc6SDag-Erling Smørgrav  * Consume len bytes from the end of buf
140a0ee8cc6SDag-Erling Smørgrav  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
141a0ee8cc6SDag-Erling Smørgrav  */
142a0ee8cc6SDag-Erling Smørgrav int	sshbuf_consume_end(struct sshbuf *buf, size_t len);
143a0ee8cc6SDag-Erling Smørgrav 
144a0ee8cc6SDag-Erling Smørgrav /* Extract or deposit some bytes */
145a0ee8cc6SDag-Erling Smørgrav int	sshbuf_get(struct sshbuf *buf, void *v, size_t len);
146a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put(struct sshbuf *buf, const void *v, size_t len);
147a0ee8cc6SDag-Erling Smørgrav int	sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v);
148a0ee8cc6SDag-Erling Smørgrav 
149a0ee8cc6SDag-Erling Smørgrav /* Append using a printf(3) format */
150a0ee8cc6SDag-Erling Smørgrav int	sshbuf_putf(struct sshbuf *buf, const char *fmt, ...)
151a0ee8cc6SDag-Erling Smørgrav 	    __attribute__((format(printf, 2, 3)));
152a0ee8cc6SDag-Erling Smørgrav int	sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap);
153a0ee8cc6SDag-Erling Smørgrav 
154a0ee8cc6SDag-Erling Smørgrav /* Functions to extract or store big-endian words of various sizes */
155a0ee8cc6SDag-Erling Smørgrav int	sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp);
156a0ee8cc6SDag-Erling Smørgrav int	sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp);
157a0ee8cc6SDag-Erling Smørgrav int	sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp);
158a0ee8cc6SDag-Erling Smørgrav int	sshbuf_get_u8(struct sshbuf *buf, u_char *valp);
159a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put_u64(struct sshbuf *buf, u_int64_t val);
160a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put_u32(struct sshbuf *buf, u_int32_t val);
161a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put_u16(struct sshbuf *buf, u_int16_t val);
162a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put_u8(struct sshbuf *buf, u_char val);
163a0ee8cc6SDag-Erling Smørgrav 
16419261079SEd Maste /* Functions to peek at the contents of a buffer without modifying it. */
16519261079SEd Maste int	sshbuf_peek_u64(const struct sshbuf *buf, size_t offset,
16619261079SEd Maste     u_int64_t *valp);
16719261079SEd Maste int	sshbuf_peek_u32(const struct sshbuf *buf, size_t offset,
16819261079SEd Maste     u_int32_t *valp);
16919261079SEd Maste int	sshbuf_peek_u16(const struct sshbuf *buf, size_t offset,
17019261079SEd Maste     u_int16_t *valp);
17119261079SEd Maste int	sshbuf_peek_u8(const struct sshbuf *buf, size_t offset,
17219261079SEd Maste     u_char *valp);
17319261079SEd Maste 
17419261079SEd Maste /*
17519261079SEd Maste  * Functions to poke values into an existing buffer (e.g. a length header
17619261079SEd Maste  * to a packet). The destination bytes must already exist in the buffer.
17719261079SEd Maste  */
17819261079SEd Maste int sshbuf_poke_u64(struct sshbuf *buf, size_t offset, u_int64_t val);
17919261079SEd Maste int sshbuf_poke_u32(struct sshbuf *buf, size_t offset, u_int32_t val);
18019261079SEd Maste int sshbuf_poke_u16(struct sshbuf *buf, size_t offset, u_int16_t val);
18119261079SEd Maste int sshbuf_poke_u8(struct sshbuf *buf, size_t offset, u_char val);
18219261079SEd Maste int sshbuf_poke(struct sshbuf *buf, size_t offset, void *v, size_t len);
18319261079SEd Maste 
184a0ee8cc6SDag-Erling Smørgrav /*
185a0ee8cc6SDag-Erling Smørgrav  * Functions to extract or store SSH wire encoded strings (u32 len || data)
186a0ee8cc6SDag-Erling Smørgrav  * The "cstring" variants admit no \0 characters in the string contents.
187a0ee8cc6SDag-Erling Smørgrav  * Caller must free *valp.
188a0ee8cc6SDag-Erling Smørgrav  */
189a0ee8cc6SDag-Erling Smørgrav int	sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp);
190a0ee8cc6SDag-Erling Smørgrav int	sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp);
191a0ee8cc6SDag-Erling Smørgrav int	sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v);
192a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len);
193a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put_cstring(struct sshbuf *buf, const char *v);
194a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v);
195a0ee8cc6SDag-Erling Smørgrav 
196a0ee8cc6SDag-Erling Smørgrav /*
197a0ee8cc6SDag-Erling Smørgrav  * "Direct" variant of sshbuf_get_string, returns pointer into the sshbuf to
198a0ee8cc6SDag-Erling Smørgrav  * avoid an malloc+memcpy. The pointer is guaranteed to be valid until the
199a0ee8cc6SDag-Erling Smørgrav  * next sshbuf-modifying function call. Caller does not free.
200a0ee8cc6SDag-Erling Smørgrav  */
201a0ee8cc6SDag-Erling Smørgrav int	sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp,
202a0ee8cc6SDag-Erling Smørgrav 	    size_t *lenp);
203a0ee8cc6SDag-Erling Smørgrav 
204a0ee8cc6SDag-Erling Smørgrav /* Skip past a string */
205a0ee8cc6SDag-Erling Smørgrav #define sshbuf_skip_string(buf) sshbuf_get_string_direct(buf, NULL, NULL)
206a0ee8cc6SDag-Erling Smørgrav 
207a0ee8cc6SDag-Erling Smørgrav /* Another variant: "peeks" into the buffer without modifying it */
208a0ee8cc6SDag-Erling Smørgrav int	sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp,
209a0ee8cc6SDag-Erling Smørgrav 	    size_t *lenp);
210a0ee8cc6SDag-Erling Smørgrav 
211a0ee8cc6SDag-Erling Smørgrav /*
212a0ee8cc6SDag-Erling Smørgrav  * Functions to extract or store SSH wire encoded bignums and elliptic
213a0ee8cc6SDag-Erling Smørgrav  * curve points.
214a0ee8cc6SDag-Erling Smørgrav  */
215a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len);
216bc5531deSDag-Erling Smørgrav int	sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
217bc5531deSDag-Erling Smørgrav 	    const u_char **valp, size_t *lenp);
218a0ee8cc6SDag-Erling Smørgrav #ifdef WITH_OPENSSL
21919261079SEd Maste int	sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM **valp);
220a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v);
221a0ee8cc6SDag-Erling Smørgrav # ifdef OPENSSL_HAS_ECC
222a0ee8cc6SDag-Erling Smørgrav int	sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g);
223a0ee8cc6SDag-Erling Smørgrav int	sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v);
224a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g);
225a0ee8cc6SDag-Erling Smørgrav int	sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v);
226a0ee8cc6SDag-Erling Smørgrav # endif /* OPENSSL_HAS_ECC */
227a0ee8cc6SDag-Erling Smørgrav #endif /* WITH_OPENSSL */
228a0ee8cc6SDag-Erling Smørgrav 
229a0ee8cc6SDag-Erling Smørgrav /* Dump the contents of the buffer in a human-readable format */
23019261079SEd Maste void	sshbuf_dump(const struct sshbuf *buf, FILE *f);
231a0ee8cc6SDag-Erling Smørgrav 
232a0ee8cc6SDag-Erling Smørgrav /* Dump specified memory in a human-readable format */
233a0ee8cc6SDag-Erling Smørgrav void	sshbuf_dump_data(const void *s, size_t len, FILE *f);
234a0ee8cc6SDag-Erling Smørgrav 
235a0ee8cc6SDag-Erling Smørgrav /* Return the hexadecimal representation of the contents of the buffer */
236a0ee8cc6SDag-Erling Smørgrav char	*sshbuf_dtob16(struct sshbuf *buf);
237a0ee8cc6SDag-Erling Smørgrav 
238a0ee8cc6SDag-Erling Smørgrav /* Encode the contents of the buffer as base64 */
23919261079SEd Maste char	*sshbuf_dtob64_string(const struct sshbuf *buf, int wrap);
24019261079SEd Maste int	sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap);
24119261079SEd Maste /* RFC4648 "base64url" encoding variant */
24219261079SEd Maste int	sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap);
243a0ee8cc6SDag-Erling Smørgrav 
244a0ee8cc6SDag-Erling Smørgrav /* Decode base64 data and append it to the buffer */
245a0ee8cc6SDag-Erling Smørgrav int	sshbuf_b64tod(struct sshbuf *buf, const char *b64);
246a0ee8cc6SDag-Erling Smørgrav 
247076ad2f8SDag-Erling Smørgrav /*
24819261079SEd Maste  * Tests whether the buffer contains the specified byte sequence at the
24919261079SEd Maste  * specified offset. Returns 0 on successful match, or a ssherr.h code
25019261079SEd Maste  * otherwise. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were
25119261079SEd Maste  * present but the buffer contents did not match those supplied. Zero-
25219261079SEd Maste  * length comparisons are not allowed.
25319261079SEd Maste  *
25419261079SEd Maste  * If sufficient data is present to make a comparison, then it is
25519261079SEd Maste  * performed with timing independent of the value of the data. If
25619261079SEd Maste  * insufficient data is present then the comparison is not attempted at
25719261079SEd Maste  * all.
25819261079SEd Maste  */
25919261079SEd Maste int	sshbuf_cmp(const struct sshbuf *b, size_t offset,
26019261079SEd Maste     const void *s, size_t len);
26119261079SEd Maste 
26219261079SEd Maste /*
26319261079SEd Maste  * Searches the buffer for the specified string. Returns 0 on success
26419261079SEd Maste  * and updates *offsetp with the offset of the first match, relative to
26519261079SEd Maste  * the start of the buffer. Otherwise sshbuf_find will return a ssherr.h
26619261079SEd Maste  * error code. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were
26719261079SEd Maste  * present in the buffer for a match to be possible but none was found.
26819261079SEd Maste  * Searches for zero-length data are not allowed.
26919261079SEd Maste  */
27019261079SEd Maste int
27119261079SEd Maste sshbuf_find(const struct sshbuf *b, size_t start_offset,
27219261079SEd Maste     const void *s, size_t len, size_t *offsetp);
27319261079SEd Maste 
27419261079SEd Maste /*
275076ad2f8SDag-Erling Smørgrav  * Duplicate the contents of a buffer to a string (caller to free).
276076ad2f8SDag-Erling Smørgrav  * Returns NULL on buffer error, or if the buffer contains a premature
277076ad2f8SDag-Erling Smørgrav  * nul character.
278076ad2f8SDag-Erling Smørgrav  */
279076ad2f8SDag-Erling Smørgrav char *sshbuf_dup_string(struct sshbuf *buf);
280076ad2f8SDag-Erling Smørgrav 
28119261079SEd Maste /*
28219261079SEd Maste  * Fill a buffer from a file descriptor or filename. Both allocate the
28319261079SEd Maste  * buffer for the caller.
28419261079SEd Maste  */
28519261079SEd Maste int sshbuf_load_fd(int, struct sshbuf **)
28619261079SEd Maste     __attribute__((__nonnull__ (2)));
28719261079SEd Maste int sshbuf_load_file(const char *, struct sshbuf **)
28819261079SEd Maste     __attribute__((__nonnull__ (2)));
28919261079SEd Maste 
29019261079SEd Maste /*
29119261079SEd Maste  * Write a buffer to a path, creating/truncating as needed (mode 0644,
29219261079SEd Maste  * subject to umask). The buffer contents are not modified.
29319261079SEd Maste  */
29419261079SEd Maste int sshbuf_write_file(const char *path, struct sshbuf *buf)
29519261079SEd Maste     __attribute__((__nonnull__ (2)));
29619261079SEd Maste 
2971323ec57SEd Maste /* Read up to maxlen bytes from a fd directly to a buffer */
2981323ec57SEd Maste int sshbuf_read(int, struct sshbuf *, size_t, size_t *)
2991323ec57SEd Maste     __attribute__((__nonnull__ (2)));
3001323ec57SEd Maste 
301a0ee8cc6SDag-Erling Smørgrav /* Macros for decoding/encoding integers */
302a0ee8cc6SDag-Erling Smørgrav #define PEEK_U64(p) \
303acc1a9efSDag-Erling Smørgrav 	(((u_int64_t)(((const u_char *)(p))[0]) << 56) | \
304acc1a9efSDag-Erling Smørgrav 	 ((u_int64_t)(((const u_char *)(p))[1]) << 48) | \
305acc1a9efSDag-Erling Smørgrav 	 ((u_int64_t)(((const u_char *)(p))[2]) << 40) | \
306acc1a9efSDag-Erling Smørgrav 	 ((u_int64_t)(((const u_char *)(p))[3]) << 32) | \
307acc1a9efSDag-Erling Smørgrav 	 ((u_int64_t)(((const u_char *)(p))[4]) << 24) | \
308acc1a9efSDag-Erling Smørgrav 	 ((u_int64_t)(((const u_char *)(p))[5]) << 16) | \
309acc1a9efSDag-Erling Smørgrav 	 ((u_int64_t)(((const u_char *)(p))[6]) << 8) | \
310acc1a9efSDag-Erling Smørgrav 	  (u_int64_t)(((const u_char *)(p))[7]))
311a0ee8cc6SDag-Erling Smørgrav #define PEEK_U32(p) \
312acc1a9efSDag-Erling Smørgrav 	(((u_int32_t)(((const u_char *)(p))[0]) << 24) | \
313acc1a9efSDag-Erling Smørgrav 	 ((u_int32_t)(((const u_char *)(p))[1]) << 16) | \
314acc1a9efSDag-Erling Smørgrav 	 ((u_int32_t)(((const u_char *)(p))[2]) << 8) | \
315acc1a9efSDag-Erling Smørgrav 	  (u_int32_t)(((const u_char *)(p))[3]))
316a0ee8cc6SDag-Erling Smørgrav #define PEEK_U16(p) \
317acc1a9efSDag-Erling Smørgrav 	(((u_int16_t)(((const u_char *)(p))[0]) << 8) | \
318acc1a9efSDag-Erling Smørgrav 	  (u_int16_t)(((const u_char *)(p))[1]))
319a0ee8cc6SDag-Erling Smørgrav 
320a0ee8cc6SDag-Erling Smørgrav #define POKE_U64(p, v) \
321a0ee8cc6SDag-Erling Smørgrav 	do { \
322acc1a9efSDag-Erling Smørgrav 		const u_int64_t __v = (v); \
323acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[0] = (__v >> 56) & 0xff; \
324acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[1] = (__v >> 48) & 0xff; \
325acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[2] = (__v >> 40) & 0xff; \
326acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[3] = (__v >> 32) & 0xff; \
327acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[4] = (__v >> 24) & 0xff; \
328acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[5] = (__v >> 16) & 0xff; \
329acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[6] = (__v >> 8) & 0xff; \
330acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[7] = __v & 0xff; \
331a0ee8cc6SDag-Erling Smørgrav 	} while (0)
332a0ee8cc6SDag-Erling Smørgrav #define POKE_U32(p, v) \
333a0ee8cc6SDag-Erling Smørgrav 	do { \
334acc1a9efSDag-Erling Smørgrav 		const u_int32_t __v = (v); \
335acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[0] = (__v >> 24) & 0xff; \
336acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[1] = (__v >> 16) & 0xff; \
337acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[2] = (__v >> 8) & 0xff; \
338acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[3] = __v & 0xff; \
339a0ee8cc6SDag-Erling Smørgrav 	} while (0)
340a0ee8cc6SDag-Erling Smørgrav #define POKE_U16(p, v) \
341a0ee8cc6SDag-Erling Smørgrav 	do { \
342acc1a9efSDag-Erling Smørgrav 		const u_int16_t __v = (v); \
343acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[0] = (__v >> 8) & 0xff; \
344acc1a9efSDag-Erling Smørgrav 		((u_char *)(p))[1] = __v & 0xff; \
345a0ee8cc6SDag-Erling Smørgrav 	} while (0)
346a0ee8cc6SDag-Erling Smørgrav 
347a0ee8cc6SDag-Erling Smørgrav /* Internal definitions follow. Exposed for regress tests */
348a0ee8cc6SDag-Erling Smørgrav #ifdef SSHBUF_INTERNAL
349a0ee8cc6SDag-Erling Smørgrav 
350a0ee8cc6SDag-Erling Smørgrav /*
351a0ee8cc6SDag-Erling Smørgrav  * Return the allocation size of buf
352a0ee8cc6SDag-Erling Smørgrav  */
353a0ee8cc6SDag-Erling Smørgrav size_t	sshbuf_alloc(const struct sshbuf *buf);
354a0ee8cc6SDag-Erling Smørgrav 
355a0ee8cc6SDag-Erling Smørgrav /*
356a0ee8cc6SDag-Erling Smørgrav  * Increment the reference count of buf.
357a0ee8cc6SDag-Erling Smørgrav  */
358a0ee8cc6SDag-Erling Smørgrav int	sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent);
359a0ee8cc6SDag-Erling Smørgrav 
360a0ee8cc6SDag-Erling Smørgrav /*
361a0ee8cc6SDag-Erling Smørgrav  * Return the parent buffer of buf, or NULL if it has no parent.
362a0ee8cc6SDag-Erling Smørgrav  */
363a0ee8cc6SDag-Erling Smørgrav const struct sshbuf *sshbuf_parent(const struct sshbuf *buf);
364a0ee8cc6SDag-Erling Smørgrav 
365a0ee8cc6SDag-Erling Smørgrav /*
366a0ee8cc6SDag-Erling Smørgrav  * Return the reference count of buf
367a0ee8cc6SDag-Erling Smørgrav  */
368a0ee8cc6SDag-Erling Smørgrav u_int	sshbuf_refcount(const struct sshbuf *buf);
369a0ee8cc6SDag-Erling Smørgrav 
370a0ee8cc6SDag-Erling Smørgrav # define SSHBUF_SIZE_INIT	256		/* Initial allocation */
371a0ee8cc6SDag-Erling Smørgrav # define SSHBUF_SIZE_INC	256		/* Preferred increment length */
3721323ec57SEd Maste # define SSHBUF_PACK_MIN	8192		/* Minimum packable offset */
373a0ee8cc6SDag-Erling Smørgrav 
374a0ee8cc6SDag-Erling Smørgrav /* # define SSHBUF_ABORT abort */
375a0ee8cc6SDag-Erling Smørgrav /* # define SSHBUF_DEBUG */
376a0ee8cc6SDag-Erling Smørgrav 
377a0ee8cc6SDag-Erling Smørgrav # ifndef SSHBUF_ABORT
378a0ee8cc6SDag-Erling Smørgrav #  define SSHBUF_ABORT()
379a0ee8cc6SDag-Erling Smørgrav # endif
380a0ee8cc6SDag-Erling Smørgrav 
381a0ee8cc6SDag-Erling Smørgrav # ifdef SSHBUF_DEBUG
382a0ee8cc6SDag-Erling Smørgrav #  define SSHBUF_DBG(x) do { \
383a0ee8cc6SDag-Erling Smørgrav 		printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \
384a0ee8cc6SDag-Erling Smørgrav 		printf x; \
385a0ee8cc6SDag-Erling Smørgrav 		printf("\n"); \
386a0ee8cc6SDag-Erling Smørgrav 		fflush(stdout); \
387a0ee8cc6SDag-Erling Smørgrav 	} while (0)
388a0ee8cc6SDag-Erling Smørgrav # else
389a0ee8cc6SDag-Erling Smørgrav #  define SSHBUF_DBG(x)
390a0ee8cc6SDag-Erling Smørgrav # endif
391a0ee8cc6SDag-Erling Smørgrav #endif /* SSHBUF_INTERNAL */
392a0ee8cc6SDag-Erling Smørgrav 
393a0ee8cc6SDag-Erling Smørgrav #endif /* _SSHBUF_H */
394