~cpick/mongrel2/release

« back to all changes in this revision

Viewing changes to src/polarssl/camellia.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:
35
35
 
36
36
#include "polarssl/camellia.h"
37
37
 
38
 
#include <string.h>
39
 
 
40
38
/*
41
39
 * 32-bit integer manipulation macros (big endian)
42
40
 */
70
68
    { 0xb0, 0x56, 0x88, 0xc2, 0xb3, 0xe6, 0xc1, 0xfd }
71
69
};
72
70
 
73
 
#ifdef POLARSSL_CAMELLIA_SMALL_MEMORY
 
71
#if defined(POLARSSL_CAMELLIA_SMALL_MEMORY)
74
72
 
75
73
static const unsigned char FSb[256] =
76
74
{
309
307
/*
310
308
 * Camellia key schedule (encryption)
311
309
 */
312
 
int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key, int keysize )
 
310
int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key, unsigned int keysize )
313
311
{
314
 
    int i, idx;
 
312
    int idx;
 
313
    size_t i;
315
314
    uint32_t *RK;
316
315
    unsigned char t[64];
317
316
    uint32_t SIGMA[6][2];
412
411
/*
413
412
 * Camellia key schedule (decryption)
414
413
 */
415
 
int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key, int keysize )
 
414
int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key, unsigned int keysize )
416
415
{
417
 
    int i, idx;
 
416
    int idx;
 
417
    size_t i;
418
418
    camellia_context cty;
419
419
    uint32_t *RK;
420
420
    uint32_t *SK;
526
526
 */
527
527
int camellia_crypt_cbc( camellia_context *ctx,
528
528
                    int mode,
529
 
                    int length,
 
529
                    size_t length,
530
530
                    unsigned char iv[16],
531
531
                    const unsigned char *input,
532
532
                    unsigned char *output )
573
573
    return( 0 );
574
574
}
575
575
 
 
576
#if defined(POLARSSL_CIPHER_MODE_CFB)
576
577
/*
577
578
 * Camellia-CFB128 buffer encryption/decryption
578
579
 */
579
580
int camellia_crypt_cfb128( camellia_context *ctx,
580
581
                       int mode,
581
 
                       int length,
582
 
                       int *iv_off,
 
582
                       size_t length,
 
583
                       size_t *iv_off,
583
584
                       unsigned char iv[16],
584
585
                       const unsigned char *input,
585
586
                       unsigned char *output )
586
587
{
587
 
    int c, n = *iv_off;
 
588
    int c;
 
589
    size_t n = *iv_off;
588
590
 
589
591
    if( mode == CAMELLIA_DECRYPT )
590
592
    {
617
619
 
618
620
    return( 0 );
619
621
}
 
622
#endif /* POLARSSL_CIPHER_MODE_CFB */
 
623
 
 
624
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
625
/*
 
626
 * Camellia-CTR buffer encryption/decryption
 
627
 */
 
628
int camellia_crypt_ctr( camellia_context *ctx,
 
629
                       size_t length,
 
630
                       size_t *nc_off,
 
631
                       unsigned char nonce_counter[16],
 
632
                       unsigned char stream_block[16],
 
633
                       const unsigned char *input,
 
634
                       unsigned char *output )
 
635
{
 
636
    int c, i, cb;
 
637
    size_t n = *nc_off;
 
638
 
 
639
    while( length-- )
 
640
    {
 
641
        if( n == 0 ) {
 
642
            camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, nonce_counter, stream_block );
 
643
 
 
644
            i = 15;
 
645
            do {
 
646
               nonce_counter[i]++;
 
647
               cb = nonce_counter[i] == 0;
 
648
            } while( i-- && cb );
 
649
 
 
650
        }
 
651
        c = *input++;
 
652
        *output++ = (unsigned char)( c ^ stream_block[n] );
 
653
 
 
654
        n = (n + 1) & 0x0F;
 
655
    }
 
656
 
 
657
    *nc_off = n;
 
658
 
 
659
    return( 0 );
 
660
}
 
661
#endif /* POLARSSL_CIPHER_MODE_CTR */
620
662
 
621
663
#if defined(POLARSSL_SELF_TEST)
622
664
 
752
794
    }
753
795
};
754
796
 
 
797
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
798
/*
 
799
 * Camellia-CTR test vectors from:
 
800
 *
 
801
 * http://www.faqs.org/rfcs/rfc5528.html
 
802
 */
 
803
 
 
804
static const unsigned char camellia_test_ctr_key[3][16] =
 
805
{
 
806
    { 0xAE, 0x68, 0x52, 0xF8, 0x12, 0x10, 0x67, 0xCC,
 
807
      0x4B, 0xF7, 0xA5, 0x76, 0x55, 0x77, 0xF3, 0x9E },
 
808
    { 0x7E, 0x24, 0x06, 0x78, 0x17, 0xFA, 0xE0, 0xD7,
 
809
      0x43, 0xD6, 0xCE, 0x1F, 0x32, 0x53, 0x91, 0x63 },
 
810
    { 0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8,
 
811
      0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC }
 
812
};
 
813
 
 
814
static const unsigned char camellia_test_ctr_nonce_counter[3][16] =
 
815
{
 
816
    { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
 
817
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
 
818
    { 0x00, 0x6C, 0xB6, 0xDB, 0xC0, 0x54, 0x3B, 0x59,
 
819
      0xDA, 0x48, 0xD9, 0x0B, 0x00, 0x00, 0x00, 0x01 },
 
820
    { 0x00, 0xE0, 0x01, 0x7B, 0x27, 0x77, 0x7F, 0x3F,
 
821
      0x4A, 0x17, 0x86, 0xF0, 0x00, 0x00, 0x00, 0x01 }
 
822
};
 
823
 
 
824
static const unsigned char camellia_test_ctr_pt[3][48] =
 
825
{
 
826
    { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
 
827
      0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
 
828
 
 
829
    { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 
830
      0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
 
831
      0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 
832
      0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
 
833
 
 
834
    { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 
835
      0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
 
836
      0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 
837
      0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
 
838
      0x20, 0x21, 0x22, 0x23 }
 
839
};
 
840
 
 
841
static const unsigned char camellia_test_ctr_ct[3][48] =
 
842
{
 
843
    { 0xD0, 0x9D, 0xC2, 0x9A, 0x82, 0x14, 0x61, 0x9A,
 
844
      0x20, 0x87, 0x7C, 0x76, 0xDB, 0x1F, 0x0B, 0x3F },
 
845
    { 0xDB, 0xF3, 0xC7, 0x8D, 0xC0, 0x83, 0x96, 0xD4,
 
846
      0xDA, 0x7C, 0x90, 0x77, 0x65, 0xBB, 0xCB, 0x44,
 
847
      0x2B, 0x8E, 0x8E, 0x0F, 0x31, 0xF0, 0xDC, 0xA7,
 
848
      0x2C, 0x74, 0x17, 0xE3, 0x53, 0x60, 0xE0, 0x48 },
 
849
    { 0xB1, 0x9D, 0x1F, 0xCD, 0xCB, 0x75, 0xEB, 0x88,
 
850
      0x2F, 0x84, 0x9C, 0xE2, 0x4D, 0x85, 0xCF, 0x73,
 
851
      0x9C, 0xE6, 0x4B, 0x2B, 0x5C, 0x9D, 0x73, 0xF1,
 
852
      0x4F, 0x2D, 0x5D, 0x9D, 0xCE, 0x98, 0x89, 0xCD,
 
853
      0xDF, 0x50, 0x86, 0x96 }
 
854
};
 
855
 
 
856
static const int camellia_test_ctr_len[3] =
 
857
    { 16, 32, 36 };
 
858
#endif /* POLARSSL_CIPHER_MODE_CTR */
755
859
 
756
860
/*
757
861
 * Checkup routine
764
868
    unsigned char src[16];
765
869
    unsigned char dst[16];
766
870
    unsigned char iv[16];
 
871
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
872
    size_t offset, len;
 
873
    unsigned char nonce_counter[16];
 
874
    unsigned char stream_block[16];
 
875
#endif
 
876
 
767
877
    camellia_context ctx;
768
878
 
769
879
    memset( key, 0, 32 );
859
969
    if( verbose != 0 )
860
970
        printf( "\n" );
861
971
 
 
972
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
973
    /*
 
974
     * CTR mode
 
975
     */
 
976
    for( i = 0; i < 6; i++ )
 
977
    {
 
978
        u = i >> 1;
 
979
        v = i  & 1;
 
980
 
 
981
        if( verbose != 0 )
 
982
            printf( "  CAMELLIA-CTR-128 (%s): ",
 
983
                    ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" );
 
984
 
 
985
        memcpy( nonce_counter, camellia_test_ctr_nonce_counter[u], 16 );
 
986
        memcpy( key, camellia_test_ctr_key[u], 16 );
 
987
 
 
988
        offset = 0;
 
989
        camellia_setkey_enc( &ctx, key, 128 );
 
990
 
 
991
        if( v == CAMELLIA_DECRYPT )
 
992
        {
 
993
            len = camellia_test_ctr_len[u];
 
994
            memcpy( buf, camellia_test_ctr_ct[u], len );
 
995
 
 
996
            camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block, buf, buf );
 
997
 
 
998
            if( memcmp( buf, camellia_test_ctr_pt[u], len ) != 0 )
 
999
            {
 
1000
                if( verbose != 0 )
 
1001
                    printf( "failed\n" );
 
1002
 
 
1003
                return( 1 );
 
1004
            }
 
1005
        }
 
1006
        else
 
1007
        {
 
1008
            len = camellia_test_ctr_len[u];
 
1009
            memcpy( buf, camellia_test_ctr_pt[u], len );
 
1010
 
 
1011
            camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block, buf, buf );
 
1012
 
 
1013
            if( memcmp( buf, camellia_test_ctr_ct[u], len ) != 0 )
 
1014
            {
 
1015
                if( verbose != 0 )
 
1016
                    printf( "failed\n" );
 
1017
 
 
1018
                return( 1 );
 
1019
            }
 
1020
        }
 
1021
 
 
1022
        if( verbose != 0 )
 
1023
            printf( "passed\n" );
 
1024
    }
 
1025
 
 
1026
    if( verbose != 0 )
 
1027
        printf( "\n" );
 
1028
#endif /* POLARSSL_CIPHER_MODE_CTR */
 
1029
 
862
1030
    return ( 0 );
863
1031
}
864
1032