Skip to content

Commit 01a3904

Browse files
committed
Provide CMAC high level API replacement
1 parent d411109 commit 01a3904

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/crypto/crypto_openssl.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,43 @@ int crypto_get_random(void *buf, size_t len)
12181218
int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
12191219
const u8 *addr[], const size_t *len, u8 *mac)
12201220
{
1221+
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1222+
+ EVP_MAC_CTX *ctx = NULL;
1223+
+ EVP_MAC *emac;
1224+
+ int ret = -1;
1225+
+ size_t outlen, i;
1226+
+ OSSL_PARAM params[2];
1227+
+ char *cipher = NULL;
1228+
+ if (TEST_FAIL())
1229+
+ return -1;
1230+
+ emac = EVP_MAC_fetch(NULL, "CMAC", NULL);
1231+
+
1232+
+ if (key_len == 32)
1233+
+ cipher = "aes-256-cbc";
1234+
+ else if (key_len == 24)
1235+
+ cipher = "aes-192-cbc";
1236+
+ else if (key_len == 16)
1237+
+ cipher = "aes-128-cbc";
1238+
+
1239+
+ params[0] = OSSL_PARAM_construct_utf8_string("cipher", cipher, 0);
1240+
+ params[1] = OSSL_PARAM_construct_end();
1241+
+
1242+
+ if (!emac || !cipher ||
1243+
+ !(ctx = EVP_MAC_CTX_new(emac)) ||
1244+
+ EVP_MAC_init(ctx, key, key_len, params) != 1)
1245+
+ goto fail;
1246+
+
1247+
+ for (i = 0; i < num_elem; i++) {
1248+
+ if (!EVP_MAC_update(ctx, addr[i], len[i]))
1249+
+ goto fail;
1250+
+ }
1251+
+ if (EVP_MAC_final(ctx, mac, &outlen, 16) != 1 || outlen != 16)
1252+
+ goto fail;
1253+
+ ret = 0;
1254+
+fail:
1255+
+ EVP_MAC_CTX_free(ctx);
1256+
+ return ret;
1257+
+#else /* OpenSSL version >= 3.0 */
12211258
CMAC_CTX *ctx;
12221259
int ret = -1;
12231260
size_t outlen, i;
@@ -1249,6 +1286,7 @@ int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
12491286
fail:
12501287
CMAC_CTX_free(ctx);
12511288
return ret;
1289+
#endif /* OpenSSL version >= 3.0 */
12521290
}
12531291

12541292

0 commit comments

Comments
 (0)