FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/asn.c
/* decrypt PKCS
 *
 * NOTE: input buffer is overwritten with decrypted data!
 *
 * input[in/out] data to decrypt and results are written to
 * sz            size of input buffer
 * password      password if used. Can be NULL for no password
 * passwordSz    size of password buffer
 *
 * returns the total size of decrypted content on success.
 */
decrypt PKCS
NOTE: input buffer is overwritten with decrypted data!

input[in/out] data to decrypt and results are written to
sz            size of input buffer
password      password if used. Can be NULL for no password
passwordSz    size of password buffer

returns the total size of decrypted content on success.
decrypt PKCS NOTE : input buffer is overwritten with decrypted data! input [ in / out ] data to decrypt and results are written to sz size of input buffer password password if used . Can be NULL for no password passwordSz size of password buffer returns the total size of decrypted content on success .
int DecryptContent(byte* input, word32 sz, const char* password, int passwordSz)
{
    word32 inOutIdx = 0, seqEnd, oid, shaOid = 0;
    int    ret = 0, first, second, length = 0, version, saltSz, id;
    int    iterations = 0, keySz = 0;
#ifdef WOLFSSL_SMALL_STACK
    byte*  salt = NULL;
    byte*  cbcIv = NULL;
#else
    byte   salt[MAX_SALT_SIZE];
    byte   cbcIv[MAX_IV_SIZE];
#endif
    byte   tag;
    if (passwordSz < 0) {
        WOLFSSL_MSG("Bad password size");
        return BAD_FUNC_ARG;
    }
    if (GetAlgoId(input, &inOutIdx, &oid, oidIgnoreType, sz) < 0) {
        ERROR_OUT(ASN_PARSE_E, exit_dc);
    }
    first  = input[inOutIdx - 2];   
    second = input[inOutIdx - 1];   
    if (CheckAlgo(first, second, &id, &version, NULL) < 0) {
        ERROR_OUT(ASN_INPUT_E, exit_dc); 
    }
    if (version == PKCS5v2) {
        if (GetSequence(input, &inOutIdx, &length, sz) < 0) {
            ERROR_OUT(ASN_PARSE_E, exit_dc);
        }
        if (GetAlgoId(input, &inOutIdx, &oid, oidKdfType, sz) < 0) {
            ERROR_OUT(ASN_PARSE_E, exit_dc);
        }
        if (oid != PBKDF2_OID) {
            ERROR_OUT(ASN_PARSE_E, exit_dc);
        }
    }
    if (GetSequence(input, &inOutIdx, &length, sz) <= 0) {
        ERROR_OUT(ASN_PARSE_E, exit_dc);
    }
    seqEnd = inOutIdx + length;
    ret = GetOctetString(input, &inOutIdx, &saltSz, sz);
    if (ret < 0)
        goto exit_dc;
    if (saltSz > MAX_SALT_SIZE) {
        ERROR_OUT(ASN_PARSE_E, exit_dc);
    }
#ifdef WOLFSSL_SMALL_STACK
    salt = (byte*)XMALLOC(MAX_SALT_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
    if (salt == NULL) {
        ERROR_OUT(MEMORY_E, exit_dc);
    }
#endif
    XMEMCPY(salt, &input[inOutIdx], saltSz);
    inOutIdx += saltSz;
    if (GetShortInt(input, &inOutIdx, &iterations, sz) < 0) {
        ERROR_OUT(ASN_PARSE_E, exit_dc);
    }
    if (seqEnd > inOutIdx) {
        word32 localIdx = inOutIdx;
        if (GetASNTag(input, &localIdx, &tag, sz) < 0) {
            ERROR_OUT(ASN_PARSE_E, exit_dc);
        }
        if (tag == ASN_INTEGER &&
                GetShortInt(input, &inOutIdx, &keySz, sz) < 0) {
            ERROR_OUT(ASN_PARSE_E, exit_dc);
        }
    }
    if (seqEnd > inOutIdx) {
        if (GetAlgoId(input, &inOutIdx, &oid, oidHmacType, sz) < 0) {
            ERROR_OUT(ASN_PARSE_E, exit_dc);
        }
        shaOid = oid;
    }
#ifdef WOLFSSL_SMALL_STACK
    cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
    if (cbcIv == NULL) {
        ERROR_OUT(MEMORY_E, exit_dc);
    }
#endif
    if (version == PKCS5v2) {
        if (GetAlgoId(input, &inOutIdx, &oid, oidBlkType, sz) < 0) {
            ERROR_OUT(ASN_PARSE_E, exit_dc);
        }
        if (CheckAlgoV2(oid, &id, NULL) < 0) {
            ERROR_OUT(ASN_PARSE_E, exit_dc); 
        }
        if (shaOid == 0)
            shaOid = oid;
        ret = GetOctetString(input, &inOutIdx, &length, sz);
        if (ret < 0)
            goto exit_dc;
        if (length > MAX_IV_SIZE) {
            ERROR_OUT(ASN_PARSE_E, exit_dc);
        }
        XMEMCPY(cbcIv, &input[inOutIdx], length);
        inOutIdx += length;
    }
    if (GetASNTag(input, &inOutIdx, &tag, sz) < 0) {
        ERROR_OUT(ASN_PARSE_E, exit_dc);
    }
    if (tag != (ASN_CONTEXT_SPECIFIC | 0) && tag != ASN_OCTET_STRING) {
        ERROR_OUT(ASN_PARSE_E, exit_dc);
    }
    if (GetLength(input, &inOutIdx, &length, sz) < 0) {
        ERROR_OUT(ASN_PARSE_E, exit_dc);
    }
    ret = wc_CryptKey(password, passwordSz, salt, saltSz, iterations, id,
                   input + inOutIdx, length, version, cbcIv, 0, shaOid);
exit_dc:
#ifdef WOLFSSL_SMALL_STACK
    XFREE(salt,  NULL, DYNAMIC_TYPE_TMP_BUFFER);
    XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
    if (ret == 0) {
        XMEMMOVE(input, input + inOutIdx, length);
        ret = length;
    }
    return ret;
}
int DecryptContent ( byte * input , word32 sz , const char * password , int passwordSz ) { word32 inOutIdx = 0 , seqEnd , oid , shaOid = 0 ; int ret = 0 , first , second , length = 0 , version , saltSz , id ; int iterations = 0 , keySz = 0 ; #ifdef WOLFSSL_SMALL_STACK byte * salt = NULL ; byte * cbcIv = NULL ; #else byte salt [ MAX_SALT_SIZE ] ; byte cbcIv [ MAX_IV_SIZE ] ; #endif byte tag ; if ( passwordSz < 0 ) { WOLFSSL_MSG ( " " ) ; return BAD_FUNC_ARG ; } if ( GetAlgoId ( input , & inOutIdx , & oid , oidIgnoreType , sz ) < 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } first = input [ inOutIdx - 2 ] ; second = input [ inOutIdx - 1 ] ; if ( CheckAlgo ( first , second , & id , & version , NULL ) < 0 ) { ERROR_OUT ( ASN_INPUT_E , exit_dc ) ; } if ( version == PKCS5v2 ) { if ( GetSequence ( input , & inOutIdx , & length , sz ) < 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } if ( GetAlgoId ( input , & inOutIdx , & oid , oidKdfType , sz ) < 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } if ( oid != PBKDF2_OID ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } } if ( GetSequence ( input , & inOutIdx , & length , sz ) <= 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } seqEnd = inOutIdx + length ; ret = GetOctetString ( input , & inOutIdx , & saltSz , sz ) ; if ( ret < 0 ) goto exit_dc ; if ( saltSz > MAX_SALT_SIZE ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } #ifdef WOLFSSL_SMALL_STACK salt = ( byte * ) XMALLOC ( MAX_SALT_SIZE , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( salt == NULL ) { ERROR_OUT ( MEMORY_E , exit_dc ) ; } #endif XMEMCPY ( salt , & input [ inOutIdx ] , saltSz ) ; inOutIdx += saltSz ; if ( GetShortInt ( input , & inOutIdx , & iterations , sz ) < 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } if ( seqEnd > inOutIdx ) { word32 localIdx = inOutIdx ; if ( GetASNTag ( input , & localIdx , & tag , sz ) < 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } if ( tag == ASN_INTEGER && GetShortInt ( input , & inOutIdx , & keySz , sz ) < 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } } if ( seqEnd > inOutIdx ) { if ( GetAlgoId ( input , & inOutIdx , & oid , oidHmacType , sz ) < 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } shaOid = oid ; } #ifdef WOLFSSL_SMALL_STACK cbcIv = ( byte * ) XMALLOC ( MAX_IV_SIZE , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( cbcIv == NULL ) { ERROR_OUT ( MEMORY_E , exit_dc ) ; } #endif if ( version == PKCS5v2 ) { if ( GetAlgoId ( input , & inOutIdx , & oid , oidBlkType , sz ) < 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } if ( CheckAlgoV2 ( oid , & id , NULL ) < 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } if ( shaOid == 0 ) shaOid = oid ; ret = GetOctetString ( input , & inOutIdx , & length , sz ) ; if ( ret < 0 ) goto exit_dc ; if ( length > MAX_IV_SIZE ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } XMEMCPY ( cbcIv , & input [ inOutIdx ] , length ) ; inOutIdx += length ; } if ( GetASNTag ( input , & inOutIdx , & tag , sz ) < 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } if ( tag != ( ASN_CONTEXT_SPECIFIC | 0 ) && tag != ASN_OCTET_STRING ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } if ( GetLength ( input , & inOutIdx , & length , sz ) < 0 ) { ERROR_OUT ( ASN_PARSE_E , exit_dc ) ; } ret = wc_CryptKey ( password , passwordSz , salt , saltSz , iterations , id , input + inOutIdx , length , version , cbcIv , 0 , shaOid ) ; exit_dc : #ifdef WOLFSSL_SMALL_STACK XFREE ( salt , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; XFREE ( cbcIv , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif if ( ret == 0 ) { XMEMMOVE ( input , input + inOutIdx , length ) ; ret = length ; } return ret ; }
decrypt PKCS
 NOTE: input buffer is overwritten with decrypted data!
decrypt PKCS NOTE : input buffer is overwritten with decrypted data!
/* PKCS version always 2nd to last byte */ /* version.algo, algo id last byte */ /* Algo ID error */ /* Find the end of this SEQUENCE so we can check for the OPTIONAL and
     * DEFAULT items. */ /* OPTIONAL key length */ /* DEFAULT HMAC is SHA-1 */ /* get encryption algo */ /* PKCS v2 algo id error */