From b2fb5441c4613465e0c8f9203cdec4f8083d2fdd Mon Sep 17 00:00:00 2001 From: Franck LENORMAND Date: Fri, 5 Oct 2018 16:08:25 +0200 Subject: [PATCH] MLK-19801-1 crypto: caam - add tag functionality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add functions to tag an object with metadata(configuration). It is possible to: - create metadata: - init_tag_object_header - init_blackey_conf - set_tag_object_conf - retrieve metadata: - get_tag_object_conf - get_blackey_conf The API expects an object to be a space a memory with an address and a size. The implementation of the tag is currently exposed but users shouldn't access it directly, they should use the functions provided. Signed-off-by: Franck LENORMAND (cherry picked from commit ebbb132da8e7f9de7f3d375eff8d87f684feb1eb) Signed-off-by: Vipul Kumar (cherry picked from commit 8b6f6b4474be33ee271dfe2cce79f9f6335733aa) -make tag functionality depend on JR -change commit headline prefix Signed-off-by: Horia Geantă --- drivers/crypto/caam/Kconfig | 10 ++ drivers/crypto/caam/Makefile | 1 + drivers/crypto/caam/tag_object.c | 260 +++++++++++++++++++++++++++++++++++++++ drivers/crypto/caam/tag_object.h | 100 +++++++++++++++ 4 files changed, 371 insertions(+) create mode 100644 drivers/crypto/caam/tag_object.c create mode 100644 drivers/crypto/caam/tag_object.h --- a/drivers/crypto/caam/Kconfig +++ b/drivers/crypto/caam/Kconfig @@ -148,6 +148,16 @@ config CRYPTO_DEV_FSL_CAAM_RNG_API Selecting this will register the SEC4 hardware rng to the hw_random API for suppying the kernel entropy pool. +config CRYPTO_DEV_FSL_CAAM_TK_API + bool "Register tagged key cryptography implementations with Crypto API" + depends on CRYPTO_DEV_FSL_CAAM_CRYPTO_API + help + Selecting this will register algorithms supporting tagged + key. + + Tagged key are keys that contains metadata indicating what + they are and how to handle them. + config CRYPTO_DEV_FSL_CAAM_RNG_TEST bool "Test caam rng" depends on CRYPTO_DEV_FSL_CAAM_RNG_API --- a/drivers/crypto/caam/Makefile +++ b/drivers/crypto/caam/Makefile @@ -24,6 +24,7 @@ caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_PKC caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_SM) += sm_store.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_SM_TEST) += sm_test.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_SECVIO) += secvio.o +caam-jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_TK_API) += tag_object.o caam-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += qi.o ifneq ($(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI),) --- /dev/null +++ b/drivers/crypto/caam/tag_object.c @@ -0,0 +1,260 @@ +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause +/* + * Copyright 2018-2019 NXP + */ + +#include +#include +#include + +#include "tag_object.h" +#include "desc.h" + +/* + * Magic number to clearly identify the structure is for us + * 0x54 = 'T' + * 0x61 = 'a' + * 0x67 = 'g' + * 0x4f = 'O' + */ +#define TAG_OBJECT_MAGIC 0x5461674f + +/** + * struct tagged_object - Structure representing a tagged object + * @tag : The configuration of the data + * @object : The object + */ +struct tagged_object { + struct tag_object_conf tag; + char object; +}; + +/** + * is_bk_type() - Determines if black key type. + * @type: The type + * + * Return: True if black key type, False otherwise. + */ +static bool is_bk_type(enum tag_type type) +{ + return (type == TAG_TYPE_BLACK_KEY_ECB) || + (type == TAG_TYPE_BLACK_KEY_ECB_TRUSTED) || + (type == TAG_TYPE_BLACK_KEY_CCM) || + (type == TAG_TYPE_BLACK_KEY_CCM_TRUSTED); +} + +/** + * is_bk_conf() - Determines if black key conf. + * @tag_obj_conf : The tag object conf + * + * Return: True if black key conf, False otherwise. + */ +bool is_bk_conf(const struct tag_object_conf *tag_obj_conf) +{ + return is_bk_type(tag_obj_conf->header.type); +} +EXPORT_SYMBOL(is_bk_conf); + +/** + * get_bk_conf() - Gets the block conf. + * @tag_obj_conf : The tag object conf + * + * Return: The block conf. + */ +const struct blackey_conf *get_bk_conf(const struct tag_object_conf *tag_obj_conf) +{ + return &tag_obj_conf->conf.bk_conf; +} + +/** + * get_tag_object_overhead() - Gets the tag object overhead. + * + * Return: The tag object overhead. + */ +size_t get_tag_object_overhead(void) +{ + return TAG_OVERHEAD; +} +EXPORT_SYMBOL(get_tag_object_overhead); + +/** + * is_valid_type() - Determines if valid type. + * @type : The type + * + * Return: True if valid type, False otherwise. + */ +bool is_valid_type(enum tag_type type) +{ + return (type > TAG_TYPE_NOT_SUPPORTED) && (type < NB_TAG_TYPE); +} +EXPORT_SYMBOL(is_valid_type); + +/** + * is_valid_header() - Determines if valid header. + * @header : The header + * + * Return: True if valid tag object conf, False otherwise. + */ +static bool is_valid_header(const struct conf_header *header) +{ + bool valid = header->_magic_number == TAG_OBJECT_MAGIC; + + valid = valid && is_valid_type(header->type); + + return valid; +} + +/** + * is_valid_tag_object_conf() - Determines if valid tag object conf. + * @tag_obj_conf : The tag object conf + * + * Return: True if valid header, False otherwise. + */ +bool is_valid_tag_object_conf(const struct tag_object_conf *tag_obj_conf) +{ + bool valid = true; + + valid = is_valid_header(&tag_obj_conf->header); + + return valid; +} +EXPORT_SYMBOL(is_valid_tag_object_conf); + +/** + * get_tag_object_conf() - Gets a pointer on the tag object conf. + * @tag_obj_conf : The tag object conf + * @buffer : The buffer + * @size : The size + * + * Return: 0 if success, else error code + */ +int get_tag_object_conf(void *buffer, size_t size, + struct tag_object_conf **tag_obj_conf) +{ + bool is_valid; + struct tagged_object *tago = (struct tagged_object *)buffer; + size_t conf_size = get_tag_object_overhead(); + + /* Check we can retrieve the conf */ + if (size < conf_size) + return -EINVAL; + + is_valid = is_valid_tag_object_conf(&tago->tag); + + *tag_obj_conf = &tago->tag; + + return (is_valid) ? 0 : -EINVAL; +} +EXPORT_SYMBOL(get_tag_object_conf); + +/** + * init_tag_object_header() - Initialize the tag object header + * @conf_header : The configuration header + * @type : The type + * + * It initialize the header structure + */ +void init_tag_object_header(struct conf_header *conf_header, + enum tag_type type) +{ + conf_header->_magic_number = TAG_OBJECT_MAGIC; + conf_header->type = type; +} +EXPORT_SYMBOL(init_tag_object_header); + +/** + * set_tag_object_conf() - Sets the tag object conf. + * @tag_obj_conf : The tag object conf + * @buffer : The buffer + * @obj_size : The object size + * @to_size : The tagged object size + * + * Return: 0 if success, else error code + */ +int set_tag_object_conf(const struct tag_object_conf *tag_obj_conf, + void *buffer, size_t obj_size, u32 *to_size) +{ + struct tagged_object *tago = buffer; + size_t conf_size = get_tag_object_overhead(); + size_t req_size = obj_size + conf_size; + + /* Check we can set the conf */ + if (*to_size < req_size) { + *to_size = req_size; + return -EINVAL; + } + + /* Move the object */ + memmove(&tago->object, buffer, obj_size); + + /* Copy the tag */ + memcpy(&tago->tag, tag_obj_conf, conf_size); + + *to_size = req_size; + + return 0; +} +EXPORT_SYMBOL(set_tag_object_conf); + +/** + * init_blackey_conf() - Initialize the black key configuration + * @blackey_conf : The blackey conf + * @len : The length + * @ccm : The ccm + * @tk : The trusted key + * + * It initialize the black key configuration structure + */ +void init_blackey_conf(struct blackey_conf *blackey_conf, + size_t len, bool ccm, bool tk) +{ + blackey_conf->real_len = len; + blackey_conf->load = KEY_ENC + | ((ccm) ? KEY_EKT : 0) + | ((tk) ? KEY_TK : 0); +} +EXPORT_SYMBOL(init_blackey_conf); + +/** + * get_blackey_conf() - Get the black key configuration + * @blackey_conf : The blackey conf + * @real_len : The real length + * @load_param : The load parameter + * + * It retrieve the black key configuration + */ +void get_blackey_conf(const struct blackey_conf *blackey_conf, + u32 *real_len, u32 *load_param) +{ + *real_len = blackey_conf->real_len; + *load_param = blackey_conf->load; +} +EXPORT_SYMBOL(get_blackey_conf); + +/** + * get_tagged_data() - Get a pointer on the data and the size + * @tagged_object : Pointer on the tagged object + * @tagged_object_size : tagged object size in bytes + * @data : Pointer on the data + * @data_size : data size in bytes + * + * Return: 0 if success, else error code + */ +int get_tagged_data(void *tagged_object, size_t tagged_object_size, + void **data, u32 *data_size) +{ + struct tagged_object *tago = + (struct tagged_object *)tagged_object; + size_t conf_size = get_tag_object_overhead(); + + /* Check we can retrieve the object */ + if (tagged_object_size < conf_size) + return -EINVAL; + + /* Retrieve the object */ + *data = &tago->object; + *data_size = tagged_object_size - conf_size; + + return 0; +} +EXPORT_SYMBOL(get_tagged_data); --- /dev/null +++ b/drivers/crypto/caam/tag_object.h @@ -0,0 +1,100 @@ +/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */ +/* + * Copyright 2018-2019 NXP + */ + +#ifndef _TAG_OBJECT_H_ +#define _TAG_OBJECT_H_ + +#include + +#define TAG_MIN_SIZE (2 * sizeof(struct conf_header)) +#define TAG_OVERHEAD sizeof(struct tag_object_conf) + +/** + * enum tag_type - Type of data represented by the tag + */ +enum tag_type { + /** @TAG_TYPE_NOT_SUPPORTED: The type is not supported */ + TAG_TYPE_NOT_SUPPORTED = 0, + + /* Type that passes is_tag_type_valid() */ + /** @TAG_TYPE_BLACK_KEY_ECB: Black key encrypted with ECB */ + TAG_TYPE_BLACK_KEY_ECB, + /** + * @TAG_TYPE_BLACK_KEY_ECB_TRUSTED: ECB Black key created by trusted + * descriptor + */ + TAG_TYPE_BLACK_KEY_ECB_TRUSTED, + /** @TAG_TYPE_BLACK_KEY_CCM: Black key encrypted with CCM */ + TAG_TYPE_BLACK_KEY_CCM, + /** + * @TAG_TYPE_BLACK_KEY_CCM_TRUSTED: CCM Black key created by trusted + * descriptor + */ + TAG_TYPE_BLACK_KEY_CCM_TRUSTED, + + /** @NB_TAG_TYPE: Number of type of tag */ + NB_TAG_TYPE, +}; + +/** + * struct conf_header - Common struture holding the type of data and the magic + * number + * @_magic_number : A magic number to identify the structure + * @type : The type of data contained + */ +struct conf_header { + u32 _magic_number; + u32 type; +}; + +/** + * struct blackey_conf - Configuration for a black key + * @load : Load parameter for CAAM + * @real_len : Length of the key before encryption + */ +struct blackey_conf { + u32 load; + u32 real_len; +}; + +/** + * struct tag_object_conf - Common structure which is the tag applied to data + * @header : Part of the data initialized with common function + * :c:func:`init_tag_object_header` + * @conf : Configuration data about the object tagged, initialized with + * specific function + */ +struct tag_object_conf { + struct conf_header header; + union { + struct blackey_conf bk_conf; + } conf; +}; + +bool is_bk_conf(const struct tag_object_conf *tag_obj_conf); + +bool is_valid_tag_object_conf(const struct tag_object_conf *tag_obj_conf); + +void init_tag_object_header(struct conf_header *conf_header, + enum tag_type type); + +int get_tag_object_conf(void *buffer, size_t buffer_size, + struct tag_object_conf **tag_obj_conf); + +int set_tag_object_conf(const struct tag_object_conf *tag_obj_conf, + void *buffer, size_t obj_size, u32 *to_size); + +size_t get_tag_object_overhead(void); + +void get_blackey_conf(const struct blackey_conf *blackey_conf, + u32 *real_len, u32 *load_param); + +void init_blackey_conf(struct blackey_conf *blackey_conf, + size_t len, bool ccm, bool tk); + +int get_tagged_data(void *buffer, size_t buffer_size, + void **data, u32 *data_size); + +#endif /* _TAG_OBJECT_H_ */ /a> 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
/**
 * Copyright (c) 2013-2014 Philipp Jakubeit, Signe Rüsch, Dominik Schürmann
 *
 * Licensed under the Bouncy Castle License (MIT license). See LICENSE file for details.
 */

package org.sufficientlysecure.keychain.ui;

import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.WindowManager;
import android.widget.Toast;

import org.spongycastle.bcpg.HashAlgorithmTags;
import org.spongycastle.util.encoders.Hex;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.Iso7816TLV;
import org.sufficientlysecure.keychain.util.Log;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Locale;

/**
 * This class provides a communication interface to OpenPGP applications on ISO SmartCard compliant
 * NFC devices.
 *
 * For the full specs, see http://g10code.com/docs/openpgp-card-2.0.pdf
 */
@TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
public class NfcActivity extends ActionBarActivity {

    // actions
    public static final String ACTION_SIGN_HASH = "sign_hash";
    public static final String ACTION_DECRYPT_SESSION_KEY = "decrypt_session_key";

    // always
    public static final String EXTRA_KEY_ID = "key_id";
    public static final String EXTRA_PIN = "pin";
    // special extra for OpenPgpService
    public static final String EXTRA_DATA = "data";

    // sign
    public static final String EXTRA_NFC_HASH_TO_SIGN = "nfc_hash";
    public static final String EXTRA_NFC_HASH_ALGO = "nfc_hash_algo";

    // decrypt
    public static final String EXTRA_NFC_ENC_SESSION_KEY = "encrypted_session_key";

    private Intent mServiceIntent;

    private static final int TIMEOUT = 100000;

    private NfcAdapter mNfcAdapter;
    private IsoDep mIsoDep;
    private String mAction;

    private String mPin;
    private Long mKeyId;

    // sign
    private byte[] mHashToSign;
    private int mHashAlgo;

    // decrypt
    private byte[] mEncryptedSessionKey;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(Constants.TAG, "NfcActivity.onCreate");

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.nfc_activity);

        Intent intent = getIntent();
        Bundle data = intent.getExtras();
        String action = intent.getAction();

        // if we get are passed a key id, save it for the check
        if (data.containsKey(EXTRA_KEY_ID)) {
            mKeyId = data.getLong(EXTRA_KEY_ID);
        }

        if (ACTION_SIGN_HASH.equals(action)) {
            mAction = action;
            mPin = data.getString(EXTRA_PIN);
            mHashToSign = data.getByteArray(EXTRA_NFC_HASH_TO_SIGN);
            mHashAlgo = data.getInt(EXTRA_NFC_HASH_ALGO);
            mServiceIntent = data.getParcelable(EXTRA_DATA);