From 1b8ee5fbf7a554a6ca5b18fc758f4e19799e9b93 Mon Sep 17 00:00:00 2001 From: Piotr Sawicki
+/* This version written November 2000 by David Ireland of
+ DI Management Services Pty Limited
- Adapted from code in the Python Cryptography Toolkit,
- version 1.0.0 by A.M. Kuchling 1995.
+ Adapted from code in the Python Cryptography Toolkit,
+ version 1.0.0 by A.M. Kuchling 1995.
*/
/* AM Kuchling's posting:-
@@ -46,13 +46,13 @@ effort (for example the reengineering of a great many Capstone chips).
*/
/* JS Park's posting:
- Modification for naming confilct.
- - Attach prefix 'SDRM_SHA1_' for all function and constants.
- - Change name of data context to 'SDRM_SHA1Context'
- endianTest code is modified to avoid gcc warning.
- Primitive data types are used, instead of user-defined data types.
- Prototypes are moved to header file.
- Not using functions are commented out.
+ Modification for naming confilct.
+ - Attach prefix 'SDRM_SHA1_' for all function and constants.
+ - Change name of data context to 'SDRM_SHA1Context'
+ endianTest code is modified to avoid gcc warning.
+ Primitive data types are used, instead of user-defined data types.
+ Prototypes are moved to header file.
+ Not using functions are commented out.
*/
@@ -62,7 +62,8 @@ void SDRM_endianTest(int *endianness);
#include "cc_sha1.h"
-static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned int len);
+static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input,
+ unsigned int len);
/* The SHS block size and message digest sizes, in bytes */
@@ -74,12 +75,12 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned
save one boolean operation each - thanks to Rich Schroeppel,
rcs@cs.arizona.edu for discovering this */
-/*#define SDRM_SHA1_f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */
-#define SDRM_SHA1_f1(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) /* Rounds 0-19 */
-#define SDRM_SHA1_f2(x,y,z) ((x) ^ (y) ^ (z)) /* Rounds 20-39 */
-/*#define SDRM_SHA1_f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */
-#define SDRM_SHA1_f3(x,y,z) (((x) & (y)) | ((z) & ((x) | (y)))) /* Rounds 40-59 */
-#define SDRM_SHA1_f4(x,y,z) ((x) ^ (y) ^ (z)) /* Rounds 60-79 */
+/*#define SDRM_SHA1_f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */
+#define SDRM_SHA1_f1(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) /* Rounds 0-19 */
+#define SDRM_SHA1_f2(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 20-39 */
+/*#define SDRM_SHA1_f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */
+#define SDRM_SHA1_f3(x, y, z) (((x) & (y)) | ((z) & ((x) | (y)))) /* Rounds 40-59 */
+#define SDRM_SHA1_f4(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 60-79 */
/* The SHS Mysterious Constants */
@@ -106,7 +107,7 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned
80-UINT2 expanded input array W, where the first 16 are copies of the input
data, and the remaining 64 are defined by
- W[ i ] = W[ i - 16 ] ^ W[ i - 14 ] ^ W[ i - 8 ] ^ W[ i - 3 ]
+ W[i] = W[i - 16] ^ W[i - 14] ^ W[i - 8] ^ W[i - 3]
This implementation generates these values on the fly in a circular
buffer - thanks to Colin Plumb, colin@nyx10.cs.du.edu for this
@@ -117,39 +118,39 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned
for this information */
#define SDRM_SHA1_expand(W, i) (W[(i) & 15] = SDRM_SHA1_ROTL(1, (W[(i) & 15] ^ W[((i) - 14) & 15] ^ \
- W[((i) - 8) & 15] ^ W[((i) - 3) & 15])))
+ W[((i) - 8) & 15] ^ W[((i) - 3) & 15])))
/* The prototype SHS sub-round. The fundamental sub-round is:
- a' = e + ROTL( 5, a ) + f( b, c, d ) + k + data;
- b' = a;
- c' = ROTL( 30, b );
- d' = c;
- e' = d;
+ a' = e + ROTL(5, a) + f(b, c, d) + k + data;
+ b' = a;
+ c' = ROTL(30, b);
+ d' = c;
+ e' = d;
but this is implemented by unrolling the loop 5 times and renaming the
- variables ( e, a, b, c, d ) = ( a', b', c', d', e' ) each iteration.
+ variables (e, a, b, c, d) = (a', b', c', d', e') each iteration.
This code is then replicated 20 times for each of the 4 functions, using
the next 20 values from the W[] array each time */
#define SDRM_SHA1_subRound(a, b, c, d, e, f, k, data) \
- (e += SDRM_SHA1_ROTL(5, a) + f(b, c, d) + (k) + (data), b = SDRM_SHA1_ROTL(30, b))
+ (e += SDRM_SHA1_ROTL(5, a) + f(b, c, d) + (k) + (data), b = SDRM_SHA1_ROTL(30, b))
/* Initialize the SHS values */
void SDRM_SHA1_Init(SDRM_SHA1Context *shsInfo)
{
- SDRM_endianTest(&shsInfo->Endianness);
- /* Set the h-vars to their initial values */
- shsInfo->digest[ 0 ] = SDRM_SHA1_h0init;
- shsInfo->digest[ 1 ] = SDRM_SHA1_h1init;
- shsInfo->digest[ 2 ] = SDRM_SHA1_h2init;
- shsInfo->digest[ 3 ] = SDRM_SHA1_h3init;
- shsInfo->digest[ 4 ] = SDRM_SHA1_h4init;
-
- /* Initialise bit count */
- shsInfo->countLo = shsInfo->countHi = 0;
+ SDRM_endianTest(&shsInfo->Endianness);
+ /* Set the h-vars to their initial values */
+ shsInfo->digest[0] = SDRM_SHA1_h0init;
+ shsInfo->digest[1] = SDRM_SHA1_h1init;
+ shsInfo->digest[2] = SDRM_SHA1_h2init;
+ shsInfo->digest[3] = SDRM_SHA1_h3init;
+ shsInfo->digest[4] = SDRM_SHA1_h4init;
+
+ /* Initialise bit count */
+ shsInfo->countLo = shsInfo->countHi = 0;
}
@@ -160,243 +161,305 @@ void SDRM_SHA1_Init(SDRM_SHA1Context *shsInfo)
Note that this corrupts the shsInfo->data area */
-static void SDRM_SHSTransform(unsigned int *digest, unsigned int *data )
- {
- unsigned int A, B, C, D, E; /* Local vars */
- unsigned int eData[ 16 ]; /* Expanded data */
-
- /* Set up first buffer and local data buffer */
- A = digest[ 0 ];
- B = digest[ 1 ];
- C = digest[ 2 ];
- D = digest[ 3 ];
- E = digest[ 4 ];
- memcpy( (unsigned char*)eData, (unsigned char*)data, SDRM_SHA1_DATASIZE );
-
- /* Heavy mangling, in 4 sub-rounds of 20 interations each. */
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 0 ] );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 1 ] );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 2 ] );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 3 ] );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 4 ] );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 5 ] );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 6 ] );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 7 ] );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 8 ] );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 9 ] );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 10 ] );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 11 ] );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 12 ] );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 13 ] );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 14 ] );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 15 ] );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, SDRM_SHA1_expand( eData, 16 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, SDRM_SHA1_expand( eData, 17 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, SDRM_SHA1_expand( eData, 18 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, SDRM_SHA1_expand( eData, 19 ) );
-
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 20 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 21 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 22 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 23 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 24 ) );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 25 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 26 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 27 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 28 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 29 ) );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 30 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 31 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 32 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 33 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 34 ) );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 35 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 36 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 37 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 38 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 39 ) );
-
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 40 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 41 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 42 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 43 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 44 ) );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 45 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 46 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 47 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 48 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 49 ) );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 50 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 51 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 52 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 53 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 54 ) );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 55 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 56 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 57 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 58 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 59 ) );
-
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 60 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 61 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 62 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 63 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 64 ) );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 65 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 66 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 67 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 68 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 69 ) );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 70 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 71 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 72 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 73 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 74 ) );
- SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 75 ) );
- SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 76 ) );
- SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 77 ) );
- SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 78 ) );
- SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 79 ) );
-
- /* Build message digest */
- digest[ 0 ] += A;
- digest[ 1 ] += B;
- digest[ 2 ] += C;
- digest[ 3 ] += D;
- digest[ 4 ] += E;
- }
+static void SDRM_SHSTransform(unsigned int *digest, unsigned int *data)
+{
+ unsigned int A, B, C, D, E; /* Local vars */
+ unsigned int eData[16]; /* Expanded data */
+
+ /* Set up first buffer and local data buffer */
+ A = digest[0];
+ B = digest[1];
+ C = digest[2];
+ D = digest[3];
+ E = digest[4];
+ memcpy((unsigned char *)eData, (unsigned char *)data, SDRM_SHA1_DATASIZE);
+
+ /* Heavy mangling, in 4 sub-rounds of 20 interations each. */
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[0]);
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[1]);
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[2]);
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[3]);
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[4]);
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[5]);
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[6]);
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[7]);
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[8]);
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[9]);
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[10]);
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[11]);
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[12]);
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[13]);
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[14]);
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[15]);
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1,
+ SDRM_SHA1_expand(eData, 16));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1,
+ SDRM_SHA1_expand(eData, 17));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1,
+ SDRM_SHA1_expand(eData, 18));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1,
+ SDRM_SHA1_expand(eData, 19));
+
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 20));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 21));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 22));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 23));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 24));
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 25));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 26));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 27));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 28));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 29));
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 30));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 31));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 32));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 33));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 34));
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 35));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 36));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 37));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 38));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2,
+ SDRM_SHA1_expand(eData, 39));
+
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 40));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 41));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 42));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 43));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 44));
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 45));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 46));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 47));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 48));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 49));
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 50));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 51));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 52));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 53));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 54));
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 55));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 56));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 57));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 58));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3,
+ SDRM_SHA1_expand(eData, 59));
+
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 60));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 61));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 62));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 63));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 64));
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 65));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 66));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 67));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 68));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 69));
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 70));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 71));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 72));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 73));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 74));
+ SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 75));
+ SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 76));
+ SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 77));
+ SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 78));
+ SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4,
+ SDRM_SHA1_expand(eData, 79));
+
+ /* Build message digest */
+ digest[0] += A;
+ digest[1] += B;
+ digest[2] += C;
+ digest[3] += D;
+ digest[4] += E;
+}
/* When run on a little-endian CPU we need to perform byte reversal on an
array of long words. */
-static void SDRM_longReverse(unsigned int *buffer, int byteCount, int Endianness)
+static void SDRM_longReverse(unsigned int *buffer, int byteCount,
+ int Endianness)
{
- unsigned int value;
+ unsigned int value;
- if (Endianness == !(0)) {
+ if (Endianness == !(0))
return;
- }
- byteCount /= sizeof( unsigned int );
- while(byteCount--)
- {
- value = *buffer;
- value = ((value & 0xFF00FF00L) >> 8) | \
- ((value & 0x00FF00FFL ) << 8);
- *buffer++ = (value << 16) | (value >> 16);
+
+ byteCount /= sizeof(unsigned int);
+
+ while (byteCount--) {
+ value = *buffer;
+ value = ((value & 0xFF00FF00L) >> 8) | \
+ ((value & 0x00FF00FFL) << 8);
+ *buffer++ = (value << 16) | (value >> 16);
}
}
/* Update SHS for a block of data */
-void SDRM_SHA1_Update(SDRM_SHA1Context *shsInfo, const unsigned char *buffer, int count)
+void SDRM_SHA1_Update(SDRM_SHA1Context *shsInfo, const unsigned char *buffer,
+ int count)
{
- unsigned int tmp;
- int dataCount;
+ unsigned int tmp;
+ int dataCount;
+
+ /* Update bitcount */
+ tmp = shsInfo->countLo;
- /* Update bitcount */
- tmp = shsInfo->countLo;
- if ((shsInfo->countLo = tmp + ((unsigned int)count << 3)) < tmp) {
+ if ((shsInfo->countLo = tmp + ((unsigned int)count << 3)) < tmp)
shsInfo->countHi++; /* Carry from low to high */
+
+ shsInfo->countHi += count >> 29;
+
+ /* Get count of bytes already in data */
+ dataCount = (int)(tmp >> 3) & 0x3F;
+
+ /* Handle any leading odd-sized chunks */
+ if (dataCount) {
+ unsigned char *p = (unsigned char *) shsInfo->data + dataCount;
+
+ dataCount = SDRM_SHA1_DATASIZE - dataCount;
+
+ if (count < dataCount) {
+ memcpy(p, buffer, count);
+ return;
+ }
+
+ memcpy(p, buffer, dataCount);
+ SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness);
+ SDRM_SHSTransform(shsInfo->digest, shsInfo->data);
+ buffer += dataCount;
+ count -= dataCount;
}
- shsInfo->countHi += count >> 29;
-
- /* Get count of bytes already in data */
- dataCount = (int)(tmp >> 3) & 0x3F;
-
- /* Handle any leading odd-sized chunks */
- if (dataCount)
- {
- unsigned char *p = (unsigned char*) shsInfo->data + dataCount;
-
- dataCount = SDRM_SHA1_DATASIZE - dataCount;
- if(count < dataCount)
- {
- memcpy(p, buffer, count);
- return;
- }
- memcpy(p, buffer, dataCount);
- SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness);
- SDRM_SHSTransform(shsInfo->digest, shsInfo->data);
- buffer += dataCount;
- count -= dataCount;
- }
-
- /* Process data in SHS_DATASIZE chunks */
- while(count >= SDRM_SHA1_DATASIZE)
- {
- memcpy((unsigned char*)shsInfo->data, buffer, SDRM_SHA1_DATASIZE);
- SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness);
- SDRM_SHSTransform(shsInfo->digest, shsInfo->data);
- buffer += SDRM_SHA1_DATASIZE;
- count -= SDRM_SHA1_DATASIZE;
- }
-
- /* Handle any remaining bytes of data. */
- memcpy( (unsigned char*)shsInfo->data, buffer, count);
- }
+ /* Process data in SHS_DATASIZE chunks */
+ while (count >= SDRM_SHA1_DATASIZE) {
+ memcpy((unsigned char *)shsInfo->data, buffer, SDRM_SHA1_DATASIZE);
+ SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness);
+ SDRM_SHSTransform(shsInfo->digest, shsInfo->data);
+ buffer += SDRM_SHA1_DATASIZE;
+ count -= SDRM_SHA1_DATASIZE;
+ }
+
+ /* Handle any remaining bytes of data. */
+ memcpy((unsigned char *)shsInfo->data, buffer, count);
+}
/* Final wrapup - pad to SHS_DATASIZE-byte boundary with the bit pattern
1 0* (64-bit count of bits processed, MSB-first) */
void SDRM_SHA1_Final(SDRM_SHA1Context *shsInfo, unsigned char *output)
{
- int count;
- unsigned char *dataPtr;
-
- /* Compute number of bytes mod 64 */
- count = (int) shsInfo->countLo;
- count = (count >> 3) & 0x3F;
-
- /* Set the first char of padding to 0x80. This is safe since there is
- always at least one byte free */
- dataPtr = (unsigned char*) shsInfo->data + count;
- *dataPtr++ = 0x80;
-
- /* Bytes of padding needed to make 64 bytes */
- count = SDRM_SHA1_DATASIZE - 1 - count;
-
- /* Pad out to 56 mod 64 */
- if( count < 8 )
- {
- /* Two lots of padding: Pad the first block to 64 bytes */
- memset(dataPtr, 0, count);
- SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness);
- SDRM_SHSTransform(shsInfo->digest, shsInfo->data);
-
- /* Now fill the next block with 56 bytes */
- memset((unsigned char*)shsInfo->data, 0, SDRM_SHA1_DATASIZE - 8);
- }
- else
- /* Pad block to 56 bytes */
- {
- memset(dataPtr, 0, count - 8);
- }
+ int count;
+ unsigned char *dataPtr;
+
+ /* Compute number of bytes mod 64 */
+ count = (int) shsInfo->countLo;
+ count = (count >> 3) & 0x3F;
+
+ /* Set the first char of padding to 0x80. This is safe since there is
+ always at least one byte free */
+ dataPtr = (unsigned char *) shsInfo->data + count;
+ *dataPtr++ = 0x80;
- /* Append length in bits and transform */
- shsInfo->data[14] = shsInfo->countHi;
- shsInfo->data[15] = shsInfo->countLo;
+ /* Bytes of padding needed to make 64 bytes */
+ count = SDRM_SHA1_DATASIZE - 1 - count;
- SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE - 8, shsInfo->Endianness);
- SDRM_SHSTransform(shsInfo->digest, shsInfo->data);
+ /* Pad out to 56 mod 64 */
+ if (count < 8) {
+ /* Two lots of padding: Pad the first block to 64 bytes */
+ memset(dataPtr, 0, count);
+ SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness);
+ SDRM_SHSTransform(shsInfo->digest, shsInfo->data);
+
+ /* Now fill the next block with 56 bytes */
+ memset((unsigned char *)shsInfo->data, 0, SDRM_SHA1_DATASIZE - 8);
+ } else
+ /* Pad block to 56 bytes */
+ memset(dataPtr, 0, count - 8);
+
+ /* Append length in bits and transform */
+ shsInfo->data[14] = shsInfo->countHi;
+ shsInfo->data[15] = shsInfo->countLo;
+
+ SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE - 8, shsInfo->Endianness);
+ SDRM_SHSTransform(shsInfo->digest, shsInfo->data);
/* Output to an array of bytes */
SDRM_SHAtoByte(output, shsInfo->digest, SDRM_SHA1_DIGESTSIZE);
/* Zeroise sensitive stuff */
- memset((unsigned char*)shsInfo, 0, sizeof(SDRM_SHA1Context));
+ memset((unsigned char *)shsInfo, 0, sizeof(SDRM_SHA1Context));
}
-static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned int len)
-{ /* Output SHA digest in byte array */
+static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input,
+ unsigned int len)
+{
+ /* Output SHA digest in byte array */
unsigned int i, j;
- for(i = 0, j = 0; j < len; i++, j += 4)
- {
- output[j+3] = (unsigned char)( input[i] & 0xff);
- output[j+2] = (unsigned char)((input[i] >> 8 ) & 0xff);
- output[j+1] = (unsigned char)((input[i] >> 16) & 0xff);
- output[j ] = (unsigned char)((input[i] >> 24) & 0xff);
+ for (i = 0, j = 0; j < len; i++, j += 4) {
+ output[j + 3] = (unsigned char)(input[i] & 0xff);
+ output[j + 2] = (unsigned char)((input[i] >> 8) & 0xff);
+ output[j + 1] = (unsigned char)((input[i] >> 16) & 0xff);
+ output[j] = (unsigned char)((input[i] >> 24) & 0xff);
}
}
@@ -404,7 +467,7 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned
//unsigned char digest[20];
//unsigned char message[3] = {'a', 'b', 'c' };
//unsigned char *mess56 =
-// "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
+// "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
/* Correct solutions from FIPS PUB 180-1 */
//char *dig1 = "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D";
@@ -422,71 +485,68 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned
//main()
//{
-// SHA_CTX sha;
-// int i;
-// BYTE big[1000];
+// SHA_CTX sha;
+// int i;
+// BYTE big[1000];
//
-// SHAInit(&sha);
-// SHAUpdate(&sha, message, 3);
-// SHAFinal(digest, &sha);
+// SHAInit(&sha);
+// SHAUpdate(&sha, message, 3);
+// SHAFinal(digest, &sha);
//
-// for (i = 0; i < 20; i++)
-// {
-// if ((i % 4) == 0) printf(" ");
-// printf("%02x", digest[i]);
-// }
-// printf("\n");
-// printf(" %s <= correct\n", dig1);
+// for (i = 0; i < 20; i++)
+// {
+// if ((i % 4) == 0) printf(" ");
+// printf("%02x", digest[i]);
+// }
+// printf("\n");
+// printf(" %s <= correct\n", dig1);
//
-// SHAInit(&sha);
-// SHAUpdate(&sha, mess56, 56);
-// SHAFinal(digest, &sha);
+// SHAInit(&sha);
+// SHAUpdate(&sha, mess56, 56);
+// SHAFinal(digest, &sha);
//
-// for (i = 0; i < 20; i++)
-// {
-// if ((i % 4) == 0) printf(" ");
-// printf("%02x", digest[i]);
-// }
-// printf("\n");
-// printf(" %s <= correct\n", dig2);
+// for (i = 0; i < 20; i++)
+// {
+// if ((i % 4) == 0) printf(" ");
+// printf("%02x", digest[i]);
+// }
+// printf("\n");
+// printf(" %s <= correct\n", dig2);
//
-// /* Fill up big array */
-// for (i = 0; i < 1000; i++)
-// big[i] = 'a';
+// /* Fill up big array */
+// for (i = 0; i < 1000; i++)
+// big[i] = 'a';
//
-// SHAInit(&sha);
-// /* Digest 1 million x 'a' */
-// for (i = 0; i < 1000; i++)
-// SHAUpdate(&sha, big, 1000);
-// SHAFinal(digest, &sha);
+// SHAInit(&sha);
+// /* Digest 1 million x 'a' */
+// for (i = 0; i < 1000; i++)
+// SHAUpdate(&sha, big, 1000);
+// SHAFinal(digest, &sha);
//
-// for (i = 0; i < 20; i++)
-// {
-// if ((i % 4) == 0) printf(" ");
-// printf("%02x", digest[i]);
-// }
-// printf("\n");
-// printf(" %s <= correct\n", dig3);
+// for (i = 0; i < 20; i++)
+// {
+// if ((i % 4) == 0) printf(" ");
+// printf("%02x", digest[i]);
+// }
+// printf("\n");
+// printf(" %s <= correct\n", dig3);
//
-// return 0;
+// return 0;
//}
/* endian.c */
void SDRM_endianTest(int *endian_ness)
{
- static short test = 1;
+ static short test = 1;
- if ( *((char *) &test) != 1)
- {
+ if (*((char *) &test) != 1) {
/* printf("Big endian = no change\n"); */
*endian_ness = !(0);
- }
- else
- {
+ } else {
/* printf("Little endian = swap\n"); */
*endian_ness = 0;
- }
+ }
}
/***************************** End of File *****************************/
diff --git a/ssflib/dep/cryptocore/source/base/cc_sha2.c b/ssflib/dep/cryptocore/source/base/cc_sha2.c
index 75ad2a9..7411093 100644
--- a/ssflib/dep/cryptocore/source/base/cc_sha2.c
+++ b/ssflib/dep/cryptocore/source/base/cc_sha2.c
@@ -32,16 +32,16 @@
*/
/* JS Park's posting:
- Modification for naming confilct.
- Attach prefix 'SDRM_' for all function and constants.
- Change name of data context to 'SDRM_SHAxxxContext' (xxx is bit length of digest)
+ Modification for naming confilct.
+ Attach prefix 'SDRM_' for all function and constants.
+ Change name of data context to 'SDRM_SHAxxxContext' (xxx is bit length of digest)
*/
#include
#include "cc_sha2.h"
-#define SDRM_SHA2_SHFR(x, n) ((x) >> (n))
+#define SDRM_SHA2_SHFR(x, n) ((x) >> (n))
#define SDRM_SHA2_ROTR(x, n) (((x) >> (n)) | ((x) << ((sizeof(x) << 3) - (n))))
#define SDRM_SHA2_ROTL(x, n) (((x) << (n)) | ((x) >> ((sizeof(x) << 3) - (n))))
#define SDRM_SHA2_CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
@@ -57,220 +57,230 @@
#define SDRM_SHA2_SHA512_F3(x) (SDRM_SHA2_ROTR(x, 1) ^ SDRM_SHA2_ROTR(x, 8) ^ SDRM_SHA2_SHFR(x, 7))
#define SDRM_SHA2_SHA512_F4(x) (SDRM_SHA2_ROTR(x, 19) ^ SDRM_SHA2_ROTR(x, 61) ^ SDRM_SHA2_SHFR(x, 6))
-#define SDRM_SHA2_UNPACK32(x, str) \
-do { \
- *((str) + 3) = (cc_u8) ((x) ); \
- *((str) + 2) = (cc_u8) ((x) >> 8); \
- *((str) + 1) = (cc_u8) ((x) >> 16); \
- *((str) + 0) = (cc_u8) ((x) >> 24); \
-} while(0)
-
-#define SDRM_SHA2_PACK32(str, x) \
-do { \
- *(x) = ((cc_u32) *((str) + 3) ) \
- | ((cc_u32) *((str) + 2) << 8) \
- | ((cc_u32) *((str) + 1) << 16) \
- | ((cc_u32) *((str) + 0) << 24); \
-} while(0)
-
-#define SDRM_SHA2_UNPACK64(x, str) \
-do { \
- *((str) + 7) = (cc_u8) ((x) ); \
- *((str) + 6) = (cc_u8) ((x) >> 8); \
- *((str) + 5) = (cc_u8) ((x) >> 16); \
- *((str) + 4) = (cc_u8) ((x) >> 24); \
- *((str) + 3) = (cc_u8) ((x) >> 32); \
- *((str) + 2) = (cc_u8) ((x) >> 40); \
- *((str) + 1) = (cc_u8) ((x) >> 48); \
- *((str) + 0) = (cc_u8) ((x) >> 56); \
-} while(0)
-
-#define SDRM_SHA2_PACK64(str, x) \
-do { \
- *(x) = ((cc_u64) *((str) + 7) ) \
- | ((cc_u64) *((str) + 6) << 8) \
- | ((cc_u64) *((str) + 5) << 16) \
- | ((cc_u64) *((str) + 4) << 24) \
- | ((cc_u64) *((str) + 3) << 32) \
- | ((cc_u64) *((str) + 2) << 40) \
- | ((cc_u64) *((str) + 1) << 48) \
- | ((cc_u64) *((str) + 0) << 56); \
-} while(0)
+#define SDRM_SHA2_UNPACK32(x, str) \
+ do { \
+ *((str) + 3) = (cc_u8) ((x)); \
+ *((str) + 2) = (cc_u8) ((x) >> 8); \
+ *((str) + 1) = (cc_u8) ((x) >> 16); \
+ *((str) + 0) = (cc_u8) ((x) >> 24); \
+ } while (0)
+
+#define SDRM_SHA2_PACK32(str, x) \
+ do { \
+ *(x) = ((cc_u32) *((str) + 3)) \
+ | ((cc_u32) *((str) + 2) << 8) \
+ | ((cc_u32) *((str) + 1) << 16) \
+ | ((cc_u32) *((str) + 0) << 24); \
+ } while (0)
+
+#define SDRM_SHA2_UNPACK64(x, str) \
+ do { \
+ *((str) + 7) = (cc_u8) ((x)); \
+ *((str) + 6) = (cc_u8) ((x) >> 8); \
+ *((str) + 5) = (cc_u8) ((x) >> 16); \
+ *((str) + 4) = (cc_u8) ((x) >> 24); \
+ *((str) + 3) = (cc_u8) ((x) >> 32); \
+ *((str) + 2) = (cc_u8) ((x) >> 40); \
+ *((str) + 1) = (cc_u8) ((x) >> 48); \
+ *((str) + 0) = (cc_u8) ((x) >> 56); \
+ } while (0)
+
+#define SDRM_SHA2_PACK64(str, x) \
+ do { \
+ *(x) = ((cc_u64) *((str) + 7)) \
+ | ((cc_u64) *((str) + 6) << 8) \
+ | ((cc_u64) *((str) + 5) << 16) \
+ | ((cc_u64) *((str) + 4) << 24) \
+ | ((cc_u64) *((str) + 3) << 32) \
+ | ((cc_u64) *((str) + 2) << 40) \
+ | ((cc_u64) *((str) + 1) << 48) \
+ | ((cc_u64) *((str) + 0) << 56); \
+ } while (0)
/* Macros used for loops unrolling */
-#define SDRM_SHA2_SHA256_SCR(i) \
-{ \
- w[i] = SDRM_SHA2_SHA256_F4(w[(i) - 2]) + w[(i) - 7] \
- + SDRM_SHA2_SHA256_F3(w[(i) - 15]) + w[(i) - 16]; \
-}
+#define SDRM_SHA2_SHA256_SCR(i) \
+ { \
+ w[i] = SDRM_SHA2_SHA256_F4(w[(i) - 2]) + w[(i) - 7] \
+ + SDRM_SHA2_SHA256_F3(w[(i) - 15]) + w[(i) - 16]; \
+ }
-#define SDRM_SHA2_SHA512_SCR(i) \
-{ \
- w[i] = SDRM_SHA2_SHA512_F4(w[(i) - 2]) + w[(i) - 7] \
- + SDRM_SHA2_SHA512_F3(w[(i) - 15]) + w[(i) - 16]; \
-}
+#define SDRM_SHA2_SHA512_SCR(i) \
+ { \
+ w[i] = SDRM_SHA2_SHA512_F4(w[(i) - 2]) + w[(i) - 7] \
+ + SDRM_SHA2_SHA512_F3(w[(i) - 15]) + w[(i) - 16]; \
+ }
-#define SDRM_SHA2_SHA256_EXP(a, b, c, d, e, f, g, h, j) \
-{ \
- t1 = wv[h] + SDRM_SHA2_SHA256_F2(wv[e]) + SDRM_SHA2_CH(wv[e], wv[f], wv[g]) \
- + sha256_k[j] + w[j]; \
- t2 = SDRM_SHA2_SHA256_F1(wv[a]) + SDRM_SHA2_MAJ(wv[a], wv[b], wv[c]); \
- wv[d] += t1; \
- wv[h] = t1 + t2; \
-}
+#define SDRM_SHA2_SHA256_EXP(a, b, c, d, e, f, g, h, j) \
+ { \
+ t1 = wv[h] + SDRM_SHA2_SHA256_F2(wv[e]) + SDRM_SHA2_CH(wv[e], wv[f], wv[g]) \
+ + sha256_k[j] + w[j]; \
+ t2 = SDRM_SHA2_SHA256_F1(wv[a]) + SDRM_SHA2_MAJ(wv[a], wv[b], wv[c]); \
+ wv[d] += t1; \
+ wv[h] = t1 + t2; \
+ }
-#define SDRM_SHA2_SHA512_EXP(a, b, c, d, e, f, g ,h, j) \
-{ \
- t1 = wv[h] + SDRM_SHA2_SHA512_F2(wv[e]) + SDRM_SHA2_CH(wv[e], wv[f], wv[g]) \
- + sha512_k[j] + w[j]; \
- t2 = SDRM_SHA2_SHA512_F1(wv[a]) + SDRM_SHA2_MAJ(wv[a], wv[b], wv[c]); \
- wv[d] += t1; \
- wv[h] = t1 + t2; \
-}
+#define SDRM_SHA2_SHA512_EXP(a, b, c, d, e, f, g, h, j) \
+ { \
+ t1 = wv[h] + SDRM_SHA2_SHA512_F2(wv[e]) + SDRM_SHA2_CH(wv[e], wv[f], wv[g]) \
+ + sha512_k[j] + w[j]; \
+ t2 = SDRM_SHA2_SHA512_F1(wv[a]) + SDRM_SHA2_MAJ(wv[a], wv[b], wv[c]); \
+ wv[d] += t1; \
+ wv[h] = t1 + t2; \
+ }
-cc_u32 sha224_h0[8] =
- {0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
- 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4};
-
-cc_u32 sha256_h0[8] =
- {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
- 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
-
-cc_u32 sha256_k[64] =
- {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
- 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
- 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
- 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
- 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
- 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
- 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
- 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
- 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
+cc_u32 sha224_h0[8] = {
+ 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
+ 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
+};
+
+cc_u32 sha256_h0[8] = {
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
+ 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
+};
+
+cc_u32 sha256_k[64] = {
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
+ 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
+ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
+ 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
+ 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
+ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
+ 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
+ 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
+};
#ifndef _OP64_NOTSUPPORTED
#ifdef _WIN32
-cc_u64 sha384_h0[8] =
- {0xcbbb9d5dc1059ed8, 0x629a292a367cd507,
- 0x9159015a3070dd17, 0x152fecd8f70e5939,
- 0x67332667ffc00b31, 0x8eb44a8768581511,
- 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4};
-
-cc_u64 sha512_h0[8] =
- {0x6a09e667f3bcc908, 0xbb67ae8584caa73b,
- 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
- 0x510e527fade682d1, 0x9b05688c2b3e6c1f,
- 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179};
-
-cc_u64 sha512_k[80] =
- {0x428a2f98d728ae22, 0x7137449123ef65cd,
- 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
- 0x3956c25bf348b538, 0x59f111f1b605d019,
- 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
- 0xd807aa98a3030242, 0x12835b0145706fbe,
- 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
- 0x72be5d74f27b896f, 0x80deb1fe3b1696b1,
- 0x9bdc06a725c71235, 0xc19bf174cf692694,
- 0xe49b69c19ef14ad2, 0xefbe4786384f25e3,
- 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
- 0x2de92c6f592b0275, 0x4a7484aa6ea6e483,
- 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
- 0x983e5152ee66dfab, 0xa831c66d2db43210,
- 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
- 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
- 0x06ca6351e003826f, 0x142929670a0e6e70,
- 0x27b70a8546d22ffc, 0x2e1b21385c26c926,
- 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
- 0x650a73548baf63de, 0x766a0abb3c77b2a8,
- 0x81c2c92e47edaee6, 0x92722c851482353b,
- 0xa2bfe8a14cf10364, 0xa81a664bbc423001,
- 0xc24b8b70d0f89791, 0xc76c51a30654be30,
- 0xd192e819d6ef5218, 0xd69906245565a910,
- 0xf40e35855771202a, 0x106aa07032bbd1b8,
- 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
- 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
- 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,
- 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
- 0x748f82ee5defb2fc, 0x78a5636f43172f60,
- 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
- 0x90befffa23631e28, 0xa4506cebde82bde9,
- 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
- 0xca273eceea26619c, 0xd186b8c721c0c207,
- 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
- 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
- 0x113f9804bef90dae, 0x1b710b35131c471b,
- 0x28db77f523047d84, 0x32caab7b40c72493,
- 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
- 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,
- 0x5fcb6fab3ad6faec, 0x6c44198c4a475817};
+cc_u64 sha384_h0[8] = {
+ 0xcbbb9d5dc1059ed8, 0x629a292a367cd507,
+ 0x9159015a3070dd17, 0x152fecd8f70e5939,
+ 0x67332667ffc00b31, 0x8eb44a8768581511,
+ 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4
+};
+
+cc_u64 sha512_h0[8] = {
+ 0x6a09e667f3bcc908, 0xbb67ae8584caa73b,
+ 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
+ 0x510e527fade682d1, 0x9b05688c2b3e6c1f,
+ 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179
+};
+
+cc_u64 sha512_k[80] = {
+ 0x428a2f98d728ae22, 0x7137449123ef65cd,
+ 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
+ 0x3956c25bf348b538, 0x59f111f1b605d019,
+ 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
+ 0xd807aa98a3030242, 0x12835b0145706fbe,
+ 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
+ 0x72be5d74f27b896f, 0x80deb1fe3b1696b1,
+ 0x9bdc06a725c71235, 0xc19bf174cf692694,
+ 0xe49b69c19ef14ad2, 0xefbe4786384f25e3,
+ 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
+ 0x2de92c6f592b0275, 0x4a7484aa6ea6e483,
+ 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
+ 0x983e5152ee66dfab, 0xa831c66d2db43210,
+ 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
+ 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
+ 0x06ca6351e003826f, 0x142929670a0e6e70,
+ 0x27b70a8546d22ffc, 0x2e1b21385c26c926,
+ 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
+ 0x650a73548baf63de, 0x766a0abb3c77b2a8,
+ 0x81c2c92e47edaee6, 0x92722c851482353b,
+ 0xa2bfe8a14cf10364, 0xa81a664bbc423001,
+ 0xc24b8b70d0f89791, 0xc76c51a30654be30,
+ 0xd192e819d6ef5218, 0xd69906245565a910,
+ 0xf40e35855771202a, 0x106aa07032bbd1b8,
+ 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
+ 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
+ 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,
+ 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
+ 0x748f82ee5defb2fc, 0x78a5636f43172f60,
+ 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
+ 0x90befffa23631e28, 0xa4506cebde82bde9,
+ 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
+ 0xca273eceea26619c, 0xd186b8c721c0c207,
+ 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
+ 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
+ 0x113f9804bef90dae, 0x1b710b35131c471b,
+ 0x28db77f523047d84, 0x32caab7b40c72493,
+ 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
+ 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,
+ 0x5fcb6fab3ad6faec, 0x6c44198c4a475817
+};
#else
-cc_u64 sha384_h0[8] =
- {0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL,
- 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL,
- 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL,
- 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL};
-
-cc_u64 sha512_h0[8] =
- {0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
- 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
- 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
- 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
-
-cc_u64 sha512_k[80] =
- {0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
- 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
- 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
- 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
- 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
- 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
- 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
- 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
- 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
- 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
- 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
- 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
- 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
- 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
- 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
- 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
- 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
- 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
- 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
- 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
- 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
- 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
- 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
- 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
- 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
- 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
- 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
- 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
- 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
- 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
- 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
- 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
- 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
- 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
- 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
- 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
- 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
- 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
- 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
- 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL};
+cc_u64 sha384_h0[8] = {
+ 0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL,
+ 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL,
+ 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL,
+ 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL
+};
+
+cc_u64 sha512_h0[8] = {
+ 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
+ 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
+ 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
+ 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
+};
+
+cc_u64 sha512_k[80] = {
+ 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
+ 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
+ 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
+ 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
+ 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
+ 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
+ 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
+ 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
+ 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
+ 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
+ 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
+ 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
+ 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
+ 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
+ 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
+ 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
+ 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
+ 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
+ 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
+ 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
+ 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
+ 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
+ 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
+ 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
+ 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
+ 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
+ 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
+ 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
+ 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
+ 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
+ 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
+ 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
+ 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
+ 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
+ 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
+ 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
+ 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
+ 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
+ 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
+ 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
+};
#endif //_WIN32
#endif //_OP64_NOTSUPPORTED
/* SHA-256 functions */
-void SDRM_SHA256_Transf(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 block_nb)
+void SDRM_SHA256_Transf(SDRM_SHA256Context *ctx, const cc_u8 *message,
+ cc_u32 block_nb)
{
cc_u32 w[64];
cc_u32 wv[8];
@@ -280,28 +290,21 @@ void SDRM_SHA256_Transf(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 bl
int j;
- for (i = 0; i < (int) block_nb; i++)
- {
+ for (i = 0; i < (int) block_nb; i++) {
sub_block = message + (i << 6);
for (j = 0; j < 16; j++)
- {
SDRM_SHA2_PACK32(&sub_block[j << 2], &w[j]);
- }
for (j = 16; j < 64; j++)
- {
SDRM_SHA2_SHA256_SCR(j);
- }
for (j = 0; j < 8; j++)
- {
wv[j] = ctx->h[j];
- }
- for (j = 0; j < 64; j++)
- {
- t1 = wv[7] + SDRM_SHA2_SHA256_F2(wv[4]) + SDRM_SHA2_CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j];
+ for (j = 0; j < 64; j++) {
+ t1 = wv[7] + SDRM_SHA2_SHA256_F2(wv[4]) + SDRM_SHA2_CH(wv[4], wv[5],
+ wv[6]) + sha256_k[j] + w[j];
t2 = SDRM_SHA2_SHA256_F1(wv[0]) + SDRM_SHA2_MAJ(wv[0], wv[1], wv[2]);
wv[7] = wv[6];
wv[6] = wv[5];
@@ -314,25 +317,23 @@ void SDRM_SHA256_Transf(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 bl
}
for (j = 0; j < 8; j++)
- {
ctx->h[j] += wv[j];
- }
}
}
-void SDRM_SHA256_Init(SDRM_SHA256Context* ctx)
+void SDRM_SHA256_Init(SDRM_SHA256Context *ctx)
{
int i;
+
for (i = 0; i < 8; i++)
- {
ctx->h[i] = sha256_h0[i];
- }
ctx->len = 0;
ctx->tot_len = 0;
}
-void SDRM_SHA256_Update(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 len)
+void SDRM_SHA256_Update(SDRM_SHA256Context *ctx, const cc_u8 *message,
+ cc_u32 len)
{
cc_u32 block_nb;
cc_u32 new_len, rem_len, tmp_len;
@@ -343,8 +344,7 @@ void SDRM_SHA256_Update(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 le
memcpy(&ctx->block[ctx->len], message, rem_len);
- if (ctx->len + len < SDRM_SHA256_DATA_SIZE)
- {
+ if (ctx->len + len < SDRM_SHA256_DATA_SIZE) {
ctx->len += len;
return;
}
@@ -365,7 +365,7 @@ void SDRM_SHA256_Update(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 le
ctx->tot_len += (block_nb + 1) << 6;
}
-void SDRM_SHA256_Final(SDRM_SHA256Context* ctx, cc_u8 *digest)
+void SDRM_SHA256_Final(SDRM_SHA256Context *ctx, cc_u8 *digest)
{
cc_u32 block_nb;
cc_u32 pm_len;
@@ -373,7 +373,8 @@ void SDRM_SHA256_Final(SDRM_SHA256Context* ctx, cc_u8 *digest)
int i;
- block_nb = (1 + ((SDRM_SHA256_DATA_SIZE - 9) < (ctx->len % SDRM_SHA256_DATA_SIZE)));
+ block_nb = (1 + ((SDRM_SHA256_DATA_SIZE - 9) < (ctx->len %
+ SDRM_SHA256_DATA_SIZE)));
len_b = (ctx->tot_len + ctx->len) << 3;
pm_len = block_nb << 6;
@@ -385,16 +386,15 @@ void SDRM_SHA256_Final(SDRM_SHA256Context* ctx, cc_u8 *digest)
SDRM_SHA256_Transf(ctx, ctx->block, block_nb);
for (i = 0 ; i < 8; i++)
- {
SDRM_SHA2_UNPACK32(ctx->h[i], &digest[i << 2]);
- }
}
#ifndef _OP64_NOTSUPPORTED
/* SHA-512 functions */
-void SDRM_SHA512_Transf(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 block_nb)
+void SDRM_SHA512_Transf(SDRM_SHA512Context *ctx, const cc_u8 *message,
+ cc_u32 block_nb)
{
cc_u64 w[80];
cc_u64 wv[8];
@@ -402,29 +402,21 @@ void SDRM_SHA512_Transf(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 bl
const cc_u8 *sub_block;
int i, j;
- for (i = 0; i < (int) block_nb; i++)
- {
+ for (i = 0; i < (int) block_nb; i++) {
sub_block = message + (i << 7);
for (j = 0; j < 16; j++)
- {
SDRM_SHA2_PACK64(&sub_block[j << 3], &w[j]);
- }
for (j = 16; j < 80; j++)
- {
SDRM_SHA2_SHA512_SCR(j);
- }
for (j = 0; j < 8; j++)
- {
wv[j] = ctx->h[j];
- }
- for (j = 0; j < 80; j++)
- {
+ for (j = 0; j < 80; j++) {
t1 = wv[7] + SDRM_SHA2_SHA512_F2(wv[4]) + SDRM_SHA2_CH(wv[4], wv[5], wv[6])
- + sha512_k[j] + w[j];
+ + sha512_k[j] + w[j];
t2 = SDRM_SHA2_SHA512_F1(wv[0]) + SDRM_SHA2_MAJ(wv[0], wv[1], wv[2]);
wv[7] = wv[6];
wv[6] = wv[5];
@@ -437,25 +429,23 @@ void SDRM_SHA512_Transf(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 bl
}
for (j = 0; j < 8; j++)
- {
ctx->h[j] += wv[j];
- }
}
}
-void SDRM_SHA512_Init(SDRM_SHA512Context* ctx)
+void SDRM_SHA512_Init(SDRM_SHA512Context *ctx)
{
int i;
+
for (i = 0; i < 8; i++)
- {
ctx->h[i] = sha512_h0[i];
- }
ctx->len = 0;
ctx->tot_len = 0;
}
-void SDRM_SHA512_Update(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 len)
+void SDRM_SHA512_Update(SDRM_SHA512Context *ctx, const cc_u8 *message,
+ cc_u32 len)
{
cc_u32 block_nb;
cc_u32 new_len, rem_len, tmp_len;
@@ -466,8 +456,7 @@ void SDRM_SHA512_Update(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 le
memcpy(&ctx->block[ctx->len], message, rem_len);
- if (ctx->len + len < SDRM_SHA512_DATA_SIZE)
- {
+ if (ctx->len + len < SDRM_SHA512_DATA_SIZE) {
ctx->len += len;
return;
}
@@ -488,7 +477,7 @@ void SDRM_SHA512_Update(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 le
ctx->tot_len += (block_nb + 1) << 7;
}
-void SDRM_SHA512_Final(SDRM_SHA512Context* ctx, cc_u8 *digest)
+void SDRM_SHA512_Final(SDRM_SHA512Context *ctx, cc_u8 *digest)
{
cc_u32 block_nb;
cc_u32 pm_len;
@@ -496,7 +485,8 @@ void SDRM_SHA512_Final(SDRM_SHA512Context* ctx, cc_u8 *digest)
int i;
- block_nb = 1 + ((SDRM_SHA512_DATA_SIZE - 17) < (ctx->len % SDRM_SHA512_DATA_SIZE));
+ block_nb = 1 + ((SDRM_SHA512_DATA_SIZE - 17) < (ctx->len %
+ SDRM_SHA512_DATA_SIZE));
len_b = (ctx->tot_len + ctx->len) << 3;
pm_len = block_nb << 7;
@@ -508,26 +498,24 @@ void SDRM_SHA512_Final(SDRM_SHA512Context* ctx, cc_u8 *digest)
SDRM_SHA512_Transf(ctx, ctx->block, block_nb);
for (i = 0 ; i < 8; i++)
- {
SDRM_SHA2_UNPACK64(ctx->h[i], &digest[i << 3]);
- }
}
/* SHA-384 functions */
-void SDRM_SHA384_Init(SDRM_SHA384Context* ctx)
+void SDRM_SHA384_Init(SDRM_SHA384Context *ctx)
{
int i;
+
for (i = 0; i < 8; i++)
- {
ctx->h[i] = sha384_h0[i];
- }
ctx->len = 0;
ctx->tot_len = 0;
}
-void SDRM_SHA384_Update(SDRM_SHA384Context* ctx, const cc_u8 *message, cc_u32 len)
+void SDRM_SHA384_Update(SDRM_SHA384Context *ctx, const cc_u8 *message,
+ cc_u32 len)
{
cc_u32 block_nb;
cc_u32 new_len, rem_len, tmp_len;
@@ -538,8 +526,7 @@ void SDRM_SHA384_Update(SDRM_SHA384Context* ctx, const cc_u8 *message, cc_u32 le
memcpy(&ctx->block[ctx->len], message, rem_len);
- if (ctx->len + len < SDRM_SHA384_DATA_SIZE)
- {
+ if (ctx->len + len < SDRM_SHA384_DATA_SIZE) {
ctx->len += len;
return;
}
@@ -560,7 +547,7 @@ void SDRM_SHA384_Update(SDRM_SHA384Context* ctx, const cc_u8 *message, cc_u32 le
ctx->tot_len += (block_nb + 1) << 7;
}
-void SDRM_SHA384_Final(SDRM_SHA384Context* ctx, cc_u8 *digest)
+void SDRM_SHA384_Final(SDRM_SHA384Context *ctx, cc_u8 *digest)
{
cc_u32 block_nb;
cc_u32 pm_len;
@@ -568,7 +555,8 @@ void SDRM_SHA384_Final(SDRM_SHA384Context* ctx, cc_u8 *digest)
int i;
- block_nb = (1 + ((SDRM_SHA384_DATA_SIZE - 17) < (ctx->len % SDRM_SHA384_DATA_SIZE)));
+ block_nb = (1 + ((SDRM_SHA384_DATA_SIZE - 17) < (ctx->len %
+ SDRM_SHA384_DATA_SIZE)));
len_b = (ctx->tot_len + ctx->len) << 3;
pm_len = block_nb << 7;
@@ -580,9 +568,7 @@ void SDRM_SHA384_Final(SDRM_SHA384Context* ctx, cc_u8 *digest)
SDRM_SHA512_Transf(ctx, ctx->block, block_nb);
for (i = 0 ; i < 6; i++)
- {
SDRM_SHA2_UNPACK64(ctx->h[i], &digest[i << 3]);
- }
}
#endif //_OP64_NOTSUPPORTED
@@ -591,70 +577,69 @@ void SDRM_SHA384_Final(SDRM_SHA384Context* ctx, cc_u8 *digest)
void SDRM_SHA224_Init(SDRM_SHA224Context *ctx)
{
- int i;
- for (i = 0; i < 8; i++) {
- ctx->h[i] = sha224_h0[i];
- }
+ int i;
+
+ for (i = 0; i < 8; i++)
+ ctx->h[i] = sha224_h0[i];
- ctx->len = 0;
- ctx->tot_len = 0;
+ ctx->len = 0;
+ ctx->tot_len = 0;
}
void SDRM_SHA224_Update(SDRM_SHA224Context *ctx, const unsigned char *message,
- unsigned int len)
+ unsigned int len)
{
- unsigned int block_nb;
- unsigned int new_len, rem_len, tmp_len;
- const unsigned char *shifted_message;
+ unsigned int block_nb;
+ unsigned int new_len, rem_len, tmp_len;
+ const unsigned char *shifted_message;
- tmp_len = SDRM_SHA224_DATA_SIZE - ctx->len;
- rem_len = len < tmp_len ? len : tmp_len;
+ tmp_len = SDRM_SHA224_DATA_SIZE - ctx->len;
+ rem_len = len < tmp_len ? len : tmp_len;
- memcpy(&ctx->block[ctx->len], message, rem_len);
+ memcpy(&ctx->block[ctx->len], message, rem_len);
- if (ctx->len + len < SDRM_SHA224_DATA_SIZE) {
- ctx->len += len;
- return;
- }
+ if (ctx->len + len < SDRM_SHA224_DATA_SIZE) {
+ ctx->len += len;
+ return;
+ }
- new_len = len - rem_len;
- block_nb = new_len / SDRM_SHA224_DATA_SIZE;
+ new_len = len - rem_len;
+ block_nb = new_len / SDRM_SHA224_DATA_SIZE;
- shifted_message = message + rem_len;
+ shifted_message = message + rem_len;
- SDRM_SHA256_Transf(ctx, ctx->block, 1);
- SDRM_SHA256_Transf(ctx, shifted_message, block_nb);
+ SDRM_SHA256_Transf(ctx, ctx->block, 1);
+ SDRM_SHA256_Transf(ctx, shifted_message, block_nb);
- rem_len = new_len % SDRM_SHA224_DATA_SIZE;
+ rem_len = new_len % SDRM_SHA224_DATA_SIZE;
- memcpy(ctx->block, &shifted_message[block_nb << 6],
- rem_len);
+ memcpy(ctx->block, &shifted_message[block_nb << 6],
+ rem_len);
- ctx->len = rem_len;
- ctx->tot_len += (block_nb + 1) << 6;
+ ctx->len = rem_len;
+ ctx->tot_len += (block_nb + 1) << 6;
}
void SDRM_SHA224_Final(SDRM_SHA224Context *ctx, unsigned char *digest)
{
- unsigned int block_nb;
- unsigned int pm_len;
- unsigned int len_b;
+ unsigned int block_nb;
+ unsigned int pm_len;
+ unsigned int len_b;
- int i;
+ int i;
- block_nb = (1 + ((SDRM_SHA224_DATA_SIZE - 9)
- < (ctx->len % SDRM_SHA224_DATA_SIZE)));
+ block_nb = (1 + ((SDRM_SHA224_DATA_SIZE - 9)
+ < (ctx->len % SDRM_SHA224_DATA_SIZE)));
- len_b = (ctx->tot_len + ctx->len) << 3;
- pm_len = block_nb << 6;
+ len_b = (ctx->tot_len + ctx->len) << 3;
+ pm_len = block_nb << 6;
- memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
- ctx->block[ctx->len] = 0x80;
- SDRM_SHA2_UNPACK32(len_b, ctx->block + pm_len - 4);
+ memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
+ ctx->block[ctx->len] = 0x80;
+ SDRM_SHA2_UNPACK32(len_b, ctx->block + pm_len - 4);
- SDRM_SHA256_Transf(ctx, ctx->block, block_nb);
+ SDRM_SHA256_Transf(ctx, ctx->block, block_nb);
- for (i = 0 ; i < 7; i++) {
- SDRM_SHA2_UNPACK32(ctx->h[i], &digest[i << 2]);
- }
+ for (i = 0 ; i < 7; i++)
+ SDRM_SHA2_UNPACK32(ctx->h[i], &digest[i << 2]);
}
diff --git a/ssflib/dep/cryptocore/source/base/cc_snow2.c b/ssflib/dep/cryptocore/source/base/cc_snow2.c
index 1d941ff..c959800 100644
--- a/ssflib/dep/cryptocore/source/base/cc_snow2.c
+++ b/ssflib/dep/cryptocore/source/base/cc_snow2.c
@@ -29,7 +29,7 @@
////////////////////////////////////////////////////////////////////////////
// pre-computated values
////////////////////////////////////////////////////////////////////////////
-static cc_u32 SNOW2_MUL_a[256]= {
+static cc_u32 SNOW2_MUL_a[256] = {
0x00000000, 0xE19FCF13, 0x6B973726, 0x8A08F835, 0xD6876E4C, 0x3718A15F, 0xBD10596A, 0x5C8F9679,
0x05A7DC98, 0xE438138B, 0x6E30EBBE, 0x8FAF24AD, 0xD320B2D4, 0x32BF7DC7, 0xB8B785F2, 0x59284AE1,
0x0AE71199, 0xEB78DE8A, 0x617026BF, 0x80EFE9AC, 0xDC607FD5, 0x3DFFB0C6, 0xB7F748F3, 0x566887E0,
@@ -64,8 +64,8 @@ static cc_u32 SNOW2_MUL_a[256]= {
0x63DC2392, 0x8243EC81, 0x084B14B4, 0xE9D4DBA7, 0xB55B4DDE, 0x54C482CD, 0xDECC7AF8, 0x3F53B5EB
};
-static cc_u32 SNOW2_MUL_ainverse[256]= {
- 0x00000000, 0x180F40CD, 0x301E8033, 0x2811C0FE, 0x603CA966, 0x7833E9AB, 0x50222955, 0x482D6998,
+static cc_u32 SNOW2_MUL_ainverse[256] = {
+ 0x00000000, 0x180F40CD, 0x301E8033, 0x2811C0FE, 0x603CA966, 0x7833E9AB, 0x50222955, 0x482D6998,
0xC078FBCC, 0xD877BB01, 0xF0667BFF, 0xE8693B32, 0xA04452AA, 0xB84B1267, 0x905AD299, 0x88559254,
0x29F05F31, 0x31FF1FFC, 0x19EEDF02, 0x01E19FCF, 0x49CCF657, 0x51C3B69A, 0x79D27664, 0x61DD36A9,
0xE988A4FD, 0xF187E430, 0xD99624CE, 0xC1996403, 0x89B40D9B, 0x91BB4D56, 0xB9AA8DA8, 0xA1A5CD65,
@@ -84,7 +84,7 @@ static cc_u32 SNOW2_MUL_ainverse[256]= {
0xE18D0321, 0xF98243EC, 0xD1938312, 0xC99CC3DF, 0x81B1AA47, 0x99BEEA8A, 0xB1AF2A74, 0xA9A06AB9,
0x21F5F8ED, 0x39FAB820, 0x11EB78DE, 0x09E43813, 0x41C9518B, 0x59C61146, 0x71D7D1B8, 0x69D89175,
0xC87D5C10, 0xD0721CDD, 0xF863DC23, 0xE06C9CEE, 0xA841F576, 0xB04EB5BB, 0x985F7545, 0x80503588,
- 0x0805A7DC, 0x100AE711, 0x381B27EF, 0x20146722, 0x68390EBA, 0x70364E77, 0x58278E89, 0x4028CE44,
+ 0x0805A7DC, 0x100AE711, 0x381B27EF, 0x20146722, 0x68390EBA, 0x70364E77, 0x58278E89, 0x4028CE44,
0xB3C4BD43, 0xABCBFD8E, 0x83DA3D70, 0x9BD57DBD, 0xD3F81425, 0xCBF754E8, 0xE3E69416, 0xFBE9D4DB,
0x73BC468F, 0x6BB30642, 0x43A2C6BC, 0x5BAD8671, 0x1380EFE9, 0x0B8FAF24, 0x239E6FDA, 0x3B912F17,
0x9A34E272, 0x823BA2BF, 0xAA2A6241, 0xB225228C, 0xFA084B14, 0xE2070BD9, 0xCA16CB27, 0xD2198BEA,
@@ -99,15 +99,15 @@ static cc_u32 SNOW2_MUL_ainverse[256]= {
0xFEDECC7A, 0xE6D18CB7, 0xCEC04C49, 0xD6CF0C84, 0x9EE2651C, 0x86ED25D1, 0xAEFCE52F, 0xB6F3A5E2
};
-static cc_u32 SNOW2_T0[256]= {
+static cc_u32 SNOW2_T0[256] = {
0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6, 0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591,
0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56, 0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec,
0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa, 0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb,
0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45, 0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b,
0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c, 0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83,
0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9, 0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a,
- 0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d, 0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f,
- 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df, 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea,
+ 0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d, 0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f,
+ 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df, 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea,
0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34, 0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b,
0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d, 0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413,
0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1, 0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6,
@@ -134,16 +134,16 @@ static cc_u32 SNOW2_T0[256]= {
0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e, 0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c
};
-static cc_u32 SNOW2_T1[256]= {
+static cc_u32 SNOW2_T1[256] = {
0x6363c6a5, 0x7c7cf884, 0x7777ee99, 0x7b7bf68d, 0xf2f2ff0d, 0x6b6bd6bd, 0x6f6fdeb1, 0xc5c59154,
0x30306050, 0x01010203, 0x6767cea9, 0x2b2b567d, 0xfefee719, 0xd7d7b562, 0xabab4de6, 0x7676ec9a,
0xcaca8f45, 0x82821f9d, 0xc9c98940, 0x7d7dfa87, 0xfafaef15, 0x5959b2eb, 0x47478ec9, 0xf0f0fb0b,
0xadad41ec, 0xd4d4b367, 0xa2a25ffd, 0xafaf45ea, 0x9c9c23bf, 0xa4a453f7, 0x7272e496, 0xc0c09b5b,
0xb7b775c2, 0xfdfde11c, 0x93933dae, 0x26264c6a, 0x36366c5a, 0x3f3f7e41, 0xf7f7f502, 0xcccc834f,
0x3434685c, 0xa5a551f4, 0xe5e5d134, 0xf1f1f908, 0x7171e293, 0xd8d8ab73, 0x31316253, 0x15152a3f,
- 0x0404080c, 0xc7c79552, 0x23234665, 0xc3c39d5e, 0x18183028, 0x969637a1, 0x05050a0f, 0x9a9a2fb5,
- 0x07070e09, 0x12122436, 0x80801b9b, 0xe2e2df3d, 0xebebcd26, 0x27274e69, 0xb2b27fcd, 0x7575ea9f,
- 0x0909121b, 0x83831d9e, 0x2c2c5874, 0x1a1a342e, 0x1b1b362d, 0x6e6edcb2, 0x5a5ab4ee, 0xa0a05bfb,
+ 0x0404080c, 0xc7c79552, 0x23234665, 0xc3c39d5e, 0x18183028, 0x969637a1, 0x05050a0f, 0x9a9a2fb5,
+ 0x07070e09, 0x12122436, 0x80801b9b, 0xe2e2df3d, 0xebebcd26, 0x27274e69, 0xb2b27fcd, 0x7575ea9f,
+ 0x0909121b, 0x83831d9e, 0x2c2c5874, 0x1a1a342e, 0x1b1b362d, 0x6e6edcb2, 0x5a5ab4ee, 0xa0a05bfb,
0x5252a4f6, 0x3b3b764d, 0xd6d6b761, 0xb3b37dce, 0x2929527b, 0xe3e3dd3e, 0x2f2f5e71, 0x84841397,
0x5353a6f5, 0xd1d1b968, 0x00000000, 0xededc12c, 0x20204060, 0xfcfce31f, 0xb1b179c8, 0x5b5bb6ed,
0x6a6ad4be, 0xcbcb8d46, 0xbebe67d9, 0x3939724b, 0x4a4a94de, 0x4c4c98d4, 0x5858b0e8, 0xcfcf854a,
@@ -169,16 +169,16 @@ static cc_u32 SNOW2_T1[256]= {
0x414182c3, 0x999929b0, 0x2d2d5a77, 0x0f0f1e11, 0xb0b07bcb, 0x5454a8fc, 0xbbbb6dd6, 0x16162c3a
};
-static cc_u32 SNOW2_T2[256]= {
+static cc_u32 SNOW2_T2[256] = {
0x63c6a563, 0x7cf8847c, 0x77ee9977, 0x7bf68d7b, 0xf2ff0df2, 0x6bd6bd6b, 0x6fdeb16f, 0xc59154c5,
0x30605030, 0x01020301, 0x67cea967, 0x2b567d2b, 0xfee719fe, 0xd7b562d7, 0xab4de6ab, 0x76ec9a76,
0xca8f45ca, 0x821f9d82, 0xc98940c9, 0x7dfa877d, 0xfaef15fa, 0x59b2eb59, 0x478ec947, 0xf0fb0bf0,
0xad41ecad, 0xd4b367d4, 0xa25ffda2, 0xaf45eaaf, 0x9c23bf9c, 0xa453f7a4, 0x72e49672, 0xc09b5bc0,
0xb775c2b7, 0xfde11cfd, 0x933dae93, 0x264c6a26, 0x366c5a36, 0x3f7e413f, 0xf7f502f7, 0xcc834fcc,
0x34685c34, 0xa551f4a5, 0xe5d134e5, 0xf1f908f1, 0x71e29371, 0xd8ab73d8, 0x31625331, 0x152a3f15,
- 0x04080c04, 0xc79552c7, 0x23466523, 0xc39d5ec3, 0x18302818, 0x9637a196, 0x050a0f05, 0x9a2fb59a,
- 0x070e0907, 0x12243612, 0x801b9b80, 0xe2df3de2, 0xebcd26eb, 0x274e6927, 0xb27fcdb2, 0x75ea9f75,
- 0x09121b09, 0x831d9e83, 0x2c58742c, 0x1a342e1a, 0x1b362d1b, 0x6edcb26e, 0x5ab4ee5a, 0xa05bfba0,
+ 0x04080c04, 0xc79552c7, 0x23466523, 0xc39d5ec3, 0x18302818, 0x9637a196, 0x050a0f05, 0x9a2fb59a,
+ 0x070e0907, 0x12243612, 0x801b9b80, 0xe2df3de2, 0xebcd26eb, 0x274e6927, 0xb27fcdb2, 0x75ea9f75,
+ 0x09121b09, 0x831d9e83, 0x2c58742c, 0x1a342e1a, 0x1b362d1b, 0x6edcb26e, 0x5ab4ee5a, 0xa05bfba0,
0x52a4f652, 0x3b764d3b, 0xd6b761d6, 0xb37dceb3, 0x29527b29, 0xe3dd3ee3, 0x2f5e712f, 0x84139784,
0x53a6f553, 0xd1b968d1, 0x00000000, 0xedc12ced, 0x20406020, 0xfce31ffc, 0xb179c8b1, 0x5bb6ed5b,
0x6ad4be6a, 0xcb8d46cb, 0xbe67d9be, 0x39724b39, 0x4a94de4a, 0x4c98d44c, 0x58b0e858, 0xcf854acf,
@@ -204,15 +204,15 @@ static cc_u32 SNOW2_T2[256]= {
0x4182c341, 0x9929b099, 0x2d5a772d, 0x0f1e110f, 0xb07bcbb0, 0x54a8fc54, 0xbb6dd6bb, 0x162c3a16
};
-static cc_u32 SNOW2_T3[256]= {
+static cc_u32 SNOW2_T3[256] = {
0xc6a56363, 0xf8847c7c, 0xee997777, 0xf68d7b7b, 0xff0df2f2, 0xd6bd6b6b, 0xdeb16f6f, 0x9154c5c5,
0x60503030, 0x02030101, 0xcea96767, 0x567d2b2b, 0xe719fefe, 0xb562d7d7, 0x4de6abab, 0xec9a7676,
0x8f45caca, 0x1f9d8282, 0x8940c9c9, 0xfa877d7d, 0xef15fafa, 0xb2eb5959, 0x8ec94747, 0xfb0bf0f0,
0x41ecadad, 0xb367d4d4, 0x5ffda2a2, 0x45eaafaf, 0x23bf9c9c, 0x53f7a4a4, 0xe4967272, 0x9b5bc0c0,
0x75c2b7b7, 0xe11cfdfd, 0x3dae9393, 0x4c6a2626, 0x6c5a3636, 0x7e413f3f, 0xf502f7f7, 0x834fcccc,
0x685c3434, 0x51f4a5a5, 0xd134e5e5, 0xf908f1f1, 0xe2937171, 0xab73d8d8, 0x62533131, 0x2a3f1515,
- 0x080c0404, 0x9552c7c7, 0x46652323, 0x9d5ec3c3, 0x30281818, 0x37a19696, 0xa0f0505, 0x2fb59a9a,
- 0x0e090707, 0x24361212, 0x1b9b8080, 0xdf3de2e2, 0xcd26ebeb, 0x4e692727, 0x7fcdb2b2, 0xea9f7575,
+ 0x080c0404, 0x9552c7c7, 0x46652323, 0x9d5ec3c3, 0x30281818, 0x37a19696, 0xa0f0505, 0x2fb59a9a,
+ 0x0e090707, 0x24361212, 0x1b9b8080, 0xdf3de2e2, 0xcd26ebeb, 0x4e692727, 0x7fcdb2b2, 0xea9f7575,
0x121b0909, 0x1d9e8383, 0x58742c2c, 0x342e1a1a, 0x362d1b1b, 0xdcb26e6e, 0xb4ee5a5a, 0x5bfba0a0,
0xa4f65252, 0x764d3b3b, 0xb761d6d6, 0x7dceb3b3, 0x527b2929, 0xdd3ee3e3, 0x5e712f2f, 0x13978484,
0xa6f55353, 0xb968d1d1, 0x00000000, 0xc12ceded, 0x40602020, 0xe31ffcfc, 0x79c8b1b1, 0xb6ed5b5b,
@@ -235,36 +235,37 @@ static cc_u32 SNOW2_T3[256]= {
0xc2a36161, 0x6a5f3535, 0xaef95757, 0x69d0b9b9, 0x17918686, 0x9958c1c1, 0x3a271d1d, 0x27b99e9e,
0xd938e1e1, 0xeb13f8f8, 0x2bb39898, 0x22331111, 0xd2bb6969, 0xa970d9d9, 0x07898e8e, 0x33a79494,
0x2db69b9b, 0x3c221e1e, 0x15928787, 0xc920e9e9, 0x8749cece, 0xaaff5555, 0x50782828, 0xa57adfdf,
- 0x038f8c8c, 0x59f8a1a1, 0x09808989, 0x1a170d0d, 0x65dabfbf, 0xd731e6e6, 0x84c64242, 0xd0b86868,
+ 0x038f8c8c, 0x59f8a1a1, 0x09808989, 0x1a170d0d, 0x65dabfbf, 0xd731e6e6, 0x84c64242, 0xd0b86868,
0x82c34141, 0x29b09999, 0x5a772d2d, 0x1e110f0f, 0x7bcbb0b0, 0xa8fc5454, 0x6dd6bbbb, 0x2c3a1616
};
////////////////////////////////////////////////////////////////////////////
// Macros
////////////////////////////////////////////////////////////////////////////
-#define a_MUL(w) (((w) << 8) ^ SNOW2_MUL_a[(w) >> 24])
-#define ainv_MUL(w) (((w) >> 8) ^ SNOW2_MUL_ainverse[(w) & 0xff])
+#define a_MUL(w) (((w) << 8) ^ SNOW2_MUL_a[(w) >> 24])
+#define ainv_MUL(w) (((w) >> 8) ^ SNOW2_MUL_ainverse[(w) & 0xff])
-#define BYTE0(w) ( (w) & 0xff)
-#define BYTE1(w) (((w) >> 8) & 0xff)
-#define BYTE2(w) (((w) >> 16) & 0xff)
-#define BYTE3(w) (((w) >> 24) & 0xff)
+#define BYTE0(w) ((w) & 0xff)
+#define BYTE1(w) (((w) >> 8) & 0xff)
+#define BYTE2(w) (((w) >> 16) & 0xff)
+#define BYTE3(w) (((w) >> 24) & 0xff)
////////////////////////////////////////////////////////////////////////////
// Functions
////////////////////////////////////////////////////////////////////////////
/*
- * @fn SDRM_SNOW2_Setup
- * @brief Setup FSM and s values
+ * @fn SDRM_SNOW2_Setup
+ * @brief Setup FSM and s values
*
- * @param ctx [out]crypto context
- * @param UserKey [in]User Key, 128 or 256 bit
- * @param keyLen [in]byte-size of User Key, 16 or 32
- * @param IV [in]16 byte initial vector
+ * @param ctx [out]crypto context
+ * @param UserKey [in]User Key, 128 or 256 bit
+ * @param keyLen [in]byte-size of User Key, 16 or 32
+ * @param IV [in]16 byte initial vector
*
- * @return CRYPTO_SUCCESS if no error is occured
+ * @return CRYPTO_SUCCESS if no error is occured
*/
-int SDRM_SNOW2_Setup(SDRM_SNOW2Context *ctx, cc_u8 *UserKey, cc_u32 keyLen, cc_u8 *IV)
+int SDRM_SNOW2_Setup(SDRM_SNOW2Context *ctx, cc_u8 *UserKey, cc_u32 keyLen,
+ cc_u8 *IV)
{
cc_u32 IV0, IV1, IV2, IV3;
cc_u32 *s = ctx->s;
@@ -273,7 +274,8 @@ int SDRM_SNOW2_Setup(SDRM_SNOW2Context *ctx, cc_u8 *UserKey, cc_u32 keyLen, cc_u
//test endian
i = 0xff;
- ctx->endian = (*((cc_u8*)&i) == 0xff) ? CRYPTO_LITTLE_ENDIAN : CRYPTO_BIG_ENDIAN;
+ ctx->endian = (*((cc_u8 *)&i) == 0xff) ? CRYPTO_LITTLE_ENDIAN :
+ CRYPTO_BIG_ENDIAN;
//Initialize IV
GET_UINT32(IV3, IV, 0)
@@ -289,60 +291,59 @@ int SDRM_SNOW2_Setup(SDRM_SNOW2Context *ctx, cc_u8 *UserKey, cc_u32 keyLen, cc_u
GET_UINT32(s[12], UserKey, 12)
s[11] = ~s[15];
s[10] = ~s[14];
- s[ 9] = ~s[13];
- s[ 8] = ~s[12];
- s[ 7] = s[15];
- s[ 6] = s[14];
- s[ 5] = s[13];
- s[ 4] = s[12];
- s[ 3] = ~s[15];
- s[ 2] = ~s[14];
- s[ 1] = ~s[13];
- s[ 0] = ~s[12];
- }
- else {
+ s[9] = ~s[13];
+ s[8] = ~s[12];
+ s[7] = s[15];
+ s[6] = s[14];
+ s[5] = s[13];
+ s[4] = s[12];
+ s[3] = ~s[15];
+ s[2] = ~s[14];
+ s[1] = ~s[13];
+ s[0] = ~s[12];
+ } else {
GET_UINT32(s[15], UserKey, 0)
GET_UINT32(s[14], UserKey, 4)
GET_UINT32(s[13], UserKey, 8)
GET_UINT32(s[12], UserKey, 12)
GET_UINT32(s[11], UserKey, 16)
GET_UINT32(s[10], UserKey, 20)
- GET_UINT32(s[ 9], UserKey, 24)
- GET_UINT32(s[ 8], UserKey, 28)
- s[ 7] = ~s[15];
- s[ 6] = ~s[14];
- s[ 5] = ~s[13];
- s[ 4] = ~s[12];
- s[ 3] = ~s[11];
- s[ 2] = ~s[10];
- s[ 1] = ~s[ 9];
- s[ 0] = ~s[ 8];
+ GET_UINT32(s[9], UserKey, 24)
+ GET_UINT32(s[8], UserKey, 28)
+ s[7] = ~s[15];
+ s[6] = ~s[14];
+ s[5] = ~s[13];
+ s[4] = ~s[12];
+ s[3] = ~s[11];
+ s[2] = ~s[10];
+ s[1] = ~s[9];
+ s[0] = ~s[8];
}
s[15] ^= IV0;
s[12] ^= IV1;
s[10] ^= IV2;
- s[ 9] ^= IV3;
+ s[9] ^= IV3;
r1 = 0;
r2 = 0;
// clock 32 times without producing any output
- for (i = 0; i < 16; i++)
- {
+ for (i = 0; i < 16; i++) {
Ft = (r1 + s[(i - 1) & 0x0f]) ^ r2;
s[i] = a_MUL(s[i]) ^ s[(i + 2) & 0x0f] ^ ainv_MUL(s[(i + 11) & 0x0f]) ^ Ft;
R1_next = r2 + s[(i + 5) & 0x0f];
- r2 = SNOW2_T0[BYTE0(r1)] ^ SNOW2_T1[BYTE1(r1)] ^ SNOW2_T2[BYTE2(r1)] ^ SNOW2_T3[BYTE3(r1)];
+ r2 = SNOW2_T0[BYTE0(r1)] ^ SNOW2_T1[BYTE1(r1)] ^ SNOW2_T2[BYTE2(r1)] ^
+ SNOW2_T3[BYTE3(r1)];
r1 = R1_next;
}
- for (i = 0; i < 16; i++)
- {
+ for (i = 0; i < 16; i++) {
Ft = (r1 + s[(i - 1) & 0x0f]) ^ r2;
s[i] = a_MUL(s[i]) ^ s[(i + 2) & 0x0f] ^ ainv_MUL(s[(i + 11) & 0x0f]) ^ Ft;
R1_next = r2 + s[(i + 5) & 0x0f];
- r2 = SNOW2_T0[BYTE0(r1)] ^ SNOW2_T1[BYTE1(r1)] ^ SNOW2_T2[BYTE2(r1)] ^ SNOW2_T3[BYTE3(r1)];
+ r2 = SNOW2_T0[BYTE0(r1)] ^ SNOW2_T1[BYTE1(r1)] ^ SNOW2_T2[BYTE2(r1)] ^
+ SNOW2_T3[BYTE3(r1)];
r1 = R1_next;
}
@@ -355,13 +356,13 @@ int SDRM_SNOW2_Setup(SDRM_SNOW2Context *ctx, cc_u8 *UserKey, cc_u32 keyLen, cc_u
}
/*
- * @fn SDRM_SNOW2_getKeyStream64
- * @brief get 64 byte key stream
+ * @fn SDRM_SNOW2_getKeyStream64
+ * @brief get 64 byte key stream
*
- * @param ctx [out]crypto context
- * @param keyStream64 [in]generated key stream
+ * @param ctx [out]crypto context
+ * @param keyStream64 [in]generated key stream
*
- * @return CRYPTO_SUCCESS if no error is occured
+ * @return CRYPTO_SUCCESS if no error is occured
*/
int SDRM_SNOW2_getKeyStream64(SDRM_SNOW2Context *ctx, cc_u32 *keyStream64)
{
@@ -369,11 +370,12 @@ int SDRM_SNOW2_getKeyStream64(SDRM_SNOW2Context *ctx, cc_u32 *keyStream64)
cc_u32 *s = ctx->s;
cc_u32 t = ctx->t;
- for (i = t; i < t + 16; i++)
- {
- s[i & 0x0f] = a_MUL(s[i & 0x0f]) ^ s[(i + 2) & 0x0f] ^ ainv_MUL(s[(i + 11) & 0x0f]);
+ for (i = t; i < t + 16; i++) {
+ s[i & 0x0f] = a_MUL(s[i & 0x0f]) ^ s[(i + 2) & 0x0f] ^ ainv_MUL(
+ s[(i + 11) & 0x0f]);
R1_next = ctx->r2 + s[(i + 5) & 0x0f];
- ctx->r2 = SNOW2_T0[BYTE0(ctx->r1)] ^ SNOW2_T1[BYTE1(ctx->r1)] ^ SNOW2_T2[BYTE2(ctx->r1)] ^ SNOW2_T3[BYTE3(ctx->r1)];
+ ctx->r2 = SNOW2_T0[BYTE0(ctx->r1)] ^ SNOW2_T1[BYTE1(ctx->r1)] ^
+ SNOW2_T2[BYTE2(ctx->r1)] ^ SNOW2_T3[BYTE3(ctx->r1)];
ctx->r1 = R1_next;
keyStream64[i] = (ctx->r1 + s[i & 0x0f]) ^ ctx->r2 ^ s[(i + 1) & 0x0f];
@@ -386,13 +388,13 @@ int SDRM_SNOW2_getKeyStream64(SDRM_SNOW2Context *ctx, cc_u32 *keyStream64)
/*
- * @fn SDRM_SNOW2_getKeyStream
- * @brief get 4 byte key stream
+ * @fn SDRM_SNOW2_getKeyStream
+ * @brief get 4 byte key stream
*
- * @param ctx [out]crypto context
- * @param keyStream [in]generated key stream
+ * @param ctx [out]crypto context
+ * @param keyStream [in]generated key stream
*
- * @return CRYPTO_SUCCESS if no error is occured
+ * @return CRYPTO_SUCCESS if no error is occured
*/
int SDRM_SNOW2_getKeyStream(SDRM_SNOW2Context *ctx, cc_u32 *keyStream)
{
@@ -400,9 +402,11 @@ int SDRM_SNOW2_getKeyStream(SDRM_SNOW2Context *ctx, cc_u32 *keyStream)
cc_u32 *s = ctx->s;
cc_u32 t = ctx->t;
- s[t & 0x0f] = a_MUL(s[t & 0x0f]) ^ s[(t + 2) & 0x0f] ^ ainv_MUL(s[(t + 11) & 0x0f]);
+ s[t & 0x0f] = a_MUL(s[t & 0x0f]) ^ s[(t + 2) & 0x0f] ^ ainv_MUL(
+ s[(t + 11) & 0x0f]);
R1_next = ctx->r2 + s[(t + 5) & 0x0f];
- ctx->r2 = SNOW2_T0[BYTE0(ctx->r1)] ^ SNOW2_T1[BYTE1(ctx->r1)] ^ SNOW2_T2[BYTE2(ctx->r1)] ^ SNOW2_T3[BYTE3(ctx->r1)];
+ ctx->r2 = SNOW2_T0[BYTE0(ctx->r1)] ^ SNOW2_T1[BYTE1(ctx->r1)] ^
+ SNOW2_T2[BYTE2(ctx->r1)] ^ SNOW2_T3[BYTE3(ctx->r1)];
ctx->r1 = R1_next;
*keyStream = (ctx->r1 + s[t & 0x0f]) ^ ctx->r2 ^ s[(t + 1) & 0x0f];
diff --git a/ssflib/dep/cryptocore/source/middle/cc_cmac.c b/ssflib/dep/cryptocore/source/middle/cc_cmac.c
index 9c960ac..1e33d93 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_cmac.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_cmac.c
@@ -30,53 +30,50 @@
////////////////////////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////////////////////////
-/*! @brief max block columns */
-#define CMAC_MAXBC (256/32)
+/*! @brief max block columns */
+#define CMAC_MAXBC (256/32)
-/*! @brief max key columns */
-#define CMAC_MAXKC (256/32)
+/*! @brief max key columns */
+#define CMAC_MAXKC (256/32)
-/*! @brief max rounds */
-#define CMAC_MAXROUNDS 14
+/*! @brief max rounds */
+#define CMAC_MAXROUNDS 14
-/*! @brief constant - defined in OMAC1a(One-Key CBC MAC1, submitted by Iwata and Kurosawa) */
+/*! @brief constant - defined in OMAC1a(One-Key CBC MAC1, submitted by Iwata and Kurosawa) */
static cc_u8 R_b[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87};
/*
- * @fn int SDRM_CMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen)
+ * @fn int SDRM_CMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen)
*
- * @brief Parameter setting for mac code generation
- * @param crt [out]crypto parameter
- * @param Key [in]user key
- * @param KeyLen [in]byte-length of Key
+ * @brief Parameter setting for mac code generation
+ * @param crt [out]crypto parameter
+ * @param Key [in]user key
+ * @param KeyLen [in]byte-length of Key
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if Parameter is NULL
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if Parameter is NULL
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
int SDRM_CMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen)
{
- cc_u8 *K1, *K2, temp[16] = {0};
- cc_u8 ZERO[16] = {0};
- int i;
- cc_u32 *RoundKey;
+ cc_u8 *K1, *K2, temp[16] = {0};
+ cc_u8 ZERO[16] = {0};
+ int i;
+ cc_u32 *RoundKey;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || (Key == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) ||
+ (Key == NULL))
return CRYPTO_NULL_POINTER;
- }
if (KeyLen != 16)
- {
return CRYPTO_INVALID_ARGUMENT;
- }
memset(crt->ctx->cmacctx->IV, 0, SDRM_AES_BLOCK_SIZ);
crt->ctx->cmacctx->BlockLen = 0;
- RoundKey = (cc_u32*)(void*)(crt->ctx->cmacctx->RoundKey);
+ RoundKey = (cc_u32 *)(void *)(crt->ctx->cmacctx->RoundKey);
K1 = crt->ctx->cmacctx->K1;
K2 = crt->ctx->cmacctx->K2;
@@ -84,98 +81,87 @@ int SDRM_CMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen)
SDRM_rijndaelEncrypt(RoundKey, 10, ZERO, temp);
- if((temp[0] >> 7) == 0x00) // L << 1
- {
+ if ((temp[0] >> 7) == 0x00) { // L << 1
for (i = 0; i < 15; i++)
- {
- K1[i] = (temp[i] << 1) | (temp[i+1] >> 7);
- }
+ K1[i] = (temp[i] << 1) | (temp[i + 1] >> 7);
+
K1[15] = temp[i] << 1;
- }
- else if ((temp[0] >> 7) == 0x01)
- {
+ } else if ((temp[0] >> 7) == 0x01) {
for (i = 0; i < 15; i++)
- {
- K1[i] = (temp[i] << 1) | (temp[i+1] >> 7);
- }
+ K1[i] = (temp[i] << 1) | (temp[i + 1] >> 7);
+
K1[15] = temp[i] << 1;
BlockXor(K1, K1, R_b);
}
- if((K1[0] >> 7) == 0x00) // K1 << 1
- {
+ if ((K1[0] >> 7) == 0x00) { // K1 << 1
for (i = 0; i < 15; i++)
- {
- K2[i] = (K1[i] << 1) | (K1[i+1] >> 7);
- }
+ K2[i] = (K1[i] << 1) | (K1[i + 1] >> 7);
+
K2[15] = K1[i] << 1;
- }
- else if ((K1[0] >> 7) == 0x01)
- {
+ } else if ((K1[0] >> 7) == 0x01) {
for (i = 0; i < 15; i++)
- {
- K2[i] = (K1[i] << 1) | (K1[i+1] >> 7);
- }
+ K2[i] = (K1[i] << 1) | (K1[i + 1] >> 7);
+
K2[15] = K1[i] << 1;
BlockXor(K2, K2, R_b);
}
-// LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K1", K1, 16);
-// LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K2", K2, 16);
+ // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K1", K1, 16);
+ // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K2", K2, 16);
return CRYPTO_SUCCESS;
}
/*
- * @fn int SDRM_CMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen)
- * @brief process data blocks
+ * @fn int SDRM_CMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen)
+ * @brief process data blocks
*
- * @param crt [out]crypto parameter
- * @param msg [in]data block
- * @param msgLen [in]byte-length of Text
+ * @param crt [out]crypto parameter
+ * @param msg [in]data block
+ * @param msgLen [in]byte-length of Text
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if Parameter is NULL
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if Parameter is NULL
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
int SDRM_CMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen)
{
- int Loop;
- cc_u8 *ptr;
+ int Loop;
+ cc_u8 *ptr;
if (msgLen == 0)
- {
return CRYPTO_SUCCESS;
- }
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || (msg == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) ||
+ (msg == NULL))
return CRYPTO_NULL_POINTER;
- }
- if (msgLen + crt->ctx->cmacctx->BlockLen <= SDRM_AES_BLOCK_SIZ)
- {
+ if (msgLen + crt->ctx->cmacctx->BlockLen <= SDRM_AES_BLOCK_SIZ) {
memcpy(crt->ctx->cmacctx->Block + crt->ctx->cmacctx->BlockLen, msg, msgLen);
crt->ctx->cmacctx->BlockLen += msgLen;
return CRYPTO_SUCCESS;
}
- memcpy(crt->ctx->cmacctx->Block + crt->ctx->cmacctx->BlockLen, msg, SDRM_AES_BLOCK_SIZ - crt->ctx->cmacctx->BlockLen);
- SDRM_CBC_Enc(ID_AES128, crt->ctx->cmacctx->IV, crt->ctx->cmacctx->Block, crt->ctx->cmacctx->RoundKey, crt->ctx->cmacctx->IV);
+ memcpy(crt->ctx->cmacctx->Block + crt->ctx->cmacctx->BlockLen, msg,
+ SDRM_AES_BLOCK_SIZ - crt->ctx->cmacctx->BlockLen);
+ SDRM_CBC_Enc(ID_AES128, crt->ctx->cmacctx->IV, crt->ctx->cmacctx->Block,
+ crt->ctx->cmacctx->RoundKey, crt->ctx->cmacctx->IV);
Loop = (msgLen + crt->ctx->cmacctx->BlockLen - 1) / SDRM_AES_BLOCK_SIZ - 1;
ptr = msg + SDRM_AES_BLOCK_SIZ - crt->ctx->cmacctx->BlockLen;
- crt->ctx->cmacctx->BlockLen = (cc_u32)(msg + msgLen - ptr) - Loop * SDRM_AES_BLOCK_SIZ;
+ crt->ctx->cmacctx->BlockLen = (cc_u32)(msg + msgLen - ptr) - Loop *
+ SDRM_AES_BLOCK_SIZ;
- while (Loop > 0)
- {
- SDRM_CBC_Enc(ID_AES128, crt->ctx->cmacctx->IV, ptr, crt->ctx->cmacctx->RoundKey, crt->ctx->cmacctx->IV);
+ while (Loop > 0) {
+ SDRM_CBC_Enc(ID_AES128, crt->ctx->cmacctx->IV, ptr, crt->ctx->cmacctx->RoundKey,
+ crt->ctx->cmacctx->IV);
Loop--;
ptr += SDRM_AES_BLOCK_SIZ;
}
-// LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "Block", crt->ctx->cmacctx->IV, 16);
+ // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "Block", crt->ctx->cmacctx->IV, 16);
memcpy(crt->ctx->cmacctx->Block, ptr, crt->ctx->cmacctx->BlockLen);
@@ -184,85 +170,81 @@ int SDRM_CMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen)
/*
- * @fn int SDRM_CMAC_final(CryptoCoreContainer *crt, cc_u8 *output, cc_u32 *outputLen)
- * @brief process last data block
+ * @fn int SDRM_CMAC_final(CryptoCoreContainer *crt, cc_u8 *output, cc_u32 *outputLen)
+ * @brief process last data block
*
- * @param crt [in]crypto parameter
- * @param output [out]generated MAC
- * @param outputLen [out]byte-length of output
+ * @param crt [in]crypto parameter
+ * @param output [out]generated MAC
+ * @param outputLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if Parameter is NULL
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if Parameter is NULL
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
int SDRM_CMAC_final(CryptoCoreContainer *crt, cc_u8 *output, cc_u32 *outputLen)
{
cc_u8 *K1, *K2;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || (output == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) ||
+ (output == NULL))
return CRYPTO_NULL_POINTER;
- }
K1 = crt->ctx->cmacctx->K1;
K2 = crt->ctx->cmacctx->K2;
if (crt->ctx->cmacctx->BlockLen == SDRM_AES_BLOCK_SIZ)
- {
BlockXor(crt->ctx->cmacctx->Block, crt->ctx->cmacctx->Block, K1);
- }
- else
- {
+
+ else {
crt->ctx->cmacctx->IV[crt->ctx->cmacctx->BlockLen] ^= 0x80;
- BlockXor(crt->ctx->cmacctx->IV, crt->ctx->cmacctx->IV, K2); // input = input XOR K2
- memset(crt->ctx->cmacctx->Block + crt->ctx->cmacctx->BlockLen, 0, SDRM_AES_BLOCK_SIZ - crt->ctx->cmacctx->BlockLen);
+ BlockXor(crt->ctx->cmacctx->IV, crt->ctx->cmacctx->IV,
+ K2); // input = input XOR K2
+ memset(crt->ctx->cmacctx->Block + crt->ctx->cmacctx->BlockLen, 0,
+ SDRM_AES_BLOCK_SIZ - crt->ctx->cmacctx->BlockLen);
}
- SDRM_CBC_Enc(ID_AES128, output, crt->ctx->cmacctx->Block, crt->ctx->cmacctx->RoundKey, crt->ctx->cmacctx->IV);
+ SDRM_CBC_Enc(ID_AES128, output, crt->ctx->cmacctx->Block,
+ crt->ctx->cmacctx->RoundKey, crt->ctx->cmacctx->IV);
if (outputLen != NULL)
- {
*outputLen = 16;
- }
return CRYPTO_SUCCESS;
}
/*
- * @fn int SDRM_CMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen, cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen)
- * @brief generate c-mac code
+ * @fn int SDRM_CMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen, cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen)
+ * @brief generate c-mac code
*
- * @param crt [in]crypto parameter
- * @param Key [in]user key
- * @param KeyLen [in]byte-length of Key
- * @param msg [in]data block
- * @param msgLen [in]byte-length of Text
- * @param output [out]generated MAC
- * @param outputLen [out]byte-length of output
+ * @param crt [in]crypto parameter
+ * @param Key [in]user key
+ * @param KeyLen [in]byte-length of Key
+ * @param msg [in]data block
+ * @param msgLen [in]byte-length of Text
+ * @param output [out]generated MAC
+ * @param outputLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if no error is occured
+ * @return CRYPTO_SUCCESS if no error is occured
*/
-int SDRM_CMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen, cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen)
+int SDRM_CMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen,
+ cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen)
{
int result;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || (Key == NULL) || (output == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) ||
+ (Key == NULL) || (output == NULL))
return CRYPTO_NULL_POINTER;
- }
result = SDRM_CMAC_init(crt, Key, KeyLen);
+
if (result != CRYPTO_SUCCESS)
- {
return result;
- }
result = SDRM_CMAC_update(crt, msg, msgLen);
+
if (result != CRYPTO_SUCCESS)
- {
- return result;
- }
+ return result;
return SDRM_CMAC_final(crt, output, outputLen);
}
diff --git a/ssflib/dep/cryptocore/source/middle/cc_dh.c b/ssflib/dep/cryptocore/source/middle/cc_dh.c
index a7f087a..8f38c17 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_dh.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_dh.c
@@ -33,78 +33,70 @@
// Functions
////////////////////////////////////////////////////////////////////////////
/**
- * @fn SDRM_GenerateDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned int* pGenerator)
- * @brief generate parameters for Diffie-Hellman protocol
+ * @fn SDRM_GenerateDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned int* pGenerator)
+ * @brief generate parameters for Diffie-Hellman protocol
*
- * @param [out] crt context
- * @param [out] pPrime prime number
- * @param [in] nPrimeLen size of pPrime buffer
- * @param [out] pGenerator generator value
+ * @param [out] crt context
+ * @param [out] pPrime prime number
+ * @param [in] nPrimeLen size of pPrime buffer
+ * @param [out] pGenerator generator value
*
- * @return int
+ * @return int
*/
-int SDRM_GenerateDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned char* pGenerator)
+int SDRM_GenerateDHParam(CryptoCoreContainer *crt, unsigned char *pPrime,
+ unsigned int nPrimeLen, unsigned char *pGenerator)
{
- SDRM_DHContext* ctx;
+ SDRM_DHContext *ctx;
cc_u32 Seed[4];
int i, sp, t1;
SDRM_BIG_NUM *p = NULL;
SDRM_BIG_NUM *g = NULL;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || (pPrime == NULL) || (pGenerator == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) ||
+ (pPrime == NULL) || (pGenerator == NULL))
return CRYPTO_NULL_POINTER;
- }
ctx = crt->ctx->dhctx;
p = SDRM_BN_Init(nPrimeLen / 2 + 1);
+
if (p == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
g = SDRM_BN_Init(nPrimeLen / 2 + 1);
- if (g == NULL)
- {
+
+ if (g == NULL) {
free(p);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
for (i = 0; i < 4; i++)
- {
Seed[i] = (rand() << 16) ^ rand();
- }
t1 = (nPrimeLen * 8 - 1) % 32;
//set security parameter for miller-rabin probabilistic primality test
if (nPrimeLen >= 128)
- {
sp = 3;
- }
+
else if (nPrimeLen >= 64)
- {
sp = 5;
- }
+
else if (nPrimeLen >= 15)
- {
sp = 15;
- }
+
else
- {
sp = 30;
- }
//generate p
p->Length = (nPrimeLen + 3) / 4;
+
do {
- SDRM_RNG_X931((cc_u8 *)Seed, nPrimeLen * 8, (cc_u8*)p->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, nPrimeLen * 8, (cc_u8 *)p->pData);
p->pData[0] |= 1L;
p->pData[p->Length - 1] &= ~((-1L) << t1);
p->pData[p->Length - 1] |= (1L << t1);
- }
- while(SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME);
+ } while (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME);
SDRM_I2OSP(p, nPrimeLen, pPrime);
@@ -114,15 +106,13 @@ int SDRM_GenerateDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsign
SDRM_OS2BN(pGenerator, nPrimeLen, g);
if (ctx->p != NULL)
- {
free(ctx->p);
- }
+
ctx->p = p;
if (ctx->g != NULL)
- {
free(ctx->g);
- }
+
ctx->g = g;
ctx->PrimeLen = nPrimeLen;
@@ -131,38 +121,37 @@ int SDRM_GenerateDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsign
}
/**
- * @fn SDRM_SetDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned int pGenerator)
- * @brief set parameters for Diffie-Hellman protocol
+ * @fn SDRM_SetDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned int pGenerator)
+ * @brief set parameters for Diffie-Hellman protocol
*
- * @param [out] crt context
- * @param [in] pPrime prime number
- * @param [in] nPrimeLen size of pPrime buffer
- * @param [in] pGenerator generator value
+ * @param [out] crt context
+ * @param [in] pPrime prime number
+ * @param [in] nPrimeLen size of pPrime buffer
+ * @param [in] pGenerator generator value
*
- * @return int
+ * @return int
*/
-int SDRM_SetDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned char* pGenerator, unsigned int nGeneratorLen)
+int SDRM_SetDHParam(CryptoCoreContainer *crt, unsigned char *pPrime,
+ unsigned int nPrimeLen, unsigned char *pGenerator, unsigned int nGeneratorLen)
{
- SDRM_DHContext* ctx;
- SDRM_BIG_NUM* p = NULL;
- SDRM_BIG_NUM* g = NULL;
+ SDRM_DHContext *ctx;
+ SDRM_BIG_NUM *p = NULL;
+ SDRM_BIG_NUM *g = NULL;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || (pPrime == NULL) || (pGenerator == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) ||
+ (pPrime == NULL) || (pGenerator == NULL))
return CRYPTO_NULL_POINTER;
- }
ctx = crt->ctx->dhctx;
p = SDRM_BN_Init(nPrimeLen / 2 + 1);
+
if (p == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
g = SDRM_BN_Init(nPrimeLen / 2 + 1);
- if (g == NULL)
- {
+
+ if (g == NULL) {
free(p);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
@@ -178,56 +167,54 @@ int SDRM_SetDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned in
}
/**
- * @fn SDRM_GenerateDHPrivate(CryptoCoreContainer* crt, unsigned char* pPub)
- * @brief generate private value and calculate public value
+ * @fn SDRM_GenerateDHPrivate(CryptoCoreContainer* crt, unsigned char* pPub)
+ * @brief generate private value and calculate public value
*
- * @param [in] crt context
- * @param [out] pPriv private value
- * @param [out] pPub public value
+ * @param [in] crt context
+ * @param [out] pPriv private value
+ * @param [out] pPub public value
*
- * @return int
+ * @return int
*/
-int SDRM_GenerateDHPrivate(CryptoCoreContainer* crt, unsigned char* pPriv, unsigned char* pPub)
+int SDRM_GenerateDHPrivate(CryptoCoreContainer *crt, unsigned char *pPriv,
+ unsigned char *pPub)
{
- SDRM_DHContext* ctx;
+ SDRM_DHContext *ctx;
cc_u32 Seed[4] = {0,};
int retVal;
- SDRM_BIG_NUM* priv = NULL;
- SDRM_BIG_NUM* pub = NULL;
+ SDRM_BIG_NUM *priv = NULL;
+ SDRM_BIG_NUM *pub = NULL;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || (pPriv == NULL) || (pPub == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) ||
+ (pPriv == NULL) || (pPub == NULL))
return CRYPTO_NULL_POINTER;
- }
ctx = crt->ctx->dhctx;
priv = SDRM_BN_Init(ctx->PrimeLen / 2 + 1);
+
if (priv == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
pub = SDRM_BN_Init(ctx->PrimeLen / 2 + 1);
- if (pub == NULL)
- {
+
+ if (pub == NULL) {
free(priv);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
//generate priv
priv->Length = (ctx->PrimeLen + 3) / 4;
- SDRM_RNG_X931((cc_u8 *)Seed, ctx->PrimeLen * 8, (cc_u8*)priv->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, ctx->PrimeLen * 8, (cc_u8 *)priv->pData);
SDRM_BN_ModRed(priv, priv, ctx->p);
#ifndef _OP64_NOTSUPPORTED
retVal = SDRM_BN_ModExp2(pub, ctx->g, priv, ctx->p);
#else
retVal = SDRM_BN_ModExp(pub, ctx->g, priv, ctx->p);
-#endif //_OP64_NOTSUPPORTED
+#endif //_OP64_NOTSUPPORTED
- if (retVal != CRYPTO_SUCCESS)
- {
+ if (retVal != CRYPTO_SUCCESS) {
free(priv);
free(pub);
@@ -244,47 +231,46 @@ int SDRM_GenerateDHPrivate(CryptoCoreContainer* crt, unsigned char* pPriv, unsig
}
/**
- * @fn SDRM_GetDHSharedSecret(CryptoCoreContainer* crt, unsigned char* pPriv, unsigned char* pPub, unsigned char* pSharedSecret)
- * @brief calculate shared secret
+ * @fn SDRM_GetDHSharedSecret(CryptoCoreContainer* crt, unsigned char* pPriv, unsigned char* pPub, unsigned char* pSharedSecret)
+ * @brief calculate shared secret
*
- * @param [in] crt context
- * @param [in] Priv private value
- * @param [in] pPub guest's public value
- * @param [out] pSharedSecret public value
+ * @param [in] crt context
+ * @param [in] Priv private value
+ * @param [in] pPub guest's public value
+ * @param [out] pSharedSecret public value
*
- * @return int
+ * @return int
*/
-int SDRM_GetDHSharedSecret(CryptoCoreContainer* crt, unsigned char* pPriv, unsigned char* pPub, unsigned char* pSharedSecret)
+int SDRM_GetDHSharedSecret(CryptoCoreContainer *crt, unsigned char *pPriv,
+ unsigned char *pPub, unsigned char *pSharedSecret)
{
- SDRM_DHContext* ctx;
- SDRM_BIG_NUM* priv = NULL;
- SDRM_BIG_NUM* pub = NULL;
- SDRM_BIG_NUM* SharedSecret = NULL;
+ SDRM_DHContext *ctx;
+ SDRM_BIG_NUM *priv = NULL;
+ SDRM_BIG_NUM *pub = NULL;
+ SDRM_BIG_NUM *SharedSecret = NULL;
int retVal;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || (pPriv == NULL) || (pPub == NULL) || (pSharedSecret == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) ||
+ (pPriv == NULL) || (pPub == NULL) || (pSharedSecret == NULL))
return CRYPTO_NULL_POINTER;
- }
ctx = crt->ctx->dhctx;
priv = SDRM_BN_Init(ctx->PrimeLen / 2 + 1);
+
if (priv == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
pub = SDRM_BN_Init(ctx->PrimeLen / 2 + 1);
- if (pub == NULL)
- {
+
+ if (pub == NULL) {
free(priv);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
SharedSecret = SDRM_BN_Init(ctx->PrimeLen / 2 + 1);
- if (SharedSecret == NULL)
- {
+
+ if (SharedSecret == NULL) {
free(priv);
free(pub);
return CRYPTO_MEMORY_ALLOC_FAIL;
@@ -297,10 +283,9 @@ int SDRM_GetDHSharedSecret(CryptoCoreContainer* crt, unsigned char* pPriv, unsig
retVal = SDRM_BN_ModExp2(SharedSecret, pub, priv, ctx->p);
#else
retVal = SDRM_BN_ModExp(SharedSecret, pub, priv, ctx->p);
-#endif //_OP64_NOTSUPPORTED
+#endif //_OP64_NOTSUPPORTED
- if (retVal != CRYPTO_SUCCESS)
- {
+ if (retVal != CRYPTO_SUCCESS) {
free(priv);
free(pub);
free(SharedSecret);
@@ -318,24 +303,19 @@ int SDRM_GetDHSharedSecret(CryptoCoreContainer* crt, unsigned char* pPriv, unsig
}
/**
- * @fn SDRM_FreeDHContext(CryptoCoreContainer* crt)
- * @brief free context buffer
+ * @fn SDRM_FreeDHContext(CryptoCoreContainer* crt)
+ * @brief free context buffer
*
- * @param [in] crt context
+ * @param [in] crt context
*/
-void SDRM_FreeDHContext(SDRM_DHContext* ctx)
+void SDRM_FreeDHContext(SDRM_DHContext *ctx)
{
- if (ctx != NULL)
- {
+ if (ctx != NULL) {
if (ctx->p != NULL)
- {
free(ctx->p);
- }
if (ctx->g != NULL)
- {
free(ctx->g);
- }
memset(ctx, 0x00, sizeof(SDRM_DHContext));
}
diff --git a/ssflib/dep/cryptocore/source/middle/cc_dsa.c b/ssflib/dep/cryptocore/source/middle/cc_dsa.c
index b8bb689..da53040 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_dsa.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_dsa.c
@@ -33,37 +33,33 @@
// Functions
////////////////////////////////////////////////////////////////////////////
/*
- * @fn SDRM_Add_DW2BA
- * @brief Add a UINT32 value to a Byte Array
- * function works correctly only when dLen >= 4
+ * @fn SDRM_Add_DW2BA
+ * @brief Add a UINT32 value to a Byte Array
+ * function works correctly only when dLen >= 4
*
- * @param BA [i/o]byte array
- * @param dLen [in]byte-length of BA
- * @param val [in]value to add
+ * @param BA [i/o]byte array
+ * @param dLen [in]byte-length of BA
+ * @param val [in]value to add
*
- * @return void
+ * @return void
*/
-void SDRM_Add_DW2BA(cc_u8* BA, cc_u32 dLen, cc_u32 val)
+void SDRM_Add_DW2BA(cc_u8 *BA, cc_u32 dLen, cc_u32 val)
{
cc_u32 i, DIGIT = 0;
- if (dLen >= 4)
- {
- DIGIT = BA[dLen - 4] ^ (BA[dLen - 3] << 8) ^ (BA[dLen - 2] << 16) ^ (BA[dLen - 1] << 24);
+ if (dLen >= 4) {
+ DIGIT = BA[dLen - 4] ^ (BA[dLen - 3] << 8) ^ (BA[dLen - 2] << 16) ^
+ (BA[dLen - 1] << 24);
DIGIT += val;
- BA[dLen - 4] = (cc_u8)(DIGIT ) & 0xff;
- BA[dLen - 3] = (cc_u8)(DIGIT >> 8 ) & 0xff;
+ BA[dLen - 4] = (cc_u8)(DIGIT) & 0xff;
+ BA[dLen - 3] = (cc_u8)(DIGIT >> 8) & 0xff;
BA[dLen - 2] = (cc_u8)(DIGIT >> 16) & 0xff;
BA[dLen - 1] = (cc_u8)(DIGIT >> 24) & 0xff;
- if (DIGIT < val)
- {
- for (i = dLen - 5; i != (cc_u32)-1; i--)
- {
+ if (DIGIT < val) {
+ for (i = dLen - 5; i != (cc_u32) - 1; i--) {
if (++BA[i] != 0)
- {
return;
- }
}
}
}
@@ -72,61 +68,64 @@ void SDRM_Add_DW2BA(cc_u8* BA, cc_u32 dLen, cc_u32 val)
}
/*
- * @fn SDRM_DSA_InitCrt
- * @brief generate DSA Context
+ * @fn SDRM_DSA_InitCrt
+ * @brief generate DSA Context
*
- * @return pointer to the generated context
- * \n NULL if memory allocation is failed
+ * @return pointer to the generated context
+ * \n NULL if memory allocation is failed
*/
SDRM_DSAContext *SDRM_DSA_InitCrt()
{
SDRM_DSAContext *ctx;
- cc_u8 *pbBuf = (cc_u8*)malloc(sizeof(SDRM_DSAContext) + SDRM_DSA_ALLOC_SIZE * 5);
+ cc_u8 *pbBuf = (cc_u8 *)malloc(sizeof(SDRM_DSAContext) +
+ SDRM_DSA_ALLOC_SIZE * 5);
if (pbBuf == NULL)
- {
return NULL;
- }
- ctx = (SDRM_DSAContext*)(void*)pbBuf;
- ctx->p = SDRM_BN_Alloc((cc_u8*)ctx + sizeof(SDRM_DSAContext), SDRM_DSA_BN_BUFSIZE);
- ctx->q = SDRM_BN_Alloc((cc_u8*)ctx->p + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- ctx->al = SDRM_BN_Alloc((cc_u8*)ctx->q + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- ctx->y = SDRM_BN_Alloc((cc_u8*)ctx->al + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- ctx->a = SDRM_BN_Alloc((cc_u8*)ctx->y + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
+ ctx = (SDRM_DSAContext *)(void *)pbBuf;
+ ctx->p = SDRM_BN_Alloc((cc_u8 *)ctx + sizeof(SDRM_DSAContext),
+ SDRM_DSA_BN_BUFSIZE);
+ ctx->q = SDRM_BN_Alloc((cc_u8 *)ctx->p + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ ctx->al = SDRM_BN_Alloc((cc_u8 *)ctx->q + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ ctx->y = SDRM_BN_Alloc((cc_u8 *)ctx->al + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ ctx->a = SDRM_BN_Alloc((cc_u8 *)ctx->y + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
return ctx;
}
/*
- * @fn int SDRM_DSA_SetParam(CryptoCoreContainer *crt,
- cc_u8 *DSA_P_Data, cc_u32 DSA_P_Len,
- cc_u8 *DSA_Q_Data, cc_u32 DSA_Q_Len,
- cc_u8 *DSA_G_Data, cc_u32 DSA_G_Len)
- * @brief set DSA parameters
+ * @fn int SDRM_DSA_SetParam(CryptoCoreContainer *crt,
+ cc_u8 *DSA_P_Data, cc_u32 DSA_P_Len,
+ cc_u8 *DSA_Q_Data, cc_u32 DSA_Q_Len,
+ cc_u8 *DSA_G_Data, cc_u32 DSA_G_Len)
+ * @brief set DSA parameters
*
- * @param crt [out]dsa context
- * @param DSA_P_Data [in]octet string of p value
- * @param DSA_P_Len [in]legnth of p_val
- * @param DSA_Q_Data [in]octet string of q value
- * @param DSA_Q_Len [in]legnth of q_val
- * @param DSA_G_Data [in]octet string of al value
- * @param DSA_G_Len [in]legnth of al_val
+ * @param crt [out]dsa context
+ * @param DSA_P_Data [in]octet string of p value
+ * @param DSA_P_Len [in]legnth of p_val
+ * @param DSA_Q_Data [in]octet string of q value
+ * @param DSA_Q_Len [in]legnth of q_val
+ * @param DSA_G_Data [in]octet string of al value
+ * @param DSA_G_Len [in]legnth of al_val
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if input parameter pointer is null
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
- * \n CRYPTO_ERROR if conversion is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if input parameter pointer is null
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * \n CRYPTO_ERROR if conversion is failed
*/
int SDRM_DSA_SetParam(CryptoCoreContainer *crt,
- cc_u8 *DSA_P_Data, cc_u32 DSA_P_Len,
- cc_u8 *DSA_Q_Data, cc_u32 DSA_Q_Len,
- cc_u8 *DSA_G_Data, cc_u32 DSA_G_Len)
+ cc_u8 *DSA_P_Data, cc_u32 DSA_P_Len,
+ cc_u8 *DSA_Q_Data, cc_u32 DSA_Q_Len,
+ cc_u8 *DSA_G_Data, cc_u32 DSA_G_Len)
{
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) || (DSA_P_Data == NULL) || (DSA_Q_Data == NULL) || (DSA_G_Data == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) ||
+ (DSA_P_Data == NULL) || (DSA_Q_Data == NULL) || (DSA_G_Data == NULL))
return CRYPTO_NULL_POINTER;
- }
SDRM_OS2BN(DSA_P_Data, DSA_P_Len, crt->ctx->dsactx->p);
SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->dsactx->p);
@@ -142,39 +141,35 @@ int SDRM_DSA_SetParam(CryptoCoreContainer *crt,
/*
- * @fn int SDRM_DSA_SetKeyPair(CryptoCoreContainer *crt,
- cc_u8 *DSA_Y_Data, cc_u32 DSA_Y_Len,
- cc_u8 *DSA_X_Data, cc_u32 DSA_X_Len)
- * @brief set DSA parameters
+ * @fn int SDRM_DSA_SetKeyPair(CryptoCoreContainer *crt,
+ cc_u8 *DSA_Y_Data, cc_u32 DSA_Y_Len,
+ cc_u8 *DSA_X_Data, cc_u32 DSA_X_Len)
+ * @brief set DSA parameters
*
- * @param crt [out]dsa context
- * @param DSA_Y_Data [in]octet string of y value
- * @param DSA_Y_Len [in]legnth of y_val
- * @param DSA_X_Data [in]octet string of a value
- * @param DSA_X_Len [in]legnth of a_val
+ * @param crt [out]dsa context
+ * @param DSA_Y_Data [in]octet string of y value
+ * @param DSA_Y_Len [in]legnth of y_val
+ * @param DSA_X_Data [in]octet string of a value
+ * @param DSA_X_Len [in]legnth of a_val
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if input parameter pointer is null
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
- * \n CRYPTO_ERROR if conversion is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if input parameter pointer is null
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * \n CRYPTO_ERROR if conversion is failed
*/
int SDRM_DSA_SetKeyPair(CryptoCoreContainer *crt,
- cc_u8 *DSA_Y_Data, cc_u32 DSA_Y_Len,
- cc_u8 *DSA_X_Data, cc_u32 DSA_X_Len)
+ cc_u8 *DSA_Y_Data, cc_u32 DSA_Y_Len,
+ cc_u8 *DSA_X_Data, cc_u32 DSA_X_Len)
{
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
- if (DSA_Y_Data != NULL)
- {
+ if (DSA_Y_Data != NULL) {
SDRM_OS2BN(DSA_Y_Data, DSA_Y_Len, crt->ctx->dsactx->y);
SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->dsactx->y);
}
- if (DSA_X_Data != NULL)
- {
+ if (DSA_X_Data != NULL) {
SDRM_OS2BN(DSA_X_Data, DSA_X_Len, crt->ctx->dsactx->a);
SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->dsactx->a);
}
@@ -183,76 +178,69 @@ int SDRM_DSA_SetKeyPair(CryptoCoreContainer *crt,
}
/*
- * @fn int SDRM_DSA_GenParam(CryptoCoreContainer *crt, cc_u32 T_Siz,
- cc_u8 *DSA_P_Data, cc_u32 *DSA_P_Len,
- cc_u8 *DSA_Q_Data, cc_u32 *DSA_Q_Len,
- cc_u8 *DSA_G_Data, cc_u32 *DSA_G_Len)
- * @brief generate and set DSA parameters
+ * @fn int SDRM_DSA_GenParam(CryptoCoreContainer *crt, cc_u32 T_Siz,
+ cc_u8 *DSA_P_Data, cc_u32 *DSA_P_Len,
+ cc_u8 *DSA_Q_Data, cc_u32 *DSA_Q_Len,
+ cc_u8 *DSA_G_Data, cc_u32 *DSA_G_Len)
+ * @brief generate and set DSA parameters
*
- * @param crt [out]dsa context
- * @param T_Siz [in]fix the length of p to 512 + 64t bit (0 <= T_Siz <= 8)
- * @param DSA_P_Data [out]octet string of p value
- * @param DSA_P_Len [out]legnth of p_val
- * @param DSA_Q_Data [out]octet string of q value
- * @param DSA_Q_Len [out]legnth of q_val
- * @param DSA_G_Data [out]octet string of al value
- * @param DSA_G_Len [out]legnth of al_val
+ * @param crt [out]dsa context
+ * @param T_Siz [in]fix the length of p to 512 + 64t bit (0 <= T_Siz <= 8)
+ * @param DSA_P_Data [out]octet string of p value
+ * @param DSA_P_Len [out]legnth of p_val
+ * @param DSA_Q_Data [out]octet string of q value
+ * @param DSA_Q_Len [out]legnth of q_val
+ * @param DSA_G_Data [out]octet string of al value
+ * @param DSA_G_Len [out]legnth of al_val
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if input parameter pointer is null
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
- * \n CRYPTO_ERROR if conversion is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if input parameter pointer is null
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * \n CRYPTO_ERROR if conversion is failed
*/
int SDRM_DSA_GenParam(CryptoCoreContainer *crt, cc_u32 T_Siz,
- cc_u8 *DSA_P_Data, cc_u32 *DSA_P_Len,
- cc_u8 *DSA_Q_Data, cc_u32 *DSA_Q_Len,
- cc_u8 *DSA_G_Data, cc_u32 *DSA_G_Len)
+ cc_u8 *DSA_P_Data, cc_u32 *DSA_P_Len,
+ cc_u8 *DSA_Q_Data, cc_u32 *DSA_Q_Len,
+ cc_u8 *DSA_G_Data, cc_u32 *DSA_G_Len)
{
- cc_u32 i, k, L, n/*, g*/;
- cc_u8 pbTemp[260], pbSeed[64];
+ cc_u32 i, k, L, n/*, g*/;
+ cc_u8 pbTemp[260], pbSeed[64];
SDRM_SHA1Context ctx;
- SDRM_BIG_NUM /**BN_A, */*BN_G, *BN_P, *BN_Q, *BN_AL, *BN_Temp;
- cc_u8 *pbBuf = NULL;
+ SDRM_BIG_NUM /**BN_A, */*BN_G, *BN_P, *BN_Q, *BN_AL, *BN_Temp;
+ cc_u8 *pbBuf = NULL;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
if (T_Siz > 8)
- {
return CRYPTO_INVALID_ARGUMENT;
- }
L = 512 + 64 * T_Siz;
n = (L - 1) / 160;
-// g = (L - 1) % 160;
+ // g = (L - 1) % 160;
+
+ pbBuf = (cc_u8 *)malloc(SDRM_DSA_ALLOC_SIZE * 2);
- pbBuf = (cc_u8*)malloc(SDRM_DSA_ALLOC_SIZE * 2);
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_G = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_DSA_BN_BUFSIZE);
- BN_Temp = SDRM_BN_Alloc((cc_u8*)BN_G + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
+ BN_G = SDRM_BN_Alloc((cc_u8 *)pbBuf, SDRM_DSA_BN_BUFSIZE);
+ BN_Temp = SDRM_BN_Alloc((cc_u8 *)BN_G + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
BN_P = crt->ctx->dsactx->p;
BN_Q = crt->ctx->dsactx->q;
BN_AL = crt->ctx->dsactx->al;
-// BN_A = crt->ctx->dsactx->a;
+ // BN_A = crt->ctx->dsactx->a;
//generate p and q
- while(1)
- {
- do
- {
+ while (1) {
+ do {
//choose a random seed s of bitlength g >= 160
for (i = 0; i < SDRM_SHA1_BLOCK_SIZ; i++)
- {
pbSeed[i] = (rand() << 16) ^ rand();
- }
+
pbSeed[0] |= 0x80;
pbSeed[SDRM_SHA1_BLOCK_SIZ - 1] |= 0x01;
@@ -267,24 +255,21 @@ int SDRM_DSA_GenParam(CryptoCoreContainer *crt, cc_u32 T_Siz,
SDRM_SHA1_Update(&ctx, pbSeed, SDRM_SHA1_BLOCK_SIZ);
SDRM_SHA1_Final(&ctx, pbTemp + SDRM_SHA1_BLOCK_SIZ);
- for (i = 0; i < SDRM_SHA1_BLOCK_SIZ / sizeof(cc_u32); i++)
- {
- ((cc_u32*)(void*)pbTemp)[i] ^= ((cc_u32*)(void*)pbTemp)[i + SDRM_SHA1_BLOCK_SIZ / sizeof(cc_u32)];
+ for (i = 0; i < SDRM_SHA1_BLOCK_SIZ / sizeof(cc_u32); i++) {
+ ((cc_u32 *)(void *)pbTemp)[i] ^= ((cc_u32 *)(void *)
+ pbTemp)[i + SDRM_SHA1_BLOCK_SIZ / sizeof(cc_u32)];
}
pbTemp[0] |= 0x80;
pbTemp[SDRM_SHA1_BLOCK_SIZ - 1] |= 0x01;
SDRM_OS2BN(pbTemp, SDRM_SHA1_BLOCK_SIZ, BN_Q);
- }
- while(SDRM_BN_MILLER_RABIN(BN_Q, 18) != CRYPTO_SUCCESS);
+ } while (SDRM_BN_MILLER_RABIN(BN_Q, 18) != CRYPTO_SUCCESS);
SDRM_INC_BA(pbSeed, SDRM_SHA1_BLOCK_SIZ);
- for (i = 0; i < 4096; i++)
- {
- for (k = 0; k <= n; k++)
- {
+ for (i = 0; i < 4096; i++) {
+ for (k = 0; k <= n; k++) {
SDRM_SHA1_Init(&ctx);
SDRM_SHA1_Update(&ctx, pbSeed, SDRM_SHA1_BLOCK_SIZ);
SDRM_SHA1_Final(&ctx, pbTemp + (n - k) * SDRM_SHA1_BLOCK_SIZ);
@@ -300,21 +285,17 @@ int SDRM_DSA_GenParam(CryptoCoreContainer *crt, cc_u32 T_Siz,
SDRM_BN_Sub(BN_P, BN_P, BN_Temp);
SDRM_BN_Add(BN_P, BN_P, BN_One);
- if (SDRM_CheckBitUINT32(BN_P->pData, L - 1))
- {
+ if (SDRM_CheckBitUINT32(BN_P->pData, L - 1)) {
if (SDRM_BN_MILLER_RABIN(BN_P, 5) == CRYPTO_ISPRIME)
- {
goto SUCCESS;
- }
+
else
- {
break;
- }
}
}
}
-SUCCESS :
+SUCCESS:
//select a generator al(alpha) of the unique cyclic group of order q in Zp
SDRM_BN_Clr(BN_Temp);
//temp = (p-1)/q
@@ -324,47 +305,33 @@ SUCCESS :
do {
//select an element g excluded in Zp*
do {
- SDRM_RNG_X931(pbSeed, L, (cc_u8*)BN_G->pData);
+ SDRM_RNG_X931(pbSeed, L, (cc_u8 *)BN_G->pData);
BN_G->Length = L / 32 + 1;
SDRM_BN_OPTIMIZE_LENGTH(BN_G);
- }
- while(SDRM_BN_Cmp(BN_G, BN_P) >= 0);
+ } while (SDRM_BN_Cmp(BN_G, BN_P) >= 0);
//al(alpha) = g^temp mod p
SDRM_BN_ModExp(BN_AL, BN_G, BN_Temp, BN_P);
- }
- while (SDRM_BN_Cmp(BN_AL, BN_One) == 0);
+ } while (SDRM_BN_Cmp(BN_AL, BN_One) == 0);
//write output
if (DSA_P_Data != NULL)
- {
SDRM_I2OSP(BN_P, L / 8, DSA_P_Data);
- }
if (DSA_P_Len != NULL)
- {
*DSA_P_Len = L / 8;
- }
if (DSA_Q_Data != NULL)
- {
SDRM_I2OSP(BN_Q, 20, DSA_Q_Data);
- }
if (DSA_Q_Len != NULL)
- {
*DSA_Q_Len = 20;
- }
if (DSA_G_Data != NULL)
- {
SDRM_I2OSP(BN_AL, BN_AL->Length * 4, DSA_G_Data);
- }
if (DSA_G_Len != NULL)
- {
*DSA_G_Len = BN_AL->Length * 4;
- }
free(pbBuf);
@@ -372,116 +339,109 @@ SUCCESS :
}
/*
- * @fn int SDRM_DSA_GenKeypair(CryptoCoreContainer *crt,
- cc_u8 *DSA_Y_Data, cc_u32 *DSA_Y_Len,
- cc_u8 *DSA_X_Data, cc_u32 *DSA_X_Len)
- * @brief generate and set DSA parameters
+ * @fn int SDRM_DSA_GenKeypair(CryptoCoreContainer *crt,
+ cc_u8 *DSA_Y_Data, cc_u32 *DSA_Y_Len,
+ cc_u8 *DSA_X_Data, cc_u32 *DSA_X_Len)
+ * @brief generate and set DSA parameters
*
- * @param crt [out]dsa context
- * @param DSA_Y_Data [out]octet string of y value
- * @param DSA_Y_Len [out]legnth of y_val
- * @param DSA_X_Data [out]octet string of a value
- * @param DSA_X_Len [out]legnth of a_val
+ * @param crt [out]dsa context
+ * @param DSA_Y_Data [out]octet string of y value
+ * @param DSA_Y_Len [out]legnth of y_val
+ * @param DSA_X_Data [out]octet string of a value
+ * @param DSA_X_Len [out]legnth of a_val
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if input parameter pointer is null
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
- * \n CRYPTO_ERROR if conversion is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if input parameter pointer is null
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * \n CRYPTO_ERROR if conversion is failed
*/
int SDRM_DSA_GenKeypair(CryptoCoreContainer *crt,
- cc_u8 *DSA_Y_Data, cc_u32 *DSA_Y_Len,
- cc_u8 *DSA_X_Data, cc_u32 *DSA_X_Len)
+ cc_u8 *DSA_Y_Data, cc_u32 *DSA_Y_Len,
+ cc_u8 *DSA_X_Data, cc_u32 *DSA_X_Len)
{
SDRM_BIG_NUM *BN_A;
- cc_u32 Seed[4], i;
+ cc_u32 Seed[4], i;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
BN_A = crt->ctx->dsactx->a;
for (i = 0; i < 4; i++)
- {
Seed[i] = (rand() << 16) ^ rand();
- }
//Select a random integer a such that 1 <= a <= q-1
do {
- SDRM_RNG_X931((cc_u8*)Seed, 160, (cc_u8*)BN_A->pData);
- BN_A->Length = 6; //6 = 160 / 32 + 1
+ SDRM_RNG_X931((cc_u8 *)Seed, 160, (cc_u8 *)BN_A->pData);
+ BN_A->Length = 6; //6 = 160 / 32 + 1
SDRM_BN_OPTIMIZE_LENGTH(BN_A);
- }
- while(SDRM_BN_Cmp(BN_A, crt->ctx->dsactx->q) >= 0);
+ } while (SDRM_BN_Cmp(BN_A, crt->ctx->dsactx->q) >= 0);
//y = al ^ a mod p
- SDRM_BN_ModExp(crt->ctx->dsactx->y, crt->ctx->dsactx->al, BN_A, crt->ctx->dsactx->p);
+ SDRM_BN_ModExp(crt->ctx->dsactx->y, crt->ctx->dsactx->al, BN_A,
+ crt->ctx->dsactx->p);
//write output
if (DSA_Y_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->dsactx->y, crt->ctx->dsactx->y->Length * 4, DSA_Y_Data);
- }
if (DSA_Y_Len != NULL)
- {
*DSA_Y_Len = crt->ctx->dsactx->y->Length * 4;
- }
if (DSA_X_Data != NULL)
- {
SDRM_I2OSP(BN_A, BN_A->Length * 4, DSA_X_Data);
- }
if (DSA_X_Len != NULL)
- {
*DSA_X_Len = BN_A->Length * 4;
- }
return CRYPTO_SUCCESS;
}
/*
- * @fn int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen)
- * @brief generate signature for given value
+ * @fn int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen)
+ * @brief generate signature for given value
*
- * @param crt [in]crypto env structure
- * @param hash [in]hash value
- * @param hashLen [in]byte-length of hash
- * @param signature [out]generated signature
- * @param signLen [out]byte-length of signature
+ * @param crt [in]crypto env structure
+ * @param hash [in]hash value
+ * @param hashLen [in]byte-length of hash
+ * @param signature [out]generated signature
+ * @param signLen [out]byte-length of signature
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
*/
-int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen)
+int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen,
+ cc_u8 *signature, cc_u32 *signLen)
{
- cc_u8 pbSeed[16] = {0};
+ cc_u8 pbSeed[16] = {0};
SDRM_BIG_NUM *BN_P, *BN_Q, *BN_AL, *BN_A;
SDRM_BIG_NUM *BN_r, *BN_s, *BN_k, *BN_hash, *BN_ar, *temp1, *temp2;
- cc_u8* pbBuf = NULL;
+ cc_u8 *pbBuf = NULL;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) || (crt->ctx->dsactx->a == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) ||
+ (crt->ctx->dsactx->a == NULL))
return CRYPTO_NULL_POINTER;
- }
- pbBuf = (cc_u8*)malloc(SDRM_DSA_ALLOC_SIZE * 7);
+ pbBuf = (cc_u8 *)malloc(SDRM_DSA_ALLOC_SIZE * 7);
+
if (!pbBuf)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_r = SDRM_BN_Alloc( pbBuf, SDRM_DSA_BN_BUFSIZE);
- BN_s = SDRM_BN_Alloc((cc_u8*)BN_r + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- BN_k = SDRM_BN_Alloc((cc_u8*)BN_s + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- BN_hash = SDRM_BN_Alloc((cc_u8*)BN_k + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- BN_ar = SDRM_BN_Alloc((cc_u8*)BN_hash + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- temp1 = SDRM_BN_Alloc((cc_u8*)BN_ar + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
+ BN_r = SDRM_BN_Alloc(pbBuf, SDRM_DSA_BN_BUFSIZE);
+ BN_s = SDRM_BN_Alloc((cc_u8 *)BN_r + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ BN_k = SDRM_BN_Alloc((cc_u8 *)BN_s + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ BN_hash = SDRM_BN_Alloc((cc_u8 *)BN_k + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ BN_ar = SDRM_BN_Alloc((cc_u8 *)BN_hash + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ temp1 = SDRM_BN_Alloc((cc_u8 *)BN_ar + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
BN_P = crt->ctx->dsactx->p;
BN_Q = crt->ctx->dsactx->q;
@@ -490,11 +450,10 @@ int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *
//select a random secret integer k, 0 < k < q
do {
- SDRM_RNG_X931(pbSeed, 160, (cc_u8*)BN_k->pData);
- BN_k->Length = 6; //6 = 160 / 32 + 1
+ SDRM_RNG_X931(pbSeed, 160, (cc_u8 *)BN_k->pData);
+ BN_k->Length = 6; //6 = 160 / 32 + 1
SDRM_BN_OPTIMIZE_LENGTH(BN_k);
- }
- while(SDRM_BN_Cmp(BN_k, BN_Q) > 0);
+ } while (SDRM_BN_Cmp(BN_k, BN_Q) > 0);
SDRM_BN_ModExp(temp1, BN_AL, BN_k, BN_P);
//r = (al ^ k mod p) mod q
@@ -502,7 +461,7 @@ int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *
SDRM_BN_ModInv(temp1, BN_k, BN_Q);
- SDRM_OS2BN((cc_u8*)hash, hashLen, BN_hash);
+ SDRM_OS2BN((cc_u8 *)hash, hashLen, BN_hash);
SDRM_BN_Mul(BN_ar, BN_A, BN_r);
SDRM_BN_Add(temp2, BN_hash, BN_ar);
@@ -515,101 +474,110 @@ int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *
SDRM_I2OSP(BN_s, 20, signature + 20);
if (signLen != NULL)
- {
*signLen = 40;
- }
free(pbBuf);
return CRYPTO_SUCCESS;
- }
+}
/*
- * @fn int SDRM_DSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result)
- * @brief generate signature for given value
+ * @fn int SDRM_DSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result)
+ * @brief generate signature for given value
*
- * @param crt [in]crypto env structure
- * @param hash [in]hash value
- * @param hashLen [in]byte-length of hash
- * @param signature [in]signature
- * @param signLen [in]byte-length of signature
- * @param result [in]result of veryfing signature
+ * @param crt [in]crypto env structure
+ * @param hash [in]hash value
+ * @param hashLen [in]byte-length of hash
+ * @param signature [in]signature
+ * @param signLen [in]byte-length of signature
+ * @param result [in]result of veryfing signature
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid
*/
-int SDRM_DSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result)
+int SDRM_DSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen,
+ cc_u8 *signature, cc_u32 signLen, int *result)
{
SDRM_BIG_NUM *w, *u1, *u2, *v, *BNH_m, *BN_r, *BN_s;
SDRM_BIG_NUM *temp1, *temp2, *temp3;
- cc_u8 *pbBuf = NULL;
+ cc_u8 *pbBuf = NULL;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) || (crt->ctx->dsactx->y == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) ||
+ (crt->ctx->dsactx->y == NULL))
return CRYPTO_NULL_POINTER;
- }
- pbBuf = (cc_u8*)malloc(SDRM_DSA_ALLOC_SIZE * 10);
+ pbBuf = (cc_u8 *)malloc(SDRM_DSA_ALLOC_SIZE * 10);
+
if (!pbBuf)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- w = SDRM_BN_Alloc( pbBuf, SDRM_DSA_BN_BUFSIZE);
- u1 = SDRM_BN_Alloc((cc_u8*)w + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- u2 = SDRM_BN_Alloc((cc_u8*)u1 + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- v = SDRM_BN_Alloc((cc_u8*)u2 + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- BNH_m = SDRM_BN_Alloc((cc_u8*)v + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- BN_r = SDRM_BN_Alloc((cc_u8*)BNH_m + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- BN_s = SDRM_BN_Alloc((cc_u8*)BN_r + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- temp1 = SDRM_BN_Alloc((cc_u8*)BN_s + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
- temp3 = SDRM_BN_Alloc((cc_u8*)temp2 + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE);
-
-
- if ((SDRM_BN_Cmp(BN_r, crt->ctx->dsactx->q) >= 0) || (SDRM_BN_Cmp(BN_s, crt->ctx->dsactx->q) >= 0)) //r < q and s < q
- {
+ w = SDRM_BN_Alloc(pbBuf, SDRM_DSA_BN_BUFSIZE);
+ u1 = SDRM_BN_Alloc((cc_u8 *)w + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ u2 = SDRM_BN_Alloc((cc_u8 *)u1 + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ v = SDRM_BN_Alloc((cc_u8 *)u2 + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ BNH_m = SDRM_BN_Alloc((cc_u8 *)v + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ BN_r = SDRM_BN_Alloc((cc_u8 *)BNH_m + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ BN_s = SDRM_BN_Alloc((cc_u8 *)BN_r + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ temp1 = SDRM_BN_Alloc((cc_u8 *)BN_s + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+ temp3 = SDRM_BN_Alloc((cc_u8 *)temp2 + SDRM_DSA_ALLOC_SIZE,
+ SDRM_DSA_BN_BUFSIZE);
+
+
+ if ((SDRM_BN_Cmp(BN_r, crt->ctx->dsactx->q) >= 0) ||
+ (SDRM_BN_Cmp(BN_s, crt->ctx->dsactx->q) >= 0)) { //r < q and s < q
free(pbBuf);
return CRYPTO_ERROR;
}
- if (signLen != 40)
- {
+ if (signLen != 40) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
- SDRM_OS2BN((cc_u8*)signature, 20, BN_r);
- SDRM_OS2BN((cc_u8*)signature + 20, 20, BN_s);
+ SDRM_OS2BN((cc_u8 *)signature, 20, BN_r);
+ SDRM_OS2BN((cc_u8 *)signature + 20, 20, BN_s);
- SDRM_BN_ModInv(w, BN_s, crt->ctx->dsactx->q); //w = s^-1 mod q
- SDRM_OS2BN((cc_u8*)hash, 20, BNH_m);
+ SDRM_BN_ModInv(w, BN_s,
+ crt->ctx->dsactx->q); //w = s^-1 mod q
+ SDRM_OS2BN((cc_u8 *)hash, 20, BNH_m);
- SDRM_BN_ModMul(u1, w, BNH_m, crt->ctx->dsactx->q); //u1 = w x h(m) mod q
- SDRM_BN_ModMul(u2, BN_r, w, crt->ctx->dsactx->q); //u2 = rw mod q
+ SDRM_BN_ModMul(u1, w, BNH_m,
+ crt->ctx->dsactx->q); //u1 = w x h(m) mod q
+ SDRM_BN_ModMul(u2, BN_r, w,
+ crt->ctx->dsactx->q); //u2 = rw mod q
- SDRM_BN_ModExp(temp1, crt->ctx->dsactx->al, u1, crt->ctx->dsactx->p); //temp1 = alpha^u1 mod p
- SDRM_BN_ModExp(temp2, crt->ctx->dsactx->y, u2, crt->ctx->dsactx->p); //temp2 = y^u2 mod p
+ SDRM_BN_ModExp(temp1, crt->ctx->dsactx->al, u1,
+ crt->ctx->dsactx->p); //temp1 = alpha^u1 mod p
+ SDRM_BN_ModExp(temp2, crt->ctx->dsactx->y, u2,
+ crt->ctx->dsactx->p); //temp2 = y^u2 mod p
- SDRM_BN_ModMul(temp3, temp1, temp2, crt->ctx->dsactx->p); //temp3 = (alpha^u1 x y^u2 mod p) mod p
+ SDRM_BN_ModMul(temp3, temp1, temp2,
+ crt->ctx->dsactx->p); //temp3 = (alpha^u1 x y^u2 mod p) mod p
- SDRM_BN_ModRed(v, temp3, crt->ctx->dsactx->q); //v = (alpha^u1 x y^u2 mod p) mod q
+ SDRM_BN_ModRed(v, temp3,
+ crt->ctx->dsactx->q); //v = (alpha^u1 x y^u2 mod p) mod q
-// SDRM_PrintBN("v : ", v);
-// SDRM_PrintBN("Hash : ", BNH_m);
+ // SDRM_PrintBN("v : ", v);
+ // SDRM_PrintBN("Hash : ", BNH_m);
if (SDRM_BN_Cmp(v, BN_r) == 0)
- {
*result = CRYPTO_VALID_SIGN;
- }
+
else
- {
*result = CRYPTO_INVALID_SIGN;
- }
free(pbBuf);
diff --git a/ssflib/dep/cryptocore/source/middle/cc_ecdh.c b/ssflib/dep/cryptocore/source/middle/cc_ecdh.c
index 67ad4fb..764e837 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_ecdh.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_ecdh.c
@@ -32,68 +32,64 @@
// Functions
////////////////////////////////////////////////////////////////////////////
/*
- * @fn SDRM_generateDH1stPhaseKey
- * @brief generate Xk and its Xv
+ * @fn SDRM_generateDH1stPhaseKey
+ * @brief generate Xk and its Xv
*
- * @param crt [in]crypto context
- * @param pchXk [out]Generated Random Number
- * @param pchXv [out]DH 1st phase value
+ * @param crt [in]crypto context
+ * @param pchXk [out]Generated Random Number
+ * @param pchXv [out]DH 1st phase value
*
- * @return CRYPTO_SUCCESS if no error is occured
+ * @return CRYPTO_SUCCESS if no error is occured
*/
-int SDRM_generateDH1stPhaseKey(CryptoCoreContainer *crt, cc_u8 *pchXk, cc_u8 *pchXv)
+int SDRM_generateDH1stPhaseKey(CryptoCoreContainer *crt, cc_u8 *pchXk,
+ cc_u8 *pchXv)
{
cc_u8 Si_ANSI_X9_31[SDRM_X931_SEED_SIZ];
- SDRM_BIG_NUM *BN_Xk, *BN_Temp;
- SDRM_EC_POINT *kP;
- SDRM_ECC_CTX *ctx;
+ SDRM_BIG_NUM *BN_Xk, *BN_Temp;
+ SDRM_EC_POINT *kP;
+ SDRM_ECC_CTX *ctx;
int i;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdhctx == NULL) || (pchXk == NULL) || (pchXv == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdhctx == NULL) ||
+ (pchXk == NULL) || (pchXv == NULL))
return CRYPTO_NULL_POINTER;
- }
ctx = crt->ctx->ecdhctx;
for (i = 0; i < SDRM_X931_SEED_SIZ; i++)
- {
Si_ANSI_X9_31[i] = ((rand() << 16) + rand()) & 0xff;
- }
BN_Temp = SDRM_BN_Init(crt->ctx->ecdsactx->uDimension >> 3);
+
if (BN_Temp == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
BN_Xk = SDRM_BN_Init(crt->ctx->ecdsactx->uDimension >> 3);
- if (BN_Xk == NULL)
- {
+
+ if (BN_Xk == NULL) {
free(BN_Temp);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
SDRM_BN_Sub(BN_Temp, ctx->ECC_n, BN_One);
+
do {
SDRM_RNG_X931(Si_ANSI_X9_31, crt->ctx->ecdsactx->uDimension, pchXk);
SDRM_OS2BN(pchXk, crt->ctx->ecdsactx->uDimension >> 3, BN_Xk);
- }
- while ((SDRM_BN_Cmp(BN_Xk, BN_One) < 0) || (SDRM_BN_Cmp(BN_Xk, BN_Temp) > 0));
+ } while ((SDRM_BN_Cmp(BN_Xk, BN_One) < 0) || (SDRM_BN_Cmp(BN_Xk, BN_Temp) > 0));
kP = SDRM_ECC_Init();
- if (kP == NULL)
- {
+
+ if (kP == NULL) {
free(BN_Temp);
free(BN_Xk);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
- if (SDRM_CTX_EC_kP(ctx, kP, ctx->ECC_G, BN_Xk) == CRYPTO_MEMORY_ALLOC_FAIL)
- {
+ if (SDRM_CTX_EC_kP(ctx, kP, ctx->ECC_G, BN_Xk) == CRYPTO_MEMORY_ALLOC_FAIL) {
free(BN_Temp);
free(BN_Xk);
free(kP);
@@ -102,7 +98,8 @@ int SDRM_generateDH1stPhaseKey(CryptoCoreContainer *crt, cc_u8 *pchXk, cc_u8 *pc
}
SDRM_BN2OS(kP->x, crt->ctx->ecdsactx->uDimension >> 3, pchXv);
- SDRM_BN2OS(kP->y, crt->ctx->ecdsactx->uDimension >> 3, pchXv + (crt->ctx->ecdsactx->uDimension >> 3));
+ SDRM_BN2OS(kP->y, crt->ctx->ecdsactx->uDimension >> 3,
+ pchXv + (crt->ctx->ecdsactx->uDimension >> 3));
free(BN_Temp);
free(BN_Xk);
@@ -112,53 +109,52 @@ int SDRM_generateDH1stPhaseKey(CryptoCoreContainer *crt, cc_u8 *pchXk, cc_u8 *pc
}
/*
- * @fn SDRM_generateDHKey
- * @brief genenrate auth key with Xk and Yv
+ * @fn SDRM_generateDHKey
+ * @brief genenrate auth key with Xk and Yv
*
- * @param crt [in]crypto context
- * @param pchXk [in]Generated Random Number
- * @param pchYv [in]DH 1st phase value
- * @param pchKauth [out]authentication key
+ * @param crt [in]crypto context
+ * @param pchXk [in]Generated Random Number
+ * @param pchYv [in]DH 1st phase value
+ * @param pchKauth [out]authentication key
*
- * @return CRYPTO_SUCCESS if no error is occured
+ * @return CRYPTO_SUCCESS if no error is occured
*/
-int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8* pchXk, cc_u8* pchYv, cc_u8* pchKauth)
+int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8 *pchXk, cc_u8 *pchYv,
+ cc_u8 *pchKauth)
{
- SDRM_BIG_NUM *BN_Xk;
- SDRM_EC_POINT *kP, *EC_Yv;
- SDRM_ECC_CTX *ctx;
+ SDRM_BIG_NUM *BN_Xk;
+ SDRM_EC_POINT *kP, *EC_Yv;
+ SDRM_ECC_CTX *ctx;
int retVal;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdhctx == NULL) || (pchXk == NULL) || (pchYv == NULL) || (pchKauth == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdhctx == NULL) ||
+ (pchXk == NULL) || (pchYv == NULL) || (pchKauth == NULL))
return CRYPTO_NULL_POINTER;
- }
ctx = crt->ctx->ecdhctx;
BN_Xk = SDRM_BN_Init(crt->ctx->ecdsactx->uDimension >> 3);
+
if (BN_Xk == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
retVal = SDRM_OS2BN(pchXk, crt->ctx->ecdsactx->uDimension >> 3, BN_Xk);
- if (retVal != CRYPTO_SUCCESS)
- {
+
+ if (retVal != CRYPTO_SUCCESS) {
free(BN_Xk);
return retVal;
}
kP = SDRM_ECC_Init();
- if (kP == NULL)
- {
+
+ if (kP == NULL) {
free(BN_Xk);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
EC_Yv = SDRM_ECC_Init();
- if (EC_Yv == NULL)
- {
+
+ if (EC_Yv == NULL) {
free(BN_Xk);
free(kP);
@@ -167,8 +163,8 @@ int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8* pchXk, cc_u8* pchYv, cc_
SDRM_EC_CLR(EC_Yv);
retVal = SDRM_OS2BN(pchYv, crt->ctx->ecdsactx->uDimension >> 3, EC_Yv->x);
- if (retVal != CRYPTO_SUCCESS)
- {
+
+ if (retVal != CRYPTO_SUCCESS) {
free(BN_Xk);
free(kP);
free(EC_Yv);
@@ -176,9 +172,10 @@ int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8* pchXk, cc_u8* pchYv, cc_
return CRYPTO_MEMORY_ALLOC_FAIL;
}
- retVal = SDRM_OS2BN(pchYv + (crt->ctx->ecdsactx->uDimension >> 3), crt->ctx->ecdsactx->uDimension >> 3, EC_Yv->y);
- if (retVal != CRYPTO_SUCCESS)
- {
+ retVal = SDRM_OS2BN(pchYv + (crt->ctx->ecdsactx->uDimension >> 3),
+ crt->ctx->ecdsactx->uDimension >> 3, EC_Yv->y);
+
+ if (retVal != CRYPTO_SUCCESS) {
free(BN_Xk);
free(kP);
free(EC_Yv);
@@ -186,8 +183,7 @@ int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8* pchXk, cc_u8* pchYv, cc_
return CRYPTO_MEMORY_ALLOC_FAIL;
}
- if (SDRM_CTX_EC_kP(ctx, kP, EC_Yv, BN_Xk) == CRYPTO_MEMORY_ALLOC_FAIL)
- {
+ if (SDRM_CTX_EC_kP(ctx, kP, EC_Yv, BN_Xk) == CRYPTO_MEMORY_ALLOC_FAIL) {
free(BN_Xk);
free(kP);
free(EC_Yv);
@@ -196,8 +192,8 @@ int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8* pchXk, cc_u8* pchYv, cc_
}
retVal = SDRM_BN2OS(kP->x, crt->ctx->ecdsactx->uDimension >> 3, pchKauth);
- if (retVal != CRYPTO_SUCCESS)
- {
+
+ if (retVal != CRYPTO_SUCCESS) {
free(BN_Xk);
free(kP);
free(EC_Yv);
diff --git a/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c b/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c
index 2436f09..1725ead 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c
@@ -35,63 +35,59 @@
// Functions
////////////////////////////////////////////////////////////////////////////
/*
- * @fn SDRM_CTX_ECDSA_KEY_GEN
- * @brief generate signature
+ * @fn SDRM_CTX_ECDSA_KEY_GEN
+ * @brief generate signature
*
- * @param ctx [out]ecc context
+ * @param ctx [out]ecc context
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
- * \n CRYPTO_NULL_POINTER if any argument is a null pointer
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * \n CRYPTO_NULL_POINTER if any argument is a null pointer
*/
int SDRM_CTX_ECDSA_KEY_GEN(SDRM_ECC_CTX *ctx)
{
- int i, retVal;
- cc_u32 Seed[4];
- SDRM_BIG_NUM *BN_d, *BN_temp;
- SDRM_EC_POINT *kP;
+ int i, retVal;
+ cc_u32 Seed[4];
+ SDRM_BIG_NUM *BN_d, *BN_temp;
+ SDRM_EC_POINT *kP;
- cc_u8 *pbBuf = NULL;
+ cc_u8 *pbBuf = NULL;
if (ctx == NULL)
- {
return CRYPTO_NULL_POINTER;
- }
- pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 2);
+ pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 2);
+
if (!pbBuf)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_d = SDRM_BN_Alloc( pbBuf , SDRM_ECC_BN_BUFSIZE);
- BN_temp = SDRM_BN_Alloc((cc_u8*)BN_d + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
+ BN_d = SDRM_BN_Alloc(pbBuf, SDRM_ECC_BN_BUFSIZE);
+ BN_temp = SDRM_BN_Alloc((cc_u8 *)BN_d + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
kP = SDRM_ECC_Init();
- if (kP == NULL)
- {
+
+ if (kP == NULL) {
free(pbBuf);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
for (i = 0; i < 4; i++)
- {
Seed[i] = (rand() << 16) ^ rand();
- }
SDRM_BN_Sub(BN_temp, ctx->ECC_n, BN_One);
+
do {
- SDRM_RNG_X931((cc_u8 *)Seed, ctx->uDimension, (cc_u8*)BN_d->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, ctx->uDimension, (cc_u8 *)BN_d->pData);
BN_d->Length = ctx->uDimension / 32;
- }
- while ((SDRM_BN_Cmp(BN_d, BN_One) < 0) || (SDRM_BN_Cmp(BN_d, BN_temp) > 0));
+ } while ((SDRM_BN_Cmp(BN_d, BN_One) < 0) || (SDRM_BN_Cmp(BN_d, BN_temp) > 0));
SDRM_BN_OPTIMIZE_LENGTH(BN_d);
SDRM_EC_SET_ZERO(kP);
retVal = SDRM_CTX_EC_kP(ctx, kP, ctx->ECC_G, BN_d);
- if (retVal != CRYPTO_SUCCESS)
- {
+
+ if (retVal != CRYPTO_SUCCESS) {
free(pbBuf);
free(kP);
@@ -108,75 +104,76 @@ int SDRM_CTX_ECDSA_KEY_GEN(SDRM_ECC_CTX *ctx)
}
/*
- * @fn SDRM_CTX_ECDSA_SIG_GEN
- * @brief generate signature
+ * @fn SDRM_CTX_ECDSA_SIG_GEN
+ * @brief generate signature
*
- * @param ctx [in]ecc context
- * @param sig [out]generated signature
- * @param hash [in]hashed message
+ * @param ctx [in]ecc context
+ * @param sig [out]generated signature
+ * @param hash [in]hashed message
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
- * \n CRYPTO_NULL_POINTER if any argument is a null pointer
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * \n CRYPTO_NULL_POINTER if any argument is a null pointer
*/
-int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash, unsigned int hashLen)
+int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash,
+ unsigned int hashLen)
{
- int i, res = -1;
- cc_u32 Seed[20];
- SDRM_BIG_NUM *BN_Tmp1, *BN_Tmp2, *BN_Tmp3;
- SDRM_BIG_NUM *BN_k, *BN_r, *BN_s, *BN_hash;
- SDRM_EC_POINT *kP;
+ int i, res = -1;
+ cc_u32 Seed[20];
+ SDRM_BIG_NUM *BN_Tmp1, *BN_Tmp2, *BN_Tmp3;
+ SDRM_BIG_NUM *BN_k, *BN_r, *BN_s, *BN_hash;
+ SDRM_EC_POINT *kP;
- cc_u8 *pbBuf = NULL;
+ cc_u8 *pbBuf = NULL;
- if ((ctx== NULL) || (sig == NULL) || (hash == NULL))
- {
+ if ((ctx == NULL) || (sig == NULL) || (hash == NULL))
return CRYPTO_NULL_POINTER;
- }
- pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 7);
+ pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 7);
+
if (!pbBuf)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_Tmp1 = SDRM_BN_Alloc( pbBuf, SDRM_ECC_BN_BUFSIZE);
- BN_Tmp2 = SDRM_BN_Alloc((cc_u8*)BN_Tmp1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
- BN_Tmp3 = SDRM_BN_Alloc((cc_u8*)BN_Tmp2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
- BN_k = SDRM_BN_Alloc((cc_u8*)BN_Tmp3 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
- BN_r = SDRM_BN_Alloc((cc_u8*)BN_k + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
- BN_s = SDRM_BN_Alloc((cc_u8*)BN_r + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
- BN_hash = SDRM_BN_Alloc((cc_u8*)BN_s + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
+ BN_Tmp1 = SDRM_BN_Alloc(pbBuf,
+ SDRM_ECC_BN_BUFSIZE);
+ BN_Tmp2 = SDRM_BN_Alloc((cc_u8 *)BN_Tmp1 + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
+ BN_Tmp3 = SDRM_BN_Alloc((cc_u8 *)BN_Tmp2 + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
+ BN_k = SDRM_BN_Alloc((cc_u8 *)BN_Tmp3 + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
+ BN_r = SDRM_BN_Alloc((cc_u8 *)BN_k + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
+ BN_s = SDRM_BN_Alloc((cc_u8 *)BN_r + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
+ BN_hash = SDRM_BN_Alloc((cc_u8 *)BN_s + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
kP = SDRM_ECC_Init();
- if (kP == NULL)
- {
+
+ if (kP == NULL) {
free(pbBuf);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
for (i = 0; i < 4; i++)
- {
Seed[i] = (rand() << 16) ^ rand();
- }
- while(1)
- {
- while(1)
- {
- // 1. [1, r-1] ������ ���� k ����
+ while (1) {
+ while (1) {
+ // 1. [1, r-1] ������ ���� k ����
SDRM_BN_Sub(BN_Tmp1, ctx->ECC_n, BN_One);
+
do {
- SDRM_RNG_X931((cc_u8 *)Seed, ctx->uDimension, (cc_u8*)BN_k->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, ctx->uDimension, (cc_u8 *)BN_k->pData);
BN_k->Length = ctx->uDimension / 32;
- }
- while((SDRM_BN_Cmp(BN_k, BN_One) < 0) || (SDRM_BN_Cmp(BN_k, BN_Tmp1) > 0));
+ } while ((SDRM_BN_Cmp(BN_k, BN_One) < 0) || (SDRM_BN_Cmp(BN_k, BN_Tmp1) > 0));
- // 2. kP = (x1, y1), r = x1 mod n(&ctx.ECC_n) ���. r = 0 �̸� k �ٽ� ����
+ // 2. kP = (x1, y1), r = x1 mod n(&ctx.ECC_n) ���. r = 0 �̸� k �ٽ� ����
SDRM_EC_SET_ZERO(kP);
res = SDRM_CTX_EC_kP(ctx, kP, ctx->ECC_G, BN_k);
- if (res != CRYPTO_SUCCESS)
- {
+
+ if (res != CRYPTO_SUCCESS) {
free(pbBuf);
free(kP);
@@ -185,33 +182,31 @@ int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash, unsigned
//SDRM_PrintBN("kP->x", kP->x);
SDRM_BN_ModRed(BN_r, kP->x, ctx->ECC_n);
- if (BN_r->Length > 0) // r = 0 �̸� k �ٽ� ����
- {
+
+ if (BN_r->Length > 0) // r = 0 �̸� k �ٽ� ����
break;
- }
}
- // 3. k^{-1} mod n ���.
+ // 3. k^{-1} mod n ���.
SDRM_BN_ModInv(BN_Tmp1, BN_k, ctx->ECC_n);
//SDRM_PrintBN("BN_k", BN_k);
//SDRM_PrintBN("ctx->ECC_n", ctx->ECC_n);
//SDRM_PrintBN("BN_Tmp1 = k^{-1} mod n", BN_Tmp1);
- // 4. s = k^{-1}(hash + dr) mod n ��� (d = private key). s = 0 �̸� 1������.
+ // 4. s = k^{-1}(hash + dr) mod n ��� (d = private key). s = 0 �̸� 1������.
// BN_Tmp2 = dr
SDRM_OS2BN(hash, hashLen, BN_hash);
SDRM_BN_ModMul(BN_Tmp2, ctx->PRIV_KEY, BN_r, ctx->ECC_n);
SDRM_BN_ModAdd(BN_Tmp3, BN_hash, BN_Tmp2, ctx->ECC_n);
SDRM_BN_ModMul(BN_s, BN_Tmp1, BN_Tmp3, ctx->ECC_n);
+
if (BN_s->Length > 0)
- {
break;
- }
}
-// (r, s) �������� ���.
+ // (r, s) �������� ���.
//SDRM_PrintBN("BN_r", BN_r);
//SDRM_PrintBN("BN_s", BN_s);
@@ -225,59 +220,64 @@ int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash, unsigned
}
/*
- * @fn SDRM_CTX_ECDSA_SIG_VERIFY
- * @brief verify ecdsa signature
+ * @fn SDRM_CTX_ECDSA_SIG_VERIFY
+ * @brief verify ecdsa signature
*
- * @param ctx [in]ecc context
- * @param sig [out]generated signature
- * @param signLen [out]byte-length of signature
- * @param hash [in]hash value
- * @param hashLen [in]byte-length of hash
+ * @param ctx [in]ecc context
+ * @param sig [out]generated signature
+ * @param signLen [out]byte-length of signature
+ * @param hash [in]hash value
+ * @param hashLen [in]byte-length of hash
*
- * @return CRYPTO_VALID_SIGN if given signature is valid
- * \n CRYPTO_INVALID_SIGN if given signature is invalid
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
- * \n CRYPTO_INVALID_ARGUMENT if any argument is out of range
- * \n CRYPTO_INFINITY_INPUT if given argument represents an infinity value
+ * @return CRYPTO_VALID_SIGN if given signature is valid
+ * \n CRYPTO_INVALID_SIGN if given signature is invalid
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * \n CRYPTO_INVALID_ARGUMENT if any argument is out of range
+ * \n CRYPTO_INFINITY_INPUT if given argument represents an infinity value
*/
-int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8 *hash, int hashLen)
+int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen,
+ cc_u8 *hash, int hashLen)
{
- int res;
- SDRM_BIG_NUM *BN_tmp, *BN_u1, *BN_u2, *BN_w, *BN_hash, *pBN_r, *pBN_s;
- SDRM_EC_POINT *EC_temp1, *EC_temp2;
+ int res;
+ SDRM_BIG_NUM *BN_tmp, *BN_u1, *BN_u2, *BN_w, *BN_hash, *pBN_r, *pBN_s;
+ SDRM_EC_POINT *EC_temp1, *EC_temp2;
- cc_u8 *pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 7);
+ cc_u8 *pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 7);
if (!pbBuf)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_tmp = SDRM_BN_Alloc( pbBuf, SDRM_ECC_BN_BUFSIZE);
- BN_u1 = SDRM_BN_Alloc((cc_u8*)BN_tmp + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
- BN_u2 = SDRM_BN_Alloc((cc_u8*)BN_u1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
- BN_w = SDRM_BN_Alloc((cc_u8*)BN_u2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
- BN_hash = SDRM_BN_Alloc((cc_u8*)BN_w + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
- pBN_r = SDRM_BN_Alloc((cc_u8*)BN_hash+ SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
- pBN_s = SDRM_BN_Alloc((cc_u8*)pBN_r + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE);
+ BN_tmp = SDRM_BN_Alloc(pbBuf,
+ SDRM_ECC_BN_BUFSIZE);
+ BN_u1 = SDRM_BN_Alloc((cc_u8 *)BN_tmp + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
+ BN_u2 = SDRM_BN_Alloc((cc_u8 *)BN_u1 + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
+ BN_w = SDRM_BN_Alloc((cc_u8 *)BN_u2 + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
+ BN_hash = SDRM_BN_Alloc((cc_u8 *)BN_w + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
+ pBN_r = SDRM_BN_Alloc((cc_u8 *)BN_hash + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
+ pBN_s = SDRM_BN_Alloc((cc_u8 *)pBN_r + SDRM_ECC_ALLOC_SIZE,
+ SDRM_ECC_BN_BUFSIZE);
EC_temp1 = SDRM_ECC_Init();
- if (EC_temp1 == NULL)
- {
+
+ if (EC_temp1 == NULL) {
free(pbBuf);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
EC_temp2 = SDRM_ECC_Init();
- if (EC_temp2 == NULL)
- {
+
+ if (EC_temp2 == NULL) {
free(pbBuf);
free(EC_temp1);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
- if ((cc_u32)signLen != (ctx->uDimension / 4))
- {
+ if ((cc_u32)signLen != (ctx->uDimension / 4)) {
free(pbBuf);
free(EC_temp1);
free(EC_temp2);
@@ -289,10 +289,10 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8
//SDRM_PrintBN("BN_r", pBN_r);
//SDRM_PrintBN("BN_s", pBN_s);
- // 1. r�� s�� ���� ����
+ // 1. r�� s�� ���� ����
SDRM_BN_Sub(BN_tmp, ctx->ECC_n, BN_One);
- if ((SDRM_BN_Cmp(pBN_r, BN_One) < 0) || (SDRM_BN_Cmp(pBN_r, BN_tmp) > 0))
- {
+
+ if ((SDRM_BN_Cmp(pBN_r, BN_One) < 0) || (SDRM_BN_Cmp(pBN_r, BN_tmp) > 0)) {
free(pbBuf);
free(EC_temp1);
free(EC_temp2);
@@ -300,8 +300,7 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8
return CRYPTO_INVALID_ARGUMENT;
}
- if ((SDRM_BN_Cmp(pBN_s, BN_One) < 0) || (SDRM_BN_Cmp(pBN_s, BN_tmp) > 0))
- {
+ if ((SDRM_BN_Cmp(pBN_s, BN_One) < 0) || (SDRM_BN_Cmp(pBN_s, BN_tmp) > 0)) {
free(pbBuf);
free(EC_temp1);
free(EC_temp2);
@@ -309,13 +308,12 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8
return CRYPTO_INVALID_ARGUMENT;
}
- // 2. w = s^(-1) mod n, BN_hash ���
+ // 2. w = s^(-1) mod n, BN_hash ���
SDRM_OS2BN(hash, hashLen, BN_hash);
res = SDRM_BN_ModInv(BN_w, pBN_s, ctx->ECC_n);
-//SDRM_PrintBN("BN_w", BN_w);
+ //SDRM_PrintBN("BN_w", BN_w);
- if (res != CRYPTO_SUCCESS)
- {
+ if (res != CRYPTO_SUCCESS) {
free(pbBuf);
free(EC_temp1);
free(EC_temp2);
@@ -323,24 +321,22 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8
return res;
}
- // 3. u1 = BN_hash *w mod n, u2 = rw mod n
+ // 3. u1 = BN_hash *w mod n, u2 = rw mod n
SDRM_BN_ModMul(BN_u1, BN_hash, BN_w, ctx->ECC_n);
SDRM_BN_ModMul(BN_u2, pBN_r, BN_w, ctx->ECC_n);
-//SDRM_PrintBN("BN_u1", BN_u1);
-//SDRM_PrintBN("BN_u2", BN_u2);
+ //SDRM_PrintBN("BN_u1", BN_u1);
+ //SDRM_PrintBN("BN_u2", BN_u2);
- // 4. (x0, y0) = u1P + u2Q, V = x0 mod n
+ // 4. (x0, y0) = u1P + u2Q, V = x0 mod n
res = SDRM_CTX_EC_2kP(ctx, EC_temp1, BN_u1, ctx->ECC_G, BN_u2, ctx->PUBLIC_KEY);
- if (res != CRYPTO_SUCCESS)
- {
+
+ if (res != CRYPTO_SUCCESS) {
free(pbBuf);
free(EC_temp1);
free(EC_temp2);
return res;
- }
- else if(EC_temp1->IsInfinity == 1)
- {
+ } else if (EC_temp1->IsInfinity == 1) {
res = CRYPTO_INFINITY_INPUT;
free(pbBuf);
free(EC_temp1);
@@ -349,16 +345,16 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8
return res;
}
-// SDRM_PrintBN("EC_temp1->x", EC_temp1->x);
-// SDRM_PrintBN("ctx->ECC_n", ctx->ECC_n);
+ // SDRM_PrintBN("EC_temp1->x", EC_temp1->x);
+ // SDRM_PrintBN("ctx->ECC_n", ctx->ECC_n);
SDRM_BN_ModRed(BN_tmp, EC_temp1->x, ctx->ECC_n);
-// SDRM_PrintBN("BN_tmp", BN_tmp);
-// SDRM_PrintBN("pBN_r", pBN_r);
+ // SDRM_PrintBN("BN_tmp", BN_tmp);
+ // SDRM_PrintBN("pBN_r", pBN_r);
// 5. V = r�� ��� ���� ok
res = SDRM_BN_Cmp_sign(BN_tmp, pBN_r);
- if (res != 0)
- {
+
+ if (res != 0) {
res = CRYPTO_INVALID_SIGN;
free(pbBuf);
free(EC_temp1);
@@ -376,162 +372,154 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8
}
/*
- * @fn SDRM_ECDSA_sign
- * @brief generate signature for given value
+ * @fn SDRM_ECDSA_sign
+ * @brief generate signature for given value
*
- * @param crt [in]crypto env structure
- * @param hash [in]hash value
- * @param hashLen [in]byte-length of hash
- * @param signature [out]generated signature
- * @param signLen [out]byte-length of signature
+ * @param crt [in]crypto env structure
+ * @param hash [in]hash value
+ * @param hashLen [in]byte-length of hash
+ * @param signature [out]generated signature
+ * @param signLen [out]byte-length of signature
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
*/
-int SDRM_ECDSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen)
+int SDRM_ECDSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen,
+ cc_u8 *signature, cc_u32 *signLen)
{
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL) || (hash == NULL) || (signature == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL) ||
+ (hash == NULL) || (signature == NULL))
return CRYPTO_NULL_POINTER;
- }
if (signLen)
- {
*signLen = crt->ctx->ecdsactx->uDimension / 4;
- }
return SDRM_CTX_ECDSA_SIG_GEN(crt->ctx->ecdsactx, signature, hash, hashLen);
}
/*
- * @fn SDRM_ECDSA_verify
- * @brief generate signature for given value
+ * @fn SDRM_ECDSA_verify
+ * @brief generate signature for given value
*
- * @param crt [in]crypto env structure
- * @param hash [in]hash value
- * @param hashLen [in]byte-length of hash
- * @param signature [in]signature
- * @param signLen [in]byte-length of signature
- * @param result [in]result of veryfing signature
+ * @param crt [in]crypto env structure
+ * @param hash [in]hash value
+ * @param hashLen [in]byte-length of hash
+ * @param signature [in]signature
+ * @param signLen [in]byte-length of signature
+ * @param result [in]result of veryfing signature
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid
*/
-int SDRM_ECDSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result)
+int SDRM_ECDSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen,
+ cc_u8 *signature, cc_u32 signLen, int *result)
{
int retVal;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL) || (hash == NULL) || (signature == NULL) || (result == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL) ||
+ (hash == NULL) || (signature == NULL) || (result == NULL))
return CRYPTO_NULL_POINTER;
- }
if (signLen != (crt->ctx->ecdsactx->uDimension / 4))
- {
return CRYPTO_INVALID_ARGUMENT;
- }
- retVal = SDRM_CTX_ECDSA_SIG_VERIFY(crt->ctx->ecdsactx, signature, signLen, hash, hashLen);
+ retVal = SDRM_CTX_ECDSA_SIG_VERIFY(crt->ctx->ecdsactx, signature, signLen, hash,
+ hashLen);
if (retVal == CRYPTO_VALID_SIGN)
- {
*result = CRYPTO_VALID_SIGN;
- }
+
else
- {
*result = CRYPTO_INVALID_SIGN;
- }
return retVal;
}
/*
- * @fn SDRM_ECC_Set_CTX
- * @brief Set parameters for ECC
+ * @fn SDRM_ECC_Set_CTX
+ * @brief Set parameters for ECC
*
- * @param crt [out]crypto env structure
- * @param Dimension [in]dimension
- * @param ECC_P_Data [in]represents p
- * @param ECC_P_Len [in]byte-length of p
- * @param ECC_A_Data [in]represents a
- * @param ECC_A_Len [in]byte-length of a
- * @param ECC_B_Data [in]represents b
- * @param ECC_B_Len [in]byte-length of b
- * @param ECC_G_X_Data [in]represents x coordinate of g
- * @param ECC_G_X_Len [in]byte-length of x coordinate of g
- * @param ECC_G_Y_Data [in]represents y coordinate of g
- * @param ECC_G_Y_Len [in]byte-length of y coordinate of g
- * @param ECC_R_Data [in]represents r
- * @param ECC_R_Len [in]byte-length of r
+ * @param crt [out]crypto env structure
+ * @param Dimension [in]dimension
+ * @param ECC_P_Data [in]represents p
+ * @param ECC_P_Len [in]byte-length of p
+ * @param ECC_A_Data [in]represents a
+ * @param ECC_A_Len [in]byte-length of a
+ * @param ECC_B_Data [in]represents b
+ * @param ECC_B_Len [in]byte-length of b
+ * @param ECC_G_X_Data [in]represents x coordinate of g
+ * @param ECC_G_X_Len [in]byte-length of x coordinate of g
+ * @param ECC_G_Y_Data [in]represents y coordinate of g
+ * @param ECC_G_Y_Len [in]byte-length of y coordinate of g
+ * @param ECC_R_Data [in]represents r
+ * @param ECC_R_Len [in]byte-length of r
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if argument is null
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if argument is null
*/
int SDRM_ECC_Set_CTX(CryptoCoreContainer *crt, cc_u16 Dimension,
- cc_u8* ECC_P_Data, cc_u32 ECC_P_Len,
- cc_u8* ECC_A_Data, cc_u32 ECC_A_Len,
- cc_u8* ECC_B_Data, cc_u32 ECC_B_Len,
- cc_u8* ECC_G_X_Data, cc_u32 ECC_G_X_Len,
- cc_u8* ECC_G_Y_Data, cc_u32 ECC_G_Y_Len,
- cc_u8* ECC_R_Data, cc_u32 ECC_R_Len)
+ cc_u8 *ECC_P_Data, cc_u32 ECC_P_Len,
+ cc_u8 *ECC_A_Data, cc_u32 ECC_A_Len,
+ cc_u8 *ECC_B_Data, cc_u32 ECC_B_Len,
+ cc_u8 *ECC_G_X_Data, cc_u32 ECC_G_X_Len,
+ cc_u8 *ECC_G_Y_Data, cc_u32 ECC_G_Y_Len,
+ cc_u8 *ECC_R_Data, cc_u32 ECC_R_Len)
{
- int retVal;
- cc_u8 zero[] = {0x00};
- SDRM_ECC_CTX *ECC_ctx;
+ int retVal;
+ cc_u8 zero[] = {0x00};
+ SDRM_ECC_CTX *ECC_ctx;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
- if ((ECC_P_Data == NULL) || (ECC_A_Data == NULL) || (ECC_B_Data == NULL) || (ECC_G_X_Data == NULL) || (ECC_G_Y_Data == NULL) || (ECC_R_Data == NULL))
- {
+ if ((ECC_P_Data == NULL) || (ECC_A_Data == NULL) || (ECC_B_Data == NULL) ||
+ (ECC_G_X_Data == NULL) || (ECC_G_Y_Data == NULL) || (ECC_R_Data == NULL))
return CRYPTO_NULL_POINTER;
- }
ECC_ctx = crt->ctx->ecdhctx;
ECC_ctx->uDimension = Dimension;
retVal = SDRM_OS2BN(ECC_P_Data, ECC_P_Len, ECC_ctx->ECC_p);
- if (retVal != CRYPTO_SUCCESS)
- {
+
+ if (retVal != CRYPTO_SUCCESS) {
free(ECC_ctx);
return retVal;
}
retVal = SDRM_OS2BN(ECC_A_Data, ECC_A_Len, ECC_ctx->ECC_a);
- if (retVal != CRYPTO_SUCCESS)
- {
+
+ if (retVal != CRYPTO_SUCCESS) {
free(ECC_ctx);
return retVal;
}
retVal = SDRM_OS2BN(ECC_B_Data, ECC_B_Len, ECC_ctx->ECC_b);
- if (retVal != CRYPTO_SUCCESS)
- {
+
+ if (retVal != CRYPTO_SUCCESS) {
free(ECC_ctx);
return retVal;
}
retVal = SDRM_OS2BN(ECC_R_Data, ECC_R_Len, ECC_ctx->ECC_n);
- if (retVal != CRYPTO_SUCCESS)
- {
+
+ if (retVal != CRYPTO_SUCCESS) {
free(ECC_ctx);
return retVal;
}
ECC_ctx->ECC_G->IsInfinity = 0;
retVal = SDRM_OS2BN(ECC_G_X_Data, ECC_G_X_Len, ECC_ctx->ECC_G->x);
- if (retVal != CRYPTO_SUCCESS)
- {
+
+ if (retVal != CRYPTO_SUCCESS) {
free(ECC_ctx);
return retVal;
}
+
retVal = SDRM_OS2BN(ECC_G_Y_Data, ECC_G_Y_Len, ECC_ctx->ECC_G->y);
- if (retVal != CRYPTO_SUCCESS)
- {
+
+ if (retVal != CRYPTO_SUCCESS) {
free(ECC_ctx);
return retVal;
}
@@ -544,127 +532,111 @@ int SDRM_ECC_Set_CTX(CryptoCoreContainer *crt, cc_u16 Dimension,
}
/*
- * @fn SDRM_ECC_genKeypair
- * @brief Generate Private Key and Generate Key Pair for ECC Signature
+ * @fn SDRM_ECC_genKeypair
+ * @brief Generate Private Key and Generate Key Pair for ECC Signature
*
- * @param crt [out]crypto env structure
- * @param PrivateKey [in]represents x coordinate of public key
- * @param PrivateKeyLen [in]byte-length of x coordinate of public key
- * @param PublicKey_X [in]represents x coordinate of public key
- * @param PublicKey_XLen [in]byte-length of x coordinate of public key
- * @param PublicKey_Y [in]represents y coordinate of public key
- * @param PublicKey_YLen [in]byte-length of y coordinate of public key
+ * @param crt [out]crypto env structure
+ * @param PrivateKey [in]represents x coordinate of public key
+ * @param PrivateKeyLen [in]byte-length of x coordinate of public key
+ * @param PublicKey_X [in]represents x coordinate of public key
+ * @param PublicKey_XLen [in]byte-length of x coordinate of public key
+ * @param PublicKey_Y [in]represents y coordinate of public key
+ * @param PublicKey_YLen [in]byte-length of y coordinate of public key
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if argument is null
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if argument is null
*/
-int SDRM_ECC_genKeypair (CryptoCoreContainer *crt,
- cc_u8 *PrivateKey, cc_u32 *PrivateKeyLen,
- cc_u8 *PublicKey_X, cc_u32 *PublicKey_XLen,
- cc_u8 *PublicKey_Y, cc_u32 *PublicKey_YLen)
+int SDRM_ECC_genKeypair(CryptoCoreContainer *crt,
+ cc_u8 *PrivateKey, cc_u32 *PrivateKeyLen,
+ cc_u8 *PublicKey_X, cc_u32 *PublicKey_XLen,
+ cc_u8 *PublicKey_Y, cc_u32 *PublicKey_YLen)
{
int retVal;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
retVal = SDRM_CTX_ECDSA_KEY_GEN(crt->ctx->ecdsactx);
+
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
- if (PrivateKey != NULL)
- {
- SDRM_I2OSP(crt->ctx->ecdsactx->PRIV_KEY, crt->ctx->ecdsactx->uDimension / 8, PrivateKey);
+ if (PrivateKey != NULL) {
+ SDRM_I2OSP(crt->ctx->ecdsactx->PRIV_KEY, crt->ctx->ecdsactx->uDimension / 8,
+ PrivateKey);
}
if (PrivateKeyLen != NULL)
- {
*PrivateKeyLen = crt->ctx->ecdsactx->uDimension / 8;
- }
- if (PublicKey_X != NULL)
- {
- SDRM_I2OSP(crt->ctx->ecdsactx->PUBLIC_KEY->x, crt->ctx->ecdsactx->uDimension / 8, PublicKey_X);
+ if (PublicKey_X != NULL) {
+ SDRM_I2OSP(crt->ctx->ecdsactx->PUBLIC_KEY->x,
+ crt->ctx->ecdsactx->uDimension / 8, PublicKey_X);
}
if (PublicKey_XLen != NULL)
- {
*PublicKey_XLen = crt->ctx->ecdsactx->uDimension / 8;
- }
- if (PublicKey_Y != NULL)
- {
- SDRM_I2OSP(crt->ctx->ecdsactx->PUBLIC_KEY->y, crt->ctx->ecdsactx->uDimension / 8, PublicKey_Y);
+ if (PublicKey_Y != NULL) {
+ SDRM_I2OSP(crt->ctx->ecdsactx->PUBLIC_KEY->y,
+ crt->ctx->ecdsactx->uDimension / 8, PublicKey_Y);
}
if (PublicKey_YLen != NULL)
- {
*PublicKey_YLen = crt->ctx->ecdsactx->uDimension / 8;
- }
return CRYPTO_SUCCESS;
}
/*
- * @fn SDRM_ECC_setKeypair
- * @brief Set key data for ECC
+ * @fn SDRM_ECC_setKeypair
+ * @brief Set key data for ECC
*
- * @param crt [out]crypto env structure
- * @param PRIV_Data [in]represents private key
- * @param PRIV_Len [in]byte-length of private key
- * @param PUB_X_Data [in]represents x coordinate of public key
- * @param PUB_X_Len [in]byte-length of x coordinate of public key
- * @param PUB_Y_Data [in]represents y coordinate of public key
- * @param PUB_Y_Len [in]byte-length of y coordinate of public key
+ * @param crt [out]crypto env structure
+ * @param PRIV_Data [in]represents private key
+ * @param PRIV_Len [in]byte-length of private key
+ * @param PUB_X_Data [in]represents x coordinate of public key
+ * @param PUB_X_Len [in]byte-length of x coordinate of public key
+ * @param PUB_Y_Data [in]represents y coordinate of public key
+ * @param PUB_Y_Len [in]byte-length of y coordinate of public key
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if argument is null
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if argument is null
*/
int SDRM_ECC_setKeypair(CryptoCoreContainer *crt,
- cc_u8* PRIV_Data, cc_u32 PRIV_Len,
- cc_u8* PUB_X_Data, cc_u32 PUB_X_Len,
- cc_u8* PUB_Y_Data, cc_u32 PUB_Y_Len)
+ cc_u8 *PRIV_Data, cc_u32 PRIV_Len,
+ cc_u8 *PUB_X_Data, cc_u32 PUB_X_Len,
+ cc_u8 *PUB_Y_Data, cc_u32 PUB_Y_Len)
{
- int retVal;
- cc_u8 zero[] = {0x00};
- SDRM_ECC_CTX *ECC_ctx;
+ int retVal;
+ cc_u8 zero[] = {0x00};
+ SDRM_ECC_CTX *ECC_ctx;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
ECC_ctx = crt->ctx->ecdsactx;
ECC_ctx->PUBLIC_KEY->IsInfinity = 0;
- if (PRIV_Data != NULL)
- {
+ if (PRIV_Data != NULL) {
retVal = SDRM_OS2BN(PRIV_Data, PRIV_Len, ECC_ctx->PRIV_KEY);
+
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
}
- if (PUB_X_Data != NULL && PUB_Y_Data != NULL)
- {
+ if (PUB_X_Data != NULL && PUB_Y_Data != NULL) {
retVal = SDRM_OS2BN(PUB_X_Data, PUB_X_Len, ECC_ctx->PUBLIC_KEY->x);
+
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
retVal = SDRM_OS2BN(PUB_Y_Data, PUB_Y_Len, ECC_ctx->PUBLIC_KEY->y);
+
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
}
SDRM_OS2BN(zero, 0, ECC_ctx->PUBLIC_KEY->z);
diff --git a/ssflib/dep/cryptocore/source/middle/cc_hmac.c b/ssflib/dep/cryptocore/source/middle/cc_hmac.c
index 731fbbd..09edd9e 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_hmac.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_hmac.c
@@ -32,75 +32,74 @@
////////////////////////////////////////////////////////////////////////////
// Functions
////////////////////////////////////////////////////////////////////////////
-int SDRM_getK0(cc_u8* k0, cc_u8* Key, cc_u32 KeyLen, cc_u32 Algorithm, cc_u32 B);
+int SDRM_getK0(cc_u8 *k0, cc_u8 *Key, cc_u32 KeyLen, cc_u32 Algorithm,
+ cc_u32 B);
/*
- * @fn SDRM_HMAC_init
- * @brief Parameter setting for mac code generation
+ * @fn SDRM_HMAC_init
+ * @brief Parameter setting for mac code generation
*
- * @param crt [out]crypto parameter
- * @param Key [in]user key
- * @param KeyLen [in]byte-length of Key
+ * @param crt [out]crypto parameter
+ * @param Key [in]user key
+ * @param KeyLen [in]byte-length of Key
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if Parameter is NULL
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if Parameter is NULL
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
int SDRM_HMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen)
{
SDRM_HMACContext *ctx;
- cc_u8 *ipad;
- cc_u32 i;
+ cc_u8 *ipad;
+ cc_u32 i;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || (Key == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) ||
+ (Key == NULL))
return CRYPTO_NULL_POINTER;
- }
ctx = crt->ctx->hmacctx;
ctx->algorithm = crt->alg;
- switch(ctx->algorithm)
- {
- case ID_HMD5 :
- ctx->B = SDRM_MD5_DATA_SIZE;
- break;
+ switch (ctx->algorithm) {
+ case ID_HMD5:
+ ctx->B = SDRM_MD5_DATA_SIZE;
+ break;
- case ID_HSHA1 :
- ctx->B = SDRM_SHA1_DATA_SIZE;
- break;
+ case ID_HSHA1:
+ ctx->B = SDRM_SHA1_DATA_SIZE;
+ break;
- case ID_HSHA224 :
- ctx->B = SDRM_SHA224_DATA_SIZE;
- break;
+ case ID_HSHA224:
+ ctx->B = SDRM_SHA224_DATA_SIZE;
+ break;
- case ID_HSHA256 :
- ctx->B = SDRM_SHA256_DATA_SIZE;
- break;
+ case ID_HSHA256:
+ ctx->B = SDRM_SHA256_DATA_SIZE;
+ break;
#ifndef _OP64_NOTSUPPORTED
- case ID_HSHA384 :
- ctx->B = SDRM_SHA384_DATA_SIZE;
- break;
- case ID_HSHA512 :
- ctx->B = SDRM_SHA512_DATA_SIZE;
- break;
+ case ID_HSHA384:
+ ctx->B = SDRM_SHA384_DATA_SIZE;
+ break;
+
+ case ID_HSHA512:
+ ctx->B = SDRM_SHA512_DATA_SIZE;
+ break;
#endif //_OP64_NOTSUPPORTED
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
- ipad = (cc_u8*)malloc(ctx->B);
+ ipad = (cc_u8 *)malloc(ctx->B);
+
if (ipad == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- ctx->k0 = (cc_u8*)malloc(ctx->B);
- if (ctx->k0 == NULL)
- {
+ ctx->k0 = (cc_u8 *)malloc(ctx->B);
+
+ if (ctx->k0 == NULL) {
free(ipad);
return CRYPTO_MEMORY_ALLOC_FAIL;
}
@@ -110,16 +109,11 @@ int SDRM_HMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen)
//ipad = k0 xor ipad
for (i = 0; i < ctx->B; i++)
- {
ipad[i] = ctx->k0[i] ^ 0x36;
- }
- if (i != ctx->B)
- {
+ if (i != ctx->B) {
for (; i < ctx->B; i++)
- {
ipad[i] = ctx->k0[i] ^ 0x36;
- }
}
ctx->md5_ctx = NULL;
@@ -131,109 +125,97 @@ int SDRM_HMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen)
ctx->sha512_ctx = NULL;
#endif //_OP64_NOTSUPPORTED
- switch(ctx->algorithm)
- {
- case ID_HMD5 :
- ctx->md5_ctx = (SDRM_MD5Context*)malloc(sizeof(SDRM_MD5Context));
-
- if (ctx->md5_ctx == NULL)
- {
- if (ipad != NULL)
- {
- free(ipad);
- }
- return CRYPTO_MEMORY_ALLOC_FAIL;
- }
-
- SDRM_MD5_Init(ctx->md5_ctx);
- SDRM_MD5_Update(ctx->md5_ctx, ipad, ctx->B);
- break;
+ switch (ctx->algorithm) {
+ case ID_HMD5:
+ ctx->md5_ctx = (SDRM_MD5Context *)malloc(sizeof(SDRM_MD5Context));
- case ID_HSHA1 :
- ctx->sha1_ctx = (SDRM_SHA1Context*)malloc(sizeof(SDRM_SHA1Context));
+ if (ctx->md5_ctx == NULL) {
+ if (ipad != NULL)
+ free(ipad);
- if (ctx->sha1_ctx == NULL)
- {
- if (ipad != NULL)
- {
- free(ipad);
- }
- return CRYPTO_MEMORY_ALLOC_FAIL;
- }
+ return CRYPTO_MEMORY_ALLOC_FAIL;
+ }
- SDRM_SHA1_Init(ctx->sha1_ctx);
- SDRM_SHA1_Update(ctx->sha1_ctx, ipad, ctx->B);
- break;
+ SDRM_MD5_Init(ctx->md5_ctx);
+ SDRM_MD5_Update(ctx->md5_ctx, ipad, ctx->B);
+ break;
- case ID_HSHA224 :
- ctx->sha224_ctx = (SDRM_SHA224Context*)malloc(sizeof(SDRM_SHA224Context));
+ case ID_HSHA1:
+ ctx->sha1_ctx = (SDRM_SHA1Context *)malloc(sizeof(SDRM_SHA1Context));
- if (ctx->sha224_ctx == NULL)
- {
- if (ipad != NULL)
- {
- free(ipad);
- }
- return CRYPTO_MEMORY_ALLOC_FAIL;
- }
+ if (ctx->sha1_ctx == NULL) {
+ if (ipad != NULL)
+ free(ipad);
- SDRM_SHA224_Init(ctx->sha224_ctx);
- SDRM_SHA224_Update(ctx->sha224_ctx, ipad, ctx->B);
- break;
+ return CRYPTO_MEMORY_ALLOC_FAIL;
+ }
- case ID_HSHA256 :
- ctx->sha256_ctx = (SDRM_SHA256Context*)malloc(sizeof(SDRM_SHA256Context));
+ SDRM_SHA1_Init(ctx->sha1_ctx);
+ SDRM_SHA1_Update(ctx->sha1_ctx, ipad, ctx->B);
+ break;
- if (ctx->sha256_ctx == NULL)
- {
- if (ipad != NULL)
- {
- free(ipad);
- }
- return CRYPTO_MEMORY_ALLOC_FAIL;
- }
+ case ID_HSHA224:
+ ctx->sha224_ctx = (SDRM_SHA224Context *)malloc(sizeof(SDRM_SHA224Context));
- SDRM_SHA256_Init(ctx->sha256_ctx);
- SDRM_SHA256_Update(ctx->sha256_ctx, ipad, ctx->B);
- break;
+ if (ctx->sha224_ctx == NULL) {
+ if (ipad != NULL)
+ free(ipad);
+
+ return CRYPTO_MEMORY_ALLOC_FAIL;
+ }
+
+ SDRM_SHA224_Init(ctx->sha224_ctx);
+ SDRM_SHA224_Update(ctx->sha224_ctx, ipad, ctx->B);
+ break;
+
+ case ID_HSHA256:
+ ctx->sha256_ctx = (SDRM_SHA256Context *)malloc(sizeof(SDRM_SHA256Context));
+
+ if (ctx->sha256_ctx == NULL) {
+ if (ipad != NULL)
+ free(ipad);
+
+ return CRYPTO_MEMORY_ALLOC_FAIL;
+ }
+
+ SDRM_SHA256_Init(ctx->sha256_ctx);
+ SDRM_SHA256_Update(ctx->sha256_ctx, ipad, ctx->B);
+ break;
#ifndef _OP64_NOTSUPPORTED
- case ID_HSHA384 :
- ctx->sha384_ctx = (SDRM_SHA384Context*)malloc(sizeof(SDRM_SHA384Context));
-
- if (ctx->sha384_ctx == NULL)
- {
- if (ipad != NULL)
- {
- free(ipad);
- }
- return CRYPTO_MEMORY_ALLOC_FAIL;
- }
-
- SDRM_SHA384_Init(ctx->sha384_ctx);
- SDRM_SHA384_Update(ctx->sha384_ctx, ipad, ctx->B);
- break;
- case ID_HSHA512 :
- ctx->sha512_ctx = (SDRM_SHA512Context*)malloc(sizeof(SDRM_SHA512Context));
+ case ID_HSHA384:
+ ctx->sha384_ctx = (SDRM_SHA384Context *)malloc(sizeof(SDRM_SHA384Context));
- if (ctx->sha512_ctx == NULL)
- {
- if (ipad != NULL)
- {
- free(ipad);
- }
- return CRYPTO_MEMORY_ALLOC_FAIL;
- }
+ if (ctx->sha384_ctx == NULL) {
+ if (ipad != NULL)
+ free(ipad);
- SDRM_SHA512_Init(ctx->sha512_ctx);
- SDRM_SHA512_Update(ctx->sha512_ctx, ipad, ctx->B);
- break;
+ return CRYPTO_MEMORY_ALLOC_FAIL;
+ }
+
+ SDRM_SHA384_Init(ctx->sha384_ctx);
+ SDRM_SHA384_Update(ctx->sha384_ctx, ipad, ctx->B);
+ break;
+
+ case ID_HSHA512:
+ ctx->sha512_ctx = (SDRM_SHA512Context *)malloc(sizeof(SDRM_SHA512Context));
+
+ if (ctx->sha512_ctx == NULL) {
+ if (ipad != NULL)
+ free(ipad);
+
+ return CRYPTO_MEMORY_ALLOC_FAIL;
+ }
+
+ SDRM_SHA512_Init(ctx->sha512_ctx);
+ SDRM_SHA512_Update(ctx->sha512_ctx, ipad, ctx->B);
+ break;
#endif //_OP64_NOTSUPPORTED
- default :
- free(ipad);
- return CRYPTO_INVALID_ARGUMENT;
+ default:
+ free(ipad);
+ return CRYPTO_INVALID_ARGUMENT;
}
free(ipad);
@@ -241,250 +223,239 @@ int SDRM_HMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen)
}
/*
- * @fn SDRM_HMAC_update
- * @brief process data blocks
+ * @fn SDRM_HMAC_update
+ * @brief process data blocks
*
- * @param crt [out]crypto parameter
- * @param msg [in]data block
- * @param msgLen [in]byte-length of Text
+ * @param crt [out]crypto parameter
+ * @param msg [in]data block
+ * @param msgLen [in]byte-length of Text
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if Parameter is NULL
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if Parameter is NULL
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
int SDRM_HMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen)
{
if (msgLen == 0)
- {
return CRYPTO_SUCCESS;
- }
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || (msg == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) ||
+ (msg == NULL))
return CRYPTO_NULL_POINTER;
- }
- switch(crt->ctx->hmacctx->algorithm)
- {
- case ID_HMD5 :
- SDRM_MD5_Update(crt->ctx->hmacctx->md5_ctx, msg, msgLen);
- break;
- case ID_HSHA1 :
- SDRM_SHA1_Update(crt->ctx->hmacctx->sha1_ctx, msg, msgLen);
- break;
- case ID_HSHA224 :
- SDRM_SHA224_Update(crt->ctx->hmacctx->sha224_ctx, msg, msgLen);
- break;
- case ID_HSHA256 :
- SDRM_SHA256_Update(crt->ctx->hmacctx->sha256_ctx, msg, msgLen);
- break;
+ switch (crt->ctx->hmacctx->algorithm) {
+ case ID_HMD5:
+ SDRM_MD5_Update(crt->ctx->hmacctx->md5_ctx, msg, msgLen);
+ break;
+
+ case ID_HSHA1:
+ SDRM_SHA1_Update(crt->ctx->hmacctx->sha1_ctx, msg, msgLen);
+ break;
+
+ case ID_HSHA224:
+ SDRM_SHA224_Update(crt->ctx->hmacctx->sha224_ctx, msg, msgLen);
+ break;
+
+ case ID_HSHA256:
+ SDRM_SHA256_Update(crt->ctx->hmacctx->sha256_ctx, msg, msgLen);
+ break;
#ifndef _OP64_NOTSUPPORTED
- case ID_HSHA384 :
- SDRM_SHA384_Update(crt->ctx->hmacctx->sha384_ctx, msg, msgLen);
- break;
- case ID_HSHA512 :
- SDRM_SHA512_Update(crt->ctx->hmacctx->sha512_ctx, msg, msgLen);
- break;
+
+ case ID_HSHA384:
+ SDRM_SHA384_Update(crt->ctx->hmacctx->sha384_ctx, msg, msgLen);
+ break;
+
+ case ID_HSHA512:
+ SDRM_SHA512_Update(crt->ctx->hmacctx->sha512_ctx, msg, msgLen);
+ break;
#endif //OP64_NOTSUPPORTED
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
return CRYPTO_SUCCESS;
}
/*
- * @fn SDRM_HMAC_final
- * @brief process last data block
+ * @fn SDRM_HMAC_final
+ * @brief process last data block
*
- * @param crt [in]crypto parameter
- * @param output [out]generated MAC
- * @param outputLen [out]byte-length of output
+ * @param crt [in]crypto parameter
+ * @param output [out]generated MAC
+ * @param outputLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if Parameter is NULL
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if Parameter is NULL
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
int SDRM_HMAC_final(CryptoCoreContainer *crt, cc_u8 *output, cc_u32 *outputLen)
{
- SDRM_HMACContext *ctx;
- SDRM_MD5Context MD5ctx;
- SDRM_SHA1Context SHA1ctx;
- SDRM_SHA224Context SHA224ctx;
- SDRM_SHA256Context SHA256ctx;
+ SDRM_HMACContext *ctx;
+ SDRM_MD5Context MD5ctx;
+ SDRM_SHA1Context SHA1ctx;
+ SDRM_SHA224Context SHA224ctx;
+ SDRM_SHA256Context SHA256ctx;
#ifndef _OP64_NOTSUPPORTED
- SDRM_SHA384Context SHA384ctx;
- SDRM_SHA512Context SHA512ctx;
+ SDRM_SHA384Context SHA384ctx;
+ SDRM_SHA512Context SHA512ctx;
#endif //_OP64_NOTSUPPORTED
- cc_u8 Step6_Result[64];
- cc_u32 HashLen, i;
+ cc_u8 Step6_Result[64];
+ cc_u32 HashLen, i;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || (output == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) ||
+ (output == NULL))
return CRYPTO_NULL_POINTER;
- }
ctx = crt->ctx->hmacctx;
//k0 = k0 xor opad
for (i = 0; i < ctx->B; i++)
- {
ctx->k0[i] ^= 0x5c;
- }
- if (i != ctx->B)
- {
+ if (i != ctx->B) {
for (; i < ctx->B; i++)
- {
ctx->k0[i] ^= 0x5c;
- }
}
//Step6 : get H((k0 xor ipad) | text) & Step 9 : make hash
- switch(ctx->algorithm)
- {
- case ID_HMD5 :
- SDRM_MD5_Final(ctx->md5_ctx, Step6_Result);
- free(ctx->md5_ctx);
+ switch (ctx->algorithm) {
+ case ID_HMD5:
+ SDRM_MD5_Final(ctx->md5_ctx, Step6_Result);
+ free(ctx->md5_ctx);
- HashLen = SDRM_MD5_BLOCK_SIZ;
+ HashLen = SDRM_MD5_BLOCK_SIZ;
- SDRM_MD5_Init(&MD5ctx);
- SDRM_MD5_Update(&MD5ctx, ctx->k0, ctx->B);
- SDRM_MD5_Update(&MD5ctx, Step6_Result, HashLen);
- SDRM_MD5_Final(&MD5ctx, output);
+ SDRM_MD5_Init(&MD5ctx);
+ SDRM_MD5_Update(&MD5ctx, ctx->k0, ctx->B);
+ SDRM_MD5_Update(&MD5ctx, Step6_Result, HashLen);
+ SDRM_MD5_Final(&MD5ctx, output);
- break;
+ break;
- case ID_HSHA1 :
- SDRM_SHA1_Final(ctx->sha1_ctx, Step6_Result);
- free(ctx->sha1_ctx);
+ case ID_HSHA1:
+ SDRM_SHA1_Final(ctx->sha1_ctx, Step6_Result);
+ free(ctx->sha1_ctx);
- HashLen = SDRM_SHA1_BLOCK_SIZ;
+ HashLen = SDRM_SHA1_BLOCK_SIZ;
- SDRM_SHA1_Init(&SHA1ctx);
- SDRM_SHA1_Update(&SHA1ctx, ctx->k0, ctx->B);
- SDRM_SHA1_Update(&SHA1ctx, Step6_Result, HashLen);
- SDRM_SHA1_Final(&SHA1ctx, output);
+ SDRM_SHA1_Init(&SHA1ctx);
+ SDRM_SHA1_Update(&SHA1ctx, ctx->k0, ctx->B);
+ SDRM_SHA1_Update(&SHA1ctx, Step6_Result, HashLen);
+ SDRM_SHA1_Final(&SHA1ctx, output);
- break;
+ break;
- case ID_HSHA224 :
- SDRM_SHA224_Final(ctx->sha224_ctx, Step6_Result);
- free(ctx->sha224_ctx);
+ case ID_HSHA224:
+ SDRM_SHA224_Final(ctx->sha224_ctx, Step6_Result);
+ free(ctx->sha224_ctx);
- HashLen = SDRM_SHA224_BLOCK_SIZ;
+ HashLen = SDRM_SHA224_BLOCK_SIZ;
- SDRM_SHA224_Init(&SHA224ctx);
- SDRM_SHA224_Update(&SHA224ctx, ctx->k0, ctx->B);
- SDRM_SHA224_Update(&SHA224ctx, Step6_Result, HashLen);
- SDRM_SHA224_Final(&SHA224ctx, output);
+ SDRM_SHA224_Init(&SHA224ctx);
+ SDRM_SHA224_Update(&SHA224ctx, ctx->k0, ctx->B);
+ SDRM_SHA224_Update(&SHA224ctx, Step6_Result, HashLen);
+ SDRM_SHA224_Final(&SHA224ctx, output);
- break;
+ break;
- case ID_HSHA256 :
- SDRM_SHA256_Final(ctx->sha256_ctx, Step6_Result);
- free(ctx->sha256_ctx);
+ case ID_HSHA256:
+ SDRM_SHA256_Final(ctx->sha256_ctx, Step6_Result);
+ free(ctx->sha256_ctx);
- HashLen = SDRM_SHA256_BLOCK_SIZ;
+ HashLen = SDRM_SHA256_BLOCK_SIZ;
- SDRM_SHA256_Init(&SHA256ctx);
- SDRM_SHA256_Update(&SHA256ctx, ctx->k0, ctx->B);
- SDRM_SHA256_Update(&SHA256ctx, Step6_Result, HashLen);
- SDRM_SHA256_Final(&SHA256ctx, output);
+ SDRM_SHA256_Init(&SHA256ctx);
+ SDRM_SHA256_Update(&SHA256ctx, ctx->k0, ctx->B);
+ SDRM_SHA256_Update(&SHA256ctx, Step6_Result, HashLen);
+ SDRM_SHA256_Final(&SHA256ctx, output);
- break;
+ break;
#ifndef _OP64_NOTSUPPORTED
- case ID_HSHA384 :
- SDRM_SHA384_Final(ctx->sha384_ctx, Step6_Result);
- free(ctx->sha384_ctx);
- HashLen = SDRM_SHA384_BLOCK_SIZ;
+ case ID_HSHA384:
+ SDRM_SHA384_Final(ctx->sha384_ctx, Step6_Result);
+ free(ctx->sha384_ctx);
- SDRM_SHA384_Init(&SHA384ctx);
- SDRM_SHA384_Update(&SHA384ctx, ctx->k0, ctx->B);
- SDRM_SHA384_Update(&SHA384ctx, Step6_Result, HashLen);
- SDRM_SHA384_Final(&SHA384ctx, output);
+ HashLen = SDRM_SHA384_BLOCK_SIZ;
- break;
+ SDRM_SHA384_Init(&SHA384ctx);
+ SDRM_SHA384_Update(&SHA384ctx, ctx->k0, ctx->B);
+ SDRM_SHA384_Update(&SHA384ctx, Step6_Result, HashLen);
+ SDRM_SHA384_Final(&SHA384ctx, output);
- case ID_HSHA512 :
- SDRM_SHA512_Final(ctx->sha512_ctx, Step6_Result);
- free(ctx->sha512_ctx);
+ break;
- HashLen = SDRM_SHA512_BLOCK_SIZ;
+ case ID_HSHA512:
+ SDRM_SHA512_Final(ctx->sha512_ctx, Step6_Result);
+ free(ctx->sha512_ctx);
- SDRM_SHA512_Init(&SHA512ctx);
- SDRM_SHA512_Update(&SHA512ctx, ctx->k0, ctx->B);
- SDRM_SHA512_Update(&SHA512ctx, Step6_Result, HashLen);
- SDRM_SHA512_Final(&SHA512ctx, output);
+ HashLen = SDRM_SHA512_BLOCK_SIZ;
- break;
+ SDRM_SHA512_Init(&SHA512ctx);
+ SDRM_SHA512_Update(&SHA512ctx, ctx->k0, ctx->B);
+ SDRM_SHA512_Update(&SHA512ctx, Step6_Result, HashLen);
+ SDRM_SHA512_Final(&SHA512ctx, output);
+
+ break;
#endif
- default :
- if (ctx->k0) {
+ default:
+ if (ctx->k0)
free(ctx->k0);
- }
- return CRYPTO_INVALID_ARGUMENT;
+
+ return CRYPTO_INVALID_ARGUMENT;
}
if (outputLen != NULL)
- {
*outputLen = HashLen;
- }
if (ctx->k0)
- {
free(ctx->k0);
- }
return CRYPTO_SUCCESS;
}
/*
- * @fn SDRM_HMAC_getMAC
- * @brief generate h-mac code
+ * @fn SDRM_HMAC_getMAC
+ * @brief generate h-mac code
*
- * @param crt [in]crypto parameter
- * @param Key [in]user key
- * @param KeyLen [in]byte-length of Key
- * @param msg [in]data block
- * @param msgLen [in]byte-length of Text
- * @param output [out]generated MAC
- * @param outputLen [out]byte-length of output
+ * @param crt [in]crypto parameter
+ * @param Key [in]user key
+ * @param KeyLen [in]byte-length of Key
+ * @param msg [in]data block
+ * @param msgLen [in]byte-length of Text
+ * @param output [out]generated MAC
+ * @param outputLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if no error is occured
+ * @return CRYPTO_SUCCESS if no error is occured
*/
-int SDRM_HMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen, cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen)
+int SDRM_HMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen,
+ cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen)
{
int result;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || (Key == NULL) || (output == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) ||
+ (Key == NULL) || (output == NULL))
return CRYPTO_NULL_POINTER;
- }
result = SDRM_HMAC_init(crt, Key, KeyLen);
+
if (result != CRYPTO_SUCCESS)
- {
return result;
- }
result = SDRM_HMAC_update(crt, msg, msgLen);
+
if (result != CRYPTO_SUCCESS)
- {
return result;
- }
return SDRM_HMAC_final(crt, output, outputLen);
}
-int SDRM_getK0(cc_u8* k0, cc_u8* Key, cc_u32 KeyLen, cc_u32 Algorithm, cc_u32 B)
+int SDRM_getK0(cc_u8 *k0, cc_u8 *Key, cc_u32 KeyLen, cc_u32 Algorithm, cc_u32 B)
{
SDRM_MD5Context MD5ctx;
SDRM_SHA1Context SHA1ctx;
@@ -497,83 +468,79 @@ int SDRM_getK0(cc_u8* k0, cc_u8* Key, cc_u32 KeyLen, cc_u32 Algorithm, cc_u32 B)
int L;
- if (KeyLen == B)
- {
+ if (KeyLen == B) {
//if the length of K = B : set K0 = K
memcpy(k0, Key, B);
- }
- else if (KeyLen > B)
- {
+ } else if (KeyLen > B) {
//if the length of K > B : get hask(K) and append (B - L) zeros
//get hash(K)
- switch(Algorithm)
- {
- case ID_HMD5 :
- SDRM_MD5_Init(&MD5ctx);
- SDRM_MD5_Update(&MD5ctx, Key, KeyLen);
- SDRM_MD5_Final(&MD5ctx, k0);
+ switch (Algorithm) {
+ case ID_HMD5:
+ SDRM_MD5_Init(&MD5ctx);
+ SDRM_MD5_Update(&MD5ctx, Key, KeyLen);
+ SDRM_MD5_Final(&MD5ctx, k0);
- L = SDRM_MD5_BLOCK_SIZ;
+ L = SDRM_MD5_BLOCK_SIZ;
- break;
+ break;
- case ID_HSHA1 :
- SDRM_SHA1_Init(&SHA1ctx);
- SDRM_SHA1_Update(&SHA1ctx, Key, KeyLen);
- SDRM_SHA1_Final(&SHA1ctx, k0);
+ case ID_HSHA1:
+ SDRM_SHA1_Init(&SHA1ctx);
+ SDRM_SHA1_Update(&SHA1ctx, Key, KeyLen);
+ SDRM_SHA1_Final(&SHA1ctx, k0);
- L = SDRM_SHA1_BLOCK_SIZ;
+ L = SDRM_SHA1_BLOCK_SIZ;
- break;
+ break;
- case ID_HSHA224 :
- SDRM_SHA224_Init(&SHA224ctx);
- SDRM_SHA224_Update(&SHA224ctx, Key, KeyLen);
- SDRM_SHA224_Final(&SHA224ctx, k0);
+ case ID_HSHA224:
+ SDRM_SHA224_Init(&SHA224ctx);
+ SDRM_SHA224_Update(&SHA224ctx, Key, KeyLen);
+ SDRM_SHA224_Final(&SHA224ctx, k0);
- L = SDRM_SHA224_BLOCK_SIZ;
+ L = SDRM_SHA224_BLOCK_SIZ;
- break;
+ break;
- case ID_HSHA256 :
- SDRM_SHA256_Init(&SHA256ctx);
- SDRM_SHA256_Update(&SHA256ctx, Key, KeyLen);
- SDRM_SHA256_Final(&SHA256ctx, k0);
+ case ID_HSHA256:
+ SDRM_SHA256_Init(&SHA256ctx);
+ SDRM_SHA256_Update(&SHA256ctx, Key, KeyLen);
+ SDRM_SHA256_Final(&SHA256ctx, k0);
- L = SDRM_SHA256_BLOCK_SIZ;
+ L = SDRM_SHA256_BLOCK_SIZ;
- break;
+ break;
#ifndef _OP64_NOTSUPPORTED
- case ID_HSHA384 :
- SDRM_SHA384_Init(&SHA384ctx);
- SDRM_SHA384_Update(&SHA384ctx, Key, KeyLen);
- SDRM_SHA384_Final(&SHA384ctx, k0);
- L = SDRM_SHA384_BLOCK_SIZ;
+ case ID_HSHA384:
+ SDRM_SHA384_Init(&SHA384ctx);
+ SDRM_SHA384_Update(&SHA384ctx, Key, KeyLen);
+ SDRM_SHA384_Final(&SHA384ctx, k0);
- break;
+ L = SDRM_SHA384_BLOCK_SIZ;
- case ID_HSHA512 :
- SDRM_SHA512_Init(&SHA512ctx);
- SDRM_SHA512_Update(&SHA512ctx, Key, KeyLen);
- SDRM_SHA512_Final(&SHA512ctx, k0);
+ break;
- L = SDRM_SHA512_BLOCK_SIZ;
+ case ID_HSHA512:
+ SDRM_SHA512_Init(&SHA512ctx);
+ SDRM_SHA512_Update(&SHA512ctx, Key, KeyLen);
+ SDRM_SHA512_Final(&SHA512ctx, k0);
+
+ L = SDRM_SHA512_BLOCK_SIZ;
- break;
+ break;
#endif
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
//append zeros
memset(k0 + L, 0x00, B - L);
- }
- else {
+ } else {
//if the length of K < B : append zerots to the end of K
memcpy(k0, Key, KeyLen);
memset(k0 + KeyLen, 0x00, B - KeyLen);
@@ -582,4 +549,4 @@ int SDRM_getK0(cc_u8* k0, cc_u8* Key, cc_u32 KeyLen, cc_u32 Algorithm, cc_u32 B)
return CRYPTO_SUCCESS;
}
-/***************************** End of File *****************************/
\ No newline at end of file
+/***************************** End of File *****************************/
diff --git a/ssflib/dep/cryptocore/source/middle/cc_rng.c b/ssflib/dep/cryptocore/source/middle/cc_rng.c
index 42d38a1..61f0f85 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_rng.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_rng.c
@@ -31,20 +31,19 @@
// Functions
////////////////////////////////////////////////////////////////////////////
/*
- * @fn SDRM_X931_seed
- * @brief Seed RNG System
+ * @fn SDRM_X931_seed
+ * @brief Seed RNG System
*
- * @param crt [in]crypto env structure
- * @param seed [in]seed for RNG System
+ * @param crt [in]crypto env structure
+ * @param seed [in]seed for RNG System
*
- * @return CRYPTO_SUCCESS if success
+ * @return CRYPTO_SUCCESS if success
*/
int SDRM_X931_seed(CryptoCoreContainer *crt, cc_u8 *seed)
{
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->x931ctx == NULL) || (seed == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->x931ctx == NULL) ||
+ (seed == NULL))
return CRYPTO_NULL_POINTER;
- }
memcpy(crt->ctx->x931ctx->Seed, seed, SDRM_X931_SEED_SIZ);
@@ -52,30 +51,30 @@ int SDRM_X931_seed(CryptoCoreContainer *crt, cc_u8 *seed)
}
/*
- * @fn SDRM_X931_get
- * @brief generate random number
+ * @fn SDRM_X931_get
+ * @brief generate random number
*
- * @param crt [in]crypto env structure
- * @param bitLength [in]bit length for generated number
- * @param data [out]generated data
+ * @param crt [in]crypto env structure
+ * @param bitLength [in]bit length for generated number
+ * @param data [out]generated data
*
- * @return CRYPTO_SUCCESS if success
+ * @return CRYPTO_SUCCESS if success
*/
-int SDRM_X931_get(CryptoCoreContainer *crt, cc_u32 bitLength, cc_u8 *data)
+int SDRM_X931_get(CryptoCoreContainer *crt, cc_u32 bitLength, cc_u8 *data)
{
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->x931ctx == NULL) || (data == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->x931ctx == NULL) ||
+ (data == NULL))
return CRYPTO_NULL_POINTER;
- }
#ifdef _WIN32_WCE
srand(GetTickCount());
#else
static int add_value = 0;
- if(++add_value == 10000) add_value = 0;
- srand(time(NULL) + add_value );
+ if (++add_value == 10000) add_value = 0;
+
+ srand(time(NULL) + add_value);
#endif
diff --git a/ssflib/dep/cryptocore/source/middle/cc_rsa.c b/ssflib/dep/cryptocore/source/middle/cc_rsa.c
index e0a2a36..8aad8ad 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_rsa.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_rsa.c
@@ -33,77 +33,81 @@
// Functions
//////////////////////////////////////////////////////////////////////////
/*
- * @fn SDRM_RSAContext *SDRM_RSA_InitCrt(cc_u32 KeyByteLen)
+ * @fn SDRM_RSAContext *SDRM_RSA_InitCrt(cc_u32 KeyByteLen)
*
- * @brief generate RSA Context
+ * @brief generate RSA Context
*
- * @return pointer to the generated context
- * \n NULL if memory allocation is failed
+ * @return pointer to the generated context
+ * \n NULL if memory allocation is failed
*/
SDRM_RSAContext *SDRM_RSA_InitCrt(cc_u32 KeyByteLen)
{
SDRM_RSAContext *ctx;
- cc_u32 RSA_KeyByteLen = KeyByteLen;
- cc_u8 *pbBuf = (cc_u8*)malloc(sizeof(SDRM_RSAContext) + SDRM_RSA_ALLOC_SIZE * 8);
+ cc_u32 RSA_KeyByteLen = KeyByteLen;
+ cc_u8 *pbBuf = (cc_u8 *)malloc(sizeof(SDRM_RSAContext) +
+ SDRM_RSA_ALLOC_SIZE * 8);
if (pbBuf == NULL)
- {
return NULL;
- }
-
- ctx = (SDRM_RSAContext*)(void*)pbBuf;
- ctx->n = SDRM_BN_Alloc((cc_u8*)ctx + sizeof(SDRM_RSAContext), SDRM_RSA_BN_BUFSIZE);
- ctx->e = SDRM_BN_Alloc((cc_u8*)ctx->n + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- ctx->d = SDRM_BN_Alloc((cc_u8*)ctx->e + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- ctx->p = SDRM_BN_Alloc((cc_u8*)ctx->d + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- ctx->q = SDRM_BN_Alloc((cc_u8*)ctx->p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- ctx->dmodp1 = SDRM_BN_Alloc((cc_u8*)ctx->q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- ctx->dmodq1 = SDRM_BN_Alloc((cc_u8*)ctx->dmodp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- ctx->iqmodp = SDRM_BN_Alloc((cc_u8*)ctx->dmodq1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- ctx->crt_operation = (unsigned int)-1;
- ctx->k = RSA_KeyByteLen;
+ ctx = (SDRM_RSAContext *)(void *)pbBuf;
+ ctx->n = SDRM_BN_Alloc((cc_u8 *)ctx + sizeof(SDRM_RSAContext),
+ SDRM_RSA_BN_BUFSIZE);
+ ctx->e = SDRM_BN_Alloc((cc_u8 *)ctx->n + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ ctx->d = SDRM_BN_Alloc((cc_u8 *)ctx->e + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ ctx->p = SDRM_BN_Alloc((cc_u8 *)ctx->d + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ ctx->q = SDRM_BN_Alloc((cc_u8 *)ctx->p + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ ctx->dmodp1 = SDRM_BN_Alloc((cc_u8 *)ctx->q + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ ctx->dmodq1 = SDRM_BN_Alloc((cc_u8 *)ctx->dmodp1 + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ ctx->iqmodp = SDRM_BN_Alloc((cc_u8 *)ctx->dmodq1 + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+
+ ctx->crt_operation = (unsigned int) - 1;
+ ctx->k = RSA_KeyByteLen;
return ctx;
}
/*
- * @fn int SDRM_RSA_setNED(CryptoCoreContainer *crt, cc_u32 PaddingMethod, cc_u8* RSA_N_Data, cc_u32 RSA_N_Len, cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, cc_u8* RSA_D_Data, cc_u32 RSA_D_Len)
- * @brief set RSA parameters
+ * @fn int SDRM_RSA_setNED(CryptoCoreContainer *crt, cc_u32 PaddingMethod, cc_u8* RSA_N_Data, cc_u32 RSA_N_Len, cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, cc_u8* RSA_D_Data, cc_u32 RSA_D_Len)
+ * @brief set RSA parameters
*
- * @param crt [out]rsa context
- * @param PaddingMethod [in]padding method
- * @param RSA_N_Data [in]n value
- * @param RSA_N_Len [in]byte-length of n
- * @param RSA_E_Data [in]e value
- * @param RSA_E_Len [in]byte-length of e
- * @param RSA_D_Data [in]d value
- * @param RSA_D_Len [in]byte-length of d
+ * @param crt [out]rsa context
+ * @param PaddingMethod [in]padding method
+ * @param RSA_N_Data [in]n value
+ * @param RSA_N_Len [in]byte-length of n
+ * @param RSA_E_Data [in]e value
+ * @param RSA_E_Len [in]byte-length of e
+ * @param RSA_D_Data [in]d value
+ * @param RSA_D_Len [in]byte-length of d
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if an argument is a null pointer
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if an argument is a null pointer
*/
int SDRM_RSA_setNED(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- cc_u8* RSA_N_Data, cc_u32 RSA_N_Len,
- cc_u8* RSA_E_Data, cc_u32 RSA_E_Len,
- cc_u8* RSA_D_Data, cc_u32 RSA_D_Len)
+ cc_u8 *RSA_N_Data, cc_u32 RSA_N_Len,
+ cc_u8 *RSA_E_Data, cc_u32 RSA_E_Len,
+ cc_u8 *RSA_D_Data, cc_u32 RSA_D_Len)
{
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (RSA_N_Data == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) ||
+ (RSA_N_Data == NULL))
return CRYPTO_NULL_POINTER;
- }
SDRM_OS2BN(RSA_N_Data, RSA_N_Len, crt->ctx->rsactx->n);
SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->n);
- if (RSA_E_Data != NULL)
- {
+ if (RSA_E_Data != NULL) {
SDRM_OS2BN(RSA_E_Data, RSA_E_Len, crt->ctx->rsactx->e);
SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->e);
}
- if (RSA_D_Data != NULL)
- {
+ if (RSA_D_Data != NULL) {
SDRM_OS2BN(RSA_D_Data, RSA_D_Len, crt->ctx->rsactx->d);
SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->d);
}
@@ -115,119 +119,100 @@ int SDRM_RSA_setNED(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
}
/*
- * @fn int SDRM_RSA_setNEDPQ(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- * cc_u8* RSA_N_Data, cc_u32 RSA_N_Len,
- * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len,
- * cc_u8* RSA_D_Data, cc_u32 RSA_D_Len,
- * cc_u8* RSA_P_Data, cc_u32 RSA_P_Len,
- * cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len,
- * cc_u8* RSA_DmodP1_Data, cc_u32 RSA_DmodP1_Len,
- * cc_u8* RSA_DmodQ1_Data, cc_u32 RSA_DmodQ1_Len,
- * cc_u8* RSA_iQmodP_Data, cc_u32 RSA_iQmodP_Len)
+ * @fn int SDRM_RSA_setNEDPQ(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
+ * cc_u8* RSA_N_Data, cc_u32 RSA_N_Len,
+ * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len,
+ * cc_u8* RSA_D_Data, cc_u32 RSA_D_Len,
+ * cc_u8* RSA_P_Data, cc_u32 RSA_P_Len,
+ * cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len,
+ * cc_u8* RSA_DmodP1_Data, cc_u32 RSA_DmodP1_Len,
+ * cc_u8* RSA_DmodQ1_Data, cc_u32 RSA_DmodQ1_Len,
+ * cc_u8* RSA_iQmodP_Data, cc_u32 RSA_iQmodP_Len)
*
- * @brief set RSA parameters
+ * @brief set RSA parameters
*
- * @param crt [out]rsa context
- * @param PaddingMethod [in]padding method
- * @param RSA_N_Data [in]n value
- * @param RSA_N_Len [in]byte-length of n
- * @param RSA_E_Data [in]e value
- * @param RSA_E_Len [in]byte-length of e
- * @param RSA_D_Data [in]d value
- * @param RSA_D_Len [in]byte-length of d
- * @param RSA_P_Data [in]p value
- * @param RSA_P_Len [in]byte-length of p
- * @param RSA_Q_Data [in]q value
- * @param RSA_Q_Len [in]byte-length of q
- * @param RSA_DmodP1_Data [in]d mod (p-1) value
- * @param RSA_DmodP1_Len [in]byte-length of d mod (p-1)
- * @param RSA_DmodQ1_Data [in]d mod (q-1) value
- * @param RSA_DmodQ1_Len [in]byte-length of d mod (q-1)
- * @param RSA_iQmodP_Data [in]q^(-1) mod p value
- * @param RSA_iQmodP_Len [in]byte-length of q^(-1) mod p
+ * @param crt [out]rsa context
+ * @param PaddingMethod [in]padding method
+ * @param RSA_N_Data [in]n value
+ * @param RSA_N_Len [in]byte-length of n
+ * @param RSA_E_Data [in]e value
+ * @param RSA_E_Len [in]byte-length of e
+ * @param RSA_D_Data [in]d value
+ * @param RSA_D_Len [in]byte-length of d
+ * @param RSA_P_Data [in]p value
+ * @param RSA_P_Len [in]byte-length of p
+ * @param RSA_Q_Data [in]q value
+ * @param RSA_Q_Len [in]byte-length of q
+ * @param RSA_DmodP1_Data [in]d mod (p-1) value
+ * @param RSA_DmodP1_Len [in]byte-length of d mod (p-1)
+ * @param RSA_DmodQ1_Data [in]d mod (q-1) value
+ * @param RSA_DmodQ1_Len [in]byte-length of d mod (q-1)
+ * @param RSA_iQmodP_Data [in]q^(-1) mod p value
+ * @param RSA_iQmodP_Len [in]byte-length of q^(-1) mod p
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if an argument is a null pointer
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if an argument is a null pointer
*/
int SDRM_RSA_setNEDPQ(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- cc_u8* RSA_N_Data, cc_u32 RSA_N_Len,
- cc_u8* RSA_E_Data, cc_u32 RSA_E_Len,
- cc_u8* RSA_D_Data, cc_u32 RSA_D_Len,
- cc_u8* RSA_P_Data, cc_u32 RSA_P_Len,
- cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len,
- cc_u8* RSA_DmodP1_Data, cc_u32 RSA_DmodP1_Len,
- cc_u8* RSA_DmodQ1_Data, cc_u32 RSA_DmodQ1_Len,
- cc_u8* RSA_iQmodP_Data, cc_u32 RSA_iQmodP_Len)
+ cc_u8 *RSA_N_Data, cc_u32 RSA_N_Len,
+ cc_u8 *RSA_E_Data, cc_u32 RSA_E_Len,
+ cc_u8 *RSA_D_Data, cc_u32 RSA_D_Len,
+ cc_u8 *RSA_P_Data, cc_u32 RSA_P_Len,
+ cc_u8 *RSA_Q_Data, cc_u32 RSA_Q_Len,
+ cc_u8 *RSA_DmodP1_Data, cc_u32 RSA_DmodP1_Len,
+ cc_u8 *RSA_DmodQ1_Data, cc_u32 RSA_DmodQ1_Len,
+ cc_u8 *RSA_iQmodP_Data, cc_u32 RSA_iQmodP_Len)
{
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
crt->ctx->rsactx->crt_operation = 0;
- if ((RSA_P_Data != NULL) && (RSA_Q_Data != NULL) && (RSA_DmodP1_Data != NULL) && (RSA_DmodQ1_Data != NULL) && (RSA_iQmodP_Data != NULL))
- {
+
+ if ((RSA_P_Data != NULL) && (RSA_Q_Data != NULL) && (RSA_DmodP1_Data != NULL) &&
+ (RSA_DmodQ1_Data != NULL) && (RSA_iQmodP_Data != NULL))
crt->ctx->rsactx->crt_operation = 1;
- }
+
else if (RSA_N_Data == NULL)
- {
return CRYPTO_NULL_POINTER;
- }
- if (RSA_N_Data != NULL)
- {
- if( !SDRM_OS2BN(RSA_N_Data, RSA_N_Len, crt->ctx->rsactx->n) ) {
- SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->n);
- }
+ if (RSA_N_Data != NULL) {
+ if (!SDRM_OS2BN(RSA_N_Data, RSA_N_Len, crt->ctx->rsactx->n))
+ SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->n);
}
- if (RSA_E_Data != NULL)
- {
- if( !SDRM_OS2BN(RSA_E_Data, RSA_E_Len, crt->ctx->rsactx->e) ) {
- SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->e);
- }
+ if (RSA_E_Data != NULL) {
+ if (!SDRM_OS2BN(RSA_E_Data, RSA_E_Len, crt->ctx->rsactx->e))
+ SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->e);
}
- if (RSA_D_Data != NULL)
- {
- if( !SDRM_OS2BN(RSA_D_Data, RSA_D_Len, crt->ctx->rsactx->d) ) {
- SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->d);
- }
+ if (RSA_D_Data != NULL) {
+ if (!SDRM_OS2BN(RSA_D_Data, RSA_D_Len, crt->ctx->rsactx->d))
+ SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->d);
}
- if (RSA_P_Data != NULL)
- {
- if( !SDRM_OS2BN(RSA_P_Data, RSA_P_Len, crt->ctx->rsactx->p) ) {
- SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->p);
- }
+ if (RSA_P_Data != NULL) {
+ if (!SDRM_OS2BN(RSA_P_Data, RSA_P_Len, crt->ctx->rsactx->p))
+ SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->p);
}
- if (RSA_Q_Data != NULL)
- {
- if( !SDRM_OS2BN(RSA_Q_Data, RSA_Q_Len, crt->ctx->rsactx->q) ) {
- SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->q);
- }
+ if (RSA_Q_Data != NULL) {
+ if (!SDRM_OS2BN(RSA_Q_Data, RSA_Q_Len, crt->ctx->rsactx->q))
+ SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->q);
}
- if (RSA_DmodP1_Data != NULL)
- {
- if( !SDRM_OS2BN(RSA_DmodP1_Data, RSA_DmodP1_Len, crt->ctx->rsactx->dmodp1) ) {
- SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->dmodp1);
- }
+ if (RSA_DmodP1_Data != NULL) {
+ if (!SDRM_OS2BN(RSA_DmodP1_Data, RSA_DmodP1_Len, crt->ctx->rsactx->dmodp1))
+ SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->dmodp1);
}
- if (RSA_DmodQ1_Data != NULL)
- {
- if( !SDRM_OS2BN(RSA_DmodQ1_Data, RSA_DmodQ1_Len, crt->ctx->rsactx->dmodq1) ) {
- SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->dmodq1);
- }
+ if (RSA_DmodQ1_Data != NULL) {
+ if (!SDRM_OS2BN(RSA_DmodQ1_Data, RSA_DmodQ1_Len, crt->ctx->rsactx->dmodq1))
+ SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->dmodq1);
}
- if (RSA_iQmodP_Data != NULL)
- {
- if( !SDRM_OS2BN(RSA_iQmodP_Data, RSA_iQmodP_Len, crt->ctx->rsactx->iqmodp) ) {
- SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->iqmodp);
- }
+ if (RSA_iQmodP_Data != NULL) {
+ if (!SDRM_OS2BN(RSA_iQmodP_Data, RSA_iQmodP_Len, crt->ctx->rsactx->iqmodp))
+ SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->iqmodp);
}
crt->ctx->rsactx->pm = PaddingMethod;
@@ -237,104 +222,97 @@ int SDRM_RSA_setNEDPQ(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
/*
- * @fn int SDRM_RSA_GenerateKey(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
- * cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len,
- * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len)
- * @brief generate and set RSA parameters
+ * @fn int SDRM_RSA_GenerateKey(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
+ * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
+ * cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len,
+ * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len)
+ * @brief generate and set RSA parameters
*
- * @param crt [in/out]rsa context
- * @param PaddingMethod [in]padding method
- * @param RSA_N_Data [out]n value
- * @param RSA_N_Len [out]byte-length of n
- * @param RSA_E_Data [out]e value
- * @param RSA_E_Len [out]byte-length of e
- * @param RSA_D_Data [out]d value
- * @param RSA_D_Len [out]byte-length of d
+ * @param crt [in/out]rsa context
+ * @param PaddingMethod [in]padding method
+ * @param RSA_N_Data [out]n value
+ * @param RSA_N_Len [out]byte-length of n
+ * @param RSA_E_Data [out]e value
+ * @param RSA_E_Len [out]byte-length of e
+ * @param RSA_D_Data [out]d value
+ * @param RSA_D_Len [out]byte-length of d
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if an argument is a null pointer
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if an argument is a null pointer
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
int SDRM_RSA_GenerateKey(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
- cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len,
- cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len)
+ cc_u8 *RSA_N_Data, cc_u32 *RSA_N_Len,
+ cc_u8 *RSA_E_Data, cc_u32 *RSA_E_Len,
+ cc_u8 *RSA_D_Data, cc_u32 *RSA_D_Len)
{
- cc_u32 Seed[4];
+ cc_u32 Seed[4];
SDRM_BIG_NUM *p, *q, *pi, *e, *temp1, *temp2;
- cc_u32 RSA_KeyByteLen = 0;
- int i, sp, t1;
+ cc_u32 RSA_KeyByteLen = 0;
+ int i, sp, t1;
cc_u8 *pbBuf = NULL;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
RSA_KeyByteLen = crt->ctx->rsactx->k;
t1 = (RSA_KeyByteLen * 4 - 1) % 32;
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 5);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 5);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- p = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE);
- q = SDRM_BN_Alloc((cc_u8*)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- pi = SDRM_BN_Alloc((cc_u8*)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- temp1 = SDRM_BN_Alloc((cc_u8*)pi + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ p = SDRM_BN_Alloc((cc_u8 *)pbBuf,
+ SDRM_RSA_BN_BUFSIZE);
+ q = SDRM_BN_Alloc((cc_u8 *)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ pi = SDRM_BN_Alloc((cc_u8 *)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ temp1 = SDRM_BN_Alloc((cc_u8 *)pi + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
e = crt->ctx->rsactx->e;
for (i = 0; i < 4; i++)
- {
Seed[i] = (rand() << 16) ^rand();
- }
//set security parameter for miller-rabin probabilistic primality test
if (RSA_KeyByteLen >= 256)
- {
sp = 3;
- }
+
else if (RSA_KeyByteLen >= 128)
- {
sp = 5;
- }
+
else if (RSA_KeyByteLen >= 30)
- {
sp = 15;
- }
+
else
- {
sp = 30;
- }
GEN_RND:
//Generate p
p->Length = (RSA_KeyByteLen + 7) / 8;
+
do {
- SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)p->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)p->pData);
p->pData[0] |= 1L;
p->pData[p->Length - 1] &= ~((-1L) << t1);
p->pData[p->Length - 1] |= (1L << t1);
- }
- while(SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME);
+ } while (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME);
//Generate q
q->Length = (RSA_KeyByteLen + 7) / 8;
+
do {
- SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)q->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)q->pData);
q->pData[0] |= 1L;
q->pData[q->Length - 1] &= ~((-1L) << t1);
q->pData[q->Length - 1] |= (1L << t1);
- }
- while(SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME);
+ } while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME);
-// SDRM_PrintBN("p", p);
-// SDRM_PrintBN("q", q);
+ // SDRM_PrintBN("p", p);
+ // SDRM_PrintBN("q", q);
//temp1 = (p - 1), temp2 = (q - 1)
SDRM_BN_Sub(temp1, p, BN_One);
@@ -347,51 +325,36 @@ GEN_RND:
//generate e
e->Length = (RSA_KeyByteLen + 3) / 4;
+
do {
do {
- SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 8 - 8, (cc_u8*)e->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 8 - 8, (cc_u8 *)e->pData);
e->pData[0] |= 0x01;
- }
- while(SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME);
- }
- while (SDRM_BN_Cmp(e, pi) >= 0);
+ } while (SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME);
+ } while (SDRM_BN_Cmp(e, pi) >= 0);
if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, pi) != CRYPTO_SUCCESS)
- {
goto GEN_RND;
- }
crt->ctx->rsactx->pm = PaddingMethod;
if (RSA_N_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->n, RSA_KeyByteLen, RSA_N_Data);
- }
if (RSA_N_Len != NULL)
- {
*RSA_N_Len = RSA_KeyByteLen;
- }
if (RSA_E_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->e, RSA_KeyByteLen, RSA_E_Data);
- }
if (RSA_E_Len != NULL)
- {
*RSA_E_Len = RSA_KeyByteLen;
- }
if (RSA_D_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->d, RSA_KeyByteLen, RSA_D_Data);
- }
if (RSA_D_Len != NULL)
- {
*RSA_D_Len = RSA_KeyByteLen;
- }
free(pbBuf);
@@ -401,254 +364,235 @@ GEN_RND:
}
int SDRM_RSA_GenerateKeyforCRT(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- cc_u8* RSA_E_Data, cc_u32 RSA_E_Len,
- cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
- cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len,
- cc_u8* RSA_P_Data, cc_u32 *RSA_P_Len,
- cc_u8* RSA_Q_Data, cc_u32 *RSA_Q_Len,
- cc_u8* RSA_DP_Data, cc_u32 *RSA_DP_Len,
- cc_u8* RSA_DQ_Data, cc_u32 *RSA_DQ_Len,
- cc_u8* RSA_QP_Data, cc_u32 *RSA_QP_Len)
+ cc_u8 *RSA_E_Data, cc_u32 RSA_E_Len,
+ cc_u8 *RSA_N_Data, cc_u32 *RSA_N_Len,
+ cc_u8 *RSA_D_Data, cc_u32 *RSA_D_Len,
+ cc_u8 *RSA_P_Data, cc_u32 *RSA_P_Len,
+ cc_u8 *RSA_Q_Data, cc_u32 *RSA_Q_Len,
+ cc_u8 *RSA_DP_Data, cc_u32 *RSA_DP_Len,
+ cc_u8 *RSA_DQ_Data, cc_u32 *RSA_DQ_Len,
+ cc_u8 *RSA_QP_Data, cc_u32 *RSA_QP_Len)
{
- cc_u32 Seed[4];
+ cc_u32 Seed[4];
SDRM_BIG_NUM *p, *q, *h, *e, *p1, *q1;
- cc_u32 RSA_KeyByteLen = 0;
- int i, sp, t1;
+ cc_u32 RSA_KeyByteLen = 0;
+ int i, sp, t1;
cc_u8 *pbBuf = NULL;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL)) {
+
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL))
return CRYPTO_NULL_POINTER;
- }
+
RSA_KeyByteLen = crt->ctx->rsactx->k;
t1 = (RSA_KeyByteLen * 4 - 1) % 32;
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 5);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 5);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- p = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE);
- q = SDRM_BN_Alloc((cc_u8*)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- h = SDRM_BN_Alloc((cc_u8*)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- p1 = SDRM_BN_Alloc((cc_u8*)h + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- q1 = SDRM_BN_Alloc((cc_u8*)p1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+
+ p = SDRM_BN_Alloc((cc_u8 *)pbBuf,
+ SDRM_RSA_BN_BUFSIZE);
+ q = SDRM_BN_Alloc((cc_u8 *)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ h = SDRM_BN_Alloc((cc_u8 *)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ p1 = SDRM_BN_Alloc((cc_u8 *)h + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ q1 = SDRM_BN_Alloc((cc_u8 *)p1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
e = crt->ctx->rsactx->e;
+
for (i = 0; i < 4; i++)
- {
Seed[i] = (rand() << 16) ^ rand();
- }
+
if (RSA_KeyByteLen >= 256)
- {
sp = 3;
- }
+
else if (RSA_KeyByteLen >= 128)
- {
sp = 5;
- }
+
else if (RSA_KeyByteLen >= 30)
- {
sp = 15;
- }
+
else
- {
sp = 30;
- }
+
GEN_RND:
p->Length = (RSA_KeyByteLen + 7) / 8;
+
do {
- SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)p->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)p->pData);
p->pData[0] |= 1L;
p->pData[p->Length - 1] &= ~((-1L) << t1);
p->pData[p->Length - 1] |= (1L << t1);
- }
- while(SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME);
+ } while (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME);
+
q->Length = (RSA_KeyByteLen + 7) / 8;
+
do {
- SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)q->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)q->pData);
q->pData[0] |= 1L;
q->pData[q->Length - 1] &= ~((-1L) << t1);
q->pData[q->Length - 1] |= (1L << t1);
- }
- while(SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME);
+ } while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME);
+
SDRM_BN_Sub(p1, p, BN_One);
SDRM_BN_Sub(q1, q, BN_One);
SDRM_BN_Mul(h, p1, q1);
SDRM_OS2BN(RSA_E_Data, RSA_E_Len, e);
- if ((SDRM_BN_CheckRelativelyPrime(e, h) != CRYPTO_ISPRIME) || (SDRM_BN_Cmp(e, h) >= 0))
- {
+
+ if ((SDRM_BN_CheckRelativelyPrime(e, h) != CRYPTO_ISPRIME) ||
+ (SDRM_BN_Cmp(e, h) >= 0))
goto GEN_RND;
- }
+
SDRM_BN_Mul(crt->ctx->rsactx->n, p, q);
+
if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, h) != CRYPTO_SUCCESS)
- {
goto GEN_RND;
- }
- if (SDRM_BN_ModRed(crt->ctx->rsactx->dmodp1, crt->ctx->rsactx->d, p1) != CRYPTO_SUCCESS)
- {
+
+ if (SDRM_BN_ModRed(crt->ctx->rsactx->dmodp1, crt->ctx->rsactx->d,
+ p1) != CRYPTO_SUCCESS)
goto GEN_RND;
- }
- if (SDRM_BN_ModRed(crt->ctx->rsactx->dmodq1, crt->ctx->rsactx->d, q1) != CRYPTO_SUCCESS)
- {
+
+ if (SDRM_BN_ModRed(crt->ctx->rsactx->dmodq1, crt->ctx->rsactx->d,
+ q1) != CRYPTO_SUCCESS)
goto GEN_RND;
- }
+
if (SDRM_BN_ModInv(crt->ctx->rsactx->iqmodp, q, p) != CRYPTO_SUCCESS)
- {
goto GEN_RND;
- }
+
crt->ctx->rsactx->pm = PaddingMethod;
+
if (RSA_N_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->n, RSA_KeyByteLen, RSA_N_Data);
- }
+
if (RSA_N_Len != NULL)
- {
*RSA_N_Len = RSA_KeyByteLen;
- }
+
if (RSA_D_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->d, RSA_KeyByteLen, RSA_D_Data);
- }
+
if (RSA_D_Len != NULL)
- {
*RSA_D_Len = RSA_KeyByteLen;
- }
+
if (RSA_P_Data != NULL)
- {
- SDRM_I2OSP(p, RSA_KeyByteLen/2, RSA_P_Data);
- }
+ SDRM_I2OSP(p, RSA_KeyByteLen / 2, RSA_P_Data);
+
if (RSA_P_Len != NULL)
- {
- *RSA_P_Len = RSA_KeyByteLen/2;
- }
+ *RSA_P_Len = RSA_KeyByteLen / 2;
+
if (RSA_Q_Data != NULL)
- {
- SDRM_I2OSP(q, RSA_KeyByteLen/2, RSA_Q_Data);
- }
+ SDRM_I2OSP(q, RSA_KeyByteLen / 2, RSA_Q_Data);
+
if (RSA_Q_Len != NULL)
- {
- *RSA_Q_Len = RSA_KeyByteLen/2;
- }
+ *RSA_Q_Len = RSA_KeyByteLen / 2;
+
if (RSA_DP_Data != NULL)
- {
- SDRM_I2OSP(crt->ctx->rsactx->dmodp1, RSA_KeyByteLen/2, RSA_DP_Data);
- }
+ SDRM_I2OSP(crt->ctx->rsactx->dmodp1, RSA_KeyByteLen / 2, RSA_DP_Data);
+
if (RSA_DP_Len != NULL)
- {
- *RSA_DP_Len = RSA_KeyByteLen/2;
- }
+ *RSA_DP_Len = RSA_KeyByteLen / 2;
+
if (RSA_DQ_Data != NULL)
- {
- SDRM_I2OSP(crt->ctx->rsactx->dmodq1, RSA_KeyByteLen/2, RSA_DQ_Data);
- }
+ SDRM_I2OSP(crt->ctx->rsactx->dmodq1, RSA_KeyByteLen / 2, RSA_DQ_Data);
+
if (RSA_DQ_Len != NULL)
- {
- *RSA_DQ_Len = RSA_KeyByteLen/2;
- }
+ *RSA_DQ_Len = RSA_KeyByteLen / 2;
+
if (RSA_QP_Data != NULL)
- {
- SDRM_I2OSP(crt->ctx->rsactx->iqmodp, RSA_KeyByteLen/2, RSA_QP_Data);
- }
+ SDRM_I2OSP(crt->ctx->rsactx->iqmodp, RSA_KeyByteLen / 2, RSA_QP_Data);
+
if (RSA_QP_Len != NULL)
- {
- *RSA_QP_Len = RSA_KeyByteLen/2;
- }
+ *RSA_QP_Len = RSA_KeyByteLen / 2;
+
free(pbBuf);
crt->ctx->rsactx->crt_operation = 0;
return CRYPTO_SUCCESS;
}
/*
- * @fn int SDRM_RSA_GenerateND(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len,
- * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
- * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len)
- * @brief generate and set RSA parameters with specfied e
+ * @fn int SDRM_RSA_GenerateND(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
+ * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len,
+ * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
+ * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len)
+ * @brief generate and set RSA parameters with specfied e
*
- * @param crt [in/out]rsa context
- * @param PaddingMethod [in]padding method
- * @param RSA_E_Data [in]e value
- * @param RSA_E_Len [in]byte-length of e
- * @param RSA_N_Data [out]n value
- * @param RSA_N_Len [out]byte-length of n
- * @param RSA_D_Data [out]d value
- * @param RSA_D_Len [out]byte-length of d
+ * @param crt [in/out]rsa context
+ * @param PaddingMethod [in]padding method
+ * @param RSA_E_Data [in]e value
+ * @param RSA_E_Len [in]byte-length of e
+ * @param RSA_N_Data [out]n value
+ * @param RSA_N_Len [out]byte-length of n
+ * @param RSA_D_Data [out]d value
+ * @param RSA_D_Len [out]byte-length of d
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if an argument is a null pointer
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if an argument is a null pointer
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
int SDRM_RSA_GenerateND(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- cc_u8* RSA_E_Data, cc_u32 RSA_E_Len,
- cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
- cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len)
+ cc_u8 *RSA_E_Data, cc_u32 RSA_E_Len,
+ cc_u8 *RSA_N_Data, cc_u32 *RSA_N_Len,
+ cc_u8 *RSA_D_Data, cc_u32 *RSA_D_Len)
{
- cc_u32 Seed[4];
+ cc_u32 Seed[4];
SDRM_BIG_NUM *p, *q, *pi, *e, *temp1, *temp2;
- cc_u32 RSA_KeyByteLen = 0;
- int i, sp, t1;
+ cc_u32 RSA_KeyByteLen = 0;
+ int i, sp, t1;
cc_u8 *pbBuf = NULL;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL)) {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL))
return CRYPTO_NULL_POINTER;
- }
RSA_KeyByteLen = crt->ctx->rsactx->k;
t1 = (RSA_KeyByteLen * 4 - 1) % 32;
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 5);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 5);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- p = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE);
- q = SDRM_BN_Alloc((cc_u8*)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- pi = SDRM_BN_Alloc((cc_u8*)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- temp1 = SDRM_BN_Alloc((cc_u8*)pi + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ p = SDRM_BN_Alloc((cc_u8 *)pbBuf,
+ SDRM_RSA_BN_BUFSIZE);
+ q = SDRM_BN_Alloc((cc_u8 *)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ pi = SDRM_BN_Alloc((cc_u8 *)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ temp1 = SDRM_BN_Alloc((cc_u8 *)pi + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
e = crt->ctx->rsactx->e;
for (i = 0; i < 4; i++)
- {
Seed[i] = (rand() << 16) ^ rand();
- }
//set security parameter for miller-rabin probabilistic primality test
if (RSA_KeyByteLen >= 256)
- {
sp = 3;
- }
+
else if (RSA_KeyByteLen >= 128)
- {
sp = 5;
- }
+
else if (RSA_KeyByteLen >= 30)
- {
sp = 15;
- }
+
else
- {
sp = 30;
- }
GEN_RND:
//Generate p
p->Length = (RSA_KeyByteLen + 7) / 8;
+
do {
- SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)p->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)p->pData);
p->pData[0] |= 1L;
p->pData[p->Length - 1] &= ~((-1L) << t1);
p->pData[p->Length - 1] |= (1L << t1);
- }
- while(SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME);
+ } while (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME);
//Generate q
q->Length = (RSA_KeyByteLen + 7) / 8;
+
do {
- SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)q->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)q->pData);
q->pData[0] |= 1L;
q->pData[q->Length - 1] &= ~((-1L) << t1);
q->pData[q->Length - 1] |= (1L << t1);
- }
- while(SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME);
+ } while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME);
//temp1 = (p - 1), temp2 = (q - 1)
SDRM_BN_Sub(temp1, p, BN_One);
@@ -661,37 +605,27 @@ GEN_RND:
//check N for e
SDRM_OS2BN(RSA_E_Data, RSA_E_Len, e);
- if ((SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME) || (SDRM_BN_Cmp(e, pi) >= 0))
- {
+
+ if ((SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME) ||
+ (SDRM_BN_Cmp(e, pi) >= 0))
goto GEN_RND;
- }
if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, pi) != CRYPTO_SUCCESS)
- {
goto GEN_RND;
- }
crt->ctx->rsactx->pm = PaddingMethod;
if (RSA_N_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->n, RSA_KeyByteLen, RSA_N_Data);
- }
if (RSA_N_Len != NULL)
- {
*RSA_N_Len = RSA_KeyByteLen;
- }
if (RSA_D_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->d, RSA_KeyByteLen, RSA_D_Data);
- }
if (RSA_D_Len != NULL)
- {
*RSA_D_Len = RSA_KeyByteLen;
- }
free(pbBuf);
@@ -701,94 +635,89 @@ GEN_RND:
}
/*
- * @fn int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len,
- * cc_u8* RSA_P_Data, cc_u32 RSA_P_Len,
- * cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len,
- * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
- * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len)
- * @brief generate D with specfied p, q, d mod (p-1), d mod (q-1) and e
+ * @fn int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
+ * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len,
+ * cc_u8* RSA_P_Data, cc_u32 RSA_P_Len,
+ * cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len,
+ * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
+ * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len)
+ * @brief generate D with specfied p, q, d mod (p-1), d mod (q-1) and e
*
- * @param crt [in/out]rsa context
- * @param PaddingMethod [in]padding method
- * @param RSA_E_Data [in]e value
- * @param RSA_E_Len [in]byte-length of e
- * @param RSA_P_Data [in]n value
- * @param RSA_P_Len [in]byte-length of n
- * @param RSA_Q_Data [in]d value
- * @param RSA_Q_Len [in]byte-length of d
- * @param RSA_D_P_Data [in]d mod (p-1) value
- * @param RSA_D_P_Len [in]byte-length of d mod (p-1)
- * @param RSA_D_Q_Data [in]d mod (q-1) value
- * @param RSA_D_Q_Len [in]byte-length of d mod (q-1)
- * @param RSA_D_Data [out]d value
- * @param RSA_D_Len [out]byte-length of d
+ * @param crt [in/out]rsa context
+ * @param PaddingMethod [in]padding method
+ * @param RSA_E_Data [in]e value
+ * @param RSA_E_Len [in]byte-length of e
+ * @param RSA_P_Data [in]n value
+ * @param RSA_P_Len [in]byte-length of n
+ * @param RSA_Q_Data [in]d value
+ * @param RSA_Q_Len [in]byte-length of d
+ * @param RSA_D_P_Data [in]d mod (p-1) value
+ * @param RSA_D_P_Len [in]byte-length of d mod (p-1)
+ * @param RSA_D_Q_Data [in]d mod (q-1) value
+ * @param RSA_D_Q_Len [in]byte-length of d mod (q-1)
+ * @param RSA_D_Data [out]d value
+ * @param RSA_D_Len [out]byte-length of d
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if an argument is a null pointer
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if an argument is a null pointer
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- cc_u8* RSA_E_Data, cc_u32 RSA_E_Len,
- cc_u8* RSA_P_Data, cc_u32 RSA_P_Len,
- cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len,
- cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
- cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len)
+ cc_u8 *RSA_E_Data, cc_u32 RSA_E_Len,
+ cc_u8 *RSA_P_Data, cc_u32 RSA_P_Len,
+ cc_u8 *RSA_Q_Data, cc_u32 RSA_Q_Len,
+ cc_u8 *RSA_N_Data, cc_u32 *RSA_N_Len,
+ cc_u8 *RSA_D_Data, cc_u32 *RSA_D_Len)
{
SDRM_BIG_NUM *p, *q, *pi, *e, *temp1, *temp2;
- cc_u32 RSA_KeyByteLen = 0;
- int sp;
+ cc_u32 RSA_KeyByteLen = 0;
+ int sp;
cc_u8 *pbBuf = NULL;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
RSA_KeyByteLen = crt->ctx->rsactx->k;
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 5);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 5);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- p = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE);
- q = SDRM_BN_Alloc((cc_u8*)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- pi = SDRM_BN_Alloc((cc_u8*)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- temp1 = SDRM_BN_Alloc((cc_u8*)pi + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ p = SDRM_BN_Alloc((cc_u8 *)pbBuf,
+ SDRM_RSA_BN_BUFSIZE);
+ q = SDRM_BN_Alloc((cc_u8 *)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ pi = SDRM_BN_Alloc((cc_u8 *)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ temp1 = SDRM_BN_Alloc((cc_u8 *)pi + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
e = crt->ctx->rsactx->e;
//set security parameter for miller-rabin probabilistic primality test
if (RSA_KeyByteLen >= 256)
- {
sp = 3;
- }
+
else if (RSA_KeyByteLen >= 128)
- {
sp = 5;
- }
+
else if (RSA_KeyByteLen >= 30)
- {
sp = 15;
- }
+
else
- {
sp = 30;
- }
- SDRM_OS2BN((cc_u8*)RSA_P_Data, RSA_P_Len, p);
- if (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME)
- {
+ SDRM_OS2BN((cc_u8 *)RSA_P_Data, RSA_P_Len, p);
+
+ if (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
- SDRM_OS2BN((cc_u8*)RSA_Q_Data, RSA_Q_Len, q);
- if (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME)
- {
+ SDRM_OS2BN((cc_u8 *)RSA_Q_Data, RSA_Q_Len, q);
+
+ if (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
@@ -804,14 +733,14 @@ int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
//check N for e
SDRM_OS2BN(RSA_E_Data, RSA_E_Len, e);
- if ((SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME) || (SDRM_BN_Cmp(e, pi) >= 0))
- {
+
+ if ((SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME) ||
+ (SDRM_BN_Cmp(e, pi) >= 0)) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
- if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, pi) != CRYPTO_SUCCESS)
- {
+ if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, pi) != CRYPTO_SUCCESS) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
@@ -819,24 +748,16 @@ int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
crt->ctx->rsactx->pm = PaddingMethod;
if (RSA_N_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->n, RSA_KeyByteLen, RSA_N_Data);
- }
if (RSA_N_Len != NULL)
- {
*RSA_N_Len = RSA_KeyByteLen;
- }
if (RSA_D_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->d, RSA_KeyByteLen, RSA_D_Data);
- }
if (RSA_D_Len != NULL)
- {
*RSA_D_Len = RSA_KeyByteLen;
- }
free(pbBuf);
@@ -846,101 +767,94 @@ int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
}
/*
- * @fn int SDRM_RSA_GenerateKeyForCRT(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
- * cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len,
- * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len,
- * cc_u8* RSA_P_Data, cc_u32 *RSA_P_Len,
- * cc_u8* RSA_Q_Data, cc_u32 *RSA_Q_Len,
- * cc_u8* RSA_DmodP1_Data, cc_u32 *RSA_DmodP1_Len,
- * cc_u8* RSA_DmodQ1_Data, cc_u32 *RSA_DmodQ1_Len,
- * cc_u8* RSA_iQmodP_Data, cc_u32 *RSA_iQmodP_Len)
- * @brief generate and set RSA parameters for CRT
+ * @fn int SDRM_RSA_GenerateKeyForCRT(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
+ * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
+ * cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len,
+ * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len,
+ * cc_u8* RSA_P_Data, cc_u32 *RSA_P_Len,
+ * cc_u8* RSA_Q_Data, cc_u32 *RSA_Q_Len,
+ * cc_u8* RSA_DmodP1_Data, cc_u32 *RSA_DmodP1_Len,
+ * cc_u8* RSA_DmodQ1_Data, cc_u32 *RSA_DmodQ1_Len,
+ * cc_u8* RSA_iQmodP_Data, cc_u32 *RSA_iQmodP_Len)
+ * @brief generate and set RSA parameters for CRT
*
- * @param crt [in/out]rsa context
- * @param PaddingMethod [in]padding method
- * @param RSA_N_Data [out]n value
- * @param RSA_N_Len [out]byte-length of n
- * @param RSA_E_Data [out]e value
- * @param RSA_E_Len [out]byte-length of e
- * @param RSA_D_Data [out]d value
- * @param RSA_D_Len [out]byte-length of d
- * @param RSA_P_Len [out]byte-length of p
- * @param RSA_Q_Data [out]q value
- * @param RSA_Q_Len [out]byte-length of q
- * @param RSA_DmodP1_Data [out]d mod (p-1) value
- * @param RSA_DmodP1_Len [out]byte-length of d mod (p-1)
- * @param RSA_DmodQ1_Data [out]d mod (q-1) value
- * @param RSA_DmodQ1_Len [out]byte-length of d mod (q-1)
- * @param RSA_iQmodP_Data [out]q^(-1) mod p value
- * @param RSA_iQmodP_Len [out]byte-length of q^(-1) mod p
+ * @param crt [in/out]rsa context
+ * @param PaddingMethod [in]padding method
+ * @param RSA_N_Data [out]n value
+ * @param RSA_N_Len [out]byte-length of n
+ * @param RSA_E_Data [out]e value
+ * @param RSA_E_Len [out]byte-length of e
+ * @param RSA_D_Data [out]d value
+ * @param RSA_D_Len [out]byte-length of d
+ * @param RSA_P_Len [out]byte-length of p
+ * @param RSA_Q_Data [out]q value
+ * @param RSA_Q_Len [out]byte-length of q
+ * @param RSA_DmodP1_Data [out]d mod (p-1) value
+ * @param RSA_DmodP1_Len [out]byte-length of d mod (p-1)
+ * @param RSA_DmodQ1_Data [out]d mod (q-1) value
+ * @param RSA_DmodQ1_Len [out]byte-length of d mod (q-1)
+ * @param RSA_iQmodP_Data [out]q^(-1) mod p value
+ * @param RSA_iQmodP_Len [out]byte-length of q^(-1) mod p
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if an argument is a null pointer
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if an argument is a null pointer
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
int SDRM_RSA_GenNEDPQ(CryptoCoreContainer *crt, cc_u32 PaddingMethod,
- cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len,
- cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len,
- cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len,
- cc_u8* RSA_P_Data, cc_u32 *RSA_P_Len,
- cc_u8* RSA_Q_Data, cc_u32 *RSA_Q_Len,
- cc_u8* RSA_DmodP1_Data, cc_u32 *RSA_DmodP1_Len,
- cc_u8* RSA_DmodQ1_Data, cc_u32 *RSA_DmodQ1_Len,
- cc_u8* RSA_iQmodP_Data, cc_u32 *RSA_iQmodP_Len)
+ cc_u8 *RSA_N_Data, cc_u32 *RSA_N_Len,
+ cc_u8 *RSA_E_Data, cc_u32 *RSA_E_Len,
+ cc_u8 *RSA_D_Data, cc_u32 *RSA_D_Len,
+ cc_u8 *RSA_P_Data, cc_u32 *RSA_P_Len,
+ cc_u8 *RSA_Q_Data, cc_u32 *RSA_Q_Len,
+ cc_u8 *RSA_DmodP1_Data, cc_u32 *RSA_DmodP1_Len,
+ cc_u8 *RSA_DmodQ1_Data, cc_u32 *RSA_DmodQ1_Len,
+ cc_u8 *RSA_iQmodP_Data, cc_u32 *RSA_iQmodP_Len)
{
- cc_u32 Seed[4];
+ cc_u32 Seed[4];
SDRM_BIG_NUM *p, *q, *pi, *e, *temp1, *temp2;
- cc_u32 RSA_KeyByteLen = 0;
- int i, sp, t1;
+ cc_u32 RSA_KeyByteLen = 0;
+ int i, sp, t1;
cc_u8 *pbBuf = NULL;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
RSA_KeyByteLen = crt->ctx->rsactx->k;
t1 = (RSA_KeyByteLen * 4 - 1) % 32;
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 3);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 3);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- pi = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE);
- temp1 = SDRM_BN_Alloc((cc_u8*)pi + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ pi = SDRM_BN_Alloc((cc_u8 *)pbBuf,
+ SDRM_RSA_BN_BUFSIZE);
+ temp1 = SDRM_BN_Alloc((cc_u8 *)pi + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
e = crt->ctx->rsactx->e;
p = crt->ctx->rsactx->p;
q = crt->ctx->rsactx->q;
for (i = 0; i < 4; i++)
- {
Seed[i] = (rand() << 16) ^ rand();
- }
//set security parameter for miller-rabin probabilistic primality test
if (RSA_KeyByteLen >= 256)
- {
sp = 3;
- }
+
else if (RSA_KeyByteLen >= 128)
- {
sp = 5;
- }
+
else if (RSA_KeyByteLen >= 30)
- {
sp = 15;
- }
+
else
- {
sp = 30;
- }
GEN_RND:
@@ -948,26 +862,24 @@ GEN_RND:
p->Length = (RSA_KeyByteLen + 7) / 8;
do {
- SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)p->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)p->pData);
p->pData[0] |= 1L;
p->pData[p->Length - 1] &= ~((-1L) << t1);
p->pData[p->Length - 1] |= (1L << t1);
- }
- while(SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME);
+ } while (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME);
//Generate q
q->Length = (RSA_KeyByteLen + 7) / 8;
do {
- SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)q->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)q->pData);
q->pData[0] |= 1L;
q->pData[q->Length - 1] &= ~((-1L) << t1);
q->pData[q->Length - 1] |= (1L << t1);
- }
- while(SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME);
+ } while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME);
-// SDRM_PrintBN("p", p);
-// SDRM_PrintBN("q", q);
+ // SDRM_PrintBN("p", p);
+ // SDRM_PrintBN("q", q);
//temp1 = (p - 1), temp2 = (q - 1)
@@ -981,19 +893,16 @@ GEN_RND:
//generate e
e->Length = (RSA_KeyByteLen + 3) / 4;
+
do {
do {
- SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 8 - 8, (cc_u8*)e->pData);
+ SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 8 - 8, (cc_u8 *)e->pData);
e->pData[0] |= 0x01;
- }
- while(SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME);
- }
- while (SDRM_BN_Cmp(e, pi) >= 0);
+ } while (SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME);
+ } while (SDRM_BN_Cmp(e, pi) >= 0);
if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, pi) != CRYPTO_SUCCESS)
- {
goto GEN_RND;
- }
//calc dmodp1 = d mod (p - 1)
SDRM_BN_ModRed(crt->ctx->rsactx->dmodp1, crt->ctx->rsactx->d, temp1);
@@ -1007,84 +916,52 @@ GEN_RND:
crt->ctx->rsactx->pm = PaddingMethod;
if (RSA_N_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->n, RSA_KeyByteLen, RSA_N_Data);
- }
if (RSA_N_Len != NULL)
- {
*RSA_N_Len = RSA_KeyByteLen;
- }
if (RSA_E_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->e, RSA_KeyByteLen, RSA_E_Data);
- }
if (RSA_E_Len != NULL)
- {
*RSA_E_Len = RSA_KeyByteLen;
- }
if (RSA_D_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->d, RSA_KeyByteLen, RSA_D_Data);
- }
if (RSA_D_Len != NULL)
- {
*RSA_D_Len = RSA_KeyByteLen;
- }
if (RSA_P_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->p, RSA_KeyByteLen / 2, RSA_P_Data);
- }
if (RSA_P_Len != NULL)
- {
*RSA_P_Len = RSA_KeyByteLen / 2;
- }
if (RSA_Q_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->q, RSA_KeyByteLen / 2, RSA_Q_Data);
- }
if (RSA_Q_Len != NULL)
- {
*RSA_Q_Len = RSA_KeyByteLen / 2;
- }
if (RSA_DmodP1_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->dmodp1, RSA_KeyByteLen / 2, RSA_DmodP1_Data);
- }
if (RSA_DmodP1_Len != NULL)
- {
*RSA_DmodP1_Len = RSA_KeyByteLen / 2;
- }
if (RSA_DmodQ1_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->dmodq1, RSA_KeyByteLen / 2, RSA_DmodQ1_Data);
- }
if (RSA_DmodQ1_Len != NULL)
- {
*RSA_DmodQ1_Len = RSA_KeyByteLen / 2;
- }
if (RSA_iQmodP_Data != NULL)
- {
SDRM_I2OSP(crt->ctx->rsactx->iqmodp, RSA_KeyByteLen / 2, RSA_iQmodP_Data);
- }
if (RSA_iQmodP_Len != NULL)
- {
*RSA_iQmodP_Len = RSA_KeyByteLen / 2;
- }
free(pbBuf);
@@ -1094,74 +971,75 @@ GEN_RND:
}
/*
- * @fn int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen)
- * @brief RSA Encryption
+ * @fn int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen)
+ * @brief RSA Encryption
*
- * @param crt [in]rsa context
- * @param in [in]message to encrypt
- * @param inLen [in]byte-length of in
- * @param out [out]encrypted message
- * @param outLen [out]byte-length of out
+ * @param crt [in]rsa context
+ * @param in [in]message to encrypt
+ * @param inLen [in]byte-length of in
+ * @param out [out]encrypted message
+ * @param outLen [out]byte-length of out
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if an argument is a null pointer
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if an argument is a null pointer
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
-int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen)
+int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen,
+ cc_u8 *out, cc_u32 *outLen)
{
SDRM_BIG_NUM *BN_pMsg, *BN_Cipher;
- int retVal = CRYPTO_ERROR;
- cc_u32 RSA_KeyByteLen = 0;
- cc_u8 *pbBuf = NULL;
+ int retVal = CRYPTO_ERROR;
+ cc_u32 RSA_KeyByteLen = 0;
+ cc_u8 *pbBuf = NULL;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (in == NULL) || (out == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) ||
+ (in == NULL) || (out == NULL))
return CRYPTO_NULL_POINTER;
- }
RSA_KeyByteLen = crt->ctx->rsactx->k;
+
if (inLen > RSA_KeyByteLen)
- {
return CRYPTO_MSG_TOO_LONG;
- }
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_pMsg = SDRM_BN_Alloc((cc_u8*)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE);
- BN_Cipher = SDRM_BN_Alloc((cc_u8*)BN_pMsg + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ BN_pMsg = SDRM_BN_Alloc((cc_u8 *)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE);
+ BN_Cipher = SDRM_BN_Alloc((cc_u8 *)BN_pMsg + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
//Padding the message
- switch(SDRM_LOW_HALF(crt->ctx->rsactx->pm))
- {
- case ID_RSAES_PKCS15 :
- retVal = SDRM_Enpad_Rsaes_pkcs15(pbBuf, in, inLen, RSA_KeyByteLen);
- break;
- case ID_RSAES_OAEP :
- retVal = SDRM_Enpad_Rsaes_oaep(pbBuf, in, inLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
- break;
- case ID_NO_PADDING :
- if( inLen != RSA_KeyByteLen) // add by guoxing.xu 20140919
- {
- free(pbBuf);
- return CRYPTO_INVALID_ARGUMENT;
- }
- memset(pbBuf, 0x00, RSA_KeyByteLen - inLen);
- memcpy(pbBuf + RSA_KeyByteLen - inLen, in, inLen);
- retVal= CRYPTO_SUCCESS;// add by guoxing.xu 20140919
- break;
- default :
+ switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) {
+ case ID_RSAES_PKCS15:
+ retVal = SDRM_Enpad_Rsaes_pkcs15(pbBuf, in, inLen, RSA_KeyByteLen);
+ break;
+
+ case ID_RSAES_OAEP:
+ retVal = SDRM_Enpad_Rsaes_oaep(pbBuf, in, inLen, RSA_KeyByteLen,
+ SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
+ break;
+
+ case ID_NO_PADDING:
+ if (inLen != RSA_KeyByteLen) { // add by guoxing.xu 20140919
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
+ }
+
+ memset(pbBuf, 0x00, RSA_KeyByteLen - inLen);
+ memcpy(pbBuf + RSA_KeyByteLen - inLen, in, inLen);
+ retVal = CRYPTO_SUCCESS; // add by guoxing.xu 20140919
+ break;
+
+ default:
+ free(pbBuf);
+ return CRYPTO_INVALID_ARGUMENT;
}
-// SDRM_PrintBN("ENPADDED Text : ", BN_pMsg);
+ // SDRM_PrintBN("ENPADDED Text : ", BN_pMsg);
- if (retVal != CRYPTO_SUCCESS)
- {
+ if (retVal != CRYPTO_SUCCESS) {
free(pbBuf);
return retVal;
}
@@ -1170,13 +1048,14 @@ int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *o
//RSA Encryption by modular exponent
#ifndef _OP64_NOTSUPPORTED
- retVal = SDRM_BN_ModExp2(BN_Cipher, BN_pMsg, crt->ctx->rsactx->e, crt->ctx->rsactx->n);
+ retVal = SDRM_BN_ModExp2(BN_Cipher, BN_pMsg, crt->ctx->rsactx->e,
+ crt->ctx->rsactx->n);
#else
- retVal = SDRM_BN_ModExp(BN_Cipher, BN_pMsg, crt->ctx->rsactx->e, crt->ctx->rsactx->n);
-#endif //_OP64_NOTSUPPORTED
+ retVal = SDRM_BN_ModExp(BN_Cipher, BN_pMsg, crt->ctx->rsactx->e,
+ crt->ctx->rsactx->n);
+#endif //_OP64_NOTSUPPORTED
- if (retVal != CRYPTO_SUCCESS)
- {
+ if (retVal != CRYPTO_SUCCESS) {
free(pbBuf);
return retVal;
}
@@ -1184,9 +1063,7 @@ int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *o
SDRM_I2OSP(BN_Cipher, RSA_KeyByteLen, out);
if (outLen != NULL)
- {
*outLen = RSA_KeyByteLen;
- }
memset(pbBuf, 0x00, SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
free(pbBuf);
@@ -1195,58 +1072,58 @@ int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *o
}
/*
- * @fn int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen)
- * @brief RSA Decryption
+ * @fn int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen)
+ * @brief RSA Decryption
*
- * @param crt [in]rsa context
- * @param in [in]message to decrypt
- * @param inLen [in]byte-length of in
- * @param out [out]decrypted message
- * @param outLen [out]byte-length of out
+ * @param crt [in]rsa context
+ * @param in [in]message to decrypt
+ * @param inLen [in]byte-length of in
+ * @param out [out]decrypted message
+ * @param outLen [out]byte-length of out
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if an argument is a null pointer
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if an argument is a null pointer
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
-int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen)
+int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen,
+ cc_u8 *out, cc_u32 *outLen)
{
- SDRM_BIG_NUM *BN_dMsg, *BN_Src;
- int retVal;
- cc_u32 plainLen;
- cc_u32 RSA_KeyByteLen = 0;
- cc_u8 *pbBuf = NULL;
-
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (in == NULL) || (out == NULL))
- {
+ SDRM_BIG_NUM *BN_dMsg, *BN_Src;
+ int retVal;
+ cc_u32 plainLen;
+ cc_u32 RSA_KeyByteLen = 0;
+ cc_u8 *pbBuf = NULL;
+
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) ||
+ (in == NULL) || (out == NULL))
return CRYPTO_NULL_POINTER;
- }
RSA_KeyByteLen = crt->ctx->rsactx->k;
+
if (inLen > RSA_KeyByteLen)
- {
return CRYPTO_MSG_TOO_LONG;
- }
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_dMsg = SDRM_BN_Alloc((cc_u8*)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE);
- BN_Src = SDRM_BN_Alloc((cc_u8*)BN_dMsg + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ BN_dMsg = SDRM_BN_Alloc((cc_u8 *)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE);
+ BN_Src = SDRM_BN_Alloc((cc_u8 *)BN_dMsg + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
SDRM_OS2BN(in, inLen, BN_Src);
//RSA Decryption by modular exponent
#ifndef _OP64_NOTSUPPORTED
- retVal = SDRM_BN_ModExp2(BN_dMsg, BN_Src, crt->ctx->rsactx->d, crt->ctx->rsactx->n);
+ retVal = SDRM_BN_ModExp2(BN_dMsg, BN_Src, crt->ctx->rsactx->d,
+ crt->ctx->rsactx->n);
#else
- retVal = SDRM_BN_ModExp(BN_dMsg, BN_Src, crt->ctx->rsactx->d, crt->ctx->rsactx->n);
-#endif //_OP64_NOTSUPPORTED
+ retVal = SDRM_BN_ModExp(BN_dMsg, BN_Src, crt->ctx->rsactx->d,
+ crt->ctx->rsactx->n);
+#endif //_OP64_NOTSUPPORTED
- if (retVal != CRYPTO_SUCCESS)
- {
+ if (retVal != CRYPTO_SUCCESS) {
free(pbBuf);
return retVal;
}
@@ -1254,34 +1131,35 @@ int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *o
SDRM_I2OSP(BN_dMsg, RSA_KeyByteLen, pbBuf);
//Remove Padding from message
- switch(SDRM_LOW_HALF(crt->ctx->rsactx->pm))
- {
- case ID_RSAES_PKCS15 :
- retVal = SDRM_Depad_Rsaes_pkcs15(out, &plainLen, pbBuf, RSA_KeyByteLen, RSA_KeyByteLen);
- break;
- case ID_RSAES_OAEP :
- retVal = SDRM_Depad_Rsaes_oaep(out, &plainLen, pbBuf, RSA_KeyByteLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
- break;
- case ID_NO_PADDING :
- memcpy(out, pbBuf, RSA_KeyByteLen);
- plainLen = RSA_KeyByteLen;
- retVal = CRYPTO_SUCCESS;
- break;
- default :
- free(pbBuf);
- return CRYPTO_INVALID_ARGUMENT;
+ switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) {
+ case ID_RSAES_PKCS15:
+ retVal = SDRM_Depad_Rsaes_pkcs15(out, &plainLen, pbBuf, RSA_KeyByteLen,
+ RSA_KeyByteLen);
+ break;
+
+ case ID_RSAES_OAEP:
+ retVal = SDRM_Depad_Rsaes_oaep(out, &plainLen, pbBuf, RSA_KeyByteLen,
+ RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
+ break;
+
+ case ID_NO_PADDING:
+ memcpy(out, pbBuf, RSA_KeyByteLen);
+ plainLen = RSA_KeyByteLen;
+ retVal = CRYPTO_SUCCESS;
+ break;
+
+ default:
+ free(pbBuf);
+ return CRYPTO_INVALID_ARGUMENT;
}
- if (retVal != CRYPTO_SUCCESS)
- {
+ if (retVal != CRYPTO_SUCCESS) {
free(pbBuf);
return retVal;
}
if (outLen != NULL)
- {
*outLen = plainLen;
- }
memset(pbBuf, 0x00, SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
free(pbBuf);
@@ -1290,166 +1168,165 @@ int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *o
}
/*
- * @fn int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen)
- * @brief RSA Decryption using CRT
+ * @fn int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen)
+ * @brief RSA Decryption using CRT
*
- * @param crt [in]rsa context
- * @param in [in]message to decrypt
- * @param inLen [in]byte-length of in
- * @param out [out]decrypted message
- * @param outLen [out]byte-length of out
+ * @param crt [in]rsa context
+ * @param in [in]message to decrypt
+ * @param inLen [in]byte-length of in
+ * @param out [out]decrypted message
+ * @param outLen [out]byte-length of out
*
- * @return CRYPTO_SUCCESS if no error is occured
- * \n CRYPTO_NULL_POINTER if an argument is a null pointer
- * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
+ * @return CRYPTO_SUCCESS if no error is occured
+ * \n CRYPTO_NULL_POINTER if an argument is a null pointer
+ * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed
*/
-int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen)
+int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen,
+ cc_u8 *out, cc_u32 *outLen)
{
- SDRM_BIG_NUM *BN_dMsg, *BN_Src;
- int retVal;
- cc_u32 plainLen;
- cc_u32 RSA_KeyByteLen = 0;
+ SDRM_BIG_NUM *BN_dMsg, *BN_Src;
+ int retVal;
+ cc_u32 plainLen;
+ cc_u32 RSA_KeyByteLen = 0;
SDRM_BIG_NUM *pi, *temp1, *temp2, *m1, *m2, *h;
- cc_u8 *pbBuf = NULL;
+ cc_u8 *pbBuf = NULL;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (in == NULL) || (out == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) ||
+ (in == NULL) || (out == NULL))
return CRYPTO_NULL_POINTER;
- }
if (crt->ctx->rsactx->crt_operation != 1)
- {
return CRYPTO_INVALID_ARGUMENT;
- }
RSA_KeyByteLen = crt->ctx->rsactx->k;
+
if (inLen > RSA_KeyByteLen)
- {
return CRYPTO_MSG_TOO_LONG;
- }
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 8 + RSA_KeyByteLen);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 8 + RSA_KeyByteLen);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_dMsg = SDRM_BN_Alloc((cc_u8*)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE);
- BN_Src = SDRM_BN_Alloc((cc_u8*)BN_dMsg + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- pi = SDRM_BN_Alloc((cc_u8*)BN_Src + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- temp1 = SDRM_BN_Alloc((cc_u8*)pi + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- m1 = SDRM_BN_Alloc((cc_u8*)temp2 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- m2 = SDRM_BN_Alloc((cc_u8*)m1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- h = SDRM_BN_Alloc((cc_u8*)m2 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ BN_dMsg = SDRM_BN_Alloc((cc_u8 *)pbBuf + RSA_KeyByteLen,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_Src = SDRM_BN_Alloc((cc_u8 *)BN_dMsg + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ pi = SDRM_BN_Alloc((cc_u8 *)BN_Src + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ temp1 = SDRM_BN_Alloc((cc_u8 *)pi + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ m1 = SDRM_BN_Alloc((cc_u8 *)temp2 + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ m2 = SDRM_BN_Alloc((cc_u8 *)m1 + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ h = SDRM_BN_Alloc((cc_u8 *)m2 + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
SDRM_OS2BN(in, inLen, BN_Src);
//RSA Decryption by CRT
/*
- dp = d mod (p - 1)
- dq = d mod (q - 1)
- qInv = (1/q) mod p where p > q
+ dp = d mod (p - 1)
+ dq = d mod (q - 1)
+ qInv = (1/q) mod p where p > q
=>
- m1 = c^dp mod p
- m2 = c^dq mod q
- h = qInv(m1 - m2) mod p if (m1 >= m2) or h = qInv(m1 + p - m2) mod p if (m1 < m2)
- m = m2 + hq
+ m1 = c^dp mod p
+ m2 = c^dq mod q
+ h = qInv(m1 - m2) mod p if (m1 >= m2) or h = qInv(m1 + p - m2) mod p if (m1 < m2)
+ m = m2 + hq
*/
// Prepare variables
// 1. dP = d mod (p - 1)
- // dP is already set when SDRM_RSA_setNEDPQ
+ // dP is already set when SDRM_RSA_setNEDPQ
// 2. dQ = d mod (q - 1)
- // dQ is already set when SDRM_RSA_setNEDPQ
+ // dQ is already set when SDRM_RSA_setNEDPQ
// 3. qInv = (1/q) mod p where p > q
- // qInv is already set when SDRM_RSA_setNEDPQ
+ // qInv is already set when SDRM_RSA_setNEDPQ
// Computation
// 4. m1 = c^dP mod p
- if(SDRM_BN_ModExp2(m1, BN_Src, crt->ctx->rsactx->dmodp1, crt->ctx->rsactx->p))
- {
+ if (SDRM_BN_ModExp2(m1, BN_Src, crt->ctx->rsactx->dmodp1,
+ crt->ctx->rsactx->p)) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
// 5. m2 = c^dQ mod q
- if(SDRM_BN_ModExp2(m2, BN_Src, crt->ctx->rsactx->dmodq1, crt->ctx->rsactx->q))
- {
+ if (SDRM_BN_ModExp2(m2, BN_Src, crt->ctx->rsactx->dmodq1,
+ crt->ctx->rsactx->q)) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
// 6. h = qInv(m1 - m2) mod p if (m1 >= m2) or h = qInv(m1 + p - m2) mod p if (m1 < m2)
- if(SDRM_BN_Cmp(m1, m2) < 0)
- {
- if(SDRM_BN_Add(m1, m1, crt->ctx->rsactx->p))
- {
+ if (SDRM_BN_Cmp(m1, m2) < 0) {
+ if (SDRM_BN_Add(m1, m1, crt->ctx->rsactx->p)) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
}
- if(SDRM_BN_Sub(m1, m1, m2))
- {
+ if (SDRM_BN_Sub(m1, m1, m2)) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
- if(SDRM_BN_ModMul(h, crt->ctx->rsactx->iqmodp, m1, crt->ctx->rsactx->p))
- {
+ if (SDRM_BN_ModMul(h, crt->ctx->rsactx->iqmodp, m1, crt->ctx->rsactx->p)) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
// 7. m = m2 + hq
- if(SDRM_BN_Mul(h, h, crt->ctx->rsactx->q))
- {
+ if (SDRM_BN_Mul(h, h, crt->ctx->rsactx->q)) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
- if(SDRM_BN_Add(BN_dMsg, m2, h))
- {
+ if (SDRM_BN_Add(BN_dMsg, m2, h)) {
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
-// SDRM_PrintBN("OAEP Text : ", BN_dMsg);
+ // SDRM_PrintBN("OAEP Text : ", BN_dMsg);
SDRM_I2OSP(BN_dMsg, RSA_KeyByteLen, pbBuf);
//Remove Padding from message
- switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm))
- {
+ switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) {
case ID_RSAES_PKCS15:
- retVal = SDRM_Depad_Rsaes_pkcs15(out, &plainLen, pbBuf, RSA_KeyByteLen, RSA_KeyByteLen);
+ retVal = SDRM_Depad_Rsaes_pkcs15(out, &plainLen, pbBuf, RSA_KeyByteLen,
+ RSA_KeyByteLen);
break;
+
case ID_RSAES_OAEP:
- retVal = SDRM_Depad_Rsaes_oaep(out, &plainLen, pbBuf, RSA_KeyByteLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
+ retVal = SDRM_Depad_Rsaes_oaep(out, &plainLen, pbBuf, RSA_KeyByteLen,
+ RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
break;
+
case ID_NO_PADDING:
memcpy(out, pbBuf, RSA_KeyByteLen);
plainLen = RSA_KeyByteLen;
retVal = CRYPTO_SUCCESS;
break;
+
default:
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
}
- if (retVal != CRYPTO_SUCCESS)
- {
+ if (retVal != CRYPTO_SUCCESS) {
free(pbBuf);
return retVal;
}
if (outLen != NULL)
- {
*outLen = plainLen;
- }
memset(pbBuf, 0x00, SDRM_RSA_ALLOC_SIZE * 8 + RSA_KeyByteLen);
free(pbBuf);
@@ -1458,86 +1335,90 @@ int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_
}
/*
- * @fn int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen)
- * @brief generate signature for given value
+ * @fn int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen)
+ * @brief generate signature for given value
*
- * @param crt [in]crypto env structure
- * @param hash [in]hash value
- * @param hashLen [in]byte-length of hash
- * @param signature [out]generated signature
- * @param signLen [out]byte-length of signature
+ * @param crt [in]crypto env structure
+ * @param hash [in]hash value
+ * @param hashLen [in]byte-length of hash
+ * @param signature [out]generated signature
+ * @param signLen [out]byte-length of signature
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
*/
-int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen)
+int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen,
+ cc_u8 *signature, cc_u32 *signLen)
{
- SDRM_BIG_NUM *BN_pMsg, *BN_Sign;
- int retVal;
- cc_u32 RSA_KeyByteLen = 0;
- cc_u8 *pbBuf = NULL;
- cc_u32 nBits;
-
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (hash == NULL) || (signature == NULL))
- {
+ SDRM_BIG_NUM *BN_pMsg, *BN_Sign;
+ int retVal;
+ cc_u32 RSA_KeyByteLen = 0;
+ cc_u8 *pbBuf = NULL;
+ cc_u32 nBits;
+
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) ||
+ (hash == NULL) || (signature == NULL))
return CRYPTO_NULL_POINTER;
- }
RSA_KeyByteLen = crt->ctx->rsactx->k;
+
if (hashLen > RSA_KeyByteLen)
- {
return CRYPTO_MSG_TOO_LONG;
- }
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_pMsg = SDRM_BN_Alloc((cc_u8*)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE);
- BN_Sign = SDRM_BN_Alloc((cc_u8*)BN_pMsg + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ BN_pMsg = SDRM_BN_Alloc((cc_u8 *)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE);
+ BN_Sign = SDRM_BN_Alloc((cc_u8 *)BN_pMsg + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
//Msg Padding
- switch(SDRM_LOW_HALF(crt->ctx->rsactx->pm))
- {
- case ID_RSASSA_PKCS15 :
- retVal = SDRM_Enpad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
- break;
- case ID_RSASSA_PSS :
- SDRM_BN_GETBITLEN(crt->ctx->rsactx->n, nBits);
- retVal = SDRM_Enpad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
- break;
- case ID_NO_PADDING :
- memset(pbBuf, 0x00, RSA_KeyByteLen - hashLen);
- //memcpy(pbBuf + hashLen, hash, RSA_KeyByteLen);
- memcpy(pbBuf + RSA_KeyByteLen - hashLen, hash, hashLen);// fixed by guoxing.xu 20140919
- retVal = CRYPTO_SUCCESS;
- break;
- default :
- free(pbBuf);
- return CRYPTO_INVALID_ARGUMENT;
+ switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) {
+ case ID_RSASSA_PKCS15:
+ retVal = SDRM_Enpad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen,
+ SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
+ break;
+
+ case ID_RSASSA_PSS:
+ SDRM_BN_GETBITLEN(crt->ctx->rsactx->n, nBits);
+ retVal = SDRM_Enpad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen,
+ SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
+ break;
+
+ case ID_NO_PADDING:
+ memset(pbBuf, 0x00, RSA_KeyByteLen - hashLen);
+ //memcpy(pbBuf + hashLen, hash, RSA_KeyByteLen);
+ memcpy(pbBuf + RSA_KeyByteLen - hashLen, hash,
+ hashLen);// fixed by guoxing.xu 20140919
+ retVal = CRYPTO_SUCCESS;
+ break;
+
+ default:
+ free(pbBuf);
+ return CRYPTO_INVALID_ARGUMENT;
}
- if (retVal != CRYPTO_SUCCESS)
- {
+ if (retVal != CRYPTO_SUCCESS) {
free(pbBuf);
return retVal;
}
-// SDRM_PrintBN("ENPADDED Msg : ", BN_pMsg);
+ // SDRM_PrintBN("ENPADDED Msg : ", BN_pMsg);
SDRM_OS2BN(pbBuf, RSA_KeyByteLen, BN_pMsg);
//RSA Signature by modular exponent
#ifndef _OP64_NOTSUPPORTED
- retVal = SDRM_BN_ModExp2(BN_Sign, BN_pMsg, crt->ctx->rsactx->d, crt->ctx->rsactx->n);
+ retVal = SDRM_BN_ModExp2(BN_Sign, BN_pMsg, crt->ctx->rsactx->d,
+ crt->ctx->rsactx->n);
#else
- retVal = SDRM_BN_ModExp(BN_Sign, BN_pMsg, crt->ctx->rsactx->d, crt->ctx->rsactx->n);
-#endif //_OP64_NOTSUPPORTED
+ retVal = SDRM_BN_ModExp(BN_Sign, BN_pMsg, crt->ctx->rsactx->d,
+ crt->ctx->rsactx->n);
+#endif //_OP64_NOTSUPPORTED
- if (retVal != CRYPTO_SUCCESS)
- {
+ if (retVal != CRYPTO_SUCCESS) {
free(pbBuf);
return retVal;
}
@@ -1545,9 +1426,7 @@ int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *
SDRM_I2OSP(BN_Sign, RSA_KeyByteLen, signature);
if (signLen != NULL)
- {
*signLen = RSA_KeyByteLen;
- }
memset(pbBuf, 0x00, SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
free(pbBuf);
@@ -1556,61 +1435,61 @@ int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *
}
/*
- * @fn int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result)
- * @brief generate signature for given value
+ * @fn int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result)
+ * @brief generate signature for given value
*
- * @param crt [in]crypto env structure
- * @param hash [in]hash value
- * @param hashLen [in]byte-length of hash
- * @param signature [in]signature
- * @param signLen [in]byte-length of signature
- * @param result [in]result of verifying signature
+ * @param crt [in]crypto env structure
+ * @param hash [in]hash value
+ * @param hashLen [in]byte-length of hash
+ * @param signature [in]signature
+ * @param signLen [in]byte-length of signature
+ * @param result [in]result of verifying signature
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid
*/
-int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result)
+int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen,
+ cc_u8 *signature, cc_u32 signLen, int *result)
{
- SDRM_BIG_NUM *BN_dMsg, *BN_Sign;
- int retVal;
- cc_u32 RSA_KeyByteLen = 0;
- cc_u8 *pbBuf = NULL;
- cc_u32 nBits;
+ SDRM_BIG_NUM *BN_dMsg, *BN_Sign;
+ int retVal;
+ cc_u32 RSA_KeyByteLen = 0;
+ cc_u8 *pbBuf = NULL;
+ cc_u32 nBits;
cc_u32 i;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (hash == NULL) || (signature == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) ||
+ (hash == NULL) || (signature == NULL))
return CRYPTO_NULL_POINTER;
- }
RSA_KeyByteLen = crt->ctx->rsactx->k;
+
if (hashLen > RSA_KeyByteLen)
- {
return CRYPTO_MSG_TOO_LONG;
- }
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_dMsg = SDRM_BN_Alloc((cc_u8*)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE);
- BN_Sign = SDRM_BN_Alloc((cc_u8*)BN_dMsg + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ BN_dMsg = SDRM_BN_Alloc((cc_u8 *)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE);
+ BN_Sign = SDRM_BN_Alloc((cc_u8 *)BN_dMsg + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
SDRM_OS2BN(signature, signLen, BN_Sign);
-// SDRM_PrintBN("Generated Sign : ", BN_Sign);
+ // SDRM_PrintBN("Generated Sign : ", BN_Sign);
//RSA Verification by modular exponent
#ifndef _OP64_NOTSUPPORTED
- retVal = SDRM_BN_ModExp2(BN_dMsg, BN_Sign, crt->ctx->rsactx->e, crt->ctx->rsactx->n);
+ retVal = SDRM_BN_ModExp2(BN_dMsg, BN_Sign, crt->ctx->rsactx->e,
+ crt->ctx->rsactx->n);
#else
- retVal = SDRM_BN_ModExp(BN_dMsg, BN_Sign, crt->ctx->rsactx->e, crt->ctx->rsactx->n);
-#endif //_OP64_NOTSUPPORTED
+ retVal = SDRM_BN_ModExp(BN_dMsg, BN_Sign, crt->ctx->rsactx->e,
+ crt->ctx->rsactx->n);
+#endif //_OP64_NOTSUPPORTED
- if (retVal != CRYPTO_SUCCESS)
- {
+ if (retVal != CRYPTO_SUCCESS) {
free(pbBuf);
return retVal;
}
@@ -1618,35 +1497,33 @@ int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8
SDRM_I2OSP(BN_dMsg, RSA_KeyByteLen, pbBuf);
//Msg Depadding
- switch(SDRM_LOW_HALF(crt->ctx->rsactx->pm))
- {
- case ID_RSASSA_PKCS15 :
- *result = SDRM_Depad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
- break;
- case ID_RSASSA_PSS :
- SDRM_BN_GETBITLEN(crt->ctx->rsactx->n, nBits);
- *result = SDRM_Depad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
- break;
- case ID_NO_PADDING :
- for (i = 0; i < (RSA_KeyByteLen - hashLen); i++)
- {
- if (pbBuf[i] != 0)
- {
- *result = CRYPTO_INVALID_SIGN;
- }
- }
+ switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) {
+ case ID_RSASSA_PKCS15:
+ *result = SDRM_Depad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen,
+ SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
+ break;
- if ((i == (RSA_KeyByteLen - hashLen)) && (memcmp(pbBuf + i, hash, hashLen) == 0))
- {
- *result = CRYPTO_VALID_SIGN;
- }
- else
- {
+ case ID_RSASSA_PSS:
+ SDRM_BN_GETBITLEN(crt->ctx->rsactx->n, nBits);
+ *result = SDRM_Depad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen,
+ SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
+ break;
+
+ case ID_NO_PADDING:
+ for (i = 0; i < (RSA_KeyByteLen - hashLen); i++) {
+ if (pbBuf[i] != 0)
*result = CRYPTO_INVALID_SIGN;
- }
+ }
- default :
- break;
+ if ((i == (RSA_KeyByteLen - hashLen)) &&
+ (memcmp(pbBuf + i, hash, hashLen) == 0))
+ *result = CRYPTO_VALID_SIGN;
+
+ else
+ *result = CRYPTO_INVALID_SIGN;
+
+ default:
+ break;
}
memset(pbBuf, 0x00, SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen);
@@ -1656,42 +1533,50 @@ int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8
}
/***************************** End of File *****************************/
-int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b, SDRM_BIG_NUM* BN_x, SDRM_BIG_NUM* BN_y)
+int SDRM_Extended_GCD(SDRM_BIG_NUM *BN_v, SDRM_BIG_NUM *BN_a,
+ SDRM_BIG_NUM *BN_b, SDRM_BIG_NUM *BN_x, SDRM_BIG_NUM *BN_y)
{
- SDRM_BIG_NUM* BN_g;
- SDRM_BIG_NUM* BN_u;
- SDRM_BIG_NUM* BN_A;
- SDRM_BIG_NUM* BN_B;
- SDRM_BIG_NUM* BN_C;
- SDRM_BIG_NUM* BN_D;
- SDRM_BIG_NUM* BN_tmp;
- SDRM_BIG_NUM* BN_xx;
- SDRM_BIG_NUM* BN_yy;
- cc_u8* pbBuf = NULL;
+ SDRM_BIG_NUM *BN_g;
+ SDRM_BIG_NUM *BN_u;
+ SDRM_BIG_NUM *BN_A;
+ SDRM_BIG_NUM *BN_B;
+ SDRM_BIG_NUM *BN_C;
+ SDRM_BIG_NUM *BN_D;
+ SDRM_BIG_NUM *BN_tmp;
+ SDRM_BIG_NUM *BN_xx;
+ SDRM_BIG_NUM *BN_yy;
+ cc_u8 *pbBuf = NULL;
cc_u32 RSA_KeyByteLen = 128;
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 9);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 9);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_g = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE);
- BN_u = SDRM_BN_Alloc((cc_u8*)BN_g + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_A = SDRM_BN_Alloc((cc_u8*)BN_u + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_B = SDRM_BN_Alloc((cc_u8*)BN_A + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_C = SDRM_BN_Alloc((cc_u8*)BN_B + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_D = SDRM_BN_Alloc((cc_u8*)BN_C + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_tmp = SDRM_BN_Alloc((cc_u8*)BN_D + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_xx = SDRM_BN_Alloc((cc_u8*)BN_tmp + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_yy = SDRM_BN_Alloc((cc_u8*)BN_xx + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ BN_g = SDRM_BN_Alloc((cc_u8 *)pbBuf,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_u = SDRM_BN_Alloc((cc_u8 *)BN_g + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_A = SDRM_BN_Alloc((cc_u8 *)BN_u + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_B = SDRM_BN_Alloc((cc_u8 *)BN_A + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_C = SDRM_BN_Alloc((cc_u8 *)BN_B + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_D = SDRM_BN_Alloc((cc_u8 *)BN_C + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_tmp = SDRM_BN_Alloc((cc_u8 *)BN_D + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_xx = SDRM_BN_Alloc((cc_u8 *)BN_tmp + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_yy = SDRM_BN_Alloc((cc_u8 *)BN_xx + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
SDRM_BN_Copy(BN_g, BN_One);
SDRM_BN_Copy(BN_xx, BN_x);
SDRM_BN_Copy(BN_yy, BN_y);
- while(!SDRM_BN_IS_ODD(BN_xx) && !SDRM_BN_IS_ODD(BN_yy))
- {
+ while (!SDRM_BN_IS_ODD(BN_xx) && !SDRM_BN_IS_ODD(BN_yy)) {
SDRM_BN_SHR(BN_xx, BN_xx, 1);
SDRM_BN_SHR(BN_yy, BN_yy, 1);
SDRM_BN_SHL(BN_g, BN_g, 1);
@@ -1705,18 +1590,14 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b
SDRM_BN_Copy(BN_C, BN_Zero);
SDRM_BN_Copy(BN_D, BN_One);
- while(1)
- {
- while(!SDRM_BN_IS_ODD(BN_u))
- {
+ while (1) {
+ while (!SDRM_BN_IS_ODD(BN_u)) {
SDRM_BN_SHR(BN_u, BN_u, 1);
- if (!SDRM_BN_IS_ODD(BN_A) && !SDRM_BN_IS_ODD(BN_B))
- {
+
+ if (!SDRM_BN_IS_ODD(BN_A) && !SDRM_BN_IS_ODD(BN_B)) {
SDRM_BN_SHR(BN_A, BN_A, 1);
SDRM_BN_SHR(BN_B, BN_B, 1);
- }
- else
- {
+ } else {
SDRM_BN_Add(BN_A, BN_A, BN_yy);
SDRM_BN_SHR(BN_A, BN_A, 1);
@@ -1725,16 +1606,13 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b
}
}
- while(!SDRM_BN_IS_ODD(BN_v))
- {
+ while (!SDRM_BN_IS_ODD(BN_v)) {
SDRM_BN_SHR(BN_v, BN_v, 1);
- if (!SDRM_BN_IS_ODD(BN_C) && !SDRM_BN_IS_ODD(BN_D))
- {
+
+ if (!SDRM_BN_IS_ODD(BN_C) && !SDRM_BN_IS_ODD(BN_D)) {
SDRM_BN_SHR(BN_C, BN_C, 1);
SDRM_BN_SHR(BN_D, BN_D, 1);
- }
- else
- {
+ } else {
SDRM_BN_Add(BN_C, BN_C, BN_yy);
SDRM_BN_SHR(BN_C, BN_C, 1);
@@ -1743,8 +1621,7 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b
}
}
- if (SDRM_BN_Cmp(BN_u, BN_v) >= 0)
- {
+ if (SDRM_BN_Cmp(BN_u, BN_v) >= 0) {
SDRM_BN_Sub(BN_tmp, BN_u, BN_v);
SDRM_BN_Copy(BN_u, BN_tmp);
@@ -1753,9 +1630,7 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b
SDRM_BN_Sub(BN_tmp, BN_B, BN_D);
SDRM_BN_Copy(BN_B, BN_tmp);
- }
- else
- {
+ } else {
SDRM_BN_Sub(BN_tmp, BN_v, BN_u);
SDRM_BN_Copy(BN_v, BN_tmp);
@@ -1766,8 +1641,7 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b
SDRM_BN_Copy(BN_D, BN_tmp);
}
- if (SDRM_BN_Cmp(BN_u, BN_Zero) == 0)
- {
+ if (SDRM_BN_Cmp(BN_u, BN_Zero) == 0) {
SDRM_BN_Copy(BN_a, BN_C);
SDRM_BN_Copy(BN_b, BN_D);
SDRM_BN_Mul(BN_tmp, BN_g, BN_v);
@@ -1783,25 +1657,27 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b
}
-int SDRM_CheckRSAKey(SDRM_BIG_NUM* BN_n, SDRM_BIG_NUM* BN_e, SDRM_BIG_NUM* BN_d)
+int SDRM_CheckRSAKey(SDRM_BIG_NUM *BN_n, SDRM_BIG_NUM *BN_e, SDRM_BIG_NUM *BN_d)
{
- SDRM_BIG_NUM* BN_m;
- SDRM_BIG_NUM* BN_c;
- SDRM_BIG_NUM* BN_m1;
- cc_u8* pbBuf = NULL;
+ SDRM_BIG_NUM *BN_m;
+ SDRM_BIG_NUM *BN_c;
+ SDRM_BIG_NUM *BN_m1;
+ cc_u8 *pbBuf = NULL;
cc_u32 RSA_KeyByteLen = 128;
int retVal;
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 3);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 3);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_m = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE);
- BN_c = SDRM_BN_Alloc((cc_u8*)BN_m + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_m1 = SDRM_BN_Alloc((cc_u8*)BN_c + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ BN_m = SDRM_BN_Alloc((cc_u8 *)pbBuf,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_c = SDRM_BN_Alloc((cc_u8 *)BN_m + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_m1 = SDRM_BN_Alloc((cc_u8 *)BN_c + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
SDRM_BN_Rand(BN_m, 1020);
@@ -1809,13 +1685,10 @@ int SDRM_CheckRSAKey(SDRM_BIG_NUM* BN_n, SDRM_BIG_NUM* BN_e, SDRM_BIG_NUM* BN_d)
SDRM_BN_ModExp(BN_m1, BN_c, BN_d, BN_n);
if (SDRM_BN_Cmp(BN_m, BN_m1) == 0)
- {
retVal = CRYPTO_SUCCESS;
- }
+
else
- {
retVal = CRYPTO_ERROR;
- }
free(pbBuf);
@@ -1824,44 +1697,57 @@ int SDRM_CheckRSAKey(SDRM_BIG_NUM* BN_n, SDRM_BIG_NUM* BN_e, SDRM_BIG_NUM* BN_d)
int SDRM_RSA_ConvertCRT2PrivateExp(cc_u8 *p320byteCRTParam, cc_u8 *PrivateExp)
{
- SDRM_BIG_NUM* BN_g;
- SDRM_BIG_NUM* BN_v;
- SDRM_BIG_NUM* BN_diff;
- SDRM_BIG_NUM* BN_k;
- SDRM_BIG_NUM* BN_r;
- SDRM_BIG_NUM* BN_l;
- SDRM_BIG_NUM* BN_u;
- SDRM_BIG_NUM* BN_n;
- SDRM_BIG_NUM* BN_e;
- SDRM_BIG_NUM* BN_d;
- SDRM_BIG_NUM* BN_p;
- SDRM_BIG_NUM* BN_q;
- SDRM_BIG_NUM* BN_dp;
- SDRM_BIG_NUM* BN_dq;
-
- cc_u8* pbBuf = NULL;
+ SDRM_BIG_NUM *BN_g;
+ SDRM_BIG_NUM *BN_v;
+ SDRM_BIG_NUM *BN_diff;
+ SDRM_BIG_NUM *BN_k;
+ SDRM_BIG_NUM *BN_r;
+ SDRM_BIG_NUM *BN_l;
+ SDRM_BIG_NUM *BN_u;
+ SDRM_BIG_NUM *BN_n;
+ SDRM_BIG_NUM *BN_e;
+ SDRM_BIG_NUM *BN_d;
+ SDRM_BIG_NUM *BN_p;
+ SDRM_BIG_NUM *BN_q;
+ SDRM_BIG_NUM *BN_dp;
+ SDRM_BIG_NUM *BN_dq;
+
+ cc_u8 *pbBuf = NULL;
cc_u32 RSA_KeyByteLen = 128;
- pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 14);
+ pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 14);
+
if (pbBuf == NULL)
- {
return CRYPTO_MEMORY_ALLOC_FAIL;
- }
- BN_g = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE);
- BN_v = SDRM_BN_Alloc((cc_u8*)BN_g + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_diff = SDRM_BN_Alloc((cc_u8*)BN_v + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_k = SDRM_BN_Alloc((cc_u8*)BN_diff + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_r = SDRM_BN_Alloc((cc_u8*)BN_k + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_l = SDRM_BN_Alloc((cc_u8*)BN_r + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_u = SDRM_BN_Alloc((cc_u8*)BN_l + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_n = SDRM_BN_Alloc((cc_u8*)BN_u + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_e = SDRM_BN_Alloc((cc_u8*)BN_n + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_d = SDRM_BN_Alloc((cc_u8*)BN_e + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_p = SDRM_BN_Alloc((cc_u8*)BN_d + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_q = SDRM_BN_Alloc((cc_u8*)BN_p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_dp = SDRM_BN_Alloc((cc_u8*)BN_q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
- BN_dq = SDRM_BN_Alloc((cc_u8*)BN_dp + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE);
+ BN_g = SDRM_BN_Alloc((cc_u8 *)pbBuf,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_v = SDRM_BN_Alloc((cc_u8 *)BN_g + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_diff = SDRM_BN_Alloc((cc_u8 *)BN_v + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_k = SDRM_BN_Alloc((cc_u8 *)BN_diff + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_r = SDRM_BN_Alloc((cc_u8 *)BN_k + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_l = SDRM_BN_Alloc((cc_u8 *)BN_r + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_u = SDRM_BN_Alloc((cc_u8 *)BN_l + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_n = SDRM_BN_Alloc((cc_u8 *)BN_u + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_e = SDRM_BN_Alloc((cc_u8 *)BN_n + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_d = SDRM_BN_Alloc((cc_u8 *)BN_e + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_p = SDRM_BN_Alloc((cc_u8 *)BN_d + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_q = SDRM_BN_Alloc((cc_u8 *)BN_p + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_dp = SDRM_BN_Alloc((cc_u8 *)BN_q + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
+ BN_dq = SDRM_BN_Alloc((cc_u8 *)BN_dp + SDRM_RSA_ALLOC_SIZE,
+ SDRM_RSA_BN_BUFSIZE);
SDRM_OS2BN(p320byteCRTParam, 64, BN_p);
SDRM_OS2BN(p320byteCRTParam + 64, 64, BN_q);
@@ -1870,9 +1756,8 @@ int SDRM_RSA_ConvertCRT2PrivateExp(cc_u8 *p320byteCRTParam, cc_u8 *PrivateExp)
SDRM_BN_Mul(BN_n, BN_p, BN_q);
- if (SDRM_BN_Cmp(BN_dp, BN_dq) < 0)
- {
- SDRM_BIG_NUM* tmp;
+ if (SDRM_BN_Cmp(BN_dp, BN_dq) < 0) {
+ SDRM_BIG_NUM *tmp;
tmp = BN_p;
BN_p = BN_q;
BN_q = tmp;
@@ -1899,30 +1784,23 @@ int SDRM_RSA_ConvertCRT2PrivateExp(cc_u8 *p320byteCRTParam, cc_u8 *PrivateExp)
SDRM_BN_ModRed(BN_r, BN_d, BN_l);
if ((SDRM_BN_Cmp(BN_r, BN_Zero) != 0) && SDRM_IS_BN_NEGATIVE(BN_r))
- {
SDRM_BN_Add(BN_d, BN_l, BN_r);
- }
+
else
- {
SDRM_BN_Copy(BN_d, BN_r);
- }
SDRM_BN_ModInv(BN_e, BN_d, BN_l);
SDRM_BN_ModInv(BN_d, BN_e, BN_k);
- if ((SDRM_BN_Cmp(BN_d, BN_Zero) != 0) && !SDRM_IS_BN_NEGATIVE(BN_d))
- {
+ if ((SDRM_BN_Cmp(BN_d, BN_Zero) != 0) && !SDRM_IS_BN_NEGATIVE(BN_d)) {
SDRM_BN_ModRed(BN_r, BN_d, BN_p);
SDRM_PrintBN("n", BN_n);
SDRM_PrintBN("e", BN_e);
SDRM_PrintBN("d", BN_d);
- if (SDRM_BN_Cmp(BN_r, BN_dp) == 0)
- {
+ if (SDRM_BN_Cmp(BN_r, BN_dp) == 0) {
if (SDRM_CheckRSAKey(BN_n, BN_e, BN_d) == CRYPTO_SUCCESS)
- {
SDRM_BN2OS(BN_d, 128, PrivateExp);
- }
}
}
diff --git a/ssflib/dep/cryptocore/source/middle/cc_symmetric.c b/ssflib/dep/cryptocore/source/middle/cc_symmetric.c
index b313fe4..06309a3 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_symmetric.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_symmetric.c
@@ -33,156 +33,158 @@
// Functions
////////////////////////////////////////////////////////////////////////////
/*
- * @fn SDRM_getEncRoundKey
- * @brief get scheduled key for encryption
+ * @fn SDRM_getEncRoundKey
+ * @brief get scheduled key for encryption
*
- * @param Algorithm [in]cipher algorithm
- * @param UserKey [in]user key
- * @param RoundKey [out]round key
+ * @param Algorithm [in]cipher algorithm
+ * @param UserKey [in]user key
+ * @param RoundKey [out]round key
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_getEncRoundKey(int Algorithm, cc_u8* UserKey, cc_u8* RoundKey)
+int SDRM_getEncRoundKey(int Algorithm, cc_u8 *UserKey, cc_u8 *RoundKey)
{
if ((UserKey == NULL) || (RoundKey == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
- switch (Algorithm)
- {
- case ID_AES128 :
- SDRM_rijndaelKeySetupEnc((cc_u32*)(void*)RoundKey, UserKey, 128);
- return CRYPTO_SUCCESS;
- case ID_AES192 :
- SDRM_rijndaelKeySetupEnc((cc_u32*)(void*)RoundKey, UserKey, 192);
- return CRYPTO_SUCCESS;
- case ID_AES256 :
- SDRM_rijndaelKeySetupEnc((cc_u32*)(void*)RoundKey, UserKey, 256);
- return CRYPTO_SUCCESS;
- case ID_DES :
- SDRM_DES_KeySched(RoundKey, UserKey, 0, 1);
- return CRYPTO_SUCCESS;
- case ID_TDES_EDE2 :
- SDRM_TDES_KeySched(RoundKey, UserKey, 16, 1);
- return CRYPTO_SUCCESS;
- case ID_TDES_EDE3 :
- SDRM_TDES_KeySched(RoundKey, UserKey, 24, 1);
- return CRYPTO_SUCCESS;
- default :
- break;
+ switch (Algorithm) {
+ case ID_AES128:
+ SDRM_rijndaelKeySetupEnc((cc_u32 *)(void *)RoundKey, UserKey, 128);
+ return CRYPTO_SUCCESS;
+
+ case ID_AES192:
+ SDRM_rijndaelKeySetupEnc((cc_u32 *)(void *)RoundKey, UserKey, 192);
+ return CRYPTO_SUCCESS;
+
+ case ID_AES256:
+ SDRM_rijndaelKeySetupEnc((cc_u32 *)(void *)RoundKey, UserKey, 256);
+ return CRYPTO_SUCCESS;
+
+ case ID_DES:
+ SDRM_DES_KeySched(RoundKey, UserKey, 0, 1);
+ return CRYPTO_SUCCESS;
+
+ case ID_TDES_EDE2:
+ SDRM_TDES_KeySched(RoundKey, UserKey, 16, 1);
+ return CRYPTO_SUCCESS;
+
+ case ID_TDES_EDE3:
+ SDRM_TDES_KeySched(RoundKey, UserKey, 24, 1);
+ return CRYPTO_SUCCESS;
+
+ default:
+ break;
}
return CRYPTO_INVALID_ARGUMENT;
}
/*
- * @fn SDRM_getDecRoundKey
- * @brief get scheduled key for decryption
+ * @fn SDRM_getDecRoundKey
+ * @brief get scheduled key for decryption
*
- * @param Algorithm [in]cipher algorithm
- * @param UserKey [in]user key
- * @param RoundKey [out]round key
+ * @param Algorithm [in]cipher algorithm
+ * @param UserKey [in]user key
+ * @param RoundKey [out]round key
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_getDecRoundKey(int Algorithm, cc_u8* UserKey, cc_u8* RoundKey)
+int SDRM_getDecRoundKey(int Algorithm, cc_u8 *UserKey, cc_u8 *RoundKey)
{
if ((UserKey == NULL) || (RoundKey == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
- switch (Algorithm)
- {
- case ID_AES128 :
- SDRM_rijndaelKeySetupDec((cc_u32*)(void*)RoundKey, UserKey, 128);
- return CRYPTO_SUCCESS;
- case ID_AES192 :
- SDRM_rijndaelKeySetupDec((cc_u32*)(void*)RoundKey, UserKey, 192);
- return CRYPTO_SUCCESS;
- case ID_AES256 :
- SDRM_rijndaelKeySetupDec((cc_u32*)(void*)RoundKey, UserKey, 256);
- return CRYPTO_SUCCESS;
- case ID_DES :
- SDRM_DES_KeySched(RoundKey, UserKey, 15, (cc_u32)-1);
- return CRYPTO_SUCCESS;
- case ID_TDES_EDE2 :
- SDRM_TDES_KeySched(RoundKey, UserKey, 16, (cc_u32)-1);
- return CRYPTO_SUCCESS;
- case ID_TDES_EDE3 :
- SDRM_TDES_KeySched(RoundKey, UserKey, 24, (cc_u32)-1);
- return CRYPTO_SUCCESS;
- default :
- break;
+ switch (Algorithm) {
+ case ID_AES128:
+ SDRM_rijndaelKeySetupDec((cc_u32 *)(void *)RoundKey, UserKey, 128);
+ return CRYPTO_SUCCESS;
+
+ case ID_AES192:
+ SDRM_rijndaelKeySetupDec((cc_u32 *)(void *)RoundKey, UserKey, 192);
+ return CRYPTO_SUCCESS;
+
+ case ID_AES256:
+ SDRM_rijndaelKeySetupDec((cc_u32 *)(void *)RoundKey, UserKey, 256);
+ return CRYPTO_SUCCESS;
+
+ case ID_DES:
+ SDRM_DES_KeySched(RoundKey, UserKey, 15, (cc_u32) - 1);
+ return CRYPTO_SUCCESS;
+
+ case ID_TDES_EDE2:
+ SDRM_TDES_KeySched(RoundKey, UserKey, 16, (cc_u32) - 1);
+ return CRYPTO_SUCCESS;
+
+ case ID_TDES_EDE3:
+ SDRM_TDES_KeySched(RoundKey, UserKey, 24, (cc_u32) - 1);
+ return CRYPTO_SUCCESS;
+
+ default:
+ break;
}
return CRYPTO_INVALID_ARGUMENT;
}
/*
- * @fn SDRM_AES_init
- * @brief intialize crypt context for aes
+ * @fn SDRM_AES_init
+ * @brief intialize crypt context for aes
*
- * @param crt [out]crypto env structure
- * @param mode [in]encryption|decryption and mode of operation
- * @param PADDING [in]padding method
- * @param key [in]user key
- * @param keysize [in]byte-length of key
- * @param IV [in]initial vector
+ * @param crt [out]crypto env structure
+ * @param mode [in]encryption|decryption and mode of operation
+ * @param PADDING [in]padding method
+ * @param key [in]user key
+ * @param keysize [in]byte-length of key
+ * @param IV [in]initial vector
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_AES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *key, cc_u32 keysize, cc_u8 *IV)
+int SDRM_AES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING,
+ cc_u8 *key, cc_u32 keysize, cc_u8 *IV)
{
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->aesctx == NULL) || (key == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->aesctx == NULL) ||
+ (key == NULL))
return CRYPTO_NULL_POINTER;
- }
- if (!(((mode >= 1111) && (mode <= 1115)) || ((mode >= 1121) && (mode <= 1125))))
- {
+
+ if (!(((mode >= 1111) && (mode <= 1115)) || ((mode >= 1121) &&
+ (mode <= 1125))))
return CRYPTO_INVALID_ARGUMENT;
- }
+
if (!((crt->alg == ID_AES128) && (keysize == 16)) &&
- !((crt->alg == ID_AES192) && (keysize == 24)) &&
- !((crt->alg == ID_AES256) && (keysize == 32)))
- {
+ !((crt->alg == ID_AES192) && (keysize == 24)) &&
+ !((crt->alg == ID_AES256) && (keysize == 32)))
return CRYPTO_INVALID_ARGUMENT;
- }
- if ((crt->alg != ID_AES128) && (crt->alg != ID_AES192) && (crt->alg != ID_AES256))
- {
+
+ if ((crt->alg != ID_AES128) && (crt->alg != ID_AES192) &&
+ (crt->alg != ID_AES256))
return CRYPTO_INVALID_ARGUMENT;
- }
- if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) && (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING))
- {
+ if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) &&
+ (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING))
return CRYPTO_INVALID_ARGUMENT;
- }
+
crt->ctx->aesctx->moo = mode;
crt->ctx->aesctx->padding = PADDING;
+
if (mode != ID_DEC_ECB && mode != ID_DEC_CBC)
- {
SDRM_getEncRoundKey(crt->alg, key, crt->ctx->aesctx->RoundKey);
- }
+
else
- {
SDRM_getDecRoundKey(crt->alg, key, crt->ctx->aesctx->RoundKey);
- }
+
if (IV)
- {
memcpy(crt->ctx->aesctx->IV, IV, SDRM_AES_BLOCK_SIZ);
- }
+
else
- {
memset(crt->ctx->aesctx->IV, 0x00, SDRM_AES_BLOCK_SIZ);
- }
+
crt->ctx->aesctx->BlockLen = 0;
GET_UINT32(crt->ctx->aesctx->CTR_Count, crt->ctx->aesctx->IV + 12, 0);
@@ -191,20 +193,21 @@ int SDRM_AES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *
}
/*
- * @fn SDRM_AES_process
- * @brief process message block
+ * @fn SDRM_AES_process
+ * @brief process message block
*
- * @param crt [in]crypto env structure
- * @param Text [in]message block
- * @param TextLen [in]byte-length of Text
- * @param output [out]proecessed message
- * @param outputLen [out]byte-length of output
+ * @param crt [in]crypto env structure
+ * @param Text [in]message block
+ * @param TextLen [in]byte-length of Text
+ * @param output [out]proecessed message
+ * @param outputLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_AES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u8 *output, cc_u32 *outputLen)
+int SDRM_AES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen,
+ cc_u8 *output, cc_u32 *outputLen)
{
int i, Temp;
int retVal, BlockLen;
@@ -212,163 +215,192 @@ int SDRM_AES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u
cc_u32 tempLen = 0;
if (outputLen != NULL)
- {
*outputLen = 0;
- }
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->aesctx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
Block = crt->ctx->aesctx->Block;
BlockLen = crt->ctx->aesctx->BlockLen;
- if ((TextLen + BlockLen) < SDRM_AES_BLOCK_SIZ)
- {
+ if ((TextLen + BlockLen) < SDRM_AES_BLOCK_SIZ) {
memcpy(Block + BlockLen, Text, TextLen);
crt->ctx->aesctx->BlockLen += TextLen;
return CRYPTO_SUCCESS;
}
- if (BlockLen)
- {
+ if (BlockLen) {
memcpy(Block + BlockLen, Text, SDRM_AES_BLOCK_SIZ - BlockLen);
- switch(crt->ctx->aesctx->moo)
- {
- case ID_ENC_ECB :
- retVal = SDRM_ECB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_ENC_CBC :
- retVal = SDRM_CBC_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_ENC_CFB :
- retVal = SDRM_CFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_ENC_OFB :
- retVal = SDRM_OFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_ENC_CTR :
- retVal = SDRM_CTR_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_DEC_ECB :
- retVal = SDRM_ECB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_DEC_CBC :
- retVal = SDRM_CBC_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_DEC_CFB :
- retVal = SDRM_CFB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_DEC_OFB :
- retVal = SDRM_OFB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_DEC_CTR :
- retVal = SDRM_CTR_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ switch (crt->ctx->aesctx->moo) {
+ case ID_ENC_ECB:
+ retVal = SDRM_ECB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CBC:
+ retVal = SDRM_CBC_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CFB:
+ retVal = SDRM_CFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_OFB:
+ retVal = SDRM_OFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CTR:
+ retVal = SDRM_CTR_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_ECB:
+ retVal = SDRM_ECB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CBC:
+ retVal = SDRM_CBC_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CFB:
+ retVal = SDRM_CFB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_OFB:
+ retVal = SDRM_OFB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CTR:
+ retVal = SDRM_CTR_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
}
Temp = TextLen - SDRM_AES_BLOCK_SIZ + 1;
- for (i = (SDRM_AES_BLOCK_SIZ - BlockLen) & 0x0f; i < Temp; i += SDRM_AES_BLOCK_SIZ)
- {
- switch(crt->ctx->aesctx->moo)
- {
- case ID_ENC_ECB :
- retVal = SDRM_ECB_Enc(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_ENC_CBC :
- retVal = SDRM_CBC_Enc(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_ENC_CFB :
- retVal = SDRM_CFB_Enc(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_ENC_OFB :
- retVal = SDRM_OFB_Enc(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_ENC_CTR :
- retVal = SDRM_CTR_Enc(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_DEC_ECB :
- retVal = SDRM_ECB_Dec(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_DEC_CBC :
- retVal = SDRM_CBC_Dec(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_DEC_CFB :
- retVal = SDRM_CFB_Dec(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_DEC_OFB :
- retVal = SDRM_OFB_Dec(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- case ID_DEC_CTR :
- retVal = SDRM_CTR_Dec(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++);
- tempLen += SDRM_AES_BLOCK_SIZ;
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+
+ for (i = (SDRM_AES_BLOCK_SIZ - BlockLen) & 0x0f; i < Temp;
+ i += SDRM_AES_BLOCK_SIZ) {
+ switch (crt->ctx->aesctx->moo) {
+ case ID_ENC_ECB:
+ retVal = SDRM_ECB_Enc(crt->alg, output + tempLen, Text + i,
+ crt->ctx->aesctx->RoundKey);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CBC:
+ retVal = SDRM_CBC_Enc(crt->alg, output + tempLen, Text + i,
+ crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CFB:
+ retVal = SDRM_CFB_Enc(crt->alg, output + tempLen, Text + i,
+ crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_OFB:
+ retVal = SDRM_OFB_Enc(crt->alg, output + tempLen, Text + i,
+ crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CTR:
+ retVal = SDRM_CTR_Enc(crt->alg, output + tempLen, Text + i,
+ crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV,
+ crt->ctx->aesctx->CTR_Count++);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_ECB:
+ retVal = SDRM_ECB_Dec(crt->alg, output + tempLen, Text + i,
+ crt->ctx->aesctx->RoundKey);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CBC:
+ retVal = SDRM_CBC_Dec(crt->alg, output + tempLen, Text + i,
+ crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CFB:
+ retVal = SDRM_CFB_Dec(crt->alg, output + tempLen, Text + i,
+ crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_OFB:
+ retVal = SDRM_OFB_Dec(crt->alg, output + tempLen, Text + i,
+ crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CTR:
+ retVal = SDRM_CTR_Dec(crt->alg, output + tempLen, Text + i,
+ crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV,
+ crt->ctx->aesctx->CTR_Count++);
+ tempLen += SDRM_AES_BLOCK_SIZ;
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
}
crt->ctx->aesctx->BlockLen = (SDRM_AES_BLOCK_SIZ + TextLen - i) & 0x0f;
- memcpy(Block, Text + TextLen - crt->ctx->aesctx->BlockLen, crt->ctx->aesctx->BlockLen);
+ memcpy(Block, Text + TextLen - crt->ctx->aesctx->BlockLen,
+ crt->ctx->aesctx->BlockLen);
if (outputLen != 0)
- {
*outputLen = tempLen;
- }
return CRYPTO_SUCCESS;
}
/*
- * @fn SDRM_AES_final
- * @brief process final block and padding
+ * @fn SDRM_AES_final
+ * @brief process final block and padding
*
- * @param crt [in]crypto env structure
- * @param input [in]message block
- * @param inputLen [in]byte-length of Text
- * @param output [out]processed message
- * @param outputLen [out]byte-length of output
+ * @param crt [in]crypto env structure
+ * @param input [in]message block
+ * @param inputLen [in]byte-length of Text
+ * @param output [out]processed message
+ * @param outputLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_AES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u8 *output, cc_u32 *outputLen)
+int SDRM_AES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen,
+ cc_u8 *output, cc_u32 *outputLen)
{
int retVal = CRYPTO_SUCCESS;
cc_u8 *Block, PADDING[16];
@@ -376,268 +408,256 @@ int SDRM_AES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u
cc_u8 t;
if (outputLen != NULL)
- {
*outputLen = 0;
- }
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->aesctx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
Block = crt->ctx->aesctx->Block;
BlockLen = crt->ctx->aesctx->BlockLen;
if (crt->ctx->aesctx->moo >= ID_DEC_ECB)
- {
goto DECRYPTION;
- }
-//ENCRYPTION:
- if (inputLen != 0)
- {
+ //ENCRYPTION:
+ if (inputLen != 0) {
unsigned int temp;
retVal = SDRM_AES_process(crt, input, inputLen, output, &temp);
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
retVal = SDRM_AES_final(crt, NULL, 0, output + temp, outputLen);
if (outputLen)
- {
*outputLen += temp;
- }
return retVal;
}
if (outputLen != NULL)
- {
*outputLen = SDRM_AES_BLOCK_SIZ;
- }
//padding
- switch(crt->ctx->aesctx->padding)
- {
- case 0 :
- case ID_PKCS5 :
- memset(Block + BlockLen, SDRM_AES_BLOCK_SIZ - BlockLen, SDRM_AES_BLOCK_SIZ - BlockLen);
- break;
- case ID_SSL_PADDING :
- memset(Block + BlockLen, SDRM_AES_BLOCK_SIZ - BlockLen - 1, SDRM_AES_BLOCK_SIZ - BlockLen);
- break;
- case ID_ZERO_PADDING :
- memset(Block + BlockLen, 0x00, SDRM_AES_BLOCK_SIZ - BlockLen);
- break;
- case ID_NO_PADDING :
- if (BlockLen == 0)
- {
- if (outputLen)
- {
- *outputLen = 0;
- }
- return CRYPTO_SUCCESS;
- }
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ switch (crt->ctx->aesctx->padding) {
+ case 0:
+ case ID_PKCS5:
+ memset(Block + BlockLen, SDRM_AES_BLOCK_SIZ - BlockLen,
+ SDRM_AES_BLOCK_SIZ - BlockLen);
+ break;
+
+ case ID_SSL_PADDING:
+ memset(Block + BlockLen, SDRM_AES_BLOCK_SIZ - BlockLen - 1,
+ SDRM_AES_BLOCK_SIZ - BlockLen);
+ break;
+
+ case ID_ZERO_PADDING:
+ memset(Block + BlockLen, 0x00, SDRM_AES_BLOCK_SIZ - BlockLen);
+ break;
+
+ case ID_NO_PADDING:
+ if (BlockLen == 0) {
+ if (outputLen)
+ *outputLen = 0;
+
+ return CRYPTO_SUCCESS;
+ }
+
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
//encryption
- switch(crt->ctx->aesctx->moo)
- {
- case ID_ENC_ECB :
- retVal = SDRM_ECB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey);
- break;
- case ID_ENC_CBC :
- retVal = SDRM_CBC_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- break;
- case ID_ENC_CFB :
- retVal = SDRM_CFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- break;
- case ID_ENC_OFB :
- retVal = SDRM_OFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- break;
- case ID_ENC_CTR :
- retVal = SDRM_CTR_Enc(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++);
- if(crt->ctx->aesctx->padding != ID_NO_PADDING)// add by xugx to support padding
- {
- BlockLen = SDRM_AES_BLOCK_SIZ;
- }
- memcpy(output, Block, BlockLen);
- if(outputLen != NULL)
- {
- *outputLen = BlockLen;
- }
- break;
- default :
- retVal = CRYPTO_INVALID_ARGUMENT;
- break;
+ switch (crt->ctx->aesctx->moo) {
+ case ID_ENC_ECB:
+ retVal = SDRM_ECB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey);
+ break;
+
+ case ID_ENC_CBC:
+ retVal = SDRM_CBC_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ break;
+
+ case ID_ENC_CFB:
+ retVal = SDRM_CFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ break;
+
+ case ID_ENC_OFB:
+ retVal = SDRM_OFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ break;
+
+ case ID_ENC_CTR:
+ retVal = SDRM_CTR_Enc(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++);
+
+ if (crt->ctx->aesctx->padding !=
+ ID_NO_PADDING) // add by xugx to support padding
+ BlockLen = SDRM_AES_BLOCK_SIZ;
+
+ memcpy(output, Block, BlockLen);
+
+ if (outputLen != NULL)
+ *outputLen = BlockLen;
+
+ break;
+
+ default:
+ retVal = CRYPTO_INVALID_ARGUMENT;
+ break;
}
return retVal;
DECRYPTION:
+
if (outputLen != NULL)
- {
*outputLen = 0;
- }
- if ((inputLen == 0) && (crt->ctx->aesctx->padding == ID_NO_PADDING) && (crt->ctx->aesctx->moo != ID_DEC_CTR))
- {
+ if ((inputLen == 0) && (crt->ctx->aesctx->padding == ID_NO_PADDING) &&
+ (crt->ctx->aesctx->moo != ID_DEC_CTR))
return CRYPTO_SUCCESS;
- }
- if (((BlockLen + inputLen) != SDRM_AES_BLOCK_SIZ) && (crt->ctx->aesctx->moo != ID_DEC_CTR))
- {
+ if (((BlockLen + inputLen) != SDRM_AES_BLOCK_SIZ) &&
+ (crt->ctx->aesctx->moo != ID_DEC_CTR))
return CRYPTO_INVALID_ARGUMENT;
- }
if (inputLen != 0)
- {
memcpy(Block + BlockLen, input, inputLen);
- }
- switch(crt->ctx->aesctx->moo)
- {
- case ID_DEC_ECB :
- retVal = SDRM_ECB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey);
- break;
- case ID_DEC_CBC :
- retVal = SDRM_CBC_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- break;
- case ID_DEC_CFB :
- retVal = SDRM_CFB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- break;
- case ID_DEC_OFB :
- retVal = SDRM_OFB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV);
- break;
- case ID_DEC_CTR :
- retVal = SDRM_CTR_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++);
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ switch (crt->ctx->aesctx->moo) {
+ case ID_DEC_ECB:
+ retVal = SDRM_ECB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey);
+ break;
+
+ case ID_DEC_CBC:
+ retVal = SDRM_CBC_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ break;
+
+ case ID_DEC_CFB:
+ retVal = SDRM_CFB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ break;
+
+ case ID_DEC_OFB:
+ retVal = SDRM_OFB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV);
+ break;
+
+ case ID_DEC_CTR:
+ retVal = SDRM_CTR_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey,
+ crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++);
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
//de-padding
t = Block[SDRM_AES_BLOCK_SIZ - 1];
- switch(crt->ctx->aesctx->padding)
- {
- case 0 :
- case ID_PKCS5 :
- if ((t > SDRM_AES_BLOCK_SIZ) || (t < 1))
- {
- return CRYPTO_INVALID_ARGUMENT;
- }
- memset(PADDING, t, t);
- break;
- case ID_SSL_PADDING :
- ++t;
- if ((t > SDRM_AES_BLOCK_SIZ) || (t < 1))
- {
- return CRYPTO_INVALID_ARGUMENT;
- }
- memset(PADDING, t - 1, t);
- break;
- case ID_ZERO_PADDING :
- {
- cc_u32 tmpLen;
- tmpLen = SDRM_AES_BLOCK_SIZ;
- while((tmpLen != 0x00) && (Block[tmpLen - 1] == 0x00))
- {
- tmpLen--;
- }
-
- memcpy(output, Block, tmpLen);
-
- if (outputLen != NULL)
- {
- *outputLen = tmpLen;
- }
- }
- return CRYPTO_SUCCESS;
- case ID_NO_PADDING :
- {
- cc_u32 tmpLen;
- tmpLen = SDRM_AES_BLOCK_SIZ;
-
- if (crt->ctx->aesctx->moo == ID_DEC_CTR)
- {
- tmpLen = BlockLen + inputLen;
- }
- else
- {
- tmpLen = SDRM_AES_BLOCK_SIZ;
- }
-
- memcpy(output, Block, tmpLen);
-
- if (outputLen != NULL)
- {
- *outputLen = tmpLen;
- }
- }
- return CRYPTO_SUCCESS;
- default :
- if (outputLen != NULL)
- {
- *outputLen = 0;
- }
+ switch (crt->ctx->aesctx->padding) {
+ case 0:
+ case ID_PKCS5:
+ if ((t > SDRM_AES_BLOCK_SIZ) || (t < 1))
return CRYPTO_INVALID_ARGUMENT;
+
+ memset(PADDING, t, t);
+ break;
+
+ case ID_SSL_PADDING:
+ ++t;
+
+ if ((t > SDRM_AES_BLOCK_SIZ) || (t < 1))
+ return CRYPTO_INVALID_ARGUMENT;
+
+ memset(PADDING, t - 1, t);
+ break;
+
+ case ID_ZERO_PADDING: {
+ cc_u32 tmpLen;
+ tmpLen = SDRM_AES_BLOCK_SIZ;
+
+ while ((tmpLen != 0x00) && (Block[tmpLen - 1] == 0x00))
+ tmpLen--;
+
+ memcpy(output, Block, tmpLen);
+
+ if (outputLen != NULL)
+ *outputLen = tmpLen;
}
- if (memcmp(PADDING, Block + SDRM_AES_BLOCK_SIZ - t, t) != 0)
- {
+ return CRYPTO_SUCCESS;
+
+ case ID_NO_PADDING: {
+ cc_u32 tmpLen;
+ tmpLen = SDRM_AES_BLOCK_SIZ;
+
+ if (crt->ctx->aesctx->moo == ID_DEC_CTR)
+ tmpLen = BlockLen + inputLen;
+
+ else
+ tmpLen = SDRM_AES_BLOCK_SIZ;
+
+ memcpy(output, Block, tmpLen);
+
+ if (outputLen != NULL)
+ *outputLen = tmpLen;
+ }
+
+ return CRYPTO_SUCCESS;
+
+ default:
+ if (outputLen != NULL)
+ *outputLen = 0;
+
return CRYPTO_INVALID_ARGUMENT;
}
- memcpy(output, Block, SDRM_AES_BLOCK_SIZ -t);
+ if (memcmp(PADDING, Block + SDRM_AES_BLOCK_SIZ - t, t) != 0)
+ return CRYPTO_INVALID_ARGUMENT;
+
+ memcpy(output, Block, SDRM_AES_BLOCK_SIZ - t);
if (outputLen != NULL)
- {
*outputLen = SDRM_AES_BLOCK_SIZ - t;
- }
return CRYPTO_SUCCESS;
}
/*
- * @fn SDRM_RC4_init
- * @brief intialize crypt context for RC4
+ * @fn SDRM_RC4_init
+ * @brief intialize crypt context for RC4
*
- * @param crt [out]crypto env structure
- * @param mode [in]encryption|decryption and mode of operation
- * @param PADDING [in]padding method, not needed
- * @param key [in]user key
- * @param keysize [in]byte-length of key
- * @param IV [in]initial vector, not needed
+ * @param crt [out]crypto env structure
+ * @param mode [in]encryption|decryption and mode of operation
+ * @param PADDING [in]padding method, not needed
+ * @param key [in]user key
+ * @param keysize [in]byte-length of key
+ * @param IV [in]initial vector, not needed
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_RC4_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *key, cc_u32 keysize, cc_u8 *IV)
+int SDRM_RC4_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING,
+ cc_u8 *key, cc_u32 keysize, cc_u8 *IV)
{
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rc4ctx == NULL) || (key == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rc4ctx == NULL) ||
+ (key == NULL))
return CRYPTO_NULL_POINTER;
- }
if (keysize > 32)
- {
return CRYPTO_INVALID_ARGUMENT;
- }
SDRM_RC4_Setup(crt->ctx->rc4ctx, key, keysize);
@@ -645,64 +665,60 @@ int SDRM_RC4_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *
}
/*
- * @fn SDRM_RC4_process
- * @brief process message block
+ * @fn SDRM_RC4_process
+ * @brief process message block
*
- * @param crt [in]crypto env structure
- * @param in [in]message block
- * @param inLen [in]byte-length of Text
- * @param out [out]processed message
- * @param outLen [out]byte-length of output
+ * @param crt [in]crypto env structure
+ * @param in [in]message block
+ * @param inLen [in]byte-length of Text
+ * @param out [out]processed message
+ * @param outLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_RC4_process(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen)
+int SDRM_RC4_process(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen,
+ cc_u8 *out, cc_u32 *outLen)
{
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rc4ctx == NULL) || (in == NULL) || (out == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rc4ctx == NULL) ||
+ (in == NULL) || (out == NULL))
return CRYPTO_NULL_POINTER;
- }
SDRM_RC4_PRNG(crt->ctx->rc4ctx, in, inLen, out);
if (outLen != NULL)
- {
*outLen = inLen;
- }
return CRYPTO_SUCCESS;
}
/*
- * @fn SDRM_SNOW2_init
- * @brief intialize crypt context for SNOW2
+ * @fn SDRM_SNOW2_init
+ * @brief intialize crypt context for SNOW2
*
- * @param crt [out]crypto env structure
- * @param mode [in]encryption|decryption and mode of operation
- * @param PADDING [in]padding method, not needed
- * @param key [in]user key
- * @param keysize [in]byte-length of key
- * @param IV [in]initial vector
+ * @param crt [out]crypto env structure
+ * @param mode [in]encryption|decryption and mode of operation
+ * @param PADDING [in]padding method, not needed
+ * @param key [in]user key
+ * @param keysize [in]byte-length of key
+ * @param IV [in]initial vector
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_SNOW2_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *key, cc_u32 keysize, cc_u8 *IV)
+int SDRM_SNOW2_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING,
+ cc_u8 *key, cc_u32 keysize, cc_u8 *IV)
{
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->snow2ctx == NULL) || (key == NULL) || (IV == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->snow2ctx == NULL) ||
+ (key == NULL) || (IV == NULL))
return CRYPTO_NULL_POINTER;
- }
if ((keysize != 16) && (keysize != 32))
- {
return CRYPTO_INVALID_ARGUMENT;
- }
SDRM_SNOW2_Setup(crt->ctx->snow2ctx, key, keysize, IV);
@@ -710,62 +726,54 @@ int SDRM_SNOW2_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8
}
/*
- * @fn SDRM_SNOW2_process
- * @brief process message block
+ * @fn SDRM_SNOW2_process
+ * @brief process message block
*
- * @param crt [in]crypto env structure
- * @param in [in]message block
- * @param inLen [in]byte-length of Text
- * @param out [out]processed message
- * @param outLen [out]byte-length of output
+ * @param crt [in]crypto env structure
+ * @param in [in]message block
+ * @param inLen [in]byte-length of Text
+ * @param out [out]processed message
+ * @param outLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_SNOW2_process(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen)
+int SDRM_SNOW2_process(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen,
+ cc_u8 *out, cc_u32 *outLen)
{
cc_u32 i, j, BlockLen, rpt, loc;
cc_u32 keyStream64[16], keyStream;
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->snow2ctx == NULL) || (in == NULL) || (out == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->snow2ctx == NULL) ||
+ (in == NULL) || (out == NULL))
return CRYPTO_NULL_POINTER;
- }
if ((inLen & 0x03) != 0)
- {
return CRYPTO_INVALID_ARGUMENT;
- }
BlockLen = inLen / 64;
- if (crt->ctx->snow2ctx->endian == CRYPTO_LITTLE_ENDIAN)
- { //little endian machine
- for (i = 0; i < BlockLen; i++)
- {
+ if (crt->ctx->snow2ctx->endian == CRYPTO_LITTLE_ENDIAN) {
+ //little endian machine
+ for (i = 0; i < BlockLen; i++) {
SDRM_SNOW2_getKeyStream64(crt->ctx->snow2ctx, keyStream64);
- for (j = 0; j < 16; j++)
- {
+ for (j = 0; j < 16; j++) {
loc = i * 64 + j * 4;
- out[loc ] = (cc_u8)(in[loc ] ^ ((keyStream64[j] >> 24) & 0xff));
+ out[loc] = (cc_u8)(in[loc] ^ ((keyStream64[j] >> 24) & 0xff));
out[loc + 1] = (cc_u8)(in[loc + 1] ^ ((keyStream64[j] >> 16) & 0xff));
out[loc + 2] = (cc_u8)(in[loc + 2] ^ ((keyStream64[j] >> 8) & 0xff));
- out[loc + 3] = (cc_u8)(in[loc + 3] ^ ((keyStream64[j] ) & 0xff));
+ out[loc + 3] = (cc_u8)(in[loc + 3] ^ ((keyStream64[j]) & 0xff));
}
}
- }
- else
- { //big endian machine
- for (i = 0; i < BlockLen; i++)
- {
+ } else {
+ //big endian machine
+ for (i = 0; i < BlockLen; i++) {
SDRM_SNOW2_getKeyStream64(crt->ctx->snow2ctx, keyStream64);
for (j = 0; j < 16; j++)
- {
- ((cc_u32*)(void*)out)[j] = ((cc_u32*)(void*)in)[j] ^ keyStream64[j];
- }
+ ((cc_u32 *)(void *)out)[j] = ((cc_u32 *)(void *)in)[j] ^ keyStream64[j];
}
}
@@ -774,80 +782,70 @@ int SDRM_SNOW2_process(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8
rpt = (inLen - (BlockLen * 64)) / 4;
- if (crt->ctx->snow2ctx->endian == CRYPTO_LITTLE_ENDIAN)
- { //little endian machine
- for (i = 0; i < rpt; i++)
- {
+ if (crt->ctx->snow2ctx->endian == CRYPTO_LITTLE_ENDIAN) {
+ //little endian machine
+ for (i = 0; i < rpt; i++) {
SDRM_SNOW2_getKeyStream(crt->ctx->snow2ctx, &keyStream);
loc = i * 4;
- out[loc ] = (cc_u8)(in[loc ] ^ ((keyStream >> 24) & 0xff));
+ out[loc] = (cc_u8)(in[loc] ^ ((keyStream >> 24) & 0xff));
out[loc + 1] = (cc_u8)(in[loc + 1] ^ ((keyStream >> 16) & 0xff));
out[loc + 2] = (cc_u8)(in[loc + 2] ^ ((keyStream >> 8) & 0xff));
- out[loc + 3] = (cc_u8)(in[loc + 3] ^ ((keyStream ) & 0xff));
+ out[loc + 3] = (cc_u8)(in[loc + 3] ^ ((keyStream) & 0xff));
}
- }
- else
- { //big endian machine
- for (i = 0; i < rpt; i++)
- {
+ } else {
+ //big endian machine
+ for (i = 0; i < rpt; i++) {
SDRM_SNOW2_getKeyStream(crt->ctx->snow2ctx, &keyStream);
- ((cc_u32*)(void*)out)[i] = ((cc_u32*)(void*)in)[i] ^ keyStream;
+ ((cc_u32 *)(void *)out)[i] = ((cc_u32 *)(void *)in)[i] ^ keyStream;
}
}
if (outLen != NULL)
- {
*outLen = inLen;
- }
return CRYPTO_SUCCESS;
}
/*
- * @fn SDRM_DES_init
- * @brief intialize crypt context for des
+ * @fn SDRM_DES_init
+ * @brief intialize crypt context for des
*
- * @param crt [out]crypto env structure
- * @param mode [in]encryption|decryption and mode of operation
- * @param PADDING [in]padding method
- * @param key [in]user key
- * @param keysize [in]byte-length of key
- * @param IV [in]initial vector
+ * @param crt [out]crypto env structure
+ * @param mode [in]encryption|decryption and mode of operation
+ * @param PADDING [in]padding method
+ * @param key [in]user key
+ * @param keysize [in]byte-length of key
+ * @param IV [in]initial vector
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_DES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *key, cc_u32 keysize, cc_u8 *IV)
+int SDRM_DES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING,
+ cc_u8 *key, cc_u32 keysize, cc_u8 *IV)
{
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->desctx == NULL) || (key == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->desctx == NULL) ||
+ (key == NULL))
return CRYPTO_NULL_POINTER;
- }
- if ((keysize != 8) || !(((mode >= 1111) && (mode <= 1115)) || ((mode >= 1121) && (mode <= 1125))))
- {
+ if ((keysize != 8) || !(((mode >= 1111) && (mode <= 1115)) || ((mode >= 1121) &&
+ (mode <= 1125))))
return CRYPTO_INVALID_ARGUMENT;
- }
crt->ctx->desctx->moo = mode;
- if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) && (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING))
- {
+ if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) &&
+ (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING))
return CRYPTO_INVALID_ARGUMENT;
- }
crt->ctx->desctx->padding = PADDING;
if (mode != ID_DEC_ECB && mode != ID_DEC_CBC)
- {
- SDRM_getEncRoundKey(ID_DES, key, (cc_u8*)(crt->ctx->desctx->RoundKey));
- }
+ SDRM_getEncRoundKey(ID_DES, key, (cc_u8 *)(crt->ctx->desctx->RoundKey));
+
else
- {
- SDRM_getDecRoundKey(ID_DES, key, (cc_u8*)(crt->ctx->desctx->RoundKey));
- }
+ SDRM_getDecRoundKey(ID_DES, key, (cc_u8 *)(crt->ctx->desctx->RoundKey));
crt->ctx->desctx->BlockLen = 0;
crt->ctx->desctx->CTR_Count = 0;
@@ -855,213 +853,241 @@ int SDRM_DES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *
memcpy(crt->ctx->desctx->UserKey, key, SDRM_DES_BLOCK_SIZ);
if (IV)
- {
memcpy(crt->ctx->desctx->IV, IV, SDRM_DES_BLOCK_SIZ);
- }
+
else
- {
memset(crt->ctx->desctx->IV, 0x00, SDRM_DES_BLOCK_SIZ);
- }
return CRYPTO_SUCCESS;
}
/*
- * @fn SDRM_DES_process
- * @brief process message block
+ * @fn SDRM_DES_process
+ * @brief process message block
*
- * @param crt [in]crypto env structure
- * @param Text [in]message block
- * @param TextLen [in]byte-length of Text
- * @param output [out]proecessed message
- * @param outputLen [out]byte-length of output
+ * @param crt [in]crypto env structure
+ * @param Text [in]message block
+ * @param TextLen [in]byte-length of Text
+ * @param output [out]proecessed message
+ * @param outputLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_DES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u8 *output, cc_u32 *outputLen)
+int SDRM_DES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen,
+ cc_u8 *output, cc_u32 *outputLen)
{
- int i, Temp;
- int retVal, BlockLen;
- cc_u8 *Block;
+ int i, Temp;
+ int retVal, BlockLen;
+ cc_u8 *Block;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->desctx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
Block = crt->ctx->desctx->Block;
BlockLen = crt->ctx->desctx->BlockLen;
*outputLen = 0;
- if ((TextLen + BlockLen) < SDRM_DES_BLOCK_SIZ)
- {
+ if ((TextLen + BlockLen) < SDRM_DES_BLOCK_SIZ) {
memcpy(Block + BlockLen, Text, TextLen);
crt->ctx->desctx->BlockLen += TextLen;
return CRYPTO_SUCCESS;
}
- if (BlockLen)
- {
+ if (BlockLen) {
memcpy(Block + BlockLen, Text, SDRM_DES_BLOCK_SIZ - BlockLen);
- switch(crt->ctx->desctx->moo)
- {
- case ID_ENC_ECB :
- retVal = SDRM_ECB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CBC :
- retVal = SDRM_CBC_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CFB :
- retVal = SDRM_CFB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_OFB :
- retVal = SDRM_OFB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CTR :
- retVal = SDRM_CTR_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_ECB :
- retVal = SDRM_ECB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CBC :
- retVal = SDRM_CBC_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CFB :
- retVal = SDRM_CFB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_OFB :
- retVal = SDRM_OFB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CTR :
- retVal = SDRM_CTR_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ switch (crt->ctx->desctx->moo) {
+ case ID_ENC_ECB:
+ retVal = SDRM_ECB_Enc(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CBC:
+ retVal = SDRM_CBC_Enc(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CFB:
+ retVal = SDRM_CFB_Enc(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_OFB:
+ retVal = SDRM_OFB_Enc(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CTR:
+ retVal = SDRM_CTR_Enc(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV,
+ crt->ctx->desctx->CTR_Count++);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_ECB:
+ retVal = SDRM_ECB_Dec(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CBC:
+ retVal = SDRM_CBC_Dec(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CFB:
+ retVal = SDRM_CFB_Dec(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_OFB:
+ retVal = SDRM_OFB_Dec(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CTR:
+ retVal = SDRM_CTR_Dec(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV,
+ crt->ctx->desctx->CTR_Count++);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
}
Temp = TextLen + BlockLen - SDRM_DES_BLOCK_SIZ + 1;
- for (i = (SDRM_DES_BLOCK_SIZ - BlockLen) & 0x07; i < Temp; i += SDRM_DES_BLOCK_SIZ)
- {
- switch(crt->ctx->desctx->moo)
- {
- case ID_ENC_ECB :
- retVal = SDRM_ECB_Enc(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CBC :
- retVal = SDRM_CBC_Enc(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CFB :
- retVal = SDRM_CFB_Enc(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_OFB :
- retVal = SDRM_OFB_Enc(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CTR :
- retVal = SDRM_CTR_Enc(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_ECB :
- retVal = SDRM_ECB_Dec(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CBC :
- retVal = SDRM_CBC_Dec(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CFB :
- retVal = SDRM_CFB_Dec(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_OFB :
- retVal = SDRM_OFB_Dec(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CTR :
- retVal = SDRM_CTR_Dec(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+
+ for (i = (SDRM_DES_BLOCK_SIZ - BlockLen) & 0x07; i < Temp;
+ i += SDRM_DES_BLOCK_SIZ) {
+ switch (crt->ctx->desctx->moo) {
+ case ID_ENC_ECB:
+ retVal = SDRM_ECB_Enc(ID_DES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->desctx->RoundKey);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CBC:
+ retVal = SDRM_CBC_Enc(ID_DES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CFB:
+ retVal = SDRM_CFB_Enc(ID_DES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_OFB:
+ retVal = SDRM_OFB_Enc(ID_DES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CTR:
+ retVal = SDRM_CTR_Enc(ID_DES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV,
+ crt->ctx->desctx->CTR_Count++);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_ECB:
+ retVal = SDRM_ECB_Dec(ID_DES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->desctx->RoundKey);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CBC:
+ retVal = SDRM_CBC_Dec(ID_DES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CFB:
+ retVal = SDRM_CFB_Dec(ID_DES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_OFB:
+ retVal = SDRM_OFB_Dec(ID_DES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CTR:
+ retVal = SDRM_CTR_Dec(ID_DES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV,
+ crt->ctx->desctx->CTR_Count++);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
}
crt->ctx->desctx->BlockLen = (SDRM_DES_BLOCK_SIZ + TextLen - i) & 0x07;
- memcpy(Block, Text + TextLen - crt->ctx->desctx->BlockLen, crt->ctx->desctx->BlockLen);
+ memcpy(Block, Text + TextLen - crt->ctx->desctx->BlockLen,
+ crt->ctx->desctx->BlockLen);
return CRYPTO_SUCCESS;
}
/*
- * @fn SDRM_DES_final
- * @brief process final block and padding
+ * @fn SDRM_DES_final
+ * @brief process final block and padding
*
- * @param crt [in]crypto env structure
- * @param input [in]message block
- * @param inputLen [in]byte-length of Text
- * @param output [out]processed message
- * @param outputLen [out]byte-length of output
+ * @param crt [in]crypto env structure
+ * @param input [in]message block
+ * @param inputLen [in]byte-length of Text
+ * @param output [out]processed message
+ * @param outputLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_DES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u8 *output, cc_u32 *outputLen)
+int SDRM_DES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen,
+ cc_u8 *output, cc_u32 *outputLen)
{
- int retVal = CRYPTO_SUCCESS;
- cc_u8 *Block, PADDING[16];
- cc_u32 BlockLen, t;
+ int retVal = CRYPTO_SUCCESS;
+ cc_u8 *Block, PADDING[16];
+ cc_u32 BlockLen, t;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->desctx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
Block = crt->ctx->desctx->Block;
BlockLen = crt->ctx->desctx->BlockLen;
if (crt->ctx->desctx->moo >= ID_DEC_ECB)
- {
goto DECRYPTION;
- }
-//ENCRYPTION:
- if (inputLen != 0)
- {
+ //ENCRYPTION:
+ if (inputLen != 0) {
retVal = SDRM_DES_process(crt, input, inputLen, output, outputLen);
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
retVal = SDRM_DES_final(crt, NULL, 0, output + *outputLen, &t);
*outputLen += t;
@@ -1070,230 +1096,225 @@ int SDRM_DES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u
}
if (outputLen != NULL)
- {
*outputLen = SDRM_DES_BLOCK_SIZ;
- }
//padding
- switch(crt->ctx->desctx->padding)
- {
- case 0 :
- case ID_PKCS5 :
- memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen);
- break;
- case ID_SSL_PADDING :
- memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen - 1, SDRM_DES_BLOCK_SIZ - BlockLen);
- break;
- case ID_ZERO_PADDING :
- memset(Block + BlockLen, 0x00, SDRM_DES_BLOCK_SIZ - BlockLen);
- break;
- case ID_NO_PADDING :
- if (BlockLen == 0)
- {
- if (outputLen)
- {
- *outputLen = 0;
- }
- return CRYPTO_SUCCESS;
- }
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ switch (crt->ctx->desctx->padding) {
+ case 0:
+ case ID_PKCS5:
+ memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen,
+ SDRM_DES_BLOCK_SIZ - BlockLen);
+ break;
+
+ case ID_SSL_PADDING:
+ memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen - 1,
+ SDRM_DES_BLOCK_SIZ - BlockLen);
+ break;
+
+ case ID_ZERO_PADDING:
+ memset(Block + BlockLen, 0x00, SDRM_DES_BLOCK_SIZ - BlockLen);
+ break;
+
+ case ID_NO_PADDING:
+ if (BlockLen == 0) {
+ if (outputLen)
+ *outputLen = 0;
+
+ return CRYPTO_SUCCESS;
+ }
+
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
//encryption
- switch(crt->ctx->desctx->moo)
- {
- case ID_ENC_ECB :
- retVal = SDRM_ECB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey);
- break;
- case ID_ENC_CBC :
- retVal = SDRM_CBC_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- break;
- case ID_ENC_CFB :
- retVal = SDRM_CFB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- break;
- case ID_ENC_OFB :
- retVal = SDRM_OFB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- break;
- case ID_ENC_CTR :
- retVal = SDRM_CTR_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++);
- break;
- default :
- retVal = CRYPTO_INVALID_ARGUMENT;
- break;
+ switch (crt->ctx->desctx->moo) {
+ case ID_ENC_ECB:
+ retVal = SDRM_ECB_Enc(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey);
+ break;
+
+ case ID_ENC_CBC:
+ retVal = SDRM_CBC_Enc(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ break;
+
+ case ID_ENC_CFB:
+ retVal = SDRM_CFB_Enc(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ break;
+
+ case ID_ENC_OFB:
+ retVal = SDRM_OFB_Enc(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ break;
+
+ case ID_ENC_CTR:
+ retVal = SDRM_CTR_Enc(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV,
+ crt->ctx->desctx->CTR_Count++);
+ break;
+
+ default:
+ retVal = CRYPTO_INVALID_ARGUMENT;
+ break;
}
return retVal;
DECRYPTION:
+
if (outputLen != NULL)
- {
*outputLen = 0;
- }
if ((inputLen == 0) && (crt->ctx->desctx->padding == ID_NO_PADDING))
- {
return CRYPTO_SUCCESS;
- }
if ((BlockLen + inputLen) != SDRM_DES_BLOCK_SIZ)
- {
return CRYPTO_INVALID_ARGUMENT;
- }
if (inputLen != 0)
- {
memcpy(Block + BlockLen, input, inputLen);
- }
- switch(crt->ctx->desctx->moo)
- {
- case ID_DEC_ECB :
- retVal = SDRM_ECB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey);
- break;
- case ID_DEC_CBC :
- retVal = SDRM_CBC_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- break;
- case ID_DEC_CFB :
- retVal = SDRM_CFB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- break;
- case ID_DEC_OFB :
- retVal = SDRM_OFB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
- break;
- case ID_DEC_CTR :
- retVal = SDRM_CTR_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++);
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ switch (crt->ctx->desctx->moo) {
+ case ID_DEC_ECB:
+ retVal = SDRM_ECB_Dec(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey);
+ break;
+
+ case ID_DEC_CBC:
+ retVal = SDRM_CBC_Dec(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ break;
+
+ case ID_DEC_CFB:
+ retVal = SDRM_CFB_Dec(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ break;
+
+ case ID_DEC_OFB:
+ retVal = SDRM_OFB_Dec(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV);
+ break;
+
+ case ID_DEC_CTR:
+ retVal = SDRM_CTR_Dec(ID_DES, output, Block,
+ (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV,
+ crt->ctx->desctx->CTR_Count++);
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
//de-padding
t = output[SDRM_DES_BLOCK_SIZ - 1];
- switch(crt->ctx->desctx->padding)
- {
- case 0 :
- case ID_PKCS5 :
- if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1))
- {
- return CRYPTO_INVALID_ARGUMENT;
- }
- memset(PADDING, t, t);
- break;
- case ID_SSL_PADDING :
- ++t;
- if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1))
- {
- return CRYPTO_INVALID_ARGUMENT;
- }
- memset(PADDING, t - 1, t);
- break;
- case ID_ZERO_PADDING :
- {
- cc_u32 tmpLen;
- tmpLen = SDRM_DES_BLOCK_SIZ;
- while((tmpLen != 0x00) && (output[tmpLen - 1] == 0x00))
- {
- tmpLen--;
- }
-
- if (outputLen != NULL)
- {
- *outputLen = tmpLen;
- }
- }
- return CRYPTO_SUCCESS;
- case ID_NO_PADDING :
- if (outputLen != NULL)
- {
- *outputLen = SDRM_DES_BLOCK_SIZ;
- }
- return CRYPTO_SUCCESS;
- default :
- if (outputLen != NULL)
- {
- *outputLen = 0;
- }
+ switch (crt->ctx->desctx->padding) {
+ case 0:
+ case ID_PKCS5:
+ if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1))
return CRYPTO_INVALID_ARGUMENT;
+
+ memset(PADDING, t, t);
+ break;
+
+ case ID_SSL_PADDING:
+ ++t;
+
+ if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1))
+ return CRYPTO_INVALID_ARGUMENT;
+
+ memset(PADDING, t - 1, t);
+ break;
+
+ case ID_ZERO_PADDING: {
+ cc_u32 tmpLen;
+ tmpLen = SDRM_DES_BLOCK_SIZ;
+
+ while ((tmpLen != 0x00) && (output[tmpLen - 1] == 0x00))
+ tmpLen--;
+
+ if (outputLen != NULL)
+ *outputLen = tmpLen;
}
- if (memcmp(PADDING, output + SDRM_DES_BLOCK_SIZ - t, t) != 0)
- {
+ return CRYPTO_SUCCESS;
+
+ case ID_NO_PADDING:
+ if (outputLen != NULL)
+ *outputLen = SDRM_DES_BLOCK_SIZ;
+
+ return CRYPTO_SUCCESS;
+
+ default:
+ if (outputLen != NULL)
+ *outputLen = 0;
+
return CRYPTO_INVALID_ARGUMENT;
}
+ if (memcmp(PADDING, output + SDRM_DES_BLOCK_SIZ - t, t) != 0)
+ return CRYPTO_INVALID_ARGUMENT;
+
if (outputLen != NULL)
- {
*outputLen = SDRM_DES_BLOCK_SIZ - t;
- }
return CRYPTO_SUCCESS;
}
/*
- * @fn SDRM_TDES_init
- * @brief intialize crypt context for triple des
+ * @fn SDRM_TDES_init
+ * @brief intialize crypt context for triple des
*
- * @param crt [out]crypto env structure
- * @param mode [in]encryption|decryption and mode of operation
- * @param PADDING [in]padding method
- * @param key [in]user key
- * @param keysize [in]byte-length of key
- * @param IV [in]initial vector
+ * @param crt [out]crypto env structure
+ * @param mode [in]encryption|decryption and mode of operation
+ * @param PADDING [in]padding method
+ * @param key [in]user key
+ * @param keysize [in]byte-length of key
+ * @param IV [in]initial vector
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_TDES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *key, cc_u32 keysize, cc_u8 *IV)
+int SDRM_TDES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING,
+ cc_u8 *key, cc_u32 keysize, cc_u8 *IV)
{
- if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->tdesctx == NULL) || (key == NULL))
- {
+ if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->tdesctx == NULL) ||
+ (key == NULL))
return CRYPTO_NULL_POINTER;
- }
- if (((keysize != 16) && (keysize != 24)) || !(((mode >= 1111) && (mode <= 1115)) || ((mode >= 1121) && (mode <= 1125))))
- {
+ if (((keysize != 16) && (keysize != 24)) || !(((mode >= 1111) &&
+ (mode <= 1115)) || ((mode >= 1121) && (mode <= 1125))))
return CRYPTO_INVALID_ARGUMENT;
- }
crt->ctx->tdesctx->moo = mode;
- if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) && (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING))
- {
+ if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) &&
+ (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING))
return CRYPTO_INVALID_ARGUMENT;
- }
crt->ctx->tdesctx->padding = PADDING;
- if ((mode != ID_DEC_ECB) && (mode != ID_DEC_CBC))
- {
+ if ((mode != ID_DEC_ECB) && (mode != ID_DEC_CBC)) {
if (keysize == 16)
- {
- SDRM_getEncRoundKey(ID_TDES_EDE2, key, (cc_u8*)(crt->ctx->tdesctx->RoundKey));
- }
+ SDRM_getEncRoundKey(ID_TDES_EDE2, key, (cc_u8 *)(crt->ctx->tdesctx->RoundKey));
+
else
- {
- SDRM_getEncRoundKey(ID_TDES_EDE3, key, (cc_u8*)(crt->ctx->tdesctx->RoundKey));
- }
- }
- else
- {
+ SDRM_getEncRoundKey(ID_TDES_EDE3, key, (cc_u8 *)(crt->ctx->tdesctx->RoundKey));
+ } else {
if (keysize == 16)
- {
- SDRM_getDecRoundKey(ID_TDES_EDE2, key, (cc_u8*)(crt->ctx->tdesctx->RoundKey));
- }
+ SDRM_getDecRoundKey(ID_TDES_EDE2, key, (cc_u8 *)(crt->ctx->tdesctx->RoundKey));
+
else
- {
- SDRM_getDecRoundKey(ID_TDES_EDE3, key, (cc_u8*)(crt->ctx->tdesctx->RoundKey));
- }
+ SDRM_getDecRoundKey(ID_TDES_EDE3, key, (cc_u8 *)(crt->ctx->tdesctx->RoundKey));
}
crt->ctx->tdesctx->BlockLen = 0;
@@ -1302,215 +1323,243 @@ int SDRM_TDES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8
memcpy(crt->ctx->tdesctx->UserKey, key, SDRM_DES_BLOCK_SIZ);
if (IV)
- {
memcpy(crt->ctx->tdesctx->IV, IV, SDRM_DES_BLOCK_SIZ);
- }
+
else
- {
memset(crt->ctx->tdesctx->IV, 0x00, SDRM_DES_BLOCK_SIZ);
- }
return CRYPTO_SUCCESS;
}
/*
- * @fn int SDRM_TDES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u8 *output, cc_u32 *outputLen)
- * @brief process message block
+ * @fn int SDRM_TDES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u8 *output, cc_u32 *outputLen)
+ * @brief process message block
*
- * @param crt [in]crypto env structure
- * @param Text [in]message block
- * @param TextLen [in]byte-length of Text
- * @param output [out]proecessed message
- * @param outputLen [out]byte-length of output
+ * @param crt [in]crypto env structure
+ * @param Text [in]message block
+ * @param TextLen [in]byte-length of Text
+ * @param output [out]proecessed message
+ * @param outputLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_TDES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u8 *output, cc_u32 *outputLen)
+int SDRM_TDES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen,
+ cc_u8 *output, cc_u32 *outputLen)
{
- int i, Temp;
- int retVal, BlockLen;
- cc_u8 *Block;
+ int i, Temp;
+ int retVal, BlockLen;
+ cc_u8 *Block;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->tdesctx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
Block = crt->ctx->tdesctx->Block;
BlockLen = crt->ctx->tdesctx->BlockLen;
*outputLen = 0;
- if ((TextLen + BlockLen) < SDRM_DES_BLOCK_SIZ)
- {
+ if ((TextLen + BlockLen) < SDRM_DES_BLOCK_SIZ) {
memcpy(Block + BlockLen, Text, TextLen);
crt->ctx->tdesctx->BlockLen += TextLen;
return CRYPTO_SUCCESS;
}
- if (BlockLen)
- {
+ if (BlockLen) {
memcpy(Block + BlockLen, Text, SDRM_DES_BLOCK_SIZ - BlockLen);
- switch(crt->ctx->tdesctx->moo)
- {
- case ID_ENC_ECB :
- retVal = SDRM_ECB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CBC :
- retVal = SDRM_CBC_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CFB :
- retVal = SDRM_CFB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_OFB :
- retVal = SDRM_OFB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CTR :
- retVal = SDRM_CTR_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_ECB :
- retVal = SDRM_ECB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CBC :
- retVal = SDRM_CBC_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CFB :
- retVal = SDRM_CFB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_OFB :
- retVal = SDRM_OFB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CTR :
- retVal = SDRM_CTR_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ switch (crt->ctx->tdesctx->moo) {
+ case ID_ENC_ECB:
+ retVal = SDRM_ECB_Enc(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CBC:
+ retVal = SDRM_CBC_Enc(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CFB:
+ retVal = SDRM_CFB_Enc(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_OFB:
+ retVal = SDRM_OFB_Enc(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CTR:
+ retVal = SDRM_CTR_Enc(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV,
+ crt->ctx->tdesctx->CTR_Count++);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_ECB:
+ retVal = SDRM_ECB_Dec(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CBC:
+ retVal = SDRM_CBC_Dec(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CFB:
+ retVal = SDRM_CFB_Dec(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_OFB:
+ retVal = SDRM_OFB_Dec(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CTR:
+ retVal = SDRM_CTR_Dec(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV,
+ crt->ctx->tdesctx->CTR_Count++);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
}
Temp = TextLen + BlockLen - SDRM_DES_BLOCK_SIZ + 1;
- for (i = (SDRM_DES_BLOCK_SIZ - BlockLen) & 0x07; i < Temp; i += SDRM_DES_BLOCK_SIZ)
- {
- switch(crt->ctx->tdesctx->moo)
- {
- case ID_ENC_ECB :
- retVal = SDRM_ECB_Enc(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CBC :
- retVal = SDRM_CBC_Enc(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CFB :
- retVal = SDRM_CFB_Enc(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_OFB :
- retVal = SDRM_OFB_Enc(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_ENC_CTR :
- retVal = SDRM_CTR_Enc(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_ECB :
- retVal = SDRM_ECB_Dec(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CBC :
- retVal = SDRM_CBC_Dec(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CFB :
- retVal = SDRM_CFB_Dec(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_OFB :
- retVal = SDRM_OFB_Dec(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- case ID_DEC_CTR :
- retVal = SDRM_CTR_Dec(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++);
- *outputLen += SDRM_DES_BLOCK_SIZ;
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+
+ for (i = (SDRM_DES_BLOCK_SIZ - BlockLen) & 0x07; i < Temp;
+ i += SDRM_DES_BLOCK_SIZ) {
+ switch (crt->ctx->tdesctx->moo) {
+ case ID_ENC_ECB:
+ retVal = SDRM_ECB_Enc(ID_TDES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CBC:
+ retVal = SDRM_CBC_Enc(ID_TDES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CFB:
+ retVal = SDRM_CFB_Enc(ID_TDES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_OFB:
+ retVal = SDRM_OFB_Enc(ID_TDES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_ENC_CTR:
+ retVal = SDRM_CTR_Enc(ID_TDES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV,
+ crt->ctx->tdesctx->CTR_Count++);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_ECB:
+ retVal = SDRM_ECB_Dec(ID_TDES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CBC:
+ retVal = SDRM_CBC_Dec(ID_TDES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CFB:
+ retVal = SDRM_CFB_Dec(ID_TDES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_OFB:
+ retVal = SDRM_OFB_Dec(ID_TDES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ case ID_DEC_CTR:
+ retVal = SDRM_CTR_Dec(ID_TDES, output + *outputLen, Text + i,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV,
+ crt->ctx->tdesctx->CTR_Count++);
+ *outputLen += SDRM_DES_BLOCK_SIZ;
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
}
crt->ctx->tdesctx->BlockLen = (SDRM_DES_BLOCK_SIZ + TextLen - i) & 0x07;
- memcpy(Block, Text + TextLen - crt->ctx->tdesctx->BlockLen, crt->ctx->tdesctx->BlockLen);
+ memcpy(Block, Text + TextLen - crt->ctx->tdesctx->BlockLen,
+ crt->ctx->tdesctx->BlockLen);
return CRYPTO_SUCCESS;
}
/*
- * @fn int SDRM_TDES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u8 *output, cc_u32 *outputLen)
- * @brief process final block and padding
+ * @fn int SDRM_TDES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u8 *output, cc_u32 *outputLen)
+ * @brief process final block and padding
*
- * @param crt [in]crypto env structure
- * @param input [in]message block
- * @param inputLen [in]byte-length of Text
- * @param output [out]processed message
- * @param outputLen [out]byte-length of output
+ * @param crt [in]crypto env structure
+ * @param input [in]message block
+ * @param inputLen [in]byte-length of Text
+ * @param output [out]processed message
+ * @param outputLen [out]byte-length of output
*
- * @return CRYPTO_SUCCESS if success
- * \n CRYPTO_NULL_POINTER if given argument is a null pointer
- * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
+ * @return CRYPTO_SUCCESS if success
+ * \n CRYPTO_NULL_POINTER if given argument is a null pointer
+ * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid
*/
-int SDRM_TDES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u8 *output, cc_u32 *outputLen)
+int SDRM_TDES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen,
+ cc_u8 *output, cc_u32 *outputLen)
{
- int retVal = CRYPTO_SUCCESS;
- cc_u8 *Block, PADDING[16];
- cc_u32 BlockLen, t;
+ int retVal = CRYPTO_SUCCESS;
+ cc_u8 *Block, PADDING[16];
+ cc_u32 BlockLen, t;
if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->tdesctx == NULL))
- {
return CRYPTO_NULL_POINTER;
- }
Block = crt->ctx->tdesctx->Block;
BlockLen = crt->ctx->tdesctx->BlockLen;
if (crt->ctx->tdesctx->moo >= ID_DEC_ECB)
- {
goto DECRYPTION;
- }
-//ENCRYPTION:
- if (inputLen != 0)
- {
+ //ENCRYPTION:
+ if (inputLen != 0) {
retVal = SDRM_TDES_process(crt, input, inputLen, output, outputLen);
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
retVal = SDRM_TDES_final(crt, NULL, 0, output + *outputLen, &t);
*outputLen += t;
@@ -1519,171 +1568,177 @@ int SDRM_TDES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_
}
if (outputLen != NULL)
- {
*outputLen = SDRM_DES_BLOCK_SIZ;
- }
//padding
- switch(crt->ctx->tdesctx->padding)
- {
- case 0 :
- case ID_PKCS5 :
- memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen);
- break;
- case ID_SSL_PADDING :
- memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen - 1, SDRM_DES_BLOCK_SIZ - BlockLen);
- break;
- case ID_ZERO_PADDING :
- memset(Block + BlockLen, 0x00, SDRM_DES_BLOCK_SIZ - BlockLen);
- break;
- case ID_NO_PADDING :
- if (BlockLen == 0)
- {
- if (outputLen)
- {
- *outputLen = 0;
- }
- return CRYPTO_SUCCESS;
- }
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ switch (crt->ctx->tdesctx->padding) {
+ case 0:
+ case ID_PKCS5:
+ memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen,
+ SDRM_DES_BLOCK_SIZ - BlockLen);
+ break;
+
+ case ID_SSL_PADDING:
+ memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen - 1,
+ SDRM_DES_BLOCK_SIZ - BlockLen);
+ break;
+
+ case ID_ZERO_PADDING:
+ memset(Block + BlockLen, 0x00, SDRM_DES_BLOCK_SIZ - BlockLen);
+ break;
+
+ case ID_NO_PADDING:
+ if (BlockLen == 0) {
+ if (outputLen)
+ *outputLen = 0;
+
+ return CRYPTO_SUCCESS;
+ }
+
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
//encryption
- switch(crt->ctx->tdesctx->moo)
- {
- case ID_ENC_ECB :
- retVal = SDRM_ECB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey);
- break;
- case ID_ENC_CBC :
- retVal = SDRM_CBC_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- break;
- case ID_ENC_CFB :
- retVal = SDRM_CFB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- break;
- case ID_ENC_OFB :
- retVal = SDRM_OFB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- break;
- case ID_ENC_CTR :
- retVal = SDRM_CTR_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++);
- break;
- default :
- retVal = CRYPTO_INVALID_ARGUMENT;
- break;
+ switch (crt->ctx->tdesctx->moo) {
+ case ID_ENC_ECB:
+ retVal = SDRM_ECB_Enc(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey);
+ break;
+
+ case ID_ENC_CBC:
+ retVal = SDRM_CBC_Enc(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ break;
+
+ case ID_ENC_CFB:
+ retVal = SDRM_CFB_Enc(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ break;
+
+ case ID_ENC_OFB:
+ retVal = SDRM_OFB_Enc(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ break;
+
+ case ID_ENC_CTR:
+ retVal = SDRM_CTR_Enc(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV,
+ crt->ctx->tdesctx->CTR_Count++);
+ break;
+
+ default:
+ retVal = CRYPTO_INVALID_ARGUMENT;
+ break;
}
return retVal;
DECRYPTION:
+
if (outputLen != NULL)
- {
*outputLen = 0;
- }
if ((inputLen == 0) && (crt->ctx->tdesctx->padding == ID_NO_PADDING))
- {
return CRYPTO_SUCCESS;
- }
if ((BlockLen + inputLen) != SDRM_DES_BLOCK_SIZ)
- {
return CRYPTO_INVALID_ARGUMENT;
- }
if (inputLen != 0)
- {
memcpy(Block + BlockLen, input, inputLen);
- }
- switch(crt->ctx->tdesctx->moo)
- {
- case ID_DEC_ECB :
- retVal = SDRM_ECB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey);
- break;
- case ID_DEC_CBC :
- retVal = SDRM_CBC_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- break;
- case ID_DEC_CFB :
- retVal = SDRM_CFB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- break;
- case ID_DEC_OFB :
- retVal = SDRM_OFB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
- break;
- case ID_DEC_CTR :
- retVal = SDRM_CTR_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++);
- break;
- default :
- return CRYPTO_INVALID_ARGUMENT;
+ switch (crt->ctx->tdesctx->moo) {
+ case ID_DEC_ECB:
+ retVal = SDRM_ECB_Dec(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey);
+ break;
+
+ case ID_DEC_CBC:
+ retVal = SDRM_CBC_Dec(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ break;
+
+ case ID_DEC_CFB:
+ retVal = SDRM_CFB_Dec(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ break;
+
+ case ID_DEC_OFB:
+ retVal = SDRM_OFB_Dec(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV);
+ break;
+
+ case ID_DEC_CTR:
+ retVal = SDRM_CTR_Dec(ID_TDES, output, Block,
+ (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV,
+ crt->ctx->tdesctx->CTR_Count++);
+ break;
+
+ default:
+ return CRYPTO_INVALID_ARGUMENT;
}
if (retVal != CRYPTO_SUCCESS)
- {
return retVal;
- }
//de-padding
t = output[SDRM_DES_BLOCK_SIZ - 1];
- switch(crt->ctx->tdesctx->padding)
- {
- case 0 :
- case ID_PKCS5 :
- if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1))
- {
- return CRYPTO_INVALID_ARGUMENT;
- }
- memset(PADDING, t, t);
- break;
- case ID_SSL_PADDING :
- ++t;
- if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1))
- {
- return CRYPTO_INVALID_ARGUMENT;
- }
- memset(PADDING, t - 1, t);
- break;
- case ID_ZERO_PADDING :
- {
- cc_u32 tmpLen;
- tmpLen = SDRM_TDES_BLOCK_SIZ;
- while((tmpLen != 0x00) && (output[tmpLen - 1] == 0x00))
- {
- tmpLen--;
- }
-
- if (outputLen != NULL)
- {
- *outputLen = tmpLen;
- }
- }
- return CRYPTO_SUCCESS;
- case ID_NO_PADDING :
- if (outputLen != NULL)
- {
- *outputLen = SDRM_TDES_BLOCK_SIZ;
- }
- return CRYPTO_SUCCESS;
- default :
- if (outputLen != NULL)
- {
- *outputLen = 0;
- }
+ switch (crt->ctx->tdesctx->padding) {
+ case 0:
+ case ID_PKCS5:
+ if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1))
return CRYPTO_INVALID_ARGUMENT;
+
+ memset(PADDING, t, t);
+ break;
+
+ case ID_SSL_PADDING:
+ ++t;
+
+ if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1))
+ return CRYPTO_INVALID_ARGUMENT;
+
+ memset(PADDING, t - 1, t);
+ break;
+
+ case ID_ZERO_PADDING: {
+ cc_u32 tmpLen;
+ tmpLen = SDRM_TDES_BLOCK_SIZ;
+
+ while ((tmpLen != 0x00) && (output[tmpLen - 1] == 0x00))
+ tmpLen--;
+
+ if (outputLen != NULL)
+ *outputLen = tmpLen;
}
- if (memcmp(PADDING, output + SDRM_TDES_BLOCK_SIZ - t, t) != 0)
- {
+ return CRYPTO_SUCCESS;
+
+ case ID_NO_PADDING:
+ if (outputLen != NULL)
+ *outputLen = SDRM_TDES_BLOCK_SIZ;
+
+ return CRYPTO_SUCCESS;
+
+ default:
+ if (outputLen != NULL)
+ *outputLen = 0;
+
return CRYPTO_INVALID_ARGUMENT;
}
+ if (memcmp(PADDING, output + SDRM_TDES_BLOCK_SIZ - t, t) != 0)
+ return CRYPTO_INVALID_ARGUMENT;
+
if (outputLen != NULL)
- {
*outputLen = SDRM_DES_BLOCK_SIZ - t;
- }
return CRYPTO_SUCCESS;
}
-/***************************** End of File *****************************/
\ No newline at end of file
+/***************************** End of File *****************************/
diff --git a/ssflib/dep/cryptocore/source/middle/cc_tdes.c b/ssflib/dep/cryptocore/source/middle/cc_tdes.c
index 1698bac..adcfc42 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_tdes.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_tdes.c
@@ -31,44 +31,41 @@
// Functions
////////////////////////////////////////////////////////////////////////////
/*
- * @fn SDRM_TDES_KeySched
- * @brief Expand the cipher key into the encryption key schedule
+ * @fn SDRM_TDES_KeySched
+ * @brief Expand the cipher key into the encryption key schedule
*
- * @param RoundKey [out]generated round key
- * @param UserKey [in]user key, 16 or 24 byte
- * @param KeyLen [in]byte-length of UserKey
- * @param RKStep [in]operation mode
+ * @param RoundKey [out]generated round key
+ * @param UserKey [in]user key, 16 or 24 byte
+ * @param KeyLen [in]byte-length of UserKey
+ * @param RKStep [in]operation mode
*
- * @return the number of rounds for the given cipher key size
+ * @return the number of rounds for the given cipher key size
*/
-int SDRM_TDES_KeySched(cc_u8 *RoundKey, cc_u8 *UserKey, cc_u32 KeyLen, cc_u32 RKStep)
+int SDRM_TDES_KeySched(cc_u8 *RoundKey, cc_u8 *UserKey, cc_u32 KeyLen,
+ cc_u32 RKStep)
{
- if (RKStep == 1)
- {
+ if (RKStep == 1) {
SDRM_DES_KeySched(RoundKey, UserKey, 0, 1);
- SDRM_DES_KeySched(RoundKey + 128, UserKey + 8, 15, (cc_u32)-1);
+ SDRM_DES_KeySched(RoundKey + 128, UserKey + 8, 15, (cc_u32) - 1);
- if (KeyLen == 16)
- { //2-key des
+ if (KeyLen == 16) {
+ //2-key des
memcpy(RoundKey + 256, RoundKey, 128);
- }
- else
- { //3-key des
+ } else {
+ //3-key des
SDRM_DES_KeySched(RoundKey + 256, UserKey + 16, 0, 1);
}
- }
- else {
- SDRM_DES_KeySched(RoundKey + 256, UserKey, 15, (cc_u32)-1);
+ } else {
+ SDRM_DES_KeySched(RoundKey + 256, UserKey, 15, (cc_u32) - 1);
SDRM_DES_KeySched(RoundKey + 128, UserKey + 8, 0, 1);
- if (KeyLen == 16)
- { //2-key des
+ if (KeyLen == 16) {
+ //2-key des
memcpy(RoundKey, RoundKey + 256, 128);
- }
- else
- { //3-key des
- SDRM_DES_KeySched(RoundKey, UserKey + 16, 15, (cc_u32)-1);
+ } else {
+ //3-key des
+ SDRM_DES_KeySched(RoundKey, UserKey + 16, 15, (cc_u32) - 1);
}
}
@@ -76,20 +73,20 @@ int SDRM_TDES_KeySched(cc_u8 *RoundKey, cc_u8 *UserKey, cc_u32 KeyLen, cc_u32 RK
}
/*
- * @fn SDRM_TDES_Encryption
- * @brief Triple DES processing for one block
+ * @fn SDRM_TDES_Encryption
+ * @brief Triple DES processing for one block
*
- * @param RoundKey [in]expanded round key
- * @param msg [in]8 byte plaintext
- * @param out [out]8 byte ciphertext
+ * @param RoundKey [in]expanded round key
+ * @param msg [in]8 byte plaintext
+ * @param out [out]8 byte ciphertext
*
- * @return CRYPTO_SUCCESS if no error is occured
+ * @return CRYPTO_SUCCESS if no error is occured
*/
int SDRM_TDES_Encryption(cc_u32 RoundKey[][2], cc_u8 *msg, cc_u8 *out)
{
cc_u8 buf[8];
- SDRM_DES_Encryption(RoundKey , msg, buf);
+ SDRM_DES_Encryption(RoundKey, msg, buf);
SDRM_DES_Encryption(RoundKey + 16, buf, buf);
SDRM_DES_Encryption(RoundKey + 32, buf, out);
@@ -98,20 +95,20 @@ int SDRM_TDES_Encryption(cc_u32 RoundKey[][2], cc_u8 *msg, cc_u8 *out)
/*
- * @fn SDRM_TDES64_Encryption
- * @brief one block Triple DES Encryption
+ * @fn SDRM_TDES64_Encryption
+ * @brief one block Triple DES Encryption
*
- * @param cipherText [out]encrypted text
- * @param plainText [in]plain text
- * @param UserKey [in]user key
+ * @param cipherText [out]encrypted text
+ * @param plainText [in]plain text
+ * @param UserKey [in]user key
*
- * @return CRYPTO_SUCCESS if success
+ * @return CRYPTO_SUCCESS if success
*/
int SDRM_TDES64_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey)
{
cc_u32 RoundKey[48][2];
- SDRM_TDES_KeySched((cc_u8*)RoundKey, UserKey, 16, 1);
+ SDRM_TDES_KeySched((cc_u8 *)RoundKey, UserKey, 16, 1);
SDRM_TDES_Encryption(RoundKey, plainText, cipherText);
@@ -119,24 +116,24 @@ int SDRM_TDES64_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey)
}
/*
- * @fn SDRM_TDES64_Decryption
- * @brief one block Triple DES Decryption
+ * @fn SDRM_TDES64_Decryption
+ * @brief one block Triple DES Decryption
*
- * @param plainText [out]decrypted text
- * @param cipherText [in]cipher text
- * @param UserKey [in]user key
+ * @param plainText [out]decrypted text
+ * @param cipherText [in]cipher text
+ * @param UserKey [in]user key
*
- * @return CRYPTO_SUCCESS if success
+ * @return CRYPTO_SUCCESS if success
*/
int SDRM_TDES64_Decryption(cc_u8 *plainText, cc_u8 *cipherText, cc_u8 *UserKey)
{
cc_u32 RoundKey[48][2];
- SDRM_TDES_KeySched((cc_u8*)RoundKey, UserKey, 16, (cc_u32)-1);
+ SDRM_TDES_KeySched((cc_u8 *)RoundKey, UserKey, 16, (cc_u32) - 1);
SDRM_TDES_Encryption(RoundKey, cipherText, plainText);
return CRYPTO_SUCCESS;
}
-/***************************** End of File *****************************/
\ No newline at end of file
+/***************************** End of File *****************************/
diff --git a/ssflib/dep/uci/source/uci_aes_xcbc_mac.c b/ssflib/dep/uci/source/uci_aes_xcbc_mac.c
index da5dbe7..e107f91 100644
--- a/ssflib/dep/uci/source/uci_aes_xcbc_mac.c
+++ b/ssflib/dep/uci/source/uci_aes_xcbc_mac.c
@@ -29,13 +29,17 @@
#include "cc_aes.h"
static unsigned char k1[] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
+ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
+ };
static unsigned char k2[] = {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
- 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02};
+ 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02
+ };
static unsigned char k3[] = {0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
- 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
+ 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
+ };
-int xcbc_init(aes_xcbc_state *xcbc, unsigned char *key, unsigned int keylen) {
+int xcbc_init(aes_xcbc_state *xcbc, unsigned char *key, unsigned int keylen)
+{
/*
(1) Derive 3 128-bit keys (K1, K2 and K3) from the 128-bit secret
@@ -44,15 +48,15 @@ int xcbc_init(aes_xcbc_state *xcbc, unsigned char *key, unsigned int keylen) {
K2 = 0x02020202020202020202020202020202 encrypted with Key K
K3 = 0x03030303030303030303030303030303 encrypted with Key K
- (2) Define E[0](iv) = 0x00000000000000000000000000000000
+ (2) Define E[0](iv) = 0x00000000000000000000000000000000
*/
- if (keylen != 16) {
+ if (keylen != 16)
return 0;
- }
- if (xcbc == NULL) {
+
+ if (xcbc == NULL)
return 0;
- }
+
memcpy(xcbc->key, key, 16);
SDRM_AES128_Encryption(xcbc->K[0], k1, xcbc->key);
SDRM_AES128_Encryption(xcbc->K[1], k2, xcbc->key);
@@ -64,7 +68,8 @@ int xcbc_init(aes_xcbc_state *xcbc, unsigned char *key, unsigned int keylen) {
memset(xcbc->IV, 0, MAXBLOCKSIZE);
return 1;
}
-int xcbc_process(aes_xcbc_state *xcbc, unsigned char *in, unsigned int inlen) {
+int xcbc_process(aes_xcbc_state *xcbc, unsigned char *in, unsigned int inlen)
+{
/*
(3) For each block M[i], where i = 1 ... n-1:
@@ -73,34 +78,40 @@ int xcbc_process(aes_xcbc_state *xcbc, unsigned char *in, unsigned int inlen) {
*/
unsigned int x;
- if (xcbc == NULL) {
+
+ if (xcbc == NULL)
return 0;
- }
+
if (xcbc->buflen == 0) {
while (inlen > xcbc->blocksize) {
- for (x = 0; x < xcbc->blocksize; x++) {
+ for (x = 0; x < xcbc->blocksize; x++)
xcbc->IV[x] ^= in[x];
- }
+
SDRM_AES128_Encryption(xcbc->IV, xcbc->IV, xcbc->key);
in += xcbc->blocksize;
inlen -= xcbc->blocksize;
}
}
+
while (inlen) {
if (xcbc->buflen == xcbc->blocksize) {
SDRM_AES128_Encryption(xcbc->IV, xcbc->IV, xcbc->key);
xcbc->buflen = 0;
}
+
xcbc->IV[xcbc->buflen++] ^= *in++;
--inlen;
}
+
return 1;
}
-int xcbc_done(aes_xcbc_state *xcbc, unsigned char *out, unsigned int *outlen) {
+int xcbc_done(aes_xcbc_state *xcbc, unsigned char *out, unsigned int *outlen)
+{
unsigned int x;
- if (xcbc == NULL || out == NULL) {
+
+ if (xcbc == NULL || out == NULL)
return 0;
- }
+
/*
(4)
a) If the blocksize of M[n] is 128 bits:
@@ -108,13 +119,12 @@ int xcbc_done(aes_xcbc_state *xcbc, unsigned char *out, unsigned int *outlen) {
Key K1, yielding E[n].
*/
if (xcbc->buflen == xcbc->blocksize) {
- for (x = 0; x < xcbc->blocksize; x++) {
+ for (x = 0; x < xcbc->blocksize; x++)
xcbc->IV[x] ^= xcbc->K[1][x];
- }
} else {
/*
(4)
- b) If the blocksize of M[n] is less than 128 bits:
+ b) If the blocksize of M[n] is less than 128 bits:
i) Pad M[n] with a single "1" bit, followed by the number of
"0" bits (possibly none) required to increase M[n]'s
@@ -125,14 +135,16 @@ int xcbc_done(aes_xcbc_state *xcbc, unsigned char *out, unsigned int *outlen) {
*/
xcbc->IV[xcbc->buflen] ^= 0x80;
- for (x = 0; x < xcbc->blocksize; x++) {
+
+ for (x = 0; x < xcbc->blocksize; x++)
xcbc->IV[x] ^= xcbc->K[2][x];
- }
}
+
SDRM_AES128_Encryption(out, xcbc->IV, xcbc->key);
- if (outlen != NULL) {
+
+ if (outlen != NULL)
*outlen = xcbc->blocksize;
- }
+
return 1;
}
diff --git a/ssflib/dep/uci/source/uci_api.c b/ssflib/dep/uci/source/uci_api.c
index 7989065..a21522c 100644
--- a/ssflib/dep/uci/source/uci_api.c
+++ b/ssflib/dep/uci/source/uci_api.c
@@ -34,382 +34,431 @@
#if 1
#define TC_PRINT(fmt...) \
- do { printf(fmt);}while(0)
+ do { printf(fmt); } while (0)
#else
#define TC_PRINT(fmt...)\
- do {;}while(0)
+ do {; } while (0)
#endif
-/*! \brief print out by byte unit */
+/*! \brief print out by byte unit */
#undef PrintBYTE
#define g_bTAdbug 1
#define TZ_PRINT(fmt...) \
- do {if (g_bTAdbug) printf("[SSFLIB] ");printf(fmt);}while(0)
+ do {if (g_bTAdbug) printf("[SSFLIB] "); printf(fmt); } while (0)
#define TZ_ERROR(fmt...) \
- do {if (g_bTAdbug) printf("[SSFLIB] ");printf(fmt);}while(0)
-#define PrintBYTE(msg, Data, DataLen) { \
- int idx; \
- TC_PRINT("%10s =", msg); \
- for(idx=0; idx<(int)DataLen; idx++) { \
- if((idx!=0) && ((idx%16)==0)) TC_PRINT("\n"); \
- if((idx % 4) == 0) TC_PRINT(" 0x"); \
- TC_PRINT("%.2x", Data[idx]); \
- } \
- TC_PRINT("\n"); \
-}
-
-int uci_context_alloc(unsigned int algorithm, uci_engine_config_e config, UCI_HANDLE* context) {
+ do {if (g_bTAdbug) printf("[SSFLIB] "); printf(fmt); } while (0)
+#define PrintBYTE(msg, Data, DataLen) { \
+ int idx; \
+ TC_PRINT("%10s =", msg); \
+ for (idx = 0; idx < (int)DataLen; idx++) { \
+ if ((idx != 0) && ((idx%16) == 0)) TC_PRINT("\n"); \
+ if ((idx % 4) == 0) TC_PRINT(" 0x"); \
+ TC_PRINT("%.2x", Data[idx]); \
+ } \
+ TC_PRINT("\n"); \
+ }
+
+int uci_context_alloc(unsigned int algorithm, uci_engine_config_e config,
+ UCI_HANDLE *context)
+{
unsigned int conf = SDRM_LOW_HALF(config);
uci_context_s *ctx;
- if (context == NULL) {
+ if (context == NULL)
return UCI_ERROR;
- }
- if (algorithm < ID_UCI_X931 || algorithm > ID_UCI_AE_CCM) {
+
+ if (algorithm < ID_UCI_X931 || algorithm > ID_UCI_AE_CCM)
return UCI_ERROR;
- }
+
#if 0
- if(algorithm == ID_UCI_AE_GCM)
- {
+
+ if (algorithm == ID_UCI_AE_GCM) {
ctx = OsaMalloc(sizeof(uci_context_s));
ctx->imp = OsaMalloc(sizeof(gcm_context));
ctx->alg = ID_UCI_AE_GCM;
return (int)ctx;
}
- if(algorithm == ID_UCI_AE_CCM)
- {
+
+ if (algorithm == ID_UCI_AE_CCM) {
ctx = OsaMalloc(sizeof(uci_context_s));
ctx->imp = OsaMalloc(sizeof(aes_ccm_context));
ctx->alg = ID_UCI_AE_CCM;
return (int)ctx;
}
+
#endif
+
if (algorithm == ID_UCI_XCBCMAC) {
- ctx = (uci_context_s*)OsaMalloc(sizeof(uci_context_s));
- if (ctx == NULL) {
+ ctx = (uci_context_s *)OsaMalloc(sizeof(uci_context_s));
+
+ if (ctx == NULL)
return UCI_ERROR;
- }
+
ctx->imp = (aes_xcbc_state *)OsaMalloc(sizeof(aes_xcbc_state));
ctx->alg = ID_UCI_XCBCMAC;
*context = ctx;
return UCI_SUCCESS;
}
- if (conf == UCI_SW_CRYPTOCORE) {
+
+ if (conf == UCI_SW_CRYPTOCORE)
return cryptocore_context_alloc(algorithm, context);
- }
- if (conf == UCI_HW) {
+
+ if (conf == UCI_HW)
return hwcrypto_context_alloc(algorithm, config, context);
- }
return UCI_ERROR;
}
-int uci_context_free(UCI_HANDLE oh) {
- uci_context_s *pctx = (uci_context_s*)oh;
+int uci_context_free(UCI_HANDLE oh)
+{
+ uci_context_s *pctx = (uci_context_s *)oh;
unsigned int conf;
- if (pctx == NULL) {
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
+
conf = SDRM_LOW_HALF(pctx->config);
+
if (pctx->alg == ID_UCI_AE_GCM || pctx->alg == ID_UCI_AE_CCM
- || pctx->alg == ID_UCI_XCBCMAC) {
+ || pctx->alg == ID_UCI_XCBCMAC) {
OsaFree(pctx->imp);
OsaFree(pctx);
return UCI_SUCCESS;
}
- if (conf == UCI_SW_CRYPTOCORE) {
+
+ if (conf == UCI_SW_CRYPTOCORE)
return cryptocore_context_free(oh);
- }
- if (conf == UCI_HW) {
+
+ if (conf == UCI_HW)
return hwcrypto_context_free(oh);
- }
+
return UCI_ERROR;
}
-int uci_md_init(UCI_HANDLE oh) {
+int uci_md_init(UCI_HANDLE oh)
+{
return cryptocore_md_init(oh);
}
-int uci_md_update(UCI_HANDLE oh, unsigned char *msg, unsigned int msg_len) {
+int uci_md_update(UCI_HANDLE oh, unsigned char *msg, unsigned int msg_len)
+{
return cryptocore_md_update(oh, msg, msg_len);
}
-int uci_md_final(UCI_HANDLE oh, unsigned char *output) {
+int uci_md_final(UCI_HANDLE oh, unsigned char *output)
+{
return cryptocore_md_final(oh, output);
}
int uci_md_get_hash(UCI_HANDLE oh, unsigned char *msg, unsigned int msg_len,
- unsigned char *output) {
+ unsigned char *output)
+{
- if (output == NULL) {
+ if (output == NULL)
return UCI_ERROR;
- }
+
return cryptocore_md_get_hash(oh, msg, msg_len, output);
}
-int uci_mac_init(UCI_HANDLE oh, unsigned char *key, unsigned int key_len) {
+int uci_mac_init(UCI_HANDLE oh, unsigned char *key, unsigned int key_len)
+{
int ret = 0;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
+
if (pctx->alg == ID_UCI_XCBCMAC) {
ret = xcbc_init((aes_xcbc_state *)(pctx->imp), key, key_len);
- if (ret != 1) {
+
+ if (ret != 1)
return UCI_ERROR;
- } else {
+
+ else
return UCI_SUCCESS;
- }
}
+
return cryptocore_mac_init(oh, key, key_len);
}
-int uci_mac_update(UCI_HANDLE oh, unsigned char *msg, unsigned int msg_len) {
+int uci_mac_update(UCI_HANDLE oh, unsigned char *msg, unsigned int msg_len)
+{
int ret = 0;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
+
if (pctx->alg == ID_UCI_XCBCMAC) {
- ret = xcbc_process((aes_xcbc_state*)(pctx->imp), msg, msg_len);
- if (ret != 1) {
+ ret = xcbc_process((aes_xcbc_state *)(pctx->imp), msg, msg_len);
+
+ if (ret != 1)
return UCI_ERROR;
- } else {
+
+ else
return UCI_SUCCESS;
- }
}
return cryptocore_mac_update(oh, msg, msg_len);
}
int uci_mac_final(UCI_HANDLE oh, unsigned char *output,
- unsigned int *output_len) {
+ unsigned int *output_len)
+{
int ret = 0;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
+
if (pctx->alg == ID_UCI_XCBCMAC) {
- ret = xcbc_done((aes_xcbc_state*)(pctx->imp), output, output_len);
- if (ret != 1) {
+ ret = xcbc_done((aes_xcbc_state *)(pctx->imp), output, output_len);
+
+ if (ret != 1)
return UCI_ERROR;
- } else {
+
+ else
return UCI_SUCCESS;
- }
}
return cryptocore_mac_final(oh, output, output_len);
}
int uci_mac_get_mac(UCI_HANDLE oh, unsigned char *key, unsigned int key_len,
- unsigned char *msg, unsigned int msg_len, unsigned char *output,
- unsigned int *output_len) {
+ unsigned char *msg, unsigned int msg_len, unsigned char *output,
+ unsigned int *output_len)
+{
//int ret = 0;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
+
if (pctx->alg == ID_UCI_XCBCMAC) {
- if (xcbc_init((aes_xcbc_state *)(pctx->imp), key, key_len) != 1) {
+ if (xcbc_init((aes_xcbc_state *)(pctx->imp), key, key_len) != 1)
return UCI_ERROR;
- }
- if (xcbc_process((aes_xcbc_state*)(pctx->imp), msg, msg_len) != 1) {
+ if (xcbc_process((aes_xcbc_state *)(pctx->imp), msg, msg_len) != 1)
return UCI_ERROR;
- }
- if (xcbc_done((aes_xcbc_state*)(pctx->imp), output, output_len) != 1) {
+ if (xcbc_done((aes_xcbc_state *)(pctx->imp), output, output_len) != 1)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
+
return cryptocore_mac_getmac(oh, key, key_len, msg, msg_len, output,
- output_len);
+ output_len);
}
int uci_se_init(UCI_HANDLE oh, unsigned int mode, unsigned padding,
- unsigned char *key, unsigned int key_len, unsigned char *iv) {
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ unsigned char *key, unsigned int key_len, unsigned char *iv)
+{
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
+
unsigned conf = SDRM_LOW_HALF(pctx->config);
- if (conf == UCI_SW_CRYPTOCORE) {
+
+ if (conf == UCI_SW_CRYPTOCORE)
return cryptocore_se_init(oh, mode, padding, key, key_len, iv);
- }
- if (conf == UCI_HW) {
+
+ if (conf == UCI_HW)
return hwcrypto_se_init(oh, mode, padding, key, key_len, iv);
- }
+
return UCI_ERROR;
}
int uci_se_process(UCI_HANDLE oh, unsigned char *input, unsigned int input_len,
- unsigned char *output, unsigned int *output_len) {
+ unsigned char *output, unsigned int *output_len)
+{
+
+ uci_context_s *pctx = (uci_context_s *)oh;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
+
unsigned conf = SDRM_LOW_HALF(pctx->config);
- if (input != NULL && output == NULL) {
+
+ if (input != NULL && output == NULL)
return UCI_ERROR;
- }
- if (conf == UCI_SW_CRYPTOCORE) {
+
+ if (conf == UCI_SW_CRYPTOCORE)
return cryptocore_se_process(oh, input, input_len, output, output_len);
- }
- if (conf == UCI_HW) {
+
+ if (conf == UCI_HW)
return hwcrypto_se_process(oh, input, input_len, output, output_len);
- }
+
return UCI_ERROR;
}
int uci_se_final(UCI_HANDLE oh, unsigned char *input, unsigned int input_len,
- unsigned char *output, unsigned int *output_len) {
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ unsigned char *output, unsigned int *output_len)
+{
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
+
unsigned conf = SDRM_LOW_HALF(pctx->config);
+
if (input != NULL && output == NULL) {
TZ_ERROR("UCI_ERROR error line = %d,%s\n", __LINE__, __func__);
return UCI_ERROR;
}
- if (conf == UCI_SW_CRYPTOCORE) {
+
+ if (conf == UCI_SW_CRYPTOCORE)
return cryptocore_se_final(oh, input, input_len, output, output_len);
- }
- if (conf == UCI_HW) {
+
+ if (conf == UCI_HW)
return hwcrypto_se_final(oh, input, input_len, output, output_len);
- }
+
TZ_ERROR("UCI_ERROR error line = %d,%s\n", __LINE__, __func__);
return UCI_ERROR;
}
int uci_se_encrypt_oneblock(UCI_HANDLE oh, unsigned char *cipher_text,
- unsigned char *plain_text, unsigned char *user_key) {
- if (cipher_text == NULL || plain_text == NULL || user_key == NULL) {
+ unsigned char *plain_text, unsigned char *user_key)
+{
+ if (cipher_text == NULL || plain_text == NULL || user_key == NULL)
return UCI_ERROR;
- }
+
return cryptocore_se_encrypt_oneblock(oh, cipher_text, plain_text, user_key);
}
int uci_se_decrypt_oneblock(UCI_HANDLE oh, unsigned char *plain_text,
- unsigned char *cipher_text, unsigned char *user_key) {
- if (cipher_text == NULL || plain_text == NULL || user_key == NULL) {
+ unsigned char *cipher_text, unsigned char *user_key)
+{
+ if (cipher_text == NULL || plain_text == NULL || user_key == NULL)
return UCI_ERROR;
- }
+
return cryptocore_se_decrypt_oneblock(oh, plain_text, cipher_text, user_key);
}
int uci_wbse_init(UCI_HANDLE oh, int flag, unsigned char *key,
- char *table_filepath, void *pencoder1, void *pencoder2) {
+ char *table_filepath, void *pencoder1, void *pencoder2)
+{
return UCI_ERROR;
}
int uci_wbse_final(UCI_HANDLE oh, unsigned char *input, unsigned int input_len,
- unsigned char *output, unsigned int output_len) {
+ unsigned char *output, unsigned int output_len)
+{
return UCI_ERROR;
}
-int uci_ae_gen_param(UCI_HANDLE oh, uci_param_s *param, unsigned int size) {
+int uci_ae_gen_param(UCI_HANDLE oh, uci_param_s *param, unsigned int size)
+{
return cryptocore_ae_gen_param(oh, param, size);
}
int uci_ae_gen_keypair(UCI_HANDLE oh, uci_key_s *keymaterial,
- uci_param_s *param) {
- if (keymaterial == NULL) {
+ uci_param_s *param)
+{
+ if (keymaterial == NULL)
return UCI_ERROR;
- }
+
return cryptocore_ae_gen_keypair(oh, keymaterial, param);
}
int uci_ae_set_keypair(UCI_HANDLE oh, uci_key_s *keymaterial,
- uci_param_s *param) {
- if (keymaterial == NULL) {
+ uci_param_s *param)
+{
+ if (keymaterial == NULL)
return UCI_ERROR;
- }
+
return cryptocore_ae_set_keypair(oh, keymaterial, param);
}
int uci_ae_encrypt(UCI_HANDLE oh, unsigned char *input, unsigned int input_len,
- unsigned char *output, unsigned int *output_len) {
+ unsigned char *output, unsigned int *output_len)
+{
return cryptocore_ae_encrypt(oh, input, input_len, output, output_len);
}
int uci_ae_decrypt(UCI_HANDLE oh, unsigned char *input, unsigned int input_len,
- unsigned char *output, unsigned int *output_len) {
+ unsigned char *output, unsigned int *output_len)
+{
return cryptocore_ae_decrypt(oh, input, input_len, output, output_len);
}
int uci_ae_decryptbycrt(UCI_HANDLE oh, unsigned char *input,
- unsigned int input_len, unsigned char *output, unsigned int *output_len) {
+ unsigned int input_len, unsigned char *output, unsigned int *output_len)
+{
return cryptocore_ae_decryptbycrt(oh, input, input_len, output, output_len);
}
int uci_wbae_encrypt(UCI_HANDLE oh, unsigned char *input,
- unsigned int input_len, unsigned char *output, unsigned int *output_len) {
+ unsigned int input_len, unsigned char *output, unsigned int *output_len)
+{
return UCI_ERROR;
}
int uci_wbae_decrypt(UCI_HANDLE oh, unsigned char *input,
- unsigned int input_len, unsigned char *output, unsigned int *output_len) {
+ unsigned int input_len, unsigned char *output, unsigned int *output_len)
+{
return UCI_ERROR;
}
int uci_ds_sign(UCI_HANDLE oh, unsigned char *hash, unsigned int hash_len,
- unsigned char *signature, unsigned int *sign_len) {
+ unsigned char *signature, unsigned int *sign_len)
+{
return cryptocore_ds_sign(oh, hash, hash_len, signature, sign_len);
}
int uci_ds_verify(UCI_HANDLE oh, unsigned char *hash, unsigned int hash_len,
- unsigned char *signature, unsigned int sign_len, int *result) {
+ unsigned char *signature, unsigned int sign_len, int *result)
+{
return cryptocore_ds_verify(oh, hash, hash_len, signature, sign_len, result);
}
int uci_dh_gen_phasekey(UCI_HANDLE oh, unsigned char *pch_xk,
- unsigned char *pch_xv, uci_param_s *param) {
+ unsigned char *pch_xv, uci_param_s *param)
+{
return cryptocore_dh_gen_dh1stphasekey(oh, pch_xk, pch_xv, param);
}
int uci_dh_gen_authkey(UCI_HANDLE oh, unsigned char *pch_xk,
- unsigned char *pch_xv, unsigned char *pch_kauth) {
+ unsigned char *pch_xv, unsigned char *pch_kauth)
+{
return cryptocore_dh_gen_dhkey(oh, pch_xk, pch_xv, pch_kauth);
}
-int uci_prng_seed(UCI_HANDLE oh, unsigned char * seed) {
+int uci_prng_seed(UCI_HANDLE oh, unsigned char *seed)
+{
return cryptocore_prng_seed(oh, seed);
}
-int uci_prng_get(UCI_HANDLE oh, unsigned int bit_len, unsigned char *data) {
+int uci_prng_get(UCI_HANDLE oh, unsigned int bit_len, unsigned char *data)
+{
return cryptocore_prng_get(oh, bit_len, data);
}
int uci_authcrypt_init(UCI_HANDLE oh, unsigned int mode, unsigned char *nonce,
- unsigned int nonce_len, unsigned int tag_len, unsigned int aad_len,
- unsigned int payload_len, unsigned char *key, unsigned int key_len) {
+ unsigned int nonce_len, unsigned int tag_len, unsigned int aad_len,
+ unsigned int payload_len, unsigned char *key, unsigned int key_len)
+{
#if 0
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
gcm_context *gctx = NULL;
aes_ccm_context *cctx = NULL;
int ret;
- if(pctx->alg == ID_UCI_AE_GCM)
- {
+
+ if (pctx->alg == ID_UCI_AE_GCM) {
gctx = (gcm_context *)pctx->imp;
- if(gcm_init(gctx, key, key_len) != 0)
- {
+
+ if (gcm_init(gctx, key, key_len) != 0)
return UCI_ERROR;
- }
- if(gcm_starts(gctx, mode, nonce, nonce_len) != 0)
- {
+
+ if (gcm_starts(gctx, mode, nonce, nonce_len) != 0)
return UCI_ERROR;
- }
+
pctx->flag = tag_len;
- }
- else if(pctx->alg == ID_UCI_AE_CCM)
- {
+ } else if (pctx->alg == ID_UCI_AE_CCM) {
cctx = (aes_ccm_context *)pctx->imp;
- ret = aes_ccm_init(cctx, mode, key, key_len, nonce, nonce_len, tag_len, aad_len, payload_len);
- if(ret != 0)
- {
+ ret = aes_ccm_init(cctx, mode, key, key_len, nonce, nonce_len, tag_len, aad_len,
+ payload_len);
+
+ if (ret != 0) {
printf("aes_ccm_init error. ret = %d\n ", ret);
return UCI_ERROR;
}
- }
- else
- {
+ } else {
printf("alg type erro \n");
return UCI_ERROR;
@@ -420,272 +469,271 @@ int uci_authcrypt_init(UCI_HANDLE oh, unsigned int mode, unsigned char *nonce,
return UCI_ERROR;
}
int uci_authcrypt_update_aad(UCI_HANDLE oh, unsigned char *aad,
- unsigned int aad_len) {
+ unsigned int aad_len)
+{
#if 0
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
gcm_context *gctx;
aes_ccm_context *cctx;
- if(pctx->alg == ID_UCI_AE_GCM)
- {
+
+ if (pctx->alg == ID_UCI_AE_GCM) {
gctx = (gcm_context *)pctx->imp;
- if(gcm_update_add(gctx, aad, aad_len) != 0)
- {
+
+ if (gcm_update_add(gctx, aad, aad_len) != 0)
return UCI_ERROR;
- }
- }
- else if(pctx->alg == ID_UCI_AE_CCM)
- {
+ } else if (pctx->alg == ID_UCI_AE_CCM) {
cctx = (aes_ccm_context *)pctx->imp;
- if(aes_ccm_update_aad(cctx,aad,aad_len) != 0)
- {
+
+ if (aes_ccm_update_aad(cctx, aad, aad_len) != 0)
return UCI_ERROR;
- }
- }
- else
- {
+ } else
return UCI_ERROR;
- }
return UCI_SUCCESS;
#endif
return UCI_ERROR;
}
int uci_authcrypt_update(UCI_HANDLE oh, unsigned char *src,
- unsigned int src_len, unsigned char *dest, unsigned int *dest_len) {
+ unsigned int src_len, unsigned char *dest, unsigned int *dest_len)
+{
#if 0
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
gcm_context *gctx;
aes_ccm_context *cctx;
- if(pctx->alg == ID_UCI_AE_GCM)
- {
+ if (pctx->alg == ID_UCI_AE_GCM) {
gctx = (gcm_context *)pctx->imp;
- if(gcm_update(gctx,src_len, src, dest) != 0)
- {
+
+ if (gcm_update(gctx, src_len, src, dest) != 0)
return UCI_ERROR;
- }
+
*dest_len = src_len;
- }
- else if(pctx->alg == ID_UCI_AE_CCM)
- {
- cctx = (aes_ccm_context*)pctx->imp;
- if(aes_ccm_process(cctx,src,src_len,dest,dest_len,NULL,NULL,0) != 0)
- {
+ } else if (pctx->alg == ID_UCI_AE_CCM) {
+ cctx = (aes_ccm_context *)pctx->imp;
+
+ if (aes_ccm_process(cctx, src, src_len, dest, dest_len, NULL, NULL, 0) != 0)
return UCI_ERROR;
- }
- }
- else
- {
+ } else
return UCI_ERROR;
- }
return UCI_SUCCESS;
#endif
return UCI_ERROR;
}
int uci_authcrypt_encryptfinal(UCI_HANDLE oh, unsigned char *src,
- unsigned int src_len, unsigned char *dest, unsigned int *dest_len,
- unsigned char *tag, unsigned int *tag_len) {
+ unsigned int src_len, unsigned char *dest, unsigned int *dest_len,
+ unsigned char *tag, unsigned int *tag_len)
+{
#if 0
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
gcm_context *gctx = NULL;
aes_ccm_context *cctx = NULL;
int ret;
- if(pctx->alg == ID_UCI_AE_GCM)
- {
+
+ if (pctx->alg == ID_UCI_AE_GCM) {
gctx = (gcm_context *)pctx->imp;
- if(gcm_update(gctx,src_len, src, dest) != 0)
- {
+
+ if (gcm_update(gctx, src_len, src, dest) != 0)
return UCI_ERROR;
- }
- if(dest_len != NULL)
- {
+
+ if (dest_len != NULL)
*dest_len = src_len;
- }
- if((ret = gcm_finish(gctx, tag, pctx->flag)) != 0)
- {
- printf("ERROR %d\n",ret);
+
+ if ((ret = gcm_finish(gctx, tag, pctx->flag)) != 0) {
+ printf("ERROR %d\n", ret);
return UCI_ERROR;
}
- if(tag_len != NULL)
- {
- *tag_len= pctx->flag;
- }
- }
- else if(pctx->alg == ID_UCI_AE_CCM)
- {
- cctx = (aes_ccm_context*)pctx->imp;
- if(aes_ccm_process(cctx,src,src_len,dest,dest_len,tag,tag_len,1) != 0)
- {
+
+ if (tag_len != NULL)
+ *tag_len = pctx->flag;
+ } else if (pctx->alg == ID_UCI_AE_CCM) {
+ cctx = (aes_ccm_context *)pctx->imp;
+
+ if (aes_ccm_process(cctx, src, src_len, dest, dest_len, tag, tag_len, 1) != 0)
return UCI_ERROR;
- }
- }
- else
- {
+ } else
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
#endif
return UCI_ERROR;
}
int uci_authcrypt_decryptfinal(UCI_HANDLE oh, unsigned char *src,
- unsigned int src_len, unsigned char *dest, unsigned int *dest_len,
- unsigned char *tag, unsigned int tag_len) {
+ unsigned int src_len, unsigned char *dest, unsigned int *dest_len,
+ unsigned char *tag, unsigned int tag_len)
+{
#if 0
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
gcm_context *gctx;
aes_ccm_context *cctx;
- unsigned char tmp[16] =
- { 0x0,};
+ unsigned char tmp[16] = {0x0,};
unsigned int len = 0;
len = tag_len;
- if(pctx->alg == ID_UCI_AE_GCM)
- {
+
+ if (pctx->alg == ID_UCI_AE_GCM) {
gctx = (gcm_context *)pctx->imp;
- if(gcm_update(gctx,src_len, src, dest) != 0)
- {
+
+ if (gcm_update(gctx, src_len, src, dest) != 0)
return UCI_ERROR;
- }
+
*dest_len = src_len;
- if(gcm_finish(gctx, tmp, pctx->flag) != 0)
- {
+
+ if (gcm_finish(gctx, tmp, pctx->flag) != 0) {
printf("gcm_finish error \n");
return UCI_ERROR;
}
- if(memcmp(tmp, tag, tag_len) != 0)
- {
- PrintBYTE("tmp",tmp,tag_len);
- PrintBYTE("tag",tag,tag_len);
+
+ if (memcmp(tmp, tag, tag_len) != 0) {
+ PrintBYTE("tmp", tmp, tag_len);
+ PrintBYTE("tag", tag, tag_len);
printf("tag not right \n");
return UCI_ERROR;
}
- }
- else if(pctx->alg == ID_UCI_AE_CCM)
- {
- cctx = (aes_ccm_context*)pctx->imp;
- if(aes_ccm_process(cctx,src,src_len,dest,dest_len,tag,&len,1) != 0)
- {
+ } else if (pctx->alg == ID_UCI_AE_CCM) {
+ cctx = (aes_ccm_context *)pctx->imp;
+
+ if (aes_ccm_process(cctx, src, src_len, dest, dest_len, tag, &len, 1) != 0)
return UCI_ERROR;
- }
- }
- else
- {
+ } else
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
#endif
return UCI_ERROR;
}
-int uci_dup_handle(UCI_HANDLE srcoh, UCI_HANDLE* destoh) {
+int uci_dup_handle(UCI_HANDLE srcoh, UCI_HANDLE *destoh)
+{
uci_context_s *srcctx = (uci_context_s *)srcoh;
uci_context_s *destctx = NULL;
- if (destoh == NULL) {
+ if (destoh == NULL)
return UCI_ERROR;
- }
- int ret = uci_context_alloc(srcctx->alg, (uci_engine_config_e)srcctx->config, destoh);
- if (ret != UCI_SUCCESS) {
+ int ret = uci_context_alloc(srcctx->alg, (uci_engine_config_e)srcctx->config,
+ destoh);
+
+ if (ret != UCI_SUCCESS)
return ret;
- }
destctx = (uci_context_s *)(*destoh);
- if (destctx == NULL) {
+
+ if (destctx == NULL)
return UCI_ERROR;
- }
switch (srcctx->alg) {
#if 0
- case ID_UCI_AE_GCM:
+
+ case ID_UCI_AE_GCM:
memcpy(destctx->imp, srcctx->imp, sizeof(gcm_context));
break;
- case ID_UCI_AE_CCM:
+
+ case ID_UCI_AE_CCM:
memcpy(destctx->imp, srcctx->imp, sizeof(aes_ccm_context));
break;
#endif
- case ID_UCI_X931:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_X931Context));
- break;
- case ID_UCI_MD5:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_MD5Context));
- break;
- case ID_UCI_SHA1:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA1Context));
- break;
- case ID_UCI_SHA224:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA224Context));
- break;
- case ID_UCI_SHA256:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA256Context));
- break;
+
+ case ID_UCI_X931:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_X931Context));
+ break;
+
+ case ID_UCI_MD5:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_MD5Context));
+ break;
+
+ case ID_UCI_SHA1:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA1Context));
+ break;
+
+ case ID_UCI_SHA224:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA224Context));
+ break;
+
+ case ID_UCI_SHA256:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA256Context));
+ break;
#ifndef _OP64_NOTSUPPORTED
- case ID_UCI_SHA384:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA384Context));
- break;
- case ID_UCI_SHA512:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA512Context));
- break;
+
+ case ID_UCI_SHA384:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA384Context));
+ break;
+
+ case ID_UCI_SHA512:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA512Context));
+ break;
#endif
- case ID_UCI_CMAC:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_CMACContext));
- break;
- case ID_UCI_XCBCMAC:
- memcpy(destctx->imp, srcctx->imp, sizeof(aes_xcbc_state));
- break;
- case ID_UCI_HMD5:
- case ID_UCI_HSHA1:
- case ID_UCI_HSHA256:
- case ID_UCI_HSHA224:
+
+ case ID_UCI_CMAC:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_CMACContext));
+ break;
+
+ case ID_UCI_XCBCMAC:
+ memcpy(destctx->imp, srcctx->imp, sizeof(aes_xcbc_state));
+ break;
+
+ case ID_UCI_HMD5:
+ case ID_UCI_HSHA1:
+ case ID_UCI_HSHA256:
+ case ID_UCI_HSHA224:
#ifndef _OP64_NOTSUPPORTED
- case ID_UCI_HSHA384:
- case ID_UCI_HSHA512:
+ case ID_UCI_HSHA384:
+ case ID_UCI_HSHA512:
#endif //_OP64_NOTSUPPORTED
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_HMACContext));
- break;
- case ID_UCI_AES128:
- case ID_UCI_AES192:
- case ID_UCI_AES256:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_AESContext));
- break;
- case ID_UCI_DES:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DESContext));
- break;
- case ID_UCI_TDES:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_TDESContext));
- break;
- case ID_UCI_RC4:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_RC4Context));
- break;
- case ID_UCI_SNOW2:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SNOW2Context));
- break;
- case ID_UCI_RSA512:
- case ID_UCI_RSA:
- case ID_UCI_RSA1024:
- case ID_UCI_RSA2048:
- case ID_UCI_RSA3072:
- case ID_UCI_RSA4096:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_RSAContext));
- break;
- case ID_UCI_DSA:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DSAContext));
- break;
- case ID_UCI_ECDSA:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_ECDSAContext));
- break;
- case ID_UCI_ECDH:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_ECDHContext));
- break;
- case ID_UCI_DH:
- memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DHContext));
- break;
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_HMACContext));
+ break;
+
+ case ID_UCI_AES128:
+ case ID_UCI_AES192:
+ case ID_UCI_AES256:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_AESContext));
+ break;
+
+ case ID_UCI_DES:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DESContext));
+ break;
+
+ case ID_UCI_TDES:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_TDESContext));
+ break;
+
+ case ID_UCI_RC4:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_RC4Context));
+ break;
+
+ case ID_UCI_SNOW2:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SNOW2Context));
+ break;
+
+ case ID_UCI_RSA512:
+ case ID_UCI_RSA:
+ case ID_UCI_RSA1024:
+ case ID_UCI_RSA2048:
+ case ID_UCI_RSA3072:
+ case ID_UCI_RSA4096:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_RSAContext));
+ break;
+
+ case ID_UCI_DSA:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DSAContext));
+ break;
+
+ case ID_UCI_ECDSA:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_ECDSAContext));
+ break;
+
+ case ID_UCI_ECDH:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_ECDHContext));
+ break;
+
+ case ID_UCI_DH:
+ memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DHContext));
+ break;
}
return UCI_SUCCESS;
diff --git a/ssflib/dep/uci/source/uci_cryptocore.c b/ssflib/dep/uci/source/uci_cryptocore.c
index 2824ee7..6a87eb1 100644
--- a/ssflib/dep/uci/source/uci_cryptocore.c
+++ b/ssflib/dep/uci/source/uci_cryptocore.c
@@ -29,285 +29,302 @@
#include "uci_aes_xcbc_mac.h"
#define g_bTAdbug 1
#define TZ_PRINT(fmt...) \
- do {if (g_bTAdbug) printf("[SSFLIB] ");printf(fmt);}while(0)
+ do {if (g_bTAdbug) printf("[SSFLIB] "); printf(fmt); } while (0)
#define TZ_ERROR(fmt...) \
- do {if (g_bTAdbug) printf("[SSFLIB] ");printf(fmt);}while(0)
+ do {if (g_bTAdbug) printf("[SSFLIB] "); printf(fmt); } while (0)
-int cryptocore_context_alloc(unsigned int algorithm, UCI_HANDLE* context) {
- uci_context_s* ctx;
+int cryptocore_context_alloc(unsigned int algorithm, UCI_HANDLE *context)
+{
+ uci_context_s *ctx;
CryptoCoreContainer *crt;
- if (context == NULL) {
+ if (context == NULL)
return UCI_ERROR;
- }
- ctx = (uci_context_s*)OsaMalloc(sizeof(uci_context_s));
- if (ctx == NULL) {
+ ctx = (uci_context_s *)OsaMalloc(sizeof(uci_context_s));
+
+ if (ctx == NULL)
return UCI_MEM_ALLOR_ERROR;
- }
crt = create_CryptoCoreContainer(algorithm);
+
if (crt == NULL) {
OsaFree(ctx);
return UCI_MEM_ALLOR_ERROR;
}
- ctx->imp = (void*)crt;
+
+ ctx->imp = (void *)crt;
ctx->config = UCI_SW;
ctx->alg = algorithm;
*context = ctx;
return UCI_SUCCESS;
}
-int cryptocore_context_free(UCI_HANDLE oh) {
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+int cryptocore_context_free(UCI_HANDLE oh)
+{
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- destroy_CryptoCoreContainer((CryptoCoreContainer*)pctx->imp);
+
+ destroy_CryptoCoreContainer((CryptoCoreContainer *)pctx->imp);
OsaFree(pctx);
pctx = NULL;
return UCI_SUCCESS;
}
-int cryptocore_md_init(UCI_HANDLE oh) {
+int cryptocore_md_init(UCI_HANDLE oh)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
ret = ((CryptoCoreContainer *)pctx->imp)->MD_init(
- (CryptoCoreContainer*)(pctx->imp));
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)(pctx->imp));
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_md_update(UCI_HANDLE oh, unsigned char *msg,
- unsigned int msg_len) {
+ unsigned int msg_len)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
ret = ((CryptoCoreContainer *)pctx->imp)->MD_update(
- ((CryptoCoreContainer*)pctx->imp), msg, msg_len);
- if (ret != CRYPTO_SUCCESS) {
+ ((CryptoCoreContainer *)pctx->imp), msg, msg_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
-int cryptocore_md_final(UCI_HANDLE oh, unsigned char *output) {
+int cryptocore_md_final(UCI_HANDLE oh, unsigned char *output)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
ret = ((CryptoCoreContainer *)pctx->imp)->MD_final(
- (CryptoCoreContainer*)pctx->imp, output);
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)pctx->imp, output);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_md_get_hash(UCI_HANDLE oh, unsigned char *msg,
- unsigned int msg_len, unsigned char * output) {
+ unsigned int msg_len, unsigned char *output)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
ret = ((CryptoCoreContainer *)pctx->imp)->MD_getHASH(
- (CryptoCoreContainer*)pctx->imp, msg, msg_len, output);
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)pctx->imp, msg, msg_len, output);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
-int cryptocore_mac_init(UCI_HANDLE oh, unsigned char *key, unsigned int key_len) {
+int cryptocore_mac_init(UCI_HANDLE oh, unsigned char *key,
+ unsigned int key_len)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
+
ret = ((CryptoCoreContainer *)pctx->imp)->MAC_init(
- (CryptoCoreContainer*)(pctx->imp), key, key_len);
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)(pctx->imp), key, key_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_mac_update(UCI_HANDLE oh, unsigned char *msg,
- unsigned int msg_len) {
+ unsigned int msg_len)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
+
ret = ((CryptoCoreContainer *)pctx->imp)->MAC_update(
- (CryptoCoreContainer*)(pctx->imp), msg, msg_len);
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)(pctx->imp), msg, msg_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_mac_final(UCI_HANDLE oh, unsigned char *output,
- unsigned int *output_len) {
+ unsigned int *output_len)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
ret = ((CryptoCoreContainer *)pctx->imp)->MAC_final(
- (CryptoCoreContainer*)(pctx->imp), output, output_len);
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)(pctx->imp), output, output_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_mac_getmac(UCI_HANDLE oh, unsigned char *key,
- unsigned int key_len, unsigned char *msg, unsigned int msg_len,
- unsigned char *output, unsigned int *output_len) {
+ unsigned int key_len, unsigned char *msg, unsigned int msg_len,
+ unsigned char *output, unsigned int *output_len)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
ret = ((CryptoCoreContainer *)pctx->imp)->MAC_getMAC(
- (CryptoCoreContainer*)(pctx->imp), key, key_len, msg, msg_len, output,
- output_len);
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)(pctx->imp), key, key_len, msg, msg_len, output,
+ output_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_se_init(UCI_HANDLE oh, unsigned int mode, unsigned padding,
- unsigned char *key, unsigned int key_len, unsigned char *iv) {
+ unsigned char *key, unsigned int key_len, unsigned char *iv)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
+
//deal with CTS base CBC
if (mode == ID_UCI_ENC_CTS || mode == ID_UCI_DEC_CTS) {
pctx->flag = mode;
+
if (mode == ID_UCI_ENC_CTS) {
ret = ((CryptoCoreContainer *)pctx->imp)->SE_init(
- (CryptoCoreContainer*)(pctx->imp), ID_UCI_ENC_CBC,
- ID_UCI_ZERO_PADDING, key, key_len, iv);
+ (CryptoCoreContainer *)(pctx->imp), ID_UCI_ENC_CBC,
+ ID_UCI_ZERO_PADDING, key, key_len, iv);
} else {
ret = ((CryptoCoreContainer *)pctx->imp)->SE_init(
- (CryptoCoreContainer*)(pctx->imp), ID_UCI_DEC_CBC,
- ID_UCI_ZERO_PADDING, key, key_len, iv);
+ (CryptoCoreContainer *)(pctx->imp), ID_UCI_DEC_CBC,
+ ID_UCI_ZERO_PADDING, key, key_len, iv);
}
} else {
ret = ((CryptoCoreContainer *)pctx->imp)->SE_init(
- (CryptoCoreContainer*)(pctx->imp), mode, padding, key, key_len, iv);
+ (CryptoCoreContainer *)(pctx->imp), mode, padding, key, key_len, iv);
}
- if (ret == CRYPTO_INVALID_ARGUMENT) {
+
+ if (ret == CRYPTO_INVALID_ARGUMENT)
return UCI_INVALID_ARGUMENT;
- }
- if (ret != CRYPTO_SUCCESS) {
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
return UCI_SUCCESS;
}
int cryptocore_se_process(UCI_HANDLE oh, unsigned char *input,
- unsigned int input_len, unsigned char *output, unsigned int *output_len) {
+ unsigned int input_len, unsigned char *output, unsigned int *output_len)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
ret = ((CryptoCoreContainer *)pctx->imp)->SE_process(
- (CryptoCoreContainer*)(pctx->imp), input, input_len, output, output_len);
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)(pctx->imp), input, input_len, output, output_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_se_final(UCI_HANDLE oh, unsigned char *input,
- unsigned int input_len, unsigned char *output, unsigned int *output_len) {
+ unsigned int input_len, unsigned char *output, unsigned int *output_len)
+{
int ret;
unsigned int lastblocksize = 0;
unsigned char lastblock[SDRM_AES_BLOCK_SIZ];
unsigned char secondlastblock[SDRM_AES_BLOCK_SIZ];
unsigned char aIV[SDRM_AES_BLOCK_SIZ] = {0x0, };
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
unsigned char *psecondlastblk = NULL; //point to second last block
unsigned char *plastblk = NULL; //point to last block
CryptoCoreContainer *crt = NULL;
- if (pctx == NULL) {
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
- crt = (CryptoCoreContainer*)(pctx->imp);
+ crt = (CryptoCoreContainer *)(pctx->imp);
- if (input_len <= SDRM_AES_BLOCK_SIZ) {
+ if (input_len <= SDRM_AES_BLOCK_SIZ)
goto final;
- }
plastblk = input + SDRM_AES_BLOCK_SIZ;
psecondlastblk = input;
@@ -317,35 +334,38 @@ int cryptocore_se_final(UCI_HANDLE oh, unsigned char *input,
#if 0
lastblocksize = input_len % SDRM_AES_BLOCK_SIZ;
- if(lastblocksize == 0)
- {
+
+ if (lastblocksize == 0)
lastblocksize = 16;
- }
- ret = crt->SE_process(crt, psecondlastblk, SDRM_AES_BLOCK_SIZ, lastblock, output_len);
- if(ret!=CRYPTO_SUCCESS)
- {
+ ret = crt->SE_process(crt, psecondlastblk, SDRM_AES_BLOCK_SIZ, lastblock,
+ output_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
#endif
- if (input_len % SDRM_AES_BLOCK_SIZ == 0) {
+
+ if (input_len % SDRM_AES_BLOCK_SIZ == 0)
crt->ctx->aesctx->padding = ID_NO_PADDING;
- }
+
ret = crt->SE_final(crt, input, input_len, output, output_len);
- if (ret != CRYPTO_SUCCESS) {
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
//swap last block
memcpy(lastblock, output + *output_len - 2 * SDRM_AES_BLOCK_SIZ,
- SDRM_AES_BLOCK_SIZ);
+ SDRM_AES_BLOCK_SIZ);
memcpy(output + *output_len - 2 * SDRM_AES_BLOCK_SIZ,
- output + *output_len - SDRM_AES_BLOCK_SIZ,
- SDRM_AES_BLOCK_SIZ);
+ output + *output_len - SDRM_AES_BLOCK_SIZ,
+ SDRM_AES_BLOCK_SIZ);
memcpy(output + *output_len - SDRM_AES_BLOCK_SIZ, lastblock,
- SDRM_AES_BLOCK_SIZ);
+ SDRM_AES_BLOCK_SIZ);
return UCI_SUCCESS;
}
+
//cts decrypt
if (pctx->flag == ID_UCI_DEC_CTS) {
lastblocksize = input_len % SDRM_AES_BLOCK_SIZ;
@@ -360,7 +380,8 @@ int cryptocore_se_final(UCI_HANDLE oh, unsigned char *input,
crt->ctx->aesctx->BlockLen = 0;
ret = crt->SE_process(crt, psecondlastblk,
- SDRM_AES_BLOCK_SIZ, secondlastblock, output_len);
+ SDRM_AES_BLOCK_SIZ, secondlastblock, output_len);
+
if (ret != CRYPTO_SUCCESS) {
TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret);
return UCI_ERROR;
@@ -369,610 +390,659 @@ int cryptocore_se_final(UCI_HANDLE oh, unsigned char *input,
// 2. Cn = Cn || Tail (Dn, B-M). Pad the ciphertext to the nearest multiple of the block size using the last B-M bits of block cipher decryption of the second-to-last ciphertext block.
memcpy(lastblock, plastblk, lastblocksize);
memcpy(lastblock + lastblocksize, secondlastblock + lastblocksize,
- SDRM_AES_BLOCK_SIZ - lastblocksize);
+ SDRM_AES_BLOCK_SIZ - lastblocksize);
memcpy(crt->ctx->aesctx->IV, aIV, SDRM_AES_BLOCK_SIZ);
// 3. Swap the last two ciphertext blocks.
// 4. Decrypt the (modified) ciphertext using the standard CBC mode up to the last block.
ret = crt->SE_process(crt, lastblock,
- SDRM_AES_BLOCK_SIZ, output, output_len);
+ SDRM_AES_BLOCK_SIZ, output, output_len);
+
if (ret != CRYPTO_SUCCESS) {
TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret);
return UCI_ERROR;
}
+
ret = crt->SE_process(crt, psecondlastblk,
- SDRM_AES_BLOCK_SIZ, lastblock, output_len);
+ SDRM_AES_BLOCK_SIZ, lastblock, output_len);
+
if (ret != CRYPTO_SUCCESS) {
TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret);
return UCI_ERROR;
}
+
memcpy(output + SDRM_AES_BLOCK_SIZ, lastblock, lastblocksize);
*output_len = input_len;
return UCI_SUCCESS;
}
+
//swap last two block and decrypto
if (input_len == 2 * SDRM_AES_BLOCK_SIZ) {
ret = crt->SE_process(crt, input + SDRM_AES_BLOCK_SIZ,
- SDRM_AES_BLOCK_SIZ, output, output_len);
+ SDRM_AES_BLOCK_SIZ, output, output_len);
+
if (ret != CRYPTO_SUCCESS) {
TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret);
return UCI_ERROR;
}
+
crt->ctx->aesctx->padding = ID_NO_PADDING;
ret = crt->SE_final(crt, input,
- SDRM_AES_BLOCK_SIZ, output + SDRM_AES_BLOCK_SIZ, output_len);
+ SDRM_AES_BLOCK_SIZ, output + SDRM_AES_BLOCK_SIZ, output_len);
+
if (ret != CRYPTO_SUCCESS) {
TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret);
return UCI_ERROR;
}
+
*output_len = 2 * SDRM_AES_BLOCK_SIZ;
return UCI_SUCCESS;
}
}
-// deal with other mode except cts
- final: ret = crt->SE_final(crt, input, input_len, output, output_len);
- if (ret != CRYPTO_SUCCESS) {
- TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret);
+
+ // deal with other mode except cts
+final:
+ ret = crt->SE_final(crt, input, input_len, output, output_len);
+
+ if (ret != CRYPTO_SUCCESS) {
+ TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret);
return UCI_ERROR;
}
+
return UCI_SUCCESS;
}
int cryptocore_se_encrypt_oneblock(UCI_HANDLE oh, unsigned char *cipher_text,
- unsigned char * plain_text, unsigned char *user_key) {
+ unsigned char *plain_text, unsigned char *user_key)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
ret = ((CryptoCoreContainer *)pctx->imp)->SE_EncryptOneBlock(cipher_text,
- plain_text, user_key);
- if (ret != CRYPTO_SUCCESS) {
+ plain_text, user_key);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_se_decrypt_oneblock(UCI_HANDLE oh, unsigned char *plain_text,
- unsigned char *cipher_text, unsigned char *user_key) {
+ unsigned char *cipher_text, unsigned char *user_key)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
ret = ((CryptoCoreContainer *)pctx->imp)->SE_DecryptOneBlock(cipher_text,
- plain_text, user_key);
- if (ret != CRYPTO_SUCCESS) {
+ plain_text, user_key);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_ae_gen_param(UCI_HANDLE oh, uci_param_s *param,
- unsigned int size) {
+ unsigned int size)
+{
int ret;
unsigned int alg;
//uci_param_imp_u *uciparm = NULL;
CryptoCoreContainer *crt = NULL;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (param == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (param == NULL)
return UCI_INVALID_ARGUMENT;
- }
alg = pctx->alg;
switch (alg) {
- case ID_UCI_DH:
- crt = create_CryptoCoreContainer(ID_DH);
- if (crt == NULL) {
- return UCI_ERROR;
- }
- if (crt->DH_GenerateParam(crt, param->uparam.udhp.prime, size,
- param->uparam.udhp.generator) != CRYPTO_SUCCESS) {
- destroy_CryptoCoreContainer(crt);
- return UCI_ERROR;
- }
- param->uparam.udhp.len = size;
+ case ID_UCI_DH:
+ crt = create_CryptoCoreContainer(ID_DH);
+
+ if (crt == NULL)
+ return UCI_ERROR;
+
+ if (crt->DH_GenerateParam(crt, param->uparam.udhp.prime, size,
+ param->uparam.udhp.generator) != CRYPTO_SUCCESS) {
destroy_CryptoCoreContainer(crt);
- break;
- case ID_UCI_DSA:
- crt = create_CryptoCoreContainer(ID_DSA);
- if (crt == NULL) {
- return UCI_ERROR;
- }
+ return UCI_ERROR;
+ }
- ret = crt->DSA_genParam(crt, size, param->uparam.udp.dsa_p_data,
- &(param->uparam.udp.dsa_p_len), param->uparam.udp.dsa_q_data,
- &(param->uparam.udp.dsa_q_len), param->uparam.udp.dsa_g_data,
- &(param->uparam.udp.dsa_g_len));
+ param->uparam.udhp.len = size;
+ destroy_CryptoCoreContainer(crt);
+ break;
- if (ret != UCI_SUCCESS) {
- destroy_CryptoCoreContainer(crt);
- return UCI_ERROR;
- }
+ case ID_UCI_DSA:
+ crt = create_CryptoCoreContainer(ID_DSA);
+
+ if (crt == NULL)
+ return UCI_ERROR;
+
+ ret = crt->DSA_genParam(crt, size, param->uparam.udp.dsa_p_data,
+ &(param->uparam.udp.dsa_p_len), param->uparam.udp.dsa_q_data,
+ &(param->uparam.udp.dsa_q_len), param->uparam.udp.dsa_g_data,
+ &(param->uparam.udp.dsa_g_len));
+
+ if (ret != UCI_SUCCESS) {
destroy_CryptoCoreContainer(crt);
- break;
- default:
- return UCI_INVALID_HANDLE;
+ return UCI_ERROR;
+ }
+
+ destroy_CryptoCoreContainer(crt);
+ break;
+
+ default:
+ return UCI_INVALID_HANDLE;
}
+
return UCI_SUCCESS;
}
int cryptocore_ae_gen_keypair(UCI_HANDLE oh, uci_key_s *keymaterial,
- uci_param_s *param) {
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_param_s *param)
+{
+ uci_context_s *pctx = (uci_context_s *)oh;
unsigned int alg;
unsigned int pad;
uci_key_s *ucikey = keymaterial;
int ret = UCI_ERROR;
uci_param_imp_u *uciparm = NULL;
- if (param == NULL) {
+
+ if (param == NULL)
return UCI_INVALID_ARGUMENT;
- }
+
uciparm = ¶m->uparam;
- if (pctx->config != UCI_SW_CRYPTOCORE) {
+ if (pctx->config != UCI_SW_CRYPTOCORE)
return UCI_INVALID_HANDLE;
- }
+
alg = pctx->alg;
+
switch (alg) {
- case ID_UCI_RSA512:
- case ID_UCI_RSA:
- case ID_UCI_RSA1024:
- case ID_UCI_RSA2048:
- case ID_UCI_RSA3072:
- case ID_UCI_RSA4096:
- pad = SDRM_LOW_HALF(uciparm->urp.padding);
- if (pad != ID_UCI_RSAES_PKCS15 && pad != ID_UCI_RSAES_OAEP
- && pad != ID_UCI_NO_PADDING && pad != ID_UCI_RSASSA_PKCS15
- && pad != ID_UCI_RSASSA_PSS) {
- return UCI_INVALID_ARGUMENT;
- }
- if (uciparm->urp.flag == RSA_GENKEYWITHNON) {
- ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypair(
- (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
- ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.e,
- &(ucikey->imp.rkey.e_len), ucikey->imp.rkey.d,
- &(ucikey->imp.rkey.d_len));
- break;
- }
- if (uciparm->urp.flag == RSA_GENKEYWITHE) {
- ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypairWithE(
- (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
- uciparm->urp.e, uciparm->urp.e_len, ucikey->imp.rkey.n,
- &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.d,
- &(ucikey->imp.rkey.d_len));
- break;
- }
- if (uciparm->urp.flag == RSA_GENKEYWITHPQE) {
- ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeyDWithPQE(
- (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
- uciparm->urp.e, uciparm->urp.e_len, uciparm->urp.p,
- uciparm->urp.p_len, uciparm->urp.q, uciparm->urp.q_len,
- ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.d,
- &(ucikey->imp.rkey.d_len));
- break;
- }
- if (uciparm->urp.flag == RSA_KEYFORCRT) {
- ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypairForCRT(
- (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
- ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.e,
- &(ucikey->imp.rkey.e_len), ucikey->imp.rkey.d,
- &(ucikey->imp.rkey.d_len), uciparm->urp.p, &(uciparm->urp.p_len),
- uciparm->urp.q, &(uciparm->urp.q_len), uciparm->urp.dmodp1,
- &(uciparm->urp.dmodp1_len), uciparm->urp.dmodq1,
- &(uciparm->urp.dmodq1_len), uciparm->urp.iqp,
- &(uciparm->urp.iqp_len));
- }
+ case ID_UCI_RSA512:
+ case ID_UCI_RSA:
+ case ID_UCI_RSA1024:
+ case ID_UCI_RSA2048:
+ case ID_UCI_RSA3072:
+ case ID_UCI_RSA4096:
+ pad = SDRM_LOW_HALF(uciparm->urp.padding);
+
+ if (pad != ID_UCI_RSAES_PKCS15 && pad != ID_UCI_RSAES_OAEP
+ && pad != ID_UCI_NO_PADDING && pad != ID_UCI_RSASSA_PKCS15
+ && pad != ID_UCI_RSASSA_PSS)
+ return UCI_INVALID_ARGUMENT;
+
+ if (uciparm->urp.flag == RSA_GENKEYWITHNON) {
+ ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypair(
+ (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
+ ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.e,
+ &(ucikey->imp.rkey.e_len), ucikey->imp.rkey.d,
+ &(ucikey->imp.rkey.d_len));
break;
- case ID_UCI_DSA:
- ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setParam(
- (CryptoCoreContainer *)pctx->imp, uciparm->udp.dsa_p_data,
- uciparm->udp.dsa_p_len, uciparm->udp.dsa_q_data,
- uciparm->udp.dsa_q_len, uciparm->udp.dsa_g_data,
- uciparm->udp.dsa_g_len);
- if (ret != CRYPTO_SUCCESS) {
- return UCI_ERROR;
- }
+ }
- ret = ((CryptoCoreContainer *)pctx->imp)->DSA_genKeypair(
- (CryptoCoreContainer *)pctx->imp, ucikey->imp.dkey.ydata,
- &(ucikey->imp.dkey.ydata_len), ucikey->imp.dkey.xdata,
- &(ucikey->imp.dkey.xdata_len));
+ if (uciparm->urp.flag == RSA_GENKEYWITHE) {
+ ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypairWithE(
+ (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
+ uciparm->urp.e, uciparm->urp.e_len, ucikey->imp.rkey.n,
+ &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.d,
+ &(ucikey->imp.rkey.d_len));
break;
- case ID_UCI_ECDSA:
- case ID_UCI_ECDH:
- //set curver parameter
- ret = ((CryptoCoreContainer *)pctx->imp)->EC_setCurve(
- (CryptoCoreContainer *)pctx->imp, uciparm->uep.dimension,
- uciparm->uep.ecc_p_data, uciparm->uep.ecc_p_len,
- uciparm->uep.ecc_a_data, uciparm->uep.ecc_a_len,
- uciparm->uep.ecc_b_data, uciparm->uep.ecc_b_len,
- uciparm->uep.ecc_g_x_data, uciparm->uep.ecc_g_x_len,
- uciparm->uep.ecc_g_y_data, uciparm->uep.ecc_g_y_len,
- uciparm->uep.ecc_r_data, uciparm->uep.ecc_r_len);
- if (ret != CRYPTO_SUCCESS) {
- break;
- }
+ }
- ret = ((CryptoCoreContainer *)pctx->imp)->EC_genKeypair(
- (CryptoCoreContainer *)pctx->imp, ucikey->imp.ekey.privatekey,
- &(ucikey->imp.ekey.privatekey_len), ucikey->imp.ekey.publickey_x,
- &(ucikey->imp.ekey.publickey_x_len), ucikey->imp.ekey.publickey_y,
- &(ucikey->imp.ekey.publickey_y_len));
+ if (uciparm->urp.flag == RSA_GENKEYWITHPQE) {
+ ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeyDWithPQE(
+ (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
+ uciparm->urp.e, uciparm->urp.e_len, uciparm->urp.p,
+ uciparm->urp.p_len, uciparm->urp.q, uciparm->urp.q_len,
+ ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.d,
+ &(ucikey->imp.rkey.d_len));
break;
- default:
+ }
+
+ if (uciparm->urp.flag == RSA_KEYFORCRT) {
+ ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypairForCRT(
+ (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
+ ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.e,
+ &(ucikey->imp.rkey.e_len), ucikey->imp.rkey.d,
+ &(ucikey->imp.rkey.d_len), uciparm->urp.p, &(uciparm->urp.p_len),
+ uciparm->urp.q, &(uciparm->urp.q_len), uciparm->urp.dmodp1,
+ &(uciparm->urp.dmodp1_len), uciparm->urp.dmodq1,
+ &(uciparm->urp.dmodq1_len), uciparm->urp.iqp,
+ &(uciparm->urp.iqp_len));
+ }
+
+ break;
+
+ case ID_UCI_DSA:
+ ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setParam(
+ (CryptoCoreContainer *)pctx->imp, uciparm->udp.dsa_p_data,
+ uciparm->udp.dsa_p_len, uciparm->udp.dsa_q_data,
+ uciparm->udp.dsa_q_len, uciparm->udp.dsa_g_data,
+ uciparm->udp.dsa_g_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
+
+ ret = ((CryptoCoreContainer *)pctx->imp)->DSA_genKeypair(
+ (CryptoCoreContainer *)pctx->imp, ucikey->imp.dkey.ydata,
+ &(ucikey->imp.dkey.ydata_len), ucikey->imp.dkey.xdata,
+ &(ucikey->imp.dkey.xdata_len));
+ break;
+
+ case ID_UCI_ECDSA:
+ case ID_UCI_ECDH:
+ //set curver parameter
+ ret = ((CryptoCoreContainer *)pctx->imp)->EC_setCurve(
+ (CryptoCoreContainer *)pctx->imp, uciparm->uep.dimension,
+ uciparm->uep.ecc_p_data, uciparm->uep.ecc_p_len,
+ uciparm->uep.ecc_a_data, uciparm->uep.ecc_a_len,
+ uciparm->uep.ecc_b_data, uciparm->uep.ecc_b_len,
+ uciparm->uep.ecc_g_x_data, uciparm->uep.ecc_g_x_len,
+ uciparm->uep.ecc_g_y_data, uciparm->uep.ecc_g_y_len,
+ uciparm->uep.ecc_r_data, uciparm->uep.ecc_r_len);
+
+ if (ret != CRYPTO_SUCCESS)
+ break;
+
+ ret = ((CryptoCoreContainer *)pctx->imp)->EC_genKeypair(
+ (CryptoCoreContainer *)pctx->imp, ucikey->imp.ekey.privatekey,
+ &(ucikey->imp.ekey.privatekey_len), ucikey->imp.ekey.publickey_x,
+ &(ucikey->imp.ekey.publickey_x_len), ucikey->imp.ekey.publickey_y,
+ &(ucikey->imp.ekey.publickey_y_len));
+ break;
+
+ default:
+ return UCI_ERROR;
}
- if (ret == CRYPTO_INVALID_ARGUMENT) {
+
+ if (ret == CRYPTO_INVALID_ARGUMENT)
return UCI_INVALID_ARGUMENT;
- }
- if (ret != CRYPTO_SUCCESS) {
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_ae_set_keypair(UCI_HANDLE oh, uci_key_s *keymaterial,
- uci_param_s *param) {
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_param_s *param)
+{
+ uci_context_s *pctx = (uci_context_s *)oh;
uci_key_s *ucikey = keymaterial;
uci_param_imp_u *uciparm = ¶m->uparam;
int ret;
unsigned int pad;
- if (pctx == NULL) {
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
+
int alg = pctx->alg;
- if (pctx->config != UCI_SW) {
+
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
switch (alg) {
- case ID_UCI_RSA512:
- case ID_UCI_RSA:
- case ID_UCI_RSA1024:
- case ID_UCI_RSA2048:
- case ID_UCI_RSA3072:
- case ID_UCI_RSA4096:
- pad = SDRM_LOW_HALF(uciparm->urp.padding);
-
- if (pad != ID_UCI_RSAES_PKCS15 && pad != ID_UCI_RSAES_OAEP
- && pad != ID_UCI_NO_PADDING && pad != ID_UCI_RSASSA_PKCS15
- && pad != ID_UCI_RSASSA_PSS) {
- return UCI_INVALID_ARGUMENT;
- }
+ case ID_UCI_RSA512:
+ case ID_UCI_RSA:
+ case ID_UCI_RSA1024:
+ case ID_UCI_RSA2048:
+ case ID_UCI_RSA3072:
+ case ID_UCI_RSA4096:
+ pad = SDRM_LOW_HALF(uciparm->urp.padding);
+
+ if (pad != ID_UCI_RSAES_PKCS15 && pad != ID_UCI_RSAES_OAEP
+ && pad != ID_UCI_NO_PADDING && pad != ID_UCI_RSASSA_PKCS15
+ && pad != ID_UCI_RSASSA_PSS)
+ return UCI_INVALID_ARGUMENT;
+
+ if (uciparm->urp.flag == RSA_KEYFORCRT) {
+ ret = ((CryptoCoreContainer *)pctx->imp)->RSA_setKeypairForCRT(
+ (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
+ ucikey->imp.rkey.n, ucikey->imp.rkey.n_len, ucikey->imp.rkey.e,
+ ucikey->imp.rkey.e_len, ucikey->imp.rkey.d, ucikey->imp.rkey.d_len,
+ uciparm->urp.p, uciparm->urp.p_len, uciparm->urp.q,
+ uciparm->urp.q_len, uciparm->urp.dmodp1, uciparm->urp.dmodp1_len,
+ uciparm->urp.dmodq1, uciparm->urp.dmodq1_len, uciparm->urp.iqp,
+ uciparm->urp.iqp_len);
+ } else {
+ ret = ((CryptoCoreContainer *)pctx->imp)->RSA_setKeypair(
+ (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
+ ucikey->imp.rkey.n, ucikey->imp.rkey.n_len, ucikey->imp.rkey.e,
+ ucikey->imp.rkey.e_len, ucikey->imp.rkey.d, ucikey->imp.rkey.d_len);
+ }
- if (uciparm->urp.flag == RSA_KEYFORCRT) {
- ret = ((CryptoCoreContainer *)pctx->imp)->RSA_setKeypairForCRT(
- (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
- ucikey->imp.rkey.n, ucikey->imp.rkey.n_len, ucikey->imp.rkey.e,
- ucikey->imp.rkey.e_len, ucikey->imp.rkey.d, ucikey->imp.rkey.d_len,
- uciparm->urp.p, uciparm->urp.p_len, uciparm->urp.q,
- uciparm->urp.q_len, uciparm->urp.dmodp1, uciparm->urp.dmodp1_len,
- uciparm->urp.dmodq1, uciparm->urp.dmodq1_len, uciparm->urp.iqp,
- uciparm->urp.iqp_len);
- } else {
- ret = ((CryptoCoreContainer *)pctx->imp)->RSA_setKeypair(
- (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding,
- ucikey->imp.rkey.n, ucikey->imp.rkey.n_len, ucikey->imp.rkey.e,
- ucikey->imp.rkey.e_len, ucikey->imp.rkey.d, ucikey->imp.rkey.d_len);
- }
- break;
- case ID_UCI_DSA:
- ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setParam(
- (CryptoCoreContainer *)pctx->imp, uciparm->udp.dsa_p_data,
- uciparm->udp.dsa_p_len, uciparm->udp.dsa_q_data,
- uciparm->udp.dsa_q_len, uciparm->udp.dsa_g_data,
- uciparm->udp.dsa_g_len);
- if (ret != CRYPTO_SUCCESS) {
- return UCI_ERROR;
- }
- ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setKeyPair(
- (CryptoCoreContainer *)pctx->imp, ucikey->imp.dkey.ydata,
- (ucikey->imp.dkey.ydata_len), ucikey->imp.dkey.xdata,
- (ucikey->imp.dkey.xdata_len));
- break;
- case ID_UCI_ECDSA:
- ret = ((CryptoCoreContainer *)pctx->imp)->EC_setCurve(
- (CryptoCoreContainer *)pctx->imp, uciparm->uep.dimension,
- uciparm->uep.ecc_p_data, uciparm->uep.ecc_p_len,
- uciparm->uep.ecc_a_data, uciparm->uep.ecc_a_len,
- uciparm->uep.ecc_b_data, uciparm->uep.ecc_b_len,
- uciparm->uep.ecc_g_x_data, uciparm->uep.ecc_g_x_len,
- uciparm->uep.ecc_g_y_data, uciparm->uep.ecc_g_y_len,
- uciparm->uep.ecc_r_data, uciparm->uep.ecc_r_len);
-
- ret = ((CryptoCoreContainer *)pctx->imp)->EC_setKeypair(
- (CryptoCoreContainer *)pctx->imp, ucikey->imp.ekey.privatekey,
- (ucikey->imp.ekey.privatekey_len), ucikey->imp.ekey.publickey_x,
- (ucikey->imp.ekey.publickey_x_len), ucikey->imp.ekey.publickey_y,
- (ucikey->imp.ekey.publickey_y_len));
- break;
- case ID_UCI_DH:
- ret = ((CryptoCoreContainer *)pctx->imp)->DH_SetParam(
- (CryptoCoreContainer *)pctx->imp, uciparm->udhp.prime,
- uciparm->udhp.len, uciparm->udhp.generator, uciparm->udhp.len);
- break;
- default:
+ break;
+
+ case ID_UCI_DSA:
+ ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setParam(
+ (CryptoCoreContainer *)pctx->imp, uciparm->udp.dsa_p_data,
+ uciparm->udp.dsa_p_len, uciparm->udp.dsa_q_data,
+ uciparm->udp.dsa_q_len, uciparm->udp.dsa_g_data,
+ uciparm->udp.dsa_g_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
- if (ret != CRYPTO_SUCCESS) {
+
+ ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setKeyPair(
+ (CryptoCoreContainer *)pctx->imp, ucikey->imp.dkey.ydata,
+ (ucikey->imp.dkey.ydata_len), ucikey->imp.dkey.xdata,
+ (ucikey->imp.dkey.xdata_len));
+ break;
+
+ case ID_UCI_ECDSA:
+ ret = ((CryptoCoreContainer *)pctx->imp)->EC_setCurve(
+ (CryptoCoreContainer *)pctx->imp, uciparm->uep.dimension,
+ uciparm->uep.ecc_p_data, uciparm->uep.ecc_p_len,
+ uciparm->uep.ecc_a_data, uciparm->uep.ecc_a_len,
+ uciparm->uep.ecc_b_data, uciparm->uep.ecc_b_len,
+ uciparm->uep.ecc_g_x_data, uciparm->uep.ecc_g_x_len,
+ uciparm->uep.ecc_g_y_data, uciparm->uep.ecc_g_y_len,
+ uciparm->uep.ecc_r_data, uciparm->uep.ecc_r_len);
+
+ ret = ((CryptoCoreContainer *)pctx->imp)->EC_setKeypair(
+ (CryptoCoreContainer *)pctx->imp, ucikey->imp.ekey.privatekey,
+ (ucikey->imp.ekey.privatekey_len), ucikey->imp.ekey.publickey_x,
+ (ucikey->imp.ekey.publickey_x_len), ucikey->imp.ekey.publickey_y,
+ (ucikey->imp.ekey.publickey_y_len));
+ break;
+
+ case ID_UCI_DH:
+ ret = ((CryptoCoreContainer *)pctx->imp)->DH_SetParam(
+ (CryptoCoreContainer *)pctx->imp, uciparm->udhp.prime,
+ uciparm->udhp.len, uciparm->udhp.generator, uciparm->udhp.len);
+ break;
+
+ default:
return UCI_ERROR;
}
+
+ if (ret != CRYPTO_SUCCESS)
+ return UCI_ERROR;
+
return UCI_SUCCESS;
}
int cryptocore_ae_encrypt(UCI_HANDLE oh, unsigned char *input,
- unsigned int input_len, unsigned char *output, unsigned int *output_len) {
+ unsigned int input_len, unsigned char *output, unsigned int *output_len)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
- if (pctx == NULL) {
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
- if (pctx->alg < ID_UCI_RSA || pctx->alg > ID_UCI_RSA512) {
+ if (pctx->alg < ID_UCI_RSA || pctx->alg > ID_UCI_RSA512)
return UCI_INVALID_HANDLE;
- }
+
ret = ((CryptoCoreContainer *)pctx->imp)->AE_encrypt(
- ((CryptoCoreContainer*)pctx->imp), input, input_len, output, output_len);
- if (ret == CRYPTO_MSG_TOO_LONG) {
+ ((CryptoCoreContainer *)pctx->imp), input, input_len, output, output_len);
+
+ if (ret == CRYPTO_MSG_TOO_LONG)
return UCI_MSG_TOO_LONG;
- }
- if (ret != CRYPTO_SUCCESS) {
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_ae_decrypt(UCI_HANDLE oh, unsigned char *input,
- unsigned int input_len, unsigned char *output, unsigned int *output_len) {
+ unsigned int input_len, unsigned char *output, unsigned int *output_len)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
- if (pctx == NULL) {
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
- if (pctx->alg < ID_UCI_RSA || pctx->alg > ID_UCI_RSA512) {
+ if (pctx->alg < ID_UCI_RSA || pctx->alg > ID_UCI_RSA512)
return UCI_INVALID_HANDLE;
- }
+
ret = ((CryptoCoreContainer *)pctx->imp)->AE_decrypt(
- ((CryptoCoreContainer*)pctx->imp), input, input_len, output, output_len);
- if (ret == CRYPTO_MSG_TOO_LONG) {
+ ((CryptoCoreContainer *)pctx->imp), input, input_len, output, output_len);
+
+ if (ret == CRYPTO_MSG_TOO_LONG)
return UCI_MSG_TOO_LONG;
- }
- if (ret != CRYPTO_SUCCESS) {
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_ae_decryptbycrt(UCI_HANDLE oh, unsigned char *input,
- unsigned int input_len, unsigned char *output, unsigned int *output_len) {
+ unsigned int input_len, unsigned char *output, unsigned int *output_len)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
- if (pctx == NULL) {
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
// ctr=(CryptoCoreContainer *)(pctx->imp);
// ctr->MD_update(ctr,msg,msg_len);
ret = ((CryptoCoreContainer *)pctx->imp)->AE_decryptByCRT(
- ((CryptoCoreContainer*)pctx->imp), input, input_len, output, output_len);
- if (ret == CRYPTO_MSG_TOO_LONG) {
+ ((CryptoCoreContainer *)pctx->imp), input, input_len, output, output_len);
+
+ if (ret == CRYPTO_MSG_TOO_LONG)
return UCI_MSG_TOO_LONG;
- }
- if (ret != CRYPTO_SUCCESS) {
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_ds_sign(UCI_HANDLE oh, unsigned char *hash,
- unsigned int hash_len, unsigned char *signature, unsigned int *sign_len) {
+ unsigned int hash_len, unsigned char *signature, unsigned int *sign_len)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
- if (pctx == NULL) {
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
ret = ((CryptoCoreContainer *)pctx->imp)->DS_sign(
- ((CryptoCoreContainer*)pctx->imp), hash, hash_len, signature, sign_len);
- if (ret == CRYPTO_MSG_TOO_LONG) {
+ ((CryptoCoreContainer *)pctx->imp), hash, hash_len, signature, sign_len);
+
+ if (ret == CRYPTO_MSG_TOO_LONG)
return UCI_MSG_TOO_LONG;
- }
- if (ret != CRYPTO_SUCCESS) {
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_ds_verify(UCI_HANDLE oh, unsigned char *hash,
- unsigned int hash_len, unsigned char *signature, unsigned int sign_len,
- int *result) {
+ unsigned int hash_len, unsigned char *signature, unsigned int sign_len,
+ int *result)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
- if (pctx == NULL) {
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
ret = ((CryptoCoreContainer *)pctx->imp)->DS_verify(
- (CryptoCoreContainer*)pctx->imp, hash, hash_len, signature, sign_len,
- result);
- if (ret == CRYPTO_MSG_TOO_LONG) {
+ (CryptoCoreContainer *)pctx->imp, hash, hash_len, signature, sign_len,
+ result);
+
+ if (ret == CRYPTO_MSG_TOO_LONG)
return UCI_MSG_TOO_LONG;
- }
- if (ret != CRYPTO_SUCCESS) {
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_dh_gen_dh1stphasekey(UCI_HANDLE oh, unsigned char *pch_xk,
- unsigned char *pch_xv, uci_param_s *param) {
+ unsigned char *pch_xv, uci_param_s *param)
+{
int ret;
uci_param_imp_u *uciparam = ¶m->uparam;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
unsigned int alg;
- if (pctx == NULL) {
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
alg = pctx->alg;
+
if (alg == ID_UCI_ECDH) {
ret = ((CryptoCoreContainer *)pctx->imp)->EC_setCurve(
- (CryptoCoreContainer *)pctx->imp, uciparam->uep.dimension,
- uciparam->uep.ecc_p_data, uciparam->uep.ecc_p_len,
- uciparam->uep.ecc_a_data, uciparam->uep.ecc_a_len,
- uciparam->uep.ecc_b_data, uciparam->uep.ecc_b_len,
- uciparam->uep.ecc_g_x_data, uciparam->uep.ecc_g_x_len,
- uciparam->uep.ecc_g_y_data, uciparam->uep.ecc_g_y_len,
- uciparam->uep.ecc_r_data, uciparam->uep.ecc_r_len);
-
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)pctx->imp, uciparam->uep.dimension,
+ uciparam->uep.ecc_p_data, uciparam->uep.ecc_p_len,
+ uciparam->uep.ecc_a_data, uciparam->uep.ecc_a_len,
+ uciparam->uep.ecc_b_data, uciparam->uep.ecc_b_len,
+ uciparam->uep.ecc_g_x_data, uciparam->uep.ecc_g_x_len,
+ uciparam->uep.ecc_g_y_data, uciparam->uep.ecc_g_y_len,
+ uciparam->uep.ecc_r_data, uciparam->uep.ecc_r_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
ret = ((CryptoCoreContainer *)pctx->imp)->ECDH_Gen1stPhaseKey(
- (CryptoCoreContainer*)pctx->imp, pch_xk, pch_xv);
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)pctx->imp, pch_xk, pch_xv);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- } else {
+
+ else
return UCI_SUCCESS;
- }
}
+
if (alg == ID_UCI_DH) {
ret = ((CryptoCoreContainer *)pctx->imp)->DH_SetParam(
- (CryptoCoreContainer *)pctx->imp, uciparam->udhp.prime,
- uciparam->udhp.len, uciparam->udhp.generator, uciparam->udhp.len);
+ (CryptoCoreContainer *)pctx->imp, uciparam->udhp.prime,
+ uciparam->udhp.len, uciparam->udhp.generator, uciparam->udhp.len);
- if (ret != CRYPTO_SUCCESS) {
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
ret = ((CryptoCoreContainer *)pctx->imp)->DH_Gen1stPhaseKey(
- (CryptoCoreContainer*)pctx->imp, pch_xk, pch_xv);
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)pctx->imp, pch_xk, pch_xv);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- } else {
+
+ else
return UCI_SUCCESS;
- }
}
+
return UCI_ERROR;
}
int cryptocore_dh_gen_dhkey(UCI_HANDLE oh, unsigned char *pch_xk,
- unsigned char *pch_xv, unsigned char *pch_kauth) {
+ unsigned char *pch_xv, unsigned char *pch_kauth)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
unsigned int alg;
- if (pctx->config != UCI_SW_CRYPTOCORE) {
+ if (pctx->config != UCI_SW_CRYPTOCORE)
return UCI_INVALID_HANDLE;
- }
+
alg = pctx->alg;
if (alg == ID_UCI_ECDH) {
ret = ((CryptoCoreContainer *)pctx->imp)->ECDH_GenAuthKey(
- (CryptoCoreContainer*)pctx->imp, pch_xk, pch_xv, pch_kauth);
+ (CryptoCoreContainer *)pctx->imp, pch_xk, pch_xv, pch_kauth);
} else if (alg == ID_UCI_DH) {
ret = ((CryptoCoreContainer *)pctx->imp)->DH_GenAuthKey(
- (CryptoCoreContainer*)pctx->imp, pch_xk, pch_xv, pch_kauth);
- } else {
+ (CryptoCoreContainer *)pctx->imp, pch_xk, pch_xv, pch_kauth);
+ } else
return UCI_INVALID_HANDLE;
- }
- if (ret != CRYPTO_SUCCESS) {
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return CRYPTO_SUCCESS;
}
-int cryptocore_prng_seed(UCI_HANDLE oh, unsigned char *seed) {
+int cryptocore_prng_seed(UCI_HANDLE oh, unsigned char *seed)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
- if (pctx == NULL) {
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
- if (pctx->alg != ID_UCI_X931) {
+
+ if (pctx->alg != ID_UCI_X931)
return UCI_INVALID_HANDLE;
- }
+
ret = ((CryptoCoreContainer *)pctx->imp)->PRNG_seed(
- (CryptoCoreContainer*)(pctx->imp), seed);
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)(pctx->imp), seed);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
int cryptocore_prng_get(UCI_HANDLE oh, unsigned int bit_len,
- unsigned char *data) {
+ unsigned char *data)
+{
int ret;
- uci_context_s *pctx = (uci_context_s*)oh;
- if (pctx == NULL) {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if (pctx->config != UCI_SW) {
+ if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- }
- if (pctx->alg != ID_UCI_X931) {
+
+ if (pctx->alg != ID_UCI_X931)
return UCI_INVALID_HANDLE;
- }
+
ret = ((CryptoCoreContainer *)pctx->imp)->PRNG_get(
- (CryptoCoreContainer*)(pctx->imp), bit_len, data);
- if (ret != CRYPTO_SUCCESS) {
+ (CryptoCoreContainer *)(pctx->imp), bit_len, data);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
}
diff --git a/ssflib/dep/uci/source/uci_hwcrypto.c b/ssflib/dep/uci/source/uci_hwcrypto.c
index 19d0ef6..adcf86e 100644
--- a/ssflib/dep/uci/source/uci_hwcrypto.c
+++ b/ssflib/dep/uci/source/uci_hwcrypto.c
@@ -37,49 +37,47 @@
#include
-/*! \brief print out by byte unit */
+/*! \brief print out by byte unit */
#undef PrintBYTE
-#define PrintBYTE(msg, Data, DataLen) { \
- int idx; \
- printf("%10s =", msg); \
- for( idx=0; idx<(int)DataLen; idx++) { \
- if( (idx!=0) && ((idx%16)==0) ) printf("\n"); \
- if((idx % 4) == 0) printf(" 0x"); \
- printf("%.2x", Data[idx]); \
- } \
- printf("\n"); \
-}
+#define PrintBYTE(msg, Data, DataLen) { \
+ int idx; \
+ printf("%10s =", msg); \
+ for (idx = 0; idx < (int)DataLen; idx++) { \
+ if ((idx != 0) && ((idx%16) == 0)) printf("\n"); \
+ if ((idx % 4) == 0) printf(" 0x"); \
+ printf("%.2x", Data[idx]); \
+ } \
+ printf("\n"); \
+ }
#define g_bTAdbug 0
#define TA_PRINT(fmt...) \
- do {if (g_bTAdbug) printf(fmt);}while(0)
+ do {if (g_bTAdbug) printf(fmt); } while (0)
#define TA_ERROR(fmt...) \
- do {if (g_bTAdbug) printf(fmt);}while(0)
+ do {if (g_bTAdbug) printf(fmt); } while (0)
/*! \brief convert 32-bit unit to 4 byte */
#undef GET_UINT32
-#define GET_UINT32(n,b,i) \
- { \
- (n) = ((unsigned int)((b)[(i) ]) << 24 ) \
- | ((unsigned int)((b)[(i) + 1]) << 16 ) \
- | ((unsigned int)((b)[(i) + 2]) << 8 ) \
- | ((unsigned int)((b)[(i) + 3]) ); \
- }
+#define GET_UINT32(n, b, i) \
+ { \
+ (n) = ((unsigned int)((b)[(i)]) << 24) \
+ | ((unsigned int)((b)[(i) + 1]) << 16) \
+ | ((unsigned int)((b)[(i) + 2]) << 8) \
+ | ((unsigned int)((b)[(i) + 3])); \
+ }
-int hwcrypto_context_alloc(unsigned int algorithm, uci_engine_config_e config, UCI_HANDLE* context)
+int hwcrypto_context_alloc(unsigned int algorithm, uci_engine_config_e config,
+ UCI_HANDLE *context)
{
- uci_context_s* ctx;
+ uci_context_s *ctx;
if (context == NULL)
- {
return UCI_ERROR;
- }
- ctx = (uci_context_s*)malloc(sizeof(uci_context_s));
- if(ctx == NULL)
- {
+ ctx = (uci_context_s *)malloc(sizeof(uci_context_s));
+
+ if (ctx == NULL)
return UCI_MEM_ALLOR_ERROR;
- }
ctx->config = config;
ctx->alg = algorithm;
@@ -88,8 +86,8 @@ int hwcrypto_context_alloc(unsigned int algorithm, uci_engine_config_e config, U
#else
ctx->imp = create_CryptoCoreContainer(algorithm);
#endif
- if(ctx->imp == NULL)
- {
+
+ if (ctx->imp == NULL) {
free(ctx);
return UCI_MEM_ALLOR_ERROR;
}
@@ -98,485 +96,456 @@ int hwcrypto_context_alloc(unsigned int algorithm, uci_engine_config_e config, U
return UCI_SUCCESS;
}
-int hwcrypto_context_free( UCI_HANDLE oh )
+int hwcrypto_context_free(UCI_HANDLE oh)
{
- uci_context_s *pctx = (uci_context_s*)oh;
- if(pctx == NULL)
- {
+ uci_context_s *pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if(pctx->imp != NULL)
- {
+
+ if (pctx->imp != NULL) {
#ifndef PC_I586
free(pctx->imp);
#else
- destroy_CryptoCoreContainer((CryptoCoreContainer*)pctx->imp);
+ destroy_CryptoCoreContainer((CryptoCoreContainer *)pctx->imp);
#endif
pctx->imp = NULL;
}
+
#ifndef PC_I586
+
/*close crypto handle*/
- if(pctx->handle >= 0)
- {
+ if (pctx->handle >= 0)
close(pctx->handle);
- }
+
#endif
free(pctx);
pctx = NULL;
return UCI_SUCCESS;
}
-int hwcrypto_se_init(UCI_HANDLE oh, unsigned int mode, unsigned int padding, unsigned char *key, unsigned int key_len, unsigned char *iv)
+int hwcrypto_se_init(UCI_HANDLE oh, unsigned int mode, unsigned int padding,
+ unsigned char *key, unsigned int key_len, unsigned char *iv)
{
#ifndef PC_I586
uci_context_s *pctx;
struct crypt_info *info;
unsigned int keytype;
int ret = 0;
- pctx = (uci_context_s*)oh;
- if(pctx == NULL)
- {
+ pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- info = (struct crypt_info*)pctx->imp;
+
+ info = (struct crypt_info *)pctx->imp;
keytype = SDRM_HIGH_HALF(pctx->config);
- if(keytype == UCI_USER_KEY && key == NULL)
- {
+ if (keytype == UCI_USER_KEY && key == NULL)
return UCI_ERROR;
- }
- switch(pctx->alg)
- {
- case ID_UCI_AES128:
- switch(mode)
- {
- case ID_UCI_ENC_CBC:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_AES_CBC_PAD;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_AES_CBC;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_ENC_CBC;
- break;
- case ID_UCI_ENC_CTR:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_AES_CTR_PAD;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_AES_CTR;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_ENC_CTR;
- break;
- case ID_UCI_ENC_ECB:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_AES_ECB_PAD;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_AES_ECB;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_ENC_ECB;
- break;
- case ID_UCI_DEC_CBC:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_AES_CBC_PAD | _MODE_DEC_;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_AES_CBC | _MODE_DEC_;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_DEC_CBC;
- break;
- case ID_UCI_DEC_CTR:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_AES_CTR_PAD | _MODE_DEC_;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_AES_CTR | _MODE_DEC_;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_DEC_CTR;
- break;
- case ID_UCI_DEC_ECB:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_AES_ECB_PAD | _MODE_DEC_;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_AES_ECB | _MODE_DEC_;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_DEC_ECB;
- break;
- default:
- return UCI_INVALID_ARGUMENT;
- }
- info->keylen = 16;
- info->ivlen = 16;
+ switch (pctx->alg) {
+ case ID_UCI_AES128:
+ switch (mode) {
+ case ID_UCI_ENC_CBC:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_AES_CBC_PAD;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_AES_CBC;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_ENC_CBC;
break;
- case ID_UCI_AES256:/*now only support ecb and ctr*/
- switch(mode)
- {
- case ID_UCI_ENC_ECB:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_AES_ECB_PAD;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_AES_ECB;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_ENC_ECB;
- break;
- case ID_UCI_DEC_ECB:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_AES_ECB_PAD | _MODE_DEC_;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_AES_ECB | _MODE_DEC_;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_DEC_ECB;
- break;
- case ID_UCI_ENC_CTR:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_AES_CTR_PAD;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_AES_CTR;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_ENC_CTR;
- break;
- case ID_UCI_DEC_CTR:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_AES_CTR_PAD | _MODE_DEC_;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_AES_CTR | _MODE_DEC_;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_DEC_CTR;
- break;
- default:
- return UCI_INVALID_ARGUMENT;
- }
- info->keylen = 32;
- info->ivlen = 16;
+
+ case ID_UCI_ENC_CTR:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_AES_CTR_PAD;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_AES_CTR;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_ENC_CTR;
break;
- case ID_UCI_DES:
- switch(mode)
- {
- case ID_UCI_ENC_CBC:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_DES_CBC_PAD;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_DES_CBC;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_ENC_CBC;
- break;
- case ID_UCI_ENC_ECB:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_DES_ECB_PAD;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_DES_ECB;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_ENC_ECB;
- break;
- case ID_UCI_DEC_CBC:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_DES_CBC_PAD | _MODE_DEC_;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_DES_CBC | _MODE_DEC_;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_DEC_CBC;
- break;
- case ID_UCI_DEC_ECB:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_DES_ECB_PAD | _MODE_DEC_;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_DES_ECB | _MODE_DEC_;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_DEC_ECB;
- break;
- default:
- return UCI_INVALID_ARGUMENT;
- }
- info->keylen = 8;
- info->ivlen = 8;
+ case ID_UCI_ENC_ECB:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_AES_ECB_PAD;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_AES_ECB;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_ENC_ECB;
break;
- case ID_UCI_TDES:
- switch(mode)
- {
- case ID_UCI_ENC_CBC:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_TDES_CBC_PAD;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_TDES_CBC;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_ENC_CBC;
- break;
- case ID_UCI_ENC_ECB:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_TDES_ECB_PAD;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_TDES_ECB;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_ENC_ECB;
- break;
- case ID_UCI_DEC_CBC:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_TDES_CBC_PAD | _MODE_DEC_;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_TDES_CBC | _MODE_DEC_;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_ENC_ECB;
- break;
- case ID_UCI_DEC_ECB:
- if(padding == ID_UCI_PKCS5)
- {
- info->mode = MI_TDES_ECB_PAD | _MODE_DEC_;
- }
- else if(padding == ID_UCI_NO_PADDING)
- {
- info->mode = MI_TDES_ECB | _MODE_DEC_;
- }
- else
- {
- return UCI_INVALID_ARGUMENT;
- }
- pctx->mode = ID_UCI_DEC_ECB;
- break;
- default:
- return UCI_INVALID_ARGUMENT;
- }
- info->keylen = 24;
- info->ivlen = 8;
+ case ID_UCI_DEC_CBC:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_AES_CBC_PAD | _MODE_DEC_;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_AES_CBC | _MODE_DEC_;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_DEC_CBC;
break;
+
+ case ID_UCI_DEC_CTR:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_AES_CTR_PAD | _MODE_DEC_;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_AES_CTR | _MODE_DEC_;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_DEC_CTR;
+ break;
+
+ case ID_UCI_DEC_ECB:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_AES_ECB_PAD | _MODE_DEC_;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_AES_ECB | _MODE_DEC_;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_DEC_ECB;
+ break;
+
default:
- return UCI_INVALID_HANDLE;
+ return UCI_INVALID_ARGUMENT;
+ }
- }
+ info->keylen = 16;
+ info->ivlen = 16;
+ break;
- /*set info key*/
+ case ID_UCI_AES256:/*now only support ecb and ctr*/
+ switch (mode) {
+ case ID_UCI_ENC_ECB:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_AES_ECB_PAD;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_AES_ECB;
- switch(keytype)
- {
- case UCI_USER_KEY:
- info->keytype = KEYID_USER_KEY;
- if(key_len != 8 && key_len != 16 && key_len != 24 && key_len != 32)
- {
+ else
return UCI_INVALID_ARGUMENT;
- }
- memcpy(info->key, key, key_len);
+
+ pctx->mode = ID_UCI_ENC_ECB;
break;
- case UCI_SECRET_KEY:
- info->keytype = KEYID_SECURE_KEY;
+
+ case ID_UCI_DEC_ECB:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_AES_ECB_PAD | _MODE_DEC_;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_AES_ECB | _MODE_DEC_;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_DEC_ECB;
break;
- case UCI_MASTER_KEY:
- info->keytype = KEYID_MASTER_KEY;
+
+ case ID_UCI_ENC_CTR:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_AES_CTR_PAD;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_AES_CTR;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_ENC_CTR;
+ break;
+
+ case ID_UCI_DEC_CTR:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_AES_CTR_PAD | _MODE_DEC_;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_AES_CTR | _MODE_DEC_;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_DEC_CTR;
break;
- default :
+
+ default:
return UCI_INVALID_ARGUMENT;
+ }
+
+ info->keylen = 32;
+ info->ivlen = 16;
+ break;
+
+ case ID_UCI_DES:
+ switch (mode) {
+ case ID_UCI_ENC_CBC:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_DES_CBC_PAD;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_DES_CBC;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_ENC_CBC;
+ break;
+
+ case ID_UCI_ENC_ECB:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_DES_ECB_PAD;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_DES_ECB;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_ENC_ECB;
+ break;
+
+ case ID_UCI_DEC_CBC:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_DES_CBC_PAD | _MODE_DEC_;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_DES_CBC | _MODE_DEC_;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_DEC_CBC;
+ break;
+
+ case ID_UCI_DEC_ECB:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_DES_ECB_PAD | _MODE_DEC_;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_DES_ECB | _MODE_DEC_;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_DEC_ECB;
+ break;
+
+ default:
+ return UCI_INVALID_ARGUMENT;
+
+ }
+
+ info->keylen = 8;
+ info->ivlen = 8;
+ break;
+
+ case ID_UCI_TDES:
+ switch (mode) {
+ case ID_UCI_ENC_CBC:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_TDES_CBC_PAD;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_TDES_CBC;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_ENC_CBC;
+ break;
+
+ case ID_UCI_ENC_ECB:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_TDES_ECB_PAD;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_TDES_ECB;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_ENC_ECB;
+ break;
+
+ case ID_UCI_DEC_CBC:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_TDES_CBC_PAD | _MODE_DEC_;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_TDES_CBC | _MODE_DEC_;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_ENC_ECB;
+ break;
+
+ case ID_UCI_DEC_ECB:
+ if (padding == ID_UCI_PKCS5)
+ info->mode = MI_TDES_ECB_PAD | _MODE_DEC_;
+
+ else if (padding == ID_UCI_NO_PADDING)
+ info->mode = MI_TDES_ECB | _MODE_DEC_;
+
+ else
+ return UCI_INVALID_ARGUMENT;
+
+ pctx->mode = ID_UCI_DEC_ECB;
+ break;
+
+ default:
+ return UCI_INVALID_ARGUMENT;
+
+ }
+
+ info->keylen = 24;
+ info->ivlen = 8;
+ break;
+
+ default:
+ return UCI_INVALID_HANDLE;
+
}
+
+ /*set info key*/
+
+ switch (keytype) {
+ case UCI_USER_KEY:
+ info->keytype = KEYID_USER_KEY;
+
+ if (key_len != 8 && key_len != 16 && key_len != 24 && key_len != 32)
+ return UCI_INVALID_ARGUMENT;
+
+ memcpy(info->key, key, key_len);
+ break;
+
+ case UCI_SECRET_KEY:
+ info->keytype = KEYID_SECURE_KEY;
+ break;
+
+ case UCI_MASTER_KEY:
+ info->keytype = KEYID_MASTER_KEY;
+ break;
+
+ default:
+ return UCI_INVALID_ARGUMENT;
+ }
+
/*setiv*/
- if(iv)
- {
+ if (iv)
memcpy(info->iv, iv, info->ivlen);
- }
+
else
- {
memset(info->iv, 0x0, info->ivlen);
- }
- pctx->handle = open("/dev/crypto", 0, 0 ); //return hndl;
+
+ pctx->handle = open("/dev/crypto", 0, 0); //return hndl;
+
//TA_PRINT("hand = %d \n",pctx->handle);
- if(pctx->handle < 0)
- {
+ if (pctx->handle < 0)
return UCI_ERROR;
- }
- if (ret = ioctl(pctx->handle, IOCTL_CRYPTO_INIT, info))
- {
- TA_PRINT("error:ioctl(hndl, IOCTL_CRYPTO_INIT, info) returned %d\n",ret);
+
+ if (ret = ioctl(pctx->handle, IOCTL_CRYPTO_INIT, info)) {
+ TA_PRINT("error:ioctl(hndl, IOCTL_CRYPTO_INIT, info) returned %d\n", ret);
return UCI_ERROR;
}
+
return UCI_SUCCESS;
#else
int ret = UCI_ERROR;
- uci_context_s *pctx = (uci_context_s*)oh;
+ uci_context_s *pctx = (uci_context_s *)oh;
unsigned int keytype;
unsigned int alg;
- //!AS current hw is not ready, so using SW pseduo way temproray.
- unsigned char hwkey_master[32]={ 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35,
- 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23,
- 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35,
- 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23};
- unsigned char hwiv_master[16] ={ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
- 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
- unsigned char hwkey_unique[32]={ 0xF0, 0x22, 0x34, 0x67, 0x66, 0x88, 0xAB, 0xCD,
- 0x12, 0x67, 0x89, 0x54, 0x32, 0x10, 0xCC, 0xFE,
- 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35,
- 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23};
- unsigned char hwiv_unique[16] ={ 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88,
- 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00};
- if(pctx == NULL)
- {
+ //!AS current hw is not ready, so using SW pseduo way temproray.
+ unsigned char hwkey_master[32] = { 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35,
+ 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23,
+ 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35,
+ 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23
+ };
+ unsigned char hwiv_master[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+ 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
+ };
+ unsigned char hwkey_unique[32] = { 0xF0, 0x22, 0x34, 0x67, 0x66, 0x88, 0xAB, 0xCD,
+ 0x12, 0x67, 0x89, 0x54, 0x32, 0x10, 0xCC, 0xFE,
+ 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35,
+ 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23
+ };
+ unsigned char hwiv_unique[16] = { 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88,
+ 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00
+ };
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
+
alg = pctx->alg;
- switch(alg)
- {
- case ID_UCI_AES128:
- key_len = 16;
- break;
- case ID_UCI_AES256:
- key_len = 32;
- break;
- case ID_UCI_DES:
- key_len = 8;
- break;
- case ID_UCI_TDES:
- key_len = 24;
+
+ switch (alg) {
+ case ID_UCI_AES128:
+ key_len = 16;
+ break;
+
+ case ID_UCI_AES256:
+ key_len = 32;
+ break;
+
+ case ID_UCI_DES:
+ key_len = 8;
+ break;
+
+ case ID_UCI_TDES:
+ key_len = 24;
}
+
keytype = SDRM_HIGH_HALF(pctx->config);
- if (keytype != UCI_USER_KEY)
- {
- if(keytype == UCI_MASTER_KEY)
- {
- ret = ((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *)pctx->imp, mode, padding, hwkey_master,key_len,hwiv_master);
+ if (keytype != UCI_USER_KEY) {
+ if (keytype == UCI_MASTER_KEY) {
+ ret = ((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *)
+ pctx->imp, mode, padding, hwkey_master, key_len, hwiv_master);
+
+ } else if (keytype == UCI_SECRET_KEY) {
+ ret = ((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *)
+ pctx->imp, mode, padding, hwkey_unique, key_len, hwiv_unique);
}
- else if(keytype == UCI_SECRET_KEY)
- {
- ret =((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *)pctx->imp, mode, padding, hwkey_unique,key_len,hwiv_unique);
- }
- }
- else
- {
- ret = ((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *)pctx->imp, mode, padding, key,key_len, iv);
+ } else {
+ ret = ((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *)
+ pctx->imp, mode, padding, key, key_len, iv);
}
- if(ret == CRYPTO_INVALID_ARGUMENT)
- {
+ if (ret == CRYPTO_INVALID_ARGUMENT)
return UCI_INVALID_ARGUMENT;
- }
- if(ret != CRYPTO_SUCCESS)
- {
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
return UCI_SUCCESS;
#endif
}
-int hwcrypto_se_process(UCI_HANDLE oh, unsigned char *input, unsigned int input_len, unsigned char *output, unsigned int *output_len)
+int hwcrypto_se_process(UCI_HANDLE oh, unsigned char *input,
+ unsigned int input_len, unsigned char *output, unsigned int *output_len)
{
#ifndef PC_I586
uci_context_s *pctx = NULL;
@@ -588,37 +557,34 @@ int hwcrypto_se_process(UCI_HANDLE oh, unsigned char *input, unsigned int input_
memset(&oper, 0, sizeof(struct crypt_oper));
- pctx = (uci_context_s*)oh;
- if(pctx == NULL)
- {
+ pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
return UCI_INVALID_HANDLE;
- }
- if(pctx->handle < 0)
- {
+
+ if (pctx->handle < 0) {
TA_PRINT("Handle error \n");
return UCI_ERROR;
}
- if(output_len != NULL)
- {
+
+ if (output_len != NULL)
*output_len = 0;
- }
- if(alg == ID_UCI_AES128 || alg == ID_UCI_AES256)
- {
- if((input_len % 16) != 0)
- {
+ if (alg == ID_UCI_AES128 || alg == ID_UCI_AES256) {
+ if ((input_len % 16) != 0) {
TA_PRINT("input_len error\n");
return UCI_ERROR;
}
+
blocksize = 16;
}
- if(alg == ID_UCI_DES || alg == ID_UCI_TDES)
- {
- if((input_len % 8) != 0)
- {
+
+ if (alg == ID_UCI_DES || alg == ID_UCI_TDES) {
+ if ((input_len % 8) != 0) {
TA_PRINT("input_len error\n");
return UCI_ERROR;
}
+
blocksize = 8;
}
@@ -626,9 +592,9 @@ int hwcrypto_se_process(UCI_HANDLE oh, unsigned char *input, unsigned int input_
oper.src_len = input_len;
oper.dst_addr = output;
oper.dst_len = output_len;
- if (ret = ioctl(pctx->handle, IOCTL_CRYPTO_CRYPT, &oper))
- {
- TA_PRINT("error:ioctl(pctx->handle , 1, &oper) returned %d\n",ret);
+
+ if (ret = ioctl(pctx->handle, IOCTL_CRYPTO_CRYPT, &oper)) {
+ TA_PRINT("error:ioctl(pctx->handle , 1, &oper) returned %d\n", ret);
return UCI_ERROR;
}
@@ -636,21 +602,23 @@ int hwcrypto_se_process(UCI_HANDLE oh, unsigned char *input, unsigned int input_
#else
int ret;
uci_context_s *ucictx = (uci_context_s *)oh;
- if(ucictx == NULL)
- {
+
+ if (ucictx == NULL)
return UCI_INVALID_HANDLE;
- }
- ret = ((CryptoCoreContainer *)ucictx->imp)->SE_process((CryptoCoreContainer*)(ucictx->imp), input, input_len, output, output_len);
- if(ret != CRYPTO_SUCCESS)
- {
+
+ ret = ((CryptoCoreContainer *)ucictx->imp)->SE_process((CryptoCoreContainer *)(
+ ucictx->imp), input, input_len, output, output_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
#endif
}
-int hwcrypto_se_final(UCI_HANDLE oh, unsigned char *input, unsigned int input_len, unsigned char *output, unsigned int *output_len)
+int hwcrypto_se_final(UCI_HANDLE oh, unsigned char *input,
+ unsigned int input_len, unsigned char *output, unsigned int *output_len)
{
#ifndef PC_I586
uci_context_s *pctx = NULL;
@@ -665,59 +633,55 @@ int hwcrypto_se_final(UCI_HANDLE oh, unsigned char *input, unsigned int input_l
unsigned int lastlen = 0;
unsigned char padding[32] = {0x0};
- memset(padding, 0, sizeof(padding)/sizeof(padding[0]));
+ memset(padding, 0, sizeof(padding) / sizeof(padding[0]));
memset(&oper, 0, sizeof(struct crypt_oper));
- pctx = (uci_context_s*)oh;
- if(pctx == NULL)
- {
+ pctx = (uci_context_s *)oh;
+
+ if (pctx == NULL)
+ return UCI_INVALID_HANDLE;
+
+ info = (struct crypt_info *) pctx->imp;
+
+ if (info == NULL)
return UCI_INVALID_HANDLE;
- }
- info = (struct crypt_info*) pctx->imp;
- if(info == NULL)
- {
- return UCI_INVALID_HANDLE;
- }
hndl = pctx->handle;
alg = pctx->alg;
- if(hndl < 0)
- {
+
+ if (hndl < 0)
return UCI_INVALID_HANDLE;
- }
- if(alg == ID_UCI_AES128 || alg == ID_UCI_AES256)
- {
+
+ if (alg == ID_UCI_AES128 || alg == ID_UCI_AES256)
blocksize = 16;
- }
- else if(alg == ID_UCI_DES|| alg == ID_UCI_TDES)
- {
+
+ else if (alg == ID_UCI_DES || alg == ID_UCI_TDES)
blocksize = 8;
- }
- else
- {
+
+ else {
return
-UCI_INVALID_HANDLE;
+ UCI_INVALID_HANDLE;
}
- if(pctx->mode == ID_UCI_ENC_CBC || pctx->mode == ID_UCI_ENC_CTR || pctx->mode == ID_UCI_ENC_ECB)/*encrypt*/
- {
+ if (pctx->mode == ID_UCI_ENC_CBC || pctx->mode == ID_UCI_ENC_CTR ||
+ pctx->mode == ID_UCI_ENC_ECB) { /*encrypt*/
lastlen = input_len % blocksize;
- if(input_len > lastlen ) /* last blocksize is bigger than blocksize or equal to blocksize*/
- {
- len = input_len -lastlen;
- ret = hwcrypto_se_process(oh,input,len,output,output_len);
- if(ret != UCI_SUCCESS)
- {
+
+ if (input_len >
+ lastlen) { /* last blocksize is bigger than blocksize or equal to blocksize*/
+ len = input_len - lastlen;
+ ret = hwcrypto_se_process(oh, input, len, output, output_len);
+
+ if (ret != UCI_SUCCESS) {
TA_PRINT("hwcrypto_se_process error \n");
return ret;
}
}
- if(MI_GET_PADDING(info->mode) == _PAD_PKCS7_)/*do padding*/
- {
- if(lastlen >0)
- {
+ if (MI_GET_PADDING(info->mode) == _PAD_PKCS7_) { /*do padding*/
+
+ if (lastlen > 0)
memcpy(padding, input + len, lastlen);
- }
+
memset(padding + lastlen, blocksize - lastlen, blocksize - lastlen);
oper.src_addr = padding;
oper.src_len = blocksize;
@@ -725,74 +689,68 @@ UCI_INVALID_HANDLE;
oper.dst_len = output_len;
//oper.final = 1;
- if(ret = ioctl(hndl, IOCTL_CRYPTO_CRYPT, &oper))
- {
- TA_PRINT("error:ioctl(hndl, 1, &oper) returned %d\n",ret);
+ if (ret = ioctl(hndl, IOCTL_CRYPTO_CRYPT, &oper)) {
+ TA_PRINT("error:ioctl(hndl, 1, &oper) returned %d\n", ret);
return UCI_ERROR;
}
*output_len = input_len - lastlen + blocksize;
}
- if(MI_GET_PADDING(info->mode) == _PAD_NO_)/*do padding*/
- {
- if(lastlen >0)
- {
+
+ if (MI_GET_PADDING(info->mode) == _PAD_NO_) { /*do padding*/
+ if (lastlen > 0)
memcpy(output + len, output + len, lastlen);
- }
+
*output_len = input_len ;
}
- }
- else/*decrypt*/
- {
+ } else { /*decrypt*/
lastlen = input_len % blocksize;
- if(input_len > lastlen)
- {
- len = input_len -lastlen;
- }
- if(len > 0)
- {
+
+ if (input_len > lastlen)
+ len = input_len - lastlen;
+
+ if (len > 0) {
oper.src_addr = (char *)input;
oper.src_len = len;
oper.dst_addr = (char *)output;
oper.dst_len = output_len;
+
//oper.final = 1;
- if (ret = ioctl(hndl, IOCTL_CRYPTO_CRYPT, &oper))
- {
- TA_PRINT("error:ioctl(hndl, 1, &oper) returned %d\n",ret);
+ if (ret = ioctl(hndl, IOCTL_CRYPTO_CRYPT, &oper)) {
+ TA_PRINT("error:ioctl(hndl, 1, &oper) returned %d\n", ret);
return UCI_ERROR;
}
}
- if(MI_GET_PADDING(info->mode) == _PAD_NO_)/*do padding*/
- {
- if(lastlen >0)
- {
+
+ if (MI_GET_PADDING(info->mode) == _PAD_NO_) { /*do padding*/
+ if (lastlen > 0)
memcpy(output + len, input + len, lastlen);
- }
+
*output_len = input_len ;
}
- if(MI_GET_PADDING(info->mode) == _PAD_PKCS7_)/*de padding*/
- {
- if(lastlen >0)
- {
- TA_PRINT("psrc_len is not aligen to %d\n",blocksize);
+
+ if (MI_GET_PADDING(info->mode) == _PAD_PKCS7_) { /*de padding*/
+ if (lastlen > 0) {
+ TA_PRINT("psrc_len is not aligen to %d\n", blocksize);
return UCI_ERROR;
}
- padlen = output[input_len -1];
+ padlen = output[input_len - 1];
+
//PrintBYTE("padding",output,input_len);
//PrintBYTE("input",input,input_len);
- if(padlen < 1 || padlen > 16)
- {
+ if (padlen < 1 || padlen > 16) {
*output_len = 0;
- TA_PRINT("padding size{%d} is incorretc ",padlen);
+ TA_PRINT("padding size{%d} is incorretc ", padlen);
return UCI_ERROR;
}
+
memset(padding, padlen, blocksize);
- if(memcmp(output + input_len - padlen ,padding, padlen) != 0)
- {
+
+ if (memcmp(output + input_len - padlen, padding, padlen) != 0) {
*output_len = 0;
- TA_PRINT("padding size{%d} is incorretc ",padlen);
+ TA_PRINT("padding size{%d} is incorretc ", padlen);
return UCI_ERROR;
}
@@ -805,15 +763,16 @@ UCI_INVALID_HANDLE;
#else
int ret;
uci_context_s *ucictx = (uci_context_s *)oh;
- if(ucictx==NULL)
- {
+
+ if (ucictx == NULL)
return UCI_INVALID_HANDLE;
- }
- ret = ((CryptoCoreContainer *)ucictx->imp)->SE_final((CryptoCoreContainer*)(ucictx->imp), input, input_len, output, output_len);
- if(ret!=CRYPTO_SUCCESS)
- {
+
+ ret = ((CryptoCoreContainer *)ucictx->imp)->SE_final((CryptoCoreContainer *)(
+ ucictx->imp), input, input_len, output, output_len);
+
+ if (ret != CRYPTO_SUCCESS)
return UCI_ERROR;
- }
+
return UCI_SUCCESS;
#endif
--
2.7.4
From 1712f0ba24e0bca9e40f09b0518da630ceb177be Mon Sep 17 00:00:00 2001
From: Uladzislau Harbuz
Date: Tue, 24 Oct 2017 17:40:16 +0200
Subject: [PATCH 11/16] Remove dead code related to downloadable TA
Change-Id: Ieff27f29b34432c0d572b85a652c05246541a9eb
---
packaging/tef-simulator.spec | 1 -
simulatordaemon/CMakeLists.txt | 1 -
simulatordaemon/inc/SecurityContext.h | 12 ----
simulatordaemon/inc/TAFactory.h | 2 +-
simulatordaemon/src/SecurityContext.cpp | 79 ----------------------
.../src/TABinaryManager/TABinaryManager.cpp | 18 ++---
.../src/TABinaryManager/TABinaryManager.h | 4 +-
simulatordaemon/src/TAFactory.cpp | 16 ++---
8 files changed, 17 insertions(+), 116 deletions(-)
diff --git a/packaging/tef-simulator.spec b/packaging/tef-simulator.spec
index 99f1571..e9696c5 100644
--- a/packaging/tef-simulator.spec
+++ b/packaging/tef-simulator.spec
@@ -13,7 +13,6 @@ BuildRequires: pkgconfig(openssl)
BuildRequires: pkgconfig(cynara-client)
BuildRequires: pkgconfig(cynara-session)
BuildRequires: pkgconfig(cynara-creds-socket)
-BuildRequires: pkgconfig(libtzplatform-config)
BuildRequires: pkgconfig(security-manager)
BuildRequires: pkgconfig(libsystemd-daemon)
BuildRequires: pkgconfig(tef-libteec)
diff --git a/simulatordaemon/CMakeLists.txt b/simulatordaemon/CMakeLists.txt
index 033b098..21b789b 100644
--- a/simulatordaemon/CMakeLists.txt
+++ b/simulatordaemon/CMakeLists.txt
@@ -23,7 +23,6 @@ PKG_CHECK_MODULES(DAEMON_DEPS REQUIRED
cynara-creds-socket
security-manager
libsystemd-daemon
- libtzplatform-config
dlog
)
diff --git a/simulatordaemon/inc/SecurityContext.h b/simulatordaemon/inc/SecurityContext.h
index 2ebf6b6..6dddfef 100644
--- a/simulatordaemon/inc/SecurityContext.h
+++ b/simulatordaemon/inc/SecurityContext.h
@@ -52,18 +52,6 @@ public:
~SecurityContext();
/**
- * This function tries to find TA by name in directories,
- * which are allowed for CA according to security policies
- *
- * @param taName Name of ta to connect for.
- * @param allowedPath Out parameter, if function returns true, it
- * contains found path to TA with given name,
- * otherwise it's empty.
- * @return true if TA was found, otherwise false.
- */
- bool findRequestedTa(const std::string &taName, std::string& allowedPath);
-
-/**
* Check if client has Tizen permission for use TEE.
*
* @param privilege Privilege to be checked for client.
diff --git a/simulatordaemon/inc/TAFactory.h b/simulatordaemon/inc/TAFactory.h
index 9063fed..3f7b092 100644
--- a/simulatordaemon/inc/TAFactory.h
+++ b/simulatordaemon/inc/TAFactory.h
@@ -55,7 +55,7 @@ private:
TAFactory();
bool checkIfTARunning(string TAUUID);
TAInstancePtr createUninitalizedTAInstance(string TAUUID, ISession* session);
- bool launchTA(string TAPath, string TAUUID, std::stringstream& str, bool debug, pid_t& pid);
+ bool launchTA(string TAUUID, std::stringstream& str, bool debug, pid_t& pid);
static void* waitForChild(void *pid);
void cleanupTAInstance(pid_t PID);
~TAFactory();
diff --git a/simulatordaemon/src/SecurityContext.cpp b/simulatordaemon/src/SecurityContext.cpp
index 3cd4e1a..109170f 100644
--- a/simulatordaemon/src/SecurityContext.cpp
+++ b/simulatordaemon/src/SecurityContext.cpp
@@ -24,22 +24,16 @@
#include "SecurityContext.h"
#include
#include
-#include
#include
#include "log.h"
-#include
#include
#include
-#include
#include
#include
#include
using p_char = std::unique_ptr>;
using p_cynara_conf = std::unique_ptr>;
-using p_tzplatform_context = std::unique_ptr>;
-
-namespace fs = boost::filesystem;
constexpr const char* SecurityContext::sysTaPaths[];
@@ -47,83 +41,10 @@ cynara* SecurityContext::_cynara = SecurityContext::initCynara();
pthread_mutex_t cynara_mutex = PTHREAD_MUTEX_INITIALIZER;
-#define FILE_WAS_FOUND 1
-#define MAX_OPENED_FD 5
-#define MAX_PATH_LENGTH 100
#define BOOST_FILESYSTEM_VERSION 3
#define RETURN_UNLOCK(ret, mtx) {pthread_mutex_unlock(&mtx); return ret;}
-std::string SecurityContext::getCaFullPathFromPkgId(char* pkgid) {
- std::string path;
-
- tzplatform_variable ids[3] = {TZ_USER_APP, TZ_SYS_RW_APP, TZ_SYS_RO_APP};
- tzplatform_context *ctx;
- if (tzplatform_context_create(&ctx) != 0) {
- LOGE(SIM_DAEMON, "Can't create tizen context");
- return "";
- }
-
- p_tzplatform_context p_ctx(ctx, &tzplatform_context_destroy);
-
- socklen_t len = (socklen_t) sizeof(struct ucred);
- struct ucred ucred;
-
- if (getsockopt(connFd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) {
- LOGE(SIM_DAEMON, "Can't get uid of client");
- return "";
- }
-
- auto clientUID = ucred.uid;
-
- if (tzplatform_context_set_user(p_ctx.get(), clientUID) != 0) {
- LOGE(SIM_DAEMON, "Can not set user for context");
- return "";
- }
-
- for (auto &id : ids) {
- path = std::move(tzplatform_context_getenv(p_ctx.get(), id));
- LOGD(SIM_DAEMON, "Path is : %s", path.c_str());
- if (!path.empty()) break;
- }
-
- if (!fs::exists(path)) {
- LOGE(SIM_DAEMON, "Path doesn't exist: %s", path.c_str());
- return "";
- }
- if (fs::is_symlink(path)) {
- path = fs::read_symlink(path).string();
- }
- if (path.empty()) {
- LOGE(SIM_DAEMON, "Bad CA path. Does this directory exist: %s ?", path.c_str());
- }
-
- path += "/" + std::string(pkgid) + TA_LOCAL_PATH;
- LOGD(SIM_DAEMON, "Path: %s", path.c_str());
- if (!fs::exists(path)) {
- LOGE(SIM_DAEMON, "Path: %s not found", path.c_str());
- return "";
- }
-
- return path;
-}
-
-
-bool SecurityContext::findRequestedTa(const std::string &ta_name, std::string &allowed_path) {
- LOGD(SIM_DAEMON, "Entry");
- std::string ta_full_path;
- /* Check if any of system ta directories contains our ta */
- for (const std::string& path : sysTaPaths) {
- ta_full_path = path + ta_name;
- if (fs::exists(ta_full_path)){
- allowed_path = path;
- return true;
- }
- }
- return false;
-}
-
-
bool SecurityContext::clientHasCynaraPermission(const std::string &privelege) {
int ret = -1;
diff --git a/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp b/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp
index f0cae26..4561b2e 100644
--- a/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp
+++ b/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp
@@ -25,6 +25,7 @@
* Include files
*-----------------------------------------------------------------------------*/
#include "TABinaryManager.h"
+#include "Config.h"
#include
#include
#include
@@ -155,10 +156,10 @@ TABinaryManager* TABinaryManager::getInstance() {
}
/**
- * This function add TA at given path to BinaryManager if it exists.
+ * This function add TA to BinaryManager if it exists.
* @return On successful completion of above operations returns true else false.
*/
-bool TABinaryManager::initTAatPath(const string &path, const string &uuid) {
+bool TABinaryManager::initTA(const string &uuid) {
LOGD(SIM_DAEMON, "Entry");
pthread_rwlock_wrlock(&binaryMapLock);
@@ -166,10 +167,10 @@ bool TABinaryManager::initTAatPath(const string &path, const string &uuid) {
bool res = false;
StructBinaryInfo info;
- if (boost::filesystem::exists(path + uuid)) {
+ if (boost::filesystem::exists(TA_STORE_PATH + uuid)) {
pthread_mutex_lock(&taLock);
try {
- if (unpackBinary(uuid, path, info)) {
+ if (unpackBinary(uuid, info)) {
binaryMap[uuid] = info;
res = true;
}
@@ -244,15 +245,14 @@ void TABinaryManager::decryptImage(StructBinaryInfo& info) {
* It is very important to check for return value from this function.
*/
-bool TABinaryManager::unpackBinary(const string &uuid, const string &path, StructBinaryInfo& info) {
+bool TABinaryManager::unpackBinary(const string &uuid, StructBinaryInfo& info) {
TAUnpack* unpacker = TAUnpack::getInstance();
bool ret = false;
- LOGD(SIM_DAEMON, "Unpacking TA %s in %s", uuid.c_str(), path.c_str());
- if (0 == unpacker->unpackTA(path, uuid)) {
+ if (0 == unpacker->unpackTA(string(TA_STORE_PATH), uuid)) {
LOGD(SIM_DAEMON, "Unpacked, filling info");
// 1. Set binary info
- info.path = path + uuid;
- info.extractpath = path + uuid + "-ext/";
+ info.path = string(TA_STORE_PATH)+ uuid;
+ info.extractpath = string(TA_STORE_PATH) + uuid + "-ext/";
info.imagePath = info.extractpath + uuid + ".image";
info.manifestPath = info.extractpath + uuid + ".manifest";
// 2. Parse manifest and store results
diff --git a/simulatordaemon/src/TABinaryManager/TABinaryManager.h b/simulatordaemon/src/TABinaryManager/TABinaryManager.h
index bf7366e..108fff0 100644
--- a/simulatordaemon/src/TABinaryManager/TABinaryManager.h
+++ b/simulatordaemon/src/TABinaryManager/TABinaryManager.h
@@ -64,7 +64,7 @@ private:
// map < string uuid, StructBinaryInfo>
map binaryMap;
TABinaryManager();
- bool unpackBinary(const string &uuid, const string &path, StructBinaryInfo& info);
+ bool unpackBinary(const string &uuid, StructBinaryInfo& info);
template
std::string IntToHex(T i, int width = sizeof(T) * 2) {
std::stringstream stream;
@@ -85,7 +85,7 @@ public:
*/
pthread_mutex_t taLock;
static TABinaryManager* getInstance();
- bool initTAatPath(const string &path, const string &uuid);
+ bool initTA (const string &uuid);
/*
* Query functions on Binary Manager
diff --git a/simulatordaemon/src/TAFactory.cpp b/simulatordaemon/src/TAFactory.cpp
index f55e11d..4c5bcbe 100644
--- a/simulatordaemon/src/TAFactory.cpp
+++ b/simulatordaemon/src/TAFactory.cpp
@@ -194,12 +194,7 @@ TAInstancePtr TAFactory::createUninitalizedTAInstance(string TAUUID,
str << TAUUID << "-";
str << InstID;
- string allowedTAPath;
- if(!session->getSecurityContext().findRequestedTa(TAUUID, allowedTAPath)) {
- LOGE(SIM_DAEMON, "Access for TA %s forbidden", TAUUID.c_str());
- return TAInstancePtr();
- }
- if (launchTA(allowedTAPath, TAUUID, str, debug, pid)) {
+ if (launchTA(TAUUID, str, debug, pid)) {
// TA is launched successfully, Create a new instance of TAInstance class
/* Check if TA is to be keep alive and accordingly set TAInstance's
@@ -347,22 +342,21 @@ void TAFactory::cleanupTAInstance(pid_t PID) {
* @param debug debug flag
* @param pid PID to be update for launched TA
*/
-bool TAFactory::launchTA(string path, string TAUUID, std::stringstream& str, bool debug,
+bool TAFactory::launchTA(string TAUUID, std::stringstream& str, bool debug,
pid_t& pid) {
int32_t result = -1;
pthread_t thread;
LOGD(SIM_DAEMON, "Entry");
- LOGD(SIM_DAEMON, "Path: %s", path.c_str());
// Get TABinaryManager instance
TABinaryManager *TABin = TABinaryManager::getInstance();
// Get TA Image path for launching
string argvPath = "";
- if (TABin->initTAatPath(path, TAUUID)) {
- argvPath = TABin->getImagePath(TAUUID);
- LOGD(SIM_DAEMON, "argvPath: ", argvPath.c_str());
+ if (TABin->initTA(TAUUID)) {
+ argvPath = TABin->getImagePath(TAUUID);
}
+
if ("" == argvPath) {
LOGE(SIM_DAEMON, "Trusted Application does not exist");
return false;
--
2.7.4
From a41ff8624122b876d3d775192c567d7885aa4ece Mon Sep 17 00:00:00 2001
From: akoszewski
Date: Mon, 6 Nov 2017 15:25:13 +0100
Subject: [PATCH 12/16] Fix smack labels on tef-simulator files
Change-Id: I7964f1b1545c9961e499755562c827eb6e9d3992
---
packaging/tef-simulator.spec | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/packaging/tef-simulator.spec b/packaging/tef-simulator.spec
index 99f1571..d318b5a 100644
--- a/packaging/tef-simulator.spec
+++ b/packaging/tef-simulator.spec
@@ -5,6 +5,7 @@ Release: 1
Group: Security
License: Apache-2.0
Source0: %{name}-%{version}.tar.gz
+Source1: %{name}.manifest
ExcludeArch: armv6l armv7hl armv7l aarch64
BuildRequires: cmake
BuildRequires: boost-devel
@@ -68,6 +69,7 @@ with TEF Simulator.
%prep
%setup -q
+cp %{SOURCE1} .
%build
# cannot call cmake rpmbuild macro because of scripts removing libTEEStub.a, which is a part of devkit
@@ -106,6 +108,7 @@ if [ $1 = 0 ] ; then
fi
%files -n %{name}
+%manifest tef-simulator.manifest
%attr(111,security_fw,security_fw) %{bin_dir}/tef-simulator-daemon
%{lib_dir}/libtef-simulator-ssflib.so
%attr(770,root,security_fw) %{tastore_dir}
@@ -113,7 +116,6 @@ fi
%attr(444,security_fw,security_fw) %{_unitdir}/tef-simulator.socket
%attr(755,security_fw,security_fw) %{lib_dir}/tef/simulator/libteec.so
-
%files -n %{name}-devkit
%{bin_dir}/TA_PackageBuilder.sh
%{bin_dir}/TAPackageMaker
--
2.7.4
From 5c88eac4359c8e3f77957ce5d8e8186c6be0b705 Mon Sep 17 00:00:00 2001
From: akoszewski
Date: Fri, 27 Oct 2017 15:25:02 +0200
Subject: [PATCH 13/16] Fix TEEC operation preprocessing
TEEC Operation arguments are now parsed properly.
Change-Id: I91a811158b118066a2377a1d47eec36b9e8e03ac
---
TEECLib/src/teec_api.c | 120 +++++++++++++++++++++++--------------------------
1 file changed, 57 insertions(+), 63 deletions(-)
diff --git a/TEECLib/src/teec_api.c b/TEECLib/src/teec_api.c
index a3c0c71..d1f68c8 100644
--- a/TEECLib/src/teec_api.c
+++ b/TEECLib/src/teec_api.c
@@ -267,6 +267,42 @@ static uint32_t checkContext(TEEC_Context *context)
return found;
}
+TEEC_Result tempSharedMemAllocate(TEEC_SharedMemory** tmpSharedMem, uint32_t type,
+ TEEC_Context* context, TEEC_Parameter param)
+{
+ TEEC_Result result;
+ *tmpSharedMem = (TEEC_SharedMemory *)OsaMalloc(sizeof(TEEC_SharedMemory));
+ (*tmpSharedMem)->size = param.tmpref.size;
+ (*tmpSharedMem)->buffer = param.tmpref.buffer;
+
+ (*tmpSharedMem)->flags = type & (TEEC_MEM_INPUT | TEEC_MEM_OUTPUT);
+
+ result = TEEC_RegisterSharedMemory(context, *tmpSharedMem);
+
+ if (result != TEEC_SUCCESS) {
+ if (*tmpSharedMem) {
+ OsaFree(*tmpSharedMem);
+ *tmpSharedMem = NULL;
+ }
+ return result;
+ }
+
+ if (type & TEEC_MEMREF_TEMP_INPUT) {
+ memcpy(((TEEC_SharedMemoryImp *)(*tmpSharedMem)->imp)->allocPtr,
+ (*tmpSharedMem)->buffer, (*tmpSharedMem)->size);
+ }
+ return result;
+}
+
+void tempSharedMemoryDeallocate(TEEC_SharedMemory** tmpSharedMem)
+{
+ if (*tmpSharedMem) {
+ TEEC_ReleaseSharedMemory(*tmpSharedMem);
+ OsaFree(*tmpSharedMem);
+ *tmpSharedMem = NULL;
+ }
+}
+
/*
* === FUNCTION ======================================================================
* Name: preProcessOperation
@@ -339,38 +375,10 @@ static TEEC_Result preProcessOperation(TEEC_Session *session,
op->paramTypes |= type << (8 * i);
if (!tmpSharedMem[i]) {
- tmpSharedMem[i] = (TEEC_SharedMemory *)OsaMalloc(
- sizeof(TEEC_SharedMemory));
- tmpSharedMem[i]->size = operation->params[i].tmpref.size;
- tmpSharedMem[i]->buffer = operation->params[i].tmpref.buffer;
-
- if (type == TEEC_MEMREF_TEMP_INPUT)
- tmpSharedMem[i]->flags = TEEC_MEM_INPUT;
-
- else if (type == TEEC_MEMREF_TEMP_OUTPUT)
- tmpSharedMem[i]->flags = TEEC_MEM_OUTPUT;
-
- else if (type == TEEC_MEMREF_TEMP_INOUT)
- tmpSharedMem[i]->flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
-
- result = TEEC_RegisterSharedMemory(
- ((TEEC_SessionImp *)session->imp)->context, tmpSharedMem[i]);
-
- if (result != TEEC_SUCCESS) {
- for (i = 0; i < 4; i++) {
- if (tmpSharedMem[i]) {
- OsaFree(tmpSharedMem[i]);
- tmpSharedMem[i] = NULL;
- }
- }
-
- return result;
- }
-
- if (type & TEEC_MEMREF_TEMP_INPUT) {
- memcpy(((TEEC_SharedMemoryImp *)tmpSharedMem[i]->imp)->allocPtr,
- tmpSharedMem[i]->buffer, tmpSharedMem[i]->size);
- }
+ result = tempSharedMemAllocate(&tmpSharedMem[i], type,
+ ((TEEC_SessionImp *)session->imp)->context, operation->params[i]);
+ if (result != TEEC_SUCCESS)
+ goto cleanup;
}
op->params[i].mem.size = tmpSharedMem[i]->size;
@@ -380,25 +388,19 @@ static TEEC_Result preProcessOperation(TEEC_Session *session,
break;
case TEEC_MEMREF_WHOLE:
- op->paramTypes |= TEE_PARAM_TYPE_MEMREF_INOUT << (8 * i);
memref = &operation->params[i].memref;
if ((NULL == memref) || (NULL == memref->parent)
|| (((TEEC_SharedMemoryImp *)memref->parent->imp)->context->imp
!= context->imp)) {
- for (i = 0; i < 4; i++) {
- if (tmpSharedMem[i]) {
- TEEC_ReleaseSharedMemory(tmpSharedMem[i]);
- OsaFree(tmpSharedMem[i]);
- tmpSharedMem[i] = NULL;
- }
- }
-
LOGE(TEEC_LIB, "Bad parameters");
- return TEEC_ERROR_BAD_PARAMETERS;
+ result = TEEC_ERROR_BAD_PARAMETERS;
+ goto cleanup;
}
memref_imp = (TEEC_SharedMemoryImp *)memref->parent->imp;
+ op->paramTypes |= (operation->params[i].memref.parent->flags |
+ (TEE_PARAM_TYPE_MEMREF_INPUT & TEE_PARAM_TYPE_MEMREF_OUTPUT)) << (8 * i);
op->params[i].mem.offset = 0;
op->params[i].mem.size = memref->parent->size;
op->params[i].mem.shmKey = memref_imp->shmKey;
@@ -411,23 +413,16 @@ static TEEC_Result preProcessOperation(TEEC_Session *session,
case TEEC_MEMREF_PARTIAL_INPUT:
case TEEC_MEMREF_PARTIAL_OUTPUT:
case TEEC_MEMREF_PARTIAL_INOUT:
- op->paramTypes |= (type + TEE_PARAM_TYPE_MEMREF_INPUT
- - TEEC_MEMREF_PARTIAL_INPUT) << (8 * i);
+ op->paramTypes |= ((operation->params[i].memref.parent->flags & type) |
+ (TEE_PARAM_TYPE_MEMREF_INPUT & TEE_PARAM_TYPE_MEMREF_OUTPUT)) << (8 * i);
memref = &operation->params[i].memref;
if ((NULL == memref) || (NULL == memref->parent)
|| (((TEEC_SharedMemoryImp *)memref->parent->imp)->context->imp
!= context->imp)) {
- for (i = 0; i < 4; i++) {
- if (tmpSharedMem[i]) {
- TEEC_ReleaseSharedMemory(tmpSharedMem[i]);
- OsaFree(tmpSharedMem[i]);
- tmpSharedMem[i] = NULL;
- }
- }
-
LOGE(TEEC_LIB, "Bad parameters");
- return TEEC_ERROR_BAD_PARAMETERS;
+ result = TEEC_ERROR_BAD_PARAMETERS;
+ goto cleanup;
}
memref_imp = (TEEC_SharedMemoryImp *)memref->parent->imp;
@@ -446,19 +441,18 @@ static TEEC_Result preProcessOperation(TEEC_Session *session,
break;
default:
- for (i = 0; i < 4; i++) {
- if (tmpSharedMem[i]) {
- TEEC_ReleaseSharedMemory(tmpSharedMem[i]);
- OsaFree(tmpSharedMem[i]);
- tmpSharedMem[i] = NULL;
- }
- }
-
- return TEEC_ERROR_BAD_PARAMETERS;
+ result = TEEC_ERROR_BAD_PARAMETERS;
+ goto cleanup;
}
}
- return TEEC_SUCCESS;
+ return result;
+
+cleanup:
+ for (i = 0; i < 4; i++) {
+ tempSharedMemoryDeallocate(&tmpSharedMem[i]);
+ }
+ return result;
}
/*
--
2.7.4
From 00a43a81817133b64e314cfafd297f5d7e9ac861 Mon Sep 17 00:00:00 2001
From: akoszewski
Date: Fri, 10 Nov 2017 12:43:23 +0100
Subject: [PATCH 14/16] Add stub PrintLog function
Add stub PrintLog function in release tef-simulator
to fix error with loading debug TA by release tef simulator
Change-Id: Ia673fbb615baaff9834c339ce965baad5317a444
---
log/log.c | 15 +++++++++++++++
log/log.h | 13 -------------
2 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/log/log.c b/log/log.c
index cfc55c6..afe0f81 100644
--- a/log/log.c
+++ b/log/log.c
@@ -150,6 +150,9 @@ const char *GetModuleLevel(IN int32_t module_level)
*
* @return void
*/
+
+#ifdef _LOGGING
+
__attribute__((visibility("default")))
void PrintLog(IN const char *function_name, IN const int32_t line_no,
IN int32_t module_level, IN int32_t debug_level, IN const char *message,
@@ -236,3 +239,15 @@ void PrintLog(IN const char *function_name, IN const int32_t line_no,
va_end(variable_list);
return;
}
+
+#else // ifdef _LOGGING
+
+__attribute__((visibility("default")))
+void PrintLog(IN const char *function_name, IN const int32_t line_no,
+ IN int32_t module_level, IN int32_t debug_level, IN const char *message,
+ ...)
+{
+ // stub function
+}
+
+#endif // ifdef _LOGGING
diff --git a/log/log.h b/log/log.h
index 11b2fd4..8c31f2d 100644
--- a/log/log.h
+++ b/log/log.h
@@ -102,8 +102,6 @@ typedef enum {
#endif
#endif // __TIZEN__
-#ifdef _LOGGING
-
#define _LOG(module_level, debug_level, ...) PrintLog(__FUNCTION__, __LINE__, module_level, debug_level, ##__VA_ARGS__)
#define LOGE(module_level, ...) PrintLog(__FUNCTION__, __LINE__, module_level, ERROR_LEVEL_LOG, ##__VA_ARGS__)
@@ -113,17 +111,6 @@ typedef enum {
#define LOGS(module_level, ...) PrintLog(__FUNCTION__, __LINE__, module_level, INFO_LEVEL_LOG, ##__VA_ARGS__)
#define LOGP(module_level, ...) PrintLog(__FUNCTION__, __LINE__, module_level, PACKET_LEVEL_LOG, ##__VA_ARGS__)
-#else //ifdef _LOGGING
-
-#define LOGE(module_level, ...)
-#define LOGV(module_level, ...)
-#define LOGD(module_level, ...)
-#define LOGI(module_level, ...)
-#define LOGS(module_level, ...)
-#define LOGP(module_level, ...)
-
-#endif //ifdef _LOGGING
-
#if defined(__cplusplus)
extern "C" {
#endif
--
2.7.4
From 36e40dddbda41db1f8cad529664dbae3d1bc664f Mon Sep 17 00:00:00 2001
From: Krzysztof Dynowski
Date: Tue, 14 Nov 2017 15:27:02 +0100
Subject: [PATCH 15/16] Send (correct) response to client when cynara danied
access
Change-Id: I53b11b0149725dc88679febb6706af42b8d37cb3
---
simulatordaemon/inc/TEEContext.h | 1 +
simulatordaemon/src/ConnectionSession.cpp | 17 +++++++++--------
simulatordaemon/src/TEEContext.cpp | 14 ++++++++++++++
3 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/simulatordaemon/inc/TEEContext.h b/simulatordaemon/inc/TEEContext.h
index 4db8506..ab68665 100644
--- a/simulatordaemon/inc/TEEContext.h
+++ b/simulatordaemon/inc/TEEContext.h
@@ -61,6 +61,7 @@ public:
uint32_t mContextID;
/* Security context wich stores info about low-level connection data*/
SecurityContext secContext;
+ bool cynara_check_result;
/* For TA internal APIs support, dummy Context is created and for recognizing
* the context as dummy isInternal member variable is used
diff --git a/simulatordaemon/src/ConnectionSession.cpp b/simulatordaemon/src/ConnectionSession.cpp
index 31fb181..8fadf2f 100644
--- a/simulatordaemon/src/ConnectionSession.cpp
+++ b/simulatordaemon/src/ConnectionSession.cpp
@@ -48,22 +48,23 @@ void ConnectionSession::start() {
// init SecurityContext of current session after initializing socket
this->secContext = SecurityContext(clientSocket.native());
+ // Create a new Context
+ pthread_rwlock_wrlock(&ctxIDLock);
+ TEECtx = new TEEContext(ctxID, this);
+ // Increment the Context ID to be assigned to next Context
+ ctxID++;
+ if (ctxID == 0) ctxID++;
+ pthread_rwlock_unlock(&ctxIDLock);
+
#ifdef _CYNARA_INTEGRATION
/* Check if client has cynara permission */
const string privilege("http://tizen.org/privilege/tee.client");
if (!secContext.clientHasCynaraPermission(privilege)) {
LOGE(SIM_DAEMON, "Client has no permission to use TEE");
- return;
+ TEECtx->cynara_check_result = false;
}
#endif /* _CYNARA_INTEGRATION */
- // Create a new Context
- pthread_rwlock_wrlock(&ctxIDLock);
- TEECtx = new TEEContext(ctxID, this);
- // Increment the Context ID to be assigned to next Context
- ctxID++;
- if (ctxID == 0) ctxID++;
- pthread_rwlock_unlock(&ctxIDLock);
currentState = CMD_READ;
// read exactly 1 byte to identify the command and execute callback when
diff --git a/simulatordaemon/src/TEEContext.cpp b/simulatordaemon/src/TEEContext.cpp
index f85729c..189f918 100644
--- a/simulatordaemon/src/TEEContext.cpp
+++ b/simulatordaemon/src/TEEContext.cpp
@@ -57,6 +57,8 @@ TEEContext::TEEContext(uint32_t contextID, IConnectionSession* connSession):
/* Clear the shared memory list (mShmList) and Session map (mSessionMap) */
mSessionMap.clear();
mShmList.clear();
+
+ cynara_check_result = true;
}
/**
@@ -75,6 +77,17 @@ TEEC_Result TEEContext::initContext(InitContextData* data) {
*/
isInternal = false;
+ if (!cynara_check_result) {
+ result = TEEC_ERROR_ACCESS_DENIED;
+ data->returnValue = result;
+ result = mConnSess->write(INITIALIZE_CONTEXT, (char*)data,
+ sizeof(InitContextData));
+ if (result != TEEC_SUCCESS) {
+ LOGE(SIM_DAEMON, "Initialize Context response write to CA FAILED");
+ }
+ return result;
+ }
+
/* Check if the TEEName is proper or not */
if (data->nameLength != 0) {
string TName(data->TEEName);
@@ -167,6 +180,7 @@ TEEC_Result TEEContext::openSession(OpenSessionData data) {
LOGD(SIM_DAEMON, "Entry");
data.returnOrigin = TEEC_ORIGIN_TEE;
data.returnValue = TEEC_ERROR_GENERIC;
+
pthread_rwlock_wrlock(&sessIDLock);
data.sessionID = sessID;
sessID++;
--
2.7.4
From 4bf6c192dc128b4407372f96450b7fa181a60f81 Mon Sep 17 00:00:00 2001
From: Uladzislau Harbuz
Date: Fri, 10 Nov 2017 18:23:01 +0100
Subject: [PATCH 16/16] Fix C++Test static analysis violations
Change-Id: Ia458bb472af6f3cd18cc9dd1ccaacb9bc7558805
---
TEEStub/PropertyAccess/Property.h | 6 +-
TEEStub/PropertyAccess/PropertyApi.cpp | 2 +-
TEEStub/PropertyAccess/PropertyUtility.cpp | 1 -
TEEStub/TACommands/CommandCloseSession.cpp | 6 +-
TEEStub/TACommands/CommandCloseSession.h | 2 +-
TEEStub/TACommands/CommandCreateEntryPoint.cpp | 12 +-
TEEStub/TACommands/CommandDestroyEntryPoint.cpp | 12 +-
include/include/tee_command.h | 1 -
log/log.c | 3 +
log/log.h | 2 +-
osal/CMakeLists.txt | 4 +
osal/OsaCommon.c | 44 ++-----
osal/OsaIpc.c | 41 ++++---
osal/OsaQueue.c | 58 +++------
osal/OsaSem.c | 65 ++++-------
osal/OsaSignal.c | 9 +-
osal/OsaTask.c | 27 ++---
simulatordaemon/inc/SecurityContext.h | 2 +-
simulatordaemon/src/ClientCommands/MakeCommand.cpp | 4 -
simulatordaemon/src/SecurityContext.cpp | 8 +-
simulatordaemon/src/TABinaryManager/TAManifest.cpp | 5 -
simulatordaemon/src/TABinaryManager/TAUnpack.cpp | 2 -
ssflib/dep/cryptocore/source/base/cc_bignum.c | 129 +--------------------
ssflib/dep/cryptocore/source/base/cc_ecc.c | 2 -
ssflib/dep/cryptocore/source/base/cc_md5.c | 7 --
ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c | 1 -
ssflib/dep/cryptocore/source/base/cc_rc4.c | 2 -
ssflib/dep/cryptocore/source/base/cc_sha1.c | 81 +------------
ssflib/dep/cryptocore/source/middle/cc_cmac.c | 5 +-
ssflib/dep/cryptocore/source/middle/cc_dsa.c | 3 +-
ssflib/dep/cryptocore/source/middle/cc_ecdsa.c | 20 +---
ssflib/dep/cryptocore/source/middle/cc_rsa.c | 21 +---
ssflib/dep/swdss/source/file_op.cpp | 3 -
ssflib/dep/swdss/source/secure_file.cpp | 43 ++-----
ssflib/dep/swdss/source/ss_crypto.cpp | 2 -
ssflib/dep/uci/source/uci_api.c | 1 -
ssflib/dep/uci/source/uci_cryptocore.c | 2 -
ssflib/dep/uci/source/uci_hwcrypto.c | 31 ++---
ssflib/inc/app_debug.h | 1 -
ssflib/src/app_debug.cpp | 1 -
ssflib/src/ssf_client.cpp | 1 -
ssflib/src/ssf_crypto.cpp | 43 ++-----
ssflib/src/ssf_storage.cpp | 21 ----
43 files changed, 172 insertions(+), 564 deletions(-)
diff --git a/TEEStub/PropertyAccess/Property.h b/TEEStub/PropertyAccess/Property.h
index 80ecb4c..23c81bc 100644
--- a/TEEStub/PropertyAccess/Property.h
+++ b/TEEStub/PropertyAccess/Property.h
@@ -52,15 +52,15 @@ public:
virtual void reset() = 0;
virtual bool getPropertyValue(PropertyValue&) = 0;
void setPropSet(uintptr_t propset) {
- this->propset = propset;
+ this->m_propset = propset;
};
- Property() : propset(0) {
+ Property() : m_propset(0) {
}
;
virtual ~Property() {
}
;
- uintptr_t propset;
+ uintptr_t m_propset;
};
#endif /* PROPERTYACCESS_PROPERTY_H_ */
diff --git a/TEEStub/PropertyAccess/PropertyApi.cpp b/TEEStub/PropertyAccess/PropertyApi.cpp
index 2a52fdf..50e3a99 100644
--- a/TEEStub/PropertyAccess/PropertyApi.cpp
+++ b/TEEStub/PropertyAccess/PropertyApi.cpp
@@ -413,7 +413,7 @@ uintptr_t _GetTargetPropsetType(TEE_PropSetHandle propsetOrEnumerator) {
(PropertyEnumHandle*)propsetOrEnumerator;
if (enumHandle && enumHandle->property) {
targetProperty = enumHandle->property;
- return targetProperty->propset;
+ return targetProperty->m_propset;
}
}
return 0;
diff --git a/TEEStub/PropertyAccess/PropertyUtility.cpp b/TEEStub/PropertyAccess/PropertyUtility.cpp
index 930f5d3..d7dfec9 100644
--- a/TEEStub/PropertyAccess/PropertyUtility.cpp
+++ b/TEEStub/PropertyAccess/PropertyUtility.cpp
@@ -127,7 +127,6 @@ TEE_Result PropertyUtility::convertToUUID(const PropertyValue& in,
uint64_t clockSeq;
string clockSeqStr = tokensString[4] + tokensString[5] + tokensString[6]
+ tokensString[7];
- //TEST CODE: string clockSeqStr("0123456789ABCDEF");
clockSeq = std::stoll(clockSeqStr);
memcpy(uuid.clockSeqAndNode, &clockSeq, sizeof(uint64_t));
// Change endian-ness
diff --git a/TEEStub/TACommands/CommandCloseSession.cpp b/TEEStub/TACommands/CommandCloseSession.cpp
index 74433bc..17db1d3 100644
--- a/TEEStub/TACommands/CommandCloseSession.cpp
+++ b/TEEStub/TACommands/CommandCloseSession.cpp
@@ -37,7 +37,7 @@
*/
CommandCloseSession::CommandCloseSession(CloseTASessionData data) :
CommandBase(CLOSESESSION) {
- this->data = data;
+ this->m_data = data;
sessionID = data.sessionID;
}
@@ -61,7 +61,7 @@ TEE_Result CommandCloseSession::execute() {
*/
std::string CommandCloseSession::getCommandUID() const {
std::stringstream ss;
- ss << data.sessionID;
+ ss << m_data.sessionID;
ss << ":";
ss << "0";
return ss.str();
@@ -81,6 +81,6 @@ CommandCloseSession::~CommandCloseSession() {
void CommandCloseSession::getSerializedData(unsigned char *data,
unsigned int &size) {
data[0] = command;
- memcpy(&data[1], (unsigned char*)&this->data, sizeof(CloseTASessionData));
+ memcpy(&data[1], (unsigned char*)&this->m_data, sizeof(CloseTASessionData));
size = sizeof(CloseTASessionData);
}
diff --git a/TEEStub/TACommands/CommandCloseSession.h b/TEEStub/TACommands/CommandCloseSession.h
index 67b38d3..fb37242 100644
--- a/TEEStub/TACommands/CommandCloseSession.h
+++ b/TEEStub/TACommands/CommandCloseSession.h
@@ -39,7 +39,7 @@
class CommandCloseSession:
public CommandBase {
public:
- CloseTASessionData data;
+ CloseTASessionData m_data;
void getSerializedData(unsigned char *data, unsigned int &size);
CommandCloseSession(CloseTASessionData data);
TEE_Result execute();
diff --git a/TEEStub/TACommands/CommandCreateEntryPoint.cpp b/TEEStub/TACommands/CommandCreateEntryPoint.cpp
index 15c0df9..bb32028 100644
--- a/TEEStub/TACommands/CommandCreateEntryPoint.cpp
+++ b/TEEStub/TACommands/CommandCreateEntryPoint.cpp
@@ -31,10 +31,10 @@
/*-----------------------------------------------------------------------------
* Member functions
*-----------------------------------------------------------------------------*/
-CommandCreateEntryPoint::CommandCreateEntryPoint(CreateTAEntryPointData data) :
+CommandCreateEntryPoint::CommandCreateEntryPoint(CreateTAEntryPointData _data) :
CommandBase(CREATE) {
- this->data = data;
- sessionID = data.sessionID;
+ this->data = _data;
+ sessionID = _data.sessionID;
}
/**
@@ -69,9 +69,9 @@ CommandCreateEntryPoint::~CommandCreateEntryPoint() {
* @param data[out] serialized object data
* @param size[out] size in bytes
*/
-void CommandCreateEntryPoint::getSerializedData(unsigned char *data,
+void CommandCreateEntryPoint::getSerializedData(unsigned char *_data,
unsigned int &size) {
- data[0] = command;
- memcpy(&data[1], (unsigned char*)&this->data, sizeof(CreateTAEntryPointData));
+ _data[0] = command;
+ memcpy(&_data[1], (unsigned char*)&this->data, sizeof(CreateTAEntryPointData));
size = sizeof(CreateTAEntryPointData);
}
diff --git a/TEEStub/TACommands/CommandDestroyEntryPoint.cpp b/TEEStub/TACommands/CommandDestroyEntryPoint.cpp
index 84af8c9..616b12f 100644
--- a/TEEStub/TACommands/CommandDestroyEntryPoint.cpp
+++ b/TEEStub/TACommands/CommandDestroyEntryPoint.cpp
@@ -31,10 +31,10 @@
/*-----------------------------------------------------------------------------
* Member functions
*-----------------------------------------------------------------------------*/
-CommandDestroyEntryPoint::CommandDestroyEntryPoint(DestroyTAEntryPointData data) :
+CommandDestroyEntryPoint::CommandDestroyEntryPoint(DestroyTAEntryPointData _data) :
CommandBase(DESTROY) {
- this->data = data;
- sessionID = data.sessionID;
+ this->data = _data;
+ sessionID = _data.sessionID;
}
//TODO: Handle exit of TA instance in a clean way
@@ -71,10 +71,10 @@ CommandDestroyEntryPoint::~CommandDestroyEntryPoint() {
* @param data[out] serialized object data
* @param size[out] size in bytes
*/
-void CommandDestroyEntryPoint::getSerializedData(unsigned char *data,
+void CommandDestroyEntryPoint::getSerializedData(unsigned char *_data,
unsigned int &size) {
- data[0] = command;
- memcpy(&data[1], (unsigned char*)&this->data,
+ _data[0] = command;
+ memcpy(&_data[1], (unsigned char*)&this->data,
sizeof(DestroyTAEntryPointData));
size = sizeof(DestroyTAEntryPointData);
}
diff --git a/include/include/tee_command.h b/include/include/tee_command.h
index c3f2c4a..f2197cd 100644
--- a/include/include/tee_command.h
+++ b/include/include/tee_command.h
@@ -36,7 +36,6 @@ typedef enum {
OPEN_TA_SESSION,
INVOKE_TA_COMMAND,
CLOSE_TA_SESSION,
- CHECK_MEMORY,
PANIC
} TEE_CMD;
#endif /* __TEE_COMMAND_H__ */
diff --git a/log/log.c b/log/log.c
index afe0f81..4b1e3d6 100644
--- a/log/log.c
+++ b/log/log.c
@@ -125,6 +125,9 @@ const char *GetModuleLevel(IN int32_t module_level)
case SSF_LIB:
return "SSF_LIB";
+ case OSA_LIB:
+ return "OSA_LIB";
+
default:
return "TA_SDK";
}
diff --git a/log/log.h b/log/log.h
index 8c31f2d..2658c0c 100644
--- a/log/log.h
+++ b/log/log.h
@@ -36,7 +36,6 @@
#define INOUT
#define OUT
-//#define _LOGGING
#ifdef _WIN
typedef int timer_t;
@@ -77,6 +76,7 @@ typedef enum {
TEE_STUB = 0x08,
TEST = 0x10,
SSF_LIB = 0x11,
+ OSA_LIB = 0x12,
ALL_MODULES = 0xFFFFFFF,
} ModuleLevel;
diff --git a/osal/CMakeLists.txt b/osal/CMakeLists.txt
index 755cd04..82f6c8d 100644
--- a/osal/CMakeLists.txt
+++ b/osal/CMakeLists.txt
@@ -26,6 +26,10 @@ SET(OSAL_SOURCES
${OSAL_PATH}/OsaTask.c
)
+INCLUDE_DIRECTORIES(
+ ${LOG_PATH}
+)
+
ADD_LIBRARY(${TARGET_TEF_SIMULATOR_OSAL} ${OSAL_SOURCES})
INSTALL(TARGETS ${TARGET_TEF_SIMULATOR_OSAL} DESTINATION ${LIB_DIR})
diff --git a/osal/OsaCommon.c b/osal/OsaCommon.c
index 3caabef..a2c652b 100644
--- a/osal/OsaCommon.c
+++ b/osal/OsaCommon.c
@@ -24,6 +24,7 @@
* Include files
*-----------------------------------------------------------------------------*/
#include "OsaLinuxUser.h"
+#include
/*-----------------------------------------------------------------------------
* Globals
@@ -148,7 +149,7 @@ int OsaTimerCreate(int *pTimerId, int periodic, int s32Time,
* Add SIGALRM in the list
*/
if (sigaddset(&(Action_t.sa_mask), SIGALRM) < 0) {
- //PrintError("In OsaTimerCreate() : Could Not Stop \n");
+ LOGE(OSA_LIB, "In OsaTimerCreate() : Could Not Stop \n");
return OSAL_ERROR;
}
@@ -156,7 +157,7 @@ int OsaTimerCreate(int *pTimerId, int periodic, int s32Time,
* Unblock the SIGALRM,if it is blocked
*/
if (sigprocmask(SIG_UNBLOCK, &(Action_t.sa_mask), NULL) < 0) {
- //PrintError("In OsaTimerCreate() : Could not mask the Signal \n");
+ LOGE(OSA_LIB, "In OsaTimerCreate() : Could not mask the Signal \n");
return OSAL_ERROR;
}
@@ -197,7 +198,7 @@ int OsaTimerStart(int iTimerId)
/* The timer is started */
if (Timer_data_t.start_timer != TRUE) {
if (setitimer(ITIMER_REAL, &Timer_data_t.iTval_t, NULL) < 0) {
- //PrintError("In OsaTimerStart() : OsaTimerStart failed \n ");
+ LOGE(OSA_LIB, "In OsaTimerStart() : OsaTimerStart failed \n ");
return OSAL_ERROR;
}
@@ -221,44 +222,19 @@ int OsaTimerStart(int iTimerId)
*/
int OsaTimerStop(int iTimerId)
{
- //struct sigaction Action_t;
- //Action_t.sa_flags = 0;
struct itimerval trivial_it; /* OSAL_080918_1 */
if (Timer_data_t.start_timer == FALSE) {
- //PrintError("In OsaTimerStop() : Timer not yet started \n");
- return OSAL_ERROR;
- }
-
- /* OSAL_080920 : Don't block the alarm signal */
-#if 0
- /* OSAL_080918_2 : init signal set variable */
- sigemptyset(&(Action_t.sa_mask));
-
- /*
- * Add SIGALRM in the signal List
- */
- if (sigaddset(&(Action_t.sa_mask), SIGALRM) < 0) {
- PrintError("In OsaTimerStop() : Could Not Stop \n");
- return OSAL_ERROR;
- }
-
- /*
- * Block SIGALRM
- */
- if (sigprocmask(SIG_BLOCK, &(Action_t.sa_mask), NULL) < 0) {
- PrintError("In OsaTimerStop() : Could not mask the Signal \n");
return OSAL_ERROR;
}
-#endif
/* OSAL_080918_1 : stop interval timer after alarm signal blocked */
trivial_it.it_value.tv_sec = 0;
trivial_it.it_value.tv_usec = 0;
if (setitimer(ITIMER_REAL, &trivial_it, NULL) == -1) {
- //PrintError("OsaTimerStop failed\n ");
+ LOGE(OSA_LIB, "OsaTimerStop failed\n ");
return OSAL_ERROR;
}
@@ -285,7 +261,7 @@ int OsaTimerDelete(int iTimerId)
//Action_t.sa_flags = 0;
if (Timer_data_t.start_timer == FALSE) {
- //PrintError("In OsaTimerDelete() : No timer present to be deleted \n");
+ LOGE(OSA_LIB, "In OsaTimerDelete() : No timer present to be deleted \n");
return OSAL_ERROR;
}
@@ -297,7 +273,7 @@ int OsaTimerDelete(int iTimerId)
/* The Timer is deleted */
if (setitimer(ITIMER_REAL, &iTmpval_t, NULL) < 0) {
- //PrintError("In OsaTimerDelete() : pOsaTimerDelete failed \n");
+ LOGE(OSA_LIB, "In OsaTimerDelete() : pOsaTimerDelete failed \n");
return -1;
}
@@ -323,14 +299,14 @@ int OsaTimerRestart(int iTimerId)
struct sigaction Action_t;
if (Timer_data_t.stop_timer == TRUE) {
- //PrintError("In OsaTimerRestart() : Has been stopped forever \n");
+ LOGE(OSA_LIB, "In OsaTimerRestart() : Has been stopped forever \n");
return OSAL_ERROR;
}
/* OSAL_080918_1 :
reset it_value to keep the first expiration after restart */
if (setitimer(ITIMER_REAL, &Timer_data_t.iTval_t, NULL) == -1) {
- //PrintError("OsaTimerRestart failed\n ");
+ LOGE(OSA_LIB, "OsaTimerRestart failed\n ");
return OSAL_ERROR;
}
@@ -341,7 +317,7 @@ int OsaTimerRestart(int iTimerId)
Action_t.sa_flags = 0;
if (sigprocmask(SIG_UNBLOCK, &(Action_t.sa_mask), NULL) < 0) {
- //PrintError("In OsaTimerRestart() : Could Not Start Again \n");
+ LOGE(OSA_LIB, "In OsaTimerRestart() : Could Not Start Again \n");
return OSAL_ERROR;
}
diff --git a/osal/OsaIpc.c b/osal/OsaIpc.c
index 07d2f89..89b5190 100644
--- a/osal/OsaIpc.c
+++ b/osal/OsaIpc.c
@@ -25,6 +25,7 @@
* Include files
*-----------------------------------------------------------------------------*/
#include "OsaLinuxUser.h"
+#include
key_t OsaGetKey(const char pcName[10])
{
@@ -36,7 +37,7 @@ key_t OsaGetKey(const char pcName[10])
memset(aName, 0x00, 10);
memcpy(aName, pcName, 9);
- uid = 1; //getuid();
+ uid = 1; // getuid was here
acc = 0;
len = strlen(aName);
@@ -103,7 +104,7 @@ int OsaShmDetach(const void *pShmAddr)
return OSAL_ERROR;
if (shmdt(pShmAddr) == -1) {
- //PrintError("Error in Detaching");
+ LOGE(OSA_LIB, "Error in Detaching");
return OSAL_ERROR;
}
@@ -147,7 +148,7 @@ static int UlOsaNamedSemCreate(const char pcName[10], int iCount,
sem = (UlOsaSem_t *)malloc(sizeof(*sem));
if (!sem) {
- //PrintError ("UlOsaSemCreate, Out of memory!\n");
+ LOGE(OSA_LIB, "UlOsaSemCreate, Out of memory!\n");
return OSAL_ERROR;
}
@@ -163,7 +164,7 @@ static int UlOsaNamedSemCreate(const char pcName[10], int iCount,
if (semctl(sem->iSemId, 0, SETVAL, semUnion) == -1) {
semctl(sem->iSemId, 0, IPC_RMID, NULL);
free(sem);
- //PrintError ("UlOsaSemCreate, semctl Failed!\n");
+ LOGE(OSA_LIB, "UlOsaSemCreate, semctl Failed!\n");
return OSAL_ERROR;
}
} else {
@@ -172,7 +173,7 @@ static int UlOsaNamedSemCreate(const char pcName[10], int iCount,
iRetVal = OSAL_EXIST;
} else {
free(sem);
- //PrintError ("UlOsaSemCreate, semget Failed!\n");
+ LOGE(OSA_LIB, "UlOsaSemCreate, semget Failed!\n");
return OSAL_ERROR;
}
}
@@ -226,7 +227,7 @@ static int UlOsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout)
ret = semop(sem->iSemId, &semBuf, 1);
} else if (iTimeout == 0) {
/* wait _inifinite_ */
- //PrintDbg ("UlOsaSemGet-infinite(%s).\n", sem->bName);
+ LOGD(OSA_LIB, "UlOsaSemGet-infinite(%s).\n", sem->bName);
semBuf.sem_num = 0;
semBuf.sem_op = -1;
semBuf.sem_flg = SEM_UNDO;
@@ -234,9 +235,9 @@ static int UlOsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout)
ret = OSAL_FAILURE_RETRY(semop(sem->iSemId, &semBuf, 1));
} else {
/* with _timeout_ */
- //PrintDbg ("UlOsaSemGet-timeout(%s).\n", sem->bName);
+ LOGD(OSA_LIB, "UlOsaSemGet-timeout(%s).\n", sem->bName);
if (iTimeout < 0) {
- //PrintError ("UlOsaSemGet-timeout: invalid arg!\n");
+ LOGE(OSA_LIB, "UlOsaSemGet-timeout: invalid arg!\n");
return OSAL_ERROR;
}
@@ -257,17 +258,17 @@ static int UlOsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout)
/* result */
if (ret == 0) {
- //PrintDbg ("UlOsaSemGet(%s) success.\n", sem->bName);
+ LOGD(OSA_LIB, "UlOsaSemGet(%s) success.\n", sem->bName);
return OSAL_OK;
} else {
if (iFlags == OSAL_SEM_NOWAIT && errno == EAGAIN) {
- //PrintError ("UlOsaSemGet-nowait: now locked, failed to get.\n");
+ LOGE(OSA_LIB, "UlOsaSemGet-nowait: now locked, failed to get.\n");
return OSAL_ERROR;
} else if (iTimeout > 0 && errno == EAGAIN) {
- //PrintError ("UlOsaSemGet-timeout(%s): time-out\n", sem->bName);
+ LOGE(OSA_LIB, "UlOsaSemGet-timeout(%s): time-out\n", sem->bName);
return OSAL_ERR_TIMEOUT;
} else {
- //PrintError ("UlOsaSemGet error, errno=%d\n", errno);
+ LOGE(OSA_LIB, "UlOsaSemGet error, errno=%d\n", errno);
return OSAL_ERROR;
}
}
@@ -281,13 +282,13 @@ static int UlOsaNamedSemRelease(void *uiSmid)
if (!sem)
return OSAL_ERROR;
- //PrintDbg ("UlOsaSemRelease(%s)\n", sem->bName);
+ LOGD(OSA_LIB, "UlOsaSemRelease(%s)\n", sem->bName);
semBuf.sem_num = 0;
semBuf.sem_op = 1;
semBuf.sem_flg = SEM_UNDO;
if (semop(sem->iSemId, &semBuf, 1) == -1) {
- //PrintError ("UlOsaSemRelease(%s) error! errno=%d.\n", sem->bName, errno);
+ LOGE(OSA_LIB, "UlOsaSemRelease(%s) error! errno=%d.\n", sem->bName, errno);
return OSAL_ERROR;
} else
return OSAL_OK;
@@ -301,11 +302,11 @@ static int UlOsaNamedSemReset(void *uiSmid)
if (!sem)
return OSAL_ERROR;
- //PrintDbg ("UlOsaSemReset(%s).\n", sem->bName);
+ LOGD(OSA_LIB, "UlOsaSemReset(%s).\n", sem->bName);
semUnion.val = sem->iCount;
if (semctl(sem->iSemId, 0, SETVAL, semUnion) == -1) {
- //PrintError ("UlOsaSemReset, semctl Failed!\n");
+ LOGE(OSA_LIB, "UlOsaSemReset, semctl Failed!\n");
return OSAL_ERROR;
}
@@ -323,10 +324,10 @@ static int UlOsaNamedSemGetval(void *uiSmid)
n = semctl(sem->iSemId, 0, GETVAL, NULL);
if (n == -1) {
- //PrintError ("UlOsaSemGetval, semctl Failed!\n");
+ LOGE(OSA_LIB, "UlOsaSemGetval, semctl Failed!\n");
return OSAL_ERROR;
} else {
- //PrintDbg ("UlOsaSemGetval(%s): now %d\n", sem->bName, n);
+ LOGD(OSA_LIB, "UlOsaSemGetval(%s): now %d\n", sem->bName, n);
return (int)n;
}
}
@@ -347,10 +348,8 @@ int OsaNamedSemRelease(void *uiSmid)
return UlOsaNamedSemRelease(uiSmid);
}
-int OsaNamedSemDelete(void
- *uiSmid) // Deleting Semaphore : Never USE!!! - junhyeong.kim, sukki.min 12.04.20
+int OsaNamedSemDelete(void *uiSmid)
{
- //return UlOsaNamedSemDelete (uiSmid);
return -1;
}
diff --git a/osal/OsaQueue.c b/osal/OsaQueue.c
index ac5567b..75d39c0 100644
--- a/osal/OsaQueue.c
+++ b/osal/OsaQueue.c
@@ -24,6 +24,7 @@
* Include files
*-----------------------------------------------------------------------------*/
#include "OsaLinuxUser.h"
+#include
/*-----------------------------------------------------------------------------
* Globals
@@ -59,7 +60,7 @@ int OsaQueueCreate(const char bName[10], unsigned int uiFlags,
mqd_t QuId;
if (puiQid == NULL) {
- PrintDbg("Null Argument(s) \n");
+ LOGD(OSA_LIB, "Null Argument(s) \n");
return OSAL_ERROR;
}
@@ -88,14 +89,14 @@ int OsaQueueCreate(const char bName[10], unsigned int uiFlags,
if (((int)*puiQid) == -1) { //IPC_CREATE
perror("In OsaQueueCreate() : msgget: msgget failed");
- //PrintError("In OsaQueueCreate() : Error no. : %d\n",errno);
+ LOGE(OSA_LIB, "In OsaQueueCreate() : Error no. : %d\n", errno);
return ((int)errno);
}
/* Get the current value from the structure for the message queue and copy it in buf_t*/
if (msgctl((int)(*puiQid), IPC_STAT, &tSetMqAttr) == -1) {
perror("In OsaQueueCreate() : msgctl: msgctl failed");
- //PrintError("In OsaQueueCreate() : Error no. : %d\n",errno);
+ LOGE(OSA_LIB, "In OsaQueueCreate() : Error no. : %d\n", errno);
return ((int)errno);
}
@@ -104,7 +105,7 @@ int OsaQueueCreate(const char bName[10], unsigned int uiFlags,
if (msgctl((int)(*puiQid), IPC_SET, &tSetMqAttr) == -1) {
perror("In OsaQueueCreate() : msgctl: msgctl failed");
- //PrintError("In OsaQueueCreate() : Error no. : %d\n",errno);
+ LOGE(OSA_LIB, "In OsaQueueCreate() : Error no. : %d\n", errno);
return ((int)errno);
}
@@ -168,7 +169,7 @@ int OsaQueueDelete(const char *bName, unsigned int uiQid)
if (msgctl((int)uiQid, IPC_RMID, NULL) == -1) {
perror("In OsaQueueDelete(): msgctl: msgctl failed");
- //PrintError("In OsaQueueDelete(): Error no. : %d\n",errno);
+ LOGE(OSA_LIB, "In OsaQueueDelete(): Error no. : %d\n", errno);
return ((int)errno);
}
@@ -204,27 +205,22 @@ int OsaQueueSend(unsigned int uiQid, unsigned int uiFlags, void *pvMsg_buf,
tMqAttr.mq_flags = O_NONBLOCK;
if ((err_no = mq_setattr((mqd_t)uiQid, &tMqAttr, (struct mq_attr *)NULL)) < 0) {
- ////PrintError("mq_setattr(): mq_setattr() Failed errno=%d\n",err_no,0,0,0,0,0); //COMMON_071024_1
+ LOGE(OSA_LIB, "mq_setattr(): mq_setattr() Failed errno=%d\n", err_no, 0, 0, 0, 0, 0);
return (OSAL_ERROR);
}
}
- if ((err_no = mq_send((mqd_t)uiQid, (const char *)pvMsg_buf, uiMsgLen,
- uiPriority)) < 0) {
- ////PrintError("mq_send():Failed errno=%d qid=%x flag=%d\n",err_no,uiQid,uiFlags,0,0,0); //COMMON_071024_1
+ if ((err_no = mq_send((mqd_t)uiQid, (const char *)pvMsg_buf, uiMsgLen, uiPriority)) < 0) {
+ LOGE(OSA_LIB, "mq_send():Failed errno=%d qid=%x flag=%d\n", err_no, uiQid, uiFlags, 0, 0, 0);
return (OSAL_ERROR);
}
#else /*SYS5 MSG QUEUE*/
- /*
- uiFlags : IPC_NOWAIT , ZERO
- pMsgLen: len - sizeof(int);
- */
osaMsgBuf_t osaMsg;
int ret;
if (uiMsgLen > MAXML) {
- ////PrintError("Message length exceeds max limit of %d\n",MAXML); //COMMON_071024_1
+ LOGE(OSA_LIB, "Message length exceeds max limit of %d\n", MAXML);
return OSAL_ERROR;
}
@@ -238,7 +234,7 @@ int OsaQueueSend(unsigned int uiQid, unsigned int uiFlags, void *pvMsg_buf,
if (ret != 0) {
//perror("In OsaQueueSend () : msgsnd failed"); //COMMON_071024_1
- ////PrintError("In OsaQueueSend() : Error no. : %d\n",errno); //COMMON_071024_1
+ LOGE(OSA_LIB, "In OsaQueueSend() : Error no. : %d\n", errno);
return ((int)errno);
}
@@ -267,12 +263,6 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void *pvMsgBuf,
{
#ifndef __NO_OS__
#ifdef POSIX_QUEUE
- /*
- uiFlags : IPC_NOWAIT , ZERO
- */
-#if 0
- struct timespec absTimeOut;
-#endif
struct mq_attr tMqAttr;
int uiMsgLen = 0;
@@ -282,18 +272,6 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void *pvMsgBuf,
else
tMqAttr.mq_flags = 0;
-#if 0
-
- if (ptTimeOut && (!(uiFlags & OSAL_Q_NOWAIT))) {
- absTimeOut.tv_sec = ptTimeOut->Sec;
- absTimeOut.tv_nsec = ptTimeOut->NanoSec;
- } else {
- absTimeOut.tv_sec = 0;
- absTimeOut.tv_nsec = 0;
- }
-
-#endif
-
if (mq_setattr((mqd_t)uiQid, &tMqAttr, (struct mq_attr *)NULL) < 0) {
perror("OsaQueueSend(): mq_setattr() Failed \n");
return ((int)errno);
@@ -308,10 +286,6 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void *pvMsgBuf,
// Update the received message length
*pMsgLen = uiMsgLen;
#else /*SYS5 MSG QUEUE*/
- /*
- uiFlags : IPC_NOWAIT, MSG_NOERROR
- buf_len : len - sizeof(int);
- */
int iRet = 0;
osaMsgBuf_t osaMsg;
@@ -329,7 +303,7 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void *pvMsgBuf,
if (errno != ENOMSG) {
perror("In OsaQueueReceive() : msgrcv failed");
- //PrintError("In OsaQueueReceive() : Msg id %d, Error no. : %d\n", uiQid, errno);
+ LOGE(OSA_LIB, "In OsaQueueReceive() : Msg id %d, Error no. : %d\n", uiQid, errno);
}
return ((int)errno);
@@ -363,7 +337,7 @@ int OsaQueueGetinfo(unsigned int uiQid, void *pvBuf)
struct mq_attr tMqAttr;
if (pvBuf == NULL) {
- //PrintError("Null Argument(s) \n");
+ LOGE(OSA_LIB, "Null Argument(s) \n");
return OSAL_ERROR;
}
@@ -387,7 +361,7 @@ int OsaQueueGetinfo(unsigned int uiQid, void *pvBuf)
if (msgctl((int)uiQid, IPC_STAT, &buf) < 0) {
perror("In OsaQueueGetinfo() : msgctl: msgctl failed");
- //PrintError("In OsaQueueGetinfo() : Error no. : %d\n",errno);
+ LOGE(OSA_LIB, "In OsaQueueGetinfo() : Error no. : %d\n", errno);
return ((int)errno);
}
@@ -426,7 +400,7 @@ int OsaQueueSetinfo(unsigned int uiQid, void *pvBuf)
struct mq_attr tMqAttr;
if (pvBuf == NULL) {
- //PrintError("Null Argument(s) \n");
+ LOGE(OSA_LIB, "Null Argument(s) \n");
return OSAL_ERROR;
}
@@ -455,7 +429,7 @@ int OsaQueueSetinfo(unsigned int uiQid, void *pvBuf)
if (msgctl((int)uiQid, IPC_SET, &buf) < 0) {
perror("In OsaQueueGetinfo() : msgctl: msgctl failed");
- //PrintError("In OsaQueueGetinfo() : Error no. : %d\n",errno);
+ LOGE(OSA_LIB, "In OsaQueueGetinfo() : Error no. : %d\n", errno);
return ((int)errno);
}
diff --git a/osal/OsaSem.c b/osal/OsaSem.c
index 710a009..82af922 100644
--- a/osal/OsaSem.c
+++ b/osal/OsaSem.c
@@ -24,6 +24,7 @@
* Include files
*-----------------------------------------------------------------------------*/
#include "OsaLinuxUser.h"
+#include
/*-----------------------------------------------------------------------------
* Globals
@@ -49,12 +50,12 @@ static int UlOsaSemCreate(const char bName[10], int iCount, int iAttribute,
sem = (UlOsaSem_t *)malloc(sizeof(*sem));
if (!sem) {
- //PrintError ("UlOsaSemCreate, Out of memory!\n");
+ LOGE(OSA_LIB, "UlOsaSemCreate, Out of memory!\n");
return OSAL_ERROR;
}
if (sem_init(&sem->sem, 1, (unsigned int)iCount) < 0) {
- //PrintError ("UlOsaSemCreate, sem_init Failed!\n");
+ LOGE(OSA_LIB, "UlOsaSemCreate, sem_init Failed!\n");
free(sem);
return OSAL_ERROR;
}
@@ -93,38 +94,19 @@ static int UlOsaSemGet(void *uiSmid, int iFlags, int iTimeout)
if (iFlags == OSAL_SEM_NOWAIT) {
/* no wait */
- //PrintDbg ("UlOsaSemGet-nowait(%s).\n", sem->bName);
+ LOGD(OSA_LIB, "UlOsaSemGet-nowait(%s).\n", sem->bName);
ret = sem_trywait(&sem->sem);
} else if (iTimeout == 0) {
/* wait _inifinite_ */
- //PrintDbg ("UlOsaSemGet-infinite(%s).\n", sem->bName);
+ LOGD(OSA_LIB, "UlOsaSemGet-infinite(%s).\n", sem->bName);
ret = OSAL_FAILURE_RETRY(sem_wait(&sem->sem));
} else {
/* with _timeout_ */
- //PrintDbg ("UlOsaSemGet-timeout(%s).\n", sem->bName);
+ LOGD(OSA_LIB, "UlOsaSemGet-timeout(%s).\n", sem->bName);
if (iTimeout < 0) {
- //PrintError ("UlOsaSemGet-timeout: invalid arg!\n");
+ LOGE(OSA_LIB, "UlOsaSemGet-timeout: invalid arg!\n");
return OSAL_ERROR;
}
-
-#if 0
- struct timeval tv;
-
- gettimeofday(&tv, NULL);
- tv.tv_sec += iTimeout / 1000000;
- tv.tv_usec += iTimeout % 1000000;
-
- if (tv.tv_usec >= 1000000) {
- tv.tv_sec += tv.tv_usec / 1000000;
- tv.tv_usec %= 1000000;
- }
-
- ts.tv_sec = tv.tv_sec;
- ts.tv_nsec = tv.tv_usec * 1000;
-
- ret = OSAL_FAILURE_RETRY(sem_timedwait(&sem->sem, &ts));
-#endif
-
do { // SoC_D00003324
ret = sem_trywait(&sem->sem);
@@ -138,18 +120,17 @@ static int UlOsaSemGet(void *uiSmid, int iFlags, int iTimeout)
/* result */
if (ret == 0) {
- //PrintDbg ("UlOsaSemGet(%s) success.\n", sem->bName);
+ LOGD(OSA_LIB, "UlOsaSemGet(%s) success.\n", sem->bName);
return OSAL_OK;
} else {
if (iFlags == OSAL_SEM_NOWAIT && errno == EAGAIN) {
- // PrintError ("UlOsaSemGet-nowait: now locked, failed to get.\n");
+ // LOGE(OSA_LIB, "UlOsaSemGet-nowait: now locked, failed to get.\n");
return OSAL_ERROR;
- } else if (iFlags == OSAL_SEM_WAIT &&
- iTimeout <= 0) {// Before : cond - (iTimeout > 0 && errno == ETIMEDOUT)
- //PrintError ("UlOsaSemGet-timeout(%s): time-out\n",sem->bName);
+ } else if (iFlags == OSAL_SEM_WAIT && iTimeout <= 0) {
+ LOGE(OSA_LIB, "UlOsaSemGet-timeout(%s): time-out\n", sem->bName);
return OSAL_ERR_TIMEOUT;
} else {
- //PrintError ("UlOsaSemGet error, errno=%d\n", errno);
+ LOGE(OSA_LIB, "UlOsaSemGet error, errno=%d\n", errno);
return OSAL_ERROR;
}
}
@@ -162,9 +143,9 @@ static int UlOsaSemRelease(void *uiSmid)
if (!sem)
return OSAL_ERROR;
- //PrintDbg ("UlOsaSemRelease(%s)\n", sem->bName);
+ LOGD(OSA_LIB, "UlOsaSemRelease(%s)\n", sem->bName);
if (sem_post(&sem->sem) != 0) {
- //PrintError ("UlOsaSemRelease(%s) error! errno=%d.\n", sem->bName, errno);
+ LOGE(OSA_LIB, "UlOsaSemRelease(%s) error! errno=%d.\n", sem->bName, errno);
return OSAL_ERROR;
} else
return OSAL_OK;
@@ -177,12 +158,12 @@ static int UlOsaSemReset(void *uiSmid)
if (!sem)
return OSAL_ERROR;
- //PrintDbg ("UlOsaSemReset(%s).\n", sem->bName);
+ LOGD(OSA_LIB, "UlOsaSemReset(%s).\n", sem->bName);
/* For threads currently blocked, the effect of destroying is not defined in POSIX.
Currently, this will not release any blocked threads. */
if (sem_destroy(&sem->sem) < 0) {
- //PrintError ("UlOsaSemReset, sem_destroy errno=%d\n", errno);
+ LOGE(OSA_LIB, "UlOsaSemReset, sem_destroy errno=%d\n", errno);
return OSAL_ERROR;
}
@@ -201,10 +182,10 @@ static int UlOsaSemGetval(void *uiSmid)
return OSAL_ERROR;
if (sem_getvalue(&sem->sem, &n) != 0) {
- //PrintError ("UlOsaSemGetval(%s), sem_getvalue errno=%d\n", sem->bName, errno);
+ LOGE(OSA_LIB, "UlOsaSemGetval(%s), sem_getvalue errno=%d\n", sem->bName, errno);
return OSAL_ERROR;
} else {
- //PrintDbg ("UlOsaSemGetval(%s): now %d\n", sem->bName, n);
+ LOGD(OSA_LIB, "UlOsaSemGetval(%s): now %d\n", sem->bName, n);
return (int)n;
}
}
@@ -324,7 +305,7 @@ int OsaMutCreate(const char bName[10], int iAttributes, void **puiMutid)
pthread_mutex_t *pmutex_t;
if (puiMutid == NULL) {
- //PrintError("In OsaMutCreate() : NULL PTR ERROR");
+ LOGE(OSA_LIB, "In OsaMutCreate() : NULL PTR ERROR");
return OSAL_ERROR;
}
@@ -353,7 +334,7 @@ int OsaMutCreate(const char bName[10], int iAttributes, void **puiMutid)
pthread_mutexattr_destroy(&attr_t);
} else {
- //PrintError("In OsaMutCreate() : No memory");
+ LOGE(OSA_LIB, "In OsaMutCreate() : No memory");
return OSAL_ERROR;
}
@@ -389,7 +370,7 @@ int OsaMutDelete(void *uiMutid)
if (iRet < 0) {
perror("In OsaMutDelete() : failed ");
- //PrintError("Error no. : %d\n",errno);
+ LOGE(OSA_LIB, "Error no. : %d\n", errno);
return ((int)errno);
}
@@ -419,7 +400,7 @@ int OsaMutRelease(void *uiMutid)
if (iRet < 0) {
perror("In OsaMutRelease() : failed ");
- //PrintError("Error no. : %d\n",errno);
+ LOGE(OSA_LIB, "Error no. : %d\n", errno);
return ((int)errno);
}
@@ -446,7 +427,7 @@ int OsaMutGet(void *uiMutid, int iFlags, int iTimeout)
if (iRet < 0) {
perror("In OsaMutGet() : failed ");
- //PrintError("Error no. : %d\n",errno);
+ LOGE(OSA_LIB, "Error no. : %d\n", errno);
return ((int)errno);
}
diff --git a/osal/OsaSignal.c b/osal/OsaSignal.c
index 14edc56..bf09d3b 100644
--- a/osal/OsaSignal.c
+++ b/osal/OsaSignal.c
@@ -24,6 +24,7 @@
* Include files
*-----------------------------------------------------------------------------*/
#include "OsaLinuxUser.h"
+#include
/*-----------------------------------------------------------------------------
* Functions
@@ -50,7 +51,7 @@ int OsaSigProcmask(int iMode, const unsigned int *puiNewmask,
if (iRet) {
perror("SigProcMask: SigProcMask Failed ");
- //PrintError("Error No. : %d\n",errno);
+ LOGE(OSA_LIB, "Error No. : %d\n", errno);
return ((int)errno);
}
@@ -77,7 +78,7 @@ int OsaSigSuspend(unsigned int *puiPending)
if (iRet) {
perror("SigSuspend: SigSuspend INTR ");
- //PrintError("Error No. : %d\n",errno);
+ LOGE(OSA_LIB, "Error No. : %d\n", errno);
return ((int)errno);
}
@@ -105,7 +106,7 @@ int OsaSigTimedwait(void)
if (iRet) {
perror("TimeWait: TimeWait INTR ");
- //PrintError("Error No. : %d\n",errno);
+ LOGE(OSA_LIB, "Error No. : %d\n", errno);
return ((int)errno);
}
@@ -133,7 +134,7 @@ int OsaSigSetmask(int iSigno)
if (sigaddset(&(Action_t.sa_mask), iSigno) < 0) {
perror("sigaddset: sigaddset Failed ");
- //PrintError("Error No. : %d\n",errno);
+ LOGE(OSA_LIB, "Error No. : %d\n", errno);
return ((int)errno);
}
diff --git a/osal/OsaTask.c b/osal/OsaTask.c
index afcc382..3c719bf 100644
--- a/osal/OsaTask.c
+++ b/osal/OsaTask.c
@@ -24,6 +24,7 @@
* Include files
*-----------------------------------------------------------------------------*/
#include "OsaLinuxUser.h"
+#include
/*-----------------------------------------------------------------------------
* MACROS
@@ -59,7 +60,7 @@ static void *_thread_start_handler(void *pArg)
if (iRet) {
perror("In OsaTaskSpawn() : prctl() Failed\n ");
- //PrintError("In OsaTaskSpawn() : prctl() error no. : %d\n", iRet);
+ LOGE(OSA_LIB, "In OsaTaskSpawn() : prctl() error no. : %d\n", iRet);
}
(*sThreadParam.pEntryFunc)(sThreadParam.pArg);
@@ -132,7 +133,7 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority,
*puiTid = NULL;
free(pThreadParam);
perror("In OsaTaskSpawn() : pthread create Failed\n ");
- //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
+ LOGE(OSA_LIB, "In OsaTaskSpawn() : error no. : %d\n", iRet);
return ((int)iRet);
}
}
@@ -144,7 +145,7 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority,
*puiTid = NULL;
free(pThreadParam);
perror("In OsaTaskSpawn() : pthread attr init Failed\n ");
- //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
+ LOGE(OSA_LIB, "In OsaTaskSpawn() : error no. : %d\n", iRet);
return ((int)iRet);
}
@@ -154,7 +155,7 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority,
*puiTid = NULL;
free(pThreadParam);
perror("In OsaTaskSpawn() : pthread attr setstacksize Failed\n ");
- //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
+ LOGE(OSA_LIB, "In OsaTaskSpawn() : error no. : %d\n", iRet);
pthread_attr_destroy(&tattr_t);
return ((int)iRet);
}
@@ -166,7 +167,7 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority,
*puiTid = NULL;
free(pThreadParam);
perror("In OsaTaskSpawn() : pthread create Failed\n ");
- //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
+ LOGE(OSA_LIB, "In OsaTaskSpawn() : error no. : %d\n", iRet);
pthread_attr_destroy(&tattr_t);
return ((int)iRet);
}
@@ -182,7 +183,7 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority,
if (iRet) {
*puiTid = NULL;
perror("In OsaTaskSpawn() : pthread setschedparam Failed\n ");
- //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
+ LOGE(OSA_LIB, "In OsaTaskSpawn() : error no. : %d\n", iRet);
pthread_kill(createThread, 0);
return ((int)iRet);
}
@@ -193,14 +194,14 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority,
if (iRet) {
*puiTid = NULL;
perror("In OsaTaskSpawn() : pthread_detach Failed\n ");
- //PrintError("In OsaTaskSpawn() : detach error no. : %d\n", iRet);
+ LOGE(OSA_LIB, "In OsaTaskSpawn() : detach error no. : %d\n", iRet);
pthread_kill(createThread, 0);
return ((int)iRet);
}
*puiTid = (void *)createThread;
- //PrintDbg("%s thread created policy: %d, priority %d\n", pName, createThreadPolicy, iPriority);
+ LOGD(OSA_LIB, "%s thread created policy: %d, priority %d\n", pName, createThreadPolicy, iPriority);
return OSAL_OK;
}
@@ -241,7 +242,7 @@ int OsaTaskDelete(void *uiTid)
if (iRet) {
perror("In OsaTaskDelete() : TaskDelete Failed ");
- //PrintError("In OsaTaskDelete() : error no. : %d\n",errno);
+ LOGE(OSA_LIB, "In OsaTaskDelete() : error no. : %d\n",errno);
return ((int)errno);
}
@@ -273,7 +274,7 @@ int OsaTaskSetPriority(unsigned int uiTid, int iNewpriority)
if (iRet) {
perror("In OsaTaskSetPriority() : TaskSetPriority set Failed ");
- //PrintError("In OsaTaskSetPriority() : error no. : %d\n",errno);
+ LOGE(OSA_LIB, "In OsaTaskSetPriority() : error no. : %d\n",errno);
return ((int)errno);
}
@@ -303,7 +304,7 @@ int OsaTaskGetPriority(unsigned int uiTid, int *piPriority)
if (iRet) {
piPriority = NULL;
perror("In OsaTaskGetPriority() : TaskGetPriority Failed ");
- //PrintError("In OsaTaskGetPriority() : error no. : %d\n",errno);
+ LOGE(OSA_LIB, "In OsaTaskGetPriority() : error no. : %d\n",errno);
return ((int)errno);
}
@@ -341,7 +342,7 @@ int OsaTaskNanosleep(int iNanosec)
if (iRetval) {
perror("TaskNanoSleep: TaskNanoSleep Failed ");
- //PrintError("Error No. : %d\n",errno);
+ LOGE(OSA_LIB, "Error No. : %d\n",errno);
return ((int)errno);
}
@@ -395,7 +396,7 @@ int OsaTaskDelaymsecs(unsigned int uiMsec)
if (iRetval) {
perror("In OsaTaskDelaymsecs() : TaskNanoSleep Failed ");
- //PrintError("In OsaTaskDelaymsecs() : error no. : %d\n",errno);
+ LOGE(OSA_LIB, "In OsaTaskDelaymsecs() : error no. : %d\n",errno);
return ((int)errno);
}
diff --git a/simulatordaemon/inc/SecurityContext.h b/simulatordaemon/inc/SecurityContext.h
index 6dddfef..2008a57 100644
--- a/simulatordaemon/inc/SecurityContext.h
+++ b/simulatordaemon/inc/SecurityContext.h
@@ -33,7 +33,7 @@
class SecurityContext {
private:
- int connFd;
+ int m_connFd;
static constexpr const size_t CYNARA_CACHE_SIZE = 100U;
diff --git a/simulatordaemon/src/ClientCommands/MakeCommand.cpp b/simulatordaemon/src/ClientCommands/MakeCommand.cpp
index 6ea78cb..a41f178 100644
--- a/simulatordaemon/src/ClientCommands/MakeCommand.cpp
+++ b/simulatordaemon/src/ClientCommands/MakeCommand.cpp
@@ -179,10 +179,6 @@ uint32_t MakeCommand::getDataSize(TEE_CMD command) {
size = sizeof(IntTACloseSessionData);
LOGD(SIM_DAEMON, "[TEEC] IntTACloseSessionData Size: %d", size);
break;
- case CHECK_MEMORY:
- // size = sizeof(CheckMemoryData);
- LOGD(SIM_DAEMON, "[TEEC] CheckMemoryData Size: %d", size);
- break;
case PANIC:
size = sizeof(IntTAPanicData);
LOGD(SIM_DAEMON, "[TEEC] PanicData Size: %d", size);
diff --git a/simulatordaemon/src/SecurityContext.cpp b/simulatordaemon/src/SecurityContext.cpp
index 109170f..70b867f 100644
--- a/simulatordaemon/src/SecurityContext.cpp
+++ b/simulatordaemon/src/SecurityContext.cpp
@@ -51,7 +51,7 @@ bool SecurityContext::clientHasCynaraPermission(const std::string &privelege) {
pthread_mutex_lock(&cynara_mutex);
char *label = nullptr;
- ret = cynara_creds_socket_get_client(connFd, CLIENT_METHOD_SMACK, &label);
+ ret = cynara_creds_socket_get_client(m_connFd, CLIENT_METHOD_SMACK, &label);
if (ret != CYNARA_API_SUCCESS) {
LOGE(SIM_DAEMON, "Couldn't get smack label of the client. Error code: %d", ret);
RETURN_UNLOCK(false, cynara_mutex);
@@ -59,7 +59,7 @@ bool SecurityContext::clientHasCynaraPermission(const std::string &privelege) {
p_char p_label(label, &free);
pid_t ca_pid = -1;
- ret = cynara_creds_socket_get_pid(connFd, &ca_pid);
+ ret = cynara_creds_socket_get_pid(m_connFd, &ca_pid);
if (ret != CYNARA_API_SUCCESS) {
LOGE(SIM_DAEMON, "Couldn't get pid of the client. Error code: %d", ret);
RETURN_UNLOCK(false, cynara_mutex);
@@ -74,7 +74,7 @@ bool SecurityContext::clientHasCynaraPermission(const std::string &privelege) {
p_char p_session(session, &free);
char *user = nullptr;
- ret = cynara_creds_socket_get_user(connFd, USER_METHOD_DEFAULT, &user);
+ ret = cynara_creds_socket_get_user(m_connFd, USER_METHOD_DEFAULT, &user);
if (ret != CYNARA_API_SUCCESS) {
LOGE(SIM_DAEMON, "Couldn't get user. Error code: %d", ret);
RETURN_UNLOCK(false, cynara_mutex);
@@ -131,7 +131,7 @@ SecurityContext::SecurityContext():
SecurityContext::SecurityContext(int connFd):
- connFd(connFd) {
+ m_connFd(connFd) {
if (_cynara == nullptr) throw std::runtime_error("Cynara is not initialized");
}
diff --git a/simulatordaemon/src/TABinaryManager/TAManifest.cpp b/simulatordaemon/src/TABinaryManager/TAManifest.cpp
index 90eb3bb..1f9c397 100644
--- a/simulatordaemon/src/TABinaryManager/TAManifest.cpp
+++ b/simulatordaemon/src/TABinaryManager/TAManifest.cpp
@@ -86,13 +86,8 @@ bool TAManifest::processXML(const string &xmlManifestPath) {
properties.extension.appName = string(propertiesExtension->first_attribute("appName")->value());
properties.extension.appVersion = string(propertiesExtension->first_attribute("appVersion")->value());
- /*properties.extension.type = string(propertiesExtension->first_attribute("type")->value());
- properties.extension.zone = string(propertiesExtension->first_attribute("zone")->value());*/
properties.extension.sdkVersion = string(propertiesExtension->first_attribute("sdkVersion")->value());
- // Removed, taEncrypion flag used now
- //properties.extension.secret = string(propertiesExtension->first_attribute("secret")->value());
-
properties.extension.launchMode = string(propertiesExtension->first_attribute("launchMode")->value());
}
}
diff --git a/simulatordaemon/src/TABinaryManager/TAUnpack.cpp b/simulatordaemon/src/TABinaryManager/TAUnpack.cpp
index e2aa854..01a4a32 100644
--- a/simulatordaemon/src/TABinaryManager/TAUnpack.cpp
+++ b/simulatordaemon/src/TABinaryManager/TAUnpack.cpp
@@ -86,7 +86,6 @@ int TAUnpack::unpackTA(string path, string uuid) {
LOGE(SIM_DAEMON, "Read failed");
return -1;
}
- // fixHeaderEndianness(&packageHeader);
// 2. Verify header
if (SECURITY_HEADER_MAGIC1 == packageHeader.magic1 &&
SECURITY_HEADER_MAGIC2 == packageHeader.magic2) {
@@ -120,7 +119,6 @@ int TAUnpack::unpackTA(string path, string uuid) {
LOGE(SIM_DAEMON, "Read failed");
return -1;
}
- //manifest.write(manifestdump, sizeWithoutPadding(manifestdump, packageHeader.manifest_size));
manifest.write(manifestdump, packageHeader.manifest_size);
manifest.flush();
delete[] manifestdump;
diff --git a/ssflib/dep/cryptocore/source/base/cc_bignum.c b/ssflib/dep/cryptocore/source/base/cc_bignum.c
index 2119c79..d6c96d7 100644
--- a/ssflib/dep/cryptocore/source/base/cc_bignum.c
+++ b/ssflib/dep/cryptocore/source/base/cc_bignum.c
@@ -424,18 +424,7 @@ static cc_u32 SDRM_DWD_MulAdd(cc_u32 *pdDest, cc_u32 dDstLen, cc_u32 *pdSrc,
pdDest[i] = (cc_u32)pdDigit2;
dTemp = pdDigit2 >> 32;
- /*SDRM_DIGIT_Mul(pdDigit, dMultiplier, pdSrc[i]);
- if ((dTemp += pdDigit[0]) < pdDigit[0])
- {
- pdDigit[1]++;
- }
-
- if ((pdDest[i] += dTemp) < dTemp)
- {
- pdDigit[1]++;
- }
- dTemp = pdDigit[1];*/
}
if (i == dDstLen)
@@ -536,18 +525,7 @@ static void SDRM_DWD_Mul(cc_u32 *pdDest, cc_u32 *pdSrc1, cc_u32 dSrcLen1,
dTemp = pdDigit2 >> 32;
- /*SDRM_DIGIT_Mul(pdDigit, pdSrc1[i], pdSrc2[j]);
- if ((dTemp += pdDigit[0]) < pdDigit[0])
- {
- pdDigit[1]++;
- }
- if ((pdDest[i + j] += dTemp) < dTemp)
- {
- pdDigit[1]++;
- }
-
- dTemp = pdDigit[1];*/
}
pdDest[i + j] = dTemp;
@@ -1436,8 +1414,6 @@ int SDRM_BN_Div(SDRM_BIG_NUM *BN_Quotient, SDRM_BIG_NUM *BN_Remainder,
if (SDRM_BN_Cmp(temp_Dividend, temp_Divisor) < 0) {
if (BN_Remainder != NULL) {
SDRM_BN_Copy(BN_Remainder, temp_Dividend);
- //free(pbBuf);
- //return CRYPTO_SUCCESS; modify by Chalyi Aleksandr: it is not correct
}
if (BN_Quotient != NULL)
@@ -1911,10 +1887,8 @@ int SDRM_MONT_Rzn2zn(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1,
SDRM_BN_OPTIMIZE_LENGTH(Src1);
SDRM_BN_SHR(BN_Dst, Src1, (Mod_Len) * 32);
- //BN_Dst->Length = Src1->Length - ri;
- BN_Dst->Length = Src1->Length - ri - 1; //Added by yhhwang
+ BN_Dst->Length = Src1->Length - ri - 1;//Added by yhhwang
- //if (SDRM_BN_Cmp(BN_Dst, Mont->Mod) >= 0)
while (SDRM_BN_Cmp(BN_Dst, Mont->Mod) >= 0)
SDRM_BN_Sub(BN_Dst, BN_Dst, Mont->Mod);
@@ -1939,45 +1913,6 @@ int SDRM_MONT_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1,
{
int ret;
- /* Begin - Add to test input range by Yong Ho Hwang (20120809) */
- /*
- if (SDRM_BN_Cmp(BN_Src1, Mont->Mod) >= 0)
- {
- ret = SDRM_BN_ModRed(BN_Src1, BN_Src1, Mont->Mod);
- if (ret != CRYPTO_SUCCESS)
- {
- return ret;
- }
- } else if ( BN_Src1->sign == 1)
- {
- printf("Minus Value\n");
- ret = SDRM_BN_Add(BN_Src1, BN_Src1, Mont->Mod);
- if (BN_Src1->sign == 1)
- {
- printf("Value Fail.\n");
- return CRYPTO_ERROR;
- }
- }
-
- if (SDRM_BN_Cmp(BN_Src2, Mont->Mod) >= 0)
- {
- ret = SDRM_BN_ModRed(BN_Src2, BN_Src2, Mont->Mod);
- if (ret != CRYPTO_SUCCESS)
- {
- return ret;
- }
- } else if ( BN_Src2->sign == 1)
- {
- printf("Minus Value\n");
- ret = SDRM_BN_Add(BN_Src2, BN_Src2, Mont->Mod);
- if (BN_Src2->sign == 1)
- {
- printf("Value Fail.\n");
- return CRYPTO_ERROR;
- }
- }
- */
- /* End - Add to test input range by Yong Ho Hwang (20120809) */
ret = SDRM_BN_Mul(BN_Dst, BN_Src1, BN_Src2);
@@ -1986,17 +1921,6 @@ int SDRM_MONT_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1,
ret = SDRM_MONT_Rzn2zn(BN_Dst, BN_Dst, Mont);
- /* Begin - Add to test input range by Yong Ho Hwang (20120809) */
- /*
- if (SDRM_BN_Cmp(BN_Dst, Mont->Mod) >= 0)
- {
- printf("Output is bigger than Mod\n");
- } else if ( BN_Dst->sign == 1)
- {
- printf("Minus Value\n");
- }
- */
- /* End - Add to test input range by Yong Ho Hwang (20120809) */
return ret;
}
@@ -2049,39 +1973,7 @@ int SDRM_MONT_Set(SDRM_BIG_MONT *Mont, SDRM_BIG_NUM *BN_Modulus)
R = SDRM_BN_Alloc((cc_u8 *)Ri + dAllocSize, dSize);
temp = SDRM_BN_Alloc((cc_u8 *)R + dAllocSize, dSize);
- //++ 2012.08.20 - modified by yhhwang to apply R=2^(160+32)
- /* == DELETED ==
- SDRM_BN_Copy(Mont->Mod, BN_Modulus);
-
- Mont->ri = (SDRM_BN_num_bits(BN_Modulus) + (SDRM_BitsInDWORD - 1)) / SDRM_BitsInDWORD * SDRM_BitsInDWORD;
-
- SDRM_BN_SHL(R, BN_One, SDRM_BitsInDWORD);
-
- buf[0] = BN_Modulus->pData[0];
- buf[1] = 0;
- temp->pData[0] = buf[0];
- temp->Length = 1;
- temp->sign = BN_Modulus->sign;
-
- SDRM_BN_ModInv(Ri, R, temp);
- if (Ri == NULL)
- {
- free(pbBuf);
-
- return CRYPTO_INVERSE_NOT_EXIST;
- }
-
- SDRM_BN_SHL(Ri, Ri, SDRM_BitsInDWORD);
- SDRM_BN_Sub(Ri, Ri, BN_One);
- SDRM_BN_Div(Ri, NULL, Ri, temp);
- SDRM_BN_Copy(Mont->Inv_Mod, Ri);
- Mont->N0 = Ri->pData[0];
-
- SDRM_BN_SHL(Mont->R, BN_One, 2 * (32 + Mont->ri));
- SDRM_BN_ModRed(Mont->R, Mont->R, Mont->Mod);
- */
- // == NEW CODE ==
SDRM_BN_Copy(Mont->Mod, BN_Modulus);
Mont->Mod->pData[Mont->Mod->Length] = 0;
@@ -2113,7 +2005,6 @@ int SDRM_MONT_Set(SDRM_BIG_MONT *Mont, SDRM_BIG_NUM *BN_Modulus)
// Compute R and R^2 mod M
SDRM_BN_SHL(Rsquare, BN_One, r2Size);
SDRM_BN_ModRed(Mont->R, Rsquare, BN_Modulus);
- //-- 2012.08.20 - modified by yhhwang
free(pbBuf);
free(Rsquare);
@@ -2595,16 +2486,9 @@ int SDRM_HEX2BN(cc_u8 *pbSrc, SDRM_BIG_NUM *BN_Dst)
//normalize length
if (n % SDRM_SIZE_BLOCK != 0)
- BN_Dst->Length += 1;
-
-#if 0 //fix prevent problem by guoxing.xu 20140826. move to before
-
- if (!BN_Dst)
- BN_Dst = SDRM_BN_Init(BN_Dst->Length * SDRM_SIZE_OF_DWORD * 8);
-
-#endif
+ BN_Dst->Length += 1;
- for (i = 0; i < BN_Dst->Length ; i++)
+ for (i = 0; i < BN_Dst->Length; i++)
BN_Dst->pData[i] = 0;
//full string: bufferHex mod Length = 0
@@ -2800,10 +2684,8 @@ cc_u8 *SDRM_BN2STRFOUR(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src)
while (!SDRM_BN_isZero(num)) {
SDRM_BN_Div(num, tempREM, num, d);
- //itoa(tempREM->pData[0], (char *)tempChar, 10);
- //sprintf((char*)tempChar, "%d", tempREM->pData[0]);
snprintf((char *)tempChar, sizeof(tempChar), "%d",
- tempREM->pData[0]); // fix prevnet 60199 by guoxing.xu
+ tempREM->pData[0]);
strDestTemp[(*numberBits)] = tempChar[0];
(*numberBits)++;
@@ -2864,14 +2746,11 @@ SDRM_BIG_NUM **SDRM_BN_MassInit(cc_u32 dBufSize, cc_u32 count)
ptr = (cc_u8 *)BN_Buf + sizeof(SDRM_BIG_NUM *) * count;
for (i = 0; i < count; i++) {
- //add by guoxing.xu to avoid warning. 2/15/2014
tmp = ptr;
BN_Buf[i] = (SDRM_BIG_NUM *)tmp;
- //BN_Buf[i] = (SDRM_BIG_NUM*)ptr;
BN_Buf[i]->Size = dBufSize;
tmp = (ptr + sizeof(SDRM_BIG_NUM));
BN_Buf[i]->pData = (cc_u32 *)tmp;
- //BN_Buf[i]->pData = (cc_u32*)(ptr + sizeof(SDRM_BIG_NUM));
ptr += bnsiz;
}
diff --git a/ssflib/dep/cryptocore/source/base/cc_ecc.c b/ssflib/dep/cryptocore/source/base/cc_ecc.c
index 9908edd..8d52715 100644
--- a/ssflib/dep/cryptocore/source/base/cc_ecc.c
+++ b/ssflib/dep/cryptocore/source/base/cc_ecc.c
@@ -1040,8 +1040,6 @@ int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1,
// Precomputation data
for (i = 0; i < 2; i++) {
- // Pw[i] = (SDRM_EC_POINT **)malloc(sizeof(SDRM_EC_POINT *) * w2);
- // if (!Pw[i]) return CRYPTO_MEMORY_ALLOC_FAIL;
for (j = 0; j < 9; j++)
Pw[i][j] = SDRM_ECC_Init();
}
diff --git a/ssflib/dep/cryptocore/source/base/cc_md5.c b/ssflib/dep/cryptocore/source/base/cc_md5.c
index cbb667f..6cef2f0 100644
--- a/ssflib/dep/cryptocore/source/base/cc_md5.c
+++ b/ssflib/dep/cryptocore/source/base/cc_md5.c
@@ -36,13 +36,6 @@ static void SDRM_Decode(cc_u32 *, const unsigned char *, cc_u32);
static unsigned char PADDING[64] = {0x80, 0,};
-/* F, G, H and I are basic MD5 functions.
- */
-//#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
-//#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
-//#define H(x, y, z) ((x) ^ (y) ^ (z))
-//#define I(x, y, z) ((y) ^ ((x) | (~z)))
-
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
#define G(x, y, z) F(z, x, y)
#define H(x, y, z) ((x) ^ (y) ^ (z))
diff --git a/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c b/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c
index 74f7eb5..dfd2171 100644
--- a/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c
+++ b/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c
@@ -755,7 +755,6 @@ int SDRM_Enpad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen,
EM[emLen - sLen - hLen - 2] ^= 0x01;
- //memset(EM, 0x00, emLen - sLen - hLen - 2);
for (i = 0; i < sLen; i++)
EM[emLen - sLen - hLen - 1 + i] ^= salt[i];
diff --git a/ssflib/dep/cryptocore/source/base/cc_rc4.c b/ssflib/dep/cryptocore/source/base/cc_rc4.c
index ec769e6..245f2e6 100644
--- a/ssflib/dep/cryptocore/source/base/cc_rc4.c
+++ b/ssflib/dep/cryptocore/source/base/cc_rc4.c
@@ -87,10 +87,8 @@ int SDRM_RC4_Setup(SDRM_RC4Context *ctx, cc_u8 *UserKey, cc_u32 keyLen)
i = 0xff;
if (((cc_u8 *)&i)[0] == 0xff) {
- // LOG4DRM_INFO(&CryptoLogCTX), "is Little Endian machine\n");
memcpy(ctx->s, RC4_S_VALUE_LITTLE, 256);
} else {
- // LOG4DRM_INFO(&CryptoLogCTX), "is Big Endian machine\n");
memcpy(ctx->s, RC4_S_VALUE_BIG, 256);
}
diff --git a/ssflib/dep/cryptocore/source/base/cc_sha1.c b/ssflib/dep/cryptocore/source/base/cc_sha1.c
index 8f3de28..c64dcbc 100644
--- a/ssflib/dep/cryptocore/source/base/cc_sha1.c
+++ b/ssflib/dep/cryptocore/source/base/cc_sha1.c
@@ -75,12 +75,10 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input,
save one boolean operation each - thanks to Rich Schroeppel,
rcs@cs.arizona.edu for discovering this */
-/*#define SDRM_SHA1_f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */
-#define SDRM_SHA1_f1(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) /* Rounds 0-19 */
-#define SDRM_SHA1_f2(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 20-39 */
-/*#define SDRM_SHA1_f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */
-#define SDRM_SHA1_f3(x, y, z) (((x) & (y)) | ((z) & ((x) | (y)))) /* Rounds 40-59 */
-#define SDRM_SHA1_f4(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 60-79 */
+#define SDRM_SHA1_f1(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) /* Rounds 0-19 */
+#define SDRM_SHA1_f2(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 20-39 */
+#define SDRM_SHA1_f3(x, y, z) (((x) & (y)) | ((z) & ((x) | (y)))) /* Rounds 40-59 */
+#define SDRM_SHA1_f4(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 60-79 */
/* The SHS Mysterious Constants */
@@ -464,77 +462,6 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input,
}
-//unsigned char digest[20];
-//unsigned char message[3] = {'a', 'b', 'c' };
-//unsigned char *mess56 =
-// "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
-
-/* Correct solutions from FIPS PUB 180-1 */
-//char *dig1 = "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D";
-//char *dig2 = "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1";
-//char *dig3 = "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F";
-
-/* Output should look like:-
- a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
- A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D <= correct
- 84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1
- 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 <= correct
- 34aa973c d4c4daa4 f61eeb2b dbad2731 6534016f
- 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F <= correct
-*/
-
-//main()
-//{
-// SHA_CTX sha;
-// int i;
-// BYTE big[1000];
-//
-// SHAInit(&sha);
-// SHAUpdate(&sha, message, 3);
-// SHAFinal(digest, &sha);
-//
-// for (i = 0; i < 20; i++)
-// {
-// if ((i % 4) == 0) printf(" ");
-// printf("%02x", digest[i]);
-// }
-// printf("\n");
-// printf(" %s <= correct\n", dig1);
-//
-// SHAInit(&sha);
-// SHAUpdate(&sha, mess56, 56);
-// SHAFinal(digest, &sha);
-//
-// for (i = 0; i < 20; i++)
-// {
-// if ((i % 4) == 0) printf(" ");
-// printf("%02x", digest[i]);
-// }
-// printf("\n");
-// printf(" %s <= correct\n", dig2);
-//
-// /* Fill up big array */
-// for (i = 0; i < 1000; i++)
-// big[i] = 'a';
-//
-// SHAInit(&sha);
-// /* Digest 1 million x 'a' */
-// for (i = 0; i < 1000; i++)
-// SHAUpdate(&sha, big, 1000);
-// SHAFinal(digest, &sha);
-//
-// for (i = 0; i < 20; i++)
-// {
-// if ((i % 4) == 0) printf(" ");
-// printf("%02x", digest[i]);
-// }
-// printf("\n");
-// printf(" %s <= correct\n", dig3);
-//
-// return 0;
-//}
-
-/* endian.c */
void SDRM_endianTest(int *endian_ness)
{
diff --git a/ssflib/dep/cryptocore/source/middle/cc_cmac.c b/ssflib/dep/cryptocore/source/middle/cc_cmac.c
index 1e33d93..0394bf6 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_cmac.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_cmac.c
@@ -107,8 +107,7 @@ int SDRM_CMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen)
BlockXor(K2, K2, R_b);
}
- // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K1", K1, 16);
- // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K2", K2, 16);
+
return CRYPTO_SUCCESS;
}
@@ -161,7 +160,7 @@ int SDRM_CMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen)
ptr += SDRM_AES_BLOCK_SIZ;
}
- // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "Block", crt->ctx->cmacctx->IV, 16);
+
memcpy(crt->ctx->cmacctx->Block, ptr, crt->ctx->cmacctx->BlockLen);
diff --git a/ssflib/dep/cryptocore/source/middle/cc_dsa.c b/ssflib/dep/cryptocore/source/middle/cc_dsa.c
index da53040..4ab673b 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_dsa.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_dsa.c
@@ -570,8 +570,7 @@ int SDRM_DSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen,
SDRM_BN_ModRed(v, temp3,
crt->ctx->dsactx->q); //v = (alpha^u1 x y^u2 mod p) mod q
- // SDRM_PrintBN("v : ", v);
- // SDRM_PrintBN("Hash : ", BNH_m);
+
if (SDRM_BN_Cmp(v, BN_r) == 0)
*result = CRYPTO_VALID_SIGN;
diff --git a/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c b/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c
index 1725ead..141797f 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c
@@ -180,7 +180,6 @@ int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash,
return res;
}
- //SDRM_PrintBN("kP->x", kP->x);
SDRM_BN_ModRed(BN_r, kP->x, ctx->ECC_n);
if (BN_r->Length > 0) // r = 0 �̸� k �ٽ� ����
@@ -190,9 +189,6 @@ int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash,
// 3. k^{-1} mod n ���.
SDRM_BN_ModInv(BN_Tmp1, BN_k, ctx->ECC_n);
- //SDRM_PrintBN("BN_k", BN_k);
- //SDRM_PrintBN("ctx->ECC_n", ctx->ECC_n);
- //SDRM_PrintBN("BN_Tmp1 = k^{-1} mod n", BN_Tmp1);
// 4. s = k^{-1}(hash + dr) mod n ��� (d = private key). s = 0 �̸� 1������.
// BN_Tmp2 = dr
@@ -206,9 +202,7 @@ int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash,
break;
}
- // (r, s) �������� ���.
- //SDRM_PrintBN("BN_r", BN_r);
- //SDRM_PrintBN("BN_s", BN_s);
+// (r, s) �������� ���.
SDRM_BN2OS(BN_r, ctx->uDimension / 8, sig);
SDRM_BN2OS(BN_s, ctx->uDimension / 8, sig + ctx->uDimension / 8);
@@ -286,8 +280,6 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen,
SDRM_OS2BN(sig, ctx->uDimension / 8, pBN_r);
SDRM_OS2BN(sig + ctx->uDimension / 8, ctx->uDimension / 8, pBN_s);
- //SDRM_PrintBN("BN_r", pBN_r);
- //SDRM_PrintBN("BN_s", pBN_s);
// 1. r�� s�� ���� ����
SDRM_BN_Sub(BN_tmp, ctx->ECC_n, BN_One);
@@ -311,7 +303,6 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen,
// 2. w = s^(-1) mod n, BN_hash ���
SDRM_OS2BN(hash, hashLen, BN_hash);
res = SDRM_BN_ModInv(BN_w, pBN_s, ctx->ECC_n);
- //SDRM_PrintBN("BN_w", BN_w);
if (res != CRYPTO_SUCCESS) {
free(pbBuf);
@@ -324,8 +315,7 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen,
// 3. u1 = BN_hash *w mod n, u2 = rw mod n
SDRM_BN_ModMul(BN_u1, BN_hash, BN_w, ctx->ECC_n);
SDRM_BN_ModMul(BN_u2, pBN_r, BN_w, ctx->ECC_n);
- //SDRM_PrintBN("BN_u1", BN_u1);
- //SDRM_PrintBN("BN_u2", BN_u2);
+
// 4. (x0, y0) = u1P + u2Q, V = x0 mod n
res = SDRM_CTX_EC_2kP(ctx, EC_temp1, BN_u1, ctx->ECC_G, BN_u2, ctx->PUBLIC_KEY);
@@ -345,12 +335,10 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen,
return res;
}
- // SDRM_PrintBN("EC_temp1->x", EC_temp1->x);
- // SDRM_PrintBN("ctx->ECC_n", ctx->ECC_n);
+
SDRM_BN_ModRed(BN_tmp, EC_temp1->x, ctx->ECC_n);
- // SDRM_PrintBN("BN_tmp", BN_tmp);
- // SDRM_PrintBN("pBN_r", pBN_r);
+
// 5. V = r�� ��� ���� ok
res = SDRM_BN_Cmp_sign(BN_tmp, pBN_r);
diff --git a/ssflib/dep/cryptocore/source/middle/cc_rsa.c b/ssflib/dep/cryptocore/source/middle/cc_rsa.c
index 8aad8ad..9f24538 100644
--- a/ssflib/dep/cryptocore/source/middle/cc_rsa.c
+++ b/ssflib/dep/cryptocore/source/middle/cc_rsa.c
@@ -311,8 +311,6 @@ GEN_RND:
q->pData[q->Length - 1] |= (1L << t1);
} while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME);
- // SDRM_PrintBN("p", p);
- // SDRM_PrintBN("q", q);
//temp1 = (p - 1), temp2 = (q - 1)
SDRM_BN_Sub(temp1, p, BN_One);
@@ -878,8 +876,6 @@ GEN_RND:
q->pData[q->Length - 1] |= (1L << t1);
} while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME);
- // SDRM_PrintBN("p", p);
- // SDRM_PrintBN("q", q);
//temp1 = (p - 1), temp2 = (q - 1)
@@ -1037,7 +1033,6 @@ int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen,
return CRYPTO_INVALID_ARGUMENT;
}
- // SDRM_PrintBN("ENPADDED Text : ", BN_pMsg);
if (retVal != CRYPTO_SUCCESS) {
free(pbBuf);
@@ -1293,7 +1288,6 @@ int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen,
return CRYPTO_INVALID_ARGUMENT;
}
- // SDRM_PrintBN("OAEP Text : ", BN_dMsg);
SDRM_I2OSP(BN_dMsg, RSA_KeyByteLen, pbBuf);
@@ -1377,24 +1371,17 @@ int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen,
//Msg Padding
switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) {
case ID_RSASSA_PKCS15:
- retVal = SDRM_Enpad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen,
- SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
+ retVal = SDRM_Enpad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
break;
-
case ID_RSASSA_PSS:
SDRM_BN_GETBITLEN(crt->ctx->rsactx->n, nBits);
- retVal = SDRM_Enpad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen,
- SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
+ retVal = SDRM_Enpad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm));
break;
-
case ID_NO_PADDING:
memset(pbBuf, 0x00, RSA_KeyByteLen - hashLen);
- //memcpy(pbBuf + hashLen, hash, RSA_KeyByteLen);
- memcpy(pbBuf + RSA_KeyByteLen - hashLen, hash,
- hashLen);// fixed by guoxing.xu 20140919
+ memcpy(pbBuf + RSA_KeyByteLen - hashLen, hash, hashLen);// fixed by guoxing.xu 20140919
retVal = CRYPTO_SUCCESS;
break;
-
default:
free(pbBuf);
return CRYPTO_INVALID_ARGUMENT;
@@ -1405,7 +1392,6 @@ int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen,
return retVal;
}
- // SDRM_PrintBN("ENPADDED Msg : ", BN_pMsg);
SDRM_OS2BN(pbBuf, RSA_KeyByteLen, BN_pMsg);
@@ -1478,7 +1464,6 @@ int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen,
SDRM_RSA_BN_BUFSIZE);
SDRM_OS2BN(signature, signLen, BN_Sign);
- // SDRM_PrintBN("Generated Sign : ", BN_Sign);
//RSA Verification by modular exponent
#ifndef _OP64_NOTSUPPORTED
diff --git a/ssflib/dep/swdss/source/file_op.cpp b/ssflib/dep/swdss/source/file_op.cpp
index c731bd0..9e1350b 100644
--- a/ssflib/dep/swdss/source/file_op.cpp
+++ b/ssflib/dep/swdss/source/file_op.cpp
@@ -74,8 +74,6 @@ int file_op::write_file(const char* filename, unsigned char* buffer,
fflush(file);
- //sync(fileno(file)); // sync blocked
-
fclose(file);
return SS_RET_SUCCESS;
@@ -198,7 +196,6 @@ int file_op::create_folder(const char* folder) {
result = SS_RET_FAIL;
SLOGE("Failed to create folder %s.", folder);
}
- //sync();
} else if (!(S_ISDIR(st.st_mode))) {
result = SS_RET_FAIL;
}
diff --git a/ssflib/dep/swdss/source/secure_file.cpp b/ssflib/dep/swdss/source/secure_file.cpp
index 9a828f1..8a0700a 100644
--- a/ssflib/dep/swdss/source/secure_file.cpp
+++ b/ssflib/dep/swdss/source/secure_file.cpp
@@ -24,7 +24,6 @@
#ifdef _SECOS_SIM_
#include "file_op.h"
-//#define SWD_SS_ROOT "/opt/usr/apps/tz_simulator/data/swdss/"
#define SWD_SS_ROOT "/tmp/tastore2/"
#endif
@@ -320,12 +319,6 @@ int is_valid_credential(const ss_credential_s& cred) {
return SS_RET_INVALID_CREDENTIAL;
}
- // checking specific values
- //if (tmp_uuid[14] != '4' || (tmp_uuid[19] != '8' && tmp_uuid[19] != '9' && tmp_uuid[19] != 'a' && tmp_uuid[19] != 'b'))
- //{
- // return SS_RET_INVALID_CREDENTIAL;
- //}
- // checking that string contains only hexidecimal values
if (-1 == is_hex(tmp_uuid, '-')) {
return SS_RET_INVALID_CREDENTIAL;
}
@@ -488,9 +481,16 @@ int secure_file::derive_file_path() {
return SS_RET_SUCCESS;
}
+ int remaining = SS_FULL_DATA_NAME_LEN;
+
memcpy(m_full_path, m_cred.uuid, SS_MAX_UUID_LEN);
- strcat(m_full_path, "/");
- strcat(m_full_path, m_data_name);
+ m_full_path[SS_MAX_UUID_LEN] = '\0';
+ remaining -= SS_MAX_UUID_LEN;
+
+ strncat(m_full_path, "/", 1);
+ remaining -= 1;
+
+ strncat(m_full_path, m_data_name, remaining - 1);
m_file_path_ready = true;
return SS_RET_SUCCESS;
#else
@@ -520,10 +520,8 @@ int secure_file::derive_file_path() {
// obtain first part of our string
byte_to_hex(pFolderName, pHash, CCryptoEngine::Hash_Size);
memcpy(sTemp, pFolderName, 2 * CCryptoEngine::Hash_Size);
- //sTemp.assign((char*)pFolderName, 2*CCryptoEngine::Hash_Size);
// concatenate with UUID and Name
- //sTemp += sUUID_and_Name;
memcpy(sTemp + 2 * CCryptoEngine::Hash_Size,
sUUID_and_Name,
strlen(sUUID_and_Name));
@@ -532,14 +530,6 @@ int secure_file::derive_file_path() {
CCryptoEngine::Hash(pHash, (CBT_OCTET*)sTemp, strlen(sTemp));
// we will use first 4 bytes of hash value
- // convert them into hex format
- //byte_to_hex(pFolderName, pHash, 4);
-
- // set folder name
- //sfolder.assign((char*)pFolderName, 8);
- //sfolder += "/";
-
- //m_full_path = sfolder;
memset(m_full_path, 0, SS_FULL_DATA_NAME_LEN);
memcpy(m_full_path, pHash, 4);
@@ -547,8 +537,6 @@ int secure_file::derive_file_path() {
byte_to_hex(dir, pHash, 4);
SLOGI("Dir is %s.", (char*)dir);
- //m_full_path[8] = '/';
-
if (0 != strlen(m_data_name)) {
// computing file name
uint64_t data_id = transform_id_to_name(transform_name_to_id(m_data_name));
@@ -809,7 +797,6 @@ int secure_file::parse_file_content(unsigned char* buffer,
switch (FileStructureType(ptr)) {
case 0: {
- // [header][hash][data][key]
m_file_content.m_pFileHeader = ptr;
ptr += HEADER_SIZE;
m_file_content.m_pHashMaterial = ptr;
@@ -821,7 +808,6 @@ int secure_file::parse_file_content(unsigned char* buffer,
break;
}
case 1: {
- // [header][hash][key][data]
m_file_content.m_pFileHeader = ptr;
ptr += HEADER_SIZE;
m_file_content.m_pHashMaterial = ptr;
@@ -833,7 +819,6 @@ int secure_file::parse_file_content(unsigned char* buffer,
break;
}
case 2: {
- // [header][data][key][hash]
m_file_content.m_pFileHeader = ptr;
ptr += HEADER_SIZE;
m_file_content.m_pFileContent = ptr;
@@ -845,7 +830,6 @@ int secure_file::parse_file_content(unsigned char* buffer,
break;
}
case 3: {
- // [header][key][data][hash]
m_file_content.m_pFileHeader = ptr;
ptr += HEADER_SIZE;
m_file_content.m_pKeyMaterial = ptr;
@@ -857,7 +841,6 @@ int secure_file::parse_file_content(unsigned char* buffer,
break;
}
case 4: {
- // [header][key][hash][data]
m_file_content.m_pFileHeader = ptr;
ptr += HEADER_SIZE;
m_file_content.m_pKeyMaterial = ptr;
@@ -869,7 +852,6 @@ int secure_file::parse_file_content(unsigned char* buffer,
break;
}
case 5: {
- // [header][data][hash][key]
m_file_content.m_pFileHeader = ptr;
ptr += HEADER_SIZE;
m_file_content.m_pFileContent = ptr;
@@ -944,7 +926,6 @@ int secure_file::serialize_data(unsigned char** buffer,
#ifdef _SECOS_SIM_
*buffer = (unsigned char*)OsaMalloc(m_write_data_size);
if (NULL == *buffer) {
- //SLOGE("fail to alloc memory for data.");
return SS_RET_MALLOC_FAILED;
}
@@ -975,7 +956,6 @@ int secure_file::serialize_data(unsigned char** buffer,
unsigned char* ptr = data;
switch (FileStructureType(m_file_content.m_pFileHeader)) {
case 0: {
- // [header][hash][data][key]
memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE);
ptr += HEADER_SIZE;
memcpy(ptr, m_file_content.m_pHashMaterial, HASH_SIZE);
@@ -989,7 +969,6 @@ int secure_file::serialize_data(unsigned char** buffer,
break;
}
case 1: {
- // [header][hash][key][data]
memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE);
ptr += HEADER_SIZE;
memcpy(ptr, m_file_content.m_pHashMaterial, HASH_SIZE);
@@ -1003,7 +982,6 @@ int secure_file::serialize_data(unsigned char** buffer,
break;
}
case 2: {
- // [header][data][key][hash]
memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE);
ptr += HEADER_SIZE;
memcpy(ptr,
@@ -1017,7 +995,6 @@ int secure_file::serialize_data(unsigned char** buffer,
break;
}
case 3: {
- // [header][key][data][hash]
memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE);
ptr += HEADER_SIZE;
memcpy(ptr, m_file_content.m_pKeyMaterial, KEY_MAT_SIZE);
@@ -1031,7 +1008,6 @@ int secure_file::serialize_data(unsigned char** buffer,
break;
}
case 4: {
- // [header][key][hash][data]
memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE);
ptr += HEADER_SIZE;
memcpy(ptr, m_file_content.m_pKeyMaterial, KEY_MAT_SIZE);
@@ -1045,7 +1021,6 @@ int secure_file::serialize_data(unsigned char** buffer,
break;
}
case 5: {
- // [header][data][hash][key]
memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE);
ptr += HEADER_SIZE;
memcpy(ptr,
diff --git a/ssflib/dep/swdss/source/ss_crypto.cpp b/ssflib/dep/swdss/source/ss_crypto.cpp
index d4c5e99..3b8bb97 100644
--- a/ssflib/dep/swdss/source/ss_crypto.cpp
+++ b/ssflib/dep/swdss/source/ss_crypto.cpp
@@ -40,8 +40,6 @@ void gen_rand_vec(CBT_OCTET* vec, CBT_UINT32 size) {
*/
int CCryptoEngine::Encrypt(uint8_t* dest, uint8_t* src, unsigned long data_len,
const uint8_t* key, unsigned long key_type) {
- //memcpy(dest,src,data_len);
- //return data_len;
unsigned int cipherTextLen, t;
CryptoCoreContainer *crt = create_CryptoCoreContainer(ID_AES);
diff --git a/ssflib/dep/uci/source/uci_api.c b/ssflib/dep/uci/source/uci_api.c
index a21522c..2873e7d 100644
--- a/ssflib/dep/uci/source/uci_api.c
+++ b/ssflib/dep/uci/source/uci_api.c
@@ -28,7 +28,6 @@
#include "uci_cryptocore.h"
#include "uci_internal.h"
#include "uci_hwcrypto.h"
-//#include "ae.h"
#include "CC_Context.h"
#include "uci_aes_xcbc_mac.h"
diff --git a/ssflib/dep/uci/source/uci_cryptocore.c b/ssflib/dep/uci/source/uci_cryptocore.c
index 6a87eb1..7aed80a 100644
--- a/ssflib/dep/uci/source/uci_cryptocore.c
+++ b/ssflib/dep/uci/source/uci_cryptocore.c
@@ -844,8 +844,6 @@ int cryptocore_ae_decryptbycrt(UCI_HANDLE oh, unsigned char *input,
if (pctx->config != UCI_SW)
return UCI_INVALID_HANDLE;
- // ctr=(CryptoCoreContainer *)(pctx->imp);
- // ctr->MD_update(ctr,msg,msg_len);
ret = ((CryptoCoreContainer *)pctx->imp)->AE_decryptByCRT(
((CryptoCoreContainer *)pctx->imp), input, input_len, output, output_len);
diff --git a/ssflib/dep/uci/source/uci_hwcrypto.c b/ssflib/dep/uci/source/uci_hwcrypto.c
index adcf86e..7642d97 100644
--- a/ssflib/dep/uci/source/uci_hwcrypto.c
+++ b/ssflib/dep/uci/source/uci_hwcrypto.c
@@ -457,9 +457,7 @@ int hwcrypto_se_init(UCI_HANDLE oh, unsigned int mode, unsigned int padding,
else
memset(info->iv, 0x0, info->ivlen);
- pctx->handle = open("/dev/crypto", 0, 0); //return hndl;
-
- //TA_PRINT("hand = %d \n",pctx->handle);
+ pctx->handle = open("/dev/crypto", 0, 0);
if (pctx->handle < 0)
return UCI_ERROR;
@@ -476,22 +474,22 @@ int hwcrypto_se_init(UCI_HANDLE oh, unsigned int mode, unsigned int padding,
uci_context_s *pctx = (uci_context_s *)oh;
unsigned int keytype;
unsigned int alg;
- //!AS current hw is not ready, so using SW pseduo way temproray.
+
unsigned char hwkey_master[32] = { 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35,
- 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23,
- 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35,
- 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23
- };
+ 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23,
+ 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35,
+ 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23
+ };
unsigned char hwiv_master[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
- 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
+ 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};
unsigned char hwkey_unique[32] = { 0xF0, 0x22, 0x34, 0x67, 0x66, 0x88, 0xAB, 0xCD,
- 0x12, 0x67, 0x89, 0x54, 0x32, 0x10, 0xCC, 0xFE,
- 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35,
- 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23
- };
+ 0x12, 0x67, 0x89, 0x54, 0x32, 0x10, 0xCC, 0xFE,
+ 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35,
+ 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23
+ };
unsigned char hwiv_unique[16] = { 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88,
- 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00
+ 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00
};
if (pctx == NULL)
@@ -715,8 +713,6 @@ int hwcrypto_se_final(UCI_HANDLE oh, unsigned char *input,
oper.src_len = len;
oper.dst_addr = (char *)output;
oper.dst_len = output_len;
-
- //oper.final = 1;
if (ret = ioctl(hndl, IOCTL_CRYPTO_CRYPT, &oper)) {
TA_PRINT("error:ioctl(hndl, 1, &oper) returned %d\n", ret);
return UCI_ERROR;
@@ -737,9 +733,6 @@ int hwcrypto_se_final(UCI_HANDLE oh, unsigned char *input,
}
padlen = output[input_len - 1];
-
- //PrintBYTE("padding",output,input_len);
- //PrintBYTE("input",input,input_len);
if (padlen < 1 || padlen > 16) {
*output_len = 0;
TA_PRINT("padding size{%d} is incorretc ", padlen);
diff --git a/ssflib/inc/app_debug.h b/ssflib/inc/app_debug.h
index f363ba3..266991c 100644
--- a/ssflib/inc/app_debug.h
+++ b/ssflib/inc/app_debug.h
@@ -67,7 +67,6 @@ unsigned char one_time_print_buffer_test[10240];
#define TURST_APP_WRN(fmt, ...) if (g_app_svc_dbglvl >= TRUSTAPP_DEBUG_LEVEL_WRN) {APP_SVC_WRN(APP_MODULE_NAME, fmt, ##__VA_ARGS__)}
#define TURST_APP_DBG(fmt, ...) if (g_app_svc_dbglvl >= TRUSTAPP_DEBUG_LEVEL_DBG) {APP_SVC_DBG(APP_MODULE_NAME, fmt, ##__VA_ARGS__)}
#define TURST_APP_LOG(fmt, ...) if (g_app_svc_dbglvl >= TRUSTAPP_DEBUG_LEVEL_LOG) {APP_SVC_LOG(APP_MODULE_NAME, fmt, ##__VA_ARGS__)}
-//#define TURST_APP_LOG(fmt, ...) TURST_APP_LOG_TEST(fmt,##__VA_ARGS__)
diff --git a/ssflib/src/app_debug.cpp b/ssflib/src/app_debug.cpp
index d169936..525accd 100644
--- a/ssflib/src/app_debug.cpp
+++ b/ssflib/src/app_debug.cpp
@@ -24,7 +24,6 @@
#include
#include
-//#define PRINT_LOG_TO_CONSOLE
#ifdef PRINT_LOG_TO_CONSOLE
#include
#define portname "/dev/ttyS0"
diff --git a/ssflib/src/ssf_client.cpp b/ssflib/src/ssf_client.cpp
index 00b07e4..2c89207 100644
--- a/ssflib/src/ssf_client.cpp
+++ b/ssflib/src/ssf_client.cpp
@@ -36,7 +36,6 @@
*-----------------------------------------------------------------------------*/
#define SOCKPATH "/tmp/simdaemon" //path to be updated
-//#define TEST
/*-----------------------------------------------------------------------------
* local functions
diff --git a/ssflib/src/ssf_crypto.cpp b/ssflib/src/ssf_crypto.cpp
index 4020cdf..21ddf54 100644
--- a/ssflib/src/ssf_crypto.cpp
+++ b/ssflib/src/ssf_crypto.cpp
@@ -31,6 +31,8 @@
#include
#include
#include
+#include
+#include
#include "CC_API.h"
#include "ssf_crypto_openssl.h"
@@ -471,12 +473,6 @@ static int sw_crypto_ioctl_init(crypto_internal_operation *operation, crypto_int
key->rsa_exponent2.buffer, &key->rsa_exponent2.size,
key->rsa_coefficient.buffer, &key->rsa_coefficient.size);
- /*if(rc == (-ETIMEDOUT))
- {
- LOGE(SSF_LIB, "Algorithm - %X : TIMEOUT \n", operation->info.algorithm);
- rc = TEE_ERROR_TIMEOUT;
- }*/
-
memcpy(key->rsa_public.buffer, E, ELen);
key->rsa_public.size = ELen;
}
@@ -2250,35 +2246,20 @@ TEE_Result TEE_AEInit(TEE_OperationHandle operation, void* nonce, size_t nonceLe
CRYPTO_PANIC;
}
// tagLen check
+ std::array values_GCM = {128, 120, 112, 104, 96};
+ std::array values_CCM = {128, 112, 96, 64, 48, 32};
switch (op->info.algorithm) {
case TEE_ALG_AES_GCM: {
- switch (tagLen) {
- case 128:
- case 120:
- case 112:
- case 104:
- case 96:
- break;
- default:
- LOGE(SSF_LIB, "Incorrect tag length %u", tagLen);
- return TEE_ERROR_NOT_SUPPORTED;
- };
- break;
+ if (std::find(values_GCM.begin(), values_GCM.end(), tagLen) == values_GCM.end()) {
+ LOGE(SSF_LIB, "Incorrect tag length %u", tagLen);
+ return TEE_ERROR_NOT_SUPPORTED;
+ }
}
case TEE_ALG_AES_CCM: {
- switch (tagLen) {
- case 128:
- case 112:
- case 96:
- case 64:
- case 48:
- case 32:
- break;
- default:
- LOGE(SSF_LIB, "Incorrect tag length %u", tagLen);
- return TEE_ERROR_NOT_SUPPORTED;
- };
- break;
+ if (std::find(values_CCM.begin(), values_CCM.end(), tagLen) == values_CCM.end()) {
+ LOGE(SSF_LIB, "Incorrect tag length %u", tagLen);
+ return TEE_ERROR_NOT_SUPPORTED;
+ }
}
default: {
LOGE(SSF_LIB, "Incorrect algorithm %x", op->info.algorithm);
diff --git a/ssflib/src/ssf_storage.cpp b/ssflib/src/ssf_storage.cpp
index 1b71c8e..1c92a21 100644
--- a/ssflib/src/ssf_storage.cpp
+++ b/ssflib/src/ssf_storage.cpp
@@ -151,67 +151,52 @@ TEE_Result allocate_transient_object(TransientObject* tr, uint32_t objectType,
//tr->attr.buf_len = (maxObjectSize + 7)>>3;
break;
case TEE_TYPE_DES:
- //if (maxObjectSize != 64) {
- // return TEE_ERROR_NOT_SUPPORTED;
- //}
- //tr->attr.buf_len = (maxObjectSize + 7)>>3;
break;
case TEE_TYPE_DES3:
if (maxObjectSize != 128 && maxObjectSize != 192)
return TEE_ERROR_NOT_SUPPORTED;
- //tr->attr.buf_len = (maxObjectSize + 7)>>3;
break;
case TEE_TYPE_HMAC_MD5:
if (maxObjectSize < 64 || maxObjectSize > 512 || maxObjectSize % 8)
return TEE_ERROR_NOT_SUPPORTED;
- //tr->attr.buf_len = (maxObjectSize + 7)>>3;
break;
case TEE_TYPE_HMAC_SHA1:
if (maxObjectSize < 80 || maxObjectSize > 512 || maxObjectSize % 8)
return TEE_ERROR_NOT_SUPPORTED;
- //tr->attr.buf_len = (maxObjectSize + 7)>>3;
break;
case TEE_TYPE_HMAC_SHA224:
if (maxObjectSize < 112 || maxObjectSize > 512 || maxObjectSize % 8)
return TEE_ERROR_NOT_SUPPORTED;
- //tr->attr.buf_len = (maxObjectSize + 7)>>3;
break;
case TEE_TYPE_HMAC_SHA256:
if (maxObjectSize < 192 || maxObjectSize > 1024 || maxObjectSize % 8)
return TEE_ERROR_NOT_SUPPORTED;
- //tr->attr.buf_len = (maxObjectSize + 7)>>3;
break;
case TEE_TYPE_HMAC_SHA384:
if (maxObjectSize < 256 || maxObjectSize > 1024 || maxObjectSize % 8)
return TEE_ERROR_NOT_SUPPORTED;
- //tr->attr.buf_len = (maxObjectSize + 7)>>3;
break;
case TEE_TYPE_HMAC_SHA512:
if (maxObjectSize < 256 || maxObjectSize > 1024 || maxObjectSize % 8)
return TEE_ERROR_NOT_SUPPORTED;
- //tr->attr.buf_len = (maxObjectSize + 7)>>3;
break;
case TEE_TYPE_RSA_PUBLIC_KEY:
case TEE_TYPE_RSA_KEYPAIR:
if (maxObjectSize < 256 || maxObjectSize > 4096 || maxObjectSize % 64)
return TEE_ERROR_NOT_SUPPORTED;
- //tr->attr.buf_len = sizeof(rsa_context);
break;
case TEE_TYPE_DSA_PUBLIC_KEY:
case TEE_TYPE_DSA_KEYPAIR:
if (maxObjectSize < 512 || maxObjectSize > 1024 || maxObjectSize % 64)
return TEE_ERROR_NOT_SUPPORTED;
- //tr->attr.buf_len = sizeof(dsa_context);
break;
case TEE_TYPE_DH_KEYPAIR:
if (maxObjectSize < 256 || maxObjectSize > 2048)
return TEE_ERROR_NOT_SUPPORTED;
- //tr->attr.buf_len = sizeof(dh_context);
break;
case TEE_TYPE_GENERIC_SECRET:
if (maxObjectSize > 4096 || maxObjectSize % 8)
return TEE_ERROR_NOT_SUPPORTED;
- //tr->attr.buf_len = (maxObjectSize + 7)>>3;
break;
default:
return TEE_ERROR_NOT_SUPPORTED;
@@ -221,9 +206,6 @@ TEE_Result allocate_transient_object(TransientObject* tr, uint32_t objectType,
tr->info.objectType = objectType;
tr->info.objectSize = 0;
tr->info.maxObjectSize = maxObjectSize;
- //tr->info.dataSize = 0;
- //tr->info.dataPosition = 0;
- //tr->info.handleFlags = 0;
tr->info.objectUsage = 0xffffffff;
return TEE_SUCCESS;
}
@@ -1245,7 +1227,6 @@ void add_to_po_list(persistent_object* po) {
g_po_list.next = &po->po_list;
}
MSG("=====PO %s added=====", po->po_file.file_name);
- //debug_list();
}
void rem_from_po_list(persistent_object* po) {
@@ -1253,7 +1234,6 @@ void rem_from_po_list(persistent_object* po) {
return;
}
MSG("=====To remove PO %s=====", po->po_file.file_name);
- //debug_list();
if (po->po_list.prev) {
po->po_list.prev->next = po->po_list.next;
}
@@ -1261,7 +1241,6 @@ void rem_from_po_list(persistent_object* po) {
po->po_list.next->prev = po->po_list.prev;
}
MSG("======PO removed=====");
- //debug_list();
}
po_user* get_po_user_from_po_list(uint32_t storageID, const void* objectID,
--
2.7.4