This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
package.scala
108 lines (83 loc) · 2.41 KB
/
package.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package tsec.cipher
import tsec.common.{ArrayHKNewt, ArrayNewt}
package object symmetric {
type Iv[A] = Iv.Type[A]
object Iv extends ArrayHKNewt
type RawCipherText[A] = RawCipherText.Type[A]
object RawCipherText extends ArrayHKNewt
type PlainText = PlainText.Type
object PlainText extends ArrayNewt
type AAD = AAD.Type
object AAD extends ArrayNewt
type AuthTag[A] = AuthTag.Type[A]
object AuthTag extends ArrayHKNewt
case class CipherText[A](content: RawCipherText[A], nonce: Iv[A]) {
def toConcatenated: Array[Byte] = content ++ nonce
}
private[tsec] val AESBlockSize = 16
/** Our general cipher type class,
* to carry cipher name information,
* block
*
* @tparam A
*/
trait Cipher[A] {
def cipherName: String
def keySizeBytes: Int
}
/** Our general typeclass over block ciphers
*
* @tparam A
*/
trait BlockCipher[A] extends Cipher[A] {
def blockSizeBytes: Int
}
/** Typeclass evidence that some type A
* is also an Authenticated Encryption Cipher
*
* It does not inherit from cipher, to
* simply exist as an evidence typeclass
*/
trait AECipher[A]
/** Typeclass evidence for a construction
* that serves as encryption for
* Authenticated encryption with Additional Data
*
*/
trait AEADCipher[A] extends AECipher[A] {
def tagSizeBytes: Int
}
object AEADCipher{
def apply[A](implicit A: AEADCipher[A]): AEADCipher[A] = A
}
/** In our implementation, we will use the most secure tag size as defined
* by: http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
* Iv length of 96 bits is recommended as per the spec on page 8
*/
val NISTTagLengthBits = 128
val NISTIvLengthBytes = 12
/** Our typeclass generalizing over AES,
* that lends itself to variable key sizes
* (128, 192 and 256 bits).
*
*/
trait AES[A] extends BlockCipher[A] with AEADCipher[A] {
val cipherName: String = "AES"
val blockSizeBytes: Int = 16
val tagSizeBytes: Int = NISTTagLengthBits / 8
}
object AES {
def apply[A](implicit a: AES[A]): AES[A] = a
val AES128KeySizeBytes = 16
val AES192KeySizeBytes = 24
val AES256KeySizeBytes = 32
}
/**
* This trait propagates type information
* about a parametrized M being a symmetric cipher mode of operation
* @tparam M
*/
trait CipherMode[M] {
def mode: String
}
}