Skip to content

💡 Feature Request - Standardized Error Returns for CryptoLib Functions #482

@porfanid

Description

@porfanid

Summary

Enhance CryptoLib by standardizing error handling across all functions to return consistent int32_t error codes instead of void or inconsistent return types. This improvement will provide applications with proper error detection, handling, and debugging capabilities when using CryptoLib functions.

Use Case

Current Problem:

  • Functions like crypto_deep_copy_string() returned char* with NULL indicating failure, but no way to distinguish between different failure reasons
  • Functions like Crypto_Local_Config(), Crypto_Local_Init(), and Crypto_Calc_CRC_Init_Table() returned void, providing no error feedback
  • Security association functions didn't validate parameters or propagate errors properly

Proposed Enhancement:

  • All functions that can fail should return int32_t with standardized CRYPTO_LIB_* error codes
  • Functions requiring output should use output parameters (e.g., crypto_deep_copy_string(const char* source, char** result))
  • Proper parameter validation with specific error codes (e.g., CRYPTO_LIB_ERR_NULL_BUFFER, CRYPTO_LIB_ERR_SPI_INDEX_OOB)
  • Error propagation from sub-functions to calling functions

Benefits:

  1. Robust Error Handling: Applications can detect and handle specific error conditions appropriately
  2. Better Debugging: Meaningful error codes help identify root causes of failures
  3. Memory Safety: Proper detection and reporting of malloc failures and null pointer conditions
  4. API Consistency: Uniform error handling pattern across the entire library
  5. Fail-Fast Behavior: Invalid parameters are caught early with specific error codes

Example Usage:

// Before: No error detection possible
char* result = crypto_deep_copy_string(source);
if (result == NULL) {
    // Could be NULL input, malloc failure, or other - unknown
}

// After: Proper error handling
char* result;
int32_t status = crypto_deep_copy_string(source, &result);
switch (status) {
    case CRYPTO_LIB_SUCCESS:
        // Use result safely
        break;
    case CRYPTO_LIB_ERR_NULL_BUFFER:
        // Handle null input parameter
        break;
    case CRYPTO_LIB_ERROR:
        // Handle memory allocation failure
        break;
    default:
        // Handle other errors
        break;
}

This feature enhancement significantly improves the robustness, debuggability, and usability of CryptoLib while maintaining backward compatibility through careful API design.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions