|
|
@ -1,3 +1,26 @@
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* l2_security.c
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Copyright 2013-2025 Michael Zillgith
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* This file is part of libIEC61850.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* libIEC61850 is free software: you can redistribute it and/or modify
|
|
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* libIEC61850 is distributed in the hope that it will be useful,
|
|
|
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
|
|
|
* along with libIEC61850. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* See COPYING file for the complete license text.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
|
@ -6,7 +29,8 @@
|
|
|
|
#include "ber_decode.h"
|
|
|
|
#include "ber_decode.h"
|
|
|
|
#include "r_session_crypto.h"
|
|
|
|
#include "r_session_crypto.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct sL2Security {
|
|
|
|
struct sL2Security
|
|
|
|
|
|
|
|
{
|
|
|
|
RSignatureAlgorithm currentSigAlgo;
|
|
|
|
RSignatureAlgorithm currentSigAlgo;
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t timeOfCurrentKey;
|
|
|
|
uint32_t timeOfCurrentKey;
|
|
|
@ -73,6 +97,16 @@ L2Security_calculateCRC16(uint8_t* data, int size)
|
|
|
|
return calculateCRC(data, size);
|
|
|
|
return calculateCRC(data, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
|
|
|
printBuffer(uint8_t* buffer, int size)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("%02x", buffer[i]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* \brief Create the security extension
|
|
|
|
* \brief Create the security extension
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -88,9 +122,9 @@ L2Security_addSecurityExtension(L2Security self, uint8_t* buffer, int start, int
|
|
|
|
printf("L2Security_addSecurityExtension: start=%i, length=%i, maxBufSize=%i\n", start, length, maxBufSize);
|
|
|
|
printf("L2Security_addSecurityExtension: start=%i, length=%i, maxBufSize=%i\n", start, length, maxBufSize);
|
|
|
|
if (self->currentSigAlgo != MC_SEC_SIG_ALGO_NONE)
|
|
|
|
if (self->currentSigAlgo != MC_SEC_SIG_ALGO_NONE)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bool hasIV = false;
|
|
|
|
|
|
|
|
int ivSize = 0;
|
|
|
|
int ivSize = 0;
|
|
|
|
int mACSize = 0;
|
|
|
|
int mACSize = 0;
|
|
|
|
|
|
|
|
uint8_t* ivBuf = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
/* determine length of the mAC */
|
|
|
|
/* determine length of the mAC */
|
|
|
|
if (self->currentSigAlgo == MC_SEC_SIG_ALGO_HMAC_SHA256_128) {
|
|
|
|
if (self->currentSigAlgo == MC_SEC_SIG_ALGO_HMAC_SHA256_128) {
|
|
|
@ -99,8 +133,17 @@ L2Security_addSecurityExtension(L2Security self, uint8_t* buffer, int start, int
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_HMAC_SHA256_256) {
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_HMAC_SHA256_256) {
|
|
|
|
mACSize = 2 + 32;
|
|
|
|
mACSize = 2 + 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_AES_GMAC_128) {
|
|
|
|
|
|
|
|
mACSize = 2 + 16;
|
|
|
|
|
|
|
|
ivSize = 12; /* IV size for AES GMAC (recommendation from NIST: https://web.cs.ucdavis.edu/~rogaway/ocb/gcm.pdf) */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_AES_GMAC_256) {
|
|
|
|
|
|
|
|
mACSize = 2 + 32;
|
|
|
|
|
|
|
|
ivSize = 12; /* IV size for AES GMAC (recommendation from NIST: https://web.cs.ucdavis.edu/~rogaway/ocb/gcm.pdf) */
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
/* signature algorithm not supported */
|
|
|
|
/* signature algorithm not supported */
|
|
|
|
|
|
|
|
printf("Signature algorithm not supported\n");
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -119,7 +162,8 @@ L2Security_addSecurityExtension(L2Security self, uint8_t* buffer, int start, int
|
|
|
|
authValueSize += (2 + BerEncoder_Int32determineEncodedSize(self->timeToNextKey));
|
|
|
|
authValueSize += (2 + BerEncoder_Int32determineEncodedSize(self->timeToNextKey));
|
|
|
|
|
|
|
|
|
|
|
|
/* IV */
|
|
|
|
/* IV */
|
|
|
|
if (hasIV) {
|
|
|
|
if (ivSize > 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
authValueSize += (2 + ivSize);
|
|
|
|
authValueSize += (2 + ivSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -157,14 +201,16 @@ L2Security_addSecurityExtension(L2Security self, uint8_t* buffer, int start, int
|
|
|
|
bufPos = BerEncoder_encodeInt32WithTL(0x82, self->timeToNextKey, buffer, bufPos);
|
|
|
|
bufPos = BerEncoder_encodeInt32WithTL(0x82, self->timeToNextKey, buffer, bufPos);
|
|
|
|
|
|
|
|
|
|
|
|
/* IV */
|
|
|
|
/* IV */
|
|
|
|
if (hasIV) {
|
|
|
|
if (ivSize > 0)
|
|
|
|
//TODO encode IV
|
|
|
|
{
|
|
|
|
|
|
|
|
bufPos = BerEncoder_encodeTL(0x83, ivSize, buffer, bufPos);
|
|
|
|
|
|
|
|
ivBuf = buffer + bufPos;
|
|
|
|
|
|
|
|
bufPos += ivSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* KeyID */
|
|
|
|
/* KeyID */
|
|
|
|
bufPos = BerEncoder_encodeInt32WithTL(0x84, self->currentKeyId, buffer, bufPos);
|
|
|
|
bufPos = BerEncoder_encodeInt32WithTL(0x84, self->currentKeyId, buffer, bufPos);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int macEnd = bufPos;
|
|
|
|
int macEnd = bufPos;
|
|
|
|
|
|
|
|
|
|
|
|
/* encode mAC */
|
|
|
|
/* encode mAC */
|
|
|
@ -178,11 +224,46 @@ L2Security_addSecurityExtension(L2Security self, uint8_t* buffer, int start, int
|
|
|
|
RSessionCrypto_createHMAC(buffer + start, macEnd - start, self->currentKey, self->currentKeySize, buffer + bufPos, 32);
|
|
|
|
RSessionCrypto_createHMAC(buffer + start, macEnd - start, self->currentKey, self->currentKeySize, buffer + bufPos, 32);
|
|
|
|
bufPos += 32;
|
|
|
|
bufPos += 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_AES_GMAC_128)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/* create IV */
|
|
|
|
|
|
|
|
if (RSessionCrypto_createRandomData(ivBuf, ivSize) == false) {
|
|
|
|
|
|
|
|
printf("ERROR - Failed to create random IV\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (RSessionCrypto_createAES_GMAC(self->currentKey, self->currentKeySize, ivBuf, ivSize, buffer + start, macEnd - start, buffer + bufPos, 16) == false)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("ERROR - Failed to create GMAC\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bufPos += 16;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_AES_GMAC_256)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/* create IV */
|
|
|
|
|
|
|
|
if (RSessionCrypto_createRandomData(ivBuf, ivSize) == false) {
|
|
|
|
|
|
|
|
printf("ERROR - Failed to create random IV\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (RSessionCrypto_createAES_GMAC(self->currentKey, self->currentKeySize, ivBuf, ivSize, buffer + start, macEnd - start, buffer + bufPos, 32) == false)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("ERROR - Failed to create GMAC\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bufPos += 32;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
/* signature algorithm not supported */
|
|
|
|
|
|
|
|
printf("Signature algorithm not supported\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return securityExtensionSize + 2;
|
|
|
|
return securityExtensionSize + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("L2_SECURITY: no signature algorithm set\n");
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -204,6 +285,12 @@ checkSecurityExtension(L2Security self, uint8_t* buffer, int secExtLen, uint8_t*
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_HMAC_SHA256_256) {
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_HMAC_SHA256_256) {
|
|
|
|
mACSize = 2 + 32;
|
|
|
|
mACSize = 2 + 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_AES_GMAC_128) {
|
|
|
|
|
|
|
|
mACSize = 2 + 16;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_AES_GMAC_256) {
|
|
|
|
|
|
|
|
mACSize = 2 + 32;
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
/* signature algorithm not supported */
|
|
|
|
/* signature algorithm not supported */
|
|
|
|
printf("L2_SECURITY: signature algorithm not supported\n");
|
|
|
|
printf("L2_SECURITY: signature algorithm not supported\n");
|
|
|
@ -320,7 +407,7 @@ checkSecurityExtension(L2Security self, uint8_t* buffer, int secExtLen, uint8_t*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_HMAC_SHA256_256)
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_HMAC_SHA256_256)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("Algo: HMAC_SHA256_256\n");
|
|
|
|
printf("Algo: HMAC_SHA256_256\n");
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t calculatedMac[32];
|
|
|
|
uint8_t calculatedMac[32];
|
|
|
|
|
|
|
|
|
|
|
@ -332,6 +419,54 @@ checkSecurityExtension(L2Security self, uint8_t* buffer, int secExtLen, uint8_t*
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_AES_GMAC_128)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("Algo: AES_GMAC_128\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t calculatedMac[16];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("IV: ");
|
|
|
|
|
|
|
|
printBuffer(ivBuffer, ivSize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("MAC: ");
|
|
|
|
|
|
|
|
printBuffer(mACBuffer, mACSize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (RSessionCrypto_createAES_GMAC(self->currentKey, self->currentKeySize, ivBuffer, ivSize, macStart, macEnd, calculatedMac, 16) == false)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("L2_SECURITY: GMAC calculation failed\n");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (memcmp(calculatedMac, mACBuffer, 16) != 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("L2_SECURITY: GMAC mismatch\n");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (self->currentSigAlgo == MC_SEC_SIG_ALGO_AES_GMAC_256)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("Algo: AES_GMAC_256\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("IV: ");
|
|
|
|
|
|
|
|
printBuffer(ivBuffer, ivSize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("MAC: ");
|
|
|
|
|
|
|
|
printBuffer(mACBuffer, mACSize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t calculatedMac[8];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (RSessionCrypto_createAES_GMAC(self->currentKey, self->currentKeySize, ivBuffer, ivSize, macStart, macEnd, calculatedMac, 32) == false)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("L2_SECURITY: GMAC calculation failed\n");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (memcmp(calculatedMac, mACBuffer, 8) != 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("L2_SECURITY: GMAC mismatch\n");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("L2_SECURITY: signature algorithm not supported\n");
|
|
|
|
printf("L2_SECURITY: signature algorithm not supported\n");
|
|
|
|