mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-14 10:37:23 -07:00
mbedtls 2.26.0
This commit is contained in:
parent
37e0ed59f2
commit
6324e2e746
187 changed files with 106114 additions and 19438 deletions
|
@ -1,24 +1,20 @@
|
|||
/*
|
||||
* Blowfish implementation
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: GPL-2.0
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
* 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
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
* 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.
|
||||
*/
|
||||
/*
|
||||
* The Blowfish block cipher was designed by Bruce Schneier in 1993.
|
||||
|
@ -27,11 +23,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_BLOWFISH_C)
|
||||
|
||||
|
@ -42,68 +34,77 @@
|
|||
|
||||
#if !defined(MBEDTLS_BLOWFISH_ALT)
|
||||
|
||||
/* Parameter validation macros */
|
||||
#define BLOWFISH_VALIDATE_RET( cond ) \
|
||||
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA )
|
||||
#define BLOWFISH_VALIDATE( cond ) \
|
||||
MBEDTLS_INTERNAL_VALIDATE( cond )
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (big endian)
|
||||
*/
|
||||
#ifndef GET_UINT32_BE
|
||||
#define GET_UINT32_BE(n,b,i) \
|
||||
{ \
|
||||
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
||||
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
||||
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
||||
| ( (uint32_t) (b)[(i) + 3] ); \
|
||||
}
|
||||
{ \
|
||||
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
||||
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
||||
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
||||
| ( (uint32_t) (b)[(i) + 3] ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef PUT_UINT32_BE
|
||||
#define PUT_UINT32_BE(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
||||
}
|
||||
{ \
|
||||
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
static const uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2] = {
|
||||
0x243F6A88L, 0x85A308D3L, 0x13198A2EL, 0x03707344L,
|
||||
0xA4093822L, 0x299F31D0L, 0x082EFA98L, 0xEC4E6C89L,
|
||||
0x452821E6L, 0x38D01377L, 0xBE5466CFL, 0x34E90C6CL,
|
||||
0xC0AC29B7L, 0xC97C50DDL, 0x3F84D5B5L, 0xB5470917L,
|
||||
0x9216D5D9L, 0x8979FB1BL
|
||||
0x243F6A88L, 0x85A308D3L, 0x13198A2EL, 0x03707344L,
|
||||
0xA4093822L, 0x299F31D0L, 0x082EFA98L, 0xEC4E6C89L,
|
||||
0x452821E6L, 0x38D01377L, 0xBE5466CFL, 0x34E90C6CL,
|
||||
0xC0AC29B7L, 0xC97C50DDL, 0x3F84D5B5L, 0xB5470917L,
|
||||
0x9216D5D9L, 0x8979FB1BL
|
||||
};
|
||||
|
||||
/* declarations of data at the end of this file */
|
||||
static const uint32_t S[4][256];
|
||||
|
||||
static uint32_t F(mbedtls_blowfish_context *ctx, uint32_t x) {
|
||||
unsigned short a, b, c, d;
|
||||
uint32_t y;
|
||||
static uint32_t F( mbedtls_blowfish_context *ctx, uint32_t x )
|
||||
{
|
||||
unsigned short a, b, c, d;
|
||||
uint32_t y;
|
||||
|
||||
d = (unsigned short)(x & 0xFF);
|
||||
x >>= 8;
|
||||
c = (unsigned short)(x & 0xFF);
|
||||
x >>= 8;
|
||||
b = (unsigned short)(x & 0xFF);
|
||||
x >>= 8;
|
||||
a = (unsigned short)(x & 0xFF);
|
||||
y = ctx->S[0][a] + ctx->S[1][b];
|
||||
y = y ^ ctx->S[2][c];
|
||||
y = y + ctx->S[3][d];
|
||||
d = (unsigned short)(x & 0xFF);
|
||||
x >>= 8;
|
||||
c = (unsigned short)(x & 0xFF);
|
||||
x >>= 8;
|
||||
b = (unsigned short)(x & 0xFF);
|
||||
x >>= 8;
|
||||
a = (unsigned short)(x & 0xFF);
|
||||
y = ctx->S[0][a] + ctx->S[1][b];
|
||||
y = y ^ ctx->S[2][c];
|
||||
y = y + ctx->S[3][d];
|
||||
|
||||
return (y);
|
||||
return( y );
|
||||
}
|
||||
|
||||
static void blowfish_enc(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr) {
|
||||
static void blowfish_enc( mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr )
|
||||
{
|
||||
uint32_t Xl, Xr, temp;
|
||||
short i;
|
||||
|
||||
Xl = *xl;
|
||||
Xr = *xr;
|
||||
|
||||
for (i = 0; i < MBEDTLS_BLOWFISH_ROUNDS; ++i) {
|
||||
for( i = 0; i < MBEDTLS_BLOWFISH_ROUNDS; ++i )
|
||||
{
|
||||
Xl = Xl ^ ctx->P[i];
|
||||
Xr = F(ctx, Xl) ^ Xr;
|
||||
Xr = F( ctx, Xl ) ^ Xr;
|
||||
|
||||
temp = Xl;
|
||||
Xl = Xr;
|
||||
|
@ -121,16 +122,18 @@ static void blowfish_enc(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *
|
|||
*xr = Xr;
|
||||
}
|
||||
|
||||
static void blowfish_dec(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr) {
|
||||
static void blowfish_dec( mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr )
|
||||
{
|
||||
uint32_t Xl, Xr, temp;
|
||||
short i;
|
||||
|
||||
Xl = *xl;
|
||||
Xr = *xr;
|
||||
|
||||
for (i = MBEDTLS_BLOWFISH_ROUNDS + 1; i > 1; --i) {
|
||||
for( i = MBEDTLS_BLOWFISH_ROUNDS + 1; i > 1; --i )
|
||||
{
|
||||
Xl = Xl ^ ctx->P[i];
|
||||
Xr = F(ctx, Xl) ^ Xr;
|
||||
Xr = F( ctx, Xl ) ^ Xr;
|
||||
|
||||
temp = Xl;
|
||||
Xl = Xr;
|
||||
|
@ -148,43 +151,55 @@ static void blowfish_dec(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *
|
|||
*xr = Xr;
|
||||
}
|
||||
|
||||
void mbedtls_blowfish_init(mbedtls_blowfish_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_blowfish_context));
|
||||
void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx )
|
||||
{
|
||||
BLOWFISH_VALIDATE( ctx != NULL );
|
||||
memset( ctx, 0, sizeof( mbedtls_blowfish_context ) );
|
||||
}
|
||||
|
||||
void mbedtls_blowfish_free(mbedtls_blowfish_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
return;
|
||||
|
||||
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_blowfish_context));
|
||||
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_blowfish_context ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* Blowfish key schedule
|
||||
*/
|
||||
int mbedtls_blowfish_setkey(mbedtls_blowfish_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits) {
|
||||
int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx,
|
||||
const unsigned char *key,
|
||||
unsigned int keybits )
|
||||
{
|
||||
unsigned int i, j, k;
|
||||
uint32_t data, datal, datar;
|
||||
BLOWFISH_VALIDATE_RET( ctx != NULL );
|
||||
BLOWFISH_VALIDATE_RET( key != NULL );
|
||||
|
||||
if (keybits < MBEDTLS_BLOWFISH_MIN_KEY_BITS || keybits > MBEDTLS_BLOWFISH_MAX_KEY_BITS ||
|
||||
(keybits % 8)) {
|
||||
return (MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH);
|
||||
if( keybits < MBEDTLS_BLOWFISH_MIN_KEY_BITS ||
|
||||
keybits > MBEDTLS_BLOWFISH_MAX_KEY_BITS ||
|
||||
keybits % 8 != 0 )
|
||||
{
|
||||
return( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
keybits >>= 3;
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (j = 0; j < 256; j++)
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
for( j = 0; j < 256; j++ )
|
||||
ctx->S[i][j] = S[i][j];
|
||||
}
|
||||
|
||||
j = 0;
|
||||
for (i = 0; i < MBEDTLS_BLOWFISH_ROUNDS + 2; ++i) {
|
||||
for( i = 0; i < MBEDTLS_BLOWFISH_ROUNDS + 2; ++i )
|
||||
{
|
||||
data = 0x00000000;
|
||||
for (k = 0; k < 4; ++k) {
|
||||
data = (data << 8) | key[j++];
|
||||
if (j >= keybits)
|
||||
for( k = 0; k < 4; ++k )
|
||||
{
|
||||
data = ( data << 8 ) | key[j++];
|
||||
if( j >= keybits )
|
||||
j = 0;
|
||||
}
|
||||
ctx->P[i] = P[i] ^ data;
|
||||
|
@ -193,83 +208,107 @@ int mbedtls_blowfish_setkey(mbedtls_blowfish_context *ctx, const unsigned char *
|
|||
datal = 0x00000000;
|
||||
datar = 0x00000000;
|
||||
|
||||
for (i = 0; i < MBEDTLS_BLOWFISH_ROUNDS + 2; i += 2) {
|
||||
blowfish_enc(ctx, &datal, &datar);
|
||||
for( i = 0; i < MBEDTLS_BLOWFISH_ROUNDS + 2; i += 2 )
|
||||
{
|
||||
blowfish_enc( ctx, &datal, &datar );
|
||||
ctx->P[i] = datal;
|
||||
ctx->P[i + 1] = datar;
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (j = 0; j < 256; j += 2) {
|
||||
blowfish_enc(ctx, &datal, &datar);
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
for( j = 0; j < 256; j += 2 )
|
||||
{
|
||||
blowfish_enc( ctx, &datal, &datar );
|
||||
ctx->S[i][j] = datal;
|
||||
ctx->S[i][j + 1] = datar;
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Blowfish-ECB block encryption/decryption
|
||||
*/
|
||||
int mbedtls_blowfish_crypt_ecb(mbedtls_blowfish_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE]) {
|
||||
int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] )
|
||||
{
|
||||
uint32_t X0, X1;
|
||||
BLOWFISH_VALIDATE_RET( ctx != NULL );
|
||||
BLOWFISH_VALIDATE_RET( mode == MBEDTLS_BLOWFISH_ENCRYPT ||
|
||||
mode == MBEDTLS_BLOWFISH_DECRYPT );
|
||||
BLOWFISH_VALIDATE_RET( input != NULL );
|
||||
BLOWFISH_VALIDATE_RET( output != NULL );
|
||||
|
||||
GET_UINT32_BE(X0, input, 0);
|
||||
GET_UINT32_BE(X1, input, 4);
|
||||
GET_UINT32_BE( X0, input, 0 );
|
||||
GET_UINT32_BE( X1, input, 4 );
|
||||
|
||||
if (mode == MBEDTLS_BLOWFISH_DECRYPT) {
|
||||
blowfish_dec(ctx, &X0, &X1);
|
||||
} else { /* MBEDTLS_BLOWFISH_ENCRYPT */
|
||||
blowfish_enc(ctx, &X0, &X1);
|
||||
if( mode == MBEDTLS_BLOWFISH_DECRYPT )
|
||||
{
|
||||
blowfish_dec( ctx, &X0, &X1 );
|
||||
}
|
||||
else /* MBEDTLS_BLOWFISH_ENCRYPT */
|
||||
{
|
||||
blowfish_enc( ctx, &X0, &X1 );
|
||||
}
|
||||
|
||||
PUT_UINT32_BE(X0, output, 0);
|
||||
PUT_UINT32_BE(X1, output, 4);
|
||||
PUT_UINT32_BE( X0, output, 0 );
|
||||
PUT_UINT32_BE( X1, output, 4 );
|
||||
|
||||
return (0);
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
/*
|
||||
* Blowfish-CBC buffer encryption/decryption
|
||||
*/
|
||||
int mbedtls_blowfish_crypt_cbc(mbedtls_blowfish_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
const unsigned char *input,
|
||||
unsigned char *output) {
|
||||
int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
int i;
|
||||
unsigned char temp[MBEDTLS_BLOWFISH_BLOCKSIZE];
|
||||
BLOWFISH_VALIDATE_RET( ctx != NULL );
|
||||
BLOWFISH_VALIDATE_RET( mode == MBEDTLS_BLOWFISH_ENCRYPT ||
|
||||
mode == MBEDTLS_BLOWFISH_DECRYPT );
|
||||
BLOWFISH_VALIDATE_RET( iv != NULL );
|
||||
BLOWFISH_VALIDATE_RET( length == 0 || input != NULL );
|
||||
BLOWFISH_VALIDATE_RET( length == 0 || output != NULL );
|
||||
|
||||
if (length % MBEDTLS_BLOWFISH_BLOCKSIZE)
|
||||
return (MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH);
|
||||
if( length % MBEDTLS_BLOWFISH_BLOCKSIZE )
|
||||
return( MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH );
|
||||
|
||||
if (mode == MBEDTLS_BLOWFISH_DECRYPT) {
|
||||
while (length > 0) {
|
||||
memcpy(temp, input, MBEDTLS_BLOWFISH_BLOCKSIZE);
|
||||
mbedtls_blowfish_crypt_ecb(ctx, mode, input, output);
|
||||
if( mode == MBEDTLS_BLOWFISH_DECRYPT )
|
||||
{
|
||||
while( length > 0 )
|
||||
{
|
||||
memcpy( temp, input, MBEDTLS_BLOWFISH_BLOCKSIZE );
|
||||
mbedtls_blowfish_crypt_ecb( ctx, mode, input, output );
|
||||
|
||||
for (i = 0; i < MBEDTLS_BLOWFISH_BLOCKSIZE; i++)
|
||||
output[i] = (unsigned char)(output[i] ^ iv[i]);
|
||||
for( i = 0; i < MBEDTLS_BLOWFISH_BLOCKSIZE;i++ )
|
||||
output[i] = (unsigned char)( output[i] ^ iv[i] );
|
||||
|
||||
memcpy(iv, temp, MBEDTLS_BLOWFISH_BLOCKSIZE);
|
||||
memcpy( iv, temp, MBEDTLS_BLOWFISH_BLOCKSIZE );
|
||||
|
||||
input += MBEDTLS_BLOWFISH_BLOCKSIZE;
|
||||
output += MBEDTLS_BLOWFISH_BLOCKSIZE;
|
||||
length -= MBEDTLS_BLOWFISH_BLOCKSIZE;
|
||||
}
|
||||
} else {
|
||||
while (length > 0) {
|
||||
for (i = 0; i < MBEDTLS_BLOWFISH_BLOCKSIZE; i++)
|
||||
output[i] = (unsigned char)(input[i] ^ iv[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
while( length > 0 )
|
||||
{
|
||||
for( i = 0; i < MBEDTLS_BLOWFISH_BLOCKSIZE; i++ )
|
||||
output[i] = (unsigned char)( input[i] ^ iv[i] );
|
||||
|
||||
mbedtls_blowfish_crypt_ecb(ctx, mode, output, output);
|
||||
memcpy(iv, output, MBEDTLS_BLOWFISH_BLOCKSIZE);
|
||||
mbedtls_blowfish_crypt_ecb( ctx, mode, output, output );
|
||||
memcpy( iv, output, MBEDTLS_BLOWFISH_BLOCKSIZE );
|
||||
|
||||
input += MBEDTLS_BLOWFISH_BLOCKSIZE;
|
||||
output += MBEDTLS_BLOWFISH_BLOCKSIZE;
|
||||
|
@ -277,7 +316,7 @@ int mbedtls_blowfish_crypt_cbc(mbedtls_blowfish_context *ctx,
|
|||
}
|
||||
}
|
||||
|
||||
return (0);
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
||||
|
||||
|
@ -285,41 +324,59 @@ int mbedtls_blowfish_crypt_cbc(mbedtls_blowfish_context *ctx,
|
|||
/*
|
||||
* Blowfish CFB buffer encryption/decryption
|
||||
*/
|
||||
int mbedtls_blowfish_crypt_cfb64(mbedtls_blowfish_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
size_t *iv_off,
|
||||
unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
const unsigned char *input,
|
||||
unsigned char *output) {
|
||||
int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
size_t *iv_off,
|
||||
unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
int c;
|
||||
size_t n = *iv_off;
|
||||
size_t n;
|
||||
|
||||
if (mode == MBEDTLS_BLOWFISH_DECRYPT) {
|
||||
while (length--) {
|
||||
if (n == 0)
|
||||
mbedtls_blowfish_crypt_ecb(ctx, MBEDTLS_BLOWFISH_ENCRYPT, iv, iv);
|
||||
BLOWFISH_VALIDATE_RET( ctx != NULL );
|
||||
BLOWFISH_VALIDATE_RET( mode == MBEDTLS_BLOWFISH_ENCRYPT ||
|
||||
mode == MBEDTLS_BLOWFISH_DECRYPT );
|
||||
BLOWFISH_VALIDATE_RET( iv != NULL );
|
||||
BLOWFISH_VALIDATE_RET( iv_off != NULL );
|
||||
BLOWFISH_VALIDATE_RET( length == 0 || input != NULL );
|
||||
BLOWFISH_VALIDATE_RET( length == 0 || output != NULL );
|
||||
|
||||
n = *iv_off;
|
||||
if( n >= 8 )
|
||||
return( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA );
|
||||
|
||||
if( mode == MBEDTLS_BLOWFISH_DECRYPT )
|
||||
{
|
||||
while( length-- )
|
||||
{
|
||||
if( n == 0 )
|
||||
mbedtls_blowfish_crypt_ecb( ctx, MBEDTLS_BLOWFISH_ENCRYPT, iv, iv );
|
||||
|
||||
c = *input++;
|
||||
*output++ = (unsigned char)(c ^ iv[n]);
|
||||
*output++ = (unsigned char)( c ^ iv[n] );
|
||||
iv[n] = (unsigned char) c;
|
||||
|
||||
n = (n + 1) % MBEDTLS_BLOWFISH_BLOCKSIZE;
|
||||
n = ( n + 1 ) % MBEDTLS_BLOWFISH_BLOCKSIZE;
|
||||
}
|
||||
} else {
|
||||
while (length--) {
|
||||
if (n == 0)
|
||||
mbedtls_blowfish_crypt_ecb(ctx, MBEDTLS_BLOWFISH_ENCRYPT, iv, iv);
|
||||
}
|
||||
else
|
||||
{
|
||||
while( length-- )
|
||||
{
|
||||
if( n == 0 )
|
||||
mbedtls_blowfish_crypt_ecb( ctx, MBEDTLS_BLOWFISH_ENCRYPT, iv, iv );
|
||||
|
||||
iv[n] = *output++ = (unsigned char)(iv[n] ^ *input++);
|
||||
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
|
||||
|
||||
n = (n + 1) % MBEDTLS_BLOWFISH_BLOCKSIZE;
|
||||
n = ( n + 1 ) % MBEDTLS_BLOWFISH_BLOCKSIZE;
|
||||
}
|
||||
}
|
||||
|
||||
*iv_off = n;
|
||||
|
||||
return (0);
|
||||
return( 0 );
|
||||
}
|
||||
#endif /*MBEDTLS_CIPHER_MODE_CFB */
|
||||
|
||||
|
@ -327,40 +384,51 @@ int mbedtls_blowfish_crypt_cfb64(mbedtls_blowfish_context *ctx,
|
|||
/*
|
||||
* Blowfish CTR buffer encryption/decryption
|
||||
*/
|
||||
int mbedtls_blowfish_crypt_ctr(mbedtls_blowfish_context *ctx,
|
||||
size_t length,
|
||||
size_t *nc_off,
|
||||
unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
const unsigned char *input,
|
||||
unsigned char *output) {
|
||||
int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
|
||||
size_t length,
|
||||
size_t *nc_off,
|
||||
unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
int c, i;
|
||||
size_t n = *nc_off;
|
||||
size_t n;
|
||||
BLOWFISH_VALIDATE_RET( ctx != NULL );
|
||||
BLOWFISH_VALIDATE_RET( nonce_counter != NULL );
|
||||
BLOWFISH_VALIDATE_RET( stream_block != NULL );
|
||||
BLOWFISH_VALIDATE_RET( nc_off != NULL );
|
||||
BLOWFISH_VALIDATE_RET( length == 0 || input != NULL );
|
||||
BLOWFISH_VALIDATE_RET( length == 0 || output != NULL );
|
||||
|
||||
while (length--) {
|
||||
if (n == 0) {
|
||||
mbedtls_blowfish_crypt_ecb(ctx, MBEDTLS_BLOWFISH_ENCRYPT, nonce_counter,
|
||||
stream_block);
|
||||
n = *nc_off;
|
||||
if( n >= 8 )
|
||||
return( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA );
|
||||
|
||||
for (i = MBEDTLS_BLOWFISH_BLOCKSIZE; i > 0; i--)
|
||||
if (++nonce_counter[i - 1] != 0)
|
||||
while( length-- )
|
||||
{
|
||||
if( n == 0 ) {
|
||||
mbedtls_blowfish_crypt_ecb( ctx, MBEDTLS_BLOWFISH_ENCRYPT, nonce_counter,
|
||||
stream_block );
|
||||
|
||||
for( i = MBEDTLS_BLOWFISH_BLOCKSIZE; i > 0; i-- )
|
||||
if( ++nonce_counter[i - 1] != 0 )
|
||||
break;
|
||||
}
|
||||
c = *input++;
|
||||
*output++ = (unsigned char)(c ^ stream_block[n]);
|
||||
*output++ = (unsigned char)( c ^ stream_block[n] );
|
||||
|
||||
n = (n + 1) % MBEDTLS_BLOWFISH_BLOCKSIZE;
|
||||
n = ( n + 1 ) % MBEDTLS_BLOWFISH_BLOCKSIZE;
|
||||
}
|
||||
|
||||
*nc_off = n;
|
||||
|
||||
return (0);
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CTR */
|
||||
|
||||
static const uint32_t S[4][256] = {
|
||||
{
|
||||
0xD1310BA6L, 0x98DFB5ACL, 0x2FFD72DBL, 0xD01ADFB7L,
|
||||
{ 0xD1310BA6L, 0x98DFB5ACL, 0x2FFD72DBL, 0xD01ADFB7L,
|
||||
0xB8E1AFEDL, 0x6A267E96L, 0xBA7C9045L, 0xF12C7F99L,
|
||||
0x24A19947L, 0xB3916CF7L, 0x0801F2E2L, 0x858EFC16L,
|
||||
0x636920D8L, 0x71574E69L, 0xA458FEA3L, 0xF4933D7EL,
|
||||
|
@ -423,10 +491,8 @@ static const uint32_t S[4][256] = {
|
|||
0xD60F573FL, 0xBC9BC6E4L, 0x2B60A476L, 0x81E67400L,
|
||||
0x08BA6FB5L, 0x571BE91FL, 0xF296EC6BL, 0x2A0DD915L,
|
||||
0xB6636521L, 0xE7B9F9B6L, 0xFF34052EL, 0xC5855664L,
|
||||
0x53B02D5DL, 0xA99F8FA1L, 0x08BA4799L, 0x6E85076AL
|
||||
},
|
||||
{
|
||||
0x4B7A70E9L, 0xB5B32944L, 0xDB75092EL, 0xC4192623L,
|
||||
0x53B02D5DL, 0xA99F8FA1L, 0x08BA4799L, 0x6E85076AL },
|
||||
{ 0x4B7A70E9L, 0xB5B32944L, 0xDB75092EL, 0xC4192623L,
|
||||
0xAD6EA6B0L, 0x49A7DF7DL, 0x9CEE60B8L, 0x8FEDB266L,
|
||||
0xECAA8C71L, 0x699A17FFL, 0x5664526CL, 0xC2B19EE1L,
|
||||
0x193602A5L, 0x75094C29L, 0xA0591340L, 0xE4183A3EL,
|
||||
|
@ -489,10 +555,8 @@ static const uint32_t S[4][256] = {
|
|||
0x9E447A2EL, 0xC3453484L, 0xFDD56705L, 0x0E1E9EC9L,
|
||||
0xDB73DBD3L, 0x105588CDL, 0x675FDA79L, 0xE3674340L,
|
||||
0xC5C43465L, 0x713E38D8L, 0x3D28F89EL, 0xF16DFF20L,
|
||||
0x153E21E7L, 0x8FB03D4AL, 0xE6E39F2BL, 0xDB83ADF7L
|
||||
},
|
||||
{
|
||||
0xE93D5A68L, 0x948140F7L, 0xF64C261CL, 0x94692934L,
|
||||
0x153E21E7L, 0x8FB03D4AL, 0xE6E39F2BL, 0xDB83ADF7L },
|
||||
{ 0xE93D5A68L, 0x948140F7L, 0xF64C261CL, 0x94692934L,
|
||||
0x411520F7L, 0x7602D4F7L, 0xBCF46B2EL, 0xD4A20068L,
|
||||
0xD4082471L, 0x3320F46AL, 0x43B7D4B7L, 0x500061AFL,
|
||||
0x1E39F62EL, 0x97244546L, 0x14214F74L, 0xBF8B8840L,
|
||||
|
@ -555,10 +619,8 @@ static const uint32_t S[4][256] = {
|
|||
0xED545578L, 0x08FCA5B5L, 0xD83D7CD3L, 0x4DAD0FC4L,
|
||||
0x1E50EF5EL, 0xB161E6F8L, 0xA28514D9L, 0x6C51133CL,
|
||||
0x6FD5C7E7L, 0x56E14EC4L, 0x362ABFCEL, 0xDDC6C837L,
|
||||
0xD79A3234L, 0x92638212L, 0x670EFA8EL, 0x406000E0L
|
||||
},
|
||||
{
|
||||
0x3A39CE37L, 0xD3FAF5CFL, 0xABC27737L, 0x5AC52D1BL,
|
||||
0xD79A3234L, 0x92638212L, 0x670EFA8EL, 0x406000E0L },
|
||||
{ 0x3A39CE37L, 0xD3FAF5CFL, 0xABC27737L, 0x5AC52D1BL,
|
||||
0x5CB0679EL, 0x4FA33742L, 0xD3822740L, 0x99BC9BBEL,
|
||||
0xD5118E9DL, 0xBF0F7315L, 0xD62D1C7EL, 0xC700C47BL,
|
||||
0xB78C1B6BL, 0x21A19045L, 0xB26EB1BEL, 0x6A366EB4L,
|
||||
|
@ -621,8 +683,7 @@ static const uint32_t S[4][256] = {
|
|||
0x85CBFE4EL, 0x8AE88DD8L, 0x7AAAF9B0L, 0x4CF9AA7EL,
|
||||
0x1948C25CL, 0x02FB8A8CL, 0x01C36AE4L, 0xD6EBE1F9L,
|
||||
0x90D4F869L, 0xA65CDEA0L, 0x3F09252DL, 0xC208E69FL,
|
||||
0xB74E6132L, 0xCE77E25BL, 0x578FDFE3L, 0x3AC372E6L
|
||||
}
|
||||
0xB74E6132L, 0xCE77E25BL, 0x578FDFE3L, 0x3AC372E6L }
|
||||
};
|
||||
|
||||
#endif /* !MBEDTLS_BLOWFISH_ALT */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue