1/*-
2 * Copyright (C) 2001-2003 by NBMK Encryption Technologies.
3 * All rights reserved.
4 *
5 * NBMK Encryption Technologies provides no support of any kind for
6 * this software.  Questions or concerns about it may be addressed to
7 * the members of the relevant open-source community at
8 * <tech-crypto@netbsd.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
12 * met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above
18 *    copyright notice, this list of conditions and the following
19 *    disclaimer in the documentation and/or other materials provided
20 *    with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/*****************************************************************************
36 * @(#) n8_dsa.h 1.25@(#)
37 *****************************************************************************/
38
39/*****************************************************************************/
40/** @file n8_dsa.h
41 *  @brief Contains definitions for DSA functions.
42 *
43 *****************************************************************************/
44
45/*****************************************************************************
46 * Revision history:
47 * 05/19/03 brr   Clean up include files.
48 * 02/20/02 brr   Removed references to the queue structure.
49 * 01/29/02 bac   Renamed NUMBER_OF_BYTES_IN_VALUE to DSA_SIGN_LENGTH.
50 * 11/19/01 bac   Added n8_DSAValidateKey.
51 * 11/10/01 spm   Replaced use of ceil() with homemade integer version.
52 * 10/12/01 dkm   Moved public portion to n8_pub_common.h and n8_pub_pk.h.
53 * 10/01/01 hml   Added UnitID and SKSKeyHandle in the KeyObject and moved
54 *                the material struct in here.
55 * 09/10/01 bac   Test for public key and modulus lengths in
56 *                N8_DSAInitializeKey (BUG #129).  Added
57 *                N8_DSA_PUBLIC_KEY_LENGTH_[MIN|MAX] and
58 *                N8_DSA_PRIVATE_KEY_LENGTH.
59 * 08/16/01 mmd   Now including n8_driver_api.h instead of simon_driver_api.h.
60 * 07/30/01 bac   Fixed length calculation macros to accept and use a queue
61 *                control pointer.  Lots of logic changes to be seemlessly
62 *                portable to the FPGA.
63 * 06/28/01 bac   Minor typo fix.
64 * 06/25/01 bac   Lots of changes to affect functionality with the v1.0.1 QMgr
65 * 05/22/01 mel   Original version.
66 ****************************************************************************/
67#ifndef N8_DSA_H
68#define N8_DSA_H
69
70#include "n8_common.h"
71#include "n8_pub_pk.h"
72#include "n8_pk_common.h"
73#include "n8_pub_errors.h"
74#include "n8_driver_api.h"
75#include "n8_sks.h"
76
77#define DSA_SIGN_LENGTH 20
78
79/* Macros for extracting DSA parameters from the Big Number Cache.
80   These take key_length as a parameter.  Some will behave badly
81   if key_length is odd. */
82
83/* Several DSA quanties are of fixed length.  They must fit in the least number of BNC
84 * digits to ensure the uppermost digit is not zero.
85 */
86#define PK_DSA_N_Bit_Length                     160
87#define PK_DSA_E1_Bit_Length                    160
88#define PK_DSA_Q_Bit_Length                     160
89#define PK_DSA_R_Bit_Length                     160
90#define PK_DSA_S_Bit_Length                     160
91#define PK_DSA_X_Bit_Length                     160
92
93/* length of private key in bytes */
94#define N8_DSA_PRIVATE_KEY_LENGTH               20
95#define N8_DSA_PUBLIC_KEY_LENGTH_MIN            64
96#define N8_DSA_PUBLIC_KEY_LENGTH_MAX           512
97
98/* BNC Layout:
99 *  N                 160 bits (rounded up)
100 *  E1                160 bits (rounded up)
101 *  R                 160 bits (rounded up)
102 *  S                 160 bits (rounded up)
103 *  P                 key length
104 *  g R mod p         key length
105 *  Q                 160 bits (rounded up)
106 *  X                 160 bits (rounded up)
107 *  cp                1 digit
108 */
109#define PK_DSA_RoundUp(__L)                     (CEIL((__L) , (SIMON_BITS_PER_DIGIT)))
110
111#define PK_DSA_N_Byte_Length              PKDIGITS_TO_BYTES(PK_DSA_N_BNC_Length)
112#define PK_DSA_E1_Byte_Length             PKDIGITS_TO_BYTES(PK_DSA_E1_BNC_Length)
113#define PK_DSA_R_Byte_Length              PKDIGITS_TO_BYTES(PK_DSA_R_BNC_Length)
114#define PK_DSA_S_Byte_Length              PKDIGITS_TO_BYTES(PK_DSA_S_BNC_Length)
115
116#define PK_DSA_P_Byte_Length(__KL)        ((__KL)*PK_Bytes_Per_BigNum_Digit)
117#define PK_DSA_P_Param_Offset             (0)
118
119#define PK_DSA_G_Byte_Length(__KL)         ((__KL)*PK_Bytes_Per_BigNum_Digit)
120#define PK_DSA_GR_MOD_P_Byte_Length(__KL)  ((__KL)*PK_Bytes_Per_BigNum_Digit)
121#define PK_DSA_GR_MOD_P_Param_Offset(__KL) PKDIGITS_TO_BYTES(__KL)
122
123#define PK_DSA_INVK_Byte_Length           (2*PK_Bytes_Per_BigNum_Digit)
124#define PK_DSA_Q_Byte_Length              PKDIGITS_TO_BYTES(PK_DSA_Q_BNC_Length)
125#define PK_DSA_Q_Param_Offset(__KL)       PKDIGITS_TO_BYTES(2*(__KL))
126
127#define PK_DSA_X_Byte_Length              PKDIGITS_TO_BYTES(PK_DSA_X_BNC_Length)
128#define PK_DSA_X_Param_Offset(__KL)       PKDIGITS_TO_BYTES(2*(__KL)+PK_DSA_Q_BNC_Length)
129
130#define PK_DSA_Y_Byte_Length(__KL)         ((__KL)*PK_Bytes_Per_BigNum_Digit)
131
132#define PK_DSA_CP_Byte_Length             (PK_Bytes_Per_BigNum_Digit)
133#define PK_DSA_CP_Param_Offset(__KL)       \
134    (PK_DSA_X_Param_Offset((__KL)) + PK_DSA_X_Byte_Length)
135
136#define PK_DSA_Param_Byte_Length(__KL)      \
137    PK_DSA_P_Byte_Length((__KL)) +        \
138    PK_DSA_GR_MOD_P_Byte_Length((__KL)) + \
139    PK_DSA_Q_Byte_Length +               \
140    PK_DSA_X_Byte_Length +               \
141    PK_DSA_CP_Byte_Length
142
143
144#define PK_DSA_SKS_Word_Length(__KL)       (8*(__KL)+20)
145
146/* BNC Offsets */
147#define PK_DSA_N_BNC_Offset   (0)
148#define PK_DSA_E1_BNC_Offset  (PK_DSA_N_BNC_Offset +  PK_DSA_N_BNC_Length)
149#define PK_DSA_R_BNC_Offset   (PK_DSA_E1_BNC_Offset + PK_DSA_E1_BNC_Length)
150#define PK_DSA_S_BNC_Offset   (PK_DSA_R_BNC_Offset +  PK_DSA_R_BNC_Length)
151#define PK_DSA_P_BNC_Offset   (PK_DSA_S_BNC_Offset +  PK_DSA_S_BNC_Length)
152
153/* BNC Lengths */
154#define PK_DSA_N_BNC_Length   PK_DSA_RoundUp(PK_DSA_N_Bit_Length)
155#define PK_DSA_E1_BNC_Length  PK_DSA_RoundUp(PK_DSA_E1_Bit_Length)
156#define PK_DSA_R_BNC_Length   PK_DSA_RoundUp(PK_DSA_R_Bit_Length)
157#define PK_DSA_S_BNC_Length   PK_DSA_RoundUp(PK_DSA_S_Bit_Length)
158#define PK_DSA_Q_BNC_Length   PK_DSA_RoundUp(PK_DSA_Q_Bit_Length)
159#define PK_DSA_X_BNC_Length   PK_DSA_RoundUp(PK_DSA_X_Bit_Length)
160#define PK_DSA_P_BNC_Length(__KL)        (__KL)
161#define PK_DSA_G_BNC_Length(__KL)        (__KL)
162#define PK_DSA_GR_MOD_P_BNC_Length(__KL) (__KL)
163#define PK_DSA_Y_BNC_Length(__KL)        (__KL)
164#define PK_DSA_CP_BNC_Length              1
165
166typedef struct
167{
168    uint32_t    q;                /* secret prime number.  used for
169                                     private operations. */
170    uint32_t    cp;               /* additive inverse of the
171                                     multiplicative inverse of the least
172                                     significant digit of p, mod 2^128.
173                                     used for private operations. */
174    uint32_t    gR_mod_p;         /* g*R mod q.  (private) */
175    uint32_t    p;                /* secret prime number.  used for
176                                     private operations. */
177    uint32_t    privateKey;       /* private key x */
178    uint32_t    publicKey;        /* public key y */
179    uint32_t    g;                /* generator = h^(p-1)/q mod p, where h is less
180                                     than p - 1 and g > 1. */
181    uint32_t    paramBlock;       /* parameter block in PK format,
182                                     suitable for loading directly into
183                                     the Big Num Cache (BNC) */
184} N8_DSAKeyObjectPhysical_t;
185
186/**********************************************************************
187 * function prototypes
188 **********************************************************************/
189N8_Status_t n8_DSAValidateKey(const N8_DSAKeyMaterial_t *material_p,
190                              const N8_KeyType_t type);
191#endif /* N8_DSA_H */
192
193
194