123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- /*====================================================================*
- Copyright (c) 2013,2021 Qualcomm Technologies, Inc.
- All Rights Reserved.
- Confidential and Proprietary - Qualcomm Technologies, Inc.
- ******************************************************************
- 2013 Qualcomm Atheros, Inc.
- *--------------------------------------------------------------------*/
- /*====================================================================*
- *
- * signed pev_cm_set_key (struct session * session, struct channel * channel, struct message * message);
- *
- * slac.h
- *
- * PEV-HLE sets the NMK and NID on PEV-PLC using CM_SET_KEY.REQ;
- * the NMK and NID must match those provided by EVSE-HLE using
- * CM_SLAC_MATCH.CNF;
- *
- * Contributor(s):
- * Charles Maier <cmaier@qca.qualcomm.com>
- *
- *--------------------------------------------------------------------*/
- #ifndef PEV_CM_SET_KEY_SOURCE
- #define PEV_CM_SET_KEY_SOURCE
- #include <string.h>
- #include "../ether/channel.h"
- #include "../tools/memory.h"
- #include "../tools/error.h"
- #include "../tools/flags.h"
- #include "../mme/qualcomm.h"
- #include "../mme/homeplug.h"
- #include "../slac/slac.h"
- #include "../plc/plc.h"
- signed pev_cm_set_key (struct session * session, struct channel * channel, struct message * message)
- {
- #ifndef __GNUC__
- #pragma pack(push,1)
- #endif
- struct __packed cm_set_key_request
- {
- struct ethernet_hdr ethernet;
- struct homeplug_fmi homeplug;
- uint8_t KEYTYPE;
- uint32_t MYNONCE;
- uint32_t YOURNONCE;
- uint8_t PID;
- uint16_t PRN;
- uint8_t PMN;
- uint8_t CCOCAP;
- uint8_t NID [SLAC_NID_LEN];
- uint8_t NEWEKS;
- uint8_t NEWKEY [SLAC_NMK_LEN];
- uint8_t RSVD [3];
- }
- * request = (struct cm_set_key_request *) (message);
- struct __packed cm_set_key_confirm
- {
- struct ethernet_hdr ethernet;
- struct homeplug_fmi homeplug;
- uint8_t RESULT;
- uint32_t MYNONCE;
- uint32_t YOURNONCE;
- uint8_t PID;
- uint16_t PRN;
- uint8_t PMN;
- uint8_t CCOCAP;
- uint8_t RSVD [27];
- }
- * confirm = (struct cm_set_key_confirm *) (message);
- #ifndef __GNUC__
- #pragma pack (pop)
- #endif
- memset (message, 0, sizeof (* message));
- debug (0, __func__, "--> CM_SET_KEY.REQ");
- EthernetHeader (& request->ethernet, channel->peer, channel->host, channel->type);
- HomePlugHeader1 (& request->homeplug, HOMEPLUG_MMV, (CM_SET_KEY | MMTYPE_REQ));
- request->KEYTYPE = SLAC_CM_SETKEY_KEYTYPE;
- memset (& request->MYNONCE, 0xAA, sizeof (request->MYNONCE));
- memset (& request->YOURNONCE, 0x00, sizeof (request->YOURNONCE));
- request->PID = SLAC_CM_SETKEY_PID;
- request->PRN = HTOLE16 (SLAC_CM_SETKEY_PRN);
- request->PMN = SLAC_CM_SETKEY_PMN;
- request->CCOCAP = SLAC_CM_SETKEY_CCO;
- memcpy (request->NID, session->NID, sizeof (request->NID));
- request->NEWEKS = SLAC_CM_SETKEY_EKS;
- memcpy (request->NEWKEY, session->NMK, sizeof (request->NEWKEY));
- #if SLAC_DEBUG
- if (_anyset (session->flags, SLAC_VERBOSE))
- {
- char string [1024];
- debug (0, __func__, "CM_SET_KEY.KEYTYPE %d", request->KEYTYPE);
- debug (0, __func__, "CM_SET_KEY.MYNONCE %s", hexstring (string, sizeof (string), & request->MYNONCE, sizeof (request->MYNONCE)));
- debug (0, __func__, "CM_SET_KEY.YOURNONCE %s", hexstring (string, sizeof (string), & request->YOURNONCE, sizeof (request->MYNONCE)));
- debug (0, __func__, "CM_SET_KEY.PID %d", request->PID);
- debug (0, __func__, "CM_SET_KEY.PRN %d", LE32TOH (request->PRN));
- debug (0, __func__, "CM_SET_KEY.PMN %d", request->PMN);
- debug (0, __func__, "CM_SET_KEY.CCoCAP %d", request->CCOCAP);
- debug (0, __func__, "CM_SET_KEY.NID %s", HEXSTRING (string, request->NID));
- debug (0, __func__, "CM_SET_KEY.NEWEKS %d", request->NEWEKS);
- debug (0, __func__, "CM_SET_KEY.NEWKEY %s", HEXSTRING (string, request->NEWKEY));
- }
- #endif
- if (sendpacket (channel, request, sizeof (* request)) <= 0)
- {
- return (debug (1, __func__, CHANNEL_CANTSEND));
- }
- while (readpacket (channel, confirm, sizeof (* confirm)) > 0)
- {
- if (ntohs (confirm->ethernet.MTYPE) != ETH_P_HPAV)
- {
- debug (session->exit, __func__, "Ignore MTYPE 0x%04X", htons (confirm->ethernet.MTYPE));
- continue;
- }
- if (confirm->homeplug.MMV != HOMEPLUG_MMV)
- {
- debug (session->exit, __func__, "Ignore MMV 0x%02X", confirm->homeplug.MMV);
- continue;
- }
- if (LE32TOH (confirm->homeplug.MMTYPE) != (CM_SET_KEY | MMTYPE_CNF))
- {
- debug (session->exit, __func__, "Ignore MMTYPE 0x%04X", LE32TOH (confirm->homeplug.MMTYPE));
- continue;
- }
- debug (0, __func__, "<-- CM_SET_KEY.CNF");
- if (! confirm->RESULT)
- {
- return (debug (session->exit, __func__, "Can't set keys"));
- }
- #if SLAC_DEBUG
- if (_anyset (session->flags, SLAC_VERBOSE))
- {
- char string [1024];
- debug (0, __func__, "CM_SET_KEY.RESULT %d", confirm->RESULT);
- debug (0, __func__, "CM_SET_KEY.MYNONCE %s", hexstring (string, sizeof (string), & confirm->MYNONCE, sizeof (confirm->MYNONCE)));
- debug (0, __func__, "CM_SET_KEY.YOURNONCE %s", hexstring (string, sizeof (string), & confirm->YOURNONCE, sizeof (confirm->MYNONCE)));
- debug (0, __func__, "CM_SET_KEY.PID %d", confirm->PID);
- debug (0, __func__, "CM_SET_KEY.PRN %d", LE32TOH (confirm->PRN));
- debug (0, __func__, "CM_SET_KEY.PMN %d", confirm->PMN);
- debug (0, __func__, "CM_SET_KEY.CCoCAP %d", confirm->CCOCAP);
- }
- #endif
- return (0);
- }
- return (debug (session->exit, __func__, "<-- CM_SET_KEY.CNF ?"));
- }
- signed pev_cm_set_key_req(struct session* session, struct channel* channel, struct message* message)
- {
- #ifndef __GNUC__
- #pragma pack(push,1)
- #endif
- struct __packed cm_set_key_request
- {
- struct ethernet_hdr ethernet;
- struct homeplug_fmi homeplug;
- uint8_t KEYTYPE;
- uint32_t MYNONCE;
- uint32_t YOURNONCE;
- uint8_t PID;
- uint16_t PRN;
- uint8_t PMN;
- uint8_t CCOCAP;
- uint8_t NID[SLAC_NID_LEN];
- uint8_t NEWEKS;
- uint8_t NEWKEY[SLAC_NMK_LEN];
- uint8_t RSVD[3];
- }
- *request = (struct cm_set_key_request*)(message);
- #ifndef __GNUC__
- #pragma pack (pop)
- #endif
- memset(message, 0, sizeof(*message));
- debug(0, __func__, "--> CM_SET_KEY.REQ");
- EthernetHeader(&request->ethernet, channel->peer, channel->host, channel->type);
- HomePlugHeader1(&request->homeplug, HOMEPLUG_MMV, (CM_SET_KEY | MMTYPE_REQ));
- request->KEYTYPE = SLAC_CM_SETKEY_KEYTYPE;
- memset(&request->MYNONCE, 0xAA, sizeof(request->MYNONCE));
- memset(&request->YOURNONCE, 0x00, sizeof(request->YOURNONCE));
- request->PID = SLAC_CM_SETKEY_PID;
- request->PRN = HTOLE16(SLAC_CM_SETKEY_PRN);
- request->PMN = SLAC_CM_SETKEY_PMN;
- request->CCOCAP = SLAC_CM_SETKEY_CCO;
- memcpy(request->NID, session->NID, sizeof(request->NID));
- request->NEWEKS = SLAC_CM_SETKEY_EKS;
- memcpy(request->NEWKEY, session->NMK, sizeof(request->NEWKEY));
- #if SLAC_DEBUG
- if (_anyset(session->flags, SLAC_VERBOSE))
- {
- char string[1024];
- debug(0, __func__, "CM_SET_KEY.KEYTYPE %d", request->KEYTYPE);
- debug(0, __func__, "CM_SET_KEY.MYNONCE %s", hexstring(string, sizeof(string), &request->MYNONCE, sizeof(request->MYNONCE)));
- debug(0, __func__, "CM_SET_KEY.YOURNONCE %s", hexstring(string, sizeof(string), &request->YOURNONCE, sizeof(request->MYNONCE)));
- debug(0, __func__, "CM_SET_KEY.PID %d", request->PID);
- debug(0, __func__, "CM_SET_KEY.PRN %d", LE32TOH(request->PRN));
- debug(0, __func__, "CM_SET_KEY.PMN %d", request->PMN);
- debug(0, __func__, "CM_SET_KEY.CCoCAP %d", request->CCOCAP);
- debug(0, __func__, "CM_SET_KEY.NID %s", HEXSTRING(string, request->NID));
- debug(0, __func__, "CM_SET_KEY.NEWEKS %d", request->NEWEKS);
- debug(0, __func__, "CM_SET_KEY.NEWKEY %s", HEXSTRING(string, request->NEWKEY));
- }
- #endif
- if (sendpacket(channel, request, sizeof(*request)) <= 0)
- {
- return (debug(1, __func__, CHANNEL_CANTSEND));
- }
-
- return 0;
- }
- signed pev_cm_set_key_cnf(struct session* session, struct channel* channel, struct message* message)
- {
- #ifndef __GNUC__
- #pragma pack(push,1)
- #endif
- struct __packed cm_set_key_confirm
- {
- struct ethernet_hdr ethernet;
- struct homeplug_fmi homeplug;
- uint8_t RESULT;
- uint32_t MYNONCE;
- uint32_t YOURNONCE;
- uint8_t PID;
- uint16_t PRN;
- uint8_t PMN;
- uint8_t CCOCAP;
- uint8_t RSVD[27];
- }
- *confirm = (struct cm_set_key_confirm*)(message);
- #ifndef __GNUC__
- #pragma pack (pop)
- #endif
- debug(0, __func__, "<-- CM_SET_KEY.CNF");
- if (!confirm->RESULT)
- {
- return (debug(session->exit, __func__, "Can't set keys"));
- }
- #if SLAC_DEBUG
- if (_anyset(session->flags, SLAC_VERBOSE))
- {
- char string[1024];
- debug(0, __func__, "CM_SET_KEY.RESULT %d", confirm->RESULT);
- debug(0, __func__, "CM_SET_KEY.MYNOUNCE %s", hexstring(string, sizeof(string), &confirm->MYNONCE, sizeof(confirm->MYNONCE)));
- debug(0, __func__, "CM_SET_KEY.YOURNOUNCE %s", hexstring(string, sizeof(string), &confirm->YOURNONCE, sizeof(confirm->MYNONCE)));
- debug(0, __func__, "CM_SET_KEY.PID %d", confirm->PID);
- debug(0, __func__, "CM_SET_KEY.PRN %d", LE32TOH(confirm->PRN));
- debug(0, __func__, "CM_SET_KEY.PMN %d", confirm->PMN);
- debug(0, __func__, "CM_SET_KEY.CCoCAP %d", confirm->CCOCAP);
- }
- #endif
- return (0);
-
- }
- signed pev_cm_get_key_req(struct plc* plc, void* NID)
- {
- struct channel* channel = (struct channel*)(plc->channel);
- struct message* message = (struct message*)(plc->message);
- #ifndef __GNUC__
- #pragma pack(push,1)
- #endif
- struct __packed cm_get_key_request
- {
- struct ethernet_hdr ethernet;
- struct homeplug_fmi homeplug;
- uint8_t REQTYPE;
- uint8_t KEYTYPE;
- uint8_t NID[SLAC_NID_LEN];
- uint32_t MYNONCE;
- uint8_t PID;
- uint16_t PRN;
- uint8_t PMN;
- uint8_t HASHKEY[384];
- }
- *request = (struct cm_get_key_request*)(message);
- #ifndef __GNUC__
- #pragma pack (pop)
- #endif
- memset(message, 0, sizeof(*message));
- debug(0, __func__, "--> CM_GET_KEY.REQ");
- EthernetHeader(&request->ethernet, channel->peer, channel->host, channel->type);
- HomePlugHeader1(&request->homeplug, HOMEPLUG_MMV, (CM_GET_KEY | MMTYPE_REQ));
- request->REQTYPE = 0x00; //0x00 - Direct
- request->KEYTYPE = SLAC_CM_SETKEY_KEYTYPE; // keytype is 0x01 for NMK
- memcpy(request->NID, (uint8_t*)NID, sizeof(request->NID));
- memset(&request->MYNONCE, 0xAA, sizeof(request->MYNONCE));
- request->PID = SLAC_CM_SETKEY_PID;
- request->PRN = HTOLE16(SLAC_CM_SETKEY_PRN);
- request->PMN = SLAC_CM_SETKEY_PMN;
- if (sendpacket(channel, request, sizeof(*request)) <= 0)
- {
- return (debug(1, __func__, CHANNEL_CANTSEND));
- }
- return 0;
- }
- signed process_pev_cm_get_key_cnf(struct plc* plc, uint8_t* NMK, uint8_t* NID)
- {
- struct message* message = (struct message*)(plc->message);
- #ifndef __GNUC__
- #pragma pack(push,1)
- #endif
- struct __packed cm_get_key_confirm
- {
- struct ethernet_hdr ethernet;
- struct homeplug_fmi homeplug;
- uint8_t RESULT;
- uint8_t KEYTYPE;
- uint32_t MYNONCE;
- uint32_t YOURNONCE;
- uint8_t NID[SLAC_NID_LEN];
- uint8_t EKS;
- uint8_t PID;
- uint16_t PRN;
- uint8_t PMN;
- uint8_t NMK[SLAC_NMK_LEN];
- }
- *confirm = (struct cm_get_key_confirm*)(message);
- #ifndef __GNUC__
- #pragma pack (pop)
- #endif
- debug(0, __func__, "<-- CM_GET_KEY.CNF");
- if (!confirm->RESULT)
- {
- return (debug(1, __func__, "Can't get keys"));
- }
- if(!memcmp(confirm->NID, NID, SLAC_NID_LEN))
- {
- return (debug(1, __func__, "NID not matching"));
- }
- if((confirm->KEYTYPE != SLAC_CM_SETKEY_KEYTYPE) || (confirm->EKS != SLAC_CM_SETKEY_EKS))
- {
- printf("key type %0x , eks %0x", confirm->KEYTYPE, confirm->EKS);
- return (debug(1, __func__, "keytype or eks don't match"));
- }
- memcpy(confirm->NMK, NMK, SLAC_NMK_LEN);
-
- return 0;
- }
- #endif
|