NEW DELHI - India's top court Wednesday upheld the legality of the government's Aadhaar national ID scheme, the world's largest biometric database, but imposed new restrictions on how the personal details of more than one billion citizens can be used.

The ruling by a five-judge bench of the Supreme Court draws a line under many years of legal challenges from critics, who said Aadhaar threatened individual privacy and risked turning the world's second-most populous nation into a surveillance state.

The government had insisted Aadhaar, which issues every Indian with a unique 12-digit ID linked to fingerprints and iris scans, would streamline welfare services and root out fraud.

It was also pitched as a transparent way to ensure that government handouts of food, fuel and other essentials to India's poorest would not be siphoned off by corrupt middlemen, a huge problem in the vast country of 1.25 billion.

In its ruling the Supreme Court upheld the legality of the scheme, saying the benefits far outweighed any risks.

The judges ruled that anybody drawing on government services - from filing tax returns to accessing pensions or welfare - would by law require an Aadhaar number.

"Supreme Court judgement on Aadhaar is a big win for the government," said Amit Malviya, a spokesman for the ruling Bharatiya Janata Party (BJP) on Twitter.

But the bench said corporations could not force customers to hand over their Aadhaar data - a key complaint from privacy advocates who say the programme had gone too far.

"I am happy with the Aadhaar ruling. It should be made mandatory only where it is really needed," said senior BJP figure Subramanian Swamy after the ruling.

- Balanced verdict -

Indians will no longer be compelled to provide their unique ID to register for services like mobile phone numbers or bank accounts, something critics said gave companies undue access to private data.

"It is a balanced verdict," Ritesh Bhatia, cyber crime investigator and data privacy expert, told AFP.

"The big thing is we don't have to give our personal details to mobile phone companies, which is a big relief as I won't be harassed with texts and calls any more."

The scheme was rolled out under the previous Congress Party government in 2010, but grew in scope under Prime Minister Narendra Modi, whose BJP swept to power in 2014.

Initially a voluntary programme, it evolved into a national ID mandatory for those wishing to access government services.

Even schoolchildren wanting free lunches provided by the state needed an Aadhaar - a controversy the court struck down in its landmark ruling.

"No child can be denied any scheme if they are not able to bring their Aadhaar number," the court said.

It also said students could not be denied enrolment if they did not possess the ID, saying a right to education was fundamental to all Indians.

The scheme has been dogged by controversy.

In October, an 11-year-old girl from an impoverished part of eastern India allegedly died of starvation after her family were unable to link their ration card to Aadhaar.

There were also scandals around Aadhaar data being allegedly sold online by criminals, and journalists being pursued over reporting on these breaches.

"India shouldn't be conditioning access to government necessities on possession of a biometric ID, nor should it be targeting journalists and researchers who write about vulnerabilities in the system's privacy protection," Kenneth Roth, executive director of Human Rights Watch, tweeted after the verdict.

Last year, in a case linked to the biometric database, the government went to the Supreme Court to argue that Indians did not have a fundamental right to privacy - a case that it lost.

The Supreme Court in January began hearing a series of challenges to the constitutional validity of Aadhaar, a Hindi word meaning "foundation".