Image caption As cabinet secretary, Sir Jeremy Heywood is the most senior civil servant in the country and a key adviser to David Cameron

Britain's top civil servant has warned ministers that banning EU migrants from claiming tax credits for four years, a key demand in their EU renegotiations, could be found to be illegal.

The BBC understands Sir Jeremy Heywood has said any ban lasting longer than six months could be discriminatory and may be cut to a "few weeks or months".

David Cameron is expected to set out his goals in more detail next week.

They are still expected to include the four year ban on tax credit claims.

But BBC's Newsnight understands legal advice from the European Commission is thought to have recommended this would amount to discrimination of non-British migrants and be illegal.

Mindful of the advice from European officials, the Cabinet Secretary has told ministers to revise down expectations of what can be achieved from EU renegotiations in relation to limiting access to tax credits.

Sir Jeremy is understood to believe the government has three options.

Firstly it could attempt to get treaty change on the matter, something he believes to be unlikely

Secondly, the government could ensure any policy affecting EU migrants would also affect British citizens to avoid accusations of discrimination

It could limit tax credits but only for a few weeks or months, not years, an option seen as being more likely

Action on migrants benefits is the most important of the policies the prime minister will next week put to Donald Tusk, president of the European Council, in a long-awaited letter.

Last November David Cameron made stopping working migrants from receiving benefits before they had been resident in the UK for four years a red line for Britain staying in the EU.

In summary: EU negotiations

Image copyright PA Image caption The prime minister's letter will give greater insight into what he wants from the negotiations

David Cameron says he has a mandate to pursue EU reform following the Conservatives' general election victory.

The PM wants to renegotiate the terms of the UK's membership ahead of a referendum by the end of 2017. He has said he will campaign for Britain to remain in the EU if he gets the reforms he wants.

What Britain wants from Europe

EU vote: When, what and why?

Osborne: UK can get 'best of both worlds'

At the time, he said: "I will insist that in the future those who want to claim tax credits and child benefit must live here and contribute to our country for a minimum of four years.

"If I succeed, I will... campaign to keep this country in a reformed EU. If our concerns fall on deaf ears… then of course I rule nothing out."

It is known that officials in the European Commission in Brussels have cautioned against the British government expecting to win the four year ban on tax credits for EU migrants to Britain.

In recent months, David Cameron has come under pressure to reveal his requests after other European countries complained they did not know what measures the prime minister wanted changing.

He will now set out his initial demands in a letter next week ahead of the summit of European leaders in December.

German pressure

Charles Grant, director of the Centre for European Reform, told BBC Newsnight that restrictions on access to benefits was Downing Street's "most important" demand.

"They made that demand as they see it as a more moderate request than their initial idea to have quotas on numerical limits on EU migrants coming into the UK," he said.

"Under pressure from Angela Merkel, the PM backed down on that, and came up with this four year demand instead.

"The trouble is it's illegal because it would discriminate against EU citizens on the basis of their nationality.

"The legal experts say that if you go for, say, a one or two-year residency qualification it's not much better than four years - it still could be seen as discriminatory. However, when you go down to months rather than years it would be much easier to get away with, and I don't think the British would have too much of a problem getting away with that rule".

But Raoul Ruparel, from the think tank Open Europe - which campaigns for EU reform, told Newsnight: "If David Cameron comes back with a ban of less than year or two he's going to have a very hard time selling that to the British public, and it may even lead to people voting out."

A spokesman for the Cabinet Office declined to comment.