| 1 |
/* |
| 2 |
* Copyright 2017-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 4 |
* |
| 5 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 6 |
* this file except in compliance with the License. You can obtain a copy |
| 7 |
* in the file LICENSE in the source distribution or at |
| 8 |
* https://www.openssl.org/source/license.html |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "internal/deprecated.h" |
| 12 |
|
| 13 |
#include "internal/cryptlib.h" |
| 14 |
#ifndef OPENSSL_NO_ARIA |
| 15 |
#include <openssl/evp.h> |
| 16 |
#include <openssl/modes.h> |
| 17 |
#include <openssl/rand.h> |
| 18 |
#include "crypto/aria.h" |
| 19 |
#include "crypto/evp.h" |
| 20 |
#include "crypto/modes.h" |
| 21 |
#include "evp_local.h" |
| 22 |
|
| 23 |
/* ARIA subkey Structure */ |
| 24 |
typedef struct { |
| 25 |
ARIA_KEY ks; |
| 26 |
} EVP_ARIA_KEY; |
| 27 |
|
| 28 |
/* ARIA GCM context */ |
| 29 |
typedef struct { |
| 30 |
union { |
| 31 |
OSSL_UNION_ALIGN; |
| 32 |
ARIA_KEY ks; |
| 33 |
} ks; /* ARIA subkey to use */ |
| 34 |
int key_set; /* Set if key initialised */ |
| 35 |
int iv_set; /* Set if an iv is set */ |
| 36 |
GCM128_CONTEXT gcm; |
| 37 |
unsigned char *iv; /* Temporary IV store */ |
| 38 |
int ivlen; /* IV length */ |
| 39 |
int taglen; |
| 40 |
int iv_gen; /* It is OK to generate IVs */ |
| 41 |
int tls_aad_len; /* TLS AAD length */ |
| 42 |
} EVP_ARIA_GCM_CTX; |
| 43 |
|
| 44 |
/* ARIA CCM context */ |
| 45 |
typedef struct { |
| 46 |
union { |
| 47 |
OSSL_UNION_ALIGN; |
| 48 |
ARIA_KEY ks; |
| 49 |
} ks; /* ARIA key schedule to use */ |
| 50 |
int key_set; /* Set if key initialised */ |
| 51 |
int iv_set; /* Set if an iv is set */ |
| 52 |
int tag_set; /* Set if tag is valid */ |
| 53 |
int len_set; /* Set if message length set */ |
| 54 |
int L, M; /* L and M parameters from RFC3610 */ |
| 55 |
int tls_aad_len; /* TLS AAD length */ |
| 56 |
CCM128_CONTEXT ccm; |
| 57 |
ccm128_f str; |
| 58 |
} EVP_ARIA_CCM_CTX; |
| 59 |
|
| 60 |
/* The subkey for ARIA is generated. */ |
| 61 |
static int aria_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
| 62 |
const unsigned char *iv, int enc) |
| 63 |
{ |
| 64 |
int ret; |
| 65 |
int mode = EVP_CIPHER_CTX_get_mode(ctx); |
| 66 |
|
| 67 |
if (enc || (mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE)) |
| 68 |
ret = ossl_aria_set_encrypt_key(key, |
| 69 |
EVP_CIPHER_CTX_get_key_length(ctx) * 8, |
| 70 |
EVP_CIPHER_CTX_get_cipher_data(ctx)); |
| 71 |
else |
| 72 |
ret = ossl_aria_set_decrypt_key(key, |
| 73 |
EVP_CIPHER_CTX_get_key_length(ctx) * 8, |
| 74 |
EVP_CIPHER_CTX_get_cipher_data(ctx)); |
| 75 |
if (ret < 0) { |
| 76 |
ERR_raise(ERR_LIB_EVP, EVP_R_ARIA_KEY_SETUP_FAILED); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
return 1; |
| 80 |
} |
| 81 |
|
| 82 |
static void aria_cbc_encrypt(const unsigned char *in, unsigned char *out, |
| 83 |
size_t len, const ARIA_KEY *key, |
| 84 |
unsigned char *ivec, const int enc) |
| 85 |
{ |
| 86 |
|
| 87 |
if (enc) |
| 88 |
CRYPTO_cbc128_encrypt(in, out, len, key, ivec, |
| 89 |
(block128_f)ossl_aria_encrypt); |
| 90 |
else |
| 91 |
CRYPTO_cbc128_decrypt(in, out, len, key, ivec, |
| 92 |
(block128_f)ossl_aria_encrypt); |
| 93 |
} |
| 94 |
|
| 95 |
static void aria_cfb128_encrypt(const unsigned char *in, unsigned char *out, |
| 96 |
size_t length, const ARIA_KEY *key, |
| 97 |
unsigned char *ivec, int *num, const int enc) |
| 98 |
{ |
| 99 |
|
| 100 |
CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc, |
| 101 |
(block128_f)ossl_aria_encrypt); |
| 102 |
} |
| 103 |
|
| 104 |
static void aria_cfb1_encrypt(const unsigned char *in, unsigned char *out, |
| 105 |
size_t length, const ARIA_KEY *key, |
| 106 |
unsigned char *ivec, int *num, const int enc) |
| 107 |
{ |
| 108 |
CRYPTO_cfb128_1_encrypt(in, out, length, key, ivec, num, enc, |
| 109 |
(block128_f)ossl_aria_encrypt); |
| 110 |
} |
| 111 |
|
| 112 |
static void aria_cfb8_encrypt(const unsigned char *in, unsigned char *out, |
| 113 |
size_t length, const ARIA_KEY *key, |
| 114 |
unsigned char *ivec, int *num, const int enc) |
| 115 |
{ |
| 116 |
CRYPTO_cfb128_8_encrypt(in, out, length, key, ivec, num, enc, |
| 117 |
(block128_f)ossl_aria_encrypt); |
| 118 |
} |
| 119 |
|
| 120 |
static void aria_ecb_encrypt(const unsigned char *in, unsigned char *out, |
| 121 |
const ARIA_KEY *key, const int enc) |
| 122 |
{ |
| 123 |
ossl_aria_encrypt(in, out, key); |
| 124 |
} |
| 125 |
|
| 126 |
static void aria_ofb128_encrypt(const unsigned char *in, unsigned char *out, |
| 127 |
size_t length, const ARIA_KEY *key, |
| 128 |
unsigned char *ivec, int *num) |
| 129 |
{ |
| 130 |
CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num, |
| 131 |
(block128_f)ossl_aria_encrypt); |
| 132 |
} |
| 133 |
|
| 134 |
IMPLEMENT_BLOCK_CIPHER(aria_128, ks, aria, EVP_ARIA_KEY, |
| 135 |
NID_aria_128, 16, 16, 16, 128, |
| 136 |
0, aria_init_key, NULL, |
| 137 |
EVP_CIPHER_set_asn1_iv, |
| 138 |
EVP_CIPHER_get_asn1_iv, |
| 139 |
NULL) |
| 140 |
IMPLEMENT_BLOCK_CIPHER(aria_192, ks, aria, EVP_ARIA_KEY, |
| 141 |
NID_aria_192, 16, 24, 16, 128, |
| 142 |
0, aria_init_key, NULL, |
| 143 |
EVP_CIPHER_set_asn1_iv, |
| 144 |
EVP_CIPHER_get_asn1_iv, |
| 145 |
NULL) |
| 146 |
IMPLEMENT_BLOCK_CIPHER(aria_256, ks, aria, EVP_ARIA_KEY, |
| 147 |
NID_aria_256, 16, 32, 16, 128, |
| 148 |
0, aria_init_key, NULL, |
| 149 |
EVP_CIPHER_set_asn1_iv, |
| 150 |
EVP_CIPHER_get_asn1_iv, |
| 151 |
NULL) |
| 152 |
|
| 153 |
#define IMPLEMENT_ARIA_CFBR(ksize, cbits) \ |
| 154 |
IMPLEMENT_CFBR(aria, aria, EVP_ARIA_KEY, ks, ksize, cbits, 16, 0) |
| 155 |
IMPLEMENT_ARIA_CFBR(128, 1) |
| 156 |
IMPLEMENT_ARIA_CFBR(192, 1) |
| 157 |
IMPLEMENT_ARIA_CFBR(256, 1) |
| 158 |
IMPLEMENT_ARIA_CFBR(128, 8) |
| 159 |
IMPLEMENT_ARIA_CFBR(192, 8) |
| 160 |
IMPLEMENT_ARIA_CFBR(256, 8) |
| 161 |
|
| 162 |
#define BLOCK_CIPHER_generic(nid, keylen, blocksize, ivlen, nmode, mode, MODE, flags) \ |
| 163 |
static const EVP_CIPHER aria_##keylen##_##mode = { \ |
| 164 |
nid##_##keylen##_##nmode, blocksize, keylen / 8, ivlen, \ |
| 165 |
flags | EVP_CIPH_##MODE##_MODE, \ |
| 166 |
EVP_ORIG_GLOBAL, \ |
| 167 |
aria_init_key, \ |
| 168 |
aria_##mode##_cipher, \ |
| 169 |
NULL, \ |
| 170 |
sizeof(EVP_ARIA_KEY), \ |
| 171 |
NULL, NULL, NULL, NULL \ |
| 172 |
}; \ |
| 173 |
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ |
| 174 |
{ \ |
| 175 |
return &aria_##keylen##_##mode; \ |
| 176 |
} |
| 177 |
|
| 178 |
static int aria_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
| 179 |
const unsigned char *in, size_t len) |
| 180 |
{ |
| 181 |
int n = EVP_CIPHER_CTX_get_num(ctx); |
| 182 |
unsigned int num; |
| 183 |
EVP_ARIA_KEY *dat = EVP_C_DATA(EVP_ARIA_KEY, ctx); |
| 184 |
|
| 185 |
if (n < 0) |
| 186 |
return 0; |
| 187 |
num = (unsigned int)n; |
| 188 |
|
| 189 |
CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv, |
| 190 |
EVP_CIPHER_CTX_buf_noconst(ctx), &num, |
| 191 |
(block128_f)ossl_aria_encrypt); |
| 192 |
EVP_CIPHER_CTX_set_num(ctx, num); |
| 193 |
return 1; |
| 194 |
} |
| 195 |
|
| 196 |
BLOCK_CIPHER_generic(NID_aria, 128, 1, 16, ctr, ctr, CTR, 0) |
| 197 |
BLOCK_CIPHER_generic(NID_aria, 192, 1, 16, ctr, ctr, CTR, 0) |
| 198 |
BLOCK_CIPHER_generic(NID_aria, 256, 1, 16, ctr, ctr, CTR, 0) |
| 199 |
|
| 200 |
/* Authenticated cipher modes (GCM/CCM) */ |
| 201 |
|
| 202 |
/* increment counter (64-bit int) by 1 */ |
| 203 |
static void ctr64_inc(unsigned char *counter) |
| 204 |
{ |
| 205 |
int n = 8; |
| 206 |
unsigned char c; |
| 207 |
|
| 208 |
do { |
| 209 |
--n; |
| 210 |
c = counter[n]; |
| 211 |
++c; |
| 212 |
counter[n] = c; |
| 213 |
if (c) |
| 214 |
return; |
| 215 |
} while (n); |
| 216 |
} |
| 217 |
|
| 218 |
static int aria_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
| 219 |
const unsigned char *iv, int enc) |
| 220 |
{ |
| 221 |
int ret; |
| 222 |
EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx); |
| 223 |
|
| 224 |
if (!iv && !key) |
| 225 |
return 1; |
| 226 |
if (key) { |
| 227 |
ret = ossl_aria_set_encrypt_key(key, |
| 228 |
EVP_CIPHER_CTX_get_key_length(ctx) * 8, |
| 229 |
&gctx->ks.ks); |
| 230 |
CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, |
| 231 |
(block128_f)ossl_aria_encrypt); |
| 232 |
if (ret < 0) { |
| 233 |
ERR_raise(ERR_LIB_EVP, EVP_R_ARIA_KEY_SETUP_FAILED); |
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
/* |
| 238 |
* If we have an iv can set it directly, otherwise use saved IV. |
| 239 |
*/ |
| 240 |
if (iv == NULL && gctx->iv_set) |
| 241 |
iv = gctx->iv; |
| 242 |
if (iv) { |
| 243 |
CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
| 244 |
gctx->iv_set = 1; |
| 245 |
} |
| 246 |
gctx->key_set = 1; |
| 247 |
} else { |
| 248 |
/* If key set use IV, otherwise copy */ |
| 249 |
if (gctx->key_set) |
| 250 |
CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
| 251 |
else |
| 252 |
memcpy(gctx->iv, iv, gctx->ivlen); |
| 253 |
gctx->iv_set = 1; |
| 254 |
gctx->iv_gen = 0; |
| 255 |
} |
| 256 |
return 1; |
| 257 |
} |
| 258 |
|
| 259 |
static int aria_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
| 260 |
{ |
| 261 |
EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, c); |
| 262 |
|
| 263 |
switch (type) { |
| 264 |
case EVP_CTRL_INIT: |
| 265 |
gctx->key_set = 0; |
| 266 |
gctx->iv_set = 0; |
| 267 |
gctx->ivlen = EVP_CIPHER_get_iv_length(c->cipher); |
| 268 |
gctx->iv = c->iv; |
| 269 |
gctx->taglen = -1; |
| 270 |
gctx->iv_gen = 0; |
| 271 |
gctx->tls_aad_len = -1; |
| 272 |
return 1; |
| 273 |
|
| 274 |
case EVP_CTRL_GET_IVLEN: |
| 275 |
*(int *)ptr = gctx->ivlen; |
| 276 |
return 1; |
| 277 |
|
| 278 |
case EVP_CTRL_AEAD_SET_IVLEN: |
| 279 |
if (arg <= 0) |
| 280 |
return 0; |
| 281 |
/* Allocate memory for IV if needed */ |
| 282 |
if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) { |
| 283 |
if (gctx->iv != c->iv) |
| 284 |
OPENSSL_free(gctx->iv); |
| 285 |
if ((gctx->iv = OPENSSL_malloc(arg)) == NULL) |
| 286 |
return 0; |
| 287 |
} |
| 288 |
gctx->ivlen = arg; |
| 289 |
return 1; |
| 290 |
|
| 291 |
case EVP_CTRL_AEAD_SET_TAG: |
| 292 |
if (arg <= 0 || arg > 16 || EVP_CIPHER_CTX_is_encrypting(c)) |
| 293 |
return 0; |
| 294 |
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg); |
| 295 |
gctx->taglen = arg; |
| 296 |
return 1; |
| 297 |
|
| 298 |
case EVP_CTRL_AEAD_GET_TAG: |
| 299 |
if (arg <= 0 || arg > 16 || !EVP_CIPHER_CTX_is_encrypting(c) |
| 300 |
|| gctx->taglen < 0) |
| 301 |
return 0; |
| 302 |
memcpy(ptr, EVP_CIPHER_CTX_buf_noconst(c), arg); |
| 303 |
return 1; |
| 304 |
|
| 305 |
case EVP_CTRL_GCM_SET_IV_FIXED: |
| 306 |
/* Special case: -1 length restores whole IV */ |
| 307 |
if (arg == -1) { |
| 308 |
memcpy(gctx->iv, ptr, gctx->ivlen); |
| 309 |
gctx->iv_gen = 1; |
| 310 |
return 1; |
| 311 |
} |
| 312 |
/* |
| 313 |
* Fixed field must be at least 4 bytes and invocation field at least |
| 314 |
* 8. |
| 315 |
*/ |
| 316 |
if ((arg < 4) || (gctx->ivlen - arg) < 8) |
| 317 |
return 0; |
| 318 |
if (arg) |
| 319 |
memcpy(gctx->iv, ptr, arg); |
| 320 |
if (EVP_CIPHER_CTX_is_encrypting(c) |
| 321 |
&& RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0) |
| 322 |
return 0; |
| 323 |
gctx->iv_gen = 1; |
| 324 |
return 1; |
| 325 |
|
| 326 |
case EVP_CTRL_GCM_IV_GEN: |
| 327 |
if (gctx->iv_gen == 0 || gctx->key_set == 0) |
| 328 |
return 0; |
| 329 |
CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen); |
| 330 |
if (arg <= 0 || arg > gctx->ivlen) |
| 331 |
arg = gctx->ivlen; |
| 332 |
memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg); |
| 333 |
/* |
| 334 |
* Invocation field will be at least 8 bytes in size and so no need |
| 335 |
* to check wrap around or increment more than last 8 bytes. |
| 336 |
*/ |
| 337 |
ctr64_inc(gctx->iv + gctx->ivlen - 8); |
| 338 |
gctx->iv_set = 1; |
| 339 |
return 1; |
| 340 |
|
| 341 |
case EVP_CTRL_GCM_SET_IV_INV: |
| 342 |
if (gctx->iv_gen == 0 || gctx->key_set == 0 |
| 343 |
|| EVP_CIPHER_CTX_is_encrypting(c)) |
| 344 |
return 0; |
| 345 |
memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg); |
| 346 |
CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen); |
| 347 |
gctx->iv_set = 1; |
| 348 |
return 1; |
| 349 |
|
| 350 |
case EVP_CTRL_AEAD_TLS1_AAD: |
| 351 |
/* Save the AAD for later use */ |
| 352 |
if (arg != EVP_AEAD_TLS1_AAD_LEN) |
| 353 |
return 0; |
| 354 |
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg); |
| 355 |
gctx->tls_aad_len = arg; |
| 356 |
{ |
| 357 |
unsigned int len = EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8 |
| 358 |
| EVP_CIPHER_CTX_buf_noconst(c)[arg - 1]; |
| 359 |
/* Correct length for explicit IV */ |
| 360 |
if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
| 361 |
return 0; |
| 362 |
len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; |
| 363 |
/* If decrypting correct for tag too */ |
| 364 |
if (!EVP_CIPHER_CTX_is_encrypting(c)) { |
| 365 |
if (len < EVP_GCM_TLS_TAG_LEN) |
| 366 |
return 0; |
| 367 |
len -= EVP_GCM_TLS_TAG_LEN; |
| 368 |
} |
| 369 |
EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8; |
| 370 |
EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff; |
| 371 |
} |
| 372 |
/* Extra padding: tag appended to record */ |
| 373 |
return EVP_GCM_TLS_TAG_LEN; |
| 374 |
|
| 375 |
case EVP_CTRL_COPY: { |
| 376 |
EVP_CIPHER_CTX *out = ptr; |
| 377 |
EVP_ARIA_GCM_CTX *gctx_out = EVP_C_DATA(EVP_ARIA_GCM_CTX, out); |
| 378 |
if (gctx->gcm.key) { |
| 379 |
if (gctx->gcm.key != &gctx->ks) |
| 380 |
return 0; |
| 381 |
gctx_out->gcm.key = &gctx_out->ks; |
| 382 |
} |
| 383 |
if (gctx->iv == c->iv) |
| 384 |
gctx_out->iv = out->iv; |
| 385 |
else { |
| 386 |
if ((gctx_out->iv = OPENSSL_malloc(gctx->ivlen)) == NULL) |
| 387 |
return 0; |
| 388 |
memcpy(gctx_out->iv, gctx->iv, gctx->ivlen); |
| 389 |
} |
| 390 |
return 1; |
| 391 |
} |
| 392 |
|
| 393 |
default: |
| 394 |
return -1; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
static int aria_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
| 399 |
const unsigned char *in, size_t len) |
| 400 |
{ |
| 401 |
EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx); |
| 402 |
int rv = -1; |
| 403 |
|
| 404 |
/* Encrypt/decrypt must be performed in place */ |
| 405 |
if (out != in |
| 406 |
|| len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) |
| 407 |
return -1; |
| 408 |
/* |
| 409 |
* Set IV from start of buffer or generate IV and write to start of |
| 410 |
* buffer. |
| 411 |
*/ |
| 412 |
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CIPHER_CTX_is_encrypting(ctx) ? EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV, |
| 413 |
EVP_GCM_TLS_EXPLICIT_IV_LEN, out) |
| 414 |
<= 0) |
| 415 |
goto err; |
| 416 |
/* Use saved AAD */ |
| 417 |
if (CRYPTO_gcm128_aad(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), |
| 418 |
gctx->tls_aad_len)) |
| 419 |
goto err; |
| 420 |
/* Fix buffer and length to point to payload */ |
| 421 |
in += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
| 422 |
out += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
| 423 |
len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
| 424 |
if (EVP_CIPHER_CTX_is_encrypting(ctx)) { |
| 425 |
/* Encrypt payload */ |
| 426 |
if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) |
| 427 |
goto err; |
| 428 |
out += len; |
| 429 |
/* Finally write tag */ |
| 430 |
CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN); |
| 431 |
rv = (int)(len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN); |
| 432 |
} else { |
| 433 |
/* Decrypt */ |
| 434 |
if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) |
| 435 |
goto err; |
| 436 |
/* Retrieve tag */ |
| 437 |
CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), |
| 438 |
EVP_GCM_TLS_TAG_LEN); |
| 439 |
/* If tag mismatch wipe buffer */ |
| 440 |
if (CRYPTO_memcmp(EVP_CIPHER_CTX_buf_noconst(ctx), in + len, |
| 441 |
EVP_GCM_TLS_TAG_LEN)) { |
| 442 |
OPENSSL_cleanse(out, len); |
| 443 |
goto err; |
| 444 |
} |
| 445 |
rv = (int)len; |
| 446 |
} |
| 447 |
|
| 448 |
err: |
| 449 |
gctx->iv_set = 0; |
| 450 |
gctx->tls_aad_len = -1; |
| 451 |
return rv; |
| 452 |
} |
| 453 |
|
| 454 |
static int aria_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
| 455 |
const unsigned char *in, size_t len) |
| 456 |
{ |
| 457 |
EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx); |
| 458 |
|
| 459 |
/* If not set up, return error */ |
| 460 |
if (!gctx->key_set) |
| 461 |
return -1; |
| 462 |
|
| 463 |
if (gctx->tls_aad_len >= 0) |
| 464 |
return aria_gcm_tls_cipher(ctx, out, in, len); |
| 465 |
|
| 466 |
if (!gctx->iv_set) |
| 467 |
return -1; |
| 468 |
if (in) { |
| 469 |
if (out == NULL) { |
| 470 |
if (CRYPTO_gcm128_aad(&gctx->gcm, in, len)) |
| 471 |
return -1; |
| 472 |
} else if (EVP_CIPHER_CTX_is_encrypting(ctx)) { |
| 473 |
if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) |
| 474 |
return -1; |
| 475 |
} else { |
| 476 |
if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) |
| 477 |
return -1; |
| 478 |
} |
| 479 |
return (int)len; |
| 480 |
} |
| 481 |
if (!EVP_CIPHER_CTX_is_encrypting(ctx)) { |
| 482 |
if (gctx->taglen < 0) |
| 483 |
return -1; |
| 484 |
if (CRYPTO_gcm128_finish(&gctx->gcm, |
| 485 |
EVP_CIPHER_CTX_buf_noconst(ctx), |
| 486 |
gctx->taglen) |
| 487 |
!= 0) |
| 488 |
return -1; |
| 489 |
gctx->iv_set = 0; |
| 490 |
return 0; |
| 491 |
} |
| 492 |
CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), 16); |
| 493 |
gctx->taglen = 16; |
| 494 |
/* Don't reuse the IV */ |
| 495 |
gctx->iv_set = 0; |
| 496 |
return 0; |
| 497 |
} |
| 498 |
|
| 499 |
static int aria_gcm_cleanup(EVP_CIPHER_CTX *ctx) |
| 500 |
{ |
| 501 |
EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx); |
| 502 |
|
| 503 |
if (gctx->iv != ctx->iv) |
| 504 |
OPENSSL_free(gctx->iv); |
| 505 |
|
| 506 |
return 1; |
| 507 |
} |
| 508 |
|
| 509 |
static int aria_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
| 510 |
const unsigned char *iv, int enc) |
| 511 |
{ |
| 512 |
int ret; |
| 513 |
EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, ctx); |
| 514 |
|
| 515 |
if (!iv && !key) |
| 516 |
return 1; |
| 517 |
|
| 518 |
if (key) { |
| 519 |
ret = ossl_aria_set_encrypt_key(key, |
| 520 |
EVP_CIPHER_CTX_get_key_length(ctx) * 8, |
| 521 |
&cctx->ks.ks); |
| 522 |
CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, |
| 523 |
&cctx->ks, (block128_f)ossl_aria_encrypt); |
| 524 |
if (ret < 0) { |
| 525 |
ERR_raise(ERR_LIB_EVP, EVP_R_ARIA_KEY_SETUP_FAILED); |
| 526 |
return 0; |
| 527 |
} |
| 528 |
cctx->str = NULL; |
| 529 |
cctx->key_set = 1; |
| 530 |
} |
| 531 |
if (iv) { |
| 532 |
memcpy(ctx->iv, iv, 15 - cctx->L); |
| 533 |
cctx->iv_set = 1; |
| 534 |
} |
| 535 |
return 1; |
| 536 |
} |
| 537 |
|
| 538 |
static int aria_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
| 539 |
{ |
| 540 |
EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, c); |
| 541 |
|
| 542 |
switch (type) { |
| 543 |
case EVP_CTRL_INIT: |
| 544 |
cctx->key_set = 0; |
| 545 |
cctx->iv_set = 0; |
| 546 |
cctx->L = 8; |
| 547 |
cctx->M = 12; |
| 548 |
cctx->tag_set = 0; |
| 549 |
cctx->len_set = 0; |
| 550 |
cctx->tls_aad_len = -1; |
| 551 |
return 1; |
| 552 |
|
| 553 |
case EVP_CTRL_GET_IVLEN: |
| 554 |
*(int *)ptr = 15 - cctx->L; |
| 555 |
return 1; |
| 556 |
|
| 557 |
case EVP_CTRL_AEAD_TLS1_AAD: |
| 558 |
/* Save the AAD for later use */ |
| 559 |
if (arg != EVP_AEAD_TLS1_AAD_LEN) |
| 560 |
return 0; |
| 561 |
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg); |
| 562 |
cctx->tls_aad_len = arg; |
| 563 |
{ |
| 564 |
uint16_t len = EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8 |
| 565 |
| EVP_CIPHER_CTX_buf_noconst(c)[arg - 1]; |
| 566 |
/* Correct length for explicit IV */ |
| 567 |
if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN) |
| 568 |
return 0; |
| 569 |
len -= EVP_CCM_TLS_EXPLICIT_IV_LEN; |
| 570 |
/* If decrypting correct for tag too */ |
| 571 |
if (!EVP_CIPHER_CTX_is_encrypting(c)) { |
| 572 |
if (len < cctx->M) |
| 573 |
return 0; |
| 574 |
len -= cctx->M; |
| 575 |
} |
| 576 |
EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8; |
| 577 |
EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff; |
| 578 |
} |
| 579 |
/* Extra padding: tag appended to record */ |
| 580 |
return cctx->M; |
| 581 |
|
| 582 |
case EVP_CTRL_CCM_SET_IV_FIXED: |
| 583 |
/* Sanity check length */ |
| 584 |
if (arg != EVP_CCM_TLS_FIXED_IV_LEN) |
| 585 |
return 0; |
| 586 |
/* Just copy to first part of IV */ |
| 587 |
memcpy(c->iv, ptr, arg); |
| 588 |
return 1; |
| 589 |
|
| 590 |
case EVP_CTRL_AEAD_SET_IVLEN: |
| 591 |
arg = 15 - arg; |
| 592 |
/* fall through */ |
| 593 |
case EVP_CTRL_CCM_SET_L: |
| 594 |
if (arg < 2 || arg > 8) |
| 595 |
return 0; |
| 596 |
cctx->L = arg; |
| 597 |
return 1; |
| 598 |
case EVP_CTRL_AEAD_SET_TAG: |
| 599 |
if ((arg & 1) || arg < 4 || arg > 16) |
| 600 |
return 0; |
| 601 |
if (EVP_CIPHER_CTX_is_encrypting(c) && ptr) |
| 602 |
return 0; |
| 603 |
if (ptr) { |
| 604 |
cctx->tag_set = 1; |
| 605 |
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg); |
| 606 |
} |
| 607 |
cctx->M = arg; |
| 608 |
return 1; |
| 609 |
|
| 610 |
case EVP_CTRL_AEAD_GET_TAG: |
| 611 |
if (!EVP_CIPHER_CTX_is_encrypting(c) || !cctx->tag_set) |
| 612 |
return 0; |
| 613 |
if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg)) |
| 614 |
return 0; |
| 615 |
cctx->tag_set = 0; |
| 616 |
cctx->iv_set = 0; |
| 617 |
cctx->len_set = 0; |
| 618 |
return 1; |
| 619 |
|
| 620 |
case EVP_CTRL_COPY: { |
| 621 |
EVP_CIPHER_CTX *out = ptr; |
| 622 |
EVP_ARIA_CCM_CTX *cctx_out = EVP_C_DATA(EVP_ARIA_CCM_CTX, out); |
| 623 |
if (cctx->ccm.key) { |
| 624 |
if (cctx->ccm.key != &cctx->ks) |
| 625 |
return 0; |
| 626 |
cctx_out->ccm.key = &cctx_out->ks; |
| 627 |
} |
| 628 |
return 1; |
| 629 |
} |
| 630 |
|
| 631 |
default: |
| 632 |
return -1; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
static int aria_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
| 637 |
const unsigned char *in, size_t len) |
| 638 |
{ |
| 639 |
EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, ctx); |
| 640 |
CCM128_CONTEXT *ccm = &cctx->ccm; |
| 641 |
|
| 642 |
/* Encrypt/decrypt must be performed in place */ |
| 643 |
if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->M)) |
| 644 |
return -1; |
| 645 |
/* If encrypting set explicit IV from sequence number (start of AAD) */ |
| 646 |
if (EVP_CIPHER_CTX_is_encrypting(ctx)) |
| 647 |
memcpy(out, EVP_CIPHER_CTX_buf_noconst(ctx), |
| 648 |
EVP_CCM_TLS_EXPLICIT_IV_LEN); |
| 649 |
/* Get rest of IV from explicit IV */ |
| 650 |
memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, |
| 651 |
EVP_CCM_TLS_EXPLICIT_IV_LEN); |
| 652 |
/* Correct length value */ |
| 653 |
len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M; |
| 654 |
if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, |
| 655 |
len)) |
| 656 |
return -1; |
| 657 |
/* Use saved AAD */ |
| 658 |
CRYPTO_ccm128_aad(ccm, EVP_CIPHER_CTX_buf_noconst(ctx), |
| 659 |
cctx->tls_aad_len); |
| 660 |
/* Fix buffer to point to payload */ |
| 661 |
in += EVP_CCM_TLS_EXPLICIT_IV_LEN; |
| 662 |
out += EVP_CCM_TLS_EXPLICIT_IV_LEN; |
| 663 |
if (EVP_CIPHER_CTX_is_encrypting(ctx)) { |
| 664 |
if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str) |
| 665 |
: CRYPTO_ccm128_encrypt(ccm, in, out, len)) |
| 666 |
return -1; |
| 667 |
if (!CRYPTO_ccm128_tag(ccm, out + len, cctx->M)) |
| 668 |
return -1; |
| 669 |
return (int)(len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M); |
| 670 |
} else { |
| 671 |
if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, cctx->str) |
| 672 |
: !CRYPTO_ccm128_decrypt(ccm, in, out, len)) { |
| 673 |
unsigned char tag[16]; |
| 674 |
if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) { |
| 675 |
if (!CRYPTO_memcmp(tag, in + len, cctx->M)) |
| 676 |
return (int)len; |
| 677 |
} |
| 678 |
} |
| 679 |
OPENSSL_cleanse(out, len); |
| 680 |
return -1; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
static int aria_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
| 685 |
const unsigned char *in, size_t len) |
| 686 |
{ |
| 687 |
EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, ctx); |
| 688 |
CCM128_CONTEXT *ccm = &cctx->ccm; |
| 689 |
|
| 690 |
/* If not set up, return error */ |
| 691 |
if (!cctx->key_set) |
| 692 |
return -1; |
| 693 |
|
| 694 |
if (cctx->tls_aad_len >= 0) |
| 695 |
return aria_ccm_tls_cipher(ctx, out, in, len); |
| 696 |
|
| 697 |
/* EVP_*Final() doesn't return any data */ |
| 698 |
if (in == NULL && out != NULL) |
| 699 |
return 0; |
| 700 |
|
| 701 |
if (!cctx->iv_set) |
| 702 |
return -1; |
| 703 |
|
| 704 |
if (!out) { |
| 705 |
if (!in) { |
| 706 |
if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len)) |
| 707 |
return -1; |
| 708 |
cctx->len_set = 1; |
| 709 |
return (int)len; |
| 710 |
} |
| 711 |
/* If have AAD need message length */ |
| 712 |
if (!cctx->len_set && len) |
| 713 |
return -1; |
| 714 |
CRYPTO_ccm128_aad(ccm, in, len); |
| 715 |
return (int)len; |
| 716 |
} |
| 717 |
|
| 718 |
/* The tag must be set before actually decrypting data */ |
| 719 |
if (!EVP_CIPHER_CTX_is_encrypting(ctx) && !cctx->tag_set) |
| 720 |
return -1; |
| 721 |
|
| 722 |
/* If not set length yet do it */ |
| 723 |
if (!cctx->len_set) { |
| 724 |
if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len)) |
| 725 |
return -1; |
| 726 |
cctx->len_set = 1; |
| 727 |
} |
| 728 |
if (EVP_CIPHER_CTX_is_encrypting(ctx)) { |
| 729 |
if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str) |
| 730 |
: CRYPTO_ccm128_encrypt(ccm, in, out, len)) |
| 731 |
return -1; |
| 732 |
cctx->tag_set = 1; |
| 733 |
return (int)len; |
| 734 |
} else { |
| 735 |
int rv = -1; |
| 736 |
if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, |
| 737 |
cctx->str) |
| 738 |
: !CRYPTO_ccm128_decrypt(ccm, in, out, len)) { |
| 739 |
unsigned char tag[16]; |
| 740 |
if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) { |
| 741 |
if (!CRYPTO_memcmp(tag, EVP_CIPHER_CTX_buf_noconst(ctx), |
| 742 |
cctx->M)) |
| 743 |
rv = (int)len; |
| 744 |
} |
| 745 |
} |
| 746 |
if (rv == -1) |
| 747 |
OPENSSL_cleanse(out, len); |
| 748 |
cctx->iv_set = 0; |
| 749 |
cctx->tag_set = 0; |
| 750 |
cctx->len_set = 0; |
| 751 |
return rv; |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
#define aria_ccm_cleanup NULL |
| 756 |
|
| 757 |
#define ARIA_AUTH_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \ |
| 758 |
| EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \ |
| 759 |
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \ |
| 760 |
| EVP_CIPH_CUSTOM_COPY | EVP_CIPH_FLAG_AEAD_CIPHER \ |
| 761 |
| EVP_CIPH_CUSTOM_IV_LENGTH) |
| 762 |
|
| 763 |
#define BLOCK_CIPHER_aead(keylen, mode, MODE) \ |
| 764 |
static const EVP_CIPHER aria_##keylen##_##mode = { \ |
| 765 |
NID_aria_##keylen##_##mode, \ |
| 766 |
1, keylen / 8, 12, \ |
| 767 |
ARIA_AUTH_FLAGS | EVP_CIPH_##MODE##_MODE, \ |
| 768 |
EVP_ORIG_GLOBAL, \ |
| 769 |
aria_##mode##_init_key, \ |
| 770 |
aria_##mode##_cipher, \ |
| 771 |
aria_##mode##_cleanup, \ |
| 772 |
sizeof(EVP_ARIA_##MODE##_CTX), \ |
| 773 |
NULL, NULL, aria_##mode##_ctrl, NULL \ |
| 774 |
}; \ |
| 775 |
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ |
| 776 |
{ \ |
| 777 |
return (EVP_CIPHER *)&aria_##keylen##_##mode; \ |
| 778 |
} |
| 779 |
|
| 780 |
BLOCK_CIPHER_aead(128, gcm, GCM) |
| 781 |
BLOCK_CIPHER_aead(192, gcm, GCM) |
| 782 |
BLOCK_CIPHER_aead(256, gcm, GCM) |
| 783 |
|
| 784 |
BLOCK_CIPHER_aead(128, ccm, CCM) |
| 785 |
BLOCK_CIPHER_aead(192, ccm, CCM) |
| 786 |
BLOCK_CIPHER_aead(256, ccm, CCM) |
| 787 |
|
| 788 |
#endif |