1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Crypto Component                                                 */
16 /**                                                                       */
17 /**   RSA public-key encryption algorithm                                 */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /**************************************************************************/
24 /*                                                                        */
25 /*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
26 /*                                                                        */
27 /*    nx_crypto_rsa.h                                     PORTABLE C      */
28 /*                                                           6.1          */
29 /*  AUTHOR                                                                */
30 /*                                                                        */
31 /*    Timothy Stapko, Microsoft Corporation                               */
32 /*                                                                        */
33 /*  DESCRIPTION                                                           */
34 /*                                                                        */
35 /*    This file defines the basic Application Interface (API) to the      */
36 /*    NetX Crypto RSA module.                                             */
37 /*                                                                        */
38 /*  RELEASE HISTORY                                                       */
39 /*                                                                        */
40 /*    DATE              NAME                      DESCRIPTION             */
41 /*                                                                        */
42 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
43 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
44 /*                                            resulting in version 6.1    */
45 /*                                                                        */
46 /**************************************************************************/
47 
48 #ifndef NX_CRYPTO_RSA_H
49 #define NX_CRYPTO_RSA_H
50 
51 /* Determine if a C++ compiler is being used.  If so, ensure that standard
52    C is used to process the API information.  */
53 #ifdef __cplusplus
54 
55 /* Yes, C++ compiler is present.  Use standard C.  */
56 extern   "C" {
57 
58 #endif
59 
60 /* Include the ThreadX and port-specific data type file.  */
61 
62 #include "nx_crypto.h"
63 
64 /* Define the maximum size of an RSA modulus supported in bits. */
65 #ifndef NX_CRYPTO_MAX_RSA_MODULUS_SIZE
66 #define NX_CRYPTO_MAX_RSA_MODULUS_SIZE    (4096) /* Default is to support 4096-bit RSA keys. */
67 #endif
68 
69 
70 /* Scratch buffer for RSA calculations.
71     Size must be no less than 10 * sizeof(modulus) + 24. 2584 bytes for 2048 bits cryption.
72     If CRT algorithm is not used, size must be no less than (7 * sizeof(modulus) + 8). 1800 bytes for 2048 bits cryption. */
73 #define NX_CRYPTO_RSA_SCRATCH_BUFFER_SIZE (((10 * (NX_CRYPTO_MAX_RSA_MODULUS_SIZE / 8)) + 24) / sizeof(USHORT))
74 
75 /* Control block for RSA cryptographic operations. */
76 typedef struct NX_CRYPTO_RSA_STRUCT
77 {
78     /* Pointer to the rsa modulus. */
79     UCHAR *nx_crypto_rsa_modulus;
80 
81     /* RSA modulus length in bytes */
82     UINT nx_crypto_rsa_modulus_length;
83 
84     /* Pointer to prime p. */
85     UCHAR *nx_crypto_rsa_prime_p;
86 
87     /* Length of prime p in bytes. */
88     UINT nx_crypto_rsa_prime_p_length;
89 
90     /* Pointer to prime q. */
91     UCHAR *nx_crypto_rsa_prime_q;
92 
93     /* Length of prime q in bytes. */
94     UINT nx_crypto_rsa_prime_q_length;
95 
96     /* Scratch buffer for RSA calculations. */
97     USHORT nx_crypto_rsa_scratch_buffer[NX_CRYPTO_RSA_SCRATCH_BUFFER_SIZE];
98 } NX_CRYPTO_RSA;
99 
100 
101 /* Function prototypes */
102 
103 UINT _nx_crypto_rsa_operation(const UCHAR *exponent, UINT exponent_length, const UCHAR *modulus, UINT modulus_length,
104                               const UCHAR *p, UINT p_length, UCHAR *q, UINT q_length,
105                               const UCHAR *input, UINT input_length, UCHAR *output,
106                               USHORT *scratch_buf_ptr, UINT scratch_buf_length);
107 
108 UINT _nx_crypto_method_rsa_cleanup(VOID *crypto_metadata);
109 
110 UINT _nx_crypto_method_rsa_operation(UINT op,      /* Encrypt, Decrypt, Authenticate */
111                                      VOID *handle, /* Crypto handler */
112                                      struct NX_CRYPTO_METHOD_STRUCT *method,
113                                      UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
114                                      UCHAR *input, ULONG input_length_in_byte,
115                                      UCHAR *iv_ptr,
116                                      UCHAR *output, ULONG output_length_in_byte,
117                                      VOID *crypto_metadata, ULONG crypto_metadata_size,
118                                      VOID *packet_ptr,
119                                      VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status));
120 
121 UINT _nx_crypto_method_rsa_init(struct NX_CRYPTO_METHOD_STRUCT *method,
122                                 UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
123                                 VOID **handle,
124                                 VOID *crypto_metadata, ULONG crypto_metadata_size);
125 
126 #ifdef __cplusplus
127 }
128 #endif
129 
130 #endif /* NX_CRYPTO_RSA_H */
131 
132