This paper investigates the border between formal employment, shadow employment, and unemployment in an equilibrium model of the labor market with market frictions. From the labor demand side, firms optimally create legal or shadow employment through a mechanism that is akin to tax evasion. From the labor supply side, heterogeneous workers sort across the two sectors, with high productivity workers entering the legal sector. Such worker sorting appears
fully consistent with most empirical evidence on shadow employment. The model sheds also light on the "shadow puzzle", the increasing size of the shadow economy in OECD countries in spite of improvements in technologies detecting tax and social security evasion. Shadow employment is correlated with unemployment, and it is tolerated because the repression of shadow activity increases unemployment. The model implies that shadow wage gaps should be lower in depressed labor markets and that deregulation of labor markets is accompanied by a decline in the average skills of the workforce in both legal and shadow sectors. Based on micro data on two countries with a sizeable shadow economy, Italy and Braziil, we find empirical support to these implications of the model. The paper suggests also that policies aimed at reducing the shadow economy are likely to increase unemployment.