17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 520d58091SDina K Nimeh * Common Development and Distribution License (the "License"). 620d58091SDina K Nimeh * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*726fad2aSDina K Nimeh 227c478bd9Sstevel@tonic-gate /* 23*726fad2aSDina K Nimeh * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #ifndef _RSA_IMPL_H 277c478bd9Sstevel@tonic-gate #define _RSA_IMPL_H 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #ifdef __cplusplus 307c478bd9Sstevel@tonic-gate extern "C" { 317c478bd9Sstevel@tonic-gate #endif 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <sys/types.h> 347c478bd9Sstevel@tonic-gate #include <bignum.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #define MIN_RSA_KEYLENGTH_IN_BYTES 32 377c478bd9Sstevel@tonic-gate #define MAX_RSA_KEYLENGTH_IN_BYTES 512 387c478bd9Sstevel@tonic-gate #define RSA_MIN_KEY_LEN 256 /* RSA min key length in bits */ 397c478bd9Sstevel@tonic-gate #define RSA_MAX_KEY_LEN 4096 /* RSA max key length in bits */ 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #ifdef _KERNEL 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 447c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h> 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #define CK_BYTE uchar_t 477c478bd9Sstevel@tonic-gate #define CK_ULONG ulong_t 487c478bd9Sstevel@tonic-gate #define CK_RV int 49*726fad2aSDina K Nimeh 507c478bd9Sstevel@tonic-gate #define CKR_OK CRYPTO_SUCCESS 51*726fad2aSDina K Nimeh #define CKR_ARGUMENTS_BAD CRYPTO_ARGUMENTS_BAD 527c478bd9Sstevel@tonic-gate #define CKR_DATA_LEN_RANGE CRYPTO_DATA_LEN_RANGE 53*726fad2aSDina K Nimeh #define CKR_DEVICE_ERROR CRYPTO_DEVICE_ERROR 54*726fad2aSDina K Nimeh #define CKR_GENERAL_ERROR CRYPTO_GENERAL_ERROR 55*726fad2aSDina K Nimeh #define CKR_HOST_MEMORY CRYPTO_HOST_MEMORY 56*726fad2aSDina K Nimeh #define CKR_KEY_SIZE_RANGE CRYPTO_KEY_SIZE_RANGE 57*726fad2aSDina K Nimeh 58*726fad2aSDina K Nimeh int random_get_bytes(uint8_t *ran_out, size_t ran_len); 59*726fad2aSDina K Nimeh int random_get_pseudo_bytes(uint8_t *ran_out, size_t ran_len); 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #else 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate #include <security/cryptoki.h> 647c478bd9Sstevel@tonic-gate #include <security/pkcs11t.h> 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate #define MD5_DER_PREFIX_Len 18 697c478bd9Sstevel@tonic-gate #define SHA1_DER_PREFIX_Len 15 7060722cc8Sizick #define SHA1_DER_PREFIX_OID_Len 13 71f66d273dSizick #define SHA2_DER_PREFIX_Len 19 7220d58091SDina K Nimeh #define DEFAULT_PUB_EXPO_Len 3 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate extern const CK_BYTE MD5_DER_PREFIX[MD5_DER_PREFIX_Len]; 757c478bd9Sstevel@tonic-gate extern const CK_BYTE SHA1_DER_PREFIX[SHA1_DER_PREFIX_Len]; 7660722cc8Sizick extern const CK_BYTE SHA1_DER_PREFIX_OID[SHA1_DER_PREFIX_OID_Len]; 77f66d273dSizick extern const CK_BYTE SHA256_DER_PREFIX[SHA2_DER_PREFIX_Len]; 78f66d273dSizick extern const CK_BYTE SHA384_DER_PREFIX[SHA2_DER_PREFIX_Len]; 79f66d273dSizick extern const CK_BYTE SHA512_DER_PREFIX[SHA2_DER_PREFIX_Len]; 8020d58091SDina K Nimeh extern const CK_BYTE DEFAULT_PUB_EXPO[DEFAULT_PUB_EXPO_Len]; 817c478bd9Sstevel@tonic-gate 82*726fad2aSDina K Nimeh 83*726fad2aSDina K Nimeh /* RSA key using BIGNUM representations */ 847c478bd9Sstevel@tonic-gate typedef struct { 857c478bd9Sstevel@tonic-gate int size; /* key size in bits */ 867c478bd9Sstevel@tonic-gate BIGNUM p; /* p */ 877c478bd9Sstevel@tonic-gate BIGNUM q; /* q */ 887c478bd9Sstevel@tonic-gate BIGNUM n; /* n = p * q (the modulus) */ 897c478bd9Sstevel@tonic-gate BIGNUM d; /* private exponent */ 907c478bd9Sstevel@tonic-gate BIGNUM e; /* public exponent */ 91*726fad2aSDina K Nimeh BIGNUM dmodpminus1; /* d mod (p - 1) (exponent 1) */ 92*726fad2aSDina K Nimeh BIGNUM dmodqminus1; /* d mod (q - 1) (exponent 2) */ 93*726fad2aSDina K Nimeh BIGNUM pinvmodq; /* p^(-1) mod q (the coefficient) */ 947c478bd9Sstevel@tonic-gate BIGNUM p_rr; /* 2^(2*(32*p->len)) mod p */ 957c478bd9Sstevel@tonic-gate BIGNUM q_rr; /* 2^(2*(32*q->len)) mod q */ 967c478bd9Sstevel@tonic-gate BIGNUM n_rr; /* 2^(2*(32*n->len)) mod n */ 977c478bd9Sstevel@tonic-gate } RSAkey; 987c478bd9Sstevel@tonic-gate 99*726fad2aSDina K Nimeh /* RSA key using byte string representations, useful for parameter lists */ 100*726fad2aSDina K Nimeh typedef struct { 101*726fad2aSDina K Nimeh uint32_t modulus_bits; /* size */ 102*726fad2aSDina K Nimeh uchar_t *modulus; /* n */ 103*726fad2aSDina K Nimeh uint32_t privexpo_bytes; 104*726fad2aSDina K Nimeh uchar_t *privexpo; /* d */ 105*726fad2aSDina K Nimeh uint32_t pubexpo_bytes; 106*726fad2aSDina K Nimeh uchar_t *pubexpo; /* e */ 107*726fad2aSDina K Nimeh uint32_t prime1_bytes; 108*726fad2aSDina K Nimeh uchar_t *prime1; /* p */ 109*726fad2aSDina K Nimeh uint32_t prime2_bytes; 110*726fad2aSDina K Nimeh uchar_t *prime2; /* q */ 111*726fad2aSDina K Nimeh uint32_t expo1_bytes; 112*726fad2aSDina K Nimeh uchar_t *expo1; /* = d mod (p - 1) */ 113*726fad2aSDina K Nimeh uint32_t expo2_bytes; 114*726fad2aSDina K Nimeh uchar_t *expo2; /* = d mod (q - 1) */ 115*726fad2aSDina K Nimeh uint32_t coeff_bytes; /* = q bytes, .... or = p bytes */ 116*726fad2aSDina K Nimeh uchar_t *coeff; /* = p^(-1) mod q, or = q^(-1) mod p */ 117*726fad2aSDina K Nimeh int (*rfunc)(void *, size_t); /* random function */ 118*726fad2aSDina K Nimeh } RSAbytekey; 119*726fad2aSDina K Nimeh 120*726fad2aSDina K Nimeh 121*726fad2aSDina K Nimeh CK_RV rsa_genkey_pair(RSAbytekey *bkey); 122*726fad2aSDina K Nimeh 123*726fad2aSDina K Nimeh CK_RV rsa_encrypt(RSAbytekey *bkey, 124*726fad2aSDina K Nimeh uchar_t *msg, uint32_t msglen, uchar_t *encrmsg); 125*726fad2aSDina K Nimeh 126*726fad2aSDina K Nimeh CK_RV rsa_decrypt(RSAbytekey *bkey, 127*726fad2aSDina K Nimeh uchar_t *encrmsg, uint32_t encrmsglen, uchar_t *msg); 128*726fad2aSDina K Nimeh 129*726fad2aSDina K Nimeh #define rsa_sign(key, msg, len, sig) rsa_decrypt((key), (msg), (len), (sig)) 130*726fad2aSDina K Nimeh #define rsa_verify(key, msg, len, sig) rsa_encrypt((key), (msg), (len), (sig)) 1317c478bd9Sstevel@tonic-gate 132b5a2d845SHai-May Chao /* 133b5a2d845SHai-May Chao * The following definitions and declarations are only used by RSA FIPS POST 134b5a2d845SHai-May Chao */ 135b5a2d845SHai-May Chao #ifdef _RSA_FIPS_POST 136b5a2d845SHai-May Chao 137b5a2d845SHai-May Chao /* RSA FIPS Declarations */ 138b5a2d845SHai-May Chao #define FIPS_RSA_PUBLIC_EXPONENT_LENGTH 3 /* 24-bits */ 139b5a2d845SHai-May Chao #define FIPS_RSA_PRIVATE_VERSION_LENGTH 1 /* 8-bits */ 140b5a2d845SHai-May Chao #define FIPS_RSA_MESSAGE_LENGTH 128 /* 1024-bits */ 141b5a2d845SHai-May Chao #define FIPS_RSA_COEFFICIENT_LENGTH 64 /* 512-bits */ 142b5a2d845SHai-May Chao #define FIPS_RSA_PRIME0_LENGTH 64 /* 512-bits */ 143b5a2d845SHai-May Chao #define FIPS_RSA_PRIME1_LENGTH 64 /* 512-bits */ 144b5a2d845SHai-May Chao #define FIPS_RSA_EXPONENT0_LENGTH 64 /* 512-bits */ 145b5a2d845SHai-May Chao #define FIPS_RSA_EXPONENT1_LENGTH 64 /* 512-bits */ 146b5a2d845SHai-May Chao #define FIPS_RSA_PRIVATE_EXPONENT_LENGTH 128 /* 1024-bits */ 147b5a2d845SHai-May Chao #define FIPS_RSA_ENCRYPT_LENGTH 128 /* 1024-bits */ 148b5a2d845SHai-May Chao #define FIPS_RSA_DECRYPT_LENGTH 128 /* 1024-bits */ 149b5a2d845SHai-May Chao #define FIPS_RSA_SIGNATURE_LENGTH 128 /* 1024-bits */ 150b5a2d845SHai-May Chao #define FIPS_RSA_MODULUS_LENGTH 128 /* 1024-bits */ 151b5a2d845SHai-May Chao #define MAX_KEY_ATTR_BUFLEN 1024 152b5a2d845SHai-May Chao 153b5a2d845SHai-May Chao typedef struct RSAPrivateKey_s { 154b5a2d845SHai-May Chao uint8_t *version; 155b5a2d845SHai-May Chao int version_len; 156*726fad2aSDina K Nimeh RSAbytekey bkey; 157b5a2d845SHai-May Chao } RSAPrivateKey_t; 158b5a2d845SHai-May Chao 159b5a2d845SHai-May Chao /* RSA FIPS functions */ 160b5a2d845SHai-May Chao extern int fips_rsa_post(void); 161*726fad2aSDina K Nimeh extern int fips_rsa_encrypt(RSAPrivateKey_t *, uint8_t *, int, uint8_t *); 162*726fad2aSDina K Nimeh extern int fips_rsa_decrypt(RSAPrivateKey_t *, uint8_t *, int, uint8_t *); 163b5a2d845SHai-May Chao 164b5a2d845SHai-May Chao #endif /* _RSA_FIPS_POST */ 165b5a2d845SHai-May Chao 1667c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate #endif 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate #endif /* _RSA_IMPL_H */ 171