Lines Matching +full:in3 +full:- +full:in2
2 * Copyright 2011-2024 The OpenSSL Project Authors. All Rights Reserved.
37 * The state of a DRBG AES-CTR.
56 * Implementation of NIST SP 800-90A CTR DRBG.
60 unsigned char *p = &ctr->V[0];
64 --n;
82 n = inlen < ctr->keylen ? inlen : ctr->keylen;
84 ctr->K[i] ^= in[i];
85 if (inlen <= ctr->keylen)
88 n = inlen - ctr->keylen;
94 ctr->V[i] ^= in[i + ctr->keylen];
98 * Process a complete block using BCC algorithm of SP 800-90A 10.3.3
108 if (!EVP_CipherUpdate(ctr->ctx_df, out, &outlen, out, len)
125 if (ctr->keylen != 16) {
129 return ctr_BCC_block(ctr, ctr->KX, in_tmp, AES_BLOCK_SIZE * num_of_blk);
141 memset(ctr->KX, 0, 48);
142 num_of_blk = ctr->keylen == 16 ? 2 : 3;
145 return ctr_BCC_block(ctr, ctr->KX, bltmp, num_of_blk * AES_BLOCK_SIZE);
158 if (ctr->bltmp_pos) {
159 size_t left = 16 - ctr->bltmp_pos;
163 memcpy(ctr->bltmp + ctr->bltmp_pos, in, left);
164 if (!ctr_BCC_blocks(ctr, ctr->bltmp))
166 ctr->bltmp_pos = 0;
167 inlen -= left;
173 for (; inlen >= 16; in += 16, inlen -= 16) {
180 memcpy(ctr->bltmp + ctr->bltmp_pos, in, inlen);
181 ctr->bltmp_pos += inlen;
188 if (ctr->bltmp_pos) {
189 memset(ctr->bltmp + ctr->bltmp_pos, 0, 16 - ctr->bltmp_pos);
190 if (!ctr_BCC_blocks(ctr, ctr->bltmp))
198 const unsigned char *in2, size_t in2len,
199 const unsigned char *in3, size_t in3len)
203 unsigned char *p = ctr->bltmp;
210 if (in2 == NULL)
212 if (in3 == NULL)
225 *p = (unsigned char)((ctr->keylen + 16) & 0xff);
226 ctr->bltmp_pos = 8;
228 || !ctr_BCC_update(ctr, in2, in2len)
229 || !ctr_BCC_update(ctr, in3, in3len)
234 if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->KX, NULL, -1))
237 if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX, &outlen, ctr->KX + ctr->keylen,
241 if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX + 16, &outlen, ctr->KX,
245 if (ctr->keylen != 16)
246 if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX + 32, &outlen,
247 ctr->KX + 16, AES_BLOCK_SIZE)
254 * NB the no-df Update in SP800-90A specifies a constant input length
261 const unsigned char *in2, size_t in2len,
264 PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
270 memcpy(V_tmp, ctr->V, 16);
272 memcpy(V_tmp + 16, ctr->V, 16);
273 if (ctr->keylen == 16) {
277 memcpy(V_tmp + 32, ctr->V, 16);
280 if (!EVP_CipherUpdate(ctr->ctx_ecb, out, &outlen, V_tmp, len)
283 memcpy(ctr->K, out, ctr->keylen);
284 memcpy(ctr->V, out + ctr->keylen, 16);
286 if (ctr->use_df) {
288 if (in1 != NULL || nonce != NULL || in2 != NULL)
289 if (!ctr_df(ctr, in1, in1len, nonce, noncelen, in2, in2len))
293 ctr_XOR(ctr, ctr->KX, drbg->seedlen);
296 ctr_XOR(ctr, in2, in2len);
299 if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->K, NULL, -1)
300 || !EVP_CipherInit_ex(ctr->ctx_ctr, NULL, NULL, ctr->K, NULL, -1))
310 PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
315 memset(ctr->K, 0, sizeof(ctr->K));
316 memset(ctr->V, 0, sizeof(ctr->V));
317 if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->K, NULL, -1))
344 PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
370 --n;
381 PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
391 if (ctr->use_df) {
412 if (!EVP_CipherInit_ex(ctr->ctx_ctr,
413 NULL, NULL, NULL, ctr->V, -1))
416 /*-
419 * than 2^31-1 bytes at a time. We process such huge generate
421 * of AES block size lower than or equal to 2^31-1.
426 ctr32 = GETU32(ctr->V + 12) + blocks;
428 /* 32-bit counter overflow into V. */
430 blocks -= ctr32;
434 ctr96_inc(ctr->V);
436 PUTU32(ctr->V + 12, ctr32);
438 if (!EVP_CipherUpdate(ctr->ctx_ctr, out, &outl, out, buflen)
443 outlen -= buflen;
464 PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
466 OPENSSL_cleanse(ctr->K, sizeof(ctr->K));
467 OPENSSL_cleanse(ctr->V, sizeof(ctr->V));
468 OPENSSL_cleanse(ctr->bltmp, sizeof(ctr->bltmp));
469 OPENSSL_cleanse(ctr->KX, sizeof(ctr->KX));
470 ctr->bltmp_pos = 0;
482 PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
484 PROV_DRBG_VERYIFY_ZEROIZATION(ctr->K);
485 PROV_DRBG_VERYIFY_ZEROIZATION(ctr->V);
486 PROV_DRBG_VERYIFY_ZEROIZATION(ctr->bltmp);
487 PROV_DRBG_VERYIFY_ZEROIZATION(ctr->KX);
488 if (ctr->bltmp_pos != 0)
495 PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
499 drbg->max_request = 1 << 16;
500 if (ctr->use_df) {
501 drbg->min_entropylen = 0;
502 drbg->max_entropylen = DRBG_MAX_LENGTH;
503 drbg->min_noncelen = 0;
504 drbg->max_noncelen = DRBG_MAX_LENGTH;
505 drbg->max_perslen = DRBG_MAX_LENGTH;
506 drbg->max_adinlen = DRBG_MAX_LENGTH;
508 if (ctr->keylen > 0) {
509 drbg->min_entropylen = ctr->keylen;
510 drbg->min_noncelen = drbg->min_entropylen / 2;
513 const size_t len = ctr->keylen > 0 ? drbg->seedlen : DRBG_MAX_LENGTH;
515 drbg->min_entropylen = len;
516 drbg->max_entropylen = len;
518 drbg->min_noncelen = 0;
519 drbg->max_noncelen = 0;
520 drbg->max_perslen = len;
521 drbg->max_adinlen = len;
528 PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
531 if (ctr->cipher_ctr == NULL) {
535 ctr->keylen = keylen = EVP_CIPHER_get_key_length(ctr->cipher_ctr);
536 if (ctr->ctx_ecb == NULL)
537 ctr->ctx_ecb = EVP_CIPHER_CTX_new();
538 if (ctr->ctx_ctr == NULL)
539 ctr->ctx_ctr = EVP_CIPHER_CTX_new();
540 if (ctr->ctx_ecb == NULL || ctr->ctx_ctr == NULL) {
545 if (!EVP_CipherInit_ex(ctr->ctx_ecb,
546 ctr->cipher_ecb, NULL, NULL, NULL, 1)
547 || !EVP_CipherInit_ex(ctr->ctx_ctr,
548 ctr->cipher_ctr, NULL, NULL, NULL, 1)) {
553 drbg->strength = keylen * 8;
554 drbg->seedlen = keylen + 16;
556 if (ctr->use_df) {
565 if (ctr->ctx_df == NULL)
566 ctr->ctx_df = EVP_CIPHER_CTX_new();
567 if (ctr->ctx_df == NULL) {
572 if (!EVP_CipherInit_ex(ctr->ctx_df,
573 ctr->cipher_ecb, NULL, df_key, NULL, 1)) {
581 EVP_CIPHER_CTX_free(ctr->ctx_ecb);
582 EVP_CIPHER_CTX_free(ctr->ctx_ctr);
583 ctr->ctx_ecb = ctr->ctx_ctr = NULL;
597 ctr->use_df = 1;
598 drbg->data = ctr;
616 if (drbg != NULL && (ctr = (PROV_DRBG_CTR *)drbg->data) != NULL) {
617 EVP_CIPHER_CTX_free(ctr->ctx_ecb);
618 EVP_CIPHER_CTX_free(ctr->ctx_ctr);
619 EVP_CIPHER_CTX_free(ctr->ctx_df);
620 EVP_CIPHER_free(ctr->cipher_ecb);
621 EVP_CIPHER_free(ctr->cipher_ctr);
631 PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
635 if (p != NULL && !OSSL_PARAM_set_int(p, ctr->use_df))
640 if (ctr->cipher_ctr == NULL
642 EVP_CIPHER_get0_name(ctr->cipher_ctr)))
664 PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)ctx->data;
665 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
674 ctr->use_df = i != 0;
680 if (p->data_type != OSSL_PARAM_UTF8_STRING)
682 propquery = (const char *)p->data;
686 const char *base = (const char *)p->data;
687 size_t ctr_str_len = sizeof("CTR") - 1;
688 size_t ecb_str_len = sizeof("ECB") - 1;
690 if (p->data_type != OSSL_PARAM_UTF8_STRING
691 || p->data_size < ctr_str_len)
693 if (OPENSSL_strcasecmp("CTR", base + p->data_size - ctr_str_len) != 0) {
697 if ((ecb = OPENSSL_strndup(base, p->data_size)) == NULL) {
701 strcpy(ecb + p->data_size - ecb_str_len, "ECB");
702 EVP_CIPHER_free(ctr->cipher_ecb);
703 EVP_CIPHER_free(ctr->cipher_ctr);
704 ctr->cipher_ctr = EVP_CIPHER_fetch(libctx, base, propquery);
705 ctr->cipher_ecb = EVP_CIPHER_fetch(libctx, ecb, propquery);
707 if (ctr->cipher_ctr == NULL || ctr->cipher_ecb == NULL) {