~ubuntu-branches/ubuntu/natty/libgcrypt11/natty-proposed

« back to all changes in this revision

Viewing changes to cipher/des.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2009-02-21 13:46:58 UTC
  • mto: (1.1.6 upstream) (2.1.3 squeeze)
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20090221134658-855twvcr4ezk2ron
ImportĀ upstreamĀ versionĀ 1.4.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* des.c - DES and Triple-DES encryption/decryption Algorithm
2
 
 * Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
 
2
 * Copyright (C) 1998, 1999, 2001, 2002, 2003,
 
3
 *               2008  Free Software Foundation, Inc.
3
4
 *
4
5
 * This file is part of Libgcrypt.
5
6
 *
100
101
 *     char *error_msg;
101
102
 *
102
103
 *     * To perform a selftest of this DES/Triple-DES implementation use the
103
 
 *       function selftest(). It will return an error string if their are
 
104
 *       function selftest(). It will return an error string if there are
104
105
 *       some problems with this library. *
105
106
 *
106
107
 *     if ( (error_msg = selftest()) )
152
153
  {
153
154
    u32 encrypt_subkeys[96];
154
155
    u32 decrypt_subkeys[96];
 
156
    struct {
 
157
      int no_weak_key;
 
158
    } flags;
155
159
  }
156
160
tripledes_ctx[1];
157
161
 
310
314
 
311
315
/*
312
316
 * Table with weak DES keys sorted in ascending order.
313
 
 * In DES their are 64 known keys wich are weak. They are weak
 
317
 * In DES their are 64 known keys which are weak. They are weak
314
318
 * because they produce only one, two or four different
315
319
 * subkeys in the subkey scheduling process.
316
320
 * The keys in this table have all their parity bits cleared.
584
588
  static const char *selftest_failed;
585
589
  int i;
586
590
 
587
 
  if (! initialized)
 
591
  if (!fips_mode () && !initialized)
588
592
    {
589
593
      initialized = 1;
590
594
      selftest_failed = selftest ();
691
695
  static const char *selftest_failed;
692
696
  int i;
693
697
 
694
 
  if (! initialized)
 
698
  if (!fips_mode () && !initialized)
695
699
    {
696
700
      initialized = 1;
697
701
      selftest_failed = selftest ();
956
960
 
957
961
    byte                result[8];
958
962
    int         i;
959
 
    static char error[80];
960
963
    tripledes_ctx       des3;
961
964
 
962
965
    for (i=0; i<sizeof(testdata)/sizeof(*testdata); ++i)
966
969
        
967
970
        tripledes_ecb_encrypt (des3, testdata[i].plain, result);
968
971
        if (memcmp (testdata[i].cipher, result, 8))
969
 
          {
970
 
            sprintf (error, "Triple-DES SSLeay test pattern no. %d "
971
 
                     "failed on encryption.", i+1);
972
 
            return error;
973
 
          }
 
972
          return "Triple-DES SSLeay test failed on encryption.";
974
973
 
975
974
        tripledes_ecb_decrypt (des3, testdata[i].cipher, result);
976
975
        if (memcmp (testdata[i].plain, result, 8))
977
 
          {
978
 
            sprintf (error, "Triple-DES SSLeay test pattern no. %d "
979
 
                     "failed on decryption.", i+1);
980
 
            return error;
981
 
          }
 
976
          return  "Triple-DES SSLeay test failed on decryption.";;
982
977
      }
983
978
  }
984
979
 
992
987
    unsigned char *p;
993
988
    gcry_md_hd_t h;
994
989
 
995
 
    if (gcry_md_open (&h, GCRY_MD_SHA1, 0))
 
990
    if (_gcry_md_open (&h, GCRY_MD_SHA1, 0))
996
991
      return "SHA1 not available";
997
992
 
998
993
    for (i = 0; i < 64; ++i)
999
 
      gcry_md_write (h, weak_keys[i], 8);
1000
 
    p = gcry_md_read (h, GCRY_MD_SHA1);
 
994
      _gcry_md_write (h, weak_keys[i], 8);
 
995
    p = _gcry_md_read (h, GCRY_MD_SHA1);
1001
996
    i = memcmp (p, weak_keys_chksum, 20);
1002
 
    gcry_md_close (h);
 
997
    _gcry_md_close (h);
1003
998
    if (i)
1004
999
      return "weak key table defect";
1005
1000
 
1022
1017
 
1023
1018
  tripledes_set3keys ( ctx, key, key+8, key+16);
1024
1019
 
1025
 
  if( is_weak_key( key ) || is_weak_key( key+8 ) || is_weak_key( key+16 ) )
 
1020
  if (ctx->flags.no_weak_key)
 
1021
    ; /* Detection has been disabled.  */
 
1022
  else if (is_weak_key (key) || is_weak_key (key+8) || is_weak_key (key+16))
1026
1023
    {
1027
1024
      _gcry_burn_stack (64);
1028
1025
      return GPG_ERR_WEAK_KEY;
1033
1030
}
1034
1031
 
1035
1032
 
 
1033
static gcry_err_code_t
 
1034
do_tripledes_set_extra_info (void *context, int what,
 
1035
                             const void *buffer, size_t buflen)
 
1036
{
 
1037
  struct _tripledes_ctx *ctx = (struct _tripledes_ctx *)context;
 
1038
  gpg_err_code_t ec = 0;
 
1039
 
 
1040
  (void)buffer;
 
1041
  (void)buflen;
 
1042
 
 
1043
  switch (what)
 
1044
    {
 
1045
    case CIPHER_INFO_NO_WEAK_KEY:
 
1046
      ctx->flags.no_weak_key = 1;
 
1047
      break;
 
1048
 
 
1049
    default:
 
1050
      ec = GPG_ERR_INV_OP; 
 
1051
      break;
 
1052
    }
 
1053
  return ec;
 
1054
}
 
1055
 
 
1056
 
1036
1057
static void
1037
1058
do_tripledes_encrypt( void *context, byte *outbuf, const byte *inbuf )
1038
1059
{
1088
1109
  _gcry_burn_stack (32);
1089
1110
}
1090
1111
 
 
1112
 
 
1113
 
 
1114
 
 
1115
/* 
 
1116
     Self-test section.
 
1117
 */
 
1118
 
 
1119
 
 
1120
/* Selftest for TripleDES.  */
 
1121
static gpg_err_code_t
 
1122
selftest_fips (int extended, selftest_report_func_t report)
 
1123
{
 
1124
  const char *what;
 
1125
  const char *errtxt;
 
1126
  
 
1127
  (void)extended; /* No extended tests available.  */
 
1128
 
 
1129
  what = "low-level";
 
1130
  errtxt = selftest ();
 
1131
  if (errtxt)
 
1132
    goto failed;
 
1133
 
 
1134
  /* The low-level self-tests are quite extensive and thus we can do
 
1135
     without high level tests.  This is also justified because we have
 
1136
     no custom block code implementation for 3des but always use the
 
1137
     standard high level block code.  */
 
1138
 
 
1139
  return 0; /* Succeeded. */
 
1140
 
 
1141
 failed:
 
1142
  if (report)
 
1143
    report ("cipher", GCRY_CIPHER_3DES, what, errtxt);
 
1144
  return GPG_ERR_SELFTEST_FAILED;
 
1145
}
 
1146
 
 
1147
 
 
1148
 
 
1149
/* Run a full self-test for ALGO and return 0 on success.  */
 
1150
static gpg_err_code_t
 
1151
run_selftests (int algo, int extended, selftest_report_func_t report)
 
1152
{
 
1153
  gpg_err_code_t ec;
 
1154
 
 
1155
  switch (algo)
 
1156
    {
 
1157
    case GCRY_CIPHER_3DES:
 
1158
      ec = selftest_fips (extended, report);
 
1159
      break;
 
1160
    default:
 
1161
      ec = GPG_ERR_CIPHER_ALGO;
 
1162
      break;
 
1163
        
 
1164
    }
 
1165
  return ec;
 
1166
}
 
1167
 
 
1168
 
 
1169
 
1091
1170
gcry_cipher_spec_t _gcry_cipher_spec_des =
1092
1171
  {
1093
1172
    "DES", NULL, NULL, 8, 64, sizeof (struct _des_ctx),
1109
1188
    "3DES", NULL, oids_tripledes, 8, 192, sizeof (struct _tripledes_ctx),
1110
1189
    do_tripledes_setkey, do_tripledes_encrypt, do_tripledes_decrypt
1111
1190
  };
 
1191
 
 
1192
cipher_extra_spec_t _gcry_cipher_extraspec_tripledes = 
 
1193
  {
 
1194
    run_selftests,
 
1195
    do_tripledes_set_extra_info
 
1196
  };