Under the Criminal Justice Act 1988, 1988 c. 33, Part XI, Torture, Section 134, the essence of the crime is that the actor "intentionally inflicts severe pain or suffering on another ...." The US statute (18 USC 2340-2340B) is similar ("specifically intended"), but explicitly enhances penalties where death results.

It is not enough to prove that serious pain or suffering was inflicted. The added requirement is that the actor intended that result. Thus, the actor's state of mind is all important. For example, if the actor inflicts serious pain or suffering negligently or recklessly, the crime of torture is not committed - other crimes are possible, but not torture.

The defense deals with situations where the actor intentionally inflicts severe pain or suffering on another, but where:

(4) It shall be a defence for a person charged with an offence under this section in respect of any conduct of his to prove that he had lawful authority, justification or excuse for that conduct.
For example, a law enforcement officer severely injures (or kills) someone in the course of apprehension, in defense of self or others, or a "fleeing felon" if that is allowed by the applicable local law.

If we have a "wartime" situation, intentional infliction of severe pain or suffering upon enemy combatants is the norm; and, in fact, may extend to civilians - even though they are known to be within the scope of the force applied against the enemy combatants.

For example, a half dozen men (all PID'd as enemy combatants) enter a house by one entrance. A half dozen women and children (with no apparent link to the men) enter it by another entrance. Both groups are fleeing from a surrounding firefight. If a 500 lb bomb is used to level the house, the official calling that shot may well be within the law (Laws of War, LOAC, IHL), even though knowing full well that the strike will result in severe pain or suffering (and likely, death) to the women and children.

We (US) have the "justification defense" generalized in Rule 916 of our 2012 Manual for Courts Martial; at II-110 & II-111:

(c) Justification. A death, injury, or other act caused or done in the proper performance of a legal duty is justified and not unlawful.

Discussion

The duty may be imposed by statute, regulation, or order. For example, the use of force by a law enforcement officer when reasonably necessary in the proper execution of a lawful apprehension is justified because the duty to apprehend is imposed by lawful authority. Also, killing an enemy combatant in battle is justified.
(d) Obedience to orders. It is a defense to any offense that the accused was acting pursuant to orders unless the accused knew the orders to be unlawful or a person of ordinary sense and understanding would have known the orders to be unlawful.

Discussion

Ordinarily the lawfulness of an order is finally decided by the military judge. See R.C.M. 801(e). An exception might exist when the sole issue is whether the person who gave the order in fact occupied a certain position at the time.

An act performed pursuant to a lawful order is justified. See subsection (c) of this rule. An act performed pursuant to an unlawful order is excused unless the accused knew it to be unlawful or a person of ordinary sense and understanding would have known it to be unlawful.
The major material point of all this is that "justification" situations are very much fact dependent, which requires one to look at those facts from the viewpoint of the actor, his knowledge and his intent.

Regards

Mike