Skip to content

Commit f39c0a0

Browse files
Merge pull request #393 from bandi13/addMagicCrypto
Add magic crypto example
2 parents 284e405 + 5a4cd11 commit f39c0a0

6 files changed

Lines changed: 541 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
diff --git a/include/mcapi.h b/include/mcapi.h
2+
index ba426d8..278a8a4 100644
3+
--- a/include/mcapi.h
4+
+++ b/include/mcapi.h
5+
@@ -62,8 +62,8 @@ MCAPI MC_RV MC_GetVersion(OUT MC_VERSION *pVersion);
6+
MCAPI MC_RV MC_GetStatus(OUT MC_UINT *pFlag);
7+
8+
MCAPI MC_RV MC_Initialize(IN MC_VOID *pInitArgs);
9+
-MCAPI MC_RV MC_Finalize();
10+
-MCAPI MC_RV MC_Selftest();
11+
+MCAPI MC_RV MC_Finalize(MC_VOID);
12+
+MCAPI MC_RV MC_Selftest(MC_VOID);
13+
MCAPI MC_STR MC_GetErrorString(MC_RV nRv);
14+
15+
MCAPI MC_RV MC_OpenSession(OUT MC_HSESSION *phSession);
16+
diff --git a/include/mcapi_error.h b/include/mcapi_error.h
17+
index c389d22..07ea9fc 100644
18+
--- a/include/mcapi_error.h
19+
+++ b/include/mcapi_error.h
20+
@@ -55,7 +55,7 @@
21+
#define MC_ERR_NOT_ENOUGH_BUFFER MC_ERR_BASE+27
22+
#define MC_ERR_NOT_SESSION_OBJECT MC_ERR_BASE+28
23+
24+
-//MC_STR MC_GetErrorString(MC_RV rv);
25+
+/* MC_STR MC_GetErrorString(MC_RV rv); */
26+
27+
28+
#endif /* _MC_HEADER_9330603E_D03D_4B8B_9746_9ED098D8A5CB */

crypto/MagicCrypto/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
bin_files = client server
2+
all: $(bin_files)
3+
4+
DEPS=common.h
5+
6+
CC=gcc
7+
#CC=clang -fsanitize=address
8+
9+
DEBUGOPT=
10+
#DEBUGOPT=-DWOLFSSL_DEBUG_TLS -DDEBUG_WOLFSSL -DDEBUG_CRYPTOCB
11+
#DEBUGOPT=-DWOLFSSL_DEBUG_TLS -DDEBUG_WOLFSSL
12+
13+
COMMONOPT=-O0 -g -IMagicCrypto/include -lwolfssl -lm -DWOLF_CRYPTO_CB
14+
15+
%: %.c $(DEPS)
16+
$(CC) $< $(DEBUGOPT) $(COMMONOPT) -o $@
17+
18+
clean:
19+
rm -f $(bin_files)

crypto/MagicCrypto/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Overview
2+
The MagicCrypto library is created by [Dream Security](https://dreamsecurity.com) which contains a certified implementation of the [ARIA cipher](https://en.wikipedia.org/wiki/ARIA_(cipher)) that is used by the South Korean government. This example makes use of the wolfSSL bindings to create a TLS 1.2 connection.
3+
4+
# Compiling
5+
You will need to compile wolfSSL with:
6+
./configure --enable-ariagcm --enable-cryptocb && make install
7+
8+
You will need to have the MagicCrypto headers and library in the wolfSSL source directory. You may need to apply the patch file to clear up some of the compiler warnings.
9+
10+
Once the wolfSSL library in installed, you can run `make` in this folder to generate the sample client and server applications. You may need to modify the Makefile to point to the MagicCrypto includes directory.
11+
12+
# Usage
13+
The sample applications depend on using the certificates found in the wolfSSL source directory. You should execute them from the folder containing the 'certs' folder. Alternatively, you can modify the `#define` in 'common.h'.
14+
15+
To run, simply start `./server` and `./client 127.0.0.1`.

crypto/MagicCrypto/client.c

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/* client-tls-cryptocb.c
2+
*
3+
* Copyright (C) 2006-2020 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL. (formerly known as CyaSSL)
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
22+
#include "common.h"
23+
24+
static void error_out(char* msg, int err)
25+
{
26+
printf("Failed at %s with code %d\n", msg, err);
27+
exit(1);
28+
}
29+
30+
int main(int argc, char** argv)
31+
{
32+
int ret = 0;
33+
#ifdef WOLF_CRYPTO_CB
34+
int sockfd;
35+
struct sockaddr_in servAddr;
36+
char buff[256];
37+
size_t len;
38+
39+
/* declare wolfSSL objects */
40+
WOLFSSL_CTX* ctx;
41+
WOLFSSL* ssl;
42+
43+
#if defined(DEBUG_WOLFSSL)
44+
wolfSSL_Debugging_ON();
45+
#endif
46+
47+
/* Check for proper calling convention */
48+
if (argc != 2) {
49+
printf("usage: %s <IPv4 address>\n", argv[0]);
50+
return 0;
51+
}
52+
53+
/* Create a socket that uses an internet IPv4 address,
54+
* Sets the socket to be stream based (TCP),
55+
* 0 means choose the default protocol. */
56+
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
57+
fprintf(stderr, "ERROR: failed to create the socket\n");
58+
ret = -1;
59+
goto end;
60+
}
61+
62+
/* Initialize the server address struct with zeros */
63+
memset(&servAddr, 0, sizeof(servAddr));
64+
65+
/* Fill in the server address */
66+
servAddr.sin_family = AF_INET; /* using IPv4 */
67+
servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
68+
69+
/* Get the server IPv4 address from the command line call */
70+
if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) != 1) {
71+
fprintf(stderr, "ERROR: invalid address\n");
72+
ret = -1;
73+
goto end;
74+
}
75+
76+
/* Connect to the server */
77+
if ((ret = connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr)))
78+
== -1) {
79+
fprintf(stderr, "ERROR: failed to connect\n");
80+
goto end;
81+
}
82+
83+
/*---------------------------------*/
84+
/* Start of wolfSSL initialization and configuration */
85+
/*---------------------------------*/
86+
/* Initialize wolfSSL */
87+
if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) {
88+
fprintf(stderr, "ERROR: Failed to initialize the library\n");
89+
goto socket_cleanup;
90+
}
91+
92+
/* Create and initialize WOLFSSL_CTX */
93+
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
94+
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
95+
ret = -1;
96+
goto socket_cleanup;
97+
}
98+
99+
/* Load client certificates into WOLFSSL_CTX */
100+
if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL))
101+
!= SSL_SUCCESS) {
102+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
103+
CA_FILE);
104+
goto ctx_cleanup;
105+
}
106+
107+
/* Load client ecc certificates into WOLFSSL_CTX */
108+
if ((ret = wolfSSL_CTX_use_certificate_chain_file(ctx, CLIENT_ECC_FILE)) !=
109+
WOLFSSL_SUCCESS) {
110+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
111+
CLIENT_ECC_FILE);
112+
goto ctx_cleanup;
113+
}
114+
115+
/* Load client ecc key into WOLFSSL_CTX */
116+
if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, CLIENT_KEY_FILE, SSL_FILETYPE_PEM))
117+
!= WOLFSSL_SUCCESS) {
118+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
119+
CLIENT_KEY_FILE);
120+
goto ctx_cleanup;
121+
}
122+
123+
if ((ret = wolfSSL_CTX_set_cipher_list(ctx, CIPHER_LIST))
124+
!= WOLFSSL_SUCCESS) {
125+
fprintf(stderr, "ERROR: failed to set cipher list\n");
126+
goto ctx_cleanup;
127+
}
128+
129+
if ((ret = wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1))
130+
!= WOLFSSL_SUCCESS) {
131+
fprintf(stderr, "ERROR: failed to set supported curve\n");
132+
goto ctx_cleanup;
133+
}
134+
135+
/* Create a WOLFSSL object */
136+
if ((ssl = wolfSSL_new(ctx)) == NULL) {
137+
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
138+
ret = -1;
139+
goto ctx_cleanup;
140+
}
141+
142+
/* Attach wolfSSL to the socket */
143+
if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) {
144+
fprintf(stderr, "ERROR: Failed to set the file descriptor\n");
145+
goto cleanup;
146+
}
147+
148+
/* Connect to wolfSSL on the server side */
149+
if ((ret = wolfSSL_connect(ssl)) != SSL_SUCCESS) {
150+
fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
151+
goto cleanup;
152+
}
153+
154+
/* Get a message for the server from stdin */
155+
printf("Message for server: ");
156+
memset(buff, 0, sizeof(buff));
157+
if (fgets(buff, sizeof(buff), stdin) == NULL) {
158+
fprintf(stderr, "ERROR: failed to get message for server\n");
159+
ret = -1;
160+
goto cleanup;
161+
}
162+
len = strnlen(buff, sizeof(buff));
163+
164+
/* Send the message to the server */
165+
if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
166+
fprintf(stderr, "ERROR: failed to write entire message\n");
167+
fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len);
168+
goto cleanup;
169+
}
170+
171+
/* Read the server data into our buff array */
172+
memset(buff, 0, sizeof(buff));
173+
if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
174+
fprintf(stderr, "ERROR: failed to read\n");
175+
goto cleanup;
176+
}
177+
178+
/* Print to stdout any data the server sends */
179+
printf("Server: %s\n", buff);
180+
181+
ret = 0;
182+
183+
/* Cleanup and return */
184+
cleanup:
185+
if (ret != 0) {
186+
fprintf(stderr,"SSL Error: %s\n",wolfSSL_ERR_error_string(wolfSSL_get_error(ssl,0), NULL));
187+
}
188+
wolfSSL_free(ssl); /* Free the wolfSSL object */
189+
ctx_cleanup:
190+
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
191+
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
192+
socket_cleanup:
193+
close(sockfd); /* Close the connection to the server */
194+
end:
195+
196+
#else
197+
printf("Please configure wolfSSL with --enable-cryptocb and try again\n");
198+
#endif /* WOLF_CRYPTO_CB */
199+
return ret; /* Return reporting a success */
200+
}

crypto/MagicCrypto/common.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* client-tls-cryptocb.c
2+
*
3+
* Copyright (C) 2006-2020 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL. (formerly known as CyaSSL)
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
22+
#ifndef COMMON_H
23+
#define COMMON_H
24+
25+
/* the usual suspects */
26+
#include <stdlib.h>
27+
#include <stdio.h>
28+
#include <string.h>
29+
30+
/* socket includes */
31+
#include <sys/socket.h>
32+
#include <arpa/inet.h>
33+
#include <netinet/in.h>
34+
#include <unistd.h>
35+
36+
/* wolfSSL */
37+
#include <wolfssl/options.h>
38+
#include <wolfssl/ssl.h>
39+
#include <wolfssl/wolfcrypt/sha256.h>
40+
#include <wolfssl/wolfcrypt/cryptocb.h>
41+
#include <wolfssl/wolfcrypt/error-crypt.h>
42+
#include <wolfssl/wolfcrypt/port/aria/aria-cryptocb.h>
43+
44+
#define DEFAULT_PORT 11111
45+
46+
#define CA_FILE "./certs/ca-ecc-cert.pem"
47+
48+
#define CLIENT_ECC_FILE "./certs/intermediate/client-chain-ecc.pem"
49+
#define SERVER_ECC_FILE "./certs/intermediate/server-chain-ecc.pem"
50+
#define CLIENT_KEY_FILE "./certs/ecc-client-key.pem"
51+
#define SERVER_KEY_FILE "./certs/ecc-key.pem"
52+
53+
#define CIPHER_LIST "ECDHE-ECDSA-ARIA128-GCM-SHA256:ECDHE-ECDSA-ARIA256-GCM-SHA384"
54+
//#define CIPHER_LIST "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384"
55+
56+
#endif /* COMMON_H */

0 commit comments

Comments
 (0)