Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
chacha20.c File Reference
ChaCha20 cipher. More...
Go to the source code of this file.
Functions | |
static void | chacha20_quarter_round (uint32_t state[16], size_t a, size_t b, size_t c, size_t d) |
ChaCha20 quarter round operation. | |
static void | chacha20_inner_block (uint32_t state[16]) |
Perform the ChaCha20 inner block operation. | |
static void | chacha20_block (const uint32_t initial_state[16], unsigned char keystream[64]) |
Generates a keystream block. | |
void | mbedtls_chacha20_init (mbedtls_chacha20_context *ctx) |
This function initializes the specified ChaCha20 context. | |
void | mbedtls_chacha20_free (mbedtls_chacha20_context *ctx) |
This function releases and clears the specified ChaCha20 context. | |
int | mbedtls_chacha20_setkey (mbedtls_chacha20_context *ctx, const unsigned char key[32]) |
This function sets the encryption/decryption key. | |
int | mbedtls_chacha20_starts (mbedtls_chacha20_context *ctx, const unsigned char nonce[12], uint32_t counter) |
This function sets the nonce and initial counter value. | |
int | mbedtls_chacha20_update (mbedtls_chacha20_context *ctx, size_t size, const unsigned char *input, unsigned char *output) |
This function encrypts or decrypts data. | |
int | mbedtls_chacha20_crypt (const unsigned char key[32], const unsigned char nonce[12], uint32_t counter, size_t data_len, const unsigned char *input, unsigned char *output) |
This function encrypts or decrypts data with ChaCha20 and the given key and nonce. | |
int | mbedtls_chacha20_self_test (int verbose) |
The ChaCha20 checkup routine. |
Detailed Description
ChaCha20 cipher.
Copyright (C) 2006-2016, ARM Limited, All Rights Reserved SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
This file is part of mbed TLS (https://tls.mbed.org)
Definition in file chacha20.c.
Function Documentation
static void chacha20_block | ( | const uint32_t | initial_state[16], |
unsigned char | keystream[64] | ||
) | [static] |
Generates a keystream block.
- Parameters:
-
initial_state The initial ChaCha20 state (key, nonce, counter). keystream Generated keystream bytes are written to this buffer.
Definition at line 145 of file chacha20.c.
static void chacha20_inner_block | ( | uint32_t | state[16] ) | [static] |
Perform the ChaCha20 inner block operation.
This function performs two rounds: the column round and the diagonal round.
- Parameters:
-
state The ChaCha20 state to update.
Definition at line 126 of file chacha20.c.
static void chacha20_quarter_round | ( | uint32_t | state[16], |
size_t | a, | ||
size_t | b, | ||
size_t | c, | ||
size_t | d | ||
) | [static] |
ChaCha20 quarter round operation.
The quarter round is defined as follows (from RFC 7539): 1. a += b; d ^= a; d <<<= 16; 2. c += d; b ^= c; b <<<= 12; 3. a += b; d ^= a; d <<<= 8; 4. c += d; b ^= c; b <<<= 7;
- Parameters:
-
state ChaCha20 state to modify. a The index of 'a' in the state. b The index of 'b' in the state. c The index of 'c' in the state. d The index of 'd' in the state.
Definition at line 91 of file chacha20.c.
int mbedtls_chacha20_crypt | ( | const unsigned char | key[32], |
const unsigned char | nonce[12], | ||
uint32_t | counter, | ||
size_t | size, | ||
const unsigned char * | input, | ||
unsigned char * | output | ||
) |
This function encrypts or decrypts data with ChaCha20 and the given key and nonce.
Since ChaCha20 is a stream cipher, the same operation is used for encrypting and decrypting data.
- Warning:
- You must never use the same (key, nonce) pair more than once. This would void any confidentiality guarantees for the messages encrypted with the same nonce and key.
- Note:
- The
input
andoutput
pointers must either be equal or point to non-overlapping buffers.
- Parameters:
-
key The encryption/decryption key. This must be 32
Bytes in length.nonce The nonce. This must be 12
Bytes in size.counter The initial counter value. This is usually 0
.size The length of the input data in Bytes. input The buffer holding the input data. This pointer can be NULL
if `size == 0`.output The buffer holding the output data. This must be able to hold size
Bytes. This pointer can beNULL
if `size == 0`.
- Returns:
0
on success.- A negative error code on failure.
Definition at line 320 of file chacha20.c.
void mbedtls_chacha20_free | ( | mbedtls_chacha20_context * | ctx ) |
This function releases and clears the specified ChaCha20 context.
- Parameters:
-
ctx The ChaCha20 context to clear. This may be NULL
, in which case this function is a no-op. If it is notNULL
, it must point to an initialized context.
Definition at line 199 of file chacha20.c.
void mbedtls_chacha20_init | ( | mbedtls_chacha20_context * | ctx ) |
This function initializes the specified ChaCha20 context.
It must be the first API called before using the context.
It is usually followed by calls to mbedtls_chacha20_setkey()
and mbedtls_chacha20_starts()
, then one or more calls to to mbedtls_chacha20_update()
, and finally to mbedtls_chacha20_free()
.
- Parameters:
-
ctx The ChaCha20 context to initialize. This must not be NULL
.
Definition at line 188 of file chacha20.c.
int mbedtls_chacha20_self_test | ( | int | verbose ) |
The ChaCha20 checkup routine.
- Returns:
0
on success.-
1
on failure.
Definition at line 535 of file chacha20.c.
int mbedtls_chacha20_setkey | ( | mbedtls_chacha20_context * | ctx, |
const unsigned char | key[32] | ||
) |
This function sets the encryption/decryption key.
- Note:
- After using this function, you must also call
mbedtls_chacha20_starts()
to set a nonce before you start encrypting/decrypting data withmbedtls_chacha_update()
.
- Parameters:
-
ctx The ChaCha20 context to which the key should be bound. It must be initialized. key The encryption/decryption key. This must be 32
Bytes in length.
- Returns:
0
on success.- MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Definition at line 207 of file chacha20.c.
int mbedtls_chacha20_starts | ( | mbedtls_chacha20_context * | ctx, |
const unsigned char | nonce[12], | ||
uint32_t | counter | ||
) |
This function sets the nonce and initial counter value.
- Note:
- A ChaCha20 context can be re-used with the same key by calling this function to change the nonce.
- Warning:
- You must never use the same nonce twice with the same key. This would void any confidentiality guarantees for the messages encrypted with the same nonce and key.
- Parameters:
-
ctx The ChaCha20 context to which the nonce should be bound. It must be initialized and bound to a key. nonce The nonce. This must be 12
Bytes in size.counter The initial counter value. This is usually 0
.
- Returns:
0
on success.- MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is NULL.
Definition at line 232 of file chacha20.c.
int mbedtls_chacha20_update | ( | mbedtls_chacha20_context * | ctx, |
size_t | size, | ||
const unsigned char * | input, | ||
unsigned char * | output | ||
) |
This function encrypts or decrypts data.
Since ChaCha20 is a stream cipher, the same operation is used for encrypting and decrypting data.
- Note:
- The
input
andoutput
pointers must either be equal or point to non-overlapping buffers. -
mbedtls_chacha20_setkey()
andmbedtls_chacha20_starts()
must be called at least once to setup the context before this function can be called. - This function can be called multiple times in a row in order to encrypt of decrypt data piecewise with the same key and nonce.
- Parameters:
-
ctx The ChaCha20 context to use for encryption or decryption. It must be initialized and bound to a key and nonce. size The length of the input data in Bytes. input The buffer holding the input data. This pointer can be NULL
if `size == 0`.output The buffer holding the output data. This must be able to hold size
Bytes. This pointer can beNULL
if `size == 0`.
- Returns:
0
on success.- A negative error code on failure.
Definition at line 255 of file chacha20.c.
Generated on Tue Jul 12 2022 13:55:06 by
