Path: blob/main/contrib/bearssl/src/ec/ecdsa_rta.c
39488 views
/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/*27* Compute ASN.1 encoded length for the provided integer. The ASN.128* encoding is signed, so its leading bit must have value 0; it must29* also be of minimal length (so leading bytes of value 0 must be30* removed, except if that would contradict the rule about the sign31* bit).32*/33static size_t34asn1_int_length(const unsigned char *x, size_t xlen)35{36while (xlen > 0 && *x == 0) {37x ++;38xlen --;39}40if (xlen == 0 || *x >= 0x80) {41xlen ++;42}43return xlen;44}4546/* see bearssl_ec.h */47size_t48br_ecdsa_raw_to_asn1(void *sig, size_t sig_len)49{50/*51* Internal buffer is large enough to accommodate a signature52* such that r and s fit on 125 bytes each (signed encoding),53* meaning a curve order of up to 999 bits. This is the limit54* that ensures "simple" length encodings.55*/56unsigned char *buf;57size_t hlen, rlen, slen, zlen, off;58unsigned char tmp[257];5960buf = sig;61if ((sig_len & 1) != 0) {62return 0;63}6465/*66* Compute lengths for the two integers.67*/68hlen = sig_len >> 1;69rlen = asn1_int_length(buf, hlen);70slen = asn1_int_length(buf + hlen, hlen);71if (rlen > 125 || slen > 125) {72return 0;73}7475/*76* SEQUENCE header.77*/78tmp[0] = 0x30;79zlen = rlen + slen + 4;80if (zlen >= 0x80) {81tmp[1] = 0x81;82tmp[2] = zlen;83off = 3;84} else {85tmp[1] = zlen;86off = 2;87}8889/*90* First INTEGER (r).91*/92tmp[off ++] = 0x02;93tmp[off ++] = rlen;94if (rlen > hlen) {95tmp[off] = 0x00;96memcpy(tmp + off + 1, buf, hlen);97} else {98memcpy(tmp + off, buf + hlen - rlen, rlen);99}100off += rlen;101102/*103* Second INTEGER (s).104*/105tmp[off ++] = 0x02;106tmp[off ++] = slen;107if (slen > hlen) {108tmp[off] = 0x00;109memcpy(tmp + off + 1, buf + hlen, hlen);110} else {111memcpy(tmp + off, buf + sig_len - slen, slen);112}113off += slen;114115/*116* Return ASN.1 signature.117*/118memcpy(sig, tmp, off);119return off;120}121122123