Moduli Generator
Handles the generation, screening, and management of cryptographic modulus files.
This class provides methods for generating moduli candidates, screening them for validity, and organizing them into structured formats used for secure communications.
This utility is intended for cryptographic security operations, where moduli
are required for operations like key exchange. It uses external tools like
ssh-keygen for candidate generation and screening processes.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
The configuration object containing paths and settings such as directory paths and security-related configurations. |
|
version |
The version of the moduli generator settings. |
|
logger |
The logger used for logging information, warnings, and errors during execution. |
|
db |
MariaDBConnector
|
Lazily instantiated attribute for database storage of moduli, created as needed. |
Source code in moduli_generator/__init__.py
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | |
db
property
Lazy initialization of the database connection property.
This property ensures that the database connection is initialized only once, upon first access, and then reused for subsequent operations.
Returns:
| Name | Type | Description |
|---|---|---|
MariaDBConnector |
MariaDBConnector
|
Initialized database connection object. |
__version__
property
Represents the version property of a class that retrieves the version information of the instance.
This property is read-only and provides access to the internal
`version` attribute of the class instance.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Current version of the instance. |
__repr__
Returns a string representation of the object.
The method constructs a formatted string that combines specific attributes of the object into a single output using colons as separators.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A formatted string representation of the object. |
Source code in moduli_generator/__init__.py
__init__
Class responsible for managing moduli configuration and related utilities. Handles logging configuration paths and supports lazy database initialization to optimize resource usage. Leverages the provided configuration to set up essential properties and integrate logging behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ModuliConfig
|
Configuration instance that contains moduli settings and utilities. |
default_config()
|
Source code in moduli_generator/__init__.py
_run_subprocess_with_logging
staticmethod
Executes a subprocess command with real-time logging for both stdout and stderr. This method
handles the logging of subprocess output streams directly as they are produced, using a
threaded approach to ensure asynchronous handling of stream data.
Subprocess outputs are logged in real-time using the provided logger object, and the function
returns a custom `StreamedResult` object containing information about the command execution
(omitting `stdout` and `stderr` data as they're already logged). Exceptions encountered during the
operation, such as errors in the process execution or issues with logging, are propagated
appropriately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The command to be executed as a list of strings. |
required |
debug_level
|
int
|
Logging level used for |
DEBUG
|
info_level
|
int
|
Logging level used for |
INFO
|
logger
|
Logger
|
Logger instance used for logging the subprocess output. |
required |
Returns:
| Type | Description |
|---|---|
CompletedProcess
|
subprocess.CompletedProcess: A custom result object containing command arguments and a return code. |
Source code in moduli_generator/__init__.py
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
_generate_candidates_static
staticmethod
Generate a file of modular arithmetic candidates using ssh-keygen via a subprocess.
This static method ensures that provided key length and nice value are validated for safe subprocess execution. The resulting candidates are saved in a file within the specified candidates directory and are uniquely identified with a timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ModuliConfig
|
Configuration object that holds the candidates directory, nice value, and logger. |
required |
key_length
|
int
|
The desired bit length for the moduli candidates. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Path |
PosixPath
|
The file path where the generated candidates are stored. |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
If the ssh-keygen process fails during candidate generation. |
Source code in moduli_generator/__init__.py
_screen_candidates_static
staticmethod
Performs screening of SSH moduli candidates using the ssh-keygen tool and
a specified "nice" value. The function validates input arguments, constructs
the appropriate command line, and handles real-time logging of the subprocess
output. Generated screened files replace candidate files on success.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ModuliConfig
|
Configuration object containing parameters such as
|
required |
candidates_file
|
PosixPath
|
Path object referring to the file containing the SSH moduli candidates to be screened. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Path |
PosixPath
|
Path to the successfully screened moduli file. |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
If the |
Source code in moduli_generator/__init__.py
_parse_moduli_files
Parses the moduli files to extract specific key-length entries and formats them into a dictionary structure for further processing. This method iterates over a set of screened files, reads their contents line by line, and extracts information about timestamp, key size, and modulus if the line meets specific criteria.
Returns:
| Type | Description |
|---|---|
Dict[str, List[Dict[str, Any]]]
|
Dict[str, List[Dict[str, Any]]]: A dictionary with parsed moduli installers under the key |
Dict[str, List[Dict[str, Any]]]
|
'screened_moduli'. Each entry is a dictionary containing 'timestamp', 'key-size', and 'modulus'. |
Source code in moduli_generator/__init__.py
_list_moduli_files
Lists all moduli files based on the configured moduli directory and file pattern.
This function retrieves and returns a list of all files in the
configured moduli directory that match the specified file pattern.
Returns:
| Type | Description |
|---|---|
List[PosixPath]
|
list[Path]: List of matching moduli files |
Source code in moduli_generator/__init__.py
generate_moduli
Generates and screens Diffie-Hellman moduli files for specified key lengths using pipeline processing for optimal performance.
This method uses a pipeline approach where candidate generation and screening happen
concurrently. As soon as a candidate file is generated, it's immediately submitted
for screening, rather than waiting for all candidates to be generated first.
This optimization reduces overall processing time by overlapping I/O and CPU operations.
Returns:
| Name | Type | Description |
|---|---|---|
ModuliGenerator |
ModuliGenerator
|
self |
Source code in moduli_generator/__init__.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | |
store_moduli
Parse, validate, and store screened moduli into the database and manage their source files once the operation is successful. This function ensures that the moduli records are stored transactionally. After successful storage, the source files are deleted.
Returns:
| Name | Type | Description |
|---|---|---|
ModuliGenerator |
ModuliGenerator
|
self |
Source code in moduli_generator/__init__.py
write_moduli_file
Writes the moduli file using the database interface.
This method retrieves the moduli necessary for the moduli file from the
database and then writes it to the appropriate location using
the database interface.
Returns:
| Name | Type | Description |
|---|---|---|
ModuliGenerator |
ModuliGenerator
|
self |
Source code in moduli_generator/__init__.py
restart_screening
Restarts the screening process for candidates grouped by their respective lengths and generates moduli files for each key length. This method processes candidates from the specified directory, screens them using a process pool executor, and returns an updated instance of the ModuliGenerator.
Returns:
| Name | Type | Description |
|---|---|---|
ModuliGenerator |
ModuliGenerator
|
The current instance of ModuliGenerator for method chaining. |
Source code in moduli_generator/__init__.py
options: members: true