Module: Toybox.Cryptography

Overview

The Cryptography Module will allow the application to create a Cipher object that can encrypt and decrypt ByteArray objects.

Since:

API Level 3.0.0

Supported Devices:

Classes Under Namespace

Classes: Cipher, CipherBasedMessageAuthenticationCode, Hash, HashBasedMessageAuthenticationCode, InvalidBlockSizeException, Key, KeyAgreement, KeyPair

Constant Summary

HashAlgorithm

Name Value Since Description
HASH_SHA1 0

API Level 3.0.0

SHA-1 implementation for Hash objects

HASH_SHA256 1

API Level 3.0.0

SHA-256 implementation for Hash objects

HASH_MD5 2

API Level 3.0.0

MD5 implementation for Hash objects

CipherAlgorithm

Name Value Since Description
CIPHER_AES128 0

API Level 3.0.0

AES128 implementation for Cipher objects

CIPHER_AES256 1

API Level 3.0.0

AES256 implementation for Cipher objects

EncryptionMode

Name Value Since Description
MODE_ECB 0

API Level 3.0.0

Electronic Code Book (ECB)

This is the simplest encryption mode. Each plaintext block is directly encrypted into a ciphertext block, independent of any other block. This mode exposes frequency of symbols in your plaintext. Other modes (e.g. CBC) are recommended.

MODE_CBC 1

API Level 3.0.0

Cipher-Block Chaining

Each ciphertext block depends on the current and all previous plaintext blocks. An Initialization Vector (IV) is required. The IV is a data block to be transmitted to the receiver. The IV can be made public, but it must be authenticated by the receiver and it should be picked randomly.

KeyPairAlgorithm

Name Value Since Description
KEY_PAIR_ELLIPTIC_CURVE_SECP224R1 0

224-bit secp224r1 Elliptic Curve

Based on the algebraic structure of elliptic curves over finite fields. ECC requires smaller keys compared to non-ECC cryptography to provide equivalent security.

KEY_PAIR_ELLIPTIC_CURVE_SECP256R1 1

256-bit secp256r1 Elliptic Curve

Based on the algebraic structure of elliptic curves over finite fields. ECC requires smaller keys compared to non-ECC cryptography to provide equivalent security.

KeyAgreementProtocol

Name Value Since Description
KEY_AGREEMENT_ECDH 0

Elliptic Curve Diffie-Hellman (ECDH)

Instance Method Summary collapse

Instance Method Details

createPublicKey(algorithm as HashAlgorithm, bytes as ByteArray) as Key

Create a public Key object from bytes to be added to a KeyAgreement.

If a public key is received from another party, it can be converted to a Key object using this method.

Parameters:

  • algorithm(Number)

    The hash algorithm to use as a KEY_PAIR_ELLIPTIC_CURVE_* constant

  • bytes(ByteArray)

    The public key bytes to be used to generate the Key

Supported Devices:

Returns:

Throws:

  • (UnexpectedTypeException)

    Thrown if an argument is not the correct type

  • (InvalidValueException)

    Thrown if the ByteArray provided is not the correct size for the selected algorithm, or the algorithm selected is not supported.

randomBytes(size as Number) as ByteArray

Generates cryptographically random bytes.

Parameters:

  • size(Number)

    The number of requested random bytes

Returns:

  • ByteArray

    A ByteArray of the requested size filled with cryptographically random bytes

Since:

API Level 3.0.0


Generated Apr 22, 2021 10:10:32 AM