components/krb5/Solaris/ucrypto/enc_provider/des.c
author Will Fiveash <will.fiveash@oracle.com>
Fri, 24 Mar 2017 12:29:03 -0500
changeset 7950 50d75ee82dad
permissions -rw-r--r--
25114734 Userland krb should use libucrypto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7950
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
     1
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
     2
/* lib/crypto/openssl/enc_provider/des.c */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
     3
/*
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
     4
 * Copyright (C) 2009 by the Massachusetts Institute of Technology.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
     5
 * All rights reserved.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
     6
 *
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
     7
 * Export of this software from the United States of America may
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
     8
 *   require a specific license from the United States Government.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
     9
 *   It is the responsibility of any person or organization contemplating
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    10
 *   export to obtain such a license before exporting.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    11
 *
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    12
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    13
 * distribute this software and its documentation for any purpose and
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    14
 * without fee is hereby granted, provided that the above copyright
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    15
 * notice appear in all copies and that both that copyright notice and
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    16
 * this permission notice appear in supporting documentation, and that
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    17
 * the name of M.I.T. not be used in advertising or publicity pertaining
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    18
 * to distribution of the software without specific, written prior
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    19
 * permission.  Furthermore if you modify this software you must label
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    20
 * your software as modified software and not distribute it in such a
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    21
 * fashion that it might be confused with the original M.I.T. software.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    22
 * M.I.T. makes no representations about the suitability of
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    23
 * this software for any purpose.  It is provided "as is" without express
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    24
 * or implied warranty.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    25
 */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    26
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    27
/*
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    28
 * Copyright (C) 1998 by the FundsXpress, INC.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    29
 *
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    30
 * All rights reserved.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    31
 *
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    32
 * Export of this software from the United States of America may require
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    33
 * a specific license from the United States Government.  It is the
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    34
 * responsibility of any person or organization contemplating export to
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    35
 * obtain such a license before exporting.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    36
 *
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    37
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    38
 * distribute this software and its documentation for any purpose and
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    39
 * without fee is hereby granted, provided that the above copyright
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    40
 * notice appear in all copies and that both that copyright notice and
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    41
 * this permission notice appear in supporting documentation, and that
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    42
 * the name of FundsXpress. not be used in advertising or publicity pertaining
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    43
 * to distribution of the software without specific, written prior
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    44
 * permission.  FundsXpress makes no representations about the suitability of
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    45
 * this software for any purpose.  It is provided "as is" without express
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    46
 * or implied warranty.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    47
 *
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    48
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    49
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    50
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    51
 */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    52
/*
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    53
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    54
 */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    55
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    56
#include "crypto_int.h"
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    57
#include <libucrypto.h>
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    58
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    59
#define DES_BLOCK_SIZE 8
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    60
#define DES_KEY_SIZE 8
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    61
#define DES_KEY_BYTES 7
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    62
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    63
static krb5_error_code
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    64
validate(krb5_key key, const krb5_data *ivec, const krb5_crypto_iov *data,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    65
         size_t num_data, size_t *inlen)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    66
{
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    67
    size_t input_length = iov_total_length(data, num_data, FALSE);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    68
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    69
    if (key->keyblock.length != DES_KEY_SIZE)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    70
        return KRB5_BAD_KEYSIZE;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    71
    if ((input_length % DES_BLOCK_SIZE) != 0)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    72
        return KRB5_BAD_MSIZE;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    73
    if (ivec && (ivec->length != DES_BLOCK_SIZE))
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    74
        return KRB5_BAD_MSIZE;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    75
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    76
    if (inlen != NULL)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    77
        *inlen = input_length;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    78
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    79
    return 0;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    80
}
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    81
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    82
static krb5_error_code
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    83
k5_des_encrypt(krb5_key key, const krb5_data *ivec, krb5_crypto_iov *data,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    84
               size_t num_data)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    85
{
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    86
    int ret;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    87
    size_t  olen, dlen;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    88
    uchar_t *obuf, *dbuf;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    89
    uchar_t tmp_iv[DES_BLOCK_SIZE];
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    90
    struct iov_cursor cursor;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    91
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    92
    ret = validate(key, ivec, data, num_data, &dlen);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    93
    if (ret != 0 || dlen == 0)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    94
        return ret;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    95
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    96
    if (ivec && ivec->data)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    97
        memcpy(tmp_iv, ivec->data, ivec->length);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    98
    else
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    99
        memset(tmp_iv, 0, sizeof(tmp_iv));
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   100
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   101
    olen = dlen;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   102
    obuf = malloc(olen);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   103
    if (!obuf)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   104
        return ENOMEM;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   105
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   106
    dbuf = malloc(dlen);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   107
    if (!dbuf){
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   108
        free(obuf);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   109
        return ENOMEM;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   110
    }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   111
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   112
    /* Init iov cursor to gather data for encypting (FALSE) */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   113
    k5_iov_cursor_init(&cursor, data, num_data, dlen, FALSE);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   114
    /* Get all that data into dbuf */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   115
    k5_iov_cursor_get(&cursor, dbuf);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   116
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   117
    if (ucrypto_encrypt(CRYPTO_DES_CBC,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   118
                        key->keyblock.contents,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   119
                        key->keyblock.length,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   120
                        tmp_iv, sizeof(tmp_iv),
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   121
                        dbuf, dlen, obuf, &olen) != CRYPTO_SUCCESS) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   122
        ret = KRB5_CRYPTO_INTERNAL;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   123
    } else {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   124
        k5_iov_cursor_put(&cursor, obuf);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   125
    }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   126
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   127
    /*
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   128
     * Updating the ivec arg, if present because of an old/obscure concept of
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   129
     * cipher state that is being used by only BSD rlogin.  See RFCs 1391 and
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   130
     * 1392 in regards to the vague description of cipher state.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   131
     */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   132
    if (!ret && ivec && ivec->data) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   133
        /* Copy last block of cipher text output */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   134
        memcpy(ivec->data, obuf + (dlen - DES_BLOCK_SIZE), DES_BLOCK_SIZE);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   135
    }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   136
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   137
    zapfree(obuf, olen);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   138
    zapfree(dbuf, dlen);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   139
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   140
    return ret;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   141
}
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   142
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   143
static krb5_error_code
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   144
k5_des_decrypt(krb5_key key, const krb5_data *ivec, krb5_crypto_iov *data,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   145
               size_t num_data)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   146
{
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   147
    int ret;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   148
    size_t  olen, dlen;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   149
    uchar_t *obuf, *dbuf;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   150
    uchar_t tmp_iv[DES_BLOCK_SIZE];
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   151
    struct iov_cursor cursor;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   152
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   153
    ret = validate(key, ivec, data, num_data, &dlen);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   154
    if (ret != 0 || dlen == 0)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   155
        return ret;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   156
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   157
    if (ivec && ivec->data)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   158
        memcpy(tmp_iv, ivec->data, ivec->length);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   159
    else
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   160
        memset(tmp_iv, 0, sizeof(tmp_iv));
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   161
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   162
    olen = dlen;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   163
    obuf = malloc(olen);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   164
    if (!obuf)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   165
        return ENOMEM;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   166
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   167
    dbuf = malloc(dlen);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   168
    if (!dbuf){
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   169
        free(obuf);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   170
        return ENOMEM;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   171
    }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   172
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   173
    /* Init iov cursor to gather cipher text for decrypting (FALSE) */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   174
    k5_iov_cursor_init(&cursor, data, num_data, dlen, FALSE);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   175
    /* Gather all that cipher text into dbuf */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   176
    k5_iov_cursor_get(&cursor, dbuf);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   177
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   178
    if (ucrypto_decrypt(CRYPTO_DES_CBC,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   179
                        key->keyblock.contents,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   180
                        key->keyblock.length,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   181
                        tmp_iv, sizeof(tmp_iv),
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   182
                        dbuf, dlen, obuf, &olen) != CRYPTO_SUCCESS) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   183
        ret = KRB5_CRYPTO_INTERNAL;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   184
    } else {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   185
        k5_iov_cursor_put(&cursor, obuf);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   186
    }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   187
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   188
    /*
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   189
     * Updating the ivec arg, if present because of an old/obscure concept of
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   190
     * cipher state that is being used by only BSD rlogin.  See RFCs 1391 and
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   191
     * 1392 in regards to the vague description of cipher state.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   192
     */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   193
    if (!ret && ivec && ivec->data) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   194
        /* Copy last block of cipher text input */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   195
        memcpy(ivec->data, dbuf + (dlen - DES_BLOCK_SIZE), DES_BLOCK_SIZE);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   196
    }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   197
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   198
    zapfree(obuf, olen);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   199
    zapfree(dbuf, dlen);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   200
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   201
    return ret;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   202
}
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   203
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   204
static krb5_error_code
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   205
k5_des_cbc_mac(krb5_key key, const krb5_crypto_iov *data, size_t num_data,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   206
               const krb5_data *ivec, krb5_data *output)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   207
{
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   208
    int ret;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   209
    struct iov_cursor cursor;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   210
    uchar_t blockY[DES_BLOCK_SIZE], blockB[DES_BLOCK_SIZE];
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   211
    size_t olen;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   212
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   213
    ret = validate(key, ivec, data, num_data, NULL);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   214
    if (ret != 0)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   215
        return ret;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   216
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   217
    if (output->length != DES_BLOCK_SIZE)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   218
        return KRB5_BAD_MSIZE;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   219
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   220
    if (ivec != NULL)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   221
        memcpy(blockY, ivec->data, DES_BLOCK_SIZE);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   222
    else
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   223
        memset(blockY, 0, DES_BLOCK_SIZE);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   224
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   225
    k5_iov_cursor_init(&cursor, data, num_data, DES_BLOCK_SIZE, FALSE);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   226
    while (k5_iov_cursor_get(&cursor, blockB)) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   227
        store_64_n(load_64_n(blockB) ^ load_64_n(blockY), blockB);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   228
        olen = sizeof(blockY);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   229
        if (ucrypto_encrypt(CRYPTO_DES_ECB,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   230
                            key->keyblock.contents,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   231
                            key->keyblock.length,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   232
                            NULL, 0,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   233
                            blockB, sizeof(blockB),
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   234
                            blockY, &olen) != CRYPTO_SUCCESS) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   235
            return KRB5_CRYPTO_INTERNAL;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   236
        }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   237
    }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   238
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   239
    memcpy(output->data, blockY, DES_BLOCK_SIZE);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   240
    return 0;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   241
}
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   242
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   243
const struct krb5_enc_provider krb5int_enc_des = {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   244
    DES_BLOCK_SIZE,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   245
    DES_KEY_BYTES, DES_KEY_SIZE,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   246
    k5_des_encrypt,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   247
    k5_des_decrypt,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   248
    k5_des_cbc_mac,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   249
    krb5int_des_init_state,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   250
    krb5int_default_free_state
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   251
};