2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* Dispatch functions for ccm mode */
12 #include "prov/ciphercommon.h"
13 #include "prov/ciphercommon_ccm.h"
14 #include "prov/providercommon.h"
15 #include "prov/providercommonerr.h"
17 static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
18 size_t *padlen, const unsigned char *in,
21 static int ccm_tls_init(PROV_CCM_CTX *ctx, unsigned char *aad, size_t alen)
25 if (!ossl_prov_is_running() || alen != EVP_AEAD_TLS1_AAD_LEN)
28 /* Save the aad for later use. */
29 memcpy(ctx->buf, aad, alen);
30 ctx->tls_aad_len = alen;
32 len = ctx->buf[alen - 2] << 8 | ctx->buf[alen - 1];
33 if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
36 /* Correct length for explicit iv. */
37 len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
42 /* Correct length for tag. */
45 ctx->buf[alen - 2] = (unsigned char)(len >> 8);
46 ctx->buf[alen - 1] = (unsigned char)(len & 0xff);
48 /* Extra padding: tag appended to record. */
52 static int ccm_tls_iv_set_fixed(PROV_CCM_CTX *ctx, unsigned char *fixed,
55 if (flen != EVP_CCM_TLS_FIXED_IV_LEN)
58 /* Copy to first part of the iv. */
59 memcpy(ctx->iv, fixed, flen);
63 static size_t ccm_get_ivlen(PROV_CCM_CTX *ctx)
68 int ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
70 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
74 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
76 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
77 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
80 if ((p->data_size & 1) || (p->data_size < 4) || p->data_size > 16) {
81 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAGLEN);
85 if (p->data != NULL) {
87 ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
90 memcpy(ctx->buf, p->data, p->data_size);
93 ctx->m = p->data_size;
96 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
100 if (!OSSL_PARAM_get_size_t(p, &sz)) {
101 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
105 if (ivlen < 2 || ivlen > 8) {
106 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
112 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
114 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
115 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
118 sz = ccm_tls_init(ctx, p->data, p->data_size);
120 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
123 ctx->tls_aad_pad_sz = sz;
126 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
128 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
129 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
132 if (ccm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
133 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
141 int ccm_get_ctx_params(void *vctx, OSSL_PARAM params[])
143 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
146 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
147 if (p != NULL && !OSSL_PARAM_set_size_t(p, ccm_get_ivlen(ctx))) {
148 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
152 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
156 if (!OSSL_PARAM_set_size_t(p, m)) {
157 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
162 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
164 if (ccm_get_ivlen(ctx) > p->data_size) {
165 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
168 if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size)
169 && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) {
170 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
175 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);
177 if (ccm_get_ivlen(ctx) > p->data_size) {
178 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
181 if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size)
182 && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) {
183 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
188 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
189 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
190 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
194 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
195 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
196 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
200 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
202 if (!ctx->enc || !ctx->tag_set) {
203 ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOTSET);
206 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
207 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
210 if (!ctx->hw->gettag(ctx, p->data, p->data_size))
219 static int ccm_init(void *vctx, const unsigned char *key, size_t keylen,
220 const unsigned char *iv, size_t ivlen, int enc)
222 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
224 if (!ossl_prov_is_running())
230 if (ivlen != ccm_get_ivlen(ctx)) {
231 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
234 memcpy(ctx->iv, iv, ivlen);
238 if (keylen != ctx->keylen) {
239 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN);
242 return ctx->hw->setkey(ctx, key, keylen);
247 int ccm_einit(void *vctx, const unsigned char *key, size_t keylen,
248 const unsigned char *iv, size_t ivlen)
250 return ccm_init(vctx, key, keylen, iv, ivlen, 1);
253 int ccm_dinit(void *vctx, const unsigned char *key, size_t keylen,
254 const unsigned char *iv, size_t ivlen)
256 return ccm_init(vctx, key, keylen, iv, ivlen, 0);
259 int ccm_stream_update(void *vctx, unsigned char *out, size_t *outl,
260 size_t outsize, const unsigned char *in,
263 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
266 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
270 if (!ccm_cipher_internal(ctx, out, outl, in, inl)) {
271 ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
277 int ccm_stream_final(void *vctx, unsigned char *out, size_t *outl,
280 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
283 if (!ossl_prov_is_running())
286 i = ccm_cipher_internal(ctx, out, outl, NULL, 0);
294 int ccm_cipher(void *vctx, unsigned char *out, size_t *outl, size_t outsize,
295 const unsigned char *in, size_t inl)
297 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
299 if (!ossl_prov_is_running())
303 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
307 if (ccm_cipher_internal(ctx, out, outl, in, inl) <= 0)
314 /* Copy the buffered iv */
315 static int ccm_set_iv(PROV_CCM_CTX *ctx, size_t mlen)
317 const PROV_CCM_HW *hw = ctx->hw;
319 if (!hw->setiv(ctx, ctx->iv, ccm_get_ivlen(ctx), mlen))
325 static int ccm_tls_cipher(PROV_CCM_CTX *ctx,
326 unsigned char *out, size_t *padlen,
327 const unsigned char *in, size_t len)
332 if (!ossl_prov_is_running())
335 /* Encrypt/decrypt must be performed in place */
336 if (in == NULL || out != in || len < EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m)
339 /* If encrypting set explicit IV from sequence number (start of AAD) */
341 memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
342 /* Get rest of IV from explicit IV */
343 memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
344 /* Correct length value */
345 len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
346 if (!ccm_set_iv(ctx, len))
350 if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len))
353 /* Fix buffer to point to payload */
354 in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
355 out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
357 if (!ctx->hw->auth_encrypt(ctx, in, out, len, out + len, ctx->m))
359 olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
361 if (!ctx->hw->auth_decrypt(ctx, in, out, len,
362 (unsigned char *)in + len, ctx->m))
372 static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
373 size_t *padlen, const unsigned char *in,
378 const PROV_CCM_HW *hw = ctx->hw;
380 /* If no key set, return error */
384 if (ctx->tls_aad_len != UNINITIALISED_SIZET)
385 return ccm_tls_cipher(ctx, out, padlen, in, len);
387 /* EVP_*Final() doesn't return any data */
388 if (in == NULL && out != NULL)
396 if (!ccm_set_iv(ctx, len))
399 /* If we have AAD, we need a message length */
400 if (!ctx->len_set && len)
402 if (!hw->setaad(ctx, in, len))
406 /* If not set length yet do it */
407 if (!ctx->len_set && !ccm_set_iv(ctx, len))
411 if (!hw->auth_encrypt(ctx, in, out, len, NULL, 0))
415 /* The tag must be set before actually decrypting data */
419 if (!hw->auth_decrypt(ctx, in, out, len, ctx->buf, ctx->m))
421 /* Finished - reset flags so calling this method again will fail */
435 void ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw)
437 ctx->keylen = keybits / 8;
444 ctx->tls_aad_len = UNINITIALISED_SIZET;