FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/asn.c
/* Write a public ECC key to output */
Write a public ECC key to output
Write a public ECC key to output
static int SetEccPublicKey(byte* output, ecc_key* key, int with_header)
{
    byte bitString[1 + MAX_LENGTH_SZ + 1];
    int  algoSz;
    int  curveSz;
    int  bitStringSz;
    int  idx;
    word32 pubSz = ECC_BUFSIZE;
#ifdef WOLFSSL_SMALL_STACK
    byte* algo = NULL;
    byte* curve = NULL;
    byte* pub;
#else
    byte algo[MAX_ALGO_SZ];
    byte curve[MAX_ALGO_SZ];
    byte pub[ECC_BUFSIZE];
#endif
    int ret;
#ifdef WOLFSSL_SMALL_STACK
    pub = (byte*)XMALLOC(ECC_BUFSIZE, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
    if (pub == NULL)
        return MEMORY_E;
#endif
#if defined(HAVE_SELFTEST) || defined(HAVE_FIPS)
    if (key != NULL && key->dp == NULL) {
        ret = BAD_FUNC_ARG;
    }
    else {
        ret = wc_ecc_export_x963(key, pub, &pubSz);
    }
#else
    ret = wc_ecc_export_x963(key, pub, &pubSz);
#endif
    if (ret != 0) {
#ifdef WOLFSSL_SMALL_STACK
        XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
        return ret;
    }
    if (with_header) {
#ifdef WOLFSSL_SMALL_STACK
        curve = (byte*)XMALLOC(MAX_ALGO_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
        if (curve == NULL) {
            XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
            return MEMORY_E;
        }
#endif
        curveSz = SetCurve(key, curve);
        if (curveSz <= 0) {
#ifdef WOLFSSL_SMALL_STACK
            XFREE(curve, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
            XFREE(pub,   key->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
            return curveSz;
        }
#ifdef WOLFSSL_SMALL_STACK
        algo = (byte*)XMALLOC(MAX_ALGO_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
        if (algo == NULL) {
            XFREE(curve, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
            XFREE(pub,   key->heap, DYNAMIC_TYPE_TMP_BUFFER);
            return MEMORY_E;
        }
#endif
        algoSz  = SetAlgoID(ECDSAk, algo, oidKeyType, curveSz);
        bitStringSz = SetBitString(pubSz, 0, bitString);
        idx = SetSequence(pubSz + curveSz + bitStringSz + algoSz, output);
        if (output)
            XMEMCPY(output + idx, algo, algoSz);
        idx += algoSz;
        if (output)
            XMEMCPY(output + idx, curve, curveSz);
        idx += curveSz;
        if (output)
            XMEMCPY(output + idx, bitString, bitStringSz);
        idx += bitStringSz;
    }
    else
        idx = 0;
    if (output)
        XMEMCPY(output + idx, pub, pubSz);
    idx += pubSz;
#ifdef WOLFSSL_SMALL_STACK
    if (with_header) {
        XFREE(algo,  key->heap, DYNAMIC_TYPE_TMP_BUFFER);
        XFREE(curve, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
    }
    XFREE(pub,   key->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
    return idx;
}
static int SetEccPublicKey ( byte * output , ecc_key * key , int with_header ) { byte bitString [ 1 + MAX_LENGTH_SZ + 1 ] ; int algoSz ; int curveSz ; int bitStringSz ; int idx ; word32 pubSz = ECC_BUFSIZE ; #ifdef WOLFSSL_SMALL_STACK byte * algo = NULL ; byte * curve = NULL ; byte * pub ; #else byte algo [ MAX_ALGO_SZ ] ; byte curve [ MAX_ALGO_SZ ] ; byte pub [ ECC_BUFSIZE ] ; #endif int ret ; #ifdef WOLFSSL_SMALL_STACK pub = ( byte * ) XMALLOC ( ECC_BUFSIZE , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( pub == NULL ) return MEMORY_E ; #endif #if defined ( HAVE_SELFTEST ) || defined ( HAVE_FIPS ) if ( key != NULL && key -> dp == NULL ) { ret = BAD_FUNC_ARG ; } else { ret = wc_ecc_export_x963 ( key , pub , & pubSz ) ; } #else ret = wc_ecc_export_x963 ( key , pub , & pubSz ) ; #endif if ( ret != 0 ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( pub , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return ret ; } if ( with_header ) { #ifdef WOLFSSL_SMALL_STACK curve = ( byte * ) XMALLOC ( MAX_ALGO_SZ , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( curve == NULL ) { XFREE ( pub , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; return MEMORY_E ; } #endif curveSz = SetCurve ( key , curve ) ; if ( curveSz <= 0 ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( curve , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; XFREE ( pub , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return curveSz ; } #ifdef WOLFSSL_SMALL_STACK algo = ( byte * ) XMALLOC ( MAX_ALGO_SZ , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( algo == NULL ) { XFREE ( curve , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; XFREE ( pub , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; return MEMORY_E ; } #endif algoSz = SetAlgoID ( ECDSAk , algo , oidKeyType , curveSz ) ; bitStringSz = SetBitString ( pubSz , 0 , bitString ) ; idx = SetSequence ( pubSz + curveSz + bitStringSz + algoSz , output ) ; if ( output ) XMEMCPY ( output + idx , algo , algoSz ) ; idx += algoSz ; if ( output ) XMEMCPY ( output + idx , curve , curveSz ) ; idx += curveSz ; if ( output ) XMEMCPY ( output + idx , bitString , bitStringSz ) ; idx += bitStringSz ; } else idx = 0 ; if ( output ) XMEMCPY ( output + idx , pub , pubSz ) ; idx += pubSz ; #ifdef WOLFSSL_SMALL_STACK if ( with_header ) { XFREE ( algo , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; XFREE ( curve , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; } XFREE ( pub , key -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return idx ; }
Write a public ECC key to output
Write a public ECC key to output
/* older version of ecc.c can not handle dp being NULL */ /* headers */ /* algo */ /* curve */ /* bit string */ /* pub */