Tuesday 24 November 2015

Reverse Engineering A Eftpos Machine Part 2



Its about time i update my progress:
Chip listing and datasheet sourcing :85% 
Keypad side: 
1. 7660cbaz (( Simple Conversion of +5V Logic Supply to +/-5V Supply + Features))
2. SP232Enc - ((High Performance 2x RS-232 Line Drivers/Receivers))

Mcu side(back):
1. HC4053 - ((ANALOGMULTIPLEXER/DEMULTIPLEXER: TRIPLE2CHANNEL)) x 2
2. FT232BL - ((FT232BL/BQ USB UART IC))
3. 93LC46-81 - ((1K/2K/4K 2.5V Microwire Serial EEPROM))
4. MC33560 - ((Power Management and Interface IC for Smartcard Readers and Couplers)),
5. LM2597 -  ((Voltage Regulator, with Features))
6. Hm628100l - Wide Temperature Range Version 8 M SRAM (1024-kword ´ 8-bit),
7. ADG713-BR - ((CMOS Low Voltage 4 OHM Quad SPST Switches)),
8. ST16c550 - ((UART WITH 16-BYTE FIFO’s))
9. ispLSI 2032e - ((In-System Programmable SuperFAST™ High Density PLD))
10. Apex HG128x64(Y?****)
11. **Maxim DS5250 ((High Speed Secure Microcontroller With Self Destruct Input))**



So far i have been using inkscape(art vector program) to create layers of the board,
the biggest issue with this program is no matter how much ram you have
even if you have a new shiny 64 bit machine
inkscape cant handle too much at once without employing several layers 
and turning them off  and on as needed, i will elaborate more on that later.

First i take as many high resolution photos of both sides of the board 
documenting all the chips that i can, sometimes the lettering isn't visible
even under a bit of magnificationso i normally take a UNI Posca
and in a light colour cover the chip markings with it, 
after letting it dry i wipe it off with a cotton sightly moist cotton tip
this leaves the lettering in a nice readable state.

Then all components have to come off the board, bagging them up for later use,
 a quick wipe off with some isopropyl alcohol and take some high resolution scans, 
i scan the board in as high a resolution(dpi) as possible.
 (the file sizes may get a bit excessive 20-70mb each if bmp) 

At this point we should have at least 4 photo/scans, 
i have many more close ups taken of various “sections”,
because ultimately we will need all the locations of the chips, 
polarity markings and even the silkscreen-
lettering will add to the arsenal to reverse the working of the board.
A thing to note when scanning these images:
ensure they are all taken with the board on the same angle in relation to the scanner,
 this will help to line up the scans later on without having to rotate from the edges,
as you can see the front or back image will have to be mirrored
in order to show its actual position in 3d space but we do this in inkscape.







 Next step is to get some sandpaper and a block, ensuring you have on gloves and a respirator
(kinda just good OHS),
 a few rubs of the sandpaper over the top layer will reveal most of the top layer traces,
 we are removing as much of the silk screen and solder as possible without going down to far,
 you can stop once you feel you have removed enough to take some clean shots.
Water and a cloth with surely help with getting rid of the dust off the board,
 also as we have agitated the copper and introduced oxygen,
 it will soon start to tarnish so try and take your shots as soon as possible,
 wiping with a moist cloth will help to revive the copper a little for a short period but once its done its done.
The best example of this is the difference in photo to scans that i have that were taken 12+ hrs apart below





These shots arent high resolution as it would be silly for  you to view a 100mb file
But they do show the process so they help to use as pointers.


These next few layers should be scanned regularly, 
i made the mistake of only taking photos and have to go back and redo them, 
as these layers may contain traces and via exits(the layer a via is connected to) 
they make it much easier to make sense of the power distribution layers and 
the ground planes across the entire board front and back.
Here i have two photos one of the back and one of  the front where i have tried to mark the vias that end on this layer, this is much easyier with hd scans.



Documenting as many of  the previous layers is the best plan of attack,
as the more reference materials you posses the more power you hold which
means less effort you have to exert.

Now if like me you start to come to the middle and start to see bond(micro) wires slow down, 
ensure you have both layers at the same point and clean up as much as you can
 while taking off little as possible and then time for some more scans,
 once again your dpi is very important here as high as you can go is best 
and as hard as it may seem you need to keep the scans rather straight when flipping the board over,
 if you take a point and ensure it stays the same distance away from the edge or similar feature.




So these are the back and front respectively,
Now the predictable parts to this so far are based solely 
on the fact that in order to carry certain frequencies and amounts 
of power these wires must be a minimum size and adhere to several rules of electrical design,
 as i said in my previous post i am not trained in electrical design rules 
but some quick searches quickly bring up many factors that would be at play here.
I started with this :

http://sjo.cn/downloads/ebook/High-Speed%20Digital%20System%20Design.pdf
High-Speed Digital System Design : A Handbook of Interconnect Theory and Design Practices
 Stephen H. HallGarrett W. HallJames A. McCall

These wires are grouped in buses and in some cases actually cross over one other.

Now we begin the hard part of reconstructing the images and tracing out the components 
around the board to ultimately draw up the rough schematic 
and then after adding in some values we get the final schematic. So lets begin..

First open inkscape then File>Import and select your first layer scan (most populated back in my case)
Leave the import settings as they are :



Im unsure if linking the file will make it any less ram intensive but you may change it if you like,
Next step is to resize the canvas the easiest way to do this is ensure the picture is selected
 then File>Document Properties> Click <+Resize page to Content+> 
then click [Resize page to drawing or secection]

Now we are right to start laying them together, if the layers bar is not up press Shft+Ctrl+L to bring it up.

Press the PLUS(+) button to add a few layers above the current layer as we will need them.

Our picture is now in layer 1 we can click the eye 
and lock next to layer 1 this will hide and lock the picture for now

Clicking on layer 2 we can now import in our next layer (the front layer in my case), 
if you haven't already its time to mirror this image, 
simply clicking it and then pressing the mirror button on the toolbar will flip it over.

Change the layer names to correspond to the layers of your board by simply double clicking its name,
Now unhide the back layer then click back onto your other layer
 change the opacity until you can line up the edges between the two
 looking for distinct marks/slots will help a great deal,
in my case its the circles that help to ensure its lined up at least at that point 
 and then stretching the edges so that the corners line up,
rinse and repeat with the inner layer, you may not have to use both the front and back scans
 but sometimes they are quite different as to how visible a certain wire is,
 see red and black colourings on my inner scans, it becomes much easier to line every thing up
 once the inner layer is applied.




This is with the microwire layer on top of  the front layer(keypad in this case) and set on 51.4% Opacity




This is the microwire layer ontop of Component layer and set on once again around 50% Opacity







My next step is to take the pinout diagrams from the datasheets
 to lay over the board in the respective places,
 pay close attention to the original photos of your board
 and take note of where the circles or tabs are to indicate pin orientation,
 the chips on the layer you flipped will have to be flipped as well, 
make sure you check and then check again that the ground plane on your board
 is where the datasheet says the chips gnd 
will be this will always help you realise if your work is correct when they line up, 
if they don't you may have the wrong chip datasheet(wrong part, wrong variation) 
it may just be that you forgot to flip it, these will line up,
 some of them you may have to edit to make the spacings between the pins appropriate,


If when you import the diagram it appears like this one above simply change the blend mode to mutiply


Place the components into individual layers so you can turn them off, 
you can even add sublayers so that you have a diagram in on sublayer,
 a copper or top sublayer, a microwire layer like so.



From this point depending on how complex your design is,
 your are best off  too start your tracing back from anything that exits the board,
 i started with the LCD and then moved onto the rj-45 jack as it contained the power entry points 
and the data entry/exit points via process of elimination you can work out a majority of the board.






My start looked like this by looking at the area with the micro wire layer visible
 as well as the top trace layer you can mark out the traces 
and microwires in colours to help you understand them as they travel across the board.







 Looking at this part of  the photos as well you can see where the components are and such.



And when i started to slow up it was looking like this 


At this stage my task manager looks like this when everything is turned on 


It can get much heavier and once it gets towards 2gb it crashes so saving frequently is advised.




The Next step is to start to make a transition to a schematic program( i use NI multisim its just easy) 
that way you can really start to see what is going on, 
i once again start to work from the rj-45 / sp232,
 the workings just unravel from that point but lead us to a old enemy the missing data sheets


 To complete the schematic you will need to map out all the various components values 
and enter them into your schematic.






My main issues and hurdles in this was that i never obtained the ds5250(main MCU) datasheet
this was quickly identified when i was checking between the photos
 and marking out parts that these where of as much importance as the rest of the components, 
luckily i had quite a few boards 
so i snapped some photos and hoped i could find the datasheets otherwise 
once i had this realisation i knew i was going to have to work on probability 
in regards to which pin on the mcu it was that i was connecting either to or from,
 i would have to look at as many variations of publicly documented chips in the Ds5*** Family 
and use what data i could publicly derive from the actual ds5250 data, 
these being a few references to directly swapping the ds5250 80pin MQFP 
and some other little tidbits,i am unfortunately looking at the 100pin varation,
 not to fear though, 
the data available leads to some good conclusions

These  Are so far: 
1. APPLICATION NOTE 2034 (USING THE DS5250 AS A DROP-IN UPGRADE FOR THE DS5002)

2. APPLICATION NOTE 3294 (Increasing System Security by Using the DS5250 as a Secure Coprocessor)

3. APPLICATION NOTE 4004 (RSA Key Generation in DS5250)

4. APPLICATION NOTE 4095 (INTERFACING THE DS5250 WITH A MAGNETIC STRIPE CARD READER)




Reading : APPLICATION NOTE 3294
Increasing System Security by Using the DS5250 as a Secure Coprocessor
 This shows many ways the chip can be employed and many suited usages


Reading : APPLICATION NOTE 2034 (USING THE DS5250 AS A DROP-IN UPGRADE FOR THE DS5002)
The DS5250 high-speed secure microcontroller in the 80-pin quad flat pack (QFP) package is a
pin-compatible, high-performance upgrade for the DS5002FP.

Table 1. DS5002FP and DS5250 Features

Performance:
DS5002FP     DS5250  FEATURES 

16MHz    25MHz  Maximum clock frequency

12          4   Clocks-per-machine cycle

25.2   8.4*  Average clocks per instruction

0.63   3.9*  Average MIPS

SECURITY:

80-bit proprietary algorithm  Single DES or 3DES  Encryption of data in program (single byte) (8-byte block)  memory (decrypted in parallel 
with program execution)

                     

No  Yes Separate encryption for program and  MOVX memory


None 4096-bit MAA engine
(1024-bit public key
modular exponentiation      Public key cryptography support
  in under 650ms)



SDI pin SDI pin: no
battery/battery attach; Destructive reset (DRS) triggers
low temperature (< 60°C)**

No  Yes (optional)  Timed access on port write

48 bytes  1024 bytes  Vector RAM

No  Yes  Unique laser ID

No  Yes  Secure loader


FLEXIBILITY:

1x, idle, stop 1x, idle, stop; 2x/4x Oscillator clock modes
(crystal multiplier); divide
by 1024 (PMM); internal ring oscillator

Serial ports

Timers

2  6  External interrupts

No Hardware and loader       Flash memory support

ROM
No Yes** Real-time clock
*Zero stretch cycles, 16-bit standard addressing mode.
**100-pin QFP only.



Requirements for Upgrading a Design to the DS5250
The DS5250 supports most of the DS5002FP features. Exceptions are:
• The DS5250 accesses all external memory (or memory-mapped I/O) using the dedicated
data bus (BA14–BA0 and BD7–BD0). Accessing memory using a multiplexed address/data
bus on ports 0 and 2 is not supported.
• The program memory cache allows for the fastest possible program execution. The fills and
stalls naturally associated with cache operation may therefore affect I/O timing or bit-bang
operations.
• Security enhancements to the DS5250 program memory space may complicate bank
switching and/or memory overlay schemes. Specifically, features such as the Program
Encryption Key Modifier Register and the use of different encryption schemes for program
and data memory may require a reassessment of DS5002FP methods of memory map
manipulation.
• The DS5250 does not support reprogrammable peripheral controller (RPC) mode.










Upgrading Designs to the 100-Pin QFP
The DS5250 in the 100-pin QFP package provides all the features of the 80-pin package except
for pin-for-pin compatibility with the DS5002FP and support for the DS5002FP modes of memory
interfacing. It also provides the following features:
• Expanded memory-interfacing options, up to 4MB of program and data memory and 4MB of
memory-mapped I/O
• On-board RTC (driven by an external 32.768kHz crystal), useful for time stamping and  selfimposed
expiration dates
• Temperature sensor that can trigger a DRS in response to a low-temperature attack
• Additional SDI input pin that can be wired to an interrupt to allow a software-controlled
tamper response
• Hardware support for external flash memory (loader ROM support included on the DS5250)




On the uVision Keil documentation for the ds5250 we see a list of features and registers
Simulated Features

The following on-chip peripherals are simulated by the Keil Software µVision Debugger.
◾Clock Divider and Multiplier
◾External Memory Stretch Cycle
◾Interrupts 15S/2L (Including External)
◾Port 0
◾Port 1
◾Port 2
◾Port 3
◾Power Saving Modes (Idle and Power Down)
◾Ring Oscillator
◾Serial UART 0 (Enhanced Interface)
◾Serial UART 1 (Enhanced Interface without T2 as BRG)
◾Timer 0
◾Timer 1
◾Timer 2 (Extended Timer 2)
◾Timer Rate Control ; 4 Clocks Per Machine Cycle
◾Watchdog Timer

The following on-chip peripherals are not simulated.
◾Boot Loader: ACON.6, BP, IA
◾CRC Generator: CRC1, CRC2, CRC3, CRC4
◾Encryption (PEK1, PEK2, DEK) and Decryption (PBDD, PBDC)
◾Flash Program Memory:FLMC
◾Memory Management Unit: ACON.7, MSIZE, MCEN, DMOS, MCON, RAMST, ROMST, MXAH
◾Modular Arithmetic Accelerator: MACT, MAS0, MAS1
◾Port 4: P4
◾Random Number Generator: RNR
◾Real-Time Clock: TRIM, RTASS, RTAS, RTAM, RTAH, RTCC, RTCSS, RTCS, RTCM, RTCH, RTCD0, RTCD1
◾Reset Control: RAR, RCR, RTR
◾Self Destruct Interrupt: PMR.1, PMR.0
◾User DES Engine: UDESD, UDESC


Now we are starting to get a good look at what is going on, 
it seems the ds5002fp shares around about 80 shared pins these may or may not be used
 in the same places on the ds5250
 as on the ds5002fp but thats almost 80% of the pin names available to us, 

Going back to the ds5250 sample datasheet we see that it is a direct swap for the ds5240

But this is also a dead end in terms of available information

So we have a public datasheet, its for the Ds5002fp, BINGO, 
this is the list of the 80 pins that both the 5002fp and the 5250 80pin Package Share,

 these are :





Thus giving us this :








As app note 2034 states:
Upgrading Designs to the 100-Pin QFP
The DS5250 in the 100-pin QFP package provides all the features of the 80-pin package 
**>>except<<** for **>>>pin-for-pin<<<** compatibility with the DS5002FP and support for the    DS5002FP modes of memory interfacing.


This means that the two 80 pin packages do share the same pins now all that is left if to identify the other 20 pins  



From this i noted these maybe the extras i am looking for:
6 BAx
6 I/O ports 
RTCx1
RTCx2 
RX  TX - Serial lines
SDI input number 2

Bringing me up to 97 pins in total, thats alot closer than the 0 we had at the start,

As I Move forward i face a new issue in knowing how to place these extra 20 pins 
into the data set we have so far, the idea i had is to dissolve the packages of both a 5002 and 5250 80pin
 also a 5250 100pin pack then using imaging techniques 
observe the similarities between the  dies noting differences to try and work out where these new pins belong. 


Untill next time stay sharp ay :P