blob: f1496de096de3fbf14aa472852c463552b918328 [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001/*****************************************************************************
2* Copyright Statement:
3* --------------------
4* This software is protected by Copyright and the information contained
5* herein is confidential. The software may not be copied and the information
6* contained herein may not be used or disclosed except with the written
7* permission of MediaTek Inc. (C) 2016
8*
9* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
10* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS (""MEDIATEK SOFTWARE"")
11* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
12* AN ""AS-IS"" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
13* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
15* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
16* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
17* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
18* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
19* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
20* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
21*
22* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
23* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
24* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
25* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
26* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
27*
28* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
29* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
30* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
31* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
32* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
33*
34*****************************************************************************/
35/*************************************************************
36*
37* This Software is the property of VIA Telecom, Inc. and may only be used pursuant to a license from VIA Telecom, Inc.
38*
39* Any unauthorized use inconsistent with the terms of such license is strictly prohibited.
40*
41* Copyright (c) 2010 VIA Telecom, Inc. All rights reserved.
42*
43*************************************************************/
44/*****************************************************************************
45*
46* FILE NAME : pswrsa.h
47*
48* DESCRIPTION :
49*
50* This file contains the implementation of RSA encryption based on TomsFastMath.
51*
52* HISTORY :
53* See Log at end of file
54*
55*****************************************************************************/
56/* TomsFastMath, a fast ISO C bignum library.
57 *
58 * This project is meant to fill in where LibTomMath
59 * falls short. That is speed ;-)
60 *
61 * This project is public domain and free for all purposes.
62 *
63 * Tom St Denis, tomstdenis@gmail.com
64 */
65#ifndef PSWRSA_H_
66#define PSWRSA_H_
67
68#include <stdio.h>
69#include <string.h>
70#include <stdlib.h>
71#include <ctype.h>
72#include <limits.h>
73
74#ifndef MIN
75 #define MIN(x,y) (((x)<(y))?(x):(y))
76#endif
77
78#ifndef MAX
79 #define MAX(x,y) (((x)>(y))?(x):(y))
80#endif
81
82/* default configurations for ARM7 besed on test result
83 */
84
85#if (CHAR_BIT & 7)
86 #error CHAR_BIT must be a multiple of eight.
87#endif
88
89typedef unsigned short fp_digit; /* 16 bits */
90typedef unsigned int fp_word; /* 32 bits */
91
92/* # of digits this is */
93#define DIGIT_BIT ((int)((CHAR_BIT) * sizeof(fp_digit)))
94
95/* Maximum Integer Precision in bits
96 * Basically the largest size you will be multiplying should be half [or smaller]
97 * of FP_MAX_SIZE-four_digit
98 * To support max 1024-bits computation need define:
99 */
100#define FP_MAX_SIZE (2048+(8*DIGIT_BIT))
101
102#define FP_MASK (fp_digit)(-1)
103#define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT)
104
105/* signs */
106#define FP_ZPOS 0
107#define FP_NEG 1
108
109/* return codes */
110#define FP_OKAY 0
111#define FP_VAL 1
112#define FP_MEM 2
113
114/* equalities */
115#define FP_LT -1 /* less than */
116#define FP_EQ 0 /* equal to */
117#define FP_GT 1 /* greater than */
118
119/* replies */
120#define FP_YES 1 /* yes response */
121#define FP_NO 0 /* no response */
122
123/* Enable Optimizing computations based on length of the 'big int'(->used):
124 Right now, not need them !
125 */
126/*#define TFM_MUL32
127#define TFM_SQR32 */
128
129/* do we want some overflow checks
130 Not required if you make sure your numbers are within range (e.g. by default
131 a modulus for fp_exptmod() can only be upto 2048 bits long)
132 */
133/* #define TFM_CHECK */
134
135/* this parameter decides the algorithm speed and the peak memory usage
136 because of the stack size limitation, this value can not be too large */
137#define MAXWINSIZE 1
138/*------------------------ data structure ------------------------- */
139
140/* a FP type */
141typedef struct {
142 fp_digit dp[FP_SIZE];
143 int used, sign;
144} fp_int;
145
146/*--------------------- functions and macros -----------------------*/
147
148/* initialize [or zero] an fp int */
149#define fp_init(a) (void)memset((a), 0, sizeof(fp_int))
150#define fp_zero(a) fp_init(a)
151
152/* zero/even/odd ? */
153#define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
154#define fp_iseven(a) (((a)->used >= 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO)
155#define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO)
156
157/* copy from a to b */
158#define fp_copy(a, b) (void)(((a) != (b)) && memcpy((b), (a), sizeof(fp_int)))
159#define fp_init_copy(a, b) fp_copy(b, a)
160
161/* negate and absolute */
162#define fp_neg(a, b) { fp_copy(a, b); (b)->sign ^= 1; }
163#define fp_abs(a, b) { fp_copy(a, b); (b)->sign = 0; }
164
165/* clamp digits */
166#define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
167
168
169/*-------------------------------public interface-----------------------------*/
170/* error codes [will be expanded in future releases] */
171enum {
172 CRYPT_OK=0, /* Result OK */
173 CRYPT_INVALID_ARG, /* Generic invalid argument */
174 CRYPT_MEM, /* Out of memory */
175
176 CRYPT_ERROR, /* Generic Error */
177 CRYPT_NOP, /* Not a failure but no operation was performed */
178 CRYPT_INVALID_KEYSIZE, /* Invalid key size given */
179 CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */
180 CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */
181 CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */
182 CRYPT_INVALID_PACKET, /* Invalid input packet given */
183 CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */
184 CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */
185 CRYPT_INVALID_CIPHER, /* Invalid cipher specified */
186 CRYPT_INVALID_HASH, /* Invalid hash specified */
187 CRYPT_INVALID_PRNG, /* Invalid PRNG specified */
188 CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */
189 CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */
190 CRYPT_FILE_NOTFOUND, /* File Not Found */
191 CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */
192 CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */
193 CRYPT_PK_DUP, /* Duplicate key already in key ring */
194 CRYPT_PK_NOT_FOUND, /* Key not found in keyring */
195 CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */
196 CRYPT_INVALID_PRIME_SIZE/* Invalid size of prime requested */
197};
198
199int rsa_exptmod(unsigned char *in, int inlen,
200 unsigned char *e, int elen,
201 unsigned char *n, int nlen,
202 unsigned char *out, int *outlen);
203int fp_unsigned_bin_size(fp_int *a);
204void fp_read_unsigned_bin(fp_int *a, unsigned char *b, int c);
205void fp_to_unsigned_bin(fp_int *a, unsigned char *b);
206
207#endif /*PSWRSA_H_*/
208
209
210