Path: blob/main/contrib/bearssl/src/ec/ecdsa_atr.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/* see bearssl_ec.h */27size_t28br_ecdsa_asn1_to_raw(void *sig, size_t sig_len)29{30/*31* Note: this code is a bit lenient in that it accepts a few32* deviations to DER with regards to minimality of encoding of33* lengths and integer values. These deviations are still34* unambiguous.35*36* Signature format is a SEQUENCE of two INTEGER values. We37* support only integers of less than 127 bytes each (signed38* encoding) so the resulting raw signature will have length39* at most 254 bytes.40*/4142unsigned char *buf, *r, *s;43size_t zlen, rlen, slen, off;44unsigned char tmp[254];4546buf = sig;47if (sig_len < 8) {48return 0;49}5051/*52* First byte is SEQUENCE tag.53*/54if (buf[0] != 0x30) {55return 0;56}5758/*59* The SEQUENCE length will be encoded over one or two bytes. We60* limit the total SEQUENCE contents to 255 bytes, because it61* makes things simpler; this is enough for subgroup orders up62* to 999 bits.63*/64zlen = buf[1];65if (zlen > 0x80) {66if (zlen != 0x81) {67return 0;68}69zlen = buf[2];70if (zlen != sig_len - 3) {71return 0;72}73off = 3;74} else {75if (zlen != sig_len - 2) {76return 0;77}78off = 2;79}8081/*82* First INTEGER (r).83*/84if (buf[off ++] != 0x02) {85return 0;86}87rlen = buf[off ++];88if (rlen >= 0x80) {89return 0;90}91r = buf + off;92off += rlen;9394/*95* Second INTEGER (s).96*/97if (off + 2 > sig_len) {98return 0;99}100if (buf[off ++] != 0x02) {101return 0;102}103slen = buf[off ++];104if (slen >= 0x80 || slen != sig_len - off) {105return 0;106}107s = buf + off;108109/*110* Removing leading zeros from r and s.111*/112while (rlen > 0 && *r == 0) {113rlen --;114r ++;115}116while (slen > 0 && *s == 0) {117slen --;118s ++;119}120121/*122* Compute common length for the two integers, then copy integers123* into the temporary buffer, and finally copy it back over the124* signature buffer.125*/126zlen = rlen > slen ? rlen : slen;127sig_len = zlen << 1;128memset(tmp, 0, sig_len);129memcpy(tmp + zlen - rlen, r, rlen);130memcpy(tmp + sig_len - slen, s, slen);131memcpy(sig, tmp, sig_len);132return sig_len;133}134135136