| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
#include "eap_v4_crypto.h" |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#if HAVE_MBEDTLS |
| 16 |
#include "mbedtls/md.h" |
| 17 |
#include "mbedtls/gcm.h" |
| 18 |
#elif HAVE_NETTLE |
| 19 |
#include <nettle/hmac.h> |
| 20 |
#include <nettle/gcm.h> |
| 21 |
#include <nettle/memops.h> |
| 22 |
#include <nettle/version.h> |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#if NETTLE_VERSION_MAJOR >= 4 |
| 28 |
static void rist_hmac_sha256_digest(struct hmac_sha256_ctx *ctx, size_t len, uint8_t *out) |
| 29 |
{ |
| 30 |
uint8_t full[SHA256_DIGEST_SIZE]; |
| 31 |
hmac_sha256_digest(ctx, full); |
| 32 |
memcpy(out, full, len < sizeof(full) ? len : sizeof(full)); |
| 33 |
_librist_crypto_secure_zero(full, sizeof(full)); |
| 34 |
} |
| 35 |
|
| 36 |
static void rist_gcm_aes256_digest(struct gcm_aes256_ctx *ctx, size_t len, uint8_t *out) |
| 37 |
{ |
| 38 |
uint8_t full[GCM_DIGEST_SIZE]; |
| 39 |
gcm_aes256_digest(ctx, full); |
| 40 |
memcpy(out, full, len < sizeof(full) ? len : sizeof(full)); |
| 41 |
} |
| 42 |
#else |
| 43 |
#define rist_hmac_sha256_digest hmac_sha256_digest |
| 44 |
#define rist_gcm_aes256_digest gcm_aes256_digest |
| 45 |
#endif |
| 46 |
#endif |
| 47 |
|
| 48 |
int _librist_crypto_hkdf_expand_sha256(const uint8_t *prk, size_t prk_len, |
| 49 |
const uint8_t *info, size_t info_len, |
| 50 |
uint8_t *okm, size_t okm_len) |
| 51 |
{ |
| 52 |
const size_t hash_len = 32; |
| 53 |
if (okm_len == 0 || okm_len > 255 * hash_len) |
| 54 |
return -1; |
| 55 |
#if HAVE_MBEDTLS |
| 56 |
const mbedtls_md_info_t *md = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); |
| 57 |
if (md == NULL) |
| 58 |
return -1; |
| 59 |
mbedtls_md_context_t ctx; |
| 60 |
mbedtls_md_init(&ctx); |
| 61 |
int rc = -1; |
| 62 |
if (mbedtls_md_setup(&ctx, md, 1) != 0) |
| 63 |
goto done; |
| 64 |
uint8_t t[32]; |
| 65 |
size_t t_len = 0; |
| 66 |
size_t done_len = 0; |
| 67 |
uint8_t counter = 0; |
| 68 |
while (done_len < okm_len) { |
| 69 |
counter++; |
| 70 |
if (mbedtls_md_hmac_starts(&ctx, prk, prk_len) != 0) goto done; |
| 71 |
if (t_len && mbedtls_md_hmac_update(&ctx, t, t_len) != 0) goto done; |
| 72 |
if (info_len && mbedtls_md_hmac_update(&ctx, info, info_len) != 0) goto done; |
| 73 |
if (mbedtls_md_hmac_update(&ctx, &counter, 1) != 0) goto done; |
| 74 |
if (mbedtls_md_hmac_finish(&ctx, t) != 0) goto done; |
| 75 |
t_len = hash_len; |
| 76 |
size_t n = okm_len - done_len < hash_len ? okm_len - done_len : hash_len; |
| 77 |
memcpy(okm + done_len, t, n); |
| 78 |
done_len += n; |
| 79 |
} |
| 80 |
rc = 0; |
| 81 |
done: |
| 82 |
_librist_crypto_secure_zero(t, sizeof(t)); |
| 83 |
mbedtls_md_free(&ctx); |
| 84 |
if (rc != 0) |
| 85 |
memset(okm, 0, okm_len); |
| 86 |
return rc; |
| 87 |
#elif HAVE_NETTLE |
| 88 |
struct hmac_sha256_ctx ctx; |
| 89 |
hmac_sha256_set_key(&ctx, prk_len, prk); |
| 90 |
uint8_t t[32]; |
| 91 |
size_t t_len = 0; |
| 92 |
size_t done_len = 0; |
| 93 |
uint8_t counter = 0; |
| 94 |
while (done_len < okm_len) { |
| 95 |
counter++; |
| 96 |
if (t_len) hmac_sha256_update(&ctx, t_len, t); |
| 97 |
if (info_len) hmac_sha256_update(&ctx, info_len, info); |
| 98 |
hmac_sha256_update(&ctx, 1, &counter); |
| 99 |
rist_hmac_sha256_digest(&ctx, hash_len, t); |
| 100 |
t_len = hash_len; |
| 101 |
size_t n = okm_len - done_len < hash_len ? okm_len - done_len : hash_len; |
| 102 |
memcpy(okm + done_len, t, n); |
| 103 |
done_len += n; |
| 104 |
} |
| 105 |
_librist_crypto_secure_zero(t, sizeof(t)); |
| 106 |
_librist_crypto_secure_zero(&ctx, sizeof(ctx)); |
| 107 |
return 0; |
| 108 |
#else |
| 109 |
(void)prk; (void)prk_len; (void)info; (void)info_len; |
| 110 |
memset(okm, 0, okm_len); |
| 111 |
return -1; |
| 112 |
#endif |
| 113 |
} |
| 114 |
|
| 115 |
int _librist_crypto_aes_gcm_encrypt(const uint8_t *key, const uint8_t *iv, size_t iv_len, |
| 116 |
const uint8_t *aad, size_t aad_len, |
| 117 |
const uint8_t *pt, size_t pt_len, |
| 118 |
uint8_t *ct, uint8_t *tag, size_t tag_len) |
| 119 |
{ |
| 120 |
#if HAVE_MBEDTLS |
| 121 |
mbedtls_gcm_context ctx; |
| 122 |
mbedtls_gcm_init(&ctx); |
| 123 |
int rc = -1; |
| 124 |
if (mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, 256) != 0) |
| 125 |
goto done; |
| 126 |
if (mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, pt_len, iv, iv_len, |
| 127 |
aad, aad_len, pt, ct, tag_len, tag) != 0) |
| 128 |
goto done; |
| 129 |
rc = 0; |
| 130 |
done: |
| 131 |
mbedtls_gcm_free(&ctx); |
| 132 |
return rc; |
| 133 |
#elif HAVE_NETTLE |
| 134 |
struct gcm_aes256_ctx ctx; |
| 135 |
gcm_aes256_set_key(&ctx, key); |
| 136 |
gcm_aes256_set_iv(&ctx, iv_len, iv); |
| 137 |
if (aad_len) |
| 138 |
gcm_aes256_update(&ctx, aad_len, aad); |
| 139 |
gcm_aes256_encrypt(&ctx, pt_len, ct, pt); |
| 140 |
rist_gcm_aes256_digest(&ctx, tag_len, tag); |
| 141 |
_librist_crypto_secure_zero(&ctx, sizeof(ctx)); |
| 142 |
return 0; |
| 143 |
#else |
| 144 |
(void)key; (void)iv; (void)iv_len; (void)aad; (void)aad_len; |
| 145 |
(void)pt; (void)pt_len; (void)ct; (void)tag; (void)tag_len; |
| 146 |
return -1; |
| 147 |
#endif |
| 148 |
} |
| 149 |
|
| 150 |
int _librist_crypto_aes_gcm_decrypt(const uint8_t *key, const uint8_t *iv, size_t iv_len, |
| 151 |
const uint8_t *aad, size_t aad_len, |
| 152 |
const uint8_t *ct, size_t ct_len, |
| 153 |
const uint8_t *tag, size_t tag_len, |
| 154 |
uint8_t *pt) |
| 155 |
{ |
| 156 |
#if HAVE_MBEDTLS |
| 157 |
mbedtls_gcm_context ctx; |
| 158 |
mbedtls_gcm_init(&ctx); |
| 159 |
int rc = -1; |
| 160 |
if (mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, 256) != 0) |
| 161 |
goto done; |
| 162 |
if (mbedtls_gcm_auth_decrypt(&ctx, ct_len, iv, iv_len, aad, aad_len, |
| 163 |
tag, tag_len, ct, pt) != 0) |
| 164 |
goto done; |
| 165 |
rc = 0; |
| 166 |
done: |
| 167 |
mbedtls_gcm_free(&ctx); |
| 168 |
if (rc != 0 && ct_len) |
| 169 |
memset(pt, 0, ct_len); |
| 170 |
return rc; |
| 171 |
#elif HAVE_NETTLE |
| 172 |
struct gcm_aes256_ctx ctx; |
| 173 |
uint8_t local_tag[16]; |
| 174 |
if (tag_len > sizeof(local_tag)) |
| 175 |
return -1; |
| 176 |
gcm_aes256_set_key(&ctx, key); |
| 177 |
gcm_aes256_set_iv(&ctx, iv_len, iv); |
| 178 |
if (aad_len) |
| 179 |
gcm_aes256_update(&ctx, aad_len, aad); |
| 180 |
gcm_aes256_decrypt(&ctx, ct_len, pt, ct); |
| 181 |
rist_gcm_aes256_digest(&ctx, tag_len, local_tag); |
| 182 |
|
| 183 |
if (memeql_sec(local_tag, tag, tag_len) != 1) { |
| 184 |
if (ct_len) |
| 185 |
memset(pt, 0, ct_len); |
| 186 |
_librist_crypto_secure_zero(&ctx, sizeof(ctx)); |
| 187 |
return -1; |
| 188 |
} |
| 189 |
_librist_crypto_secure_zero(&ctx, sizeof(ctx)); |
| 190 |
return 0; |
| 191 |
#else |
| 192 |
(void)key; (void)iv; (void)iv_len; (void)aad; (void)aad_len; |
| 193 |
(void)ct; (void)tag; (void)tag_len; |
| 194 |
if (ct_len) |
| 195 |
memset(pt, 0, ct_len); |
| 196 |
return -1; |
| 197 |
#endif |
| 198 |
} |