public BlobStoreTest() { LoggerUtils.setupLoggers(); List<CardInfo> lst = SmartCardUtils.GetReaderNames(); String readerName = lst[0].ReaderName; smartCard = new SmartCard(readerName, pin); CardMode mode = this.smartCard.GetCardMode(); if (mode != CardMode.ROOT) { this.smartCard.SetCardInRootMode(); } BigInteger.TryParse(pString, out p); BigInteger.TryParse(qString, out q); KeyPair pq = new KeyPair(p, q); String puk = this.smartCard.InitDevice(pq, pin); }
private void initCardButton_Click(object sender, RoutedEventArgs e) { using (SmartCardTransaction scT = new SmartCardTransaction(this.smartCard.device)) { string puk; try { String pin = "1234"; KeyPair pq = new KeyPair(p, q); puk = this.smartCard.InitDevice(pq, pin); } catch (ErrorCode ex) { String msg = String.Format("Could not INITIALIZE DEVICE. error code: {0} {1}", ex.SW1, ex.SW2); System.Windows.MessageBox.Show(msg); } } this.CleanAndClose(); }
/// <summary> /// This is the descrypt method from the ABC4Trust Lite doc appendix B p. 227 /// It will decrypt the chipher based on the N provided. /// Please note that the smartcard return Big-endian byte stream and descrypt expect /// little endian data. /// </summary> /// <param name="keyPair">keys used to decrypt the data</param> /// <param name="chipher">data to descrypt</param> /// <returns>Byte stream of the descrypted data.</returns> public static byte[] Decrypt(KeyPair keyPair, byte[] chipher) { BigInteger d = ModInverse(new BigInteger(3), keyPair.Phi); BigInteger iChiper = new BigInteger(chipher); BigInteger plain = BigInteger.ModPow(iChiper, d, keyPair.N); byte[] NArray = keyPair.N.ToByteArray(); byte[] plainBytes = plain.ToByteArray(); Array.Reverse(plainBytes, 0, plainBytes.Length); byte[] plainBytesWithZero = new byte[NArray.Length]; Buffer.BlockCopy(plainBytes, 0, plainBytesWithZero, plainBytesWithZero.Length - plainBytes.Length, plainBytes.Length); byte[] pad = new byte[plainBytesWithZero.Length - 32]; byte[] h = new byte[32]; Buffer.BlockCopy(plainBytesWithZero, 0, pad, 0, pad.Length); Buffer.BlockCopy(plainBytesWithZero, pad.Length, h, 0, 32); SHA256 shaM = new SHA256Managed(); byte[] hPrime = shaM.ComputeHash(pad); if (!Utils.ByteArrayCompare(hPrime, h)) { Utils.PrintByteArrayToConsole(hPrime); Utils.PrintByteArrayToConsole(h); } shaM.Clear(); //dispose the sha256 object. byte[] L = new byte[2]; Buffer.BlockCopy(pad, 1, L, 0, 2); Array.Reverse(L, 0, L.Length); int LSize = BitConverter.ToInt16(L, 0); byte[] data = new byte[LSize]; Buffer.BlockCopy(pad, 3, data, 0, data.Length); return data; }
public String InitDevice(KeyPair keyPair, String pin) { byte[] pk = keyPair.N.ToByteArray(); Array.Reverse(pk, 0, pk.Length); ErrorCode errPutData = device.PutData(pk); if (!errPutData.IsOK) { errPutData.Command = "PutData"; DebugUtils.DebugPrintErrorCodes(errPutData); throw errPutData; } ErrorCode errSetAuth = device.SetAuthKey(0x00); if (!errSetAuth.IsOK) { errSetAuth.Command = "SetAuthKey"; DebugUtils.DebugPrintErrorCodes(errSetAuth); throw errSetAuth; } byte[] rawData; ErrorCode errInitDevice = device.InitDevice(new byte[] { 0x00, 0x01}, new byte[] {0x00, 0x20 }, out rawData); if (!errInitDevice.IsOK) { errInitDevice.Command = "InitDevice"; DebugUtils.DebugPrintErrorCodes(errInitDevice); throw errInitDevice; } Array.Reverse(rawData, 0, rawData.Length); byte[] decryptData = SmartCardCrypto.Decrypt(keyPair, rawData); byte[] initPin = new byte[4]; byte[] initPuk = new byte[8]; Buffer.BlockCopy(decryptData, 0, initPin, 0, 4); Buffer.BlockCopy(decryptData, 4, initPuk, 0, 8); byte[] pinA = Encoding.ASCII.GetBytes(pin); ErrorCode errSetPin = device.SetPin(initPin, pinA); if (!errSetPin.IsOK) { errSetPin.Command = "SetPin"; DebugUtils.DebugPrintErrorCodes(errSetPin); throw errSetPin; } return BitConverter.ToString(initPuk); }
public void InitDevice() { try { CardMode mode = this.smartCard.GetCardMode(); if (mode != CardMode.ROOT) { this.smartCard.SetCardInRootMode(); } KeyPair pq = new KeyPair(p, q); String puk = this.smartCard.InitDevice(pq, pin); } catch (ErrorCode ex) { Assert.Fail(String.Format("Reset the device failed: {0}:{1} with command {2}", ex.SW1, ex.SW2, ex.Command)); } catch (Exception ex) { Assert.Fail(String.Format("Reset the device failed: {0}", ex.Message)); } }