1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #ifndef _RSA_IMPL_H 28*0Sstevel@tonic-gate #define _RSA_IMPL_H 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate #ifdef __cplusplus 33*0Sstevel@tonic-gate extern "C" { 34*0Sstevel@tonic-gate #endif 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate #include <sys/types.h> 37*0Sstevel@tonic-gate #include <bignum.h> 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate #define MIN_RSA_KEYLENGTH_IN_BYTES 32 40*0Sstevel@tonic-gate #define MAX_RSA_KEYLENGTH_IN_BYTES 512 41*0Sstevel@tonic-gate #define RSA_MIN_KEY_LEN 256 /* RSA min key length in bits */ 42*0Sstevel@tonic-gate #define RSA_MAX_KEY_LEN 4096 /* RSA max key length in bits */ 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate #define MIN_PKCS1_PADLEN 11 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate #ifdef _KERNEL 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate #include <sys/sunddi.h> 49*0Sstevel@tonic-gate #include <sys/crypto/common.h> 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate #define CK_BYTE uchar_t 52*0Sstevel@tonic-gate #define CK_ULONG ulong_t 53*0Sstevel@tonic-gate #define CK_RV int 54*0Sstevel@tonic-gate #define CKR_OK CRYPTO_SUCCESS 55*0Sstevel@tonic-gate #define CKR_HOST_MEMORY CRYPTO_HOST_MEMORY 56*0Sstevel@tonic-gate #define CKR_DATA_LEN_RANGE CRYPTO_DATA_LEN_RANGE 57*0Sstevel@tonic-gate #define CKR_ENCRYPTED_DATA_INVALID CRYPTO_ENCRYPTED_DATA_INVALID 58*0Sstevel@tonic-gate #define CKR_SIGNATURE_INVALID CRYPTO_SIGNATURE_INVALID 59*0Sstevel@tonic-gate #define CKR_FUNCTION_FAILED CRYPTO_NOT_SUPPORTED 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #else 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate #include <security/cryptoki.h> 64*0Sstevel@tonic-gate #include <security/pkcs11t.h> 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate #endif /* _KERNEL */ 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate #define MD5_DER_PREFIX_Len 18 69*0Sstevel@tonic-gate #define SHA1_DER_PREFIX_Len 15 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate extern const CK_BYTE MD5_DER_PREFIX[MD5_DER_PREFIX_Len]; 72*0Sstevel@tonic-gate extern const CK_BYTE SHA1_DER_PREFIX[SHA1_DER_PREFIX_Len]; 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate typedef struct { 75*0Sstevel@tonic-gate int size; /* key size in bits */ 76*0Sstevel@tonic-gate BIGNUM p; /* p */ 77*0Sstevel@tonic-gate BIGNUM q; /* q */ 78*0Sstevel@tonic-gate BIGNUM n; /* n = p * q (the modulus) */ 79*0Sstevel@tonic-gate BIGNUM d; /* private exponent */ 80*0Sstevel@tonic-gate BIGNUM e; /* public exponent */ 81*0Sstevel@tonic-gate BIGNUM dmodpminus1; /* d mod (p - 1) */ 82*0Sstevel@tonic-gate BIGNUM dmodqminus1; /* d mod (q - 1) */ 83*0Sstevel@tonic-gate BIGNUM pinvmodq; /* p^(-1) mod q */ 84*0Sstevel@tonic-gate BIGNUM p_rr; /* 2^(2*(32*p->len)) mod p */ 85*0Sstevel@tonic-gate BIGNUM q_rr; /* 2^(2*(32*q->len)) mod q */ 86*0Sstevel@tonic-gate BIGNUM n_rr; /* 2^(2*(32*n->len)) mod n */ 87*0Sstevel@tonic-gate } RSAkey; 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate BIG_ERR_CODE RSA_key_init(RSAkey *key, int psize, int qsize); 91*0Sstevel@tonic-gate void RSA_key_finish(RSAkey *key); 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate CK_RV soft_encrypt_rsa_pkcs_encode(uint8_t *databuf, 94*0Sstevel@tonic-gate size_t datalen, uint8_t *padbuf, size_t padbuflen); 95*0Sstevel@tonic-gate CK_RV soft_decrypt_rsa_pkcs_decode(uint8_t *padbuf, int *plen); 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate CK_RV soft_sign_rsa_pkcs_encode(uint8_t *pData, size_t dataLen, 98*0Sstevel@tonic-gate uint8_t *data, size_t mbit_l); 99*0Sstevel@tonic-gate CK_RV soft_verify_rsa_pkcs_decode(uint8_t *data, int *mbit_l); 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate #ifdef _KERNEL 102*0Sstevel@tonic-gate int knzero_random_generator(uint8_t *ran_out, size_t ran_len); 103*0Sstevel@tonic-gate void kmemset(uint8_t *buf, char pattern, size_t len); 104*0Sstevel@tonic-gate #endif 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate #ifdef __cplusplus 107*0Sstevel@tonic-gate } 108*0Sstevel@tonic-gate #endif 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate #endif /* _RSA_IMPL_H */ 111