Add a test for EVP_PKEY_keymake() and EVP_PKEY_make()
[openssl.git] / test / evp_fromdata_test.c
1 /*
2  * Copyright 2019 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/evp.h>
11 #include <openssl/provider.h>
12 #include <openssl/params.h>
13 #include <openssl/core_names.h>
14 #include "internal/nelem.h"
15 #include "crypto/evp.h"          /* For the internal API */
16 #include "testutil.h"
17
18 /* Array indexes used in test_fromdata_rsa */
19 #define N       0
20 #define E       1
21 #define D       2
22 #define P       3
23 #define Q       4
24 #define DP      5
25 #define DQ      6
26 #define QINV    7
27
28 static int test_fromdata_rsa(void)
29 {
30     int ret = 0;
31     EVP_PKEY_CTX *ctx = NULL;
32     EVP_PKEY *pk = NULL;
33     /*
34      * 32-bit RSA key, extracted from this command,
35      * executed with OpenSSL 1.0.2:
36      *
37      * openssl genrsa 32 | openssl rsa -text
38      */
39     static unsigned long key_numbers[] = {
40         0xbc747fc5,              /* N */
41         0x10001,                 /* E */
42         0x7b133399,              /* D */
43         0xe963,                  /* P */
44         0xceb7,                  /* Q */
45         0x8599,                  /* DP */
46         0xbd87,                  /* DQ */
47         0xcc3b,                  /* QINV */
48     };
49     OSSL_PARAM fromdata_params[] = {
50         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_N, &key_numbers[N]),
51         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_E, &key_numbers[E]),
52         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_D, &key_numbers[D]),
53         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR, &key_numbers[P]),
54         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR, &key_numbers[Q]),
55         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT, &key_numbers[DP]),
56         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT, &key_numbers[DQ]),
57         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_COEFFICIENT, &key_numbers[QINV]),
58         OSSL_PARAM_END
59     };
60
61     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_provided(NULL, "RSA", NULL)))
62         goto err;
63
64     if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx))
65         || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params)))
66         goto err;
67
68     /*
69      * TODO(3.0) We can't do much more at this point without using internals,
70      * because RSA functionality is still missing.  When the time comes, it
71      * would be nice to try and do something "useful" with this key, such
72      * as signing a small piece of data.
73      */
74     ret = 1;
75
76  err:
77     EVP_PKEY_free(pk);
78     EVP_PKEY_CTX_free(ctx);
79
80     return ret;
81 }
82
83 int setup_tests(void)
84 {
85     ADD_TEST(test_fromdata_rsa);
86     return 1;
87 }