diff options
author | jsing <> | 2014-05-15 13:53:47 +0000 |
---|---|---|
committer | jsing <> | 2014-05-15 13:53:47 +0000 |
commit | 913c052f3efbb5dc0f2b7866824da20593470b34 (patch) | |
tree | fd0906417d1eccc2db6e54f415726272f17bb826 | |
parent | c7bee58156162d7faa5269011e51ac838785e058 (diff) | |
download | openbsd-913c052f3efbb5dc0f2b7866824da20593470b34.tar.gz openbsd-913c052f3efbb5dc0f2b7866824da20593470b34.tar.bz2 openbsd-913c052f3efbb5dc0f2b7866824da20593470b34.zip |
Add an AEAD EVP interface to libcrypto, along with AES-GCM AEAD
implementations. This largely pulls in Adam Langley's AEAD patches from
Chromium's OpenSSL.
ok miod@
-rw-r--r-- | src/lib/libcrypto/crypto/Makefile | 4 | ||||
-rw-r--r-- | src/lib/libcrypto/evp/e_aes.c | 246 | ||||
-rw-r--r-- | src/lib/libcrypto/evp/evp.h | 114 | ||||
-rw-r--r-- | src/lib/libcrypto/evp/evp_aead.c | 192 | ||||
-rw-r--r-- | src/lib/libcrypto/evp/evp_err.c | 8 | ||||
-rw-r--r-- | src/lib/libcrypto/evp/evp_locl.h | 22 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/evp/e_aes.c | 246 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/evp/evp.h | 114 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/evp/evp_aead.c | 192 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/evp/evp_err.c | 8 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/evp/evp_locl.h | 22 |
11 files changed, 1100 insertions, 68 deletions
diff --git a/src/lib/libcrypto/crypto/Makefile b/src/lib/libcrypto/crypto/Makefile index 6d9eac1383..7e1f497223 100644 --- a/src/lib/libcrypto/crypto/Makefile +++ b/src/lib/libcrypto/crypto/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.33 2014/05/14 14:46:35 jsing Exp $ | 1 | # $OpenBSD: Makefile,v 1.34 2014/05/15 13:53:46 jsing Exp $ |
2 | 2 | ||
3 | LIB= crypto | 3 | LIB= crypto |
4 | 4 | ||
@@ -152,7 +152,7 @@ SRCS+= c_all.c c_allc.c c_alld.c evp_lib.c bio_ok.c | |||
152 | SRCS+= evp_pkey.c evp_pbe.c p5_crpt.c p5_crpt2.c | 152 | SRCS+= evp_pkey.c evp_pbe.c p5_crpt.c p5_crpt2.c |
153 | SRCS+= e_old.c pmeth_lib.c pmeth_fn.c pmeth_gn.c m_sigver.c | 153 | SRCS+= e_old.c pmeth_lib.c pmeth_fn.c pmeth_gn.c m_sigver.c |
154 | SRCS+= e_aes_cbc_hmac_sha1.c e_rc4_hmac_md5.c | 154 | SRCS+= e_aes_cbc_hmac_sha1.c e_rc4_hmac_md5.c |
155 | SRCS+= e_chacha.c | 155 | SRCS+= e_chacha.c evp_aead.c |
156 | 156 | ||
157 | # hmac/ | 157 | # hmac/ |
158 | SRCS+= hmac.c hm_ameth.c hm_pmeth.c | 158 | SRCS+= hmac.c hm_ameth.c hm_pmeth.c |
diff --git a/src/lib/libcrypto/evp/e_aes.c b/src/lib/libcrypto/evp/e_aes.c index fd1c34526f..e4d9457c96 100644 --- a/src/lib/libcrypto/evp/e_aes.c +++ b/src/lib/libcrypto/evp/e_aes.c | |||
@@ -786,48 +786,46 @@ aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
786 | } | 786 | } |
787 | } | 787 | } |
788 | 788 | ||
789 | static int | 789 | static ctr128_f |
790 | aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 790 | aes_gcm_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx, |
791 | const unsigned char *iv, int enc) | 791 | const unsigned char *key, size_t key_len) |
792 | { | 792 | { |
793 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; | ||
794 | |||
795 | if (!iv && !key) | ||
796 | return 1; | ||
797 | if (key) { | ||
798 | do { | ||
799 | #ifdef BSAES_CAPABLE | 793 | #ifdef BSAES_CAPABLE |
800 | if (BSAES_CAPABLE) { | 794 | if (BSAES_CAPABLE) { |
801 | AES_set_encrypt_key(key, ctx->key_len * 8, | 795 | AES_set_encrypt_key(key, key_len * 8, aes_key); |
802 | &gctx->ks); | 796 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); |
803 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, | 797 | return (ctr128_f)bsaes_ctr32_encrypt_blocks; |
804 | (block128_f)AES_encrypt); | 798 | } else |
805 | gctx->ctr = | ||
806 | (ctr128_f)bsaes_ctr32_encrypt_blocks; | ||
807 | break; | ||
808 | } else | ||
809 | #endif | 799 | #endif |
810 | #ifdef VPAES_CAPABLE | 800 | #ifdef VPAES_CAPABLE |
811 | if (VPAES_CAPABLE) { | 801 | if (VPAES_CAPABLE) { |
812 | vpaes_set_encrypt_key(key, ctx->key_len * 8, | 802 | vpaes_set_encrypt_key(key, key_len * 8, aes_key); |
813 | &gctx->ks); | 803 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)vpaes_encrypt); |
814 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, | 804 | return NULL; |
815 | (block128_f)vpaes_encrypt); | 805 | } else |
816 | gctx->ctr = NULL; | ||
817 | break; | ||
818 | } else | ||
819 | #endif | 806 | #endif |
820 | (void)0; /* terminate potentially open 'else' */ | 807 | (void)0; /* terminate potentially open 'else' */ |
821 | 808 | ||
822 | AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); | 809 | AES_set_encrypt_key(key, key_len * 8, aes_key); |
823 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, | 810 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); |
824 | (block128_f)AES_encrypt); | ||
825 | #ifdef AES_CTR_ASM | 811 | #ifdef AES_CTR_ASM |
826 | gctx->ctr = (ctr128_f)AES_ctr32_encrypt; | 812 | return (ctr128_f)AES_ctr32_encrypt; |
827 | #else | 813 | #else |
828 | gctx->ctr = NULL; | 814 | return NULL; |
829 | #endif | 815 | #endif |
830 | } while (0); | 816 | } |
817 | |||
818 | static int | ||
819 | aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | ||
820 | const unsigned char *iv, int enc) | ||
821 | { | ||
822 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; | ||
823 | |||
824 | if (!iv && !key) | ||
825 | return 1; | ||
826 | if (key) { | ||
827 | gctx->ctr = aes_gcm_set_key(&gctx->ks, &gctx->gcm, | ||
828 | key, ctx->key_len); | ||
831 | 829 | ||
832 | /* If we have an iv can set it directly, otherwise use | 830 | /* If we have an iv can set it directly, otherwise use |
833 | * saved IV. | 831 | * saved IV. |
@@ -1263,4 +1261,186 @@ BLOCK_CIPHER_custom(NID_aes, 192, 1,12, ccm, CCM, | |||
1263 | BLOCK_CIPHER_custom(NID_aes, 256, 1,12, ccm, CCM, | 1261 | BLOCK_CIPHER_custom(NID_aes, 256, 1,12, ccm, CCM, |
1264 | EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) | 1262 | EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) |
1265 | 1263 | ||
1264 | #define EVP_AEAD_AES_GCM_TAG_LEN 16 | ||
1265 | |||
1266 | struct aead_aes_gcm_ctx { | ||
1267 | union { | ||
1268 | double align; | ||
1269 | AES_KEY ks; | ||
1270 | } ks; | ||
1271 | GCM128_CONTEXT gcm; | ||
1272 | ctr128_f ctr; | ||
1273 | unsigned char tag_len; | ||
1274 | }; | ||
1275 | |||
1276 | static int | ||
1277 | aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const unsigned char *key, size_t key_len, | ||
1278 | size_t tag_len) | ||
1279 | { | ||
1280 | struct aead_aes_gcm_ctx *gcm_ctx; | ||
1281 | const size_t key_bits = key_len * 8; | ||
1282 | |||
1283 | if (key_bits != 128 && key_bits != 256) { | ||
1284 | EVPerr(EVP_F_AEAD_AES_GCM_INIT, EVP_R_BAD_KEY_LENGTH); | ||
1285 | return 0; /* EVP_AEAD_CTX_init should catch this. */ | ||
1286 | } | ||
1287 | |||
1288 | if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) | ||
1289 | tag_len = EVP_AEAD_AES_GCM_TAG_LEN; | ||
1290 | |||
1291 | if (tag_len > EVP_AEAD_AES_GCM_TAG_LEN) { | ||
1292 | EVPerr(EVP_F_AEAD_AES_GCM_INIT, EVP_R_TAG_TOO_LARGE); | ||
1293 | return 0; | ||
1294 | } | ||
1295 | |||
1296 | gcm_ctx = OPENSSL_malloc(sizeof(struct aead_aes_gcm_ctx)); | ||
1297 | if (gcm_ctx == NULL) | ||
1298 | return 0; | ||
1299 | |||
1300 | #ifdef AESNI_CAPABLE | ||
1301 | if (AESNI_CAPABLE) { | ||
1302 | aesni_set_encrypt_key(key, key_bits, &gcm_ctx->ks.ks); | ||
1303 | CRYPTO_gcm128_init(&gcm_ctx->gcm, &gcm_ctx->ks.ks, | ||
1304 | (block128_f)aesni_encrypt); | ||
1305 | gcm_ctx->ctr = (ctr128_f) aesni_ctr32_encrypt_blocks; | ||
1306 | } else | ||
1307 | #endif | ||
1308 | { | ||
1309 | gcm_ctx->ctr = aes_gcm_set_key(&gcm_ctx->ks.ks, &gcm_ctx->gcm, | ||
1310 | key, key_len); | ||
1311 | } | ||
1312 | gcm_ctx->tag_len = tag_len; | ||
1313 | ctx->aead_state = gcm_ctx; | ||
1314 | |||
1315 | return 1; | ||
1316 | } | ||
1317 | |||
1318 | static void | ||
1319 | aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) | ||
1320 | { | ||
1321 | struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; | ||
1322 | |||
1323 | OPENSSL_free(gcm_ctx); | ||
1324 | } | ||
1325 | |||
1326 | static ssize_t | ||
1327 | aead_aes_gcm_seal(const EVP_AEAD_CTX *ctx, unsigned char *out, | ||
1328 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
1329 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
1330 | size_t ad_len) | ||
1331 | { | ||
1332 | size_t bulk = 0; | ||
1333 | const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; | ||
1334 | GCM128_CONTEXT gcm; | ||
1335 | |||
1336 | if (max_out_len < in_len + gcm_ctx->tag_len) { | ||
1337 | EVPerr(EVP_F_AEAD_AES_GCM_SEAL, EVP_R_BUFFER_TOO_SMALL); | ||
1338 | return -1; | ||
1339 | } | ||
1340 | |||
1341 | memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm)); | ||
1342 | CRYPTO_gcm128_setiv(&gcm, nonce, nonce_len); | ||
1343 | |||
1344 | if (ad_len > 0 && CRYPTO_gcm128_aad(&gcm, ad, ad_len)) | ||
1345 | return -1; | ||
1346 | |||
1347 | if (gcm_ctx->ctr) { | ||
1348 | if (CRYPTO_gcm128_encrypt_ctr32(&gcm, in + bulk, out + bulk, | ||
1349 | in_len - bulk, gcm_ctx->ctr)) | ||
1350 | return -1; | ||
1351 | } else { | ||
1352 | if (CRYPTO_gcm128_encrypt(&gcm, in + bulk, out + bulk, | ||
1353 | in_len - bulk)) | ||
1354 | return -1; | ||
1355 | } | ||
1356 | |||
1357 | CRYPTO_gcm128_tag(&gcm, out + in_len, gcm_ctx->tag_len); | ||
1358 | return in_len + gcm_ctx->tag_len; | ||
1359 | } | ||
1360 | |||
1361 | static ssize_t | ||
1362 | aead_aes_gcm_open(const EVP_AEAD_CTX *ctx, unsigned char *out, | ||
1363 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
1364 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
1365 | size_t ad_len) | ||
1366 | { | ||
1367 | size_t bulk = 0; | ||
1368 | const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; | ||
1369 | unsigned char tag[EVP_AEAD_AES_GCM_TAG_LEN]; | ||
1370 | size_t out_len; | ||
1371 | GCM128_CONTEXT gcm; | ||
1372 | |||
1373 | if (in_len < gcm_ctx->tag_len) { | ||
1374 | EVPerr(EVP_F_AEAD_AES_GCM_OPEN, EVP_R_BAD_DECRYPT); | ||
1375 | return -1; | ||
1376 | } | ||
1377 | |||
1378 | out_len = in_len - gcm_ctx->tag_len; | ||
1379 | |||
1380 | if (max_out_len < out_len) { | ||
1381 | EVPerr(EVP_F_AEAD_AES_GCM_OPEN, EVP_R_BUFFER_TOO_SMALL); | ||
1382 | return -1; | ||
1383 | } | ||
1384 | |||
1385 | memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm)); | ||
1386 | CRYPTO_gcm128_setiv(&gcm, nonce, nonce_len); | ||
1387 | |||
1388 | if (CRYPTO_gcm128_aad(&gcm, ad, ad_len)) | ||
1389 | return -1; | ||
1390 | |||
1391 | if (gcm_ctx->ctr) { | ||
1392 | if (CRYPTO_gcm128_decrypt_ctr32(&gcm, in + bulk, out + bulk, | ||
1393 | in_len-bulk-gcm_ctx->tag_len, gcm_ctx->ctr)) | ||
1394 | return -1; | ||
1395 | } else { | ||
1396 | if (CRYPTO_gcm128_decrypt(&gcm, in + bulk, out + bulk, | ||
1397 | in_len - bulk - gcm_ctx->tag_len)) | ||
1398 | return -1; | ||
1399 | } | ||
1400 | |||
1401 | CRYPTO_gcm128_tag(&gcm, tag, gcm_ctx->tag_len); | ||
1402 | if (CRYPTO_memcmp(tag, in + out_len, gcm_ctx->tag_len) != 0) { | ||
1403 | EVPerr(EVP_F_AEAD_AES_GCM_OPEN, EVP_R_BAD_DECRYPT); | ||
1404 | return -1; | ||
1405 | } | ||
1406 | |||
1407 | return out_len; | ||
1408 | } | ||
1409 | |||
1410 | static const EVP_AEAD aead_aes_128_gcm = { | ||
1411 | .key_len = 16, | ||
1412 | .nonce_len = 12, | ||
1413 | .overhead = EVP_AEAD_AES_GCM_TAG_LEN, | ||
1414 | .max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN, | ||
1415 | |||
1416 | .init = aead_aes_gcm_init, | ||
1417 | .cleanup = aead_aes_gcm_cleanup, | ||
1418 | .seal = aead_aes_gcm_seal, | ||
1419 | .open = aead_aes_gcm_open, | ||
1420 | }; | ||
1421 | |||
1422 | static const EVP_AEAD aead_aes_256_gcm = { | ||
1423 | .key_len = 32, | ||
1424 | .nonce_len = 12, | ||
1425 | .overhead = EVP_AEAD_AES_GCM_TAG_LEN, | ||
1426 | .max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN, | ||
1427 | |||
1428 | .init = aead_aes_gcm_init, | ||
1429 | .cleanup = aead_aes_gcm_cleanup, | ||
1430 | .seal = aead_aes_gcm_seal, | ||
1431 | .open = aead_aes_gcm_open, | ||
1432 | }; | ||
1433 | |||
1434 | const EVP_AEAD * | ||
1435 | EVP_aead_aes_128_gcm(void) | ||
1436 | { | ||
1437 | return &aead_aes_128_gcm; | ||
1438 | } | ||
1439 | |||
1440 | const EVP_AEAD * | ||
1441 | EVP_aead_aes_256_gcm(void) | ||
1442 | { | ||
1443 | return &aead_aes_256_gcm; | ||
1444 | } | ||
1445 | |||
1266 | #endif | 1446 | #endif |
diff --git a/src/lib/libcrypto/evp/evp.h b/src/lib/libcrypto/evp/evp.h index 54aa8a4a66..f8395fbe7b 100644 --- a/src/lib/libcrypto/evp/evp.h +++ b/src/lib/libcrypto/evp/evp.h | |||
@@ -1205,6 +1205,110 @@ void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, | |||
1205 | int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2), | 1205 | int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2), |
1206 | int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value)); | 1206 | int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value)); |
1207 | 1207 | ||
1208 | /* Authenticated Encryption with Additional Data. | ||
1209 | * | ||
1210 | * AEAD couples confidentiality and integrity in a single primtive. AEAD | ||
1211 | * algorithms take a key and then can seal and open individual messages. Each | ||
1212 | * message has a unique, per-message nonce and, optionally, additional data | ||
1213 | * which is authenticated but not included in the output. */ | ||
1214 | |||
1215 | struct evp_aead_st; | ||
1216 | typedef struct evp_aead_st EVP_AEAD; | ||
1217 | |||
1218 | #ifndef OPENSSL_NO_AES | ||
1219 | /* EVP_aes_128_gcm is AES-128 in Galois Counter Mode. */ | ||
1220 | const EVP_AEAD *EVP_aead_aes_128_gcm(void); | ||
1221 | /* EVP_aes_256_gcm is AES-256 in Galois Counter Mode. */ | ||
1222 | const EVP_AEAD *EVP_aead_aes_256_gcm(void); | ||
1223 | #endif | ||
1224 | |||
1225 | /* EVP_AEAD_key_length returns the length of the keys used. */ | ||
1226 | size_t EVP_AEAD_key_length(const EVP_AEAD *aead); | ||
1227 | |||
1228 | /* EVP_AEAD_nonce_length returns the length of the per-message nonce. */ | ||
1229 | size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead); | ||
1230 | |||
1231 | /* EVP_AEAD_max_overhead returns the maximum number of additional bytes added | ||
1232 | * by the act of sealing data with the AEAD. */ | ||
1233 | size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead); | ||
1234 | |||
1235 | /* EVP_AEAD_max_tag_len returns the maximum tag length when using this AEAD. | ||
1236 | * This * is the largest value that can be passed as a tag length to | ||
1237 | * EVP_AEAD_CTX_init. */ | ||
1238 | size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead); | ||
1239 | |||
1240 | /* An EVP_AEAD_CTX represents an AEAD algorithm configured with a specific key | ||
1241 | * and message-independent IV. */ | ||
1242 | typedef struct evp_aead_ctx_st { | ||
1243 | const EVP_AEAD *aead; | ||
1244 | /* aead_state is an opaque pointer to the AEAD specific state. */ | ||
1245 | void *aead_state; | ||
1246 | } EVP_AEAD_CTX; | ||
1247 | |||
1248 | /* EVP_AEAD_MAX_TAG_LENGTH is the maximum tag length used by any AEAD | ||
1249 | * defined in this header. */ | ||
1250 | #define EVP_AEAD_MAX_TAG_LENGTH 16 | ||
1251 | |||
1252 | /* EVP_AEAD_DEFAULT_TAG_LENGTH is a magic value that can be passed to | ||
1253 | * EVP_AEAD_CTX_init to indicate that the default tag length for an AEAD | ||
1254 | * should be used. */ | ||
1255 | #define EVP_AEAD_DEFAULT_TAG_LENGTH 0 | ||
1256 | |||
1257 | /* EVP_AEAD_init initializes the context for the given AEAD algorithm. | ||
1258 | * The implementation argument may be NULL to choose the default implementation. | ||
1259 | * Authentication tags may be truncated by passing a tag length. A tag length | ||
1260 | * of zero indicates the default tag length should be used. */ | ||
1261 | int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, | ||
1262 | const unsigned char *key, size_t key_len, size_t tag_len, ENGINE *impl); | ||
1263 | |||
1264 | /* EVP_AEAD_CTX_cleanup frees any data allocated for this context. */ | ||
1265 | void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx); | ||
1266 | |||
1267 | /* EVP_AEAD_CTX_seal encrypts and authenticates the input and authenticates | ||
1268 | * any additional data (AD). The result is written as output, with the number | ||
1269 | * of bytes written being returned, or -1 on error. | ||
1270 | * | ||
1271 | * This function may be called (with the same EVP_AEAD_CTX) concurrently with | ||
1272 | * itself or EVP_AEAD_CTX_open. | ||
1273 | * | ||
1274 | * At most max_out_len bytes are written as output and, in order to ensure | ||
1275 | * success, this value should be the length of the input plus the result of | ||
1276 | * EVP_AEAD_overhead. | ||
1277 | * | ||
1278 | * The length of the nonce is must be equal to the result of | ||
1279 | * EVP_AEAD_nonce_length for this AEAD. | ||
1280 | * | ||
1281 | * EVP_AEAD_CTX_seal never results in a partial output. If max_out_len is | ||
1282 | * insufficient, -1 will be returned. | ||
1283 | * | ||
1284 | * If the input and output are aliased then out must be <= in. */ | ||
1285 | ssize_t EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, unsigned char *out, | ||
1286 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
1287 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
1288 | size_t ad_len); | ||
1289 | |||
1290 | /* EVP_AEAD_CTX_open authenticates the input and additional data, decrypting | ||
1291 | * the input and writing it as output. The number of bytes decrypted and | ||
1292 | * written as output is returned, or -1 on error. | ||
1293 | * | ||
1294 | * This function may be called (with the same EVP_AEAD_CTX) concurrently with | ||
1295 | * itself or EVP_AEAD_CTX_seal. | ||
1296 | * | ||
1297 | * At most the number of input bytes are written as output. In order to ensure | ||
1298 | * success, max_out_len should be at least the same as the input length. | ||
1299 | * | ||
1300 | * The length of nonce must be equal to the result of EVP_AEAD_nonce_length | ||
1301 | * for this AEAD. | ||
1302 | * | ||
1303 | * EVP_AEAD_CTX_open never results in a partial output. If max_out_len is | ||
1304 | * insufficient, -1 will be returned. | ||
1305 | * | ||
1306 | * If the input and output are aliased then out must be <= in. */ | ||
1307 | ssize_t EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, unsigned char *out, | ||
1308 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
1309 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
1310 | size_t ad_len); | ||
1311 | |||
1208 | void EVP_add_alg_module(void); | 1312 | void EVP_add_alg_module(void); |
1209 | 1313 | ||
1210 | /* BEGIN ERROR CODES */ | 1314 | /* BEGIN ERROR CODES */ |
@@ -1216,6 +1320,11 @@ void ERR_load_EVP_strings(void); | |||
1216 | /* Error codes for the EVP functions. */ | 1320 | /* Error codes for the EVP functions. */ |
1217 | 1321 | ||
1218 | /* Function codes. */ | 1322 | /* Function codes. */ |
1323 | #define EVP_F_AEAD_AES_GCM_INIT 187 | ||
1324 | #define EVP_F_AEAD_AES_GCM_OPEN 188 | ||
1325 | #define EVP_F_AEAD_AES_GCM_SEAL 189 | ||
1326 | #define EVP_F_AEAD_CTX_OPEN 185 | ||
1327 | #define EVP_F_AEAD_CTX_SEAL 186 | ||
1219 | #define EVP_F_AESNI_INIT_KEY 165 | 1328 | #define EVP_F_AESNI_INIT_KEY 165 |
1220 | #define EVP_F_AESNI_XTS_CIPHER 176 | 1329 | #define EVP_F_AESNI_XTS_CIPHER 176 |
1221 | #define EVP_F_AES_INIT_KEY 133 | 1330 | #define EVP_F_AES_INIT_KEY 133 |
@@ -1230,6 +1339,9 @@ void ERR_load_EVP_strings(void); | |||
1230 | #define EVP_F_DSA_PKEY2PKCS8 135 | 1339 | #define EVP_F_DSA_PKEY2PKCS8 135 |
1231 | #define EVP_F_ECDSA_PKEY2PKCS8 129 | 1340 | #define EVP_F_ECDSA_PKEY2PKCS8 129 |
1232 | #define EVP_F_ECKEY_PKEY2PKCS8 132 | 1341 | #define EVP_F_ECKEY_PKEY2PKCS8 132 |
1342 | #define EVP_F_EVP_AEAD_CTX_INIT 180 | ||
1343 | #define EVP_F_EVP_AEAD_CTX_OPEN 190 | ||
1344 | #define EVP_F_EVP_AEAD_CTX_SEAL 191 | ||
1233 | #define EVP_F_EVP_CIPHERINIT_EX 123 | 1345 | #define EVP_F_EVP_CIPHERINIT_EX 123 |
1234 | #define EVP_F_EVP_CIPHER_CTX_COPY 163 | 1346 | #define EVP_F_EVP_CIPHER_CTX_COPY 163 |
1235 | #define EVP_F_EVP_CIPHER_CTX_CTRL 124 | 1347 | #define EVP_F_EVP_CIPHER_CTX_CTRL 124 |
@@ -1345,10 +1457,12 @@ void ERR_load_EVP_strings(void); | |||
1345 | #define EVP_R_NO_VERIFY_FUNCTION_CONFIGURED 105 | 1457 | #define EVP_R_NO_VERIFY_FUNCTION_CONFIGURED 105 |
1346 | #define EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 150 | 1458 | #define EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 150 |
1347 | #define EVP_R_OPERATON_NOT_INITIALIZED 151 | 1459 | #define EVP_R_OPERATON_NOT_INITIALIZED 151 |
1460 | #define EVP_R_OUTPUT_ALIASES_INPUT 172 | ||
1348 | #define EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE 117 | 1461 | #define EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE 117 |
1349 | #define EVP_R_PRIVATE_KEY_DECODE_ERROR 145 | 1462 | #define EVP_R_PRIVATE_KEY_DECODE_ERROR 145 |
1350 | #define EVP_R_PRIVATE_KEY_ENCODE_ERROR 146 | 1463 | #define EVP_R_PRIVATE_KEY_ENCODE_ERROR 146 |
1351 | #define EVP_R_PUBLIC_KEY_NOT_RSA 106 | 1464 | #define EVP_R_PUBLIC_KEY_NOT_RSA 106 |
1465 | #define EVP_R_TAG_TOO_LARGE 171 | ||
1352 | #define EVP_R_TOO_LARGE 164 | 1466 | #define EVP_R_TOO_LARGE 164 |
1353 | #define EVP_R_UNKNOWN_CIPHER 160 | 1467 | #define EVP_R_UNKNOWN_CIPHER 160 |
1354 | #define EVP_R_UNKNOWN_DIGEST 161 | 1468 | #define EVP_R_UNKNOWN_DIGEST 161 |
diff --git a/src/lib/libcrypto/evp/evp_aead.c b/src/lib/libcrypto/evp/evp_aead.c new file mode 100644 index 0000000000..137e3dd05b --- /dev/null +++ b/src/lib/libcrypto/evp/evp_aead.c | |||
@@ -0,0 +1,192 @@ | |||
1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
2 | * All rights reserved. | ||
3 | * | ||
4 | * This package is an SSL implementation written | ||
5 | * by Eric Young (eay@cryptsoft.com). | ||
6 | * The implementation was written so as to conform with Netscapes SSL. | ||
7 | * | ||
8 | * This library is free for commercial and non-commercial use as long as | ||
9 | * the following conditions are aheared to. The following conditions | ||
10 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
12 | * included with this distribution is covered by the same copyright terms | ||
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
14 | * | ||
15 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
16 | * the code are not to be removed. | ||
17 | * If this package is used in a product, Eric Young should be given attribution | ||
18 | * as the author of the parts of the library used. | ||
19 | * This can be in the form of a textual message at program startup or | ||
20 | * in documentation (online or textual) provided with the package. | ||
21 | * | ||
22 | * Redistribution and use in source and binary forms, with or without | ||
23 | * modification, are permitted provided that the following conditions | ||
24 | * are met: | ||
25 | * 1. Redistributions of source code must retain the copyright | ||
26 | * notice, this list of conditions and the following disclaimer. | ||
27 | * 2. Redistributions in binary form must reproduce the above copyright | ||
28 | * notice, this list of conditions and the following disclaimer in the | ||
29 | * documentation and/or other materials provided with the distribution. | ||
30 | * 3. All advertising materials mentioning features or use of this software | ||
31 | * must display the following acknowledgement: | ||
32 | * "This product includes cryptographic software written by | ||
33 | * Eric Young (eay@cryptsoft.com)" | ||
34 | * The word 'cryptographic' can be left out if the rouines from the library | ||
35 | * being used are not cryptographic related :-). | ||
36 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
37 | * the apps directory (application code) you must include an acknowledgement: | ||
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
39 | * | ||
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
50 | * SUCH DAMAGE. | ||
51 | * | ||
52 | * The licence and distribution terms for any publically available version or | ||
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
54 | * copied and put under another distribution licence | ||
55 | * [including the GNU Public Licence.] | ||
56 | */ | ||
57 | |||
58 | #include <limits.h> | ||
59 | #include <string.h> | ||
60 | |||
61 | #include <openssl/evp.h> | ||
62 | #include <openssl/err.h> | ||
63 | |||
64 | #include "evp_locl.h" | ||
65 | |||
66 | size_t EVP_AEAD_key_length(const EVP_AEAD *aead) | ||
67 | { | ||
68 | return aead->key_len; | ||
69 | } | ||
70 | |||
71 | size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead) | ||
72 | { | ||
73 | return aead->nonce_len; | ||
74 | } | ||
75 | |||
76 | size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead) | ||
77 | { | ||
78 | return aead->overhead; | ||
79 | } | ||
80 | |||
81 | size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead) | ||
82 | { | ||
83 | return aead->max_tag_len; | ||
84 | } | ||
85 | |||
86 | int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, | ||
87 | const unsigned char *key, size_t key_len, | ||
88 | size_t tag_len, ENGINE *impl) | ||
89 | { | ||
90 | ctx->aead = aead; | ||
91 | if (key_len != aead->key_len) | ||
92 | { | ||
93 | EVPerr(EVP_F_EVP_AEAD_CTX_INIT,EVP_R_UNSUPPORTED_KEY_SIZE); | ||
94 | return 0; | ||
95 | } | ||
96 | return aead->init(ctx, key, key_len, tag_len); | ||
97 | } | ||
98 | |||
99 | void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx) | ||
100 | { | ||
101 | if (ctx->aead == NULL) | ||
102 | return; | ||
103 | ctx->aead->cleanup(ctx); | ||
104 | ctx->aead = NULL; | ||
105 | } | ||
106 | |||
107 | /* check_alias returns 0 if out points within the buffer determined by in | ||
108 | * and in_len and 1 otherwise. | ||
109 | * | ||
110 | * When processing, there's only an issue if out points within in[:in_len] | ||
111 | * and isn't equal to in. If that's the case then writing the output will | ||
112 | * stomp input that hasn't been read yet. | ||
113 | * | ||
114 | * This function checks for that case. */ | ||
115 | static int check_alias(const unsigned char *in, size_t in_len, | ||
116 | const unsigned char *out) | ||
117 | { | ||
118 | if (out <= in) | ||
119 | return 1; | ||
120 | if (in + in_len <= out) | ||
121 | return 1; | ||
122 | return 0; | ||
123 | } | ||
124 | |||
125 | ssize_t EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, | ||
126 | unsigned char *out, size_t max_out_len, | ||
127 | const unsigned char *nonce, size_t nonce_len, | ||
128 | const unsigned char *in, size_t in_len, | ||
129 | const unsigned char *ad, size_t ad_len) | ||
130 | { | ||
131 | size_t possible_out_len = in_len + ctx->aead->overhead; | ||
132 | ssize_t r; | ||
133 | |||
134 | if (possible_out_len < in_len /* overflow */ || | ||
135 | possible_out_len > SSIZE_MAX /* return value cannot be | ||
136 | represented */) | ||
137 | { | ||
138 | EVPerr(EVP_F_EVP_AEAD_CTX_SEAL, EVP_R_TOO_LARGE); | ||
139 | goto error; | ||
140 | } | ||
141 | |||
142 | if (!check_alias(in, in_len, out)) | ||
143 | { | ||
144 | EVPerr(EVP_F_EVP_AEAD_CTX_SEAL, EVP_R_OUTPUT_ALIASES_INPUT); | ||
145 | goto error; | ||
146 | } | ||
147 | |||
148 | r = ctx->aead->seal(ctx, out, max_out_len, nonce, nonce_len, | ||
149 | in, in_len, ad, ad_len); | ||
150 | if (r >= 0) | ||
151 | return r; | ||
152 | |||
153 | error: | ||
154 | /* In the event of an error, clear the output buffer so that a caller | ||
155 | * that doesn't check the return value doesn't send raw data. */ | ||
156 | memset(out, 0, max_out_len); | ||
157 | return -1; | ||
158 | } | ||
159 | |||
160 | ssize_t EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, | ||
161 | unsigned char *out, size_t max_out_len, | ||
162 | const unsigned char *nonce, size_t nonce_len, | ||
163 | const unsigned char *in, size_t in_len, | ||
164 | const unsigned char *ad, size_t ad_len) | ||
165 | { | ||
166 | ssize_t r; | ||
167 | |||
168 | if (in_len > SSIZE_MAX) | ||
169 | { | ||
170 | EVPerr(EVP_F_EVP_AEAD_CTX_OPEN, EVP_R_TOO_LARGE); | ||
171 | goto error; /* may not be able to represent return value. */ | ||
172 | } | ||
173 | |||
174 | if (!check_alias(in, in_len, out)) | ||
175 | { | ||
176 | EVPerr(EVP_F_EVP_AEAD_CTX_OPEN, EVP_R_OUTPUT_ALIASES_INPUT); | ||
177 | goto error; | ||
178 | } | ||
179 | |||
180 | r = ctx->aead->open(ctx, out, max_out_len, nonce, nonce_len, | ||
181 | in, in_len, ad, ad_len); | ||
182 | |||
183 | if (r >= 0) | ||
184 | return r; | ||
185 | |||
186 | error: | ||
187 | /* In the event of an error, clear the output buffer so that a caller | ||
188 | * that doesn't check the return value doesn't try and process bad | ||
189 | * data. */ | ||
190 | memset(out, 0, max_out_len); | ||
191 | return -1; | ||
192 | } | ||
diff --git a/src/lib/libcrypto/evp/evp_err.c b/src/lib/libcrypto/evp/evp_err.c index 8024731938..9391036d0f 100644 --- a/src/lib/libcrypto/evp/evp_err.c +++ b/src/lib/libcrypto/evp/evp_err.c | |||
@@ -69,6 +69,9 @@ | |||
69 | #define ERR_REASON(reason) ERR_PACK(ERR_LIB_EVP,0,reason) | 69 | #define ERR_REASON(reason) ERR_PACK(ERR_LIB_EVP,0,reason) |
70 | 70 | ||
71 | static ERR_STRING_DATA EVP_str_functs[] = { | 71 | static ERR_STRING_DATA EVP_str_functs[] = { |
72 | {ERR_FUNC(EVP_F_AEAD_AES_GCM_INIT), "AEAD_AES_GCM_INIT"}, | ||
73 | {ERR_FUNC(EVP_F_AEAD_AES_GCM_OPEN), "AEAD_AES_GCM_OPEN"}, | ||
74 | {ERR_FUNC(EVP_F_AEAD_AES_GCM_SEAL), "AEAD_AES_GCM_SEAL"}, | ||
72 | {ERR_FUNC(EVP_F_AESNI_INIT_KEY), "AESNI_INIT_KEY"}, | 75 | {ERR_FUNC(EVP_F_AESNI_INIT_KEY), "AESNI_INIT_KEY"}, |
73 | {ERR_FUNC(EVP_F_AESNI_XTS_CIPHER), "AESNI_XTS_CIPHER"}, | 76 | {ERR_FUNC(EVP_F_AESNI_XTS_CIPHER), "AESNI_XTS_CIPHER"}, |
74 | {ERR_FUNC(EVP_F_AES_INIT_KEY), "AES_INIT_KEY"}, | 77 | {ERR_FUNC(EVP_F_AES_INIT_KEY), "AES_INIT_KEY"}, |
@@ -83,6 +86,9 @@ static ERR_STRING_DATA EVP_str_functs[] = { | |||
83 | {ERR_FUNC(EVP_F_DSA_PKEY2PKCS8), "DSA_PKEY2PKCS8"}, | 86 | {ERR_FUNC(EVP_F_DSA_PKEY2PKCS8), "DSA_PKEY2PKCS8"}, |
84 | {ERR_FUNC(EVP_F_ECDSA_PKEY2PKCS8), "ECDSA_PKEY2PKCS8"}, | 87 | {ERR_FUNC(EVP_F_ECDSA_PKEY2PKCS8), "ECDSA_PKEY2PKCS8"}, |
85 | {ERR_FUNC(EVP_F_ECKEY_PKEY2PKCS8), "ECKEY_PKEY2PKCS8"}, | 88 | {ERR_FUNC(EVP_F_ECKEY_PKEY2PKCS8), "ECKEY_PKEY2PKCS8"}, |
89 | {ERR_FUNC(EVP_F_EVP_AEAD_CTX_INIT), "EVP_AEAD_CTX_init"}, | ||
90 | {ERR_FUNC(EVP_F_EVP_AEAD_CTX_OPEN), "EVP_AEAD_CTX_open"}, | ||
91 | {ERR_FUNC(EVP_F_EVP_AEAD_CTX_SEAL), "EVP_AEAD_CTX_seal"}, | ||
86 | {ERR_FUNC(EVP_F_EVP_CIPHERINIT_EX), "EVP_CipherInit_ex"}, | 92 | {ERR_FUNC(EVP_F_EVP_CIPHERINIT_EX), "EVP_CipherInit_ex"}, |
87 | {ERR_FUNC(EVP_F_EVP_CIPHER_CTX_COPY), "EVP_CIPHER_CTX_copy"}, | 93 | {ERR_FUNC(EVP_F_EVP_CIPHER_CTX_COPY), "EVP_CIPHER_CTX_copy"}, |
88 | {ERR_FUNC(EVP_F_EVP_CIPHER_CTX_CTRL), "EVP_CIPHER_CTX_ctrl"}, | 94 | {ERR_FUNC(EVP_F_EVP_CIPHER_CTX_CTRL), "EVP_CIPHER_CTX_ctrl"}, |
@@ -200,10 +206,12 @@ static ERR_STRING_DATA EVP_str_reasons[] = { | |||
200 | {ERR_REASON(EVP_R_NO_VERIFY_FUNCTION_CONFIGURED), "no verify function configured"}, | 206 | {ERR_REASON(EVP_R_NO_VERIFY_FUNCTION_CONFIGURED), "no verify function configured"}, |
201 | {ERR_REASON(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE), "operation not supported for this keytype"}, | 207 | {ERR_REASON(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE), "operation not supported for this keytype"}, |
202 | {ERR_REASON(EVP_R_OPERATON_NOT_INITIALIZED), "operaton not initialized"}, | 208 | {ERR_REASON(EVP_R_OPERATON_NOT_INITIALIZED), "operaton not initialized"}, |
209 | {ERR_REASON(EVP_R_OUTPUT_ALIASES_INPUT), "output aliases input"}, | ||
203 | {ERR_REASON(EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE), "pkcs8 unknown broken type"}, | 210 | {ERR_REASON(EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE), "pkcs8 unknown broken type"}, |
204 | {ERR_REASON(EVP_R_PRIVATE_KEY_DECODE_ERROR), "private key decode error"}, | 211 | {ERR_REASON(EVP_R_PRIVATE_KEY_DECODE_ERROR), "private key decode error"}, |
205 | {ERR_REASON(EVP_R_PRIVATE_KEY_ENCODE_ERROR), "private key encode error"}, | 212 | {ERR_REASON(EVP_R_PRIVATE_KEY_ENCODE_ERROR), "private key encode error"}, |
206 | {ERR_REASON(EVP_R_PUBLIC_KEY_NOT_RSA) , "public key not rsa"}, | 213 | {ERR_REASON(EVP_R_PUBLIC_KEY_NOT_RSA) , "public key not rsa"}, |
214 | {ERR_REASON(EVP_R_TAG_TOO_LARGE), "tag too large"}, | ||
207 | {ERR_REASON(EVP_R_TOO_LARGE) , "too large"}, | 215 | {ERR_REASON(EVP_R_TOO_LARGE) , "too large"}, |
208 | {ERR_REASON(EVP_R_UNKNOWN_CIPHER) , "unknown cipher"}, | 216 | {ERR_REASON(EVP_R_UNKNOWN_CIPHER) , "unknown cipher"}, |
209 | {ERR_REASON(EVP_R_UNKNOWN_DIGEST) , "unknown digest"}, | 217 | {ERR_REASON(EVP_R_UNKNOWN_DIGEST) , "unknown digest"}, |
diff --git a/src/lib/libcrypto/evp/evp_locl.h b/src/lib/libcrypto/evp/evp_locl.h index 673c85f8bd..6f9218eafc 100644 --- a/src/lib/libcrypto/evp/evp_locl.h +++ b/src/lib/libcrypto/evp/evp_locl.h | |||
@@ -342,3 +342,25 @@ void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx); | |||
342 | 342 | ||
343 | int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, | 343 | int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, |
344 | ASN1_TYPE *param, const EVP_CIPHER *c, const EVP_MD *md, int en_de); | 344 | ASN1_TYPE *param, const EVP_CIPHER *c, const EVP_MD *md, int en_de); |
345 | |||
346 | /* EVP_AEAD represents a specific AEAD algorithm. */ | ||
347 | struct evp_aead_st { | ||
348 | unsigned char key_len; | ||
349 | unsigned char nonce_len; | ||
350 | unsigned char overhead; | ||
351 | unsigned char max_tag_len; | ||
352 | |||
353 | int (*init)(struct evp_aead_ctx_st*, const unsigned char *key, | ||
354 | size_t key_len, size_t tag_len); | ||
355 | void (*cleanup)(struct evp_aead_ctx_st*); | ||
356 | |||
357 | ssize_t (*seal)(const struct evp_aead_ctx_st *ctx, unsigned char *out, | ||
358 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
359 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
360 | size_t ad_len); | ||
361 | |||
362 | ssize_t (*open)(const struct evp_aead_ctx_st *ctx, unsigned char *out, | ||
363 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
364 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
365 | size_t ad_len); | ||
366 | }; | ||
diff --git a/src/lib/libssl/src/crypto/evp/e_aes.c b/src/lib/libssl/src/crypto/evp/e_aes.c index fd1c34526f..e4d9457c96 100644 --- a/src/lib/libssl/src/crypto/evp/e_aes.c +++ b/src/lib/libssl/src/crypto/evp/e_aes.c | |||
@@ -786,48 +786,46 @@ aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
786 | } | 786 | } |
787 | } | 787 | } |
788 | 788 | ||
789 | static int | 789 | static ctr128_f |
790 | aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 790 | aes_gcm_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx, |
791 | const unsigned char *iv, int enc) | 791 | const unsigned char *key, size_t key_len) |
792 | { | 792 | { |
793 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; | ||
794 | |||
795 | if (!iv && !key) | ||
796 | return 1; | ||
797 | if (key) { | ||
798 | do { | ||
799 | #ifdef BSAES_CAPABLE | 793 | #ifdef BSAES_CAPABLE |
800 | if (BSAES_CAPABLE) { | 794 | if (BSAES_CAPABLE) { |
801 | AES_set_encrypt_key(key, ctx->key_len * 8, | 795 | AES_set_encrypt_key(key, key_len * 8, aes_key); |
802 | &gctx->ks); | 796 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); |
803 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, | 797 | return (ctr128_f)bsaes_ctr32_encrypt_blocks; |
804 | (block128_f)AES_encrypt); | 798 | } else |
805 | gctx->ctr = | ||
806 | (ctr128_f)bsaes_ctr32_encrypt_blocks; | ||
807 | break; | ||
808 | } else | ||
809 | #endif | 799 | #endif |
810 | #ifdef VPAES_CAPABLE | 800 | #ifdef VPAES_CAPABLE |
811 | if (VPAES_CAPABLE) { | 801 | if (VPAES_CAPABLE) { |
812 | vpaes_set_encrypt_key(key, ctx->key_len * 8, | 802 | vpaes_set_encrypt_key(key, key_len * 8, aes_key); |
813 | &gctx->ks); | 803 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)vpaes_encrypt); |
814 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, | 804 | return NULL; |
815 | (block128_f)vpaes_encrypt); | 805 | } else |
816 | gctx->ctr = NULL; | ||
817 | break; | ||
818 | } else | ||
819 | #endif | 806 | #endif |
820 | (void)0; /* terminate potentially open 'else' */ | 807 | (void)0; /* terminate potentially open 'else' */ |
821 | 808 | ||
822 | AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); | 809 | AES_set_encrypt_key(key, key_len * 8, aes_key); |
823 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, | 810 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); |
824 | (block128_f)AES_encrypt); | ||
825 | #ifdef AES_CTR_ASM | 811 | #ifdef AES_CTR_ASM |
826 | gctx->ctr = (ctr128_f)AES_ctr32_encrypt; | 812 | return (ctr128_f)AES_ctr32_encrypt; |
827 | #else | 813 | #else |
828 | gctx->ctr = NULL; | 814 | return NULL; |
829 | #endif | 815 | #endif |
830 | } while (0); | 816 | } |
817 | |||
818 | static int | ||
819 | aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | ||
820 | const unsigned char *iv, int enc) | ||
821 | { | ||
822 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; | ||
823 | |||
824 | if (!iv && !key) | ||
825 | return 1; | ||
826 | if (key) { | ||
827 | gctx->ctr = aes_gcm_set_key(&gctx->ks, &gctx->gcm, | ||
828 | key, ctx->key_len); | ||
831 | 829 | ||
832 | /* If we have an iv can set it directly, otherwise use | 830 | /* If we have an iv can set it directly, otherwise use |
833 | * saved IV. | 831 | * saved IV. |
@@ -1263,4 +1261,186 @@ BLOCK_CIPHER_custom(NID_aes, 192, 1,12, ccm, CCM, | |||
1263 | BLOCK_CIPHER_custom(NID_aes, 256, 1,12, ccm, CCM, | 1261 | BLOCK_CIPHER_custom(NID_aes, 256, 1,12, ccm, CCM, |
1264 | EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) | 1262 | EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) |
1265 | 1263 | ||
1264 | #define EVP_AEAD_AES_GCM_TAG_LEN 16 | ||
1265 | |||
1266 | struct aead_aes_gcm_ctx { | ||
1267 | union { | ||
1268 | double align; | ||
1269 | AES_KEY ks; | ||
1270 | } ks; | ||
1271 | GCM128_CONTEXT gcm; | ||
1272 | ctr128_f ctr; | ||
1273 | unsigned char tag_len; | ||
1274 | }; | ||
1275 | |||
1276 | static int | ||
1277 | aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const unsigned char *key, size_t key_len, | ||
1278 | size_t tag_len) | ||
1279 | { | ||
1280 | struct aead_aes_gcm_ctx *gcm_ctx; | ||
1281 | const size_t key_bits = key_len * 8; | ||
1282 | |||
1283 | if (key_bits != 128 && key_bits != 256) { | ||
1284 | EVPerr(EVP_F_AEAD_AES_GCM_INIT, EVP_R_BAD_KEY_LENGTH); | ||
1285 | return 0; /* EVP_AEAD_CTX_init should catch this. */ | ||
1286 | } | ||
1287 | |||
1288 | if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) | ||
1289 | tag_len = EVP_AEAD_AES_GCM_TAG_LEN; | ||
1290 | |||
1291 | if (tag_len > EVP_AEAD_AES_GCM_TAG_LEN) { | ||
1292 | EVPerr(EVP_F_AEAD_AES_GCM_INIT, EVP_R_TAG_TOO_LARGE); | ||
1293 | return 0; | ||
1294 | } | ||
1295 | |||
1296 | gcm_ctx = OPENSSL_malloc(sizeof(struct aead_aes_gcm_ctx)); | ||
1297 | if (gcm_ctx == NULL) | ||
1298 | return 0; | ||
1299 | |||
1300 | #ifdef AESNI_CAPABLE | ||
1301 | if (AESNI_CAPABLE) { | ||
1302 | aesni_set_encrypt_key(key, key_bits, &gcm_ctx->ks.ks); | ||
1303 | CRYPTO_gcm128_init(&gcm_ctx->gcm, &gcm_ctx->ks.ks, | ||
1304 | (block128_f)aesni_encrypt); | ||
1305 | gcm_ctx->ctr = (ctr128_f) aesni_ctr32_encrypt_blocks; | ||
1306 | } else | ||
1307 | #endif | ||
1308 | { | ||
1309 | gcm_ctx->ctr = aes_gcm_set_key(&gcm_ctx->ks.ks, &gcm_ctx->gcm, | ||
1310 | key, key_len); | ||
1311 | } | ||
1312 | gcm_ctx->tag_len = tag_len; | ||
1313 | ctx->aead_state = gcm_ctx; | ||
1314 | |||
1315 | return 1; | ||
1316 | } | ||
1317 | |||
1318 | static void | ||
1319 | aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) | ||
1320 | { | ||
1321 | struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; | ||
1322 | |||
1323 | OPENSSL_free(gcm_ctx); | ||
1324 | } | ||
1325 | |||
1326 | static ssize_t | ||
1327 | aead_aes_gcm_seal(const EVP_AEAD_CTX *ctx, unsigned char *out, | ||
1328 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
1329 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
1330 | size_t ad_len) | ||
1331 | { | ||
1332 | size_t bulk = 0; | ||
1333 | const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; | ||
1334 | GCM128_CONTEXT gcm; | ||
1335 | |||
1336 | if (max_out_len < in_len + gcm_ctx->tag_len) { | ||
1337 | EVPerr(EVP_F_AEAD_AES_GCM_SEAL, EVP_R_BUFFER_TOO_SMALL); | ||
1338 | return -1; | ||
1339 | } | ||
1340 | |||
1341 | memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm)); | ||
1342 | CRYPTO_gcm128_setiv(&gcm, nonce, nonce_len); | ||
1343 | |||
1344 | if (ad_len > 0 && CRYPTO_gcm128_aad(&gcm, ad, ad_len)) | ||
1345 | return -1; | ||
1346 | |||
1347 | if (gcm_ctx->ctr) { | ||
1348 | if (CRYPTO_gcm128_encrypt_ctr32(&gcm, in + bulk, out + bulk, | ||
1349 | in_len - bulk, gcm_ctx->ctr)) | ||
1350 | return -1; | ||
1351 | } else { | ||
1352 | if (CRYPTO_gcm128_encrypt(&gcm, in + bulk, out + bulk, | ||
1353 | in_len - bulk)) | ||
1354 | return -1; | ||
1355 | } | ||
1356 | |||
1357 | CRYPTO_gcm128_tag(&gcm, out + in_len, gcm_ctx->tag_len); | ||
1358 | return in_len + gcm_ctx->tag_len; | ||
1359 | } | ||
1360 | |||
1361 | static ssize_t | ||
1362 | aead_aes_gcm_open(const EVP_AEAD_CTX *ctx, unsigned char *out, | ||
1363 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
1364 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
1365 | size_t ad_len) | ||
1366 | { | ||
1367 | size_t bulk = 0; | ||
1368 | const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; | ||
1369 | unsigned char tag[EVP_AEAD_AES_GCM_TAG_LEN]; | ||
1370 | size_t out_len; | ||
1371 | GCM128_CONTEXT gcm; | ||
1372 | |||
1373 | if (in_len < gcm_ctx->tag_len) { | ||
1374 | EVPerr(EVP_F_AEAD_AES_GCM_OPEN, EVP_R_BAD_DECRYPT); | ||
1375 | return -1; | ||
1376 | } | ||
1377 | |||
1378 | out_len = in_len - gcm_ctx->tag_len; | ||
1379 | |||
1380 | if (max_out_len < out_len) { | ||
1381 | EVPerr(EVP_F_AEAD_AES_GCM_OPEN, EVP_R_BUFFER_TOO_SMALL); | ||
1382 | return -1; | ||
1383 | } | ||
1384 | |||
1385 | memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm)); | ||
1386 | CRYPTO_gcm128_setiv(&gcm, nonce, nonce_len); | ||
1387 | |||
1388 | if (CRYPTO_gcm128_aad(&gcm, ad, ad_len)) | ||
1389 | return -1; | ||
1390 | |||
1391 | if (gcm_ctx->ctr) { | ||
1392 | if (CRYPTO_gcm128_decrypt_ctr32(&gcm, in + bulk, out + bulk, | ||
1393 | in_len-bulk-gcm_ctx->tag_len, gcm_ctx->ctr)) | ||
1394 | return -1; | ||
1395 | } else { | ||
1396 | if (CRYPTO_gcm128_decrypt(&gcm, in + bulk, out + bulk, | ||
1397 | in_len - bulk - gcm_ctx->tag_len)) | ||
1398 | return -1; | ||
1399 | } | ||
1400 | |||
1401 | CRYPTO_gcm128_tag(&gcm, tag, gcm_ctx->tag_len); | ||
1402 | if (CRYPTO_memcmp(tag, in + out_len, gcm_ctx->tag_len) != 0) { | ||
1403 | EVPerr(EVP_F_AEAD_AES_GCM_OPEN, EVP_R_BAD_DECRYPT); | ||
1404 | return -1; | ||
1405 | } | ||
1406 | |||
1407 | return out_len; | ||
1408 | } | ||
1409 | |||
1410 | static const EVP_AEAD aead_aes_128_gcm = { | ||
1411 | .key_len = 16, | ||
1412 | .nonce_len = 12, | ||
1413 | .overhead = EVP_AEAD_AES_GCM_TAG_LEN, | ||
1414 | .max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN, | ||
1415 | |||
1416 | .init = aead_aes_gcm_init, | ||
1417 | .cleanup = aead_aes_gcm_cleanup, | ||
1418 | .seal = aead_aes_gcm_seal, | ||
1419 | .open = aead_aes_gcm_open, | ||
1420 | }; | ||
1421 | |||
1422 | static const EVP_AEAD aead_aes_256_gcm = { | ||
1423 | .key_len = 32, | ||
1424 | .nonce_len = 12, | ||
1425 | .overhead = EVP_AEAD_AES_GCM_TAG_LEN, | ||
1426 | .max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN, | ||
1427 | |||
1428 | .init = aead_aes_gcm_init, | ||
1429 | .cleanup = aead_aes_gcm_cleanup, | ||
1430 | .seal = aead_aes_gcm_seal, | ||
1431 | .open = aead_aes_gcm_open, | ||
1432 | }; | ||
1433 | |||
1434 | const EVP_AEAD * | ||
1435 | EVP_aead_aes_128_gcm(void) | ||
1436 | { | ||
1437 | return &aead_aes_128_gcm; | ||
1438 | } | ||
1439 | |||
1440 | const EVP_AEAD * | ||
1441 | EVP_aead_aes_256_gcm(void) | ||
1442 | { | ||
1443 | return &aead_aes_256_gcm; | ||
1444 | } | ||
1445 | |||
1266 | #endif | 1446 | #endif |
diff --git a/src/lib/libssl/src/crypto/evp/evp.h b/src/lib/libssl/src/crypto/evp/evp.h index 54aa8a4a66..f8395fbe7b 100644 --- a/src/lib/libssl/src/crypto/evp/evp.h +++ b/src/lib/libssl/src/crypto/evp/evp.h | |||
@@ -1205,6 +1205,110 @@ void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, | |||
1205 | int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2), | 1205 | int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2), |
1206 | int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value)); | 1206 | int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value)); |
1207 | 1207 | ||
1208 | /* Authenticated Encryption with Additional Data. | ||
1209 | * | ||
1210 | * AEAD couples confidentiality and integrity in a single primtive. AEAD | ||
1211 | * algorithms take a key and then can seal and open individual messages. Each | ||
1212 | * message has a unique, per-message nonce and, optionally, additional data | ||
1213 | * which is authenticated but not included in the output. */ | ||
1214 | |||
1215 | struct evp_aead_st; | ||
1216 | typedef struct evp_aead_st EVP_AEAD; | ||
1217 | |||
1218 | #ifndef OPENSSL_NO_AES | ||
1219 | /* EVP_aes_128_gcm is AES-128 in Galois Counter Mode. */ | ||
1220 | const EVP_AEAD *EVP_aead_aes_128_gcm(void); | ||
1221 | /* EVP_aes_256_gcm is AES-256 in Galois Counter Mode. */ | ||
1222 | const EVP_AEAD *EVP_aead_aes_256_gcm(void); | ||
1223 | #endif | ||
1224 | |||
1225 | /* EVP_AEAD_key_length returns the length of the keys used. */ | ||
1226 | size_t EVP_AEAD_key_length(const EVP_AEAD *aead); | ||
1227 | |||
1228 | /* EVP_AEAD_nonce_length returns the length of the per-message nonce. */ | ||
1229 | size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead); | ||
1230 | |||
1231 | /* EVP_AEAD_max_overhead returns the maximum number of additional bytes added | ||
1232 | * by the act of sealing data with the AEAD. */ | ||
1233 | size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead); | ||
1234 | |||
1235 | /* EVP_AEAD_max_tag_len returns the maximum tag length when using this AEAD. | ||
1236 | * This * is the largest value that can be passed as a tag length to | ||
1237 | * EVP_AEAD_CTX_init. */ | ||
1238 | size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead); | ||
1239 | |||
1240 | /* An EVP_AEAD_CTX represents an AEAD algorithm configured with a specific key | ||
1241 | * and message-independent IV. */ | ||
1242 | typedef struct evp_aead_ctx_st { | ||
1243 | const EVP_AEAD *aead; | ||
1244 | /* aead_state is an opaque pointer to the AEAD specific state. */ | ||
1245 | void *aead_state; | ||
1246 | } EVP_AEAD_CTX; | ||
1247 | |||
1248 | /* EVP_AEAD_MAX_TAG_LENGTH is the maximum tag length used by any AEAD | ||
1249 | * defined in this header. */ | ||
1250 | #define EVP_AEAD_MAX_TAG_LENGTH 16 | ||
1251 | |||
1252 | /* EVP_AEAD_DEFAULT_TAG_LENGTH is a magic value that can be passed to | ||
1253 | * EVP_AEAD_CTX_init to indicate that the default tag length for an AEAD | ||
1254 | * should be used. */ | ||
1255 | #define EVP_AEAD_DEFAULT_TAG_LENGTH 0 | ||
1256 | |||
1257 | /* EVP_AEAD_init initializes the context for the given AEAD algorithm. | ||
1258 | * The implementation argument may be NULL to choose the default implementation. | ||
1259 | * Authentication tags may be truncated by passing a tag length. A tag length | ||
1260 | * of zero indicates the default tag length should be used. */ | ||
1261 | int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, | ||
1262 | const unsigned char *key, size_t key_len, size_t tag_len, ENGINE *impl); | ||
1263 | |||
1264 | /* EVP_AEAD_CTX_cleanup frees any data allocated for this context. */ | ||
1265 | void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx); | ||
1266 | |||
1267 | /* EVP_AEAD_CTX_seal encrypts and authenticates the input and authenticates | ||
1268 | * any additional data (AD). The result is written as output, with the number | ||
1269 | * of bytes written being returned, or -1 on error. | ||
1270 | * | ||
1271 | * This function may be called (with the same EVP_AEAD_CTX) concurrently with | ||
1272 | * itself or EVP_AEAD_CTX_open. | ||
1273 | * | ||
1274 | * At most max_out_len bytes are written as output and, in order to ensure | ||
1275 | * success, this value should be the length of the input plus the result of | ||
1276 | * EVP_AEAD_overhead. | ||
1277 | * | ||
1278 | * The length of the nonce is must be equal to the result of | ||
1279 | * EVP_AEAD_nonce_length for this AEAD. | ||
1280 | * | ||
1281 | * EVP_AEAD_CTX_seal never results in a partial output. If max_out_len is | ||
1282 | * insufficient, -1 will be returned. | ||
1283 | * | ||
1284 | * If the input and output are aliased then out must be <= in. */ | ||
1285 | ssize_t EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, unsigned char *out, | ||
1286 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
1287 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
1288 | size_t ad_len); | ||
1289 | |||
1290 | /* EVP_AEAD_CTX_open authenticates the input and additional data, decrypting | ||
1291 | * the input and writing it as output. The number of bytes decrypted and | ||
1292 | * written as output is returned, or -1 on error. | ||
1293 | * | ||
1294 | * This function may be called (with the same EVP_AEAD_CTX) concurrently with | ||
1295 | * itself or EVP_AEAD_CTX_seal. | ||
1296 | * | ||
1297 | * At most the number of input bytes are written as output. In order to ensure | ||
1298 | * success, max_out_len should be at least the same as the input length. | ||
1299 | * | ||
1300 | * The length of nonce must be equal to the result of EVP_AEAD_nonce_length | ||
1301 | * for this AEAD. | ||
1302 | * | ||
1303 | * EVP_AEAD_CTX_open never results in a partial output. If max_out_len is | ||
1304 | * insufficient, -1 will be returned. | ||
1305 | * | ||
1306 | * If the input and output are aliased then out must be <= in. */ | ||
1307 | ssize_t EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, unsigned char *out, | ||
1308 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
1309 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
1310 | size_t ad_len); | ||
1311 | |||
1208 | void EVP_add_alg_module(void); | 1312 | void EVP_add_alg_module(void); |
1209 | 1313 | ||
1210 | /* BEGIN ERROR CODES */ | 1314 | /* BEGIN ERROR CODES */ |
@@ -1216,6 +1320,11 @@ void ERR_load_EVP_strings(void); | |||
1216 | /* Error codes for the EVP functions. */ | 1320 | /* Error codes for the EVP functions. */ |
1217 | 1321 | ||
1218 | /* Function codes. */ | 1322 | /* Function codes. */ |
1323 | #define EVP_F_AEAD_AES_GCM_INIT 187 | ||
1324 | #define EVP_F_AEAD_AES_GCM_OPEN 188 | ||
1325 | #define EVP_F_AEAD_AES_GCM_SEAL 189 | ||
1326 | #define EVP_F_AEAD_CTX_OPEN 185 | ||
1327 | #define EVP_F_AEAD_CTX_SEAL 186 | ||
1219 | #define EVP_F_AESNI_INIT_KEY 165 | 1328 | #define EVP_F_AESNI_INIT_KEY 165 |
1220 | #define EVP_F_AESNI_XTS_CIPHER 176 | 1329 | #define EVP_F_AESNI_XTS_CIPHER 176 |
1221 | #define EVP_F_AES_INIT_KEY 133 | 1330 | #define EVP_F_AES_INIT_KEY 133 |
@@ -1230,6 +1339,9 @@ void ERR_load_EVP_strings(void); | |||
1230 | #define EVP_F_DSA_PKEY2PKCS8 135 | 1339 | #define EVP_F_DSA_PKEY2PKCS8 135 |
1231 | #define EVP_F_ECDSA_PKEY2PKCS8 129 | 1340 | #define EVP_F_ECDSA_PKEY2PKCS8 129 |
1232 | #define EVP_F_ECKEY_PKEY2PKCS8 132 | 1341 | #define EVP_F_ECKEY_PKEY2PKCS8 132 |
1342 | #define EVP_F_EVP_AEAD_CTX_INIT 180 | ||
1343 | #define EVP_F_EVP_AEAD_CTX_OPEN 190 | ||
1344 | #define EVP_F_EVP_AEAD_CTX_SEAL 191 | ||
1233 | #define EVP_F_EVP_CIPHERINIT_EX 123 | 1345 | #define EVP_F_EVP_CIPHERINIT_EX 123 |
1234 | #define EVP_F_EVP_CIPHER_CTX_COPY 163 | 1346 | #define EVP_F_EVP_CIPHER_CTX_COPY 163 |
1235 | #define EVP_F_EVP_CIPHER_CTX_CTRL 124 | 1347 | #define EVP_F_EVP_CIPHER_CTX_CTRL 124 |
@@ -1345,10 +1457,12 @@ void ERR_load_EVP_strings(void); | |||
1345 | #define EVP_R_NO_VERIFY_FUNCTION_CONFIGURED 105 | 1457 | #define EVP_R_NO_VERIFY_FUNCTION_CONFIGURED 105 |
1346 | #define EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 150 | 1458 | #define EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 150 |
1347 | #define EVP_R_OPERATON_NOT_INITIALIZED 151 | 1459 | #define EVP_R_OPERATON_NOT_INITIALIZED 151 |
1460 | #define EVP_R_OUTPUT_ALIASES_INPUT 172 | ||
1348 | #define EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE 117 | 1461 | #define EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE 117 |
1349 | #define EVP_R_PRIVATE_KEY_DECODE_ERROR 145 | 1462 | #define EVP_R_PRIVATE_KEY_DECODE_ERROR 145 |
1350 | #define EVP_R_PRIVATE_KEY_ENCODE_ERROR 146 | 1463 | #define EVP_R_PRIVATE_KEY_ENCODE_ERROR 146 |
1351 | #define EVP_R_PUBLIC_KEY_NOT_RSA 106 | 1464 | #define EVP_R_PUBLIC_KEY_NOT_RSA 106 |
1465 | #define EVP_R_TAG_TOO_LARGE 171 | ||
1352 | #define EVP_R_TOO_LARGE 164 | 1466 | #define EVP_R_TOO_LARGE 164 |
1353 | #define EVP_R_UNKNOWN_CIPHER 160 | 1467 | #define EVP_R_UNKNOWN_CIPHER 160 |
1354 | #define EVP_R_UNKNOWN_DIGEST 161 | 1468 | #define EVP_R_UNKNOWN_DIGEST 161 |
diff --git a/src/lib/libssl/src/crypto/evp/evp_aead.c b/src/lib/libssl/src/crypto/evp/evp_aead.c new file mode 100644 index 0000000000..137e3dd05b --- /dev/null +++ b/src/lib/libssl/src/crypto/evp/evp_aead.c | |||
@@ -0,0 +1,192 @@ | |||
1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
2 | * All rights reserved. | ||
3 | * | ||
4 | * This package is an SSL implementation written | ||
5 | * by Eric Young (eay@cryptsoft.com). | ||
6 | * The implementation was written so as to conform with Netscapes SSL. | ||
7 | * | ||
8 | * This library is free for commercial and non-commercial use as long as | ||
9 | * the following conditions are aheared to. The following conditions | ||
10 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
12 | * included with this distribution is covered by the same copyright terms | ||
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
14 | * | ||
15 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
16 | * the code are not to be removed. | ||
17 | * If this package is used in a product, Eric Young should be given attribution | ||
18 | * as the author of the parts of the library used. | ||
19 | * This can be in the form of a textual message at program startup or | ||
20 | * in documentation (online or textual) provided with the package. | ||
21 | * | ||
22 | * Redistribution and use in source and binary forms, with or without | ||
23 | * modification, are permitted provided that the following conditions | ||
24 | * are met: | ||
25 | * 1. Redistributions of source code must retain the copyright | ||
26 | * notice, this list of conditions and the following disclaimer. | ||
27 | * 2. Redistributions in binary form must reproduce the above copyright | ||
28 | * notice, this list of conditions and the following disclaimer in the | ||
29 | * documentation and/or other materials provided with the distribution. | ||
30 | * 3. All advertising materials mentioning features or use of this software | ||
31 | * must display the following acknowledgement: | ||
32 | * "This product includes cryptographic software written by | ||
33 | * Eric Young (eay@cryptsoft.com)" | ||
34 | * The word 'cryptographic' can be left out if the rouines from the library | ||
35 | * being used are not cryptographic related :-). | ||
36 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
37 | * the apps directory (application code) you must include an acknowledgement: | ||
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
39 | * | ||
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
50 | * SUCH DAMAGE. | ||
51 | * | ||
52 | * The licence and distribution terms for any publically available version or | ||
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
54 | * copied and put under another distribution licence | ||
55 | * [including the GNU Public Licence.] | ||
56 | */ | ||
57 | |||
58 | #include <limits.h> | ||
59 | #include <string.h> | ||
60 | |||
61 | #include <openssl/evp.h> | ||
62 | #include <openssl/err.h> | ||
63 | |||
64 | #include "evp_locl.h" | ||
65 | |||
66 | size_t EVP_AEAD_key_length(const EVP_AEAD *aead) | ||
67 | { | ||
68 | return aead->key_len; | ||
69 | } | ||
70 | |||
71 | size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead) | ||
72 | { | ||
73 | return aead->nonce_len; | ||
74 | } | ||
75 | |||
76 | size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead) | ||
77 | { | ||
78 | return aead->overhead; | ||
79 | } | ||
80 | |||
81 | size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead) | ||
82 | { | ||
83 | return aead->max_tag_len; | ||
84 | } | ||
85 | |||
86 | int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, | ||
87 | const unsigned char *key, size_t key_len, | ||
88 | size_t tag_len, ENGINE *impl) | ||
89 | { | ||
90 | ctx->aead = aead; | ||
91 | if (key_len != aead->key_len) | ||
92 | { | ||
93 | EVPerr(EVP_F_EVP_AEAD_CTX_INIT,EVP_R_UNSUPPORTED_KEY_SIZE); | ||
94 | return 0; | ||
95 | } | ||
96 | return aead->init(ctx, key, key_len, tag_len); | ||
97 | } | ||
98 | |||
99 | void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx) | ||
100 | { | ||
101 | if (ctx->aead == NULL) | ||
102 | return; | ||
103 | ctx->aead->cleanup(ctx); | ||
104 | ctx->aead = NULL; | ||
105 | } | ||
106 | |||
107 | /* check_alias returns 0 if out points within the buffer determined by in | ||
108 | * and in_len and 1 otherwise. | ||
109 | * | ||
110 | * When processing, there's only an issue if out points within in[:in_len] | ||
111 | * and isn't equal to in. If that's the case then writing the output will | ||
112 | * stomp input that hasn't been read yet. | ||
113 | * | ||
114 | * This function checks for that case. */ | ||
115 | static int check_alias(const unsigned char *in, size_t in_len, | ||
116 | const unsigned char *out) | ||
117 | { | ||
118 | if (out <= in) | ||
119 | return 1; | ||
120 | if (in + in_len <= out) | ||
121 | return 1; | ||
122 | return 0; | ||
123 | } | ||
124 | |||
125 | ssize_t EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, | ||
126 | unsigned char *out, size_t max_out_len, | ||
127 | const unsigned char *nonce, size_t nonce_len, | ||
128 | const unsigned char *in, size_t in_len, | ||
129 | const unsigned char *ad, size_t ad_len) | ||
130 | { | ||
131 | size_t possible_out_len = in_len + ctx->aead->overhead; | ||
132 | ssize_t r; | ||
133 | |||
134 | if (possible_out_len < in_len /* overflow */ || | ||
135 | possible_out_len > SSIZE_MAX /* return value cannot be | ||
136 | represented */) | ||
137 | { | ||
138 | EVPerr(EVP_F_EVP_AEAD_CTX_SEAL, EVP_R_TOO_LARGE); | ||
139 | goto error; | ||
140 | } | ||
141 | |||
142 | if (!check_alias(in, in_len, out)) | ||
143 | { | ||
144 | EVPerr(EVP_F_EVP_AEAD_CTX_SEAL, EVP_R_OUTPUT_ALIASES_INPUT); | ||
145 | goto error; | ||
146 | } | ||
147 | |||
148 | r = ctx->aead->seal(ctx, out, max_out_len, nonce, nonce_len, | ||
149 | in, in_len, ad, ad_len); | ||
150 | if (r >= 0) | ||
151 | return r; | ||
152 | |||
153 | error: | ||
154 | /* In the event of an error, clear the output buffer so that a caller | ||
155 | * that doesn't check the return value doesn't send raw data. */ | ||
156 | memset(out, 0, max_out_len); | ||
157 | return -1; | ||
158 | } | ||
159 | |||
160 | ssize_t EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, | ||
161 | unsigned char *out, size_t max_out_len, | ||
162 | const unsigned char *nonce, size_t nonce_len, | ||
163 | const unsigned char *in, size_t in_len, | ||
164 | const unsigned char *ad, size_t ad_len) | ||
165 | { | ||
166 | ssize_t r; | ||
167 | |||
168 | if (in_len > SSIZE_MAX) | ||
169 | { | ||
170 | EVPerr(EVP_F_EVP_AEAD_CTX_OPEN, EVP_R_TOO_LARGE); | ||
171 | goto error; /* may not be able to represent return value. */ | ||
172 | } | ||
173 | |||
174 | if (!check_alias(in, in_len, out)) | ||
175 | { | ||
176 | EVPerr(EVP_F_EVP_AEAD_CTX_OPEN, EVP_R_OUTPUT_ALIASES_INPUT); | ||
177 | goto error; | ||
178 | } | ||
179 | |||
180 | r = ctx->aead->open(ctx, out, max_out_len, nonce, nonce_len, | ||
181 | in, in_len, ad, ad_len); | ||
182 | |||
183 | if (r >= 0) | ||
184 | return r; | ||
185 | |||
186 | error: | ||
187 | /* In the event of an error, clear the output buffer so that a caller | ||
188 | * that doesn't check the return value doesn't try and process bad | ||
189 | * data. */ | ||
190 | memset(out, 0, max_out_len); | ||
191 | return -1; | ||
192 | } | ||
diff --git a/src/lib/libssl/src/crypto/evp/evp_err.c b/src/lib/libssl/src/crypto/evp/evp_err.c index 8024731938..9391036d0f 100644 --- a/src/lib/libssl/src/crypto/evp/evp_err.c +++ b/src/lib/libssl/src/crypto/evp/evp_err.c | |||
@@ -69,6 +69,9 @@ | |||
69 | #define ERR_REASON(reason) ERR_PACK(ERR_LIB_EVP,0,reason) | 69 | #define ERR_REASON(reason) ERR_PACK(ERR_LIB_EVP,0,reason) |
70 | 70 | ||
71 | static ERR_STRING_DATA EVP_str_functs[] = { | 71 | static ERR_STRING_DATA EVP_str_functs[] = { |
72 | {ERR_FUNC(EVP_F_AEAD_AES_GCM_INIT), "AEAD_AES_GCM_INIT"}, | ||
73 | {ERR_FUNC(EVP_F_AEAD_AES_GCM_OPEN), "AEAD_AES_GCM_OPEN"}, | ||
74 | {ERR_FUNC(EVP_F_AEAD_AES_GCM_SEAL), "AEAD_AES_GCM_SEAL"}, | ||
72 | {ERR_FUNC(EVP_F_AESNI_INIT_KEY), "AESNI_INIT_KEY"}, | 75 | {ERR_FUNC(EVP_F_AESNI_INIT_KEY), "AESNI_INIT_KEY"}, |
73 | {ERR_FUNC(EVP_F_AESNI_XTS_CIPHER), "AESNI_XTS_CIPHER"}, | 76 | {ERR_FUNC(EVP_F_AESNI_XTS_CIPHER), "AESNI_XTS_CIPHER"}, |
74 | {ERR_FUNC(EVP_F_AES_INIT_KEY), "AES_INIT_KEY"}, | 77 | {ERR_FUNC(EVP_F_AES_INIT_KEY), "AES_INIT_KEY"}, |
@@ -83,6 +86,9 @@ static ERR_STRING_DATA EVP_str_functs[] = { | |||
83 | {ERR_FUNC(EVP_F_DSA_PKEY2PKCS8), "DSA_PKEY2PKCS8"}, | 86 | {ERR_FUNC(EVP_F_DSA_PKEY2PKCS8), "DSA_PKEY2PKCS8"}, |
84 | {ERR_FUNC(EVP_F_ECDSA_PKEY2PKCS8), "ECDSA_PKEY2PKCS8"}, | 87 | {ERR_FUNC(EVP_F_ECDSA_PKEY2PKCS8), "ECDSA_PKEY2PKCS8"}, |
85 | {ERR_FUNC(EVP_F_ECKEY_PKEY2PKCS8), "ECKEY_PKEY2PKCS8"}, | 88 | {ERR_FUNC(EVP_F_ECKEY_PKEY2PKCS8), "ECKEY_PKEY2PKCS8"}, |
89 | {ERR_FUNC(EVP_F_EVP_AEAD_CTX_INIT), "EVP_AEAD_CTX_init"}, | ||
90 | {ERR_FUNC(EVP_F_EVP_AEAD_CTX_OPEN), "EVP_AEAD_CTX_open"}, | ||
91 | {ERR_FUNC(EVP_F_EVP_AEAD_CTX_SEAL), "EVP_AEAD_CTX_seal"}, | ||
86 | {ERR_FUNC(EVP_F_EVP_CIPHERINIT_EX), "EVP_CipherInit_ex"}, | 92 | {ERR_FUNC(EVP_F_EVP_CIPHERINIT_EX), "EVP_CipherInit_ex"}, |
87 | {ERR_FUNC(EVP_F_EVP_CIPHER_CTX_COPY), "EVP_CIPHER_CTX_copy"}, | 93 | {ERR_FUNC(EVP_F_EVP_CIPHER_CTX_COPY), "EVP_CIPHER_CTX_copy"}, |
88 | {ERR_FUNC(EVP_F_EVP_CIPHER_CTX_CTRL), "EVP_CIPHER_CTX_ctrl"}, | 94 | {ERR_FUNC(EVP_F_EVP_CIPHER_CTX_CTRL), "EVP_CIPHER_CTX_ctrl"}, |
@@ -200,10 +206,12 @@ static ERR_STRING_DATA EVP_str_reasons[] = { | |||
200 | {ERR_REASON(EVP_R_NO_VERIFY_FUNCTION_CONFIGURED), "no verify function configured"}, | 206 | {ERR_REASON(EVP_R_NO_VERIFY_FUNCTION_CONFIGURED), "no verify function configured"}, |
201 | {ERR_REASON(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE), "operation not supported for this keytype"}, | 207 | {ERR_REASON(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE), "operation not supported for this keytype"}, |
202 | {ERR_REASON(EVP_R_OPERATON_NOT_INITIALIZED), "operaton not initialized"}, | 208 | {ERR_REASON(EVP_R_OPERATON_NOT_INITIALIZED), "operaton not initialized"}, |
209 | {ERR_REASON(EVP_R_OUTPUT_ALIASES_INPUT), "output aliases input"}, | ||
203 | {ERR_REASON(EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE), "pkcs8 unknown broken type"}, | 210 | {ERR_REASON(EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE), "pkcs8 unknown broken type"}, |
204 | {ERR_REASON(EVP_R_PRIVATE_KEY_DECODE_ERROR), "private key decode error"}, | 211 | {ERR_REASON(EVP_R_PRIVATE_KEY_DECODE_ERROR), "private key decode error"}, |
205 | {ERR_REASON(EVP_R_PRIVATE_KEY_ENCODE_ERROR), "private key encode error"}, | 212 | {ERR_REASON(EVP_R_PRIVATE_KEY_ENCODE_ERROR), "private key encode error"}, |
206 | {ERR_REASON(EVP_R_PUBLIC_KEY_NOT_RSA) , "public key not rsa"}, | 213 | {ERR_REASON(EVP_R_PUBLIC_KEY_NOT_RSA) , "public key not rsa"}, |
214 | {ERR_REASON(EVP_R_TAG_TOO_LARGE), "tag too large"}, | ||
207 | {ERR_REASON(EVP_R_TOO_LARGE) , "too large"}, | 215 | {ERR_REASON(EVP_R_TOO_LARGE) , "too large"}, |
208 | {ERR_REASON(EVP_R_UNKNOWN_CIPHER) , "unknown cipher"}, | 216 | {ERR_REASON(EVP_R_UNKNOWN_CIPHER) , "unknown cipher"}, |
209 | {ERR_REASON(EVP_R_UNKNOWN_DIGEST) , "unknown digest"}, | 217 | {ERR_REASON(EVP_R_UNKNOWN_DIGEST) , "unknown digest"}, |
diff --git a/src/lib/libssl/src/crypto/evp/evp_locl.h b/src/lib/libssl/src/crypto/evp/evp_locl.h index 673c85f8bd..6f9218eafc 100644 --- a/src/lib/libssl/src/crypto/evp/evp_locl.h +++ b/src/lib/libssl/src/crypto/evp/evp_locl.h | |||
@@ -342,3 +342,25 @@ void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx); | |||
342 | 342 | ||
343 | int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, | 343 | int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, |
344 | ASN1_TYPE *param, const EVP_CIPHER *c, const EVP_MD *md, int en_de); | 344 | ASN1_TYPE *param, const EVP_CIPHER *c, const EVP_MD *md, int en_de); |
345 | |||
346 | /* EVP_AEAD represents a specific AEAD algorithm. */ | ||
347 | struct evp_aead_st { | ||
348 | unsigned char key_len; | ||
349 | unsigned char nonce_len; | ||
350 | unsigned char overhead; | ||
351 | unsigned char max_tag_len; | ||
352 | |||
353 | int (*init)(struct evp_aead_ctx_st*, const unsigned char *key, | ||
354 | size_t key_len, size_t tag_len); | ||
355 | void (*cleanup)(struct evp_aead_ctx_st*); | ||
356 | |||
357 | ssize_t (*seal)(const struct evp_aead_ctx_st *ctx, unsigned char *out, | ||
358 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
359 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
360 | size_t ad_len); | ||
361 | |||
362 | ssize_t (*open)(const struct evp_aead_ctx_st *ctx, unsigned char *out, | ||
363 | size_t max_out_len, const unsigned char *nonce, size_t nonce_len, | ||
364 | const unsigned char *in, size_t in_len, const unsigned char *ad, | ||
365 | size_t ad_len); | ||
366 | }; | ||