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

Since:

API Level 3.0.0

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

Since:

API Level 3.0.0

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

Since:

API Level 3.0.0

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

Since:

API Level 3.0.0

Name Value Since Description
KEY_PAIR_ELLIPTIC_CURVE_SECP224R1 0

API Level 3.0.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

API Level 3.0.0

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

Since:

API Level 3.0.0

Name Value Since Description
KEY_AGREEMENT_ECDH 0

API Level 3.0.0

Elliptic Curve Diffie-Hellman (ECDH)

Instance Method Summary collapse

Instance Method Details

createPublicKey(algorithm as Cryptography.HashAlgorithm, bytes as Lang.ByteArray) as Cryptography.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.

Note:

bytes is expected to be in little-endian byte order.

Parameters:

  • algorithm(Lang.Number)

    The hash algorithm to use as a KEY_PAIR_ELLIPTIC_CURVE_* constant

  • bytes(Lang.ByteArray)

    The public key bytes to be used to generate the Key

Supported Devices:

Returns:

See Also:

Since:

API Level 3.0.0

Throws:

randomBytes(size as Lang.Number) as Lang.ByteArray

Generates cryptographically random bytes.

Parameters:

  • size(Lang.Number)

    The number of requested random bytes

Returns:

  • Lang.ByteArray

    A ByteArray of the requested size filled with cryptographically random bytes

Since:

API Level 3.0.0


Generated Sep 25, 2023 12:31:45 PM