Since you asked about the 10% Federal penalty and taxable portion my answer is about that. Mhoran’s answer points out additional state tax implications.
The short answer is that the penalty is on the earnings portion only.
And the short answer for how much of the distribution is from earnings is that it comes from the amounts the 529 plan provider reports on form 1099Q.
The longer answer is that Publication 970 describes how to calculate a taxable distribution based on the amount a distribution is more than qualified education expenses and take into account various exceptions. It also states that
Generally, if you receive a taxable distribution, you also must pay a 10% additional tax on the amount included in income.
The really long answer for how the earnings portion of a distribution is determined (ie what the plan provider puts on form 1099Q) is given in two references that the instructions to form 1099Q only list and link to:
Box 2. Earnings
To determine the earnings or (loss) on the gross distribution reported in box 1, use the earnings ratio described in Proposed Regulations section 1.529-3, Notice 2001-81, and Notice 2016-13. You can find Notice 2001-81 on page 617 of Internal Revenue Bulletin 2001-52 at IRS.gov/pub/irs-irbs/ irb01-52.pdf and Notice 2016-13 at IRS.gov/irb/ 2016-07_IRB/ar06.html.
Basically the revenue bulletin and notice describe the timing of account values to use to make a ratio of the earnings portion of the account to the total value of the account. And that ratio is used to determine the amount of earnings to allocate to that distribution.