Add ECDSA to providers
[openssl.git] / crypto / ec / ecdsa_aid.c
1 /*
2  * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdlib.h>
11
12 #include <openssl/objects.h>
13 #include "crypto/ec.h"
14
15 #define ASN1_SEQUENCE 0x30
16 #define ASN1_OID 0x06
17 #define OID_FIRST(a, b) a * 40 + b
18 #define DER_840() 0x86, 0x48    /* DER encoding of number 840 is 2 bytes */
19 #define DER_10045() 0xCE, 0x3D  /* DER encoding of number 10045 is 2 bytes */
20 #define SHA1_SZ 7
21 #define SHA2_SZ 8
22 #define SHA3_SZ 9
23
24 /*
25  * -- RFC 3279
26  * ansi-X9-62 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) 10045 }
27  * id-ecSigType OBJECT IDENTIFIER ::= { ansi-X9-62 signatures(4) }
28  *
29  * ecdsa-with-SHA1 OBJECT IDENTIFIER ::= { id-ecSigType 1 }
30  */
31 #define ENCODE_ALGORITHMIDENTIFIER_SHA1(name)                                  \
32 static const unsigned char algorithmidentifier_##name##_der[] = {              \
33     ASN1_SEQUENCE, 2 + SHA1_SZ,                                                \
34     ASN1_OID, SHA1_SZ, OID_FIRST(1, 2), DER_840(), DER_10045(), 4, 1           \
35 }
36
37 /*
38  * -- RFC 5758
39  *
40  * ecdsa-with-SHA224 OBJECT IDENTIFIER ::= { iso(1) member-body(2)
41  *      us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 1 }
42  *
43  * ecdsa-with-SHA256 OBJECT IDENTIFIER ::= { iso(1) member-body(2)
44  *      us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 2 }
45  *
46  * ecdsa-with-SHA384 OBJECT IDENTIFIER ::= { iso(1) member-body(2)
47  *      us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 3 }
48  *
49  * ecdsa-with-SHA512 OBJECT IDENTIFIER ::= { iso(1) member-body(2)
50  *      us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 4 }
51  */
52 #define ENCODE_ALGORITHMIDENTIFIER_SHA2(name, n)                               \
53 static const unsigned char algorithmidentifier_##name##_der[] = {              \
54     ASN1_SEQUENCE, 2 + SHA2_SZ,                                                \
55     ASN1_OID, SHA2_SZ, OID_FIRST(1, 2), DER_840(), DER_10045(), 4, 3, n        \
56 }
57
58 /*
59  * https://csrc.nist.gov/projects/computer-security-objects-register/algorithm-registration
60  *
61  * sigAlgs OBJECT IDENTIFIER ::= { 2 16 840 1 101 3 4 3 }
62  *
63  * id-ecdsa-with-sha3-224 ::= { sigAlgs 9 }
64  * id-ecdsa-with-sha3-256 ::= { sigAlgs 10 }
65  * id-ecdsa-with-sha3-384 ::= { sigAlgs 11 }
66  * id-ecdsa-with-sha3-512 ::= { sigAlgs 12 }
67  */
68 #define ENCODE_ALGORITHMIDENTIFIER_SHA3(name, n)                               \
69 static const unsigned char algorithmidentifier_##name##_der[] = {              \
70     ASN1_SEQUENCE, 2 + SHA3_SZ,                                                \
71     ASN1_OID, SHA3_SZ, OID_FIRST(2, 16), DER_840(), 1, 101, 3, 4, 3, n         \
72 }
73
74 ENCODE_ALGORITHMIDENTIFIER_SHA1(sha1);
75 ENCODE_ALGORITHMIDENTIFIER_SHA2(sha224, 1);
76 ENCODE_ALGORITHMIDENTIFIER_SHA2(sha256, 2);
77 ENCODE_ALGORITHMIDENTIFIER_SHA2(sha384, 3);
78 ENCODE_ALGORITHMIDENTIFIER_SHA2(sha512, 4);
79 ENCODE_ALGORITHMIDENTIFIER_SHA3(sha3_224, 9);
80 ENCODE_ALGORITHMIDENTIFIER_SHA3(sha3_256, 10);
81 ENCODE_ALGORITHMIDENTIFIER_SHA3(sha3_384, 11);
82 ENCODE_ALGORITHMIDENTIFIER_SHA3(sha3_512, 12);
83 /* TODO - Add SHAKE OIDS when they are standardized */
84
85 #define MD_CASE(name)                                                   \
86     case NID_##name:                                                    \
87         *len = sizeof(algorithmidentifier_##name##_der);                \
88         return algorithmidentifier_##name##_der
89
90 const unsigned char *ecdsa_algorithmidentifier_encoding(int md_nid, size_t *len)
91 {
92     switch (md_nid) {
93         MD_CASE(sha1);
94         MD_CASE(sha224);
95         MD_CASE(sha256);
96         MD_CASE(sha384);
97         MD_CASE(sha512);
98         MD_CASE(sha3_224);
99         MD_CASE(sha3_256);
100         MD_CASE(sha3_384);
101         MD_CASE(sha3_512);
102     default:
103         return NULL;
104     }
105 }