xref: /openbmc/qemu/tests/unit/test-crypto-der.c (revision 3b34ccad)
199d423f1SLei He /*
299d423f1SLei He  * QEMU Crypto akcipher algorithms
399d423f1SLei He  *
499d423f1SLei He  * Copyright (c) 2022 Bytedance
599d423f1SLei He  * Author: lei he <helei@bytedance.com>
699d423f1SLei He  *
799d423f1SLei He  * This library is free software; you can redistribute it and/or
899d423f1SLei He  * modify it under the terms of the GNU Lesser General Public
999d423f1SLei He  * License as published by the Free Software Foundation; either
1099d423f1SLei He  * version 2.1 of the License, or (at your option) any later version.
1199d423f1SLei He  *
1299d423f1SLei He  * This library is distributed in the hope that it will be useful,
1399d423f1SLei He  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1499d423f1SLei He  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1599d423f1SLei He  * Lesser General Public License for more details.
1699d423f1SLei He  *
1799d423f1SLei He  * You should have received a copy of the GNU Lesser General Public
1899d423f1SLei He  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
1999d423f1SLei He  *
2099d423f1SLei He  */
2199d423f1SLei He 
2299d423f1SLei He #include "qemu/osdep.h"
2399d423f1SLei He #include "crypto/der.h"
2499d423f1SLei He 
2599d423f1SLei He /* rsa(512) private key, generated by openssl */
2699d423f1SLei He static const uint8_t test_rsa512_priv_key[] =
2799d423f1SLei He     "\x30\x82\x01\x39"      /* SEQUENCE, offset: 0, length: 313 */
2899d423f1SLei He     "\x02\x01\x00"          /* INTEGER, offset: 4, length: 1 */
2999d423f1SLei He     "\x02\x41"              /* INTEGER, offset: 7, length: 65 */
3099d423f1SLei He     "\x00\xb9\xe1\x22\xdb\x56\x2f\xb6\xf7\xf0\x0a\x87\x43\x07\x12\xdb"
3199d423f1SLei He     "\x6d\xb6\x2b\x41\x8d\x2c\x3c\xa5\xdd\x78\x9a\x8f\xab\x8e\xf2\x4a"
3299d423f1SLei He     "\xc8\x34\x0c\x12\x4f\x11\x90\xc6\xc2\xa5\xd0\xcd\xfb\xfc\x2c\x95"
3399d423f1SLei He     "\x56\x82\xdf\x39\xf3\x3b\x1d\x62\x26\x97\xb7\x93\x25\xc7\xec\x7e"
3499d423f1SLei He     "\xf7"
3599d423f1SLei He     "\x02\x03\x01\x00\x01"  /* INTEGER, offset: 74, length: 3 */
3699d423f1SLei He     "\x02\x40"              /* INTEGER, offset: 79, length: 64 */
3799d423f1SLei He     "\x1e\x80\xfe\xda\x65\xdb\x70\xb8\x61\x91\x28\xbf\x6c\x32\xc1\x05"
3899d423f1SLei He     "\xd1\x26\x6a\x1c\x83\xcc\xf4\x1f\x53\x42\x72\x1f\x62\x57\x0a\xc4"
3999d423f1SLei He     "\x66\x76\x30\x87\xb9\xb1\xb9\x6a\x63\xfd\x8f\x3e\xfc\x35\x3f\xd6"
4099d423f1SLei He     "\x2e\x6c\xc8\x70\x8a\x17\xc1\x28\x6a\xfe\x51\x56\xb3\x92\x6f\x09"
4199d423f1SLei He     "\x02\x21"              /* INTEGER, offset: 145, length: 33 */
4299d423f1SLei He     "\x00\xe3\x2e\x2d\x8d\xba\x1c\x34\x4c\x49\x9f\xc1\xa6\xdd\xd7\x13"
4399d423f1SLei He     "\x8d\x05\x48\xdd\xff\x5c\x30\xbc\x6b\xc4\x18\x9d\xfc\xa2\xd0\x9b"
4499d423f1SLei He     "\x4d"
4599d423f1SLei He     "\x02\x21"             /* INTEGER, offset: 180, length: 33 */
4699d423f1SLei He     "\x00\xd1\x75\xaf\x4b\xc6\x1a\xb0\x98\x14\x42\xae\x33\xf3\x44\xde"
4799d423f1SLei He     "\x21\xcb\x04\xda\xfb\x1e\x35\x92\xcd\x69\xc0\x83\x06\x83\x8e\x39"
4899d423f1SLei He     "\x53"
4999d423f1SLei He     "\x02\x20"             /* INTEGER, offset: 215, length: 32 */
5099d423f1SLei He     "\x68\x8d\x2a\xf7\xcb\xcc\x09\x21\x86\xcc\x98\x21\xc4\x7c\xa4\x09"
5199d423f1SLei He     "\xc5\x81\xd8\x71\x1a\x2b\x6f\xbb\xa4\xde\xb3\x6e\xbe\x3b\x85\x0d"
5299d423f1SLei He     "\x02\x20"             /* INTEGER, offset: 249, length: 32 */
5399d423f1SLei He     "\x64\x06\x0e\xef\xe0\x6a\x5e\x6a\x41\x42\x96\x6d\xb8\x7d\xea\x95"
5499d423f1SLei He     "\xb8\x9d\x58\xf5\x12\x38\x03\x22\x94\x9d\x99\xf4\x42\x5e\x68\x81"
5599d423f1SLei He     "\x02\x20"             /* INTEGER, offset: 283, length: 32 */
5699d423f1SLei He     "\x7f\x1d\x87\xe8\x55\x30\x75\xc7\x29\xec\xc9\x65\x76\x5a\x6a\xa3"
5799d423f1SLei He     "\x4a\x6e\xe1\x26\x65\xd1\x76\xd5\xb9\xd1\x8b\xa8\x73\xe2\x6a\x9e";
5899d423f1SLei He 
5999d423f1SLei He static const uint8_t test_rsa2048_priv_key[] =
6099d423f1SLei He     "\x30\x82\x04\xa6"          /* SEQUENCE, offset: 0, length 1190 */
6199d423f1SLei He     "\x02\x01\x00"              /* INTEGER, offset: 4, length: 1 */
6299d423f1SLei He     "\x02\x82\x01\x01"          /* INTEGER, offset: 7, length: 257 */
6399d423f1SLei He     "\x00\xd1\x48\xc2\xc1\x1d\x4f\x94\xf2\xbb\x9b\xe2\x2d\xe1\xea\x4c"
6499d423f1SLei He     "\xce\x41\x72\xe3\x41\x7e\x9d\x91\x85\xa3\x4e\xe1\x2c\xf6\x52\x6d"
6599d423f1SLei He     "\xf9\x84\x64\xdf\x87\x28\x4a\xc9\x9d\x78\x93\x47\xc8\xd9\x66\x2e"
6699d423f1SLei He     "\xf4\xc6\xf0\x32\x15\x1a\xe8\xaf\x5a\xca\x3a\xd3\x3e\xf6\xde\x86"
6799d423f1SLei He     "\xdd\x9b\xa6\x4d\x74\x58\xf0\x11\x7f\x66\xd5\x1c\xd8\xde\xa3\xf8"
6899d423f1SLei He     "\xa3\xfc\x33\x55\x89\xa9\xc3\xea\x5b\x2e\x31\x06\xf8\xcb\x9e\x6e"
6999d423f1SLei He     "\xb2\x68\x0d\xe6\xc3\x5c\x2d\xf8\xa2\xbd\x00\x1a\xf6\xb6\xdd\x14"
7099d423f1SLei He     "\x8d\x11\x6d\x2d\xc6\x0c\x09\xe6\xf6\xb9\x8b\x87\x4c\x9f\x4d\x63"
7199d423f1SLei He     "\xd3\x94\xf4\x32\xca\xcf\x5e\xbf\xe2\x7f\x73\x5a\x65\xec\x82\x0d"
7299d423f1SLei He     "\x7f\x30\x25\x03\xd4\x3a\xff\xa2\xe8\xd6\xb5\x1f\x4f\x36\x64\x61"
7399d423f1SLei He     "\xc3\x5f\xb2\x9e\x0c\x53\x04\x19\x34\x99\xe8\xe3\xe6\xd3\x2f\x45"
7499d423f1SLei He     "\x58\x8e\x5d\x54\x5a\xa0\xc0\x5e\x51\x9b\x22\x15\xec\x26\x6f\x72"
7599d423f1SLei He     "\x68\xe9\xbf\x5d\x1d\xb5\xd9\xe4\x81\x1a\x92\x66\xa8\xcb\x73\x46"
7699d423f1SLei He     "\xab\x96\x7b\xf8\x9c\xf5\xb5\x9e\x2b\x13\x71\xe0\x01\x0c\x59\x1b"
7799d423f1SLei He     "\x63\x9f\xb7\xd1\xcd\x47\x8e\xc7\x3a\xbe\xcb\x47\xa7\x23\x43\xa7"
7899d423f1SLei He     "\x7d\xbd\x2c\x4e\x22\x37\xcc\xf9\x1b\x1b\xbb\xed\xec\xf0\x47\x92"
7999d423f1SLei He     "\x43"
8099d423f1SLei He     "\x02\x03\x01\x00\x01"      /* INTEGER, offset 268, length 3 */
8199d423f1SLei He     "\x02\x82\x01\x01"          /* INTEGER, offset 273, length 257 */
8299d423f1SLei He     "\x00\x8d\x21\x97\x0c\x29\x9a\xf8\x23\xf4\x76\x3b\xc1\x9b\x3e\xa8"
8399d423f1SLei He     "\x8a\xd2\xc2\x0a\x14\xa9\xb0\xd2\x68\x9f\x67\x5b\x1c\x3a\x03\xfe"
8499d423f1SLei He     "\x5b\xac\x77\x65\xf1\xbc\x2f\x2a\xe5\x01\x61\xb8\x9f\xee\x53\x25"
8599d423f1SLei He     "\x49\x36\x3a\xd6\x5b\x3b\x29\x3c\xcf\x69\xde\xdf\x83\xef\x70\xc2"
8699d423f1SLei He     "\xdc\x00\xd1\xd6\x1b\xa6\xba\x45\xe2\x77\x53\x31\xbf\xe1\xec\x0b"
8799d423f1SLei He     "\x89\x72\x52\x9f\xd5\x54\xe1\x64\x52\x16\xc5\x43\x21\x56\x16\xc2"
8899d423f1SLei He     "\x29\x97\x58\x00\x8d\x2f\xc5\x64\x8d\x42\x0d\x27\x21\xc6\xd1\x31"
8999d423f1SLei He     "\xc1\xab\xc5\xc7\x7f\x6d\xb0\xe3\xca\xef\xf6\xf2\xc7\xae\x09\xbf"
9099d423f1SLei He     "\x4d\xc0\x4e\x90\x2c\x28\xb9\xcc\x22\x74\xf2\xd5\xff\x4d\x86\xf6"
9199d423f1SLei He     "\xec\x45\x1f\xbf\x25\x4c\x30\x26\x76\x4f\x09\x13\x83\xef\x35\x73"
9299d423f1SLei He     "\xa3\xa2\xb1\x40\xcf\x07\x7a\x83\xae\xea\x00\xea\x74\xc7\x54\x6a"
9399d423f1SLei He     "\x88\x19\xed\x35\xd3\x7e\x5e\xac\x51\xc1\x1e\x5e\x2c\x57\x72\x20"
9499d423f1SLei He     "\x10\x6a\x0c\x47\xe1\xf0\x36\x70\xd2\xa7\x57\x64\x47\x46\x9f\xca"
9599d423f1SLei He     "\x23\x8a\x48\x50\x1d\x33\x6a\x86\x46\x69\xed\x54\x65\x6b\x9e\xab"
9699d423f1SLei He     "\x1f\x84\x87\xf4\x92\x8a\x6c\x44\x20\xaa\x8d\xd8\x50\xde\x45\x74"
9799d423f1SLei He     "\xe0\xa8\xc7\xb9\x38\x74\x24\x51\x33\xf0\x39\x54\x6c\x11\xae\xc2"
9899d423f1SLei He     "\x29"
9999d423f1SLei He     "\x02\x81\x81"              /* INTEGER, offset 534, length 129 */
10099d423f1SLei He     "\x00\xe8\x26\xd1\xf9\xa0\xd3\x0e\x3f\x2f\x89\x9b\x94\x16\x12\xd1"
10199d423f1SLei He     "\xae\x3c\x53\x9c\xcf\xc6\xf7\x03\xf5\xdf\x39\xdc\x25\x5d\xcb\xb8"
10299d423f1SLei He     "\xb9\x74\x3e\x3b\x36\xf6\xa0\x8d\xb1\x0e\xd8\xfe\x8c\xcd\x01\x13"
10399d423f1SLei He     "\x77\x73\x08\x0f\x32\xbd\xe6\x95\xdc\xd0\x14\x7d\x44\xdc\x3e\xd9"
10499d423f1SLei He     "\xaa\x8a\x32\xe6\x0e\x76\xb6\x05\xc5\x6b\x87\x78\x9a\x32\xe2\xf8"
10599d423f1SLei He     "\x78\xba\x58\x75\x58\xd5\x26\x9d\x9a\x0f\xb6\xca\xb5\x27\xd8\x58"
10699d423f1SLei He     "\xae\x3f\x49\x54\xd2\x2b\xac\x28\x39\x88\x31\x42\x12\x08\xea\x0b"
10799d423f1SLei He     "\x39\x58\xae\xf3\x82\xa0\xe2\x75\x7c\x96\xa9\xb8\x57\x29\x6d\xd7"
10899d423f1SLei He     "\x37"
10999d423f1SLei He     "\x02\x81\x81"              /* INTEGER, offset 666, length 129 */
11099d423f1SLei He     "\x00\xe6\xc8\x91\x50\x49\x97\x56\x70\x6e\x25\xf5\x77\x25\xa5\x41"
11199d423f1SLei He     "\xfe\xd7\x25\x1b\xc1\x4a\xff\x37\x44\x2b\x46\xa0\xdf\xe8\x02\x09"
11299d423f1SLei He     "\xdd\xa8\x41\xa1\x12\x84\x3c\xf8\xc2\x13\x3e\xb8\x4b\x22\x01\xac"
11399d423f1SLei He     "\xa6\x09\xb2\xe9\xcd\xc8\x51\xee\xde\xa3\x1e\x6b\xfe\xb1\xf8\xb6"
11499d423f1SLei He     "\x9e\x48\x36\x62\x0b\x05\xfa\x38\xc1\x06\x04\x58\x95\x4d\x25\x13"
11599d423f1SLei He     "\x6d\x0b\x12\x0b\xc9\x6d\x59\xfc\x33\x03\x36\x01\x12\x09\x72\x74"
11699d423f1SLei He     "\x5e\x98\x65\x66\x2f\x3a\xde\xd8\xd4\xee\x6f\x82\xe6\x36\x49\x12"
11799d423f1SLei He     "\x6a\x94\x28\xe9\x28\x9e\xef\x29\xdc\xdf\xab\x94\x65\x02\x4e\x4b"
11899d423f1SLei He     "\x55"
11999d423f1SLei He     "\x02\x81\x81"              /* INTEGER, offset 798, length 129 */
12099d423f1SLei He     "\x00\xc9\xda\xb7\x48\x6e\x66\x15\x45\x2b\x78\x63\x26\x67\xeb\x05"
12199d423f1SLei He     "\x16\x92\xad\xc0\xf3\x88\xf4\xcf\x24\xc2\x6b\xf4\xd7\x28\xaf\x32"
12299d423f1SLei He     "\x77\x4e\x73\xad\xd9\x24\xa8\x85\x8b\x26\x75\xd7\x1f\x66\x41\x41"
12399d423f1SLei He     "\x43\xe3\x69\x66\x8d\xa0\x41\x16\x9d\x60\xef\xef\xdc\x28\x05\x1e"
12499d423f1SLei He     "\x0e\x03\x0c\x2e\xac\xf4\xdb\x60\x39\x40\x3e\x12\xc7\x40\xe7\xc9"
12599d423f1SLei He     "\x54\x6f\xf2\xea\x55\xcb\x40\x40\x58\xec\xc0\xeb\x90\x88\x8c\xbc"
12699d423f1SLei He     "\xcf\x05\x88\x25\x90\x79\x18\xc0\x01\x06\x42\x8e\x48\x50\x27\xf0"
12799d423f1SLei He     "\x8a\x74\x69\xea\xa1\xf2\x71\xf5\xe5\xd6\xba\xcb\xe6\x3d\xc7\x9c"
12899d423f1SLei He     "\x11"
12999d423f1SLei He      "\x02\x81\x81"              /* INTEGER, offset 930, length 129 */
13099d423f1SLei He     "\x00\xc9\xf5\x04\xad\x34\xe9\x39\xdc\x83\x97\xb6\x3a\x40\xf8\x60"
13199d423f1SLei He     "\x4b\x69\xec\xf0\x5f\xf3\x88\x69\xcd\xbe\xed\x3c\xc5\x14\x5c\x0c"
13299d423f1SLei He     "\x54\x2b\xf4\xda\xc6\xc0\x70\x36\xe4\x67\x41\x00\xb7\xc7\x17\x9e"
13399d423f1SLei He     "\x05\x63\x01\x6d\x77\x06\x71\x24\xcf\x32\x01\xe2\x51\xed\x5e\x90"
13499d423f1SLei He     "\x38\xed\x4a\xa1\xfb\xb1\x8c\x69\xf4\x08\x96\xef\x0a\x20\x8b\x6c"
13599d423f1SLei He     "\x77\x85\x33\x92\x9a\xff\x95\xba\x8c\xcd\xa7\x89\xc2\x46\x00\x21"
13699d423f1SLei He     "\xf3\xd1\xfb\x12\x34\x0c\x99\x8d\x38\xb1\x3b\x66\x5a\x9d\x70\xce"
13799d423f1SLei He     "\xab\xf3\xe1\xe5\x40\x05\xed\x97\x3d\xd1\x82\x6e\x07\x02\xc0\x8f"
13899d423f1SLei He     "\x4d"
13999d423f1SLei He     "\x02\x81\x81"              /* INTEGER, offset 1062, length 129 */
14099d423f1SLei He     "\x00\xe4\x96\x79\xa8\x6a\x70\xdd\x67\x42\xff\x15\x11\x9e\x01\x71"
14199d423f1SLei He     "\xac\xf1\x70\x7d\x87\xe2\x6e\x0c\x4d\xbb\x21\x15\xbb\xa7\x4e\x0c"
14299d423f1SLei He     "\x09\x7e\x82\xca\x91\xbe\xd0\xdd\x9c\x8c\xb0\x77\x64\x30\x1b\x7e"
14399d423f1SLei He     "\xbb\x69\xcb\x4c\xde\xd6\x6a\xb9\x72\x15\x79\xdc\x05\x99\x69\x8b"
14499d423f1SLei He     "\x24\xa1\xad\x13\x35\x31\xc0\x0b\xf1\xd2\x06\x7c\x94\x1a\x21\x2f"
14599d423f1SLei He     "\x02\xb9\xf0\xd0\xbb\xf7\xb7\x78\xf9\x3d\x76\x60\xd6\x6b\x5f\x35"
14699d423f1SLei He     "\x88\x14\x33\xe6\xbc\xca\x6b\x88\x90\x57\x3b\x0c\xa3\x6e\x47\xdf"
14799d423f1SLei He     "\x4e\x2f\x4c\xf9\xab\x97\x38\xe4\x20\x32\x32\x96\xc8\x9e\x79\xd3"
14899d423f1SLei He     "\x12";
14999d423f1SLei He 
150*3b34ccadSLei He static const uint8_t test_ecdsa_p192_priv_key[] =
151*3b34ccadSLei He     "\x30\x53"               /* SEQUENCE, offset 0, length 83 */
152*3b34ccadSLei He     "\x02\x01\x01"           /* INTEGER, offset 2, length 1 */
153*3b34ccadSLei He     "\x04\x18"               /* OCTET STRING, offset 5, length 24 */
154*3b34ccadSLei He     "\xcb\xc8\x86\x0e\x66\x3c\xf7\x5a\x44\x13\xb8\xef\xea\x1d\x7b\xa6"
155*3b34ccadSLei He     "\x1c\xda\xf4\x1b\xc7\x67\x6b\x35"
156*3b34ccadSLei He     "\xa1\x34"               /* CONTEXT SPECIFIC 1, offset 31, length 52 */
157*3b34ccadSLei He     "\x03\x32"               /* BIT STRING, offset 33, length 50 */
158*3b34ccadSLei He     "\x00\x04\xc4\x16\xb3\xff\xac\xd5\x87\x98\xf7\xd9\x45\xfe\xd3\x5c"
159*3b34ccadSLei He     "\x17\x9d\xb2\x36\x22\xcc\x07\xb3\x6d\x3c\x4e\x04\x5f\xeb\xb6\x52"
160*3b34ccadSLei He     "\x58\xfb\x36\x10\x52\xb7\x01\x62\x0e\x94\x51\x1d\xe2\xef\x10\x82"
161*3b34ccadSLei He     "\x88\x78";
162*3b34ccadSLei He 
163*3b34ccadSLei He static const uint8_t test_ecdsa_p256_priv_key[] =
164*3b34ccadSLei He     "\x30\x77"              /* SEQUENCE, offset 0, length 119 */
165*3b34ccadSLei He     "\x02\x01\x01"          /* INTEGER, offset 2, length 1 */
166*3b34ccadSLei He     "\x04\x20"              /* OCTET STRING, offset 5, length 32 */
167*3b34ccadSLei He     "\xf6\x92\xdd\x29\x1c\x6e\xef\xb6\xb2\x73\x9f\x40\x1b\xb3\x2a\x28"
168*3b34ccadSLei He     "\xd2\x37\xd6\x4a\x5b\xe4\x40\x4c\x6a\x95\x99\xfa\xf7\x92\x49\xbe"
169*3b34ccadSLei He     "\xa0\x0a"              /* CONTEXT SPECIFIC 0, offset 39, length 10 */
170*3b34ccadSLei He     "\x06\x08"              /* OID, offset 41, length 8 */
171*3b34ccadSLei He     "\x2a\x86\x48\xce\x3d\x03\x01\x07"
172*3b34ccadSLei He     "\xa1\x44"              /* CONTEXT SPECIFIC 1, offset 51, length 68 */
173*3b34ccadSLei He     "\x03\x42"              /* BIT STRING, offset 53, length 66 */
174*3b34ccadSLei He     "\x00\x04\xed\x42\x9c\x67\x79\xbe\x46\x83\x88\x3e\x8c\xc1\x33\xf3"
175*3b34ccadSLei He     "\xc3\xf6\x2c\xf3\x13\x6a\x00\xc2\xc9\x3e\x87\x7f\x86\x39\xe6\xae"
176*3b34ccadSLei He     "\xe3\xb9\xba\x2f\x58\x63\x32\x62\x62\x54\x07\x27\xf9\x5a\x3a\xc7"
177*3b34ccadSLei He     "\x3a\x6b\x5b\xbc\x0d\x33\xba\xbb\xd4\xa3\xff\x4f\x9e\xdd\xf5\x59"
178*3b34ccadSLei He     "\xc0\xf6";
179*3b34ccadSLei He 
18099d423f1SLei He #define MAX_CHECKER_COUNT 32
18199d423f1SLei He 
qcrypto_wrapped_decode_ctx_tag0(const uint8_t ** data,size_t * dlen,QCryptoDERDecodeCb cb,void * opaque,Error ** errp)182*3b34ccadSLei He static int qcrypto_wrapped_decode_ctx_tag0(const uint8_t **data, size_t *dlen,
183*3b34ccadSLei He                                            QCryptoDERDecodeCb cb, void *opaque,
184*3b34ccadSLei He                                            Error **errp)
185*3b34ccadSLei He {
186*3b34ccadSLei He    return qcrypto_der_decode_ctx_tag(data, dlen, 0, cb, opaque, errp);
187*3b34ccadSLei He }
188*3b34ccadSLei He 
qcrypto_wrapped_decode_ctx_tag1(const uint8_t ** data,size_t * dlen,QCryptoDERDecodeCb cb,void * opaque,Error ** errp)189*3b34ccadSLei He static int qcrypto_wrapped_decode_ctx_tag1(const uint8_t **data, size_t *dlen,
190*3b34ccadSLei He                                            QCryptoDERDecodeCb cb, void *opaque,
191*3b34ccadSLei He                                            Error **errp)
192*3b34ccadSLei He {
193*3b34ccadSLei He    return qcrypto_der_decode_ctx_tag(data, dlen, 1, cb, opaque, errp);
194*3b34ccadSLei He }
195*3b34ccadSLei He 
19699d423f1SLei He typedef struct QCryptoAns1DecoderResultChecker QCryptoAns1DecoderResultChecker;
19799d423f1SLei He struct QCryptoAns1DecoderResultChecker {
19899d423f1SLei He     int (*action) (const uint8_t **data, size_t *dlen,
19999d423f1SLei He                    QCryptoDERDecodeCb cb, void *opaque, Error **errp);
20099d423f1SLei He     QCryptoDERDecodeCb cb;
201*3b34ccadSLei He     bool constructed;
20299d423f1SLei He     const uint8_t *exp_value;
20399d423f1SLei He     size_t exp_vlen;
20499d423f1SLei He };
20599d423f1SLei He 
20699d423f1SLei He typedef struct QCryptoAns1DecoderTestData QCryptoAns1DecoderTestData;
20799d423f1SLei He struct QCryptoAns1DecoderTestData {
20899d423f1SLei He     const char *path;
20999d423f1SLei He     const uint8_t *test_data;
21099d423f1SLei He     size_t test_data_len;
21199d423f1SLei He     QCryptoAns1DecoderResultChecker checker[MAX_CHECKER_COUNT];
21299d423f1SLei He };
21399d423f1SLei He 
21499d423f1SLei He typedef struct QCryptoAns1DecoderTestContext QCryptoAns1DecoderTestContext;
21599d423f1SLei He struct QCryptoAns1DecoderTestContext {
21699d423f1SLei He     const uint8_t *data;
21799d423f1SLei He     size_t dlen;
21899d423f1SLei He };
21999d423f1SLei He 
checker_callback(void * opaque,const uint8_t * value,size_t vlen,Error ** errp)22099d423f1SLei He static int checker_callback(void *opaque, const uint8_t *value,
22199d423f1SLei He                             size_t vlen, Error **errp)
22299d423f1SLei He {
22399d423f1SLei He     QCryptoAns1DecoderResultChecker *checker =
22499d423f1SLei He         (QCryptoAns1DecoderResultChecker *)opaque;
22599d423f1SLei He 
22699d423f1SLei He     g_assert(value == checker->exp_value);
22799d423f1SLei He     g_assert(vlen == checker->exp_vlen);
22899d423f1SLei He     return 0;
22999d423f1SLei He }
23099d423f1SLei He 
test_ans1(const void * opaque)23199d423f1SLei He static void test_ans1(const void *opaque)
23299d423f1SLei He {
23399d423f1SLei He     const QCryptoAns1DecoderTestData *test_data =
23499d423f1SLei He         (QCryptoAns1DecoderTestData *)opaque;
23599d423f1SLei He     QCryptoAns1DecoderTestContext ctx[MAX_CHECKER_COUNT];
23699d423f1SLei He     int seq_depth = 0, checker_idx = 0;
23799d423f1SLei He     ctx[seq_depth].data = test_data->test_data;
23899d423f1SLei He     ctx[seq_depth].dlen = test_data->test_data_len;
23999d423f1SLei He     bool all_checker_completed = false;
24099d423f1SLei He 
24199d423f1SLei He     do {
24299d423f1SLei He         const QCryptoAns1DecoderResultChecker *checker =
24399d423f1SLei He             &test_data->checker[checker_idx++];
24499d423f1SLei He         QCryptoAns1DecoderTestContext *c = &ctx[seq_depth];
24599d423f1SLei He         if (!checker->action) {
24699d423f1SLei He             all_checker_completed = true;
24799d423f1SLei He             break;
24899d423f1SLei He         }
24999d423f1SLei He         g_assert(checker->action(&c->data, &c->dlen, checker_callback,
25099d423f1SLei He                                  (void *)checker, &error_abort)
25199d423f1SLei He             == checker->exp_vlen);
252*3b34ccadSLei He         if (checker->constructed) {
25399d423f1SLei He             ++seq_depth;
25499d423f1SLei He             ctx[seq_depth].data = checker->exp_value;
25599d423f1SLei He             ctx[seq_depth].dlen = checker->exp_vlen;
25699d423f1SLei He         }
25799d423f1SLei He         while (seq_depth != 0 && ctx[seq_depth].dlen == 0) {
25899d423f1SLei He             --seq_depth;
25999d423f1SLei He         }
26099d423f1SLei He 
26199d423f1SLei He     } while (true);
26299d423f1SLei He     g_assert(seq_depth == 0);
26399d423f1SLei He     g_assert(ctx[seq_depth].dlen == 0);
26499d423f1SLei He     g_assert(all_checker_completed);
26599d423f1SLei He }
26699d423f1SLei He 
26799d423f1SLei He static QCryptoAns1DecoderTestData test_data[] = {
26899d423f1SLei He {
26999d423f1SLei He     .path = "/crypto/der/parse-rsa512-priv-key",
27099d423f1SLei He     .test_data = test_rsa512_priv_key,
27199d423f1SLei He     .test_data_len = sizeof(test_rsa512_priv_key) - 1,
27299d423f1SLei He     .checker = {
273*3b34ccadSLei He         { qcrypto_der_decode_seq, checker_callback, true,
27499d423f1SLei He           test_rsa512_priv_key + 4, 313 },
275*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
27699d423f1SLei He           test_rsa512_priv_key + 4 + 2, 1 },
277*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
27899d423f1SLei He           test_rsa512_priv_key + 7 + 2, 65 },
279*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
28099d423f1SLei He           test_rsa512_priv_key + 74 + 2, 3 },
281*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
28299d423f1SLei He           test_rsa512_priv_key + 79 + 2, 64 },
283*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
28499d423f1SLei He           test_rsa512_priv_key + 145 + 2, 33 },
285*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
28699d423f1SLei He           test_rsa512_priv_key + 180 + 2, 33 },
287*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
28899d423f1SLei He           test_rsa512_priv_key + 215 + 2, 32 },
289*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
29099d423f1SLei He           test_rsa512_priv_key + 249 + 2, 32 },
291*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
29299d423f1SLei He           test_rsa512_priv_key + 283 + 2, 32 },
29399d423f1SLei He     },
29499d423f1SLei He },
29599d423f1SLei He {
29699d423f1SLei He     .path = "/crypto/der/parse-rsa2048-priv-key",
29799d423f1SLei He     .test_data = test_rsa2048_priv_key,
29899d423f1SLei He     .test_data_len = sizeof(test_rsa2048_priv_key) - 1,
29999d423f1SLei He     .checker = {
300*3b34ccadSLei He         { qcrypto_der_decode_seq, checker_callback, true,
30199d423f1SLei He           test_rsa2048_priv_key + 4, 1190 },
302*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
30399d423f1SLei He           test_rsa2048_priv_key + 4 + 2, 1 },
304*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
30599d423f1SLei He           test_rsa2048_priv_key + 7 + 4, 257 },
306*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
30799d423f1SLei He           test_rsa2048_priv_key + 268 + 2, 3 },
308*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
30999d423f1SLei He           test_rsa2048_priv_key + 273 + 4, 257 },
310*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
31199d423f1SLei He           test_rsa2048_priv_key + 534 + 3, 129 },
312*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
31399d423f1SLei He           test_rsa2048_priv_key + 666 + 3, 129 },
314*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
31599d423f1SLei He           test_rsa2048_priv_key + 798 + 3, 129 },
316*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
31799d423f1SLei He           test_rsa2048_priv_key + 930 + 3, 129 },
318*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
31999d423f1SLei He           test_rsa2048_priv_key + 1062 + 3, 129 },
32099d423f1SLei He     },
32199d423f1SLei He },
322*3b34ccadSLei He {
323*3b34ccadSLei He     .path = "/crypto/der/parse-ecdsa-p192-priv-key",
324*3b34ccadSLei He     .test_data = test_ecdsa_p192_priv_key,
325*3b34ccadSLei He     .test_data_len = sizeof(test_ecdsa_p192_priv_key) - 1,
326*3b34ccadSLei He     .checker = {
327*3b34ccadSLei He         { qcrypto_der_decode_seq, checker_callback, true,
328*3b34ccadSLei He           test_ecdsa_p192_priv_key + 2, 83 },
329*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
330*3b34ccadSLei He           test_ecdsa_p192_priv_key + 2 + 2, 1 },
331*3b34ccadSLei He         { qcrypto_der_decode_octet_str, checker_callback, false,
332*3b34ccadSLei He           test_ecdsa_p192_priv_key + 5 + 2, 24 },
333*3b34ccadSLei He         { qcrypto_wrapped_decode_ctx_tag1, checker_callback, true,
334*3b34ccadSLei He           test_ecdsa_p192_priv_key + 31 + 2, 52 },
335*3b34ccadSLei He         { qcrypto_der_decode_bit_str , checker_callback, false,
336*3b34ccadSLei He           test_ecdsa_p192_priv_key + 33 + 2, 50 },
337*3b34ccadSLei He     },
338*3b34ccadSLei He },
339*3b34ccadSLei He {
340*3b34ccadSLei He     .path = "/crypto/der/parse-ecdsa-p256-priv-key",
341*3b34ccadSLei He     .test_data = test_ecdsa_p256_priv_key,
342*3b34ccadSLei He     .test_data_len = sizeof(test_ecdsa_p256_priv_key) - 1,
343*3b34ccadSLei He     .checker = {
344*3b34ccadSLei He         { qcrypto_der_decode_seq, checker_callback, true,
345*3b34ccadSLei He           test_ecdsa_p256_priv_key + 2, 119 },
346*3b34ccadSLei He         { qcrypto_der_decode_int, checker_callback, false,
347*3b34ccadSLei He           test_ecdsa_p256_priv_key + 2 + 2, 1 },
348*3b34ccadSLei He         { qcrypto_der_decode_octet_str, checker_callback, false,
349*3b34ccadSLei He           test_ecdsa_p256_priv_key + 5 + 2, 32 },
350*3b34ccadSLei He         { qcrypto_wrapped_decode_ctx_tag0, checker_callback, true,
351*3b34ccadSLei He           test_ecdsa_p256_priv_key + 39 + 2, 10 },
352*3b34ccadSLei He         { qcrypto_der_decode_oid, checker_callback, false,
353*3b34ccadSLei He           test_ecdsa_p256_priv_key + 41 + 2, 8 },
354*3b34ccadSLei He         { qcrypto_wrapped_decode_ctx_tag1, checker_callback, true,
355*3b34ccadSLei He           test_ecdsa_p256_priv_key + 51 + 2, 68 },
356*3b34ccadSLei He         { qcrypto_der_decode_bit_str , checker_callback, false,
357*3b34ccadSLei He           test_ecdsa_p256_priv_key + 53 + 2, 66 },
358*3b34ccadSLei He     },
359*3b34ccadSLei He },
36099d423f1SLei He };
36199d423f1SLei He 
main(int argc,char ** argv)36299d423f1SLei He int main(int argc, char **argv)
36399d423f1SLei He {
36499d423f1SLei He     size_t i;
36599d423f1SLei He     g_test_init(&argc, &argv, NULL);
36699d423f1SLei He 
36799d423f1SLei He     for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
36899d423f1SLei He         g_test_add_data_func(test_data[i].path, &test_data[i], test_ans1);
36999d423f1SLei He     }
37099d423f1SLei He 
37199d423f1SLei He     return g_test_run();
37299d423f1SLei He }
373