4b4d3a62f16aef967c653bc28b329215b2aa34e5
[openssl.git] / crypto / rsa / rsa_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/rsa.h"
14
15 #define ASN1_SEQUENCE 0x30
16 #define ASN1_OID 0x06
17
18 /*
19  * -- RFC 2313
20  * pkcs-1 OBJECT IDENTIFIER ::= {
21  *     iso(1) member-body(2) US(840) rsadsi(113549) pkcs(1) 1
22  * }
23  */
24
25 /*
26  * -- RFC 3279
27  * md2WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 2 }
28  * md5WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 4 }
29  * sha1WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 5 }
30  */
31 #define ENCODE_ALGORITHMIDENTIFIER_PKCS1(name, n)                       \
32     static const unsigned char algorithmidentifier_##name##_der[] = {   \
33         ASN1_SEQUENCE, 0x0b,                                            \
34           ASN1_OID, 0x09, 1 * 40 + 2,  134, 72, 134, 247, 13, 1, 1, n   \
35 }
36 #ifndef FIPS_MODE
37 ENCODE_ALGORITHMIDENTIFIER_PKCS1(md2, 2);
38 ENCODE_ALGORITHMIDENTIFIER_PKCS1(md5, 4);
39 #endif
40 ENCODE_ALGORITHMIDENTIFIER_PKCS1(sha1, 5);
41
42 /*
43  * -- RFC 4055
44  * sha224WithRSAEncryption  OBJECT IDENTIFIER  ::=  { pkcs-1 14 }
45  * sha256WithRSAEncryption  OBJECT IDENTIFIER  ::=  { pkcs-1 11 }
46  * sha384WithRSAEncryption  OBJECT IDENTIFIER  ::=  { pkcs-1 12 }
47  * sha512WithRSAEncryption  OBJECT IDENTIFIER  ::=  { pkcs-1 13 }
48  */
49 ENCODE_ALGORITHMIDENTIFIER_PKCS1(sha224, 14);
50 ENCODE_ALGORITHMIDENTIFIER_PKCS1(sha256, 11);
51 ENCODE_ALGORITHMIDENTIFIER_PKCS1(sha384, 12);
52 ENCODE_ALGORITHMIDENTIFIER_PKCS1(sha512, 13);
53
54 /*
55  * -- https://csrc.nist.gov/projects/computer-security-objects-register/algorithm-registration
56  *
57  * sigAlgs OBJECT IDENTIFIER ::= { 2 16 840 1 101 3 4 3 }
58  *
59  * id-rsassa-pkcs1-v1_5-with-sha3-224 ::= { sigAlgs 13 }
60  * id-rsassa-pkcs1-v1_5-with-sha3-256 ::= { sigAlgs 14 }
61  * id-rsassa-pkcs1-v1_5-with-sha3-384 ::= { sigAlgs 15 }
62  * id-rsassa-pkcs1-v1_5-with-sha3-512 ::= { sigAlgs 16 }
63  */
64 #define ENCODE_ALGORITHMIDENTIFIER_SIGALGS(name, n)                     \
65     static const unsigned char algorithmidentifier_##name##_der[] = {   \
66         ASN1_SEQUENCE, 0x0c,                                            \
67           ASN1_OID, 0x0a, 1 * 40 + 2,  16, 134, 72, 1, 101, 3, 4, 3, n  \
68 }
69 ENCODE_ALGORITHMIDENTIFIER_SIGALGS(sha3_224, 13);
70 ENCODE_ALGORITHMIDENTIFIER_SIGALGS(sha3_256, 14);
71 ENCODE_ALGORITHMIDENTIFIER_SIGALGS(sha3_384, 15);
72 ENCODE_ALGORITHMIDENTIFIER_SIGALGS(sha3_512, 16);
73
74 #define MD_CASE(name)                                                   \
75     case NID_##name:                                                    \
76         *len = sizeof(algorithmidentifier_##name##_der);                \
77         return algorithmidentifier_##name##_der
78
79 const unsigned char *rsa_algorithmidentifier_encoding(int md_nid, size_t *len)
80 {
81     switch (md_nid) {
82 #ifndef FIPS_MODE
83         MD_CASE(md2);
84         MD_CASE(md5);
85 #endif
86         MD_CASE(sha1);
87         MD_CASE(sha224);
88         MD_CASE(sha256);
89         MD_CASE(sha384);
90         MD_CASE(sha512);
91         MD_CASE(sha3_224);
92         MD_CASE(sha3_256);
93         MD_CASE(sha3_384);
94         MD_CASE(sha3_512);
95     default:
96         return NULL;
97     }
98 }