-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSRND.h
112 lines (89 loc) · 4.1 KB
/
SRND.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Software Based Random Number Generation Layer for Secure Payment Systems
EMV-co approved SRND algorithm
Author : Eray Ozturk | erayozturk1@gmail.com
Details : Implementation of the SHA-256 hashing algorithm specification can be found here:
http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
This implementation uses little endian byte order.
*/
#ifndef SRND_H
#define SRND_H
/*
TID
Terminal ID
EMV Data element tag '9F1C'
8 bytes alpha numeric (see Table 33 of EMV Book 3)
IFDSN
IFD Serial
EMV Data element tag '9F1E'
8 bytes alpha numeric (see Table 33 of EMV Book 3)
TVP
Time varying parameter
Date and time with finer granularity than 0.1 second
Implementation dependent. Should be an internal value not the external clock
RAND
Value from external RNG
Random value sourced outside the kernel (e.g. PCI-approved hardware RNG) to be used if available
8 bytes binary
P
Pre-image of UN
P is an internal register maintained in the terminal in volatile memory and never output.
32 bytes binary
Q
Persistent variant of P
Q shall be initialised to a terminal-unique random number prior to deployment. It is maintained in the terminal in non-volatile memory and never output. It is updated every time the power is cycled.
32 bytes binary
AC
Application Cryptogram
This is generated by card and can be read by GENERATE AC (9F26) command
8 bytes binary
*/
////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Headers ////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stddef.h>
////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Macros /////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
#define Q_BLOCK_SIZE 32 // Q and SHA256 outputs a 32 byte digest
#define Q_FILE "Q.bin"
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Types /////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
typedef unsigned char u8; // 8-bit byte
typedef unsigned int u32; // 32-bit word, change to "long" for 16-bit machines
////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// Functions ///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
//! The SRND_PowerUp function should be called once on Init or Power-up.
/*!
\param [in] TID : Terminal ID - 8 bytes
\param [in] IFDSN : IFD Serial - 8 bytes
\param [in] TVP : Time varying parameter
\param [in] RAND : Value from external RNG - 8 bytes
\return none
*/
void SRND_PowerUp(u8 *TID, u8 *IFDSN, u32 TVP, u8 *RAND);
//! The SRND_BeforeTransaction function should be called before every transaction to generate UN (unpredictable number)
/*!
\param [in] RAND : Value from external RNG - 8 bytes
\param [out] UN : Unpredictable number - 4 bytes
\return 0 if successfull
*/
int SRND_BeforeTransaction(u8 *RAND, u8 *UN);
//! The SRND_AfterTransaction function should be called after every transaction even if it fails
/*!
\param [in] TVP : Time varying parameter
\param [in] RAND : Value from external RNG - 8 bytes
\param [in] AC : Application Cryptogram from card '9F26' - 8 bytes
\return none
*/
void SRND_AfterTransaction(u32 TVP, u8 *RAND, u8 *AC);
//! The SRND_PowerDown function should be called once on DeInit or Power-down.
/*!
\param none
\return none
*/
void SRND_PowerDown();
#endif // SRND_H