Add RSA SHA512 truncated digest support
[openssl.git] / providers / common / der / der_rsa.c.in
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 <openssl/bn.h>
11 #include <openssl/obj_mac.h>
12 #include "prov/der_rsa.h"
13
14 /* Well known OIDs precompiled */
15 {-
16     $OUT = oids_to_c::process_leaves('providers/common/der/RSA.asn1',
17                                      { dir => $config{sourcedir},
18                                        filter => \&oids_to_c::filter_to_C });
19 -}
20
21 int DER_w_algorithmIdentifier_RSA(WPACKET *pkt, int tag, RSA *rsa)
22 {
23     return DER_w_begin_sequence(pkt, tag)
24         /* No parameters (yet?) */
25         && DER_w_precompiled(pkt, -1, der_oid_rsaEncryption,
26                              sizeof(der_oid_rsaEncryption))
27         && DER_w_end_sequence(pkt, tag);
28 }
29
30 /* Aliases so we can have a uniform MD_CASE */
31 #define der_oid_sha3_224WithRSAEncryption \
32     der_oid_id_rsassa_pkcs1_v1_5_with_sha3_224
33 #define der_oid_sha3_256WithRSAEncryption \
34     der_oid_id_rsassa_pkcs1_v1_5_with_sha3_256
35 #define der_oid_sha3_384WithRSAEncryption \
36     der_oid_id_rsassa_pkcs1_v1_5_with_sha3_384
37 #define der_oid_sha3_512WithRSAEncryption \
38     der_oid_id_rsassa_pkcs1_v1_5_with_sha3_512
39
40 #define MD_CASE(name)                                                   \
41     case NID_##name:                                                    \
42         precompiled = der_oid_##name##WithRSAEncryption;                \
43         precompiled_sz = sizeof(der_oid_##name##WithRSAEncryption);     \
44         break;
45
46 int DER_w_algorithmIdentifier_RSA_with(WPACKET *pkt, int tag,
47                                        RSA *rsa, int mdnid)
48 {
49     const unsigned char *precompiled = NULL;
50     size_t precompiled_sz = 0;
51
52     switch (mdnid) {
53 #ifndef FIPS_MODULE
54         MD_CASE(md2);
55         MD_CASE(md5);
56         MD_CASE(md4);
57         MD_CASE(ripemd160);
58 /* TODO(3.0) Decide what to do about mdc2 and md5_sha1 */
59 #endif
60         MD_CASE(sha1);
61         MD_CASE(sha224);
62         MD_CASE(sha256);
63         MD_CASE(sha384);
64         MD_CASE(sha512);
65         MD_CASE(sha512_224);
66         MD_CASE(sha512_256);
67         MD_CASE(sha3_224);
68         MD_CASE(sha3_256);
69         MD_CASE(sha3_384);
70         MD_CASE(sha3_512);
71     default:
72         return 0;
73     }
74
75     return DER_w_begin_sequence(pkt, tag)
76         /* No parameters (yet?) */
77         && DER_w_precompiled(pkt, -1, precompiled, precompiled_sz)
78         && DER_w_end_sequence(pkt, tag);
79 }