1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci/* 11e1051a39Sopenharmony_ci * Camellia low level APIs are deprecated for public use, but still ok for 12e1051a39Sopenharmony_ci * internal use. 13e1051a39Sopenharmony_ci */ 14e1051a39Sopenharmony_ci#include "internal/deprecated.h" 15e1051a39Sopenharmony_ci 16e1051a39Sopenharmony_ci#include <openssl/opensslconf.h> 17e1051a39Sopenharmony_ci 18e1051a39Sopenharmony_ci#include <openssl/evp.h> 19e1051a39Sopenharmony_ci#include <openssl/err.h> 20e1051a39Sopenharmony_ci#include <string.h> 21e1051a39Sopenharmony_ci#include <assert.h> 22e1051a39Sopenharmony_ci#include <openssl/camellia.h> 23e1051a39Sopenharmony_ci#include "crypto/evp.h" 24e1051a39Sopenharmony_ci#include "crypto/modes.h" 25e1051a39Sopenharmony_ci#include "crypto/cmll_platform.h" 26e1051a39Sopenharmony_ci#include "evp_local.h" 27e1051a39Sopenharmony_ci 28e1051a39Sopenharmony_cistatic int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 29e1051a39Sopenharmony_ci const unsigned char *iv, int enc); 30e1051a39Sopenharmony_ci 31e1051a39Sopenharmony_ci/* Camellia subkey Structure */ 32e1051a39Sopenharmony_citypedef struct { 33e1051a39Sopenharmony_ci CAMELLIA_KEY ks; 34e1051a39Sopenharmony_ci block128_f block; 35e1051a39Sopenharmony_ci union { 36e1051a39Sopenharmony_ci cbc128_f cbc; 37e1051a39Sopenharmony_ci ctr128_f ctr; 38e1051a39Sopenharmony_ci } stream; 39e1051a39Sopenharmony_ci} EVP_CAMELLIA_KEY; 40e1051a39Sopenharmony_ci 41e1051a39Sopenharmony_ci#define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4)) 42e1051a39Sopenharmony_ci 43e1051a39Sopenharmony_ci/* Attribute operation for Camellia */ 44e1051a39Sopenharmony_ci#define data(ctx) EVP_C_DATA(EVP_CAMELLIA_KEY,ctx) 45e1051a39Sopenharmony_ci 46e1051a39Sopenharmony_ci#if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__)) 47e1051a39Sopenharmony_ci/* ---------^^^ this is not a typo, just a way to detect that 48e1051a39Sopenharmony_ci * assembler support was in general requested... */ 49e1051a39Sopenharmony_ci# include "crypto/sparc_arch.h" 50e1051a39Sopenharmony_ci 51e1051a39Sopenharmony_cistatic int cmll_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 52e1051a39Sopenharmony_ci const unsigned char *iv, int enc) 53e1051a39Sopenharmony_ci{ 54e1051a39Sopenharmony_ci int ret, mode, bits; 55e1051a39Sopenharmony_ci EVP_CAMELLIA_KEY *dat = 56e1051a39Sopenharmony_ci (EVP_CAMELLIA_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx); 57e1051a39Sopenharmony_ci 58e1051a39Sopenharmony_ci mode = EVP_CIPHER_CTX_get_mode(ctx); 59e1051a39Sopenharmony_ci bits = EVP_CIPHER_CTX_get_key_length(ctx) * 8; 60e1051a39Sopenharmony_ci 61e1051a39Sopenharmony_ci cmll_t4_set_key(key, bits, &dat->ks); 62e1051a39Sopenharmony_ci 63e1051a39Sopenharmony_ci if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) 64e1051a39Sopenharmony_ci && !enc) { 65e1051a39Sopenharmony_ci ret = 0; 66e1051a39Sopenharmony_ci dat->block = (block128_f) cmll_t4_decrypt; 67e1051a39Sopenharmony_ci switch (bits) { 68e1051a39Sopenharmony_ci case 128: 69e1051a39Sopenharmony_ci dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? 70e1051a39Sopenharmony_ci (cbc128_f) cmll128_t4_cbc_decrypt : NULL; 71e1051a39Sopenharmony_ci break; 72e1051a39Sopenharmony_ci case 192: 73e1051a39Sopenharmony_ci case 256: 74e1051a39Sopenharmony_ci dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? 75e1051a39Sopenharmony_ci (cbc128_f) cmll256_t4_cbc_decrypt : NULL; 76e1051a39Sopenharmony_ci break; 77e1051a39Sopenharmony_ci default: 78e1051a39Sopenharmony_ci ret = -1; 79e1051a39Sopenharmony_ci } 80e1051a39Sopenharmony_ci } else { 81e1051a39Sopenharmony_ci ret = 0; 82e1051a39Sopenharmony_ci dat->block = (block128_f) cmll_t4_encrypt; 83e1051a39Sopenharmony_ci switch (bits) { 84e1051a39Sopenharmony_ci case 128: 85e1051a39Sopenharmony_ci if (mode == EVP_CIPH_CBC_MODE) 86e1051a39Sopenharmony_ci dat->stream.cbc = (cbc128_f) cmll128_t4_cbc_encrypt; 87e1051a39Sopenharmony_ci else if (mode == EVP_CIPH_CTR_MODE) 88e1051a39Sopenharmony_ci dat->stream.ctr = (ctr128_f) cmll128_t4_ctr32_encrypt; 89e1051a39Sopenharmony_ci else 90e1051a39Sopenharmony_ci dat->stream.cbc = NULL; 91e1051a39Sopenharmony_ci break; 92e1051a39Sopenharmony_ci case 192: 93e1051a39Sopenharmony_ci case 256: 94e1051a39Sopenharmony_ci if (mode == EVP_CIPH_CBC_MODE) 95e1051a39Sopenharmony_ci dat->stream.cbc = (cbc128_f) cmll256_t4_cbc_encrypt; 96e1051a39Sopenharmony_ci else if (mode == EVP_CIPH_CTR_MODE) 97e1051a39Sopenharmony_ci dat->stream.ctr = (ctr128_f) cmll256_t4_ctr32_encrypt; 98e1051a39Sopenharmony_ci else 99e1051a39Sopenharmony_ci dat->stream.cbc = NULL; 100e1051a39Sopenharmony_ci break; 101e1051a39Sopenharmony_ci default: 102e1051a39Sopenharmony_ci ret = -1; 103e1051a39Sopenharmony_ci } 104e1051a39Sopenharmony_ci } 105e1051a39Sopenharmony_ci 106e1051a39Sopenharmony_ci if (ret < 0) { 107e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_CAMELLIA_KEY_SETUP_FAILED); 108e1051a39Sopenharmony_ci return 0; 109e1051a39Sopenharmony_ci } 110e1051a39Sopenharmony_ci 111e1051a39Sopenharmony_ci return 1; 112e1051a39Sopenharmony_ci} 113e1051a39Sopenharmony_ci 114e1051a39Sopenharmony_ci# define cmll_t4_cbc_cipher camellia_cbc_cipher 115e1051a39Sopenharmony_cistatic int cmll_t4_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 116e1051a39Sopenharmony_ci const unsigned char *in, size_t len); 117e1051a39Sopenharmony_ci 118e1051a39Sopenharmony_ci# define cmll_t4_ecb_cipher camellia_ecb_cipher 119e1051a39Sopenharmony_cistatic int cmll_t4_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 120e1051a39Sopenharmony_ci const unsigned char *in, size_t len); 121e1051a39Sopenharmony_ci 122e1051a39Sopenharmony_ci# define cmll_t4_ofb_cipher camellia_ofb_cipher 123e1051a39Sopenharmony_cistatic int cmll_t4_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 124e1051a39Sopenharmony_ci const unsigned char *in, size_t len); 125e1051a39Sopenharmony_ci 126e1051a39Sopenharmony_ci# define cmll_t4_cfb_cipher camellia_cfb_cipher 127e1051a39Sopenharmony_cistatic int cmll_t4_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 128e1051a39Sopenharmony_ci const unsigned char *in, size_t len); 129e1051a39Sopenharmony_ci 130e1051a39Sopenharmony_ci# define cmll_t4_cfb8_cipher camellia_cfb8_cipher 131e1051a39Sopenharmony_cistatic int cmll_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 132e1051a39Sopenharmony_ci const unsigned char *in, size_t len); 133e1051a39Sopenharmony_ci 134e1051a39Sopenharmony_ci# define cmll_t4_cfb1_cipher camellia_cfb1_cipher 135e1051a39Sopenharmony_cistatic int cmll_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 136e1051a39Sopenharmony_ci const unsigned char *in, size_t len); 137e1051a39Sopenharmony_ci 138e1051a39Sopenharmony_ci# define cmll_t4_ctr_cipher camellia_ctr_cipher 139e1051a39Sopenharmony_cistatic int cmll_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 140e1051a39Sopenharmony_ci const unsigned char *in, size_t len); 141e1051a39Sopenharmony_ci 142e1051a39Sopenharmony_ci# define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \ 143e1051a39Sopenharmony_cistatic const EVP_CIPHER cmll_t4_##keylen##_##mode = { \ 144e1051a39Sopenharmony_ci nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \ 145e1051a39Sopenharmony_ci flags|EVP_CIPH_##MODE##_MODE, \ 146e1051a39Sopenharmony_ci EVP_ORIG_GLOBAL, \ 147e1051a39Sopenharmony_ci cmll_t4_init_key, \ 148e1051a39Sopenharmony_ci cmll_t4_##mode##_cipher, \ 149e1051a39Sopenharmony_ci NULL, \ 150e1051a39Sopenharmony_ci sizeof(EVP_CAMELLIA_KEY), \ 151e1051a39Sopenharmony_ci NULL,NULL,NULL,NULL }; \ 152e1051a39Sopenharmony_cistatic const EVP_CIPHER camellia_##keylen##_##mode = { \ 153e1051a39Sopenharmony_ci nid##_##keylen##_##nmode,blocksize, \ 154e1051a39Sopenharmony_ci keylen/8,ivlen, \ 155e1051a39Sopenharmony_ci flags|EVP_CIPH_##MODE##_MODE, \ 156e1051a39Sopenharmony_ci EVP_ORIG_GLOBAL, \ 157e1051a39Sopenharmony_ci camellia_init_key, \ 158e1051a39Sopenharmony_ci camellia_##mode##_cipher, \ 159e1051a39Sopenharmony_ci NULL, \ 160e1051a39Sopenharmony_ci sizeof(EVP_CAMELLIA_KEY), \ 161e1051a39Sopenharmony_ci NULL,NULL,NULL,NULL }; \ 162e1051a39Sopenharmony_ciconst EVP_CIPHER *EVP_camellia_##keylen##_##mode(void) \ 163e1051a39Sopenharmony_ci{ return SPARC_CMLL_CAPABLE?&cmll_t4_##keylen##_##mode:&camellia_##keylen##_##mode; } 164e1051a39Sopenharmony_ci 165e1051a39Sopenharmony_ci#else 166e1051a39Sopenharmony_ci 167e1051a39Sopenharmony_ci# define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \ 168e1051a39Sopenharmony_cistatic const EVP_CIPHER camellia_##keylen##_##mode = { \ 169e1051a39Sopenharmony_ci nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \ 170e1051a39Sopenharmony_ci flags|EVP_CIPH_##MODE##_MODE, \ 171e1051a39Sopenharmony_ci EVP_ORIG_GLOBAL, \ 172e1051a39Sopenharmony_ci camellia_init_key, \ 173e1051a39Sopenharmony_ci camellia_##mode##_cipher, \ 174e1051a39Sopenharmony_ci NULL, \ 175e1051a39Sopenharmony_ci sizeof(EVP_CAMELLIA_KEY), \ 176e1051a39Sopenharmony_ci NULL,NULL,NULL,NULL }; \ 177e1051a39Sopenharmony_ciconst EVP_CIPHER *EVP_camellia_##keylen##_##mode(void) \ 178e1051a39Sopenharmony_ci{ return &camellia_##keylen##_##mode; } 179e1051a39Sopenharmony_ci 180e1051a39Sopenharmony_ci#endif 181e1051a39Sopenharmony_ci 182e1051a39Sopenharmony_ci#define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \ 183e1051a39Sopenharmony_ci BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \ 184e1051a39Sopenharmony_ci BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \ 185e1051a39Sopenharmony_ci BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \ 186e1051a39Sopenharmony_ci BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \ 187e1051a39Sopenharmony_ci BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \ 188e1051a39Sopenharmony_ci BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \ 189e1051a39Sopenharmony_ci BLOCK_CIPHER_generic(nid, keylen, 1, 16, ctr, ctr, CTR, flags) 190e1051a39Sopenharmony_ci 191e1051a39Sopenharmony_ci/* The subkey for Camellia is generated. */ 192e1051a39Sopenharmony_cistatic int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 193e1051a39Sopenharmony_ci const unsigned char *iv, int enc) 194e1051a39Sopenharmony_ci{ 195e1051a39Sopenharmony_ci int ret, mode; 196e1051a39Sopenharmony_ci EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx); 197e1051a39Sopenharmony_ci 198e1051a39Sopenharmony_ci ret = Camellia_set_key(key, EVP_CIPHER_CTX_get_key_length(ctx) * 8, 199e1051a39Sopenharmony_ci &dat->ks); 200e1051a39Sopenharmony_ci if (ret < 0) { 201e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_CAMELLIA_KEY_SETUP_FAILED); 202e1051a39Sopenharmony_ci return 0; 203e1051a39Sopenharmony_ci } 204e1051a39Sopenharmony_ci 205e1051a39Sopenharmony_ci mode = EVP_CIPHER_CTX_get_mode(ctx); 206e1051a39Sopenharmony_ci if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) 207e1051a39Sopenharmony_ci && !enc) { 208e1051a39Sopenharmony_ci dat->block = (block128_f) Camellia_decrypt; 209e1051a39Sopenharmony_ci dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? 210e1051a39Sopenharmony_ci (cbc128_f) Camellia_cbc_encrypt : NULL; 211e1051a39Sopenharmony_ci } else { 212e1051a39Sopenharmony_ci dat->block = (block128_f) Camellia_encrypt; 213e1051a39Sopenharmony_ci dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? 214e1051a39Sopenharmony_ci (cbc128_f) Camellia_cbc_encrypt : NULL; 215e1051a39Sopenharmony_ci } 216e1051a39Sopenharmony_ci 217e1051a39Sopenharmony_ci return 1; 218e1051a39Sopenharmony_ci} 219e1051a39Sopenharmony_ci 220e1051a39Sopenharmony_cistatic int camellia_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 221e1051a39Sopenharmony_ci const unsigned char *in, size_t len) 222e1051a39Sopenharmony_ci{ 223e1051a39Sopenharmony_ci EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx); 224e1051a39Sopenharmony_ci 225e1051a39Sopenharmony_ci if (dat->stream.cbc) 226e1051a39Sopenharmony_ci (*dat->stream.cbc) (in, out, len, &dat->ks, ctx->iv, 227e1051a39Sopenharmony_ci EVP_CIPHER_CTX_is_encrypting(ctx)); 228e1051a39Sopenharmony_ci else if (EVP_CIPHER_CTX_is_encrypting(ctx)) 229e1051a39Sopenharmony_ci CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, dat->block); 230e1051a39Sopenharmony_ci else 231e1051a39Sopenharmony_ci CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, dat->block); 232e1051a39Sopenharmony_ci 233e1051a39Sopenharmony_ci return 1; 234e1051a39Sopenharmony_ci} 235e1051a39Sopenharmony_ci 236e1051a39Sopenharmony_cistatic int camellia_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 237e1051a39Sopenharmony_ci const unsigned char *in, size_t len) 238e1051a39Sopenharmony_ci{ 239e1051a39Sopenharmony_ci size_t bl = EVP_CIPHER_CTX_get_block_size(ctx); 240e1051a39Sopenharmony_ci size_t i; 241e1051a39Sopenharmony_ci EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx); 242e1051a39Sopenharmony_ci 243e1051a39Sopenharmony_ci if (len < bl) 244e1051a39Sopenharmony_ci return 1; 245e1051a39Sopenharmony_ci 246e1051a39Sopenharmony_ci for (i = 0, len -= bl; i <= len; i += bl) 247e1051a39Sopenharmony_ci (*dat->block) (in + i, out + i, &dat->ks); 248e1051a39Sopenharmony_ci 249e1051a39Sopenharmony_ci return 1; 250e1051a39Sopenharmony_ci} 251e1051a39Sopenharmony_ci 252e1051a39Sopenharmony_cistatic int camellia_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 253e1051a39Sopenharmony_ci const unsigned char *in, size_t len) 254e1051a39Sopenharmony_ci{ 255e1051a39Sopenharmony_ci EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx); 256e1051a39Sopenharmony_ci 257e1051a39Sopenharmony_ci int num = EVP_CIPHER_CTX_get_num(ctx); 258e1051a39Sopenharmony_ci CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, ctx->iv, &num, dat->block); 259e1051a39Sopenharmony_ci EVP_CIPHER_CTX_set_num(ctx, num); 260e1051a39Sopenharmony_ci return 1; 261e1051a39Sopenharmony_ci} 262e1051a39Sopenharmony_ci 263e1051a39Sopenharmony_cistatic int camellia_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 264e1051a39Sopenharmony_ci const unsigned char *in, size_t len) 265e1051a39Sopenharmony_ci{ 266e1051a39Sopenharmony_ci EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx); 267e1051a39Sopenharmony_ci 268e1051a39Sopenharmony_ci int num = EVP_CIPHER_CTX_get_num(ctx); 269e1051a39Sopenharmony_ci CRYPTO_cfb128_encrypt(in, out, len, &dat->ks, ctx->iv, &num, 270e1051a39Sopenharmony_ci EVP_CIPHER_CTX_is_encrypting(ctx), dat->block); 271e1051a39Sopenharmony_ci EVP_CIPHER_CTX_set_num(ctx, num); 272e1051a39Sopenharmony_ci return 1; 273e1051a39Sopenharmony_ci} 274e1051a39Sopenharmony_ci 275e1051a39Sopenharmony_cistatic int camellia_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 276e1051a39Sopenharmony_ci const unsigned char *in, size_t len) 277e1051a39Sopenharmony_ci{ 278e1051a39Sopenharmony_ci EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx); 279e1051a39Sopenharmony_ci 280e1051a39Sopenharmony_ci int num = EVP_CIPHER_CTX_get_num(ctx); 281e1051a39Sopenharmony_ci CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks, ctx->iv, &num, 282e1051a39Sopenharmony_ci EVP_CIPHER_CTX_is_encrypting(ctx), dat->block); 283e1051a39Sopenharmony_ci EVP_CIPHER_CTX_set_num(ctx, num); 284e1051a39Sopenharmony_ci return 1; 285e1051a39Sopenharmony_ci} 286e1051a39Sopenharmony_ci 287e1051a39Sopenharmony_cistatic int camellia_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 288e1051a39Sopenharmony_ci const unsigned char *in, size_t len) 289e1051a39Sopenharmony_ci{ 290e1051a39Sopenharmony_ci EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx); 291e1051a39Sopenharmony_ci 292e1051a39Sopenharmony_ci if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) { 293e1051a39Sopenharmony_ci int num = EVP_CIPHER_CTX_get_num(ctx); 294e1051a39Sopenharmony_ci CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks, ctx->iv, &num, 295e1051a39Sopenharmony_ci EVP_CIPHER_CTX_is_encrypting(ctx), 296e1051a39Sopenharmony_ci dat->block); 297e1051a39Sopenharmony_ci EVP_CIPHER_CTX_set_num(ctx, num); 298e1051a39Sopenharmony_ci return 1; 299e1051a39Sopenharmony_ci } 300e1051a39Sopenharmony_ci 301e1051a39Sopenharmony_ci while (len >= MAXBITCHUNK) { 302e1051a39Sopenharmony_ci int num = EVP_CIPHER_CTX_get_num(ctx); 303e1051a39Sopenharmony_ci CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, &dat->ks, 304e1051a39Sopenharmony_ci ctx->iv, &num, 305e1051a39Sopenharmony_ci EVP_CIPHER_CTX_is_encrypting(ctx), 306e1051a39Sopenharmony_ci dat->block); 307e1051a39Sopenharmony_ci EVP_CIPHER_CTX_set_num(ctx, num); 308e1051a39Sopenharmony_ci len -= MAXBITCHUNK; 309e1051a39Sopenharmony_ci out += MAXBITCHUNK; 310e1051a39Sopenharmony_ci in += MAXBITCHUNK; 311e1051a39Sopenharmony_ci } 312e1051a39Sopenharmony_ci if (len) { 313e1051a39Sopenharmony_ci int num = EVP_CIPHER_CTX_get_num(ctx); 314e1051a39Sopenharmony_ci CRYPTO_cfb128_1_encrypt(in, out, len * 8, &dat->ks, 315e1051a39Sopenharmony_ci ctx->iv, &num, 316e1051a39Sopenharmony_ci EVP_CIPHER_CTX_is_encrypting(ctx), 317e1051a39Sopenharmony_ci dat->block); 318e1051a39Sopenharmony_ci EVP_CIPHER_CTX_set_num(ctx, num); 319e1051a39Sopenharmony_ci } 320e1051a39Sopenharmony_ci 321e1051a39Sopenharmony_ci return 1; 322e1051a39Sopenharmony_ci} 323e1051a39Sopenharmony_ci 324e1051a39Sopenharmony_cistatic int camellia_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 325e1051a39Sopenharmony_ci const unsigned char *in, size_t len) 326e1051a39Sopenharmony_ci{ 327e1051a39Sopenharmony_ci int snum = EVP_CIPHER_CTX_get_num(ctx); 328e1051a39Sopenharmony_ci unsigned int num; 329e1051a39Sopenharmony_ci EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx); 330e1051a39Sopenharmony_ci 331e1051a39Sopenharmony_ci if (snum < 0) 332e1051a39Sopenharmony_ci return 0; 333e1051a39Sopenharmony_ci num = snum; 334e1051a39Sopenharmony_ci if (dat->stream.ctr) 335e1051a39Sopenharmony_ci CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, ctx->iv, 336e1051a39Sopenharmony_ci EVP_CIPHER_CTX_buf_noconst(ctx), 337e1051a39Sopenharmony_ci &num, 338e1051a39Sopenharmony_ci dat->stream.ctr); 339e1051a39Sopenharmony_ci else 340e1051a39Sopenharmony_ci CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv, 341e1051a39Sopenharmony_ci EVP_CIPHER_CTX_buf_noconst(ctx), &num, 342e1051a39Sopenharmony_ci dat->block); 343e1051a39Sopenharmony_ci EVP_CIPHER_CTX_set_num(ctx, num); 344e1051a39Sopenharmony_ci return 1; 345e1051a39Sopenharmony_ci} 346e1051a39Sopenharmony_ci 347e1051a39Sopenharmony_ciBLOCK_CIPHER_generic_pack(NID_camellia, 128, 0) 348e1051a39Sopenharmony_ci BLOCK_CIPHER_generic_pack(NID_camellia, 192, 0) 349e1051a39Sopenharmony_ci BLOCK_CIPHER_generic_pack(NID_camellia, 256, 0) 350