1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _NANOHUB_RSA_H_
18 #define _NANOHUB_RSA_H_
19 
20 #include <stdint.h>
21 
22 #define RSA_LEN     2048
23 #define RSA_LIMBS   ((RSA_LEN + 31)/ 32)
24 #define RSA_BYTES   sizeof(uint32_t[RSA_LIMBS])
25 #define RSA_WORDS   (RSA_BYTES / sizeof(uint32_t)) //limbs may change in size, but words are always same :)
26 
27 struct RsaState {
28     uint32_t tmpA[RSA_LIMBS * 2];
29     uint32_t tmpB[RSA_LIMBS + 1];
30 
31 #if defined(RSA_SUPPORT_PRIV_OP_LOWRAM)
32     uint32_t tmpC[RSA_LIMBS + 1];
33 #elif defined (RSA_SUPPORT_PRIV_OP_BIGRAM)
34     uint32_t tmpC[RSA_LIMBS * 2];
35 #endif
36 };
37 
38 //calculate a ^ 65537 mod c, where a and c are each exactly RSA_LEN bits long, result is only valid as long as state is. state needs no init, set state to 0 at start, call till it is zero on return
39 const uint32_t* rsaPubOpIterative(struct RsaState* state, const uint32_t *a, const uint32_t *c, uint32_t *state1, uint32_t *state2, uint32_t *stepP);
40 
41 #if defined(RSA_SUPPORT_PRIV_OP_LOWRAM) || defined (RSA_SUPPORT_PRIV_OP_BIGRAM)
42 //calculate a ^ b mod c, where a and c are each exactly RSA_LEN bits long, result is only valid as long as state is. state needs no init
43 const uint32_t* rsaPrivOp(struct RsaState* state, const uint32_t *a, const uint32_t *b, const uint32_t *c);
44 const uint32_t* rsaPubOp(struct RsaState* state, const uint32_t *a, const uint32_t *c);
45 
46 #ifdef ARM
47 #error "RSA private ops must never be compiled into firmware."
48 #endif
49 
50 #endif
51 
52 
53 #endif // _NANOHUB_RSA_H_
54 
55