summaryrefslogtreecommitdiff
path: root/crypto_libraries/inc/nx_crypto_drbg.h
blob: e7c15bae1b59fa3f7a2741a30909a8f3205e34c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Crypto Component                                                 */
/**                                                                       */
/**   Deterministic Random Bit Generator (DRBG)                           */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/**************************************************************************/
/*                                                                        */
/*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
/*                                                                        */
/*    nx_crypto_drbg.h                                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This file defines the basic Application Interface (API) to the      */
/*    NetX Crypto DRBG module.                                            */
/*                                                                        */
/**************************************************************************/

#ifndef NX_CRYPTO_DRBG_H
#define NX_CRYPTO_DRBG_H

/* Determine if a C++ compiler is being used.  If so, ensure that standard
   C is used to process the API information.  */
#ifdef __cplusplus

/* Yes, C++ compiler is present.  Use standard C.  */
extern   "C" {

#endif

#include "nx_crypto.h"

/* Constants. */
#define NX_CRYPTO_DRBG_BLOCK_LENGTH_AES (16)
#define NX_CRYPTO_DRBG_MAX_BLOCK_LENGTH (16)
#define NX_CRYPTO_DRBG_MAX_KEY_LENGTH   (32)
#define NX_CRYPTO_DRBG_MAX_SEEDLEN      (48)

#ifndef NX_CRYPTO_DRBG_BLOCK_LENGTH
#define NX_CRYPTO_DRBG_BLOCK_LENGTH     (NX_CRYPTO_DRBG_BLOCK_LENGTH_AES)
#endif

#define NX_CRYPTO_DRBG_DF_INPUT_OFFSET  (NX_CRYPTO_DRBG_BLOCK_LENGTH + 8)

#ifndef NX_CRYPTO_DRBG_SEED_BUFFER_LEN
#define NX_CRYPTO_DRBG_SEED_BUFFER_LEN  (256)
#endif

#ifndef NX_CRYPTO_DRBG_MAX_ENTROPY_LEN
#define NX_CRYPTO_DRBG_MAX_ENTROPY_LEN  (125)
#endif

#ifndef NX_CRYPTO_DRBG_MAX_SEED_LIFE
#define NX_CRYPTO_DRBG_MAX_SEED_LIFE    (100000)
#endif

#ifndef NX_CRYPTO_DRBG_MUTEX_GET
#define NX_CRYPTO_DRBG_MUTEX_GET
#endif

#ifndef NX_CRYPTO_DRBG_MUTEX_PUT
#define NX_CRYPTO_DRBG_MUTEX_PUT
#endif

#ifndef NX_CRYPTO_DRBG_USE_DF
#define NX_CRYPTO_DRBG_USE_DF (1)
#endif

#ifndef NX_CRYPTO_DRBG_PREDICTION_RESISTANCE
#define NX_CRYPTO_DRBG_PREDICTION_RESISTANCE (1)
#endif

#ifndef NX_CRYPTO_DRBG_CTR_CRYPTO_METHOD
extern NX_CRYPTO_METHOD crypto_method_aes_cbc_128;
#define NX_CRYPTO_DRBG_CTR_CRYPTO_METHOD &crypto_method_aes_cbc_128
#endif

#ifndef NX_CRYPTO_DRBG_CTR_CRYPTO_METADATA
#define NX_CRYPTO_DRBG_CTR_CRYPTO_METADATA _nx_crypto_ctr_metadata
#define NX_CRYPTO_DRBG_CTR_METADATA_SIZE (sizeof(NX_CRYPTO_AES))
#endif

#ifndef NX_CRYPTO_DRBG_ENTROPY_INPUT_FUNC
#define NX_CRYPTO_DRBG_ENTROPY_INPUT_FUNC _nx_crypto_drbg_rnd_entropy_input
#endif



/* DRBG control structure. */
typedef struct NX_CRYPTO_DRBG_STRUCT
{
    /* Crypto method and metadata used in the DRBG. */
    NX_CRYPTO_METHOD *nx_crypto_drbg_crypto_method;
    VOID *nx_crypto_drbg_crypto_metadata;

    UINT (*nx_crypto_drbg_get_entropy)(UCHAR *entropy, UINT *entropy_len, UINT entropy_max_len);

    UINT  nx_crypto_drbg_use_df;
    UINT  nx_crypto_drbg_prediction_resistance;
    UINT  nx_crypto_drbg_security_strength;

    UINT  nx_crypto_drbg_instantiated;

    /* DRBG working state. */
    UCHAR nx_crypto_drbg_key[NX_CRYPTO_DRBG_MAX_KEY_LENGTH];
    UCHAR nx_crypto_drbg_v[NX_CRYPTO_DRBG_MAX_BLOCK_LENGTH];

    /* A counter that indicates the number of requests for pseudorandom bits since instantiation or reseeding. */
    UINT  nx_crypto_drgb_reseed_counter;

    UINT  nx_crypto_drbg_seedlen;

    UCHAR nx_crypto_drbg_buffer[NX_CRYPTO_DRBG_SEED_BUFFER_LEN];
} NX_CRYPTO_DRBG;

/* DRBG control structure. */
typedef struct NX_CRYPTO_DRBG_OPTIONS_STRUCT
{
    /* Crypto method and metadata used in the DRBG. */
    NX_CRYPTO_METHOD *crypto_method;
    VOID *crypto_metadata;

    UINT (*entropy_input)(UCHAR *entropy, UINT *entropy_len, UINT entropy_max_len);

    UINT  use_df;
    UINT  prediction_resistance;
    UINT  security_strength;
} NX_CRYPTO_DRBG_OPTIONS;


/* Function prototypes */


UINT _nx_crypto_drbg_instantiate(NX_CRYPTO_DRBG *drbg_ptr,
                                 UCHAR *nonce,
                                 UINT nonce_len,
                                 UCHAR *personalization_string,
                                 UINT personalization_string_len);

UINT _nx_crypto_drbg_reseed(NX_CRYPTO_DRBG *drbg_ptr,
                            UCHAR *additional_input,
                            UINT additional_input_len);

UINT _nx_crypto_drbg_generate(NX_CRYPTO_DRBG *drbg_ptr,
                              UCHAR *output, UINT output_length_in_byte,
                              UCHAR *additional_input,
                              UINT additional_input_len);

UINT _nx_crypto_method_drbg_init(struct  NX_CRYPTO_METHOD_STRUCT *method,
                                 UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
                                 VOID  **handle,
                                 VOID  *crypto_metadata,
                                 ULONG crypto_metadata_size);

UINT _nx_crypto_method_drbg_cleanup(VOID *crypto_metadata);

UINT _nx_crypto_method_drbg_operation(UINT op,
                                      VOID *handle,
                                      struct NX_CRYPTO_METHOD_STRUCT *method,
                                      UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
                                      UCHAR *input, ULONG input_length_in_byte,
                                      UCHAR *iv_ptr,
                                      UCHAR *output, ULONG output_length_in_byte,
                                      VOID *crypto_metadata, ULONG crypto_metadata_size,
                                      VOID *packet_ptr,
                                      VOID (*nx_crypto_hw_process_callback)(VOID *, UINT));

UINT _nx_crypto_drbg(UINT bits, UCHAR *result);

#ifdef __cplusplus
}
#endif

#endif /* NX_CRYPTO_DRBG_H */