MCPcopy Create free account
hub / github.com/ElementsProject/lightning / IsValidSignatureEncoding

Function IsValidSignatureEncoding

bitcoin/signature.c:229–293  ·  view source on GitHub ↗

Stolen direct from bitcoin/src/script/sign.cpp: // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2014 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. */

Source from the content-addressed store, hash-verified

227// file COPYING or http://www.opensource.org/licenses/mit-license.php.
228*/
229static bool IsValidSignatureEncoding(const unsigned char sig[], size_t len)
230{
231 // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] [sighash]
232 // * total-length: 1-byte length descriptor of everything that follows,
233 // excluding the sighash byte.
234 // * R-length: 1-byte length descriptor of the R value that follows.
235 // * R: arbitrary-length big-endian encoded R value. It must use the shortest
236 // possible encoding for a positive integers (which means no null bytes at
237 // the start, except a single one when the next byte has its highest bit set).
238 // * S-length: 1-byte length descriptor of the S value that follows.
239 // * S: arbitrary-length big-endian encoded S value. The same rules apply.
240 // * sighash: 1-byte value indicating what data is hashed (not part of the DER
241 // signature)
242
243 // Minimum and maximum size constraints.
244 if (len < 9) return false;
245 if (len > 73) return false;
246
247 // A signature is of type 0x30 (compound).
248 if (sig[0] != 0x30) return false;
249
250 // Make sure the length covers the entire signature.
251 if (sig[1] != len - 3) return false;
252
253 // Extract the length of the R element.
254 unsigned int lenR = sig[3];
255
256 // Make sure the length of the S element is still inside the signature.
257 if (5 + lenR >= len) return false;
258
259 // Extract the length of the S element.
260 unsigned int lenS = sig[5 + lenR];
261
262 // Verify that the length of the signature matches the sum of the length
263 // of the elements.
264 if ((size_t)lenR + (size_t)lenS + 7 != len) return false;
265
266 // Check whether the R element is an integer.
267 if (sig[2] != 0x02) return false;
268
269 // Zero-length integers are not allowed for R.
270 if (lenR == 0) return false;
271
272 // Negative numbers are not allowed for R.
273 if (sig[4] & 0x80) return false;
274
275 // Null bytes at the start of R are not allowed, unless R would
276 // otherwise be interpreted as a negative number.
277 if (lenR > 1 && (sig[4] == 0x00) && !(sig[5] & 0x80)) return false;
278
279 // Check whether the S element is an integer.
280 if (sig[lenR + 4] != 0x02) return false;
281
282 // Zero-length integers are not allowed for S.
283 if (lenS == 0) return false;
284
285 // Negative numbers are not allowed for S.
286 if (sig[lenR + 6] & 0x80) return false;

Callers 1

signature_to_derFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected