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