OTTAWA -- The Supreme Court of Canada has ruled that the Harper government's law requiring mandatory minimum sentences for gun crimes is unconstitutional.

By a 6-3 margin, the high court has upheld the 2013 Ontario Court of Appeal ruling that labelled the law cruel and unusual and struck it down.

The ruling is a setback for the government's tough-on-crime agenda.

The court was deciding two appeals involving mandatory minimum sentences for gun crimes brought by provincial and federal attorneys general.

The appeal court struck down both the three-year mandatory minimum for a first offence of possessing a loaded prohibited gun, as well as the five-year minimum for a second offence.

The Ontario and federal governments wanted the Supreme Court to reverse the decision, arguing that the minimums do not breach the charter protection against cruel and unusual punishment.

The new sentencing rules were enacted in 2008 as part of a sweeping omnibus bill introduced by the federal Conservatives.

The two governments argued the move was an effort to combat a serious danger posed by the proliferation of handgun possession cases.

In one of the appeals at issue, a young Toronto man with no criminal record was sentenced to three years after pleading guilty to possession of a loaded firearm.

The judge said that without the mandatory minimum, he would have sentenced Hussein Nur to two-and-a-half years.

In the second case, Sidney Charles pleaded guilty to firearms offences after he was found in his rooming house bedroom with a loaded and unlicensed semi-automatic handgun. He was sentenced to five years because he had two previous convictions.

In defending the mandatory sentence for repeat offenders, Ottawa and Ontario argue that it is within a reasonable range of legislative choice.

The Supreme Court has clashed with the Conservative government on several key policies, although it recently sided with Ottawa over the destruction of gun registry data, which Quebec sought to preserve.

That win for the Conservatives came after several losses at the Supreme Court.

The justices rejected Harper's appointment of Quebec judge Marc Nadon to their ranks, stymied an effort to stop judges from giving extra credit for time spent in custody before sentencing and ruled that Parliament could not reform the Senate on its own.