1 #include "wire.h"
2 #include <assert.h>
3 #include <ccan/crypto/siphash24/siphash24.h>
4 #include <ccan/endian/endian.h>
5 #include <ccan/mem/mem.h>
6 #include <common/utils.h>
7 
towire(u8 ** pptr,const void * data,size_t len)8 void towire(u8 **pptr, const void *data, size_t len)
9 {
10 	size_t oldsize = tal_count(*pptr);
11 
12 	tal_resize(pptr, oldsize + len);
13 	memcpy(*pptr + oldsize, memcheck(data, len), len);
14 }
15 
towire_u8(u8 ** pptr,u8 v)16 void towire_u8(u8 **pptr, u8 v)
17 {
18 	towire(pptr, &v, sizeof(v));
19 }
20 
towire_u16(u8 ** pptr,u16 v)21 void towire_u16(u8 **pptr, u16 v)
22 {
23 	be16 l = cpu_to_be16(v);
24 	towire(pptr, &l, sizeof(l));
25 }
26 
towire_u32(u8 ** pptr,u32 v)27 void towire_u32(u8 **pptr, u32 v)
28 {
29 	be32 l = cpu_to_be32(v);
30 	towire(pptr, &l, sizeof(l));
31 }
32 
towire_u64(u8 ** pptr,u64 v)33 void towire_u64(u8 **pptr, u64 v)
34 {
35 	be64 l = cpu_to_be64(v);
36 	towire(pptr, &l, sizeof(l));
37 }
38 
towire_tlv_uint(u8 ** pptr,u64 v)39 static void towire_tlv_uint(u8 **pptr, u64 v)
40 {
41 	u8 bytes[8];
42 	size_t num_zeroes;
43 	be64 val;
44 
45 	val = cpu_to_be64(v);
46 	BUILD_ASSERT(sizeof(val) == sizeof(bytes));
47 	memcpy(bytes, &val, sizeof(bytes));
48 
49 	for (num_zeroes = 0; num_zeroes < sizeof(bytes); num_zeroes++)
50 		if (bytes[num_zeroes] != 0)
51 			break;
52 
53 	towire(pptr, bytes + num_zeroes, sizeof(bytes) - num_zeroes);
54 }
55 
towire_tu16(u8 ** pptr,u16 v)56 void towire_tu16(u8 **pptr, u16 v)
57 {
58 	return towire_tlv_uint(pptr, v);
59 }
60 
towire_tu32(u8 ** pptr,u32 v)61 void towire_tu32(u8 **pptr, u32 v)
62 {
63 	return towire_tlv_uint(pptr, v);
64 }
65 
towire_tu64(u8 ** pptr,u64 v)66 void towire_tu64(u8 **pptr, u64 v)
67 {
68 	return towire_tlv_uint(pptr, v);
69 }
70 
towire_bool(u8 ** pptr,bool v)71 void towire_bool(u8 **pptr, bool v)
72 {
73 	u8 val = v;
74 	towire(pptr, &val, sizeof(val));
75 }
76 
towire_errcode_t(u8 ** pptr,errcode_t v)77 void towire_errcode_t(u8 **pptr, errcode_t v)
78 {
79 	towire_u32(pptr, (u32)v);
80 }
81 
towire_secp256k1_ecdsa_signature(u8 ** pptr,const secp256k1_ecdsa_signature * sig)82 void towire_secp256k1_ecdsa_signature(u8 **pptr,
83 				      const secp256k1_ecdsa_signature *sig)
84 {
85 	u8 compact[64];
86 
87 	secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx,
88 						    compact, sig);
89 	towire(pptr, compact, sizeof(compact));
90 }
91 
towire_secp256k1_ecdsa_recoverable_signature(u8 ** pptr,const secp256k1_ecdsa_recoverable_signature * rsig)92 void towire_secp256k1_ecdsa_recoverable_signature(u8 **pptr,
93 			const secp256k1_ecdsa_recoverable_signature *rsig)
94 {
95 	u8 compact[64];
96 	int recid;
97 
98 	secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_ctx,
99 								compact,
100 								&recid,
101 								rsig);
102 	towire(pptr, compact, sizeof(compact));
103 	towire_u8(pptr, recid);
104 }
105 
towire_sha256(u8 ** pptr,const struct sha256 * sha256)106 void towire_sha256(u8 **pptr, const struct sha256 *sha256)
107 {
108 	towire(pptr, sha256, sizeof(*sha256));
109 }
110 
towire_ripemd160(u8 ** pptr,const struct ripemd160 * ripemd)111 void towire_ripemd160(u8 **pptr, const struct ripemd160 *ripemd)
112 {
113 	towire(pptr, ripemd, sizeof(*ripemd));
114 }
115 
towire_u8_array(u8 ** pptr,const u8 * arr,size_t num)116 void towire_u8_array(u8 **pptr, const u8 *arr, size_t num)
117 {
118 	towire(pptr, arr, num);
119 }
120 
towire_utf8_array(u8 ** pptr,const char * arr,size_t num)121 void towire_utf8_array(u8 **pptr, const char *arr, size_t num)
122 {
123 	assert(utf8_check(arr, num));
124 	towire(pptr, arr, num);
125 }
126 
towire_pad(u8 ** pptr,size_t num)127 void towire_pad(u8 **pptr, size_t num)
128 {
129 	/* Simply insert zeros. */
130 	size_t oldsize = tal_count(*pptr);
131 
132 	tal_resize(pptr, oldsize + num);
133 	memset(*pptr + oldsize, 0, num);
134 }
135 
towire_wirestring(u8 ** pptr,const char * str)136 void towire_wirestring(u8 **pptr, const char *str)
137 {
138 	towire(pptr, str, strlen(str) + 1);
139 }
140 
towire_siphash_seed(u8 ** pptr,const struct siphash_seed * seed)141 void towire_siphash_seed(u8 **pptr, const struct siphash_seed *seed)
142 {
143 	towire(pptr, seed, sizeof(*seed));
144 }
145