iso7816/command/
instruction.rs

1use core::ops::{BitAnd, BitOr};
2
3#[derive(Copy, Clone, Debug, Eq, PartialEq)]
4pub enum Instruction {
5    Select,
6    GetData,
7    Verify,
8    ChangeReferenceData,
9    ResetRetryCounter,
10    GeneralAuthenticate,
11    PutData,
12    GenerateAsymmetricKeyPair,
13    GetResponse,
14    ReadBinary,
15    WriteBinary,
16    // Unknown(BinaryInstruction),
17    Unknown(u8),
18}
19
20pub struct UnknownInstruction {}
21
22impl From<u8> for Instruction {
23    fn from(ins: u8) -> Self {
24        match ins {
25            0x20 => Instruction::Verify,
26            0x24 => Instruction::ChangeReferenceData,
27            0x2c => Instruction::ResetRetryCounter,
28            0x47 => Instruction::GenerateAsymmetricKeyPair,
29            0x87 => Instruction::GeneralAuthenticate,
30            0xa4 => Instruction::Select,
31            0xc0 => Instruction::GetResponse,
32            0xcb => Instruction::GetData,
33            0xdb => Instruction::PutData,
34            0xb0 => Instruction::ReadBinary,
35            0xd0 => Instruction::WriteBinary,
36            ins => Instruction::Unknown(ins),
37        }
38    }
39}
40
41impl From<Instruction> for u8 {
42    fn from(instruction: Instruction) -> u8 {
43        match instruction {
44            Instruction::Verify => 0x20,
45            Instruction::ChangeReferenceData => 0x24,
46            Instruction::ResetRetryCounter => 0x2c,
47            Instruction::GenerateAsymmetricKeyPair => 0x47,
48            Instruction::GeneralAuthenticate => 0x87,
49            Instruction::Select => 0xa4,
50            Instruction::GetResponse => 0xc0,
51            Instruction::GetData => 0xcb,
52            Instruction::PutData => 0xdb,
53            Instruction::ReadBinary => 0xb0,
54            Instruction::WriteBinary => 0xd0,
55            Instruction::Unknown(ins) => ins,
56        }
57    }
58}
59
60impl BitAnd for Instruction {
61    type Output = Self;
62    fn bitand(self, rhs: Self) -> Self::Output {
63        let rhs: u8 = rhs.into();
64        let this: u8 = self.into();
65        (this & rhs).into()
66    }
67}
68
69impl BitOr for Instruction {
70    type Output = Self;
71    fn bitor(self, rhs: Self) -> Self::Output {
72        let rhs: u8 = rhs.into();
73        let this: u8 = self.into();
74        (this | rhs).into()
75    }
76}
77
78// impl TryFrom<u8> for Instruction {
79//     type Error = UnknownInstruction;
80
81//     fn try_from(ins: u8) -> Result<Self, Self::Error> {
82//         let instruction = match ins {
83//             0x20 => Instruction::Verify,
84//             0x24 => Instruction::ChangeReferenceData,
85//             0x2c => Instruction::ResetRetryCounter,
86//             0x47 => Instruction::GenerateAsymmetricKeyPair,
87//             0x87 => Instruction::GeneralAuthenticate,
88//             0xa4 => Instruction::Select,
89//             0xc0 => Instruction::GetResponse,
90//             0xcb => Instruction::GetData,
91//             0xdb => Instruction::PutData,
92//             _ => return Instruction::Unknown(ins),
93//             Err(UnknownInstruction {})
94//         };
95
96//         Ok(instruction)
97//     }
98// }