static void
H_Init(HMAC_CTX *ctx)
{
- unsigned char allzero[SHA256_DIGEST_LENGTH];
+ uint8_t allzero[SHA256_DIGEST_LENGTH];
memset(allzero, 0, SHA256_DIGEST_LENGTH);
HMAC_Init(ctx, allzero, SHA256_DIGEST_LENGTH, EVP_sha256());
}
static void
-H_Update(HMAC_CTX *ctx, const unsigned char *data, int len)
+H_Update(HMAC_CTX *ctx, const uint8_t *data, int len)
{
HMAC_Update(ctx, data, len);
}
static void
-H_Final(HMAC_CTX *ctx, unsigned char *digest)
+H_Final(HMAC_CTX *ctx, uint8_t *digest)
{
unsigned int mdlen = SHA256_DIGEST_LENGTH;
/* a counter-based KDF based on NIST SP800-108 */
static void
-eap_pwd_kdf(unsigned char *key, int keylen, const char *label, int labellen,
- unsigned char *result, int resultbitlen)
+eap_pwd_kdf(uint8_t *key, int keylen, const char *label, int labellen,
+ uint8_t *result, int resultbitlen)
{
HMAC_CTX hctx;
- unsigned char digest[SHA256_DIGEST_LENGTH];
- unsigned short i, ctr, L;
+ uint8_t digest[SHA256_DIGEST_LENGTH];
+ uint16_t i, ctr, L;
int resultbytelen, len = 0;
unsigned int mdlen = SHA256_DIGEST_LENGTH;
- unsigned char mask = 0xff;
+ uint8_t mask = 0xff;
resultbytelen = (resultbitlen + 7)/8;
ctr = 0;
if (ctr > 1) {
HMAC_Update(&hctx, digest, mdlen);
}
- HMAC_Update(&hctx, (unsigned char *) &i, sizeof(unsigned short));
- HMAC_Update(&hctx, (const unsigned char *)label, labellen);
- HMAC_Update(&hctx, (unsigned char *) &L, sizeof(unsigned short));
+ HMAC_Update(&hctx, (uint8_t *) &i, sizeof(uint16_t));
+ HMAC_Update(&hctx, (const uint8_t *)label, labellen);
+ HMAC_Update(&hctx, (uint8_t *) &L, sizeof(uint16_t));
HMAC_Final(&hctx, digest, &mdlen);
if ((len + (int) mdlen) > resultbytelen) {
memcpy(result + len, digest, resultbytelen - len);
}
int
-compute_password_element (pwd_session_t *sess, unsigned short grp_num,
+compute_password_element (pwd_session_t *sess, uint16_t grp_num,
char *password, int password_len,
char *id_server, int id_server_len,
char *id_peer, int id_peer_len,
{
BIGNUM *x_candidate = NULL, *rnd = NULL, *cofactor = NULL;
HMAC_CTX ctx;
- unsigned char pwe_digest[SHA256_DIGEST_LENGTH], *prfbuf = NULL, ctr;
+ uint8_t pwe_digest[SHA256_DIGEST_LENGTH], *prfbuf = NULL, ctr;
int nid, is_odd, primebitlen, primebytelen, ret = 0;
switch (grp_num) { /* from IANA registry for IKE D-H groups */
* counter)
*/
H_Init(&ctx);
- H_Update(&ctx, (unsigned char *)token, sizeof(*token));
- H_Update(&ctx, (unsigned char *)id_peer, id_peer_len);
- H_Update(&ctx, (unsigned char *)id_server, id_server_len);
- H_Update(&ctx, (unsigned char *)password, password_len);
- H_Update(&ctx, (unsigned char *)&ctr, sizeof(ctr));
+ H_Update(&ctx, (uint8_t *)token, sizeof(*token));
+ H_Update(&ctx, (uint8_t *)id_peer, id_peer_len);
+ H_Update(&ctx, (uint8_t *)id_server, id_server_len);
+ H_Update(&ctx, (uint8_t *)password, password_len);
+ H_Update(&ctx, (uint8_t *)&ctr, sizeof(ctr));
H_Final(&ctx, pwe_digest);
BN_bin2bn(pwe_digest, SHA256_DIGEST_LENGTH, rnd);
}
int
-process_peer_commit (pwd_session_t *sess, unsigned char *commit, BN_CTX *bnctx)
+process_peer_commit (pwd_session_t *sess, uint8_t *commit, BN_CTX *bnctx)
{
- unsigned char *ptr;
+ uint8_t *ptr;
BIGNUM *x = NULL, *y = NULL, *cofactor = NULL;
EC_POINT *K = NULL, *point = NULL;
int res = 1;
}
/* element, x then y, followed by scalar */
- ptr = (unsigned char *)commit;
+ ptr = (uint8_t *)commit;
BN_bin2bn(ptr, BN_num_bytes(sess->prime), x);
ptr += BN_num_bytes(sess->prime);
BN_bin2bn(ptr, BN_num_bytes(sess->prime), y);
}
int
-compute_server_confirm (pwd_session_t *sess, unsigned char *buf, BN_CTX *bnctx)
+compute_server_confirm (pwd_session_t *sess, uint8_t *buf, BN_CTX *bnctx)
{
BIGNUM *x = NULL, *y = NULL;
HMAC_CTX ctx;
- unsigned char *cruft = NULL;
+ uint8_t *cruft = NULL;
int offset, req = -1;
/*
/*
* finally, ciphersuite
*/
- H_Update(&ctx, (unsigned char *)&sess->ciphersuite, sizeof(sess->ciphersuite));
+ H_Update(&ctx, (uint8_t *)&sess->ciphersuite, sizeof(sess->ciphersuite));
H_Final(&ctx, buf);
}
int
-compute_peer_confirm (pwd_session_t *sess, unsigned char *buf, BN_CTX *bnctx)
+compute_peer_confirm (pwd_session_t *sess, uint8_t *buf, BN_CTX *bnctx)
{
BIGNUM *x = NULL, *y = NULL;
HMAC_CTX ctx;
- unsigned char *cruft = NULL;
+ uint8_t *cruft = NULL;
int offset, req = -1;
/*
/*
* finally, ciphersuite
*/
- H_Update(&ctx, (unsigned char *)&sess->ciphersuite, sizeof(sess->ciphersuite));
+ H_Update(&ctx, (uint8_t *)&sess->ciphersuite, sizeof(sess->ciphersuite));
H_Final(&ctx, buf);
}
int
-compute_keys (pwd_session_t *sess, unsigned char *peer_confirm,
- unsigned char *msk, unsigned char *emsk)
+compute_keys (pwd_session_t *sess, uint8_t *peer_confirm,
+ uint8_t *msk, uint8_t *emsk)
{
HMAC_CTX ctx;
- unsigned char mk[SHA256_DIGEST_LENGTH], *cruft;
- unsigned char session_id[SHA256_DIGEST_LENGTH + 1];
- unsigned char msk_emsk[128]; /* 64 each */
+ uint8_t mk[SHA256_DIGEST_LENGTH], *cruft;
+ uint8_t session_id[SHA256_DIGEST_LENGTH + 1];
+ uint8_t msk_emsk[128]; /* 64 each */
int offset;
if ((cruft = malloc(BN_num_bytes(sess->prime))) == NULL) {
*/
session_id[0] = PW_EAP_PWD;
H_Init(&ctx);
- H_Update(&ctx, (unsigned char *)&sess->ciphersuite, sizeof(sess->ciphersuite));
+ H_Update(&ctx, (uint8_t *)&sess->ciphersuite, sizeof(sess->ciphersuite));
offset = BN_num_bytes(sess->order) - BN_num_bytes(sess->peer_scalar);
memset(cruft, 0, BN_num_bytes(sess->prime));
BN_bn2bin(sess->peer_scalar, cruft + offset);
memset(cruft, 0, BN_num_bytes(sess->prime));
BN_bn2bin(sess->my_scalar, cruft + offset);
H_Update(&ctx, cruft, BN_num_bytes(sess->order));
- H_Final(&ctx, (unsigned char *)&session_id[1]);
+ H_Final(&ctx, (uint8_t *)&session_id[1]);
/* then compute MK = H(k | commit-peer | commit-server) */
H_Init(&ctx);
#include <openssl/hmac.h>
typedef struct _pwd_hdr {
- unsigned char lm_exchange;
+ uint8_t lm_exchange;
#define EAP_PWD_EXCH_ID 1
#define EAP_PWD_EXCH_COMMIT 2
#define EAP_PWD_EXCH_CONFIRM 3
-// unsigned short total_length; /* there if the L-bit is set */
- unsigned char data[0];
+// uint16_t total_length; /* there if the L-bit is set */
+ uint8_t data[0];
} __attribute__ ((packed)) pwd_hdr;
#define EAP_PWD_GET_LENGTH_BIT(x) ((x)->lm_exchange & 0x80)
#define EAP_PWD_SET_EXCHANGE(x,y) ((x)->lm_exchange |= (y))
typedef struct _pwd_id_packet {
- unsigned short group_num;
- unsigned char random_function;
+ uint16_t group_num;
+ uint8_t random_function;
#define EAP_PWD_DEF_RAND_FUN 1
- unsigned char prf;
+ uint8_t prf;
#define EAP_PWD_DEF_PRF 1
- unsigned char token[4];
- unsigned char prep;
+ uint8_t token[4];
+ uint8_t prep;
#define EAP_PWD_PREP_NONE 0
#define EAP_PWD_PREP_MS 1
#define EAP_PWD_PREP_SASL 2
- unsigned char identity[0];
+ uint8_t identity[0];
} __attribute__ ((packed)) pwd_id_packet;
typedef struct _pwd_session_t {
- unsigned short state;
+ uint16_t state;
#define PWD_STATE_ID_REQ 1
#define PWD_STATE_COMMIT 2
#define PWD_STATE_CONFIRM 3
- unsigned short group_num;
+ uint16_t group_num;
uint32_t ciphersuite;
uint32_t token;
char peer_id[MAX_STRING_LEN];
int peer_id_len;
int mtu;
- unsigned char *in_buf; /* reassembled fragments */
+ uint8_t *in_buf; /* reassembled fragments */
int in_buf_pos;
int in_buf_len;
- unsigned char *out_buf; /* message to fragment */
+ uint8_t *out_buf; /* message to fragment */
int out_buf_pos;
int out_buf_len;
EC_GROUP *group;
BIGNUM *my_scalar;
EC_POINT *my_element;
EC_POINT *peer_element;
- unsigned char my_confirm[SHA256_DIGEST_LENGTH];
+ uint8_t my_confirm[SHA256_DIGEST_LENGTH];
} pwd_session_t;
-int compute_password_element(pwd_session_t *sess, unsigned short grp_num,
+int compute_password_element(pwd_session_t *sess, uint16_t grp_num,
char *password, int password_len,
char *id_server, int id_server_len,
char *id_peer, int id_peer_len,
uint32_t *token);
int compute_scalar_element(pwd_session_t *sess, BN_CTX *bnctx);
-int process_peer_commit (pwd_session_t *sess, unsigned char *commit, BN_CTX *bnctx);
-int compute_server_confirm(pwd_session_t *sess, unsigned char *buf, BN_CTX *bnctx);
-int compute_peer_confirm(pwd_session_t *sess, unsigned char *buf, BN_CTX *bnctx);
-int compute_keys(pwd_session_t *sess, unsigned char *peer_confirm,
- unsigned char *msk, unsigned char *emsk);
+int process_peer_commit (pwd_session_t *sess, uint8_t *commit, BN_CTX *bnctx);
+int compute_server_confirm(pwd_session_t *sess, uint8_t *buf, BN_CTX *bnctx);
+int compute_peer_confirm(pwd_session_t *sess, uint8_t *buf, BN_CTX *bnctx);
+int compute_keys(pwd_session_t *sess, uint8_t *peer_confirm,
+ uint8_t *msk, uint8_t *emsk);
#ifdef PRINTBUF
-void print_buf(char *str, unsigned char *buf, int len);
+void print_buf(char *str, uint8_t *buf, int len);
#endif /* PRINTBUF */
#endif /* _EAP_PWD_H */
send_pwd_request (pwd_session_t *sess, EAP_DS *eap_ds)
{
int len;
- unsigned short totlen;
+ uint16_t totlen;
pwd_hdr *hdr;
len = (sess->out_buf_len - sess->out_buf_pos) + sizeof(pwd_hdr);
EAP_PWD_SET_LENGTH_BIT(hdr);
totlen = ntohs(sess->out_buf_len);
memcpy(hdr->data, (char *)&totlen, sizeof(totlen));
- memcpy(hdr->data + sizeof(unsigned short),
+ memcpy(hdr->data + sizeof(uint16_t),
sess->out_buf,
- sess->mtu - sizeof(pwd_hdr) - sizeof(unsigned short));
- sess->out_buf_pos += (sess->mtu - sizeof(pwd_hdr) - sizeof(unsigned short));
+ sess->mtu - sizeof(pwd_hdr) - sizeof(uint16_t));
+ sess->out_buf_pos += (sess->mtu - sizeof(pwd_hdr) - sizeof(uint16_t));
} else {
/*
* an intermediate fragment
EAP_DS *eap_ds;
int len, ret = 0;
eap_pwd_t *inst = (eap_pwd_t *)arg;
- unsigned short offset;
- unsigned char exch, *buf, *ptr, msk[MSK_EMSK_LEN], emsk[MSK_EMSK_LEN];
- unsigned char peer_confirm[SHA256_DIGEST_LENGTH];
+ uint16_t offset;
+ uint8_t exch, *buf, *ptr, msk[MSK_EMSK_LEN], emsk[MSK_EMSK_LEN];
+ uint8_t peer_confirm[SHA256_DIGEST_LENGTH];
BIGNUM *x = NULL, *y = NULL;
if ((handler == NULL) ||
}
memset(pwd_session->in_buf, 0, pwd_session->in_buf_len);
pwd_session->in_buf_pos = 0;
- buf += sizeof(unsigned short);
- len -= sizeof(unsigned short);
+ buf += sizeof(uint16_t);
+ len -= sizeof(uint16_t);
}
/*
/*
* we've agreed on the ciphersuite, record it...
*/
- ptr = (unsigned char *)&pwd_session->ciphersuite;
- memcpy(ptr, (char *)&id->group_num, sizeof(unsigned short));
- ptr += sizeof(unsigned short);
+ ptr = (uint8_t *)&pwd_session->ciphersuite;
+ memcpy(ptr, (char *)&id->group_num, sizeof(uint16_t));
+ ptr += sizeof(uint16_t);
*ptr = EAP_PWD_DEF_RAND_FUN;
- ptr += sizeof(unsigned char);
+ ptr += sizeof(uint8_t);
*ptr = EAP_PWD_DEF_PRF;
pwd_session->peer_id_len = len - sizeof(pwd_id_packet);