FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/asn.c
/* Convert private DsaKey key to DER format, write to output (inLen),
   return bytes written */
Convert private DsaKey key to DER format, write to output (inLen),
return bytes written
Convert private DsaKey key to DER format write to output ( inLen ) return bytes written
int wc_DsaKeyToDer(DsaKey* key, byte* output, word32 inLen)
{
    word32 seqSz, verSz, rawLen, intTotalLen = 0;
    word32 sizes[DSA_INTS];
    int    i, j, outLen, ret = 0, mpSz;
    byte  seq[MAX_SEQ_SZ];
    byte  ver[MAX_VERSION_SZ];
    byte* tmps[DSA_INTS];
    if (!key || !output)
        return BAD_FUNC_ARG;
    if (key->type != DSA_PRIVATE)
        return BAD_FUNC_ARG;
    for (i = 0; i < DSA_INTS; i++)
        tmps[i] = NULL;
    for (i = 0; i < DSA_INTS; i++) {
        mp_int* keyInt = GetDsaInt(key, i);
        rawLen = mp_unsigned_bin_size(keyInt) + 1;
        tmps[i] = (byte*)XMALLOC(rawLen + MAX_SEQ_SZ, key->heap,
                                                              DYNAMIC_TYPE_DSA);
        if (tmps[i] == NULL) {
            ret = MEMORY_E;
            break;
        }
        mpSz = SetASNIntMP(keyInt, -1, tmps[i]);
        if (mpSz < 0) {
            ret = mpSz;
            break;
        }
        intTotalLen += (sizes[i] = mpSz);
    }
    if (ret != 0) {
        FreeTmpDsas(tmps, key->heap);
        return ret;
    }
    verSz = SetMyVersion(0, ver, FALSE);
    seqSz = SetSequence(verSz + intTotalLen, seq);
    outLen = seqSz + verSz + intTotalLen;
    if (outLen > (int)inLen) {
        FreeTmpDsas(tmps, key->heap);
        return BAD_FUNC_ARG;
    }
    XMEMCPY(output, seq, seqSz);
    j = seqSz;
    XMEMCPY(output + j, ver, verSz);
    j += verSz;
    for (i = 0; i < DSA_INTS; i++) {
        XMEMCPY(output + j, tmps[i], sizes[i]);
        j += sizes[i];
    }
    FreeTmpDsas(tmps, key->heap);
    return outLen;
}
int wc_DsaKeyToDer ( DsaKey * key , byte * output , word32 inLen ) { word32 seqSz , verSz , rawLen , intTotalLen = 0 ; word32 sizes [ DSA_INTS ] ; int i , j , outLen , ret = 0 , mpSz ; byte seq [ MAX_SEQ_SZ ] ; byte ver [ MAX_VERSION_SZ ] ; byte * tmps [ DSA_INTS ] ; if ( ! key || ! output ) return BAD_FUNC_ARG ; if ( key -> type != DSA_PRIVATE ) return BAD_FUNC_ARG ; for ( i = 0 ; i < DSA_INTS ; i ++ ) tmps [ i ] = NULL ; for ( i = 0 ; i < DSA_INTS ; i ++ ) { mp_int * keyInt = GetDsaInt ( key , i ) ; rawLen = mp_unsigned_bin_size ( keyInt ) + 1 ; tmps [ i ] = ( byte * ) XMALLOC ( rawLen + MAX_SEQ_SZ , key -> heap , DYNAMIC_TYPE_DSA ) ; if ( tmps [ i ] == NULL ) { ret = MEMORY_E ; break ; } mpSz = SetASNIntMP ( keyInt , -1 , tmps [ i ] ) ; if ( mpSz < 0 ) { ret = mpSz ; break ; } intTotalLen += ( sizes [ i ] = mpSz ) ; } if ( ret != 0 ) { FreeTmpDsas ( tmps , key -> heap ) ; return ret ; } verSz = SetMyVersion ( 0 , ver , FALSE ) ; seqSz = SetSequence ( verSz + intTotalLen , seq ) ; outLen = seqSz + verSz + intTotalLen ; if ( outLen > ( int ) inLen ) { FreeTmpDsas ( tmps , key -> heap ) ; return BAD_FUNC_ARG ; } XMEMCPY ( output , seq , seqSz ) ; j = seqSz ; XMEMCPY ( output + j , ver , verSz ) ; j += verSz ; for ( i = 0 ; i < DSA_INTS ; i ++ ) { XMEMCPY ( output + j , tmps [ i ] , sizes [ i ] ) ; j += sizes [ i ] ; } FreeTmpDsas ( tmps , key -> heap ) ; return outLen ; }
Convert private DsaKey key to DER format, write to output (inLen),
return bytes written
Convert private DsaKey key to DER format write to output ( inLen ) return bytes written
/* write all big ints from key to DER tmps */ /* make headers */ /* write to output */