components/krb5/Solaris/ucrypto/enc_provider/rc4.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
/*
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    60
 * The loopback field is NULL if uctx is uninitialized (no encrypt or decrypt
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    61
 * operation has taken place), or is a pointer to the structure address if uctx
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    62
 * is initialized.  If an application copies the state (not a valid operation,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    63
 * but one which happens to works with some other enc providers), we can detect
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    64
 * it via the loopback field and return a sane error code.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    65
 */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    66
struct arcfour_state {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    67
    struct arcfour_state *loopback;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    68
    int op_type; /* encrypt, decrypt */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    69
    crypto_ctx_t uctx; /* ucrypto context */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    70
};
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    71
#define ENCRYPT_OP 1
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    72
#define DECRYPT_OP 2
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    73
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    74
#define RC4_KEY_SIZE 16
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    75
#define RC4_BLOCK_SIZE 1
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    76
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    77
static krb5_error_code
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    78
k5_arcfour_docrypt(krb5_key key, const krb5_data *state, krb5_crypto_iov *data,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    79
                   size_t num_data, int op_type)
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
    int ret = 0;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    82
    crypto_ctx_t local_uctx, *uctx; /* ucrypto context */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    83
    size_t len, i;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    84
    struct arcfour_state *arcstate;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    85
    krb5_boolean do_init = TRUE;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    86
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    87
    arcstate = (state != NULL) ? (struct arcfour_state *)state->data : NULL;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    88
    if (arcstate != NULL) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    89
        /*
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    90
         * If loopback points to arcstate we know that uctx has been init'ed
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    91
         * but if it doesn't and it isn't NULL then arcstate has been corrupted
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    92
         * which means we can't trust that uctx is valid.
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    93
         */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    94
        if (arcstate->loopback == arcstate)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    95
            do_init = FALSE;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    96
        else if (arcstate->loopback != NULL)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    97
            return KRB5_CRYPTO_INTERNAL;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    98
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
    99
        uctx = &arcstate->uctx;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   100
    } else {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   101
        /* Use the local ucrypto context, will need to be cleaned up in this
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   102
         * function. */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   103
        uctx = &local_uctx;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   104
    }
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
    if (do_init) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   107
        if (op_type == ENCRYPT_OP) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   108
            if (ucrypto_encrypt_init(uctx, CRYPTO_RC4,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   109
                                     key->keyblock.contents,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   110
                                     key->keyblock.length,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   111
                                     NULL, 0) != CRYPTO_SUCCESS) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   112
                return KRB5_CRYPTO_INTERNAL;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   113
            }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   114
        } else {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   115
            if (ucrypto_decrypt_init(uctx, CRYPTO_RC4,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   116
                                     key->keyblock.contents,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   117
                                     key->keyblock.length,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   118
                                     NULL, 0) != CRYPTO_SUCCESS) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   119
                return KRB5_CRYPTO_INTERNAL;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   120
            }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   121
        }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   122
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   123
        /* Saved ucrypto context is init'ed, mark as initialized. */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   124
        if (arcstate) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   125
            arcstate->loopback = arcstate;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   126
            arcstate->op_type = op_type;
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
    }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   129
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   130
    for (i = 0; i < num_data; i++) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   131
        krb5_crypto_iov *iov = &data[i];
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   132
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   133
        if (ENCRYPT_IOV(iov)) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   134
            len = (size_t)iov->data.length;
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
            if (op_type == ENCRYPT_OP) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   137
                if (ucrypto_encrypt_update(uctx,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   138
                                           (const uchar_t *)iov->data.data,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   139
                                           len,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   140
                                           (uchar_t *)iov->data.data,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   141
                                           &len) != CRYPTO_SUCCESS) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   142
                    ret = KRB5_CRYPTO_INTERNAL;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   143
                    break;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   144
                }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   145
            } else {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   146
                if (ucrypto_decrypt_update(uctx,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   147
                                           (const uchar_t *)iov->data.data,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   148
                                           len,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   149
                                           (uchar_t *)iov->data.data,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   150
                                           &len) != CRYPTO_SUCCESS) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   151
                    ret = KRB5_CRYPTO_INTERNAL;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   152
                    break;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   153
                }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   154
            }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   155
        }
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
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   158
    if (!arcstate) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   159
        /* Local ucrypto context is not saved, clean it up now. */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   160
        len = 0;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   161
        if (op_type == ENCRYPT_OP)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   162
            (void)ucrypto_encrypt_final(uctx, NULL, &len);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   163
        else
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   164
            (void)ucrypto_decrypt_final(uctx, NULL, &len);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   165
    }
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
    return ret;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   168
}
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   169
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   170
static krb5_error_code
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   171
k5_arcfour_encrypt(krb5_key key, const krb5_data *state, krb5_crypto_iov *data,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   172
                   size_t num_data)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   173
{
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   174
    return k5_arcfour_docrypt(key, state, data, num_data, ENCRYPT_OP);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   175
}
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   176
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   177
static krb5_error_code
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   178
k5_arcfour_decrypt(krb5_key key, const krb5_data *state, krb5_crypto_iov *data,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   179
                   size_t num_data)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   180
{
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   181
    return k5_arcfour_docrypt(key, state, data, num_data, DECRYPT_OP);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   182
}
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   183
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   184
static krb5_error_code
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   185
krb5int_arcfour_init_state(const krb5_keyblock *key, krb5_keyusage usage,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   186
                           krb5_data *state)
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
    struct arcfour_state *arcstate;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   189
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   190
    arcstate = calloc(1, sizeof (*arcstate));
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   191
    if (arcstate == NULL)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   192
        return ENOMEM;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   193
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   194
    state->data = (char *)arcstate;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   195
    state->length = sizeof (*arcstate);
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
    return 0;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   198
}
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   199
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   200
static void
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   201
krb5int_arcfour_free_state(krb5_data *state)
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
    struct arcfour_state *arcstate = (struct arcfour_state *)state->data;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   204
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   205
    /* If arcstate->loopback == arcstate then the ucrypto context was init'ed
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   206
     * and uctx clean up is required. */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   207
    if (arcstate && arcstate->loopback == arcstate) {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   208
        size_t len = 0;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   209
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   210
        if (arcstate->op_type == ENCRYPT_OP)
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   211
            (void) ucrypto_encrypt_final(&arcstate->uctx, NULL, &len);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   212
        else
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   213
            (void) ucrypto_decrypt_final(&arcstate->uctx, NULL, &len);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   214
    }
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   215
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   216
    free(arcstate);
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   217
    state->data = NULL;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   218
    state->length = 0;
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
    return;
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   221
}
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   222
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   223
const struct krb5_enc_provider krb5int_enc_arcfour = {
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   224
    /* This seems to work... although I am not sure what the
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   225
       implications are in other places in the kerberos library */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   226
    RC4_BLOCK_SIZE,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   227
    /* Keysize is arbitrary in arcfour, but the constraints of the
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   228
       system, and to attempt to work with the MSFT system forces us
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   229
       to 16byte/128bit.  Since there is no parity in the key, the
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   230
       byte and length are the same.  */
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   231
    RC4_KEY_SIZE, RC4_KEY_SIZE,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   232
    k5_arcfour_encrypt,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   233
    k5_arcfour_decrypt,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   234
    NULL,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   235
    krb5int_arcfour_init_state,
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   236
    krb5int_arcfour_free_state
50d75ee82dad 25114734 Userland krb should use libucrypto
Will Fiveash <will.fiveash@oracle.com>
parents:
diff changeset
   237
};