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