The former head of NHS Digital has said he was put under "immense pressure" by the Home Office under Theresa May to release data on immigrants despite his concerns over its legality.
Kingsley Manning said he was challenged for "daring" to question if there was a legal basis for handing over confidential patient data that would help the Home Office trace suspected illegal immigrants.
Last month, the Home Office published an agreement showing the basis by which information can be requested from NHS Digital.
But Mr Manning said the NHS body has been forced to hand over data that the Home Office would find useful since "at least" 2005, sometimes to junior officials who would just "ring up" and ask for it.
In an interview with the Health Service Journal (HSJ), Mr Manning said: "We said to the Home Office: 'We need to understand what the legal basis of this is.'
"The Home Office response was: 'How dare you even question our right to this information. This is data that belongs to the public. It is paid for by the taxpayer. We should use it for public policy'."
When Mr Manning launched a review to establish a clear legal framework for the data sharing, there was an "enormous reluctance from both the Home Office and the Department of Health to clarify any element of this process", he said.
"The Home Office view was that tracing illegal immigrants was a manifesto commitment.
"If I didn't agree to co-operate (with the sharing of patient data) they would simply take the issue to Downing Street."
The new agreement announced last month has "maximised" Home Office powers "to the absolute limit", Mr Manning said.
He added: "All the Home Office has to say now is: 'We have lost track of this individual', and NHS Digital will have to hand over confidential patient information, such as the patient's name, gender, date of birth, last registered address and area details of their GP.
"There is no provision for transparency, no provision for oversight or scrutiny and there is no role for the National Data Guardian.
"Nor is there any provision to alert patients to the possibility that information from their NHS patient record could be passed on to the Home Office."
Mr Manning said he had become "deeply concerned" once he took the helm of the NHS body in 2013 that data had been handed over since at least 2005.
"We were supposed to be a safe haven for patient data, and here we were giving out information on thousands of people a year to the Home Office, without any transparency or oversight," he said.
And he said the Home Office was "pretty upset" by the idea that the number of tracing requests from the Government department should be published.
He said: "I made it clear that NHS Digital needed to understand the legal basis for the exchange of data.
"We were in a position where we would have a relatively junior official from the Home Office ring up and say: 'On my say-so I'd like the information on the following person'.
"But as far as I could see there was no reporting of this, other than as a declaration."
Mr Manning commissioned an external review, chaired by Professor Maria Goddard, director of the Centre for Health Economics at York University.
The review has been completed but not published.
He said: "I came under immense pressure to leave matters as they were - particularly after the 2015 election when immigration became a fundamental political issue.
"The threat was that if we pursued this line of questioning we would be deemed to be an 'insufficient partner' within the system.
"My key concern has always been that highly vulnerable people will be deterred from accessing the health system because they are worried that their information will be shared with the Home Office."
A Government spokesman said: "We share limited information between health agencies and the Home Office to trace immigration offenders and vulnerable people, and prevent those without the right to access benefits and services doing so at the expense of the UK taxpayer.
"Access to this information is strictly controlled, with strong legal safeguards. No clinical information is shared, and before anything at all is shared there has to be a legal basis to do so.
"Immigration officials only contact the NHS when other reasonable attempts to locate people have been unsuccessful."
Mr Manning told the BBC Radio 4 Today programme that when he became chairman he was concerned about data being passed to both the police and Home Office.
He said there was no problem in providing information on a defined legal basis to the police or security services, but questioned its use in immigration matters.
He said NHS Digital is "a safe haven for your data, in this case, except if you are an immigrant, except if the Home Office wants it".
Around 10,000 people a year were being traced by the Home Office through NHS Digital, he added.
Figures show that from September to November last year, the Home Office made 2,224 requests for information.
A Government spokesman said: "We share limited information between health agencies and the Home Office to trace immigration offenders and vulnerable people, and prevent those without the right to access benefits and services doing so at the expense of the UK taxpayer.
"Access to this information is strictly controlled, with strong legal safeguards. No clinical information is shared, and before anything at all is shared there has to be a legal basis to do so. Immigration officials only contact the NHS when other reasonable attempts to locate people have been unsuccessful."