~cpick/mongrel2/release

« back to all changes in this revision

Viewing changes to src/polarssl/aes.c

  • Committer: Chris Pick
  • Date: 2013-06-30 16:39:57 UTC
  • mfrom: (1106.1.15)
  • Revision ID: git-v1:ec39967acb6bc9867ed9b9dc3774304ca6b9c294
Merge tag 'v1.8.1' into debian

Hotfix for github issue 148

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include "polarssl/aes.h"
37
37
#include "polarssl/padlock.h"
38
38
 
39
 
#include <string.h>
40
 
 
41
39
/*
42
40
 * 32-bit integer manipulation macros (little endian)
43
41
 */
441
439
/*
442
440
 * AES key schedule (encryption)
443
441
 */
444
 
int aes_setkey_enc( aes_context *ctx, const unsigned char *key, int keysize )
 
442
int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize )
445
443
{
446
 
    int i;
 
444
    unsigned int i;
447
445
    unsigned long *RK;
448
446
 
449
447
#if !defined(POLARSSL_AES_ROM_TABLES)
546
544
/*
547
545
 * AES key schedule (decryption)
548
546
 */
549
 
int aes_setkey_dec( aes_context *ctx, const unsigned char *key, int keysize )
 
547
int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize )
550
548
{
551
549
    int i, j;
552
550
    aes_context cty;
758
756
 */
759
757
int aes_crypt_cbc( aes_context *ctx,
760
758
                    int mode,
761
 
                    int length,
 
759
                    size_t length,
762
760
                    unsigned char iv[16],
763
761
                    const unsigned char *input,
764
762
                    unsigned char *output )
817
815
    return( 0 );
818
816
}
819
817
 
 
818
#if defined(POLARSSL_CIPHER_MODE_CFB)
820
819
/*
821
820
 * AES-CFB128 buffer encryption/decryption
822
821
 */
823
822
int aes_crypt_cfb128( aes_context *ctx,
824
823
                       int mode,
825
 
                       int length,
826
 
                       int *iv_off,
 
824
                       size_t length,
 
825
                       size_t *iv_off,
827
826
                       unsigned char iv[16],
828
827
                       const unsigned char *input,
829
828
                       unsigned char *output )
830
829
{
831
 
    int c, n = *iv_off;
 
830
    int c;
 
831
    size_t n = *iv_off;
832
832
 
833
833
    if( mode == AES_DECRYPT )
834
834
    {
861
861
 
862
862
    return( 0 );
863
863
}
 
864
#endif /*POLARSSL_CIPHER_MODE_CFB */
 
865
 
 
866
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
867
/*
 
868
 * AES-CTR buffer encryption/decryption
 
869
 */
 
870
int aes_crypt_ctr( aes_context *ctx,
 
871
                       size_t length,
 
872
                       size_t *nc_off,
 
873
                       unsigned char nonce_counter[16],
 
874
                       unsigned char stream_block[16],
 
875
                       const unsigned char *input,
 
876
                       unsigned char *output )
 
877
{
 
878
    int c, i, cb;
 
879
    size_t n = *nc_off;
 
880
 
 
881
    while( length-- )
 
882
    {
 
883
        if( n == 0 ) {
 
884
            aes_crypt_ecb( ctx, AES_ENCRYPT, nonce_counter, stream_block );
 
885
 
 
886
            i = 15;
 
887
            do {
 
888
               nonce_counter[i]++;
 
889
               cb = nonce_counter[i] == 0;
 
890
            } while( i-- && cb );
 
891
 
 
892
        }
 
893
        c = *input++;
 
894
        *output++ = (unsigned char)( c ^ stream_block[n] );
 
895
 
 
896
        n = (n + 1) & 0x0F;
 
897
    }
 
898
 
 
899
    *nc_off = n;
 
900
 
 
901
    return( 0 );
 
902
}
 
903
#endif /* POLARSSL_CIPHER_MODE_CTR */
864
904
 
865
905
#if defined(POLARSSL_SELF_TEST)
866
906
 
911
951
      0x6F, 0xCD, 0x88, 0xB2, 0xCC, 0x89, 0x8F, 0xF0 }
912
952
};
913
953
 
 
954
#if defined(POLARSSL_CIPHER_MODE_CFB)
914
955
/*
915
956
 * AES-CFB128 test vectors from:
916
957
 *
974
1015
      0x75, 0xA3, 0x85, 0x74, 0x1A, 0xB9, 0xCE, 0xF8,
975
1016
      0x20, 0x31, 0x62, 0x3D, 0x55, 0xB1, 0xE4, 0x71 }
976
1017
};
 
1018
#endif /* POLARSSL_CIPHER_MODE_CFB */
 
1019
 
 
1020
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
1021
/*
 
1022
 * AES-CTR test vectors from:
 
1023
 *
 
1024
 * http://www.faqs.org/rfcs/rfc3686.html
 
1025
 */
 
1026
 
 
1027
static const unsigned char aes_test_ctr_key[3][16] =
 
1028
{
 
1029
    { 0xAE, 0x68, 0x52, 0xF8, 0x12, 0x10, 0x67, 0xCC,
 
1030
      0x4B, 0xF7, 0xA5, 0x76, 0x55, 0x77, 0xF3, 0x9E },
 
1031
    { 0x7E, 0x24, 0x06, 0x78, 0x17, 0xFA, 0xE0, 0xD7,
 
1032
      0x43, 0xD6, 0xCE, 0x1F, 0x32, 0x53, 0x91, 0x63 },
 
1033
    { 0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8,
 
1034
      0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC }
 
1035
};
 
1036
 
 
1037
static const unsigned char aes_test_ctr_nonce_counter[3][16] =
 
1038
{
 
1039
    { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
 
1040
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
 
1041
    { 0x00, 0x6C, 0xB6, 0xDB, 0xC0, 0x54, 0x3B, 0x59,
 
1042
      0xDA, 0x48, 0xD9, 0x0B, 0x00, 0x00, 0x00, 0x01 },
 
1043
    { 0x00, 0xE0, 0x01, 0x7B, 0x27, 0x77, 0x7F, 0x3F,
 
1044
      0x4A, 0x17, 0x86, 0xF0, 0x00, 0x00, 0x00, 0x01 }
 
1045
};
 
1046
 
 
1047
static const unsigned char aes_test_ctr_pt[3][48] =
 
1048
{
 
1049
    { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
 
1050
      0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
 
1051
 
 
1052
    { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 
1053
      0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
 
1054
      0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 
1055
      0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
 
1056
 
 
1057
    { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 
1058
      0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
 
1059
      0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 
1060
      0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
 
1061
      0x20, 0x21, 0x22, 0x23 }
 
1062
};
 
1063
 
 
1064
static const unsigned char aes_test_ctr_ct[3][48] =
 
1065
{
 
1066
    { 0xE4, 0x09, 0x5D, 0x4F, 0xB7, 0xA7, 0xB3, 0x79,
 
1067
      0x2D, 0x61, 0x75, 0xA3, 0x26, 0x13, 0x11, 0xB8 },
 
1068
    { 0x51, 0x04, 0xA1, 0x06, 0x16, 0x8A, 0x72, 0xD9,
 
1069
      0x79, 0x0D, 0x41, 0xEE, 0x8E, 0xDA, 0xD3, 0x88,
 
1070
      0xEB, 0x2E, 0x1E, 0xFC, 0x46, 0xDA, 0x57, 0xC8,
 
1071
      0xFC, 0xE6, 0x30, 0xDF, 0x91, 0x41, 0xBE, 0x28 },
 
1072
    { 0xC1, 0xCF, 0x48, 0xA8, 0x9F, 0x2F, 0xFD, 0xD9,
 
1073
      0xCF, 0x46, 0x52, 0xE9, 0xEF, 0xDB, 0x72, 0xD7,
 
1074
      0x45, 0x40, 0xA4, 0x2B, 0xDE, 0x6D, 0x78, 0x36,
 
1075
      0xD5, 0x9A, 0x5C, 0xEA, 0xAE, 0xF3, 0x10, 0x53,
 
1076
      0x25, 0xB2, 0x07, 0x2F }
 
1077
};
 
1078
 
 
1079
static const int aes_test_ctr_len[3] =
 
1080
    { 16, 32, 36 };
 
1081
#endif /* POLARSSL_CIPHER_MODE_CTR */
977
1082
 
978
1083
/*
979
1084
 * Checkup routine
980
1085
 */
981
1086
int aes_self_test( int verbose )
982
1087
{
983
 
    int i, j, u, v, offset;
 
1088
    int i, j, u, v;
984
1089
    unsigned char key[32];
985
1090
    unsigned char buf[64];
986
1091
    unsigned char prv[16];
987
1092
    unsigned char iv[16];
 
1093
#if defined(POLARSSL_CIPHER_MODE_CTR) || defined(POLARSSL_CIPHER_MODE_CFB)
 
1094
    size_t offset;
 
1095
#endif
 
1096
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
1097
    int len;
 
1098
    unsigned char nonce_counter[16];
 
1099
    unsigned char stream_block[16];
 
1100
#endif
988
1101
    aes_context ctx;
989
1102
 
990
1103
    memset( key, 0, 32 );
1103
1216
    if( verbose != 0 )
1104
1217
        printf( "\n" );
1105
1218
 
 
1219
#if defined(POLARSSL_CIPHER_MODE_CFB)
1106
1220
    /*
1107
1221
     * CFB128 mode
1108
1222
     */
1152
1266
            printf( "passed\n" );
1153
1267
    }
1154
1268
 
1155
 
 
1156
 
    if( verbose != 0 )
1157
 
        printf( "\n" );
 
1269
    if( verbose != 0 )
 
1270
        printf( "\n" );
 
1271
#endif /* POLARSSL_CIPHER_MODE_CFB */
 
1272
 
 
1273
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
1274
    /*
 
1275
     * CTR mode
 
1276
     */
 
1277
    for( i = 0; i < 6; i++ )
 
1278
    {
 
1279
        u = i >> 1;
 
1280
        v = i  & 1;
 
1281
 
 
1282
        if( verbose != 0 )
 
1283
            printf( "  AES-CTR-128 (%s): ",
 
1284
                    ( v == AES_DECRYPT ) ? "dec" : "enc" );
 
1285
 
 
1286
        memcpy( nonce_counter, aes_test_ctr_nonce_counter[u], 16 );
 
1287
        memcpy( key, aes_test_ctr_key[u], 16 );
 
1288
 
 
1289
        offset = 0;
 
1290
        aes_setkey_enc( &ctx, key, 128 );
 
1291
 
 
1292
        if( v == AES_DECRYPT )
 
1293
        {
 
1294
            len = aes_test_ctr_len[u];
 
1295
            memcpy( buf, aes_test_ctr_ct[u], len );
 
1296
 
 
1297
            aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block, buf, buf );
 
1298
 
 
1299
            if( memcmp( buf, aes_test_ctr_pt[u], len ) != 0 )
 
1300
            {
 
1301
                if( verbose != 0 )
 
1302
                    printf( "failed\n" );
 
1303
 
 
1304
                return( 1 );
 
1305
            }
 
1306
        }
 
1307
        else
 
1308
        {
 
1309
            len = aes_test_ctr_len[u];
 
1310
            memcpy( buf, aes_test_ctr_pt[u], len );
 
1311
 
 
1312
            aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block, buf, buf );
 
1313
 
 
1314
            if( memcmp( buf, aes_test_ctr_ct[u], len ) != 0 )
 
1315
            {
 
1316
                if( verbose != 0 )
 
1317
                    printf( "failed\n" );
 
1318
 
 
1319
                return( 1 );
 
1320
            }
 
1321
        }
 
1322
 
 
1323
        if( verbose != 0 )
 
1324
            printf( "passed\n" );
 
1325
    }
 
1326
 
 
1327
    if( verbose != 0 )
 
1328
        printf( "\n" );
 
1329
#endif /* POLARSSL_CIPHER_MODE_CTR */
1158
1330
 
1159
1331
    return( 0 );
1160
1332
}