Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Vladislav Rykov
gateway_protocol
Commits
c285eeb1
Commit
c285eeb1
authored
Oct 07, 2020
by
Vladislav Rykov
Browse files
encryption added+tested
parent
e4d46d30
Changes
2
Hide whitespace changes
Inline
Side-by-side
gateway_protocol/gateway_protocol.c
View file @
c285eeb1
#include <gateway_protocol/gateway_protocol.h>
#include "gateway_protocol.h"
#include "security_adapter.h"
#define GATEWAY_PROTOCOL_APP_KEY_SIZE 8
static
uint8_t
app_key
[
GATEWAY_PROTOCOL_APP_KEY_SIZE
];
static
uint8_t
app_key
[
GATEWAY_PROTOCOL_APP_KEY_SIZE
];
static
uint8_t
dev_id
=
0xFF
;
static
uint8_t
dev_id
=
0xFF
;
static
uint8_t
secure_key
[
GATEWAY_PROTOCOL_SECURE_KEY_SIZE
];
void
gateway_protocol_init
(
const
uint8_t
*
appkey
,
const
uint8_t
devid
)
{
static
uint8_t
secure
=
1
;
void
gateway_protocol_init
(
const
uint8_t
*
appkey
,
const
uint8_t
devid
,
const
uint8_t
*
securekey
,
const
uint8_t
secured
)
{
memcpy
(
app_key
,
appkey
,
GATEWAY_PROTOCOL_APP_KEY_SIZE
);
memcpy
(
app_key
,
appkey
,
GATEWAY_PROTOCOL_APP_KEY_SIZE
);
dev_id
=
devid
;
dev_id
=
devid
;
memcpy
(
secure_key
,
securekey
,
GATEWAY_PROTOCOL_SECURE_KEY_SIZE
);
secure
=
secured
;
}
}
void
gateway_protocol_packet_encode
(
void
gateway_protocol_packet_encode
(
...
@@ -33,6 +41,17 @@ void gateway_protocol_packet_encode (
...
@@ -33,6 +41,17 @@ void gateway_protocol_packet_encode (
memcpy
(
&
packet
[
*
packet_length
],
payload
,
payload_length
);
memcpy
(
&
packet
[
*
packet_length
],
payload
,
payload_length
);
(
*
packet_length
)
+=
payload_length
;
(
*
packet_length
)
+=
payload_length
;
if
(
secure
)
{
security_adapter_encrypt
(
secure_key
,
&
packet
[
GATEWAY_PROTOCOL_APP_KEY_SIZE
],
packet_length
,
&
packet
[
GATEWAY_PROTOCOL_APP_KEY_SIZE
],
(
*
packet_length
-
GATEWAY_PROTOCOL_APP_KEY_SIZE
)
);
*
packet_length
+=
GATEWAY_PROTOCOL_APP_KEY_SIZE
;
}
}
}
uint8_t
gateway_protocol_packet_decode
(
uint8_t
gateway_protocol_packet_decode
(
...
@@ -49,19 +68,28 @@ uint8_t gateway_protocol_packet_decode (
...
@@ -49,19 +68,28 @@ uint8_t gateway_protocol_packet_decode (
memcpy
(
appkey
,
&
packet
[
p_len
],
GATEWAY_PROTOCOL_APP_KEY_SIZE
);
memcpy
(
appkey
,
&
packet
[
p_len
],
GATEWAY_PROTOCOL_APP_KEY_SIZE
);
p_len
+=
GATEWAY_PROTOCOL_APP_KEY_SIZE
;
p_len
+=
GATEWAY_PROTOCOL_APP_KEY_SIZE
;
if
(
secure
)
{
security_adapter_decrypt
(
secure_key
,
&
packet
[
GATEWAY_PROTOCOL_APP_KEY_SIZE
],
(
packet_length
-
GATEWAY_PROTOCOL_APP_KEY_SIZE
),
&
packet
[
GATEWAY_PROTOCOL_APP_KEY_SIZE
],
payload_length
);
}
dev
=
packet
[
p_len
];
dev
=
packet
[
p_len
];
p_len
++
;
p_len
++
;
*
packet_type
=
(
gateway_protocol_packet_type_t
)
packet
[
p_len
];
*
packet_type
=
(
gateway_protocol_packet_type_t
)
packet
[
p_len
];
p_len
++
;
p_len
++
;
*
payload_length
=
packet
[
p_len
];
p_len
++
;
p_len
++
;
*
payload_length
=
packet_length
-
p_len
;
memcpy
(
payload
,
&
packet
[
p_len
],
*
payload_length
);
memcpy
(
payload
,
&
packet
[
p_len
],
*
payload_length
);
p_len
+=
*
payload_length
;
p_len
+=
*
payload_length
;
return
(
!
memcmp
(
appkey
,
app_key
,
GATEWAY_PROTOCOL_APP_KEY_SIZE
)
&&
return
(
!
memcmp
(
appkey
,
app_key
,
GATEWAY_PROTOCOL_APP_KEY_SIZE
)
&&
dev
==
dev_id
&&
dev
==
dev_id
);
p_len
==
packet_length
);
}
}
\ No newline at end of file
gateway_protocol/gateway_protocol.h
View file @
c285eeb1
...
@@ -4,7 +4,9 @@
...
@@ -4,7 +4,9 @@
#include <stdint.h>
#include <stdint.h>
#include <string.h>
#include <string.h>
#define GATEWAY_PROTOCOL_PACKET_SIZE_MAX 128
#define GATEWAY_PROTOCOL_APP_KEY_SIZE 8
#define GATEWAY_PROTOCOL_MAX_PACKET_SIZE 160
#define GATEWAY_PROTOCOL_SECURE_KEY_SIZE 16
#ifdef __cplusplus
#ifdef __cplusplus
extern
"C"
{
extern
"C"
{
...
@@ -26,7 +28,11 @@ typedef enum {
...
@@ -26,7 +28,11 @@ typedef enum {
GATEWAY_PROTOCOL_STAT_NACK
=
0xFF
GATEWAY_PROTOCOL_STAT_NACK
=
0xFF
}
gateway_protocol_stat_t
;
}
gateway_protocol_stat_t
;
void
gateway_protocol_init
(
const
uint8_t
*
appkey
,
const
uint8_t
devid
);
void
gateway_protocol_init
(
const
uint8_t
*
appkey
,
const
uint8_t
devid
,
const
uint8_t
*
securekey
,
const
uint8_t
secured
);
void
gateway_protocol_packet_encode
(
void
gateway_protocol_packet_encode
(
const
gateway_protocol_packet_type_t
packet_type
,
const
gateway_protocol_packet_type_t
packet_type
,
...
@@ -46,4 +52,4 @@ uint8_t gateway_protocol_packet_decode (
...
@@ -46,4 +52,4 @@ uint8_t gateway_protocol_packet_decode (
}
}
#endif
#endif
#endif // __GATEWAY_PROTOCOL_H__
#endif // __GATEWAY_PROTOCOL_H__
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment