Manuel Pégourié-Gonnard | 7d5ef17 | 2022-01-27 13:09:13 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * This is a companion to aead_psa.c, doing the same operations with the |
| 3 | * legacy Cipher API. The goal is that comparing the two programs will help people |
| 4 | * migrating to the PSA Crypto API. |
| 5 | * |
| 6 | * Copyright The Mbed TLS Contributors |
| 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * When used with multi-part AEAD operations, the `mbedtls_cipher_context` |
| 24 | * serves a triple purpose (1) hold the key, (2) store the algorithm when no |
| 25 | * operation is active, and (3) save progress information for the current |
| 26 | * operation. With PSA those roles are held by disinct objects: (1) a |
| 27 | * psa_key_id_t to hold the key, a (2) psa_algorithm_t to represent the |
| 28 | * algorithm, and (3) a psa_operation_t for multi-part progress. |
| 29 | * |
| 30 | * On the other hand, with PSA, the algorithms encodes the desired tag length; |
| 31 | * with Cipher the desired tag length needs to be tracked separately. |
| 32 | * |
| 33 | * This program and its compation aead_psa.c illustrate this by doing the |
| 34 | * same sequence of multi-part AEAD computation with both APIs; looking at the |
| 35 | * two side by side should make the differences and similarities clear. |
| 36 | */ |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | |
| 40 | #include "mbedtls/build_info.h" |
| 41 | |
| 42 | #if !defined(MBEDTLS_CIPHER_C) || \ |
| 43 | !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_GCM_C) || \ |
| 44 | !defined(MBEDTLS_CHACHAPOLY_C) |
| 45 | int main( void ) |
| 46 | { |
| 47 | printf( "MBEDTLS_MD_C and/or " |
| 48 | "MBEDTLS_AES_C and/or MBEDTLS_GCM_C and/or " |
| 49 | "MBEDTLS_CHACHAPOLY_C not defined\r\n" ); |
| 50 | return( 0 ); |
| 51 | } |
| 52 | #else |
| 53 | |
| 54 | #include <string.h> |
| 55 | |
| 56 | #include "mbedtls/cipher.h" |
| 57 | |
| 58 | /* |
| 59 | * Dummy data and helper functions |
| 60 | */ |
| 61 | const char usage[] = "Usage: aead_non_psa [aes128-gcm|aes256-gcm|aes128-gcm_8|chachapoly]"; |
| 62 | |
| 63 | const unsigned char iv1[12] = { 0x00 }; |
| 64 | const unsigned char add_data1[] = { 0x01, 0x02 }; |
| 65 | const unsigned char msg1_part1[] = { 0x03, 0x04 }; |
| 66 | const unsigned char msg1_part2[] = { 0x05, 0x06, 0x07 }; |
| 67 | const size_t msg1_size = sizeof( msg1_part1 ) + sizeof( msg1_part2 ); |
| 68 | |
| 69 | const unsigned char iv2[12] = { 0x10 }; |
| 70 | const unsigned char add_data2[] = { 0x11, 0x12 }; |
| 71 | const unsigned char msg2_part1[] = { 0x13, 0x14 }; |
| 72 | const unsigned char msg2_part2[] = { 0x15, 0x16, 0x17 }; |
| 73 | const size_t msg2_size = sizeof( msg2_part1 ) + sizeof( msg2_part2 ); |
| 74 | |
| 75 | const size_t msg_max_size = msg1_size > msg2_size ? msg1_size : msg2_size; |
| 76 | |
| 77 | const unsigned char key_bytes[32] = { 0x2a }; |
| 78 | |
| 79 | void print_out( const char *title, unsigned char *out, size_t len ) |
| 80 | { |
| 81 | printf( "%s:", title ); |
| 82 | for( size_t i = 0; i < len; i++ ) |
| 83 | printf( " %02x", out[i] ); |
| 84 | printf( "\n" ); |
| 85 | } |
| 86 | |
| 87 | #define CHK( code ) \ |
| 88 | do { \ |
| 89 | ret = code; \ |
| 90 | if( ret != 0 ) { \ |
| 91 | printf( "%03d: ret = -0x%04x\n", __LINE__, (unsigned) -ret ); \ |
| 92 | goto exit; \ |
| 93 | } \ |
| 94 | } while( 0 ) |
| 95 | |
| 96 | static int aead_prepare( const char *info, |
| 97 | mbedtls_cipher_context_t *ctx, |
| 98 | size_t *tag_len ) |
| 99 | { |
| 100 | int ret; |
| 101 | |
| 102 | mbedtls_cipher_type_t type; |
| 103 | if( strcmp( info, "aes128-gcm" ) == 0 ) { |
| 104 | type = MBEDTLS_CIPHER_AES_128_GCM; |
| 105 | *tag_len = 16; |
| 106 | } else if( strcmp( info, "aes256-gcm" ) == 0 ) { |
| 107 | type = MBEDTLS_CIPHER_AES_256_GCM; |
| 108 | *tag_len = 16; |
| 109 | } else if( strcmp( info, "aes128-gcm_8" ) == 0 ) { |
| 110 | type = MBEDTLS_CIPHER_AES_128_GCM; |
| 111 | *tag_len = 8; |
| 112 | } else if( strcmp( info, "chachapoly" ) == 0 ) { |
| 113 | type = MBEDTLS_CIPHER_CHACHA20_POLY1305; |
| 114 | *tag_len = 16; |
| 115 | } else { |
| 116 | puts( usage ); |
| 117 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 118 | } |
| 119 | |
| 120 | CHK( mbedtls_cipher_setup( ctx, |
| 121 | mbedtls_cipher_info_from_type( type ) ) ); |
| 122 | |
| 123 | int key_len = mbedtls_cipher_get_key_bitlen( ctx ); |
| 124 | CHK( mbedtls_cipher_setkey( ctx, key_bytes, key_len, MBEDTLS_ENCRYPT ) ); |
| 125 | |
| 126 | exit: |
| 127 | return( ret ); |
| 128 | } |
| 129 | |
| 130 | static void aead_info( const mbedtls_cipher_context_t *ctx, size_t tag_len ) |
| 131 | { |
| 132 | // no convenient way to get the cipher type (for example, AES) |
| 133 | const char *ciph = "???"; |
| 134 | int key_bits = mbedtls_cipher_get_key_bitlen( ctx ); |
| 135 | mbedtls_cipher_mode_t mode = mbedtls_cipher_get_cipher_mode( ctx ); |
| 136 | |
| 137 | const char *mode_str = mode == MBEDTLS_MODE_GCM ? "GCM" |
| 138 | : mode == MBEDTLS_MODE_CHACHAPOLY ? "ChachaPoly" |
| 139 | : "???"; |
| 140 | |
| 141 | printf( "cipher: %s, %d, %s, %u\n", ciph, key_bits, mode_str, (unsigned) tag_len ); |
| 142 | } |
| 143 | |
| 144 | static int aead_encrypt( mbedtls_cipher_context_t *ctx, size_t tag_len, |
| 145 | const unsigned char *iv, size_t iv_len, |
| 146 | const unsigned char *ad, size_t ad_len, |
| 147 | const unsigned char *pa, size_t pa_len, |
| 148 | const unsigned char *pb, size_t pb_len ) |
| 149 | { |
| 150 | int ret; |
| 151 | size_t olen; |
| 152 | unsigned char out[msg_max_size + 16]; |
| 153 | unsigned char *p = out; |
| 154 | |
| 155 | CHK( mbedtls_cipher_set_iv( ctx, iv, iv_len ) ); |
| 156 | CHK( mbedtls_cipher_reset( ctx ) ); |
| 157 | CHK( mbedtls_cipher_update_ad( ctx, ad, ad_len ) ); |
| 158 | CHK( mbedtls_cipher_update( ctx, pa, pa_len, p, &olen ) ); |
| 159 | p += olen; |
| 160 | CHK( mbedtls_cipher_update( ctx, pb, pb_len, p, &olen ) ); |
| 161 | p += olen; |
| 162 | CHK( mbedtls_cipher_finish( ctx, p, &olen ) ); |
| 163 | p += olen; |
| 164 | CHK( mbedtls_cipher_write_tag( ctx, p, tag_len ) ); |
| 165 | p += tag_len; |
| 166 | |
| 167 | olen = p - out; |
| 168 | print_out( "cipher", out, olen ); |
| 169 | |
| 170 | exit: |
| 171 | return( ret ); |
| 172 | } |
| 173 | |
| 174 | static int aead_demo( const char *info ) |
| 175 | { |
| 176 | int ret = 0; |
| 177 | |
| 178 | mbedtls_cipher_context_t ctx; |
| 179 | size_t tag_len; |
| 180 | |
| 181 | mbedtls_cipher_init( &ctx ); |
| 182 | |
| 183 | CHK( aead_prepare( info, &ctx, &tag_len ) ); |
| 184 | |
| 185 | aead_info( &ctx, tag_len ); |
| 186 | |
| 187 | CHK( aead_encrypt( &ctx, tag_len, |
| 188 | iv1, sizeof( iv1 ), add_data1, sizeof( add_data1 ), |
| 189 | msg1_part1, sizeof( msg1_part1 ), |
| 190 | msg1_part2, sizeof( msg1_part2 ) ) ); |
| 191 | CHK( aead_encrypt( &ctx, tag_len, |
| 192 | iv2, sizeof( iv2 ), add_data2, sizeof( add_data2 ), |
| 193 | msg2_part1, sizeof( msg2_part1 ), |
| 194 | msg2_part2, sizeof( msg2_part2 ) ) ); |
| 195 | |
| 196 | exit: |
| 197 | mbedtls_cipher_free( &ctx ); |
| 198 | |
| 199 | return( ret ); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | /* |
| 204 | * Main function |
| 205 | */ |
| 206 | int main( int argc, char **argv ) |
| 207 | { |
| 208 | if( argc != 2 ) |
| 209 | { |
| 210 | puts( usage ); |
| 211 | return( 1 ); |
| 212 | } |
| 213 | |
| 214 | aead_demo( argv[1] ); |
| 215 | } |
| 216 | |
| 217 | #endif |