Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | /*
* Copyright (c) 2019 Siddharth Chandrasekaran <siddharth@embedjournal.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <logging/log.h>
LOG_MODULE_DECLARE(osdp, CONFIG_OSDP_LOG_LEVEL);
#include <string.h>
#include "osdp_common.h"
#define TAG "SC: "
#define OSDP_SC_EOM_MARKER 0x80 /* End of Message Marker */
/* Default key as specified in OSDP specification */
static const uint8_t osdp_scbk_default[16] = {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F
};
void osdp_compute_scbk(struct osdp_pd *pd, uint8_t *scbk)
{
int i;
struct osdp *ctx = TO_CTX(pd);
memcpy(scbk, pd->sc.pd_client_uid, 8);
for (i = 8; i < 16; i++) {
scbk[i] = ~scbk[i - 8];
}
osdp_encrypt(ctx->sc_master_key, NULL, scbk, 16);
}
void osdp_compute_session_keys(struct osdp *ctx)
{
int i;
struct osdp_pd *pd = GET_CURRENT_PD(ctx);
if (ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD)) {
memcpy(pd->sc.scbk, osdp_scbk_default, 16);
} else {
/**
* Compute SCBK only in CP mode. PD mode, expect to already have
* the SCBK (sent from application layer).
*/
if (ISSET_FLAG(pd, PD_FLAG_PD_MODE) == 0) {
osdp_compute_scbk(pd, pd->sc.scbk);
}
}
memset(pd->sc.s_enc, 0, 16);
memset(pd->sc.s_mac1, 0, 16);
memset(pd->sc.s_mac2, 0, 16);
pd->sc.s_enc[0] = 0x01; pd->sc.s_enc[1] = 0x82;
pd->sc.s_mac1[0] = 0x01; pd->sc.s_mac1[1] = 0x01;
pd->sc.s_mac2[0] = 0x01; pd->sc.s_mac2[1] = 0x02;
for (i = 2; i < 8; i++) {
pd->sc.s_enc[i] = pd->sc.cp_random[i - 2];
pd->sc.s_mac1[i] = pd->sc.cp_random[i - 2];
pd->sc.s_mac2[i] = pd->sc.cp_random[i - 2];
}
osdp_encrypt(pd->sc.scbk, NULL, pd->sc.s_enc, 16);
osdp_encrypt(pd->sc.scbk, NULL, pd->sc.s_mac1, 16);
osdp_encrypt(pd->sc.scbk, NULL, pd->sc.s_mac2, 16);
}
void osdp_compute_cp_cryptogram(struct osdp_pd *pd)
{
/* cp_cryptogram = AES-ECB( pd_random[8] || cp_random[8], s_enc ) */
memcpy(pd->sc.cp_cryptogram + 0, pd->sc.pd_random, 8);
memcpy(pd->sc.cp_cryptogram + 8, pd->sc.cp_random, 8);
osdp_encrypt(pd->sc.s_enc, NULL, pd->sc.cp_cryptogram, 16);
}
/**
* Like memcmp; but operates at constant time.
*
* Returns 0 if memory pointed to by s1 and and s2 are identical; non-zero
* otherwise.
*/
static int osdp_ct_compare(const void *s1, const void *s2, size_t len)
{
size_t i, ret = 0;
const uint8_t *_s1 = s1;
const uint8_t *_s2 = s2;
for (i = 0; i < len; i++) {
ret |= _s1[i] ^ _s2[i];
}
return (int)ret;
}
int osdp_verify_cp_cryptogram(struct osdp_pd *pd)
{
uint8_t cp_crypto[16];
/* cp_cryptogram = AES-ECB( pd_random[8] || cp_random[8], s_enc ) */
memcpy(cp_crypto + 0, pd->sc.pd_random, 8);
memcpy(cp_crypto + 8, pd->sc.cp_random, 8);
osdp_encrypt(pd->sc.s_enc, NULL, cp_crypto, 16);
if (osdp_ct_compare(pd->sc.cp_cryptogram, cp_crypto, 16) != 0) {
return -1;
}
return 0;
}
void osdp_compute_pd_cryptogram(struct osdp_pd *pd)
{
/* pd_cryptogram = AES-ECB( cp_random[8] || pd_random[8], s_enc ) */
memcpy(pd->sc.pd_cryptogram + 0, pd->sc.cp_random, 8);
memcpy(pd->sc.pd_cryptogram + 8, pd->sc.pd_random, 8);
osdp_encrypt(pd->sc.s_enc, NULL, pd->sc.pd_cryptogram, 16);
}
int osdp_verify_pd_cryptogram(struct osdp_pd *pd)
{
uint8_t pd_crypto[16];
/* pd_cryptogram = AES-ECB( cp_random[8] || pd_random[8], s_enc ) */
memcpy(pd_crypto + 0, pd->sc.cp_random, 8);
memcpy(pd_crypto + 8, pd->sc.pd_random, 8);
osdp_encrypt(pd->sc.s_enc, NULL, pd_crypto, 16);
if (osdp_ct_compare(pd->sc.pd_cryptogram, pd_crypto, 16) != 0) {
return -1;
}
return 0;
}
void osdp_compute_rmac_i(struct osdp_pd *pd)
{
/* rmac_i = AES-ECB( AES-ECB( cp_cryptogram, s_mac1 ), s_mac2 ) */
memcpy(pd->sc.r_mac, pd->sc.cp_cryptogram, 16);
osdp_encrypt(pd->sc.s_mac1, NULL, pd->sc.r_mac, 16);
osdp_encrypt(pd->sc.s_mac2, NULL, pd->sc.r_mac, 16);
}
int osdp_decrypt_data(struct osdp_pd *pd, int is_cmd, uint8_t *data, int length)
{
int i;
uint8_t iv[16];
if (length % 16 != 0) {
LOG_ERR(TAG "decrypt_pkt invalid len:%d", length);
return -1;
}
memcpy(iv, is_cmd ? pd->sc.r_mac : pd->sc.c_mac, 16);
for (i = 0; i < 16; i++) {
iv[i] = ~iv[i];
}
osdp_decrypt(pd->sc.s_enc, iv, data, length);
while (data[length - 1] == 0x00) {
length--;
}
if (data[length - 1] != OSDP_SC_EOM_MARKER) {
return -1;
}
data[length - 1] = 0;
return length - 1;
}
int osdp_encrypt_data(struct osdp_pd *pd, int is_cmd, uint8_t *data, int length)
{
int i, pad_len;
uint8_t iv[16];
data[length] = OSDP_SC_EOM_MARKER; /* append EOM marker */
pad_len = AES_PAD_LEN(length + 1);
if ((pad_len - length - 1) > 0) {
memset(data + length + 1, 0, pad_len - length - 1);
}
memcpy(iv, is_cmd ? pd->sc.r_mac : pd->sc.c_mac, 16);
for (i = 0; i < 16; i++) {
iv[i] = ~iv[i];
}
osdp_encrypt(pd->sc.s_enc, iv, data, pad_len);
return pad_len;
}
int osdp_compute_mac(struct osdp_pd *pd, int is_cmd,
const uint8_t *data, int len)
{
int pad_len;
uint8_t buf[CONFIG_OSDP_UART_BUFFER_LENGTH] = { 0 };
uint8_t iv[16];
memcpy(buf, data, len);
pad_len = (len % 16 == 0) ? len : AES_PAD_LEN(len);
if (len % 16 != 0) {
buf[len] = 0x80; /* end marker */
}
/**
* MAC for data blocks B[1] .. B[N] (post padding) is computed as:
* IV1 = R_MAC (or) C_MAC -- depending on is_cmd
* IV2 = B[N-1] after -- AES-CBC ( IV1, B[1] to B[N-1], SMAC-1 )
* MAC = AES-ECB ( IV2, B[N], SMAC-2 )
*/
memcpy(iv, is_cmd ? pd->sc.r_mac : pd->sc.c_mac, 16);
if (pad_len > 16) {
/* N-1 blocks -- encrypted with SMAC-1 */
osdp_encrypt(pd->sc.s_mac1, iv, buf, pad_len - 16);
/* N-1 th block is the IV for N th block */
memcpy(iv, buf + pad_len - 32, 16);
}
/* N-th Block encrypted with SMAC-2 == MAC */
osdp_encrypt(pd->sc.s_mac2, iv, buf + pad_len - 16, 16);
memcpy(is_cmd ? pd->sc.c_mac : pd->sc.r_mac, buf + pad_len - 16, 16);
return 0;
}
void osdp_sc_init(struct osdp_pd *pd)
{
uint8_t key[16];
if (ISSET_FLAG(pd, PD_FLAG_PD_MODE)) {
memcpy(key, pd->sc.scbk, 16);
}
memset(&pd->sc, 0, sizeof(struct osdp_secure_channel));
if (ISSET_FLAG(pd, PD_FLAG_PD_MODE)) {
memcpy(pd->sc.scbk, key, 16);
}
if (ISSET_FLAG(pd, PD_FLAG_PD_MODE)) {
pd->sc.pd_client_uid[0] = BYTE_0(pd->id.vendor_code);
pd->sc.pd_client_uid[1] = BYTE_1(pd->id.vendor_code);
pd->sc.pd_client_uid[2] = BYTE_0(pd->id.model);
pd->sc.pd_client_uid[3] = BYTE_1(pd->id.version);
pd->sc.pd_client_uid[4] = BYTE_0(pd->id.serial_number);
pd->sc.pd_client_uid[5] = BYTE_1(pd->id.serial_number);
pd->sc.pd_client_uid[6] = BYTE_2(pd->id.serial_number);
pd->sc.pd_client_uid[7] = BYTE_3(pd->id.serial_number);
}
}
|